2 Copyright (C) 1999-2009 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "pbd/error.h"
25 #include "pbd/pthread_utils.h"
26 #include "ardour/butler.h"
27 #include "ardour/crossfade.h"
28 #include "ardour/io.h"
29 #include "ardour/midi_diskstream.h"
30 #include "ardour/session.h"
36 static float _read_data_rate;
37 static float _write_data_rate;
41 Butler::Butler(Session& s)
42 : SessionHandleRef (s)
44 , audio_dstream_buffer_size(0)
45 , midi_dstream_buffer_size(0)
47 g_atomic_int_set(&should_do_transport_work, 0);
56 Butler::start_thread()
58 const float rate = (float)_session.frame_rate();
60 /* size is in Samples, not bytes */
61 audio_dstream_buffer_size = (uint32_t) floor (Config->get_audio_track_buffer_seconds() * rate);
64 * XXX: Jack needs to tell us the MIDI buffer size
65 * (i.e. how many MIDI bytes we might see in a cycle)
67 midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
69 MidiDiskstream::set_readahead_frames ((nframes_t)(Config->get_midi_readahead() * rate));
71 Crossfade::set_buffer_size (audio_dstream_buffer_size);
75 if (pipe (request_pipe)) {
76 error << string_compose(_("Cannot create transport request signal pipe (%1)"),
77 strerror (errno)) << endmsg;
81 if (fcntl (request_pipe[0], F_SETFL, O_NONBLOCK)) {
82 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
83 strerror (errno)) << endmsg;
87 if (fcntl (request_pipe[1], F_SETFL, O_NONBLOCK)) {
88 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
89 strerror (errno)) << endmsg;
93 if (pthread_create_and_store ("disk butler", &thread, _thread_work, this)) {
94 error << _("Session: could not create butler thread") << endmsg;
98 //pthread_detach (thread);
104 Butler::terminate_thread ()
108 const char c = Request::Quit;
109 ::write (request_pipe[1], &c, 1);
110 pthread_join (thread, &status);
115 Butler::_thread_work (void* arg)
117 SessionEvent::create_per_thread_pool ("butler events", 64);
118 pthread_set_name (X_("butler"));
119 return ((Butler *) arg)->thread_work ();
123 Butler::thread_work ()
128 microseconds_t begin, end;
130 struct pollfd pfd[1];
131 bool disk_work_outstanding = false;
132 Session::DiskstreamList::iterator i;
135 pfd[0].fd = request_pipe[0];
136 pfd[0].events = POLLIN|POLLERR|POLLHUP;
138 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
140 if (errno == EINTR) {
144 error << string_compose (_("poll on butler request pipe failed (%1)"),
150 if (pfd[0].revents & ~POLLIN) {
151 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
155 if (pfd[0].revents & POLLIN) {
159 /* empty the pipe of all current requests */
162 size_t nread = ::read (request_pipe[0], &req, sizeof (req));
165 switch ((Request::Type) req) {
179 pthread_exit_pbd (0);
187 } else if (nread == 0) {
189 } else if (errno == EAGAIN) {
192 fatal << _("Error reading from butler request pipe") << endmsg;
198 if (transport_work_requested()) {
199 _session.butler_transport_work ();
202 disk_work_outstanding = false;
206 begin = get_microseconds();
208 boost::shared_ptr<Session::DiskstreamList> dsl = _session.diskstream_list().reader ();
210 // for (i = dsl->begin(); i != dsl->end(); ++i) {
211 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
214 for (i = dsl->begin(); !transport_work_requested() && should_run && i != dsl->end(); ++i) {
216 boost::shared_ptr<Diskstream> ds = *i;
218 /* don't read inactive tracks */
220 boost::shared_ptr<IO> io = ds->io();
222 if (io && !io->active()) {
226 switch (ds->do_refill ()) {
228 bytes += ds->read_data_count();
231 bytes += ds->read_data_count();
232 disk_work_outstanding = true;
237 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
243 if (i != dsl->end()) {
244 /* we didn't get to all the streams */
245 disk_work_outstanding = true;
248 if (!err && transport_work_requested()) {
253 end = get_microseconds();
254 if (end - begin > 0) {
255 _read_data_rate = (float) bytes / (float) (end - begin);
257 _read_data_rate = 0; // infinity better
263 begin = get_microseconds();
265 for (i = dsl->begin(); !transport_work_requested() && should_run && i != dsl->end(); ++i) {
266 // cerr << "write behind for " << (*i)->name () << endl;
268 /* note that we still try to flush diskstreams attached to inactive routes
271 switch ((*i)->do_flush (ButlerContext)) {
273 bytes += (*i)->write_data_count();
276 bytes += (*i)->write_data_count();
277 disk_work_outstanding = true;
283 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
284 /* don't break - try to flush all streams in case they
285 are split across disks.
290 if (err && _session.actively_recording()) {
291 /* stop the transport and try to catch as much possible
292 captured state as we can.
294 _session.request_stop ();
297 if (i != dsl->end()) {
298 /* we didn't get to all the streams */
299 disk_work_outstanding = true;
302 if (!err && transport_work_requested()) {
307 // there are no apparent users for this calculation?
308 end = get_microseconds();
309 if (end - begin > 0) {
310 _write_data_rate = (float) bytes / (float) (end - begin);
312 _write_data_rate = 0; // Well, infinity would be better
316 if (!disk_work_outstanding) {
317 _session.refresh_disk_space ();
322 Glib::Mutex::Lock lm (request_lock);
324 if (should_run && (disk_work_outstanding || transport_work_requested())) {
325 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
326 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
336 pthread_exit_pbd (0);
342 Butler::schedule_transport_work ()
344 g_atomic_int_inc (&should_do_transport_work);
351 char c = Request::Run;
352 ::write (request_pipe[1], &c, 1);
358 Glib::Mutex::Lock lm (request_lock);
359 char c = Request::Pause;
360 ::write (request_pipe[1], &c, 1);
361 paused.wait(request_lock);
365 Butler::wait_until_finished ()
367 Glib::Mutex::Lock lm (request_lock);
368 char c = Request::Wake;
369 ::write (request_pipe[1], &c, 1);
370 paused.wait(request_lock);
374 Butler::transport_work_requested () const
376 return g_atomic_int_get(&should_do_transport_work);
380 Butler::read_data_rate () const
382 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
383 in action. ignore it.
385 return _read_data_rate > 10485.7600000f ? 0.0f : _read_data_rate;
389 Butler::write_data_rate () const
391 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
392 in action. ignore it.
394 return _write_data_rate > 10485.7600000f ? 0.0f : _write_data_rate;
397 } // namespace ARDOUR