variable renaming in Butler for various buffer sizes
[ardour.git] / libs / ardour / butler.cc
1 /*
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>
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26
27 #ifndef PLATFORM_WINDOWS
28 #include <poll.h>
29 #endif
30
31 #include "pbd/error.h"
32 #include "pbd/pthread_utils.h"
33
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"
42
43 #include "pbd/i18n.h"
44
45 using namespace PBD;
46
47 namespace ARDOUR {
48
49 Butler::Butler(Session& s)
50         : SessionHandleRef (s)
51         , thread()
52         , have_thread (false)
53         , _audio_capture_buffer_size(0)
54         , _audio_playback_buffer_size(0)
55         , _midi_buffer_size(0)
56         , pool_trash(16)
57         , _xthread (true)
58 {
59         g_atomic_int_set(&should_do_transport_work, 0);
60         SessionEvent::pool->set_trash (&pool_trash);
61
62         /* catch future changes to parameters */
63         Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Butler::config_changed, this, _1));
64 }
65
66 Butler::~Butler()
67 {
68         terminate_thread ();
69 }
70
71 void
72 Butler::map_parameters ()
73 {
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);
77 }
78
79 void
80 Butler::config_changed (std::string p)
81 {
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 ();
88                 }
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 ();
93                 }
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()));
102         }
103 }
104
105 int
106 Butler::start_thread()
107 {
108         // set up capture and playback buffering
109         DiskIOProcessor::set_buffering_parameters (Config->get_buffering_preset());
110
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);
115
116         /* size is in bytes
117          * XXX: AudioEngine needs to tell us the MIDI buffer size
118          * (i.e. how many MIDI bytes we might see in a cycle)
119          */
120         _midi_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
121
122         DiskReader::set_midi_readahead_samples ((samplecnt_t) (Config->get_midi_readahead() * rate));
123
124         should_run = false;
125
126         if (pthread_create_and_store ("disk butler", &thread, _thread_work, this)) {
127                 error << _("Session: could not create butler thread") << endmsg;
128                 return -1;
129         }
130
131         //pthread_detach (thread);
132         have_thread = true;
133
134         // we are ready to request buffer adjustments
135         _session.adjust_capture_buffering ();
136         _session.adjust_playback_buffering ();
137
138         return 0;
139 }
140
141 void
142 Butler::terminate_thread ()
143 {
144         if (have_thread) {
145                 void* status;
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);
149         }
150 }
151
152 void *
153 Butler::_thread_work (void* arg)
154 {
155         SessionEvent::create_per_thread_pool ("butler events", 4096);
156         pthread_set_name (X_("butler"));
157         return ((Butler *) arg)->thread_work ();
158 }
159
160 void *
161 Butler::thread_work ()
162 {
163         uint32_t err = 0;
164
165         bool disk_work_outstanding = false;
166         RouteList::iterator i;
167
168         while (true) {
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()));
170
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()));
173
174                         char msg;
175                         /* empty the pipe of all current requests */
176                         if (_xthread.receive (msg, true) >= 0) {
177                                 Request::Type req = (Request::Type) msg;
178                                 switch (req) {
179
180                                         case Request::Run:
181                                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler asked to run @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
182                                                 should_run = true;
183                                                 break;
184
185                                         case Request::Pause:
186                                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler asked to pause @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
187                                                 should_run = false;
188                                                 break;
189
190                                         case Request::Quit:
191                                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler asked to quit @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
192                                                 return 0;
193                                                 abort(); /*NOTREACHED*/
194                                                 break;
195
196                                         default:
197                                                 break;
198                                 }
199                         }
200                 }
201
202
203           restart:
204                 DEBUG_TRACE (DEBUG::Butler, "at restart for disk work\n");
205                 disk_work_outstanding = false;
206
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()));
211                 }
212
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);
218                         tr->do_refill ();
219                         _session.the_auditioner()->seek_response(audition_seek);
220                 }
221
222                 boost::shared_ptr<RouteList> rl = _session.get_routes();
223
224                 RouteList rl_with_auditioner = *rl;
225                 rl_with_auditioner.push_back (_session.the_auditioner());
226
227                 DEBUG_TRACE (DEBUG::Butler, string_compose ("butler starts refill loop, twr = %1\n", transport_work_requested()));
228
229                 for (i = rl_with_auditioner.begin(); !transport_work_requested() && should_run && i != rl_with_auditioner.end(); ++i) {
230
231                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
232
233                         if (!tr) {
234                                 continue;
235                         }
236
237                         boost::shared_ptr<IO> io = tr->input ();
238
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()));
242                                 continue;
243                         }
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 ()) {
246                         case 0:
247                                 //DEBUG_TRACE (DEBUG::Butler, string_compose ("\ttrack refill done %1\n", tr->name()));
248                                 break;
249
250                         case 1:
251                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("\ttrack refill unfinished %1\n", tr->name()));
252                                 disk_work_outstanding = true;
253                                 break;
254
255                         default:
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;
258                                 break;
259                         }
260
261                 }
262
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;
266                 }
267
268                 if (!err && transport_work_requested()) {
269                         DEBUG_TRACE (DEBUG::Butler, "transport work requested during refill, back to restart\n");
270                         goto restart;
271                 }
272
273                 disk_work_outstanding = disk_work_outstanding || flush_tracks_to_disk_normal (rl, err);
274
275                 if (err && _session.actively_recording()) {
276                         /* stop the transport and try to catch as much possible
277                            captured state as we can.
278                         */
279                         DEBUG_TRACE (DEBUG::Butler, "error occurred during recording - stop transport\n");
280                         _session.request_stop ();
281                 }
282
283                 if (!err && transport_work_requested()) {
284                         DEBUG_TRACE (DEBUG::Butler, "transport work requested during flush, back to restart\n");
285                         goto restart;
286                 }
287
288                 if (!disk_work_outstanding) {
289                         _session.refresh_disk_space ();
290                 }
291
292                 {
293                         Glib::Threads::Mutex::Lock lm (request_lock);
294
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()));
298                                 goto restart;
299                         }
300
301                         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler signals pause @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
302                         paused.signal();
303                 }
304
305                 DEBUG_TRACE (DEBUG::Butler, "butler emptying pool trash\n");
306                 empty_pool_trash ();
307         }
308
309         return (0);
310 }
311
312 bool
313 Butler::flush_tracks_to_disk_normal (boost::shared_ptr<RouteList> rl, uint32_t& errors)
314 {
315         bool disk_work_outstanding = false;
316
317         for (RouteList::iterator i = rl->begin(); !transport_work_requested() && should_run && i != rl->end(); ++i) {
318
319                 // cerr << "write behind for " << (*i)->name () << endl;
320
321                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
322
323                 if (!tr) {
324                         continue;
325                 }
326
327                 /* note that we still try to flush diskstreams attached to inactive routes
328                  */
329
330                 int ret;
331
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);
334                 switch (ret) {
335                 case 0:
336                         //DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush complete for %1\n", tr->name()));
337                         break;
338
339                 case 1:
340                         //DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush not finished for %1\n", tr->name()));
341                         disk_work_outstanding = true;
342                         break;
343
344                 default:
345                         errors++;
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.
350                         */
351                 }
352         }
353
354         return disk_work_outstanding;
355 }
356
357 bool
358 Butler::flush_tracks_to_disk_after_locate (boost::shared_ptr<RouteList> rl, uint32_t& errors)
359 {
360         bool disk_work_outstanding = false;
361
362         /* almost the same as the "normal" version except that we do not test
363          * for transport_work_requested() and we force flushes.
364          */
365
366         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
367
368                 // cerr << "write behind for " << (*i)->name () << endl;
369
370                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
371
372                 if (!tr) {
373                         continue;
374                 }
375
376                 /* note that we still try to flush diskstreams attached to inactive routes
377                  */
378
379                 int ret;
380
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);
383                 switch (ret) {
384                 case 0:
385                         DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush complete for %1\n", tr->name()));
386                         break;
387
388                 case 1:
389                         DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush not finished for %1\n", tr->name()));
390                         disk_work_outstanding = true;
391                         break;
392
393                 default:
394                         errors++;
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.
399                         */
400                 }
401         }
402
403         return disk_work_outstanding;
404 }
405
406 void
407 Butler::schedule_transport_work ()
408 {
409         DEBUG_TRACE (DEBUG::Butler, "requesting more transport work\n");
410         g_atomic_int_inc (&should_do_transport_work);
411         summon ();
412 }
413
414 void
415 Butler::queue_request (Request::Type r)
416 {
417         char c = 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.
422                  *
423                  * a lost "run" requests under normal RT operation
424                  * is mostly harmless.
425                  *
426                  * TODO if ardour is freehweeling, wait & retry.
427                  * ditto for Request::Type Quit
428                  */
429                 assert(1); // we're screwd
430         }
431 }
432
433 void
434 Butler::summon ()
435 {
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);
438 }
439
440 void
441 Butler::stop ()
442 {
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);
447 }
448
449 void
450 Butler::wait_until_finished ()
451 {
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);
456 }
457
458 bool
459 Butler::transport_work_requested () const
460 {
461         return g_atomic_int_get(&should_do_transport_work);
462 }
463
464 void
465 Butler::empty_pool_trash ()
466 {
467         /* look in the trash, deleting empty pools until we come to one that is not empty */
468
469         RingBuffer<CrossThreadPool*>::rw_vector vec;
470         pool_trash.get_read_vector (&vec);
471
472         guint deleted = 0;
473
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];
478                                 ++deleted;
479                         } else {
480                                 /* found a non-empty pool, so stop deleting */
481                                 if (deleted) {
482                                         pool_trash.increment_read_idx (deleted);
483                                 }
484                                 return;
485                         }
486                 }
487         }
488
489         if (deleted) {
490                 pool_trash.increment_read_idx (deleted);
491         }
492 }
493
494 void
495 Butler::drop_references ()
496 {
497         std::cerr << "Butler drops pool trash\n";
498         SessionEvent::pool->set_trash (0);
499 }
500
501
502 } // namespace ARDOUR
503