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"
32 #include "ardour/auditioner.h"
38 static float _read_data_rate;
39 static float _write_data_rate;
43 Butler::Butler(Session& s)
44 : SessionHandleRef (s)
46 , audio_dstream_buffer_size(0)
47 , midi_dstream_buffer_size(0)
50 g_atomic_int_set(&should_do_transport_work, 0);
51 SessionEvent::pool->set_trash (&pool_trash);
60 Butler::start_thread()
62 const float rate = (float)_session.frame_rate();
64 /* size is in Samples, not bytes */
65 audio_dstream_buffer_size = (uint32_t) floor (Config->get_audio_track_buffer_seconds() * rate);
68 * XXX: Jack needs to tell us the MIDI buffer size
69 * (i.e. how many MIDI bytes we might see in a cycle)
71 midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
73 MidiDiskstream::set_readahead_frames ((nframes_t)(Config->get_midi_readahead() * rate));
75 Crossfade::set_buffer_size (audio_dstream_buffer_size);
79 if (pipe (request_pipe)) {
80 error << string_compose(_("Cannot create transport request signal pipe (%1)"),
81 strerror (errno)) << endmsg;
85 if (fcntl (request_pipe[0], F_SETFL, O_NONBLOCK)) {
86 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
87 strerror (errno)) << endmsg;
91 if (fcntl (request_pipe[1], F_SETFL, O_NONBLOCK)) {
92 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
93 strerror (errno)) << endmsg;
97 if (pthread_create_and_store ("disk butler", &thread, _thread_work, this)) {
98 error << _("Session: could not create butler thread") << endmsg;
102 //pthread_detach (thread);
108 Butler::terminate_thread ()
112 const char c = Request::Quit;
113 ::write (request_pipe[1], &c, 1);
114 pthread_join (thread, &status);
119 Butler::_thread_work (void* arg)
121 SessionEvent::create_per_thread_pool ("butler events", 64);
122 pthread_set_name (X_("butler"));
123 return ((Butler *) arg)->thread_work ();
127 Butler::thread_work ()
132 microseconds_t begin, end;
134 struct pollfd pfd[1];
135 bool disk_work_outstanding = false;
136 RouteList::iterator i;
139 pfd[0].fd = request_pipe[0];
140 pfd[0].events = POLLIN|POLLERR|POLLHUP;
142 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
144 if (errno == EINTR) {
148 error << string_compose (_("poll on butler request pipe failed (%1)"),
154 if (pfd[0].revents & ~POLLIN) {
155 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
159 if (pfd[0].revents & POLLIN) {
163 /* empty the pipe of all current requests */
166 size_t nread = ::read (request_pipe[0], &req, sizeof (req));
169 switch ((Request::Type) req) {
183 pthread_exit_pbd (0);
191 } else if (nread == 0) {
193 } else if (errno == EAGAIN) {
196 fatal << _("Error reading from butler request pipe") << endmsg;
202 if (transport_work_requested()) {
203 _session.butler_transport_work ();
206 disk_work_outstanding = false;
210 begin = get_microseconds();
212 boost::shared_ptr<RouteList> rl = _session.get_routes();
214 RouteList rl_with_auditioner = *rl;
215 rl_with_auditioner.push_back (_session.the_auditioner());
217 // for (i = dsl->begin(); i != dsl->end(); ++i) {
218 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
221 for (i = rl_with_auditioner.begin(); !transport_work_requested() && should_run && i != rl_with_auditioner.end(); ++i) {
223 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
228 /* don't read inactive tracks */
230 boost::shared_ptr<IO> io = tr->input ();
232 if (io && !io->active()) {
236 switch (tr->do_refill ()) {
238 bytes += tr->read_data_count();
241 bytes += tr->read_data_count();
242 disk_work_outstanding = true;
247 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
253 if (i != rl_with_auditioner.begin() && i != rl_with_auditioner.end()) {
254 /* we didn't get to all the streams */
255 disk_work_outstanding = true;
258 if (!err && transport_work_requested()) {
263 end = get_microseconds();
264 if (end - begin > 0) {
265 _read_data_rate = (float) bytes / (float) (end - begin);
267 _read_data_rate = 0; // infinity better
273 begin = get_microseconds();
275 for (i = rl->begin(); !transport_work_requested() && should_run && i != rl->end(); ++i) {
276 // cerr << "write behind for " << (*i)->name () << endl;
278 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
283 /* note that we still try to flush diskstreams attached to inactive routes
286 switch (tr->do_flush (ButlerContext)) {
288 bytes += tr->write_data_count();
291 bytes += tr->write_data_count();
292 disk_work_outstanding = true;
298 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
299 /* don't break - try to flush all streams in case they
300 are split across disks.
305 if (err && _session.actively_recording()) {
306 /* stop the transport and try to catch as much possible
307 captured state as we can.
309 _session.request_stop ();
312 if (i != rl->begin() && i != rl->end()) {
313 /* we didn't get to all the streams */
314 disk_work_outstanding = true;
317 if (!err && transport_work_requested()) {
322 // there are no apparent users for this calculation?
323 end = get_microseconds();
324 if (end - begin > 0) {
325 _write_data_rate = (float) bytes / (float) (end - begin);
327 _write_data_rate = 0; // Well, infinity would be better
331 if (!disk_work_outstanding) {
332 _session.refresh_disk_space ();
337 Glib::Mutex::Lock lm (request_lock);
339 if (should_run && (disk_work_outstanding || transport_work_requested())) {
340 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
341 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
353 pthread_exit_pbd (0);
359 Butler::schedule_transport_work ()
361 g_atomic_int_inc (&should_do_transport_work);
368 char c = Request::Run;
369 ::write (request_pipe[1], &c, 1);
375 Glib::Mutex::Lock lm (request_lock);
376 char c = Request::Pause;
377 ::write (request_pipe[1], &c, 1);
378 paused.wait(request_lock);
382 Butler::wait_until_finished ()
384 Glib::Mutex::Lock lm (request_lock);
385 char c = Request::Wake;
386 ::write (request_pipe[1], &c, 1);
387 paused.wait(request_lock);
391 Butler::transport_work_requested () const
393 return g_atomic_int_get(&should_do_transport_work);
397 Butler::read_data_rate () const
399 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
400 in action. ignore it.
402 return _read_data_rate > 10485.7600000f ? 0.0f : _read_data_rate;
406 Butler::write_data_rate () const
408 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
409 in action. ignore it.
411 return _write_data_rate > 10485.7600000f ? 0.0f : _write_data_rate;
415 Butler::empty_pool_trash ()
417 /* look in the trash, deleting empty pools until we come to one that is not empty */
419 RingBuffer<CrossThreadPool*>::rw_vector vec;
420 pool_trash.get_read_vector (&vec);
424 for (int i = 0; i < 2; ++i) {
425 for (guint j = 0; j < vec.len[i]; ++j) {
426 if (vec.buf[i][j]->empty()) {
427 delete vec.buf[i][j];
430 /* found a non-empty pool, so stop deleting */
432 pool_trash.increment_read_idx (deleted);
440 pool_trash.increment_read_idx (deleted);
445 Butler::drop_references ()
447 SessionEvent::pool->set_trash (0);
451 } // namespace ARDOUR