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"
31 #include "ardour/track.h"
37 static float _read_data_rate;
38 static float _write_data_rate;
42 Butler::Butler(Session& s)
43 : SessionHandleRef (s)
45 , audio_dstream_buffer_size(0)
46 , midi_dstream_buffer_size(0)
49 g_atomic_int_set(&should_do_transport_work, 0);
50 SessionEvent::pool->set_trash (&pool_trash);
59 Butler::start_thread()
61 const float rate = (float)_session.frame_rate();
63 /* size is in Samples, not bytes */
64 audio_dstream_buffer_size = (uint32_t) floor (Config->get_audio_track_buffer_seconds() * rate);
67 * XXX: Jack needs to tell us the MIDI buffer size
68 * (i.e. how many MIDI bytes we might see in a cycle)
70 midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
72 MidiDiskstream::set_readahead_frames ((nframes_t)(Config->get_midi_readahead() * rate));
74 Crossfade::set_buffer_size (audio_dstream_buffer_size);
78 if (pipe (request_pipe)) {
79 error << string_compose(_("Cannot create transport request signal pipe (%1)"),
80 strerror (errno)) << endmsg;
84 if (fcntl (request_pipe[0], F_SETFL, O_NONBLOCK)) {
85 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
86 strerror (errno)) << endmsg;
90 if (fcntl (request_pipe[1], F_SETFL, O_NONBLOCK)) {
91 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
92 strerror (errno)) << endmsg;
96 if (pthread_create_and_store ("disk butler", &thread, _thread_work, this)) {
97 error << _("Session: could not create butler thread") << endmsg;
101 //pthread_detach (thread);
107 Butler::terminate_thread ()
111 const char c = Request::Quit;
112 ::write (request_pipe[1], &c, 1);
113 pthread_join (thread, &status);
118 Butler::_thread_work (void* arg)
120 SessionEvent::create_per_thread_pool ("butler events", 64);
121 pthread_set_name (X_("butler"));
122 return ((Butler *) arg)->thread_work ();
126 Butler::thread_work ()
131 microseconds_t begin, end;
133 struct pollfd pfd[1];
134 bool disk_work_outstanding = false;
135 RouteList::iterator i;
138 pfd[0].fd = request_pipe[0];
139 pfd[0].events = POLLIN|POLLERR|POLLHUP;
141 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
143 if (errno == EINTR) {
147 error << string_compose (_("poll on butler request pipe failed (%1)"),
153 if (pfd[0].revents & ~POLLIN) {
154 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
158 if (pfd[0].revents & POLLIN) {
162 /* empty the pipe of all current requests */
165 size_t nread = ::read (request_pipe[0], &req, sizeof (req));
168 switch ((Request::Type) req) {
182 pthread_exit_pbd (0);
190 } else if (nread == 0) {
192 } else if (errno == EAGAIN) {
195 fatal << _("Error reading from butler request pipe") << endmsg;
201 if (transport_work_requested()) {
202 _session.butler_transport_work ();
205 disk_work_outstanding = false;
209 begin = get_microseconds();
211 boost::shared_ptr<RouteList> rl = _session.get_routes();
213 // for (i = dsl->begin(); i != dsl->end(); ++i) {
214 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
217 for (i = rl->begin(); !transport_work_requested() && should_run && i != rl->end(); ++i) {
219 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
224 /* don't read inactive tracks */
226 boost::shared_ptr<IO> io = tr->input ();
228 if (io && !io->active()) {
232 switch (tr->do_refill ()) {
234 bytes += tr->read_data_count();
237 bytes += tr->read_data_count();
238 disk_work_outstanding = true;
243 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
249 if (i != rl->begin() && i != rl->end()) {
250 /* we didn't get to all the streams */
251 disk_work_outstanding = true;
254 if (!err && transport_work_requested()) {
259 end = get_microseconds();
260 if (end - begin > 0) {
261 _read_data_rate = (float) bytes / (float) (end - begin);
263 _read_data_rate = 0; // infinity better
269 begin = get_microseconds();
271 for (i = rl->begin(); !transport_work_requested() && should_run && i != rl->end(); ++i) {
272 // cerr << "write behind for " << (*i)->name () << endl;
274 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
279 /* note that we still try to flush diskstreams attached to inactive routes
282 switch (tr->do_flush (ButlerContext)) {
284 bytes += tr->write_data_count();
287 bytes += tr->write_data_count();
288 disk_work_outstanding = true;
294 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
295 /* don't break - try to flush all streams in case they
296 are split across disks.
301 if (err && _session.actively_recording()) {
302 /* stop the transport and try to catch as much possible
303 captured state as we can.
305 _session.request_stop ();
308 if (i != rl->begin() && i != rl->end()) {
309 /* we didn't get to all the streams */
310 disk_work_outstanding = true;
313 if (!err && transport_work_requested()) {
318 // there are no apparent users for this calculation?
319 end = get_microseconds();
320 if (end - begin > 0) {
321 _write_data_rate = (float) bytes / (float) (end - begin);
323 _write_data_rate = 0; // Well, infinity would be better
327 if (!disk_work_outstanding) {
328 _session.refresh_disk_space ();
333 Glib::Mutex::Lock lm (request_lock);
335 if (should_run && (disk_work_outstanding || transport_work_requested())) {
336 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
337 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
349 pthread_exit_pbd (0);
355 Butler::schedule_transport_work ()
357 g_atomic_int_inc (&should_do_transport_work);
364 char c = Request::Run;
365 ::write (request_pipe[1], &c, 1);
371 Glib::Mutex::Lock lm (request_lock);
372 char c = Request::Pause;
373 ::write (request_pipe[1], &c, 1);
374 paused.wait(request_lock);
378 Butler::wait_until_finished ()
380 Glib::Mutex::Lock lm (request_lock);
381 char c = Request::Wake;
382 ::write (request_pipe[1], &c, 1);
383 paused.wait(request_lock);
387 Butler::transport_work_requested () const
389 return g_atomic_int_get(&should_do_transport_work);
393 Butler::read_data_rate () const
395 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
396 in action. ignore it.
398 return _read_data_rate > 10485.7600000f ? 0.0f : _read_data_rate;
402 Butler::write_data_rate () const
404 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
405 in action. ignore it.
407 return _write_data_rate > 10485.7600000f ? 0.0f : _write_data_rate;
411 Butler::empty_pool_trash ()
413 /* look in the trash, deleting empty pools until we come to one that is not empty */
415 RingBuffer<CrossThreadPool*>::rw_vector vec;
416 pool_trash.get_read_vector (&vec);
420 for (int i = 0; i < 2; ++i) {
421 for (guint j = 0; j < vec.len[i]; ++j) {
422 if (vec.buf[i][j]->empty()) {
423 delete vec.buf[i][j];
426 /* found a non-empty pool, so stop deleting */
428 pool_trash.increment_read_idx (deleted);
436 pool_trash.increment_read_idx (deleted);
441 Butler::drop_references ()
443 SessionEvent::pool->set_trash (0);
447 } // namespace ARDOUR