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)
48 g_atomic_int_set(&should_do_transport_work, 0);
49 SessionEvent::pool->set_trash (&pool_trash);
58 Butler::start_thread()
60 const float rate = (float)_session.frame_rate();
62 /* size is in Samples, not bytes */
63 audio_dstream_buffer_size = (uint32_t) floor (Config->get_audio_track_buffer_seconds() * rate);
66 * XXX: Jack needs to tell us the MIDI buffer size
67 * (i.e. how many MIDI bytes we might see in a cycle)
69 midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
71 MidiDiskstream::set_readahead_frames ((nframes_t)(Config->get_midi_readahead() * rate));
73 Crossfade::set_buffer_size (audio_dstream_buffer_size);
77 if (pipe (request_pipe)) {
78 error << string_compose(_("Cannot create transport request signal pipe (%1)"),
79 strerror (errno)) << endmsg;
83 if (fcntl (request_pipe[0], F_SETFL, O_NONBLOCK)) {
84 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
85 strerror (errno)) << endmsg;
89 if (fcntl (request_pipe[1], F_SETFL, O_NONBLOCK)) {
90 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
91 strerror (errno)) << endmsg;
95 if (pthread_create_and_store ("disk butler", &thread, _thread_work, this)) {
96 error << _("Session: could not create butler thread") << endmsg;
100 //pthread_detach (thread);
106 Butler::terminate_thread ()
110 const char c = Request::Quit;
111 ::write (request_pipe[1], &c, 1);
112 pthread_join (thread, &status);
117 Butler::_thread_work (void* arg)
119 SessionEvent::create_per_thread_pool ("butler events", 64);
120 pthread_set_name (X_("butler"));
121 return ((Butler *) arg)->thread_work ();
125 Butler::thread_work ()
130 microseconds_t begin, end;
132 struct pollfd pfd[1];
133 bool disk_work_outstanding = false;
134 Session::DiskstreamList::iterator i;
137 pfd[0].fd = request_pipe[0];
138 pfd[0].events = POLLIN|POLLERR|POLLHUP;
140 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
142 if (errno == EINTR) {
146 error << string_compose (_("poll on butler request pipe failed (%1)"),
152 if (pfd[0].revents & ~POLLIN) {
153 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
157 if (pfd[0].revents & POLLIN) {
161 /* empty the pipe of all current requests */
164 size_t nread = ::read (request_pipe[0], &req, sizeof (req));
167 switch ((Request::Type) req) {
181 pthread_exit_pbd (0);
189 } else if (nread == 0) {
191 } else if (errno == EAGAIN) {
194 fatal << _("Error reading from butler request pipe") << endmsg;
200 if (transport_work_requested()) {
201 _session.butler_transport_work ();
204 disk_work_outstanding = false;
208 begin = get_microseconds();
210 boost::shared_ptr<Session::DiskstreamList> dsl = _session.diskstream_list().reader ();
212 // for (i = dsl->begin(); i != dsl->end(); ++i) {
213 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
216 for (i = dsl->begin(); !transport_work_requested() && should_run && i != dsl->end(); ++i) {
218 boost::shared_ptr<Diskstream> ds = *i;
220 /* don't read inactive tracks */
222 boost::shared_ptr<IO> io = ds->io();
224 if (io && !io->active()) {
228 switch (ds->do_refill ()) {
230 bytes += ds->read_data_count();
233 bytes += ds->read_data_count();
234 disk_work_outstanding = true;
239 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
245 if (i != dsl->begin() && i != dsl->end()) {
246 /* we didn't get to all the streams */
247 disk_work_outstanding = true;
250 if (!err && transport_work_requested()) {
255 end = get_microseconds();
256 if (end - begin > 0) {
257 _read_data_rate = (float) bytes / (float) (end - begin);
259 _read_data_rate = 0; // infinity better
265 begin = get_microseconds();
267 for (i = dsl->begin(); !transport_work_requested() && should_run && i != dsl->end(); ++i) {
268 // cerr << "write behind for " << (*i)->name () << endl;
270 /* note that we still try to flush diskstreams attached to inactive routes
273 switch ((*i)->do_flush (ButlerContext)) {
275 bytes += (*i)->write_data_count();
278 bytes += (*i)->write_data_count();
279 disk_work_outstanding = true;
285 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
286 /* don't break - try to flush all streams in case they
287 are split across disks.
292 if (err && _session.actively_recording()) {
293 /* stop the transport and try to catch as much possible
294 captured state as we can.
296 _session.request_stop ();
299 if (i != dsl->begin() && i != dsl->end()) {
300 /* we didn't get to all the streams */
301 disk_work_outstanding = true;
304 if (!err && transport_work_requested()) {
309 // there are no apparent users for this calculation?
310 end = get_microseconds();
311 if (end - begin > 0) {
312 _write_data_rate = (float) bytes / (float) (end - begin);
314 _write_data_rate = 0; // Well, infinity would be better
318 if (!disk_work_outstanding) {
319 _session.refresh_disk_space ();
324 Glib::Mutex::Lock lm (request_lock);
326 if (should_run && (disk_work_outstanding || transport_work_requested())) {
327 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
328 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
340 pthread_exit_pbd (0);
346 Butler::schedule_transport_work ()
348 g_atomic_int_inc (&should_do_transport_work);
355 char c = Request::Run;
356 ::write (request_pipe[1], &c, 1);
362 Glib::Mutex::Lock lm (request_lock);
363 char c = Request::Pause;
364 ::write (request_pipe[1], &c, 1);
365 paused.wait(request_lock);
369 Butler::wait_until_finished ()
371 Glib::Mutex::Lock lm (request_lock);
372 char c = Request::Wake;
373 ::write (request_pipe[1], &c, 1);
374 paused.wait(request_lock);
378 Butler::transport_work_requested () const
380 return g_atomic_int_get(&should_do_transport_work);
384 Butler::read_data_rate () const
386 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
387 in action. ignore it.
389 return _read_data_rate > 10485.7600000f ? 0.0f : _read_data_rate;
393 Butler::write_data_rate () const
395 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
396 in action. ignore it.
398 return _write_data_rate > 10485.7600000f ? 0.0f : _write_data_rate;
402 Butler::empty_pool_trash ()
404 /* look in the trash, deleting empty pools until we come to one that is not empty */
406 RingBuffer<CrossThreadPool*>::rw_vector vec;
407 pool_trash.get_read_vector (&vec);
411 for (int i = 0; i < 2; ++i) {
412 for (guint j = 0; j < vec.len[i]; ++j) {
413 if (vec.buf[i][j]->empty()) {
414 delete vec.buf[i][j];
417 /* found a non-empty pool, so stop deleting */
419 pool_trash.increment_read_idx (deleted);
427 pool_trash.increment_read_idx (deleted);
431 } // namespace ARDOUR