Move butler methods from Session to Butler.
[ardour.git] / libs / ardour / session_butler.cc
index 2a217e5630db2cd8098339424f75a7be6e02310e..41b4851ce5a0112622255cedf9cf70176ffc2f2b 100644 (file)
@@ -47,9 +47,6 @@ using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
-static float _read_data_rate;
-static float _write_data_rate;
-
 /* XXX put this in the right place */
 
 static inline uint32_t next_power_of_two (uint32_t n)
@@ -68,326 +65,13 @@ static inline uint32_t next_power_of_two (uint32_t n)
  BUTLER THREAD
  ---------------------------------------------------------------------------*/
 
-int
-Session::start_butler_thread ()
-{
-       /* size is in Samples, not bytes */
-       audio_dstream_buffer_size = (uint32_t) floor (Config->get_audio_track_buffer_seconds() * (float) frame_rate());
-
-       /* size is in bytes
-        * XXX: Jack needs to tell us the MIDI buffer size
-        * (i.e. how many MIDI bytes we might see in a cycle)
-        */
-       midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * (float)frame_rate());
-       MidiDiskstream::set_readahead_frames ((nframes_t) (Config->get_midi_readahead() * (float) frame_rate()));
-
-       Crossfade::set_buffer_size (audio_dstream_buffer_size);
-
-       butler->should_run = false;
-
-       if (pipe (butler->request_pipe)) {
-               error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
-               return -1;
-       }
-
-       if (fcntl (butler->request_pipe[0], F_SETFL, O_NONBLOCK)) {
-               error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"), strerror (errno)) << endmsg;
-               return -1;
-       }
-
-       if (fcntl (butler->request_pipe[1], F_SETFL, O_NONBLOCK)) {
-               error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"), strerror (errno)) << endmsg;
-               return -1;
-       }
-
-       if (pthread_create_and_store ("disk butler", &butler->thread, 0, _butler_thread_work, this)) {
-               error << _("Session: could not create butler thread") << endmsg;
-               return -1;
-       }
-
-       // pthread_detach (butler->thread);
-
-       return 0;
-}
-
-void
-Session::terminate_butler_thread ()
-{
-       if (butler->thread) {
-               void* status;
-               char c = ButlerRequest::Quit;
-               ::write (butler->request_pipe[1], &c, 1);
-               pthread_join (butler->thread, &status);
-       }
-}
-
-void
-Session::schedule_butler_transport_work ()
-{
-       g_atomic_int_inc (&butler->should_do_transport_work);
-       summon_butler ();
-}
-
 void
 Session::schedule_curve_reallocation ()
 {
        post_transport_work = PostTransportWork (post_transport_work | PostTransportCurveRealloc);
-       schedule_butler_transport_work ();
-}
-
-void
-Session::summon_butler ()
-{
-       char c = ButlerRequest::Run;
-       ::write (butler->request_pipe[1], &c, 1);
-       // PBD::stacktrace (cerr);
-}
-
-void
-Session::stop_butler ()
-{
-       Glib::Mutex::Lock lm (butler->request_lock);
-       char c = ButlerRequest::Pause;
-       ::write (butler->request_pipe[1], &c, 1);
-       butler->paused.wait(butler->request_lock);
-}
-
-void
-Session::wait_till_butler_finished ()
-{
-       Glib::Mutex::Lock lm (butler->request_lock);
-       char c = ButlerRequest::Wake;
-       ::write (butler->request_pipe[1], &c, 1);
-       butler->paused.wait(butler->request_lock);
-}
-
-void *
-Session::_butler_thread_work (void* arg)
-{
-       PBD::notify_gui_about_thread_creation (pthread_self(), X_("Butler"));
-       return ((Session *) arg)->butler_thread_work ();
-       return 0;
-}
-
-void *
-Session::butler_thread_work ()
-{
-       uint32_t err = 0;
-       int32_t bytes;
-       bool compute_io;
-       microseconds_t begin, end;
-
-       struct pollfd pfd[1];
-       bool disk_work_outstanding = false;
-       DiskstreamList::iterator i;
-
-       while (true) {
-               pfd[0].fd = butler->request_pipe[0];
-               pfd[0].events = POLLIN|POLLERR|POLLHUP;
-
-               if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
-
-                       if (errno == EINTR) {
-                               continue;
-                       }
-
-                       error << string_compose (_("poll on butler request pipe failed (%1)"),
-                                         strerror (errno))
-                             << endmsg;
-                       break;
-               }
-
-               if (pfd[0].revents & ~POLLIN) {
-                       error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
-                       break;
-               }
-
-               if (pfd[0].revents & POLLIN) {
-
-                       char req;
-
-                       /* empty the pipe of all current requests */
-
-                       while (1) {
-                               size_t nread = ::read (butler->request_pipe[0], &req, sizeof (req));
-                               if (nread == 1) {
-
-                                       switch ((ButlerRequest::Type) req) {
-
-                                       case ButlerRequest::Wake:
-                                               break;
-
-                                       case ButlerRequest::Run:
-                                               butler->should_run = true;
-                                               break;
-
-                                       case ButlerRequest::Pause:
-                                               butler->should_run = false;
-                                               break;
-
-                                       case ButlerRequest::Quit:
-                                               pthread_exit_pbd (0);
-                                               /*NOTREACHED*/
-                                               break;
-
-                                       default:
-                                               break;
-                                       }
-
-                               } else if (nread == 0) {
-                                       break;
-                               } else if (errno == EAGAIN) {
-                                       break;
-                               } else {
-                                       fatal << _("Error reading from butler request pipe") << endmsg;
-                                       /*NOTREACHED*/
-                               }
-                       }
-               }
-
-               if (transport_work_requested()) {
-                       butler_transport_work ();
-               }
-
-               disk_work_outstanding = false;
-               bytes = 0;
-               compute_io = true;
-
-               begin = get_microseconds();
-
-               boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader ();
-
-//             for (i = dsl->begin(); i != dsl->end(); ++i) {
-//                     cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
-//             }
-
-               for (i = dsl->begin(); !transport_work_requested() && butler->should_run && i != dsl->end(); ++i) {
-
-                       boost::shared_ptr<Diskstream> ds = *i;
-
-                       /* don't read inactive tracks */
-
-                       boost::shared_ptr<IO> io = ds->io();
-
-                       if (io && !io->active()) {
-                               continue;
-                       }
-
-                       switch (ds->do_refill ()) {
-                       case 0:
-                               bytes += ds->read_data_count();
-                               break;
-                       case 1:
-                               bytes += ds->read_data_count();
-                               disk_work_outstanding = true;
-                               break;
-
-                       default:
-                               compute_io = false;
-                               error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
-                               break;
-                       }
-
-               }
-
-               if (i != dsl->end()) {
-                       /* we didn't get to all the streams */
-                       disk_work_outstanding = true;
-               }
-
-               if (!err && transport_work_requested()) {
-                       continue;
-               }
-
-               if (compute_io) {
-                       end = get_microseconds();
-                       if(end-begin > 0) {
-                       _read_data_rate = (float) bytes / (float) (end - begin);
-                       } else { _read_data_rate = 0; // infinity better
-                        }
-               }
-
-               bytes = 0;
-               compute_io = true;
-               begin = get_microseconds();
-
-               for (i = dsl->begin(); !transport_work_requested() && butler->should_run && i != dsl->end(); ++i) {
-                       // cerr << "write behind for " << (*i)->name () << endl;
-
-                       /* note that we still try to flush diskstreams attached to inactive routes
-                        */
-
-                       switch ((*i)->do_flush (ButlerContext)) {
-                       case 0:
-                               bytes += (*i)->write_data_count();
-                               break;
-                       case 1:
-                               bytes += (*i)->write_data_count();
-                               disk_work_outstanding = true;
-                               break;
-
-                       default:
-                               err++;
-                               compute_io = false;
-                               error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
-                               /* don't break - try to flush all streams in case they
-                                  are split across disks.
-                               */
-                       }
-               }
-
-               if (err && actively_recording()) {
-                       /* stop the transport and try to catch as much possible
-                          captured state as we can.
-                       */
-                       request_stop ();
-               }
-
-               if (i != dsl->end()) {
-                       /* we didn't get to all the streams */
-                       disk_work_outstanding = true;
-               }
-
-               if (!err && transport_work_requested()) {
-                       continue;
-               }
-
-               if (compute_io) {
-                       // there are no apparent users for this calculation?
-                       end = get_microseconds();
-                       if(end-begin > 0) {
-                       _write_data_rate = (float) bytes / (float) (end - begin);
-                       } else {
-                       _write_data_rate = 0; // Well, infinity would be better
-                       }
-               }
-
-               if (!disk_work_outstanding) {
-                       refresh_disk_space ();
-               }
-
-
-               {
-                       Glib::Mutex::Lock lm (butler->request_lock);
-
-                       if (butler->should_run && (disk_work_outstanding || transport_work_requested())) {
-//                             for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
-//                                     cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
-//                             }
-
-                               continue;
-                       }
-
-                       butler->paused.signal();
-               }
-       }
-
-       pthread_exit_pbd (0);
-       /*NOTREACHED*/
-       return (0);
+       _butler->schedule_transport_work ();
 }
 
-
 void
 Session::request_overwrite_buffer (Diskstream* stream)
 {
@@ -417,25 +101,7 @@ Session::overwrite_some_buffers (Diskstream* ds)
        }
 
        post_transport_work = PostTransportWork (post_transport_work | PostTransportOverWrite);
-       schedule_butler_transport_work ();
-}
-
-float
-Session::read_data_rate () const
-{
-       /* disk i/o in excess of 10000MB/sec indicate the buffer cache
-          in action. ignore it.
-       */
-       return _read_data_rate > 10485.7600000f ? 0.0f : _read_data_rate;
-}
-
-float
-Session::write_data_rate () const
-{
-       /* disk i/o in excess of 10000MB/sec indicate the buffer cache
-          in action. ignore it.
-       */
-       return _write_data_rate > 10485.7600000f ? 0.0f : _write_data_rate;
+       _butler->schedule_transport_work ();
 }
 
 uint32_t
@@ -474,8 +140,3 @@ Session::reset_playback_load_min ()
        g_atomic_int_set (&_playback_load_min, 100);
 }
 
-bool
-Session::transport_work_requested () const
-{
-       return g_atomic_int_get(&butler->should_do_transport_work);
-}