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)
44 , audio_dstream_buffer_size(0)
45 , midi_dstream_buffer_size(0)
47 g_atomic_int_set(&should_do_transport_work, 0);
55 Butler::start_thread()
57 const float rate = (float)session->frame_rate();
59 /* size is in Samples, not bytes */
60 audio_dstream_buffer_size = (uint32_t) floor (Config->get_audio_track_buffer_seconds() * rate);
63 * XXX: Jack needs to tell us the MIDI buffer size
64 * (i.e. how many MIDI bytes we might see in a cycle)
66 midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
68 MidiDiskstream::set_readahead_frames ((nframes_t)(Config->get_midi_readahead() * rate));
70 Crossfade::set_buffer_size (audio_dstream_buffer_size);
74 if (pipe (request_pipe)) {
75 error << string_compose(_("Cannot create transport request signal pipe (%1)"),
76 strerror (errno)) << endmsg;
80 if (fcntl (request_pipe[0], F_SETFL, O_NONBLOCK)) {
81 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
82 strerror (errno)) << endmsg;
86 if (fcntl (request_pipe[1], F_SETFL, O_NONBLOCK)) {
87 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
88 strerror (errno)) << endmsg;
92 if (pthread_create_and_store ("disk butler", &thread, 0, _thread_work, this)) {
93 error << _("Session: could not create butler thread") << endmsg;
97 //pthread_detach (thread);
103 Butler::terminate_thread ()
107 const char c = Request::Quit;
108 ::write (request_pipe[1], &c, 1);
109 pthread_join (thread, &status);
114 Butler::_thread_work (void* arg)
116 PBD::notify_gui_about_thread_creation (pthread_self(), X_("Butler"));
117 return ((Butler *) arg)->thread_work ();
121 Butler::thread_work ()
126 microseconds_t begin, end;
128 struct pollfd pfd[1];
129 bool disk_work_outstanding = false;
130 Session::DiskstreamList::iterator i;
133 pfd[0].fd = request_pipe[0];
134 pfd[0].events = POLLIN|POLLERR|POLLHUP;
136 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
138 if (errno == EINTR) {
142 error << string_compose (_("poll on butler request pipe failed (%1)"),
148 if (pfd[0].revents & ~POLLIN) {
149 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
153 if (pfd[0].revents & POLLIN) {
157 /* empty the pipe of all current requests */
160 size_t nread = ::read (request_pipe[0], &req, sizeof (req));
163 switch ((Request::Type) req) {
177 pthread_exit_pbd (0);
185 } else if (nread == 0) {
187 } else if (errno == EAGAIN) {
190 fatal << _("Error reading from butler request pipe") << endmsg;
196 if (transport_work_requested()) {
197 session->butler_transport_work ();
200 disk_work_outstanding = false;
204 begin = get_microseconds();
206 boost::shared_ptr<Session::DiskstreamList> dsl = session->diskstream_list().reader ();
208 // for (i = dsl->begin(); i != dsl->end(); ++i) {
209 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
212 for (i = dsl->begin(); !transport_work_requested() && should_run && i != dsl->end(); ++i) {
214 boost::shared_ptr<Diskstream> ds = *i;
216 /* don't read inactive tracks */
218 boost::shared_ptr<IO> io = ds->io();
220 if (io && !io->active()) {
224 switch (ds->do_refill ()) {
226 bytes += ds->read_data_count();
229 bytes += ds->read_data_count();
230 disk_work_outstanding = true;
235 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
241 if (i != dsl->end()) {
242 /* we didn't get to all the streams */
243 disk_work_outstanding = true;
246 if (!err && transport_work_requested()) {
251 end = get_microseconds();
252 if (end - begin > 0) {
253 _read_data_rate = (float) bytes / (float) (end - begin);
255 _read_data_rate = 0; // infinity better
261 begin = get_microseconds();
263 for (i = dsl->begin(); !transport_work_requested() && should_run && i != dsl->end(); ++i) {
264 // cerr << "write behind for " << (*i)->name () << endl;
266 /* note that we still try to flush diskstreams attached to inactive routes
269 switch ((*i)->do_flush (ButlerContext)) {
271 bytes += (*i)->write_data_count();
274 bytes += (*i)->write_data_count();
275 disk_work_outstanding = true;
281 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
282 /* don't break - try to flush all streams in case they
283 are split across disks.
288 if (err && session->actively_recording()) {
289 /* stop the transport and try to catch as much possible
290 captured state as we can.
292 session->request_stop ();
295 if (i != dsl->end()) {
296 /* we didn't get to all the streams */
297 disk_work_outstanding = true;
300 if (!err && transport_work_requested()) {
305 // there are no apparent users for this calculation?
306 end = get_microseconds();
308 _write_data_rate = (float) bytes / (float) (end - begin);
310 _write_data_rate = 0; // Well, infinity would be better
314 if (!disk_work_outstanding) {
315 session->refresh_disk_space ();
320 Glib::Mutex::Lock lm (request_lock);
322 if (should_run && (disk_work_outstanding || transport_work_requested())) {
323 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
324 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
334 pthread_exit_pbd (0);
340 Butler::schedule_transport_work ()
342 g_atomic_int_inc (&should_do_transport_work);
349 char c = Request::Run;
350 ::write (request_pipe[1], &c, 1);
356 Glib::Mutex::Lock lm (request_lock);
357 char c = Request::Pause;
358 ::write (request_pipe[1], &c, 1);
359 paused.wait(request_lock);
363 Butler::wait_until_finished ()
365 Glib::Mutex::Lock lm (request_lock);
366 char c = Request::Wake;
367 ::write (request_pipe[1], &c, 1);
368 paused.wait(request_lock);
372 Butler::transport_work_requested () const
374 return g_atomic_int_get(&should_do_transport_work);
378 Butler::read_data_rate () const
380 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
381 in action. ignore it.
383 return _read_data_rate > 10485.7600000f ? 0.0f : _read_data_rate;
387 Butler::write_data_rate () const
389 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
390 in action. ignore it.
392 return _write_data_rate > 10485.7600000f ? 0.0f : _write_data_rate;
395 } // namespace ARDOUR