[Summary] HOT FIX: Fixed crash which happens on an attempt to load a session with...
[ardour.git] / libs / ardour / butler.cc
1 /*
2     Copyright (C) 1999-2009 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23
24 #ifndef PLATFORM_WINDOWS
25 #include <poll.h>
26 #endif
27
28 #include "pbd/error.h"
29 #include "pbd/pthread_utils.h"
30 #include "ardour/debug.h"
31 #include "ardour/butler.h"
32 #include "ardour/io.h"
33 #include "ardour/midi_diskstream.h"
34 #include "ardour/session.h"
35 #include "ardour/track.h"
36 #include "ardour/auditioner.h"
37
38 #include "i18n.h"
39
40 using namespace PBD;
41
42 namespace ARDOUR {
43
44 Butler::Butler(Session& s)
45         : SessionHandleRef (s)
46         , thread()
47         , have_thread (false)
48         , audio_dstream_capture_buffer_size(0)
49         , audio_dstream_playback_buffer_size(0)
50         , midi_dstream_buffer_size(0)
51         , pool_trash(16)
52         , _xthread (true)
53 {
54         g_atomic_int_set(&should_do_transport_work, 0);
55         SessionEvent::pool->set_trash (&pool_trash);
56
57         /* catch future changes to parameters */
58         Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Butler::config_changed, this, _1));
59 }
60
61 Butler::~Butler()
62 {
63         terminate_thread ();
64 }
65
66 void
67 Butler::map_parameters ()
68 {
69         /* use any current ones that we care about */
70         boost::function<void (std::string)> ff (boost::bind (&Butler::config_changed, this, _1));
71         Config->map_parameters (ff);
72 }
73
74 void
75 Butler::config_changed (std::string p)
76 {
77         if (p == "playback-buffer-seconds") {
78                 _session.adjust_playback_buffering ();
79                 if (Config->get_buffering_preset() == Custom) {
80                         /* size is in Samples, not bytes */
81                         audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * _session.frame_rate());
82                         _session.adjust_playback_buffering ();
83                 } else {
84                         std::cerr << "Skip explicit buffer seconds, preset in use\n";
85                 }
86         } else if (p == "capture-buffer-seconds") {
87                 if (Config->get_buffering_preset() == Custom) {
88                         audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * _session.frame_rate());
89                         _session.adjust_capture_buffering ();
90                 } else {
91                         std::cerr << "Skip explicit buffer seconds, preset in use\n";
92                 }
93         } else if (p == "buffering-preset") {
94                 Diskstream::set_buffering_parameters (Config->get_buffering_preset());
95                 audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * _session.frame_rate());
96                 audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * _session.frame_rate());
97                 _session.adjust_capture_buffering ();
98                 _session.adjust_playback_buffering ();
99         } else if (p == "midi-readahead") {
100                 MidiDiskstream::set_readahead_frames ((framecnt_t) (Config->get_midi_readahead() * _session.frame_rate()));
101         }
102 }
103
104 int
105 Butler::start_thread()
106 {
107     Diskstream::set_buffering_parameters (Config->get_buffering_preset());
108     
109         /* size is in Samples, not bytes */
110     const float rate = (float)_session.frame_rate();
111         audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * rate);
112         audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * rate);
113     
114     _session.adjust_capture_buffering ();
115     _session.adjust_playback_buffering ();
116     
117         /* size is in bytes
118          * XXX: Jack needs to tell us the MIDI buffer size
119          * (i.e. how many MIDI bytes we might see in a cycle)
120          */
121         midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
122
123         MidiDiskstream::set_readahead_frames ((framecnt_t) (Config->get_midi_readahead() * rate));
124
125         should_run = false;
126
127         if (pthread_create_and_store ("disk butler", &thread, _thread_work, this)) {
128                 error << _("Session: could not create butler thread") << endmsg;
129                 return -1;
130         }
131
132         //pthread_detach (thread);
133         have_thread = true;
134         return 0;
135 }
136
137 void
138 Butler::terminate_thread ()
139 {
140         if (have_thread) {
141                 void* status;
142                 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: ask butler to quit @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
143                 queue_request (Request::Quit);
144                 pthread_join (thread, &status);
145         }
146 }
147
148 void *
149 Butler::_thread_work (void* arg)
150 {
151         SessionEvent::create_per_thread_pool ("butler events", 4096);
152         pthread_set_name (X_("butler"));
153         return ((Butler *) arg)->thread_work ();
154 }
155
156 void *
157 Butler::thread_work ()
158 {
159         uint32_t err = 0;
160
161         bool disk_work_outstanding = false;
162         RouteList::iterator i;
163
164         while (true) {
165                 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()));
166
167                 if(!disk_work_outstanding) {
168                         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 butler waits for requests @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
169
170                         char msg;
171                         /* empty the pipe of all current requests */
172                         if (_xthread.receive (msg, true) >= 0) {
173                                 Request::Type req = (Request::Type) msg;
174                                 switch (req) {
175
176                                         case Request::Run:
177                                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler asked to run @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
178                                                 should_run = true;
179                                                 break;
180
181                                         case Request::Pause:
182                                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler asked to pause @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
183                                                 should_run = false;
184                                                 break;
185
186                                         case Request::Quit:
187                                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler asked to quit @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
188                                                 return 0;
189                                                 abort(); /*NOTREACHED*/
190                                                 break;
191
192                                         default:
193                                                 break;
194                                 }
195                         }
196                 }
197
198                 
199           restart:
200                 DEBUG_TRACE (DEBUG::Butler, "at restart for disk work\n");
201                 disk_work_outstanding = false;
202
203                 if (transport_work_requested()) {
204                         DEBUG_TRACE (DEBUG::Butler, string_compose ("do transport work @ %1\n", g_get_monotonic_time()));
205                         _session.butler_transport_work ();
206                         DEBUG_TRACE (DEBUG::Butler, string_compose ("\ttransport work complete @ %1\n", g_get_monotonic_time()));
207                 }
208
209                 frameoffset_t audition_seek;
210                 if (should_run && _session.is_auditioning()
211                                 && (audition_seek = _session.the_auditioner()->seek_frame()) >= 0) {
212                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (_session.the_auditioner());
213                         DEBUG_TRACE (DEBUG::Butler, "seek the auditioner\n");
214                         tr->seek(audition_seek);
215                         _session.the_auditioner()->seek_response(audition_seek);
216                 }
217
218                 boost::shared_ptr<RouteList> rl = _session.get_routes();
219
220                 RouteList rl_with_auditioner = *rl;
221                 rl_with_auditioner.push_back (_session.the_auditioner());
222
223                 for (i = rl_with_auditioner.begin(); !transport_work_requested() && should_run && i != rl_with_auditioner.end(); ++i) {
224
225                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
226
227                         if (!tr) {
228                                 continue;
229                         }
230
231                         boost::shared_ptr<IO> io = tr->input ();
232
233                         if (io && !io->active()) {
234                                 /* don't read inactive tracks */
235                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("butler skips inactive track %1\n", tr->name()));
236                                 continue;
237                         }
238                         DEBUG_TRACE (DEBUG::Butler, string_compose ("butler refills %1, playback load = %2\n", tr->name(), tr->playback_buffer_load()));
239                         switch (tr->do_refill ()) {
240                         case 0:
241                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("\ttrack refill done %1\n", tr->name()));
242                                 break;
243                                 
244                         case 1:
245                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("\ttrack refill unfinished %1\n", tr->name()));
246                                 disk_work_outstanding = true;
247                                 break;
248
249                         default:
250                                 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
251                                 std::cerr << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << std::endl;
252                                 break;
253                         }
254
255                 }
256
257                 if (i != rl_with_auditioner.begin() && i != rl_with_auditioner.end()) {
258                         /* we didn't get to all the streams */
259                         disk_work_outstanding = true;
260                 }
261
262                 if (!err && transport_work_requested()) {
263                         DEBUG_TRACE (DEBUG::Butler, "transport work requested during refill, back to restart\n");
264                         goto restart;
265                 }
266
267                 for (i = rl->begin(); !transport_work_requested() && should_run && i != rl->end(); ++i) {
268                         // cerr << "write behind for " << (*i)->name () << endl;
269
270                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
271
272                         if (!tr) {
273                                 continue;
274                         }
275
276                         /* note that we still try to flush diskstreams attached to inactive routes
277                          */
278
279                         gint64 before, after;
280                         int ret;
281
282                         DEBUG_TRACE (DEBUG::Butler, string_compose ("butler flushes track %1 capture load %2\n", tr->name(), tr->capture_buffer_load()));
283                         before = g_get_monotonic_time ();
284                         ret = tr->do_flush (ButlerContext);
285                         after = g_get_monotonic_time ();
286                         switch (ret) {
287                         case 0:
288                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush complete for %1, %2 usecs\n", tr->name(), after - before));
289                                 break;
290                                 
291                         case 1:
292                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("\tflush not finished for %1, %2 usecs\n", tr->name(), after - before));
293                                 disk_work_outstanding = true;
294                                 break;
295
296                         default:
297                                 err++;
298                                 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
299                                 std::cerr << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << std::endl;
300                                 /* don't break - try to flush all streams in case they
301                                    are split across disks.
302                                 */
303                         }
304                 }
305
306                 if (err && _session.actively_recording()) {
307                         /* stop the transport and try to catch as much possible
308                            captured state as we can.
309                         */
310                         DEBUG_TRACE (DEBUG::Butler, "error occurred during recording - stop transport\n");
311                         _session.request_stop ();
312                 }
313
314                 if (i != rl->begin() && i != rl->end()) {
315                         /* we didn't get to all the streams */
316                         DEBUG_TRACE (DEBUG::Butler, "not all tracks processed, will need to go back for more\n");
317                         disk_work_outstanding = true;
318                 }
319
320                 if (!err && transport_work_requested()) {
321                         DEBUG_TRACE (DEBUG::Butler, "transport work requested during flush, back to restart\n");
322                         goto restart;
323                 }
324
325                 if (!disk_work_outstanding) {
326                         _session.refresh_disk_space ();
327                 }
328
329
330                 {
331                         Glib::Threads::Mutex::Lock lm (request_lock);
332
333                         if (should_run && (disk_work_outstanding || transport_work_requested())) {
334                                 DEBUG_TRACE (DEBUG::Butler, string_compose ("at end, should run %1 disk work %2 transport work %3 ... goto restart\n",
335                                                                             should_run, disk_work_outstanding, transport_work_requested()));
336                                 goto restart;
337                         }
338
339                         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: butler signals pause @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
340                         paused.signal();
341                 }
342
343                 DEBUG_TRACE (DEBUG::Butler, "butler emptying pool trash\n");
344                 empty_pool_trash ();
345         }
346
347         return (0);
348 }
349
350 void
351 Butler::schedule_transport_work ()
352 {
353         g_atomic_int_inc (&should_do_transport_work);
354         summon ();
355 }
356
357 void
358 Butler::queue_request (Request::Type r)
359 {
360         char c = r;
361         if (_xthread.deliver (c) != 1) {
362                 /* the x-thread channel is non-blocking
363                  * write may fail, but we really don't want to wait
364                  * under normal circumstances.
365                  *
366                  * a lost "run" requests under normal RT operation
367                  * is mostly harmless.
368                  *
369                  * TODO if ardour is freehweeling, wait & retry.
370                  * ditto for Request::Type Quit
371                  */
372                 assert(1); // we're screwd
373         }
374 }
375
376 void
377 Butler::summon ()
378 {
379         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: summon butler to run @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
380         queue_request (Request::Run);
381 }
382
383 void
384 Butler::stop ()
385 {
386         Glib::Threads::Mutex::Lock lm (request_lock);
387         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: asking butler to stop @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
388         queue_request (Request::Pause);
389         paused.wait(request_lock);
390 }
391
392 void
393 Butler::wait_until_finished ()
394 {
395         Glib::Threads::Mutex::Lock lm (request_lock);
396         DEBUG_TRACE (DEBUG::Butler, string_compose ("%1: waiting for butler to finish @ %2\n", DEBUG_THREAD_SELF, g_get_monotonic_time()));
397         queue_request (Request::Pause);
398         paused.wait(request_lock);
399 }
400
401 bool
402 Butler::transport_work_requested () const
403 {
404         return g_atomic_int_get(&should_do_transport_work);
405 }
406
407 void
408 Butler::empty_pool_trash ()
409 {
410         /* look in the trash, deleting empty pools until we come to one that is not empty */
411
412         RingBuffer<CrossThreadPool*>::rw_vector vec;
413         pool_trash.get_read_vector (&vec);
414
415         guint deleted = 0;
416
417         for (int i = 0; i < 2; ++i) {
418                 for (guint j = 0; j < vec.len[i]; ++j) {
419                         if (vec.buf[i][j]->empty()) {
420                                 delete vec.buf[i][j];
421                                 ++deleted;
422                         } else {
423                                 /* found a non-empty pool, so stop deleting */
424                                 if (deleted) {
425                                         pool_trash.increment_read_idx (deleted);
426                                 }
427                                 return;
428                         }
429                 }
430         }
431
432         if (deleted) {
433                 pool_trash.increment_read_idx (deleted);
434         }
435 }
436
437 void
438 Butler::drop_references ()
439 {
440         std::cerr << "Butler drops pool trash\n";
441         SessionEvent::pool->set_trash (0);
442 }
443
444
445 } // namespace ARDOUR
446