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 return ((Butler *) arg)->thread_work ();
122 Butler::thread_work ()
127 microseconds_t begin, end;
129 struct pollfd pfd[1];
130 bool disk_work_outstanding = false;
131 Session::DiskstreamList::iterator i;
134 pfd[0].fd = request_pipe[0];
135 pfd[0].events = POLLIN|POLLERR|POLLHUP;
137 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
139 if (errno == EINTR) {
143 error << string_compose (_("poll on butler request pipe failed (%1)"),
149 if (pfd[0].revents & ~POLLIN) {
150 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
154 if (pfd[0].revents & POLLIN) {
158 /* empty the pipe of all current requests */
161 size_t nread = ::read (request_pipe[0], &req, sizeof (req));
164 switch ((Request::Type) req) {
178 pthread_exit_pbd (0);
186 } else if (nread == 0) {
188 } else if (errno == EAGAIN) {
191 fatal << _("Error reading from butler request pipe") << endmsg;
197 if (transport_work_requested()) {
198 _session.butler_transport_work ();
201 disk_work_outstanding = false;
205 begin = get_microseconds();
207 boost::shared_ptr<Session::DiskstreamList> dsl = _session.diskstream_list().reader ();
209 // for (i = dsl->begin(); i != dsl->end(); ++i) {
210 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
213 for (i = dsl->begin(); !transport_work_requested() && should_run && i != dsl->end(); ++i) {
215 boost::shared_ptr<Diskstream> ds = *i;
217 /* don't read inactive tracks */
219 boost::shared_ptr<IO> io = ds->io();
221 if (io && !io->active()) {
225 switch (ds->do_refill ()) {
227 bytes += ds->read_data_count();
230 bytes += ds->read_data_count();
231 disk_work_outstanding = true;
236 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
242 if (i != dsl->end()) {
243 /* we didn't get to all the streams */
244 disk_work_outstanding = true;
247 if (!err && transport_work_requested()) {
252 end = get_microseconds();
253 if (end - begin > 0) {
254 _read_data_rate = (float) bytes / (float) (end - begin);
256 _read_data_rate = 0; // infinity better
262 begin = get_microseconds();
264 for (i = dsl->begin(); !transport_work_requested() && should_run && i != dsl->end(); ++i) {
265 // cerr << "write behind for " << (*i)->name () << endl;
267 /* note that we still try to flush diskstreams attached to inactive routes
270 switch ((*i)->do_flush (ButlerContext)) {
272 bytes += (*i)->write_data_count();
275 bytes += (*i)->write_data_count();
276 disk_work_outstanding = true;
282 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
283 /* don't break - try to flush all streams in case they
284 are split across disks.
289 if (err && _session.actively_recording()) {
290 /* stop the transport and try to catch as much possible
291 captured state as we can.
293 _session.request_stop ();
296 if (i != dsl->end()) {
297 /* we didn't get to all the streams */
298 disk_work_outstanding = true;
301 if (!err && transport_work_requested()) {
306 // there are no apparent users for this calculation?
307 end = get_microseconds();
308 if (end - begin > 0) {
309 _write_data_rate = (float) bytes / (float) (end - begin);
311 _write_data_rate = 0; // Well, infinity would be better
315 if (!disk_work_outstanding) {
316 _session.refresh_disk_space ();
321 Glib::Mutex::Lock lm (request_lock);
323 if (should_run && (disk_work_outstanding || transport_work_requested())) {
324 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
325 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
335 pthread_exit_pbd (0);
341 Butler::schedule_transport_work ()
343 g_atomic_int_inc (&should_do_transport_work);
350 char c = Request::Run;
351 ::write (request_pipe[1], &c, 1);
357 Glib::Mutex::Lock lm (request_lock);
358 char c = Request::Pause;
359 ::write (request_pipe[1], &c, 1);
360 paused.wait(request_lock);
364 Butler::wait_until_finished ()
366 Glib::Mutex::Lock lm (request_lock);
367 char c = Request::Wake;
368 ::write (request_pipe[1], &c, 1);
369 paused.wait(request_lock);
373 Butler::transport_work_requested () const
375 return g_atomic_int_get(&should_do_transport_work);
379 Butler::read_data_rate () const
381 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
382 in action. ignore it.
384 return _read_data_rate > 10485.7600000f ? 0.0f : _read_data_rate;
388 Butler::write_data_rate () const
390 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
391 in action. ignore it.
393 return _write_data_rate > 10485.7600000f ? 0.0f : _write_data_rate;
396 } // namespace ARDOUR