2 * Copyright (C) 1999-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2009-2015 David Robillard <d@drobilla.net>
4 * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
6 * Copyright (C) 2015 GZharun <grygoriiz@wavesglobal.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #ifndef PLATFORM_WINDOWS
31 #include "pbd/error.h"
32 #include "pbd/pthread_utils.h"
34 #include "ardour/butler.h"
35 #include "ardour/debug.h"
36 #include "ardour/disk_io.h"
37 #include "ardour/disk_reader.h"
38 #include "ardour/io.h"
39 #include "ardour/session.h"
40 #include "ardour/track.h"
41 #include "ardour/auditioner.h"
49 Butler::Butler(Session& s)
50 : SessionHandleRef (s)
53 , _audio_capture_buffer_size(0)
54 , _audio_playback_buffer_size(0)
55 , _midi_buffer_size(0)
59 g_atomic_int_set(&should_do_transport_work, 0);
60 SessionEvent::pool->set_trash (&pool_trash);
62 /* catch future changes to parameters */
63 Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Butler::config_changed, this, _1));
72 Butler::map_parameters ()
74 /* use any current ones that we care about */
75 boost::function<void (std::string)> ff (boost::bind (&Butler::config_changed, this, _1));
76 Config->map_parameters (ff);
80 Butler::config_changed (std::string p)
82 if (p == "playback-buffer-seconds") {
83 _session.adjust_playback_buffering ();
84 if (Config->get_buffering_preset() == Custom) {
85 /* size is in Samples, not bytes */
86 _audio_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * _session.sample_rate());
87 _session.adjust_playback_buffering ();
89 } else if (p == "capture-buffer-seconds") {
90 if (Config->get_buffering_preset() == Custom) {
91 _audio_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * _session.sample_rate());
92 _session.adjust_capture_buffering ();
94 } else if (p == "buffering-preset") {
95 DiskIOProcessor::set_buffering_parameters (Config->get_buffering_preset());
96 _audio_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * _session.sample_rate());
97 _audio_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * _session.sample_rate());
98 _session.adjust_capture_buffering ();
99 _session.adjust_playback_buffering ();
100 } else if (p == "midi-readahead") {
101 DiskReader::set_midi_readahead_samples ((samplecnt_t) (Config->get_midi_readahead() * _session.sample_rate()));
106 Butler::start_thread()
108 // set up capture and playback buffering
109 DiskIOProcessor::set_buffering_parameters (Config->get_buffering_preset());
111 /* size is in Samples, not bytes */
112 const float rate = (float)_session.sample_rate();
113 _audio_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * rate);
114 _audio_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * rate);
117 * XXX: AudioEngine needs to tell us the MIDI buffer size
118 * (i.e. how many MIDI bytes we might see in a cycle)
120 _midi_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
122 DiskReader::set_midi_readahead_samples ((samplecnt_t) (Config->get_midi_readahead() * rate));
126 if (pthread_create_and_store ("disk butler", &thread, _thread_work, this)) {
127 error << _("Session: could not create butler thread") << endmsg;
131 //pthread_detach (thread);
134 // we are ready to request buffer adjustments
135 _session.adjust_capture_buffering ();
136 _session.adjust_playback_buffering ();
142 Butler::terminate_thread ()
146 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: ask butler to quit @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
147 queue_request (Request::Quit);
148 pthread_join (thread, &status);
153 Butler::_thread_work (void* arg)
155 SessionEvent::create_per_thread_pool ("butler events", 4096);
156 pthread_set_name (X_("butler"));
157 return ((Butler *) arg)->thread_work ();
161 Butler::thread_work ()
165 bool disk_work_outstanding = false;
166 RouteList::iterator i;
169 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 butler main loop, disk work outstanding ? %2 @ %3\n", DEBUG_THREAD_SELF, disk_work_outstanding, g_get_monotonic_time()));
171 if(!disk_work_outstanding) {
172 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 butler waits for requests @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
175 /* empty the pipe of all current requests */
176 if (_xthread.receive (msg, true) >= 0) {
177 Request::Type req = (Request::Type) msg;
181 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler asked to run @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
186 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler asked to pause @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
191 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler asked to quit @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
193 abort(); /*NOTREACHED*/
204 DEBUG_TRACE (DEBUG::Butler, "at restart for disk work\n");
205 disk_work_outstanding = false;
207 if (transport_work_requested()) {
208 DEBUG_TRACE (DEBUG::Butler, string_compose ("do transport work @ %1\n", g_get_monotonic_time()));
209 _session.butler_transport_work ();
210 DEBUG_TRACE (DEBUG::Butler, string_compose ("\ttransport work complete @ %1, twr = %2\n", g_get_monotonic_time(), transport_work_requested()));
213 sampleoffset_t audition_seek;
214 if (should_run && _session.is_auditioning() && (audition_seek = _session.the_auditioner()->seek_sample()) >= 0) {
215 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (_session.the_auditioner());
216 DEBUG_TRACE (DEBUG::Butler, "seek the auditioner\n");
217 tr->seek(audition_seek);
219 _session.the_auditioner()->seek_response(audition_seek);
222 boost::shared_ptr<RouteList> rl = _session.get_routes();
224 RouteList rl_with_auditioner = *rl;
225 rl_with_auditioner.push_back (_session.the_auditioner());
227 DEBUG_TRACE (DEBUG::Butler, string_compose ("butler starts refill loop, twr = %1\n", transport_work_requested()));
229 for (i = rl_with_auditioner.begin(); !transport_work_requested() && should_run && i != rl_with_auditioner.end(); ++i) {
231 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
237 boost::shared_ptr<IO> io = tr->input ();
239 if (io && !io->active()) {
240 /* don't read inactive tracks */
241 // DEBUG_TRACE (DEBUG::Butler, string_compose ("butler skips inactive track %1\n", tr->name()));
244 // DEBUG_TRACE (DEBUG::Butler, string_compose ("butler refills %1, playback load = %2\n", tr->name(), tr->playback_buffer_load()));
245 switch (tr->do_refill ()) {
247 //DEBUG_TRACE (DEBUG::Butler, string_compose ("\ttrack refill done %1\n", tr->name()));
251 DEBUG_TRACE (DEBUG::Butler, string_compose ("\ttrack refill unfinished %1\n", tr->name()));
252 disk_work_outstanding = true;
256 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
257 std::cerr << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << std::endl;
263 if (i != rl_with_auditioner.begin() && i != rl_with_auditioner.end()) {
264 /* we didn't get to all the streams */
265 disk_work_outstanding = true;
268 if (!err && transport_work_requested()) {
269 DEBUG_TRACE (DEBUG::Butler, "transport work requested during refill, back to restart\n");
273 disk_work_outstanding = disk_work_outstanding || flush_tracks_to_disk_normal (rl, err);
275 if (err && _session.actively_recording()) {
276 /* stop the transport and try to catch as much possible
277 captured state as we can.
279 DEBUG_TRACE (DEBUG::Butler, "error occurred during recording - stop transport\n");
280 _session.request_stop ();
283 if (!err && transport_work_requested()) {
284 DEBUG_TRACE (DEBUG::Butler, "transport work requested during flush, back to restart\n");
288 if (!disk_work_outstanding) {
289 _session.refresh_disk_space ();
293 Glib::Threads::Mutex::Lock lm (request_lock);
295 if (should_run && (disk_work_outstanding || transport_work_requested())) {
296 DEBUG_TRACE (DEBUG::Butler, string_compose ("at end, should run %1 disk work %2 transport work %3 ... goto restart\n",
297 should_run, disk_work_outstanding, transport_work_requested()));
301 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler signals pause @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
305 DEBUG_TRACE (DEBUG::Butler, "butler emptying pool trash\n");
313 Butler::flush_tracks_to_disk_normal (boost::shared_ptr<RouteList> rl, uint32_t& errors)
315 bool disk_work_outstanding = false;
317 for (RouteList::iterator i = rl->begin(); !transport_work_requested() && should_run && i != rl->end(); ++i) {
319 // cerr << "write behind for " << (*i)->name () << endl;
321 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
327 /* note that we still try to flush diskstreams attached to inactive routes
332 // DEBUG_TRACE (DEBUG::Butler, string_compose ("butler flushes track %1 capture load %2\n", tr->name(), tr->capture_buffer_load()));
333 ret = tr->do_flush (ButlerContext, false);
336 //DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush complete for %1\n", tr->name()));
340 //DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush not finished for %1\n", tr->name()));
341 disk_work_outstanding = true;
346 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
347 std::cerr << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << std::endl;
348 /* don't break - try to flush all streams in case they
349 are split across disks.
354 return disk_work_outstanding;
358 Butler::flush_tracks_to_disk_after_locate (boost::shared_ptr<RouteList> rl, uint32_t& errors)
360 bool disk_work_outstanding = false;
362 /* almost the same as the "normal" version except that we do not test
363 * for transport_work_requested() and we force flushes.
366 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
368 // cerr << "write behind for " << (*i)->name () << endl;
370 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
376 /* note that we still try to flush diskstreams attached to inactive routes
381 DEBUG_TRACE (DEBUG::Butler, string_compose ("butler flushes track %1 capture load %2\n", tr->name(), tr->capture_buffer_load()));
382 ret = tr->do_flush (ButlerContext, true);
385 DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush complete for %1\n", tr->name()));
389 DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush not finished for %1\n", tr->name()));
390 disk_work_outstanding = true;
395 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
396 std::cerr << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << std::endl;
397 /* don't break - try to flush all streams in case they
398 are split across disks.
403 return disk_work_outstanding;
407 Butler::schedule_transport_work ()
409 DEBUG_TRACE (DEBUG::Butler, "requesting more transport work\n");
410 g_atomic_int_inc (&should_do_transport_work);
415 Butler::queue_request (Request::Type r)
418 if (_xthread.deliver (c) != 1) {
419 /* the x-thread channel is non-blocking
420 * write may fail, but we really don't want to wait
421 * under normal circumstances.
423 * a lost "run" requests under normal RT operation
424 * is mostly harmless.
426 * TODO if ardour is freehweeling, wait & retry.
427 * ditto for Request::Type Quit
429 assert(1); // we're screwd
436 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: summon butler to run @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
437 queue_request (Request::Run);
443 Glib::Threads::Mutex::Lock lm (request_lock);
444 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: asking butler to stop @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
445 queue_request (Request::Pause);
446 paused.wait(request_lock);
450 Butler::wait_until_finished ()
452 Glib::Threads::Mutex::Lock lm (request_lock);
453 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: waiting for butler to finish @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
454 queue_request (Request::Pause);
455 paused.wait(request_lock);
459 Butler::transport_work_requested () const
461 return g_atomic_int_get(&should_do_transport_work);
465 Butler::empty_pool_trash ()
467 /* look in the trash, deleting empty pools until we come to one that is not empty */
469 RingBuffer<CrossThreadPool*>::rw_vector vec;
470 pool_trash.get_read_vector (&vec);
474 for (int i = 0; i < 2; ++i) {
475 for (guint j = 0; j < vec.len[i]; ++j) {
476 if (vec.buf[i][j]->empty()) {
477 delete vec.buf[i][j];
480 /* found a non-empty pool, so stop deleting */
482 pool_trash.increment_read_idx (deleted);
490 pool_trash.increment_read_idx (deleted);
495 Butler::drop_references ()
497 std::cerr << "Butler drops pool trash\n";
498 SessionEvent::pool->set_trash (0);
502 } // namespace ARDOUR