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_capture_buffer_size(0)
47 , audio_dstream_playback_buffer_size(0)
48 , midi_dstream_buffer_size(0)
51 g_atomic_int_set(&should_do_transport_work, 0);
52 SessionEvent::pool->set_trash (&pool_trash);
54 Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Butler::config_changed, this, _1));
63 Butler::config_changed (std::string p)
65 if (p == "playback-buffer-seconds") {
66 /* size is in Samples, not bytes */
67 audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * _session.frame_rate());
68 _session.adjust_playback_buffering ();
69 } else if (p == "capture-buffer-seconds") {
70 audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * _session.frame_rate());
71 _session.adjust_capture_buffering ();
76 Butler::start_thread()
78 const float rate = (float)_session.frame_rate();
80 /* size is in Samples, not bytes */
81 audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * rate);
82 audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * rate);
85 * XXX: Jack needs to tell us the MIDI buffer size
86 * (i.e. how many MIDI bytes we might see in a cycle)
88 midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
90 MidiDiskstream::set_readahead_frames ((nframes_t)(Config->get_midi_readahead() * rate));
92 Crossfade::set_buffer_size (audio_dstream_playback_buffer_size);
96 if (pipe (request_pipe)) {
97 error << string_compose(_("Cannot create transport request signal pipe (%1)"),
98 strerror (errno)) << endmsg;
102 if (fcntl (request_pipe[0], F_SETFL, O_NONBLOCK)) {
103 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
104 strerror (errno)) << endmsg;
108 if (fcntl (request_pipe[1], F_SETFL, O_NONBLOCK)) {
109 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
110 strerror (errno)) << endmsg;
114 if (pthread_create_and_store ("disk butler", &thread, _thread_work, this)) {
115 error << _("Session: could not create butler thread") << endmsg;
119 //pthread_detach (thread);
125 Butler::terminate_thread ()
129 const char c = Request::Quit;
130 ::write (request_pipe[1], &c, 1);
131 pthread_join (thread, &status);
136 Butler::_thread_work (void* arg)
138 SessionEvent::create_per_thread_pool ("butler events", 64);
139 pthread_set_name (X_("butler"));
140 return ((Butler *) arg)->thread_work ();
144 Butler::thread_work ()
149 microseconds_t begin, end;
151 struct pollfd pfd[1];
152 bool disk_work_outstanding = false;
153 RouteList::iterator i;
156 pfd[0].fd = request_pipe[0];
157 pfd[0].events = POLLIN|POLLERR|POLLHUP;
159 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
161 if (errno == EINTR) {
165 error << string_compose (_("poll on butler request pipe failed (%1)"),
171 if (pfd[0].revents & ~POLLIN) {
172 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
176 if (pfd[0].revents & POLLIN) {
180 /* empty the pipe of all current requests */
183 size_t nread = ::read (request_pipe[0], &req, sizeof (req));
186 switch ((Request::Type) req) {
200 pthread_exit_pbd (0);
208 } else if (nread == 0) {
210 } else if (errno == EAGAIN) {
213 fatal << _("Error reading from butler request pipe") << endmsg;
219 if (transport_work_requested()) {
220 _session.butler_transport_work ();
223 disk_work_outstanding = false;
227 begin = get_microseconds();
229 boost::shared_ptr<RouteList> rl = _session.get_routes();
231 RouteList rl_with_auditioner = *rl;
232 rl_with_auditioner.push_back (_session.the_auditioner());
234 // for (i = dsl->begin(); i != dsl->end(); ++i) {
235 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
238 for (i = rl_with_auditioner.begin(); !transport_work_requested() && should_run && i != rl_with_auditioner.end(); ++i) {
240 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
245 /* don't read inactive tracks */
247 boost::shared_ptr<IO> io = tr->input ();
249 if (io && !io->active()) {
253 switch (tr->do_refill ()) {
255 bytes += tr->read_data_count();
258 bytes += tr->read_data_count();
259 disk_work_outstanding = true;
264 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
270 if (i != rl_with_auditioner.begin() && i != rl_with_auditioner.end()) {
271 /* we didn't get to all the streams */
272 disk_work_outstanding = true;
275 if (!err && transport_work_requested()) {
280 end = get_microseconds();
281 if (end - begin > 0) {
282 _read_data_rate = (float) bytes / (float) (end - begin);
284 _read_data_rate = 0; // infinity better
290 begin = get_microseconds();
292 for (i = rl->begin(); !transport_work_requested() && should_run && i != rl->end(); ++i) {
293 // cerr << "write behind for " << (*i)->name () << endl;
295 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
300 /* note that we still try to flush diskstreams attached to inactive routes
303 switch (tr->do_flush (ButlerContext)) {
305 bytes += tr->write_data_count();
308 bytes += tr->write_data_count();
309 disk_work_outstanding = true;
315 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
316 /* don't break - try to flush all streams in case they
317 are split across disks.
322 if (err && _session.actively_recording()) {
323 /* stop the transport and try to catch as much possible
324 captured state as we can.
326 _session.request_stop ();
329 if (i != rl->begin() && i != rl->end()) {
330 /* we didn't get to all the streams */
331 disk_work_outstanding = true;
334 if (!err && transport_work_requested()) {
339 // there are no apparent users for this calculation?
340 end = get_microseconds();
341 if (end - begin > 0) {
342 _write_data_rate = (float) bytes / (float) (end - begin);
344 _write_data_rate = 0; // Well, infinity would be better
348 if (!disk_work_outstanding) {
349 _session.refresh_disk_space ();
354 Glib::Mutex::Lock lm (request_lock);
356 if (should_run && (disk_work_outstanding || transport_work_requested())) {
357 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
358 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
370 pthread_exit_pbd (0);
376 Butler::schedule_transport_work ()
378 g_atomic_int_inc (&should_do_transport_work);
385 char c = Request::Run;
386 ::write (request_pipe[1], &c, 1);
392 Glib::Mutex::Lock lm (request_lock);
393 char c = Request::Pause;
394 ::write (request_pipe[1], &c, 1);
395 paused.wait(request_lock);
399 Butler::wait_until_finished ()
401 Glib::Mutex::Lock lm (request_lock);
402 char c = Request::Wake;
403 ::write (request_pipe[1], &c, 1);
404 paused.wait(request_lock);
408 Butler::transport_work_requested () const
410 return g_atomic_int_get(&should_do_transport_work);
414 Butler::read_data_rate () const
416 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
417 in action. ignore it.
419 return _read_data_rate > 10485.7600000f ? 0.0f : _read_data_rate;
423 Butler::write_data_rate () const
425 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
426 in action. ignore it.
428 return _write_data_rate > 10485.7600000f ? 0.0f : _write_data_rate;
432 Butler::empty_pool_trash ()
434 /* look in the trash, deleting empty pools until we come to one that is not empty */
436 RingBuffer<CrossThreadPool*>::rw_vector vec;
437 pool_trash.get_read_vector (&vec);
441 for (int i = 0; i < 2; ++i) {
442 for (guint j = 0; j < vec.len[i]; ++j) {
443 if (vec.buf[i][j]->empty()) {
444 delete vec.buf[i][j];
447 /* found a non-empty pool, so stop deleting */
449 pool_trash.increment_read_idx (deleted);
457 pool_trash.increment_read_idx (deleted);
462 Butler::drop_references ()
464 SessionEvent::pool->set_trash (0);
468 } // namespace ARDOUR