Move Diskstream ownership to Track, so that Session no longer holds lists of Diskstre...
[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 #include <poll.h>
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
33 #include "i18n.h"
34
35 using namespace PBD;
36
37 static float _read_data_rate;
38 static float _write_data_rate;
39
40 namespace ARDOUR {
41
42 Butler::Butler(Session& s)
43         : SessionHandleRef (s)
44         , thread(0)
45         , audio_dstream_buffer_size(0)
46         , midi_dstream_buffer_size(0)
47         , pool_trash(16)
48 {
49         g_atomic_int_set(&should_do_transport_work, 0);
50         SessionEvent::pool->set_trash (&pool_trash);
51 }
52
53 Butler::~Butler()
54 {
55         terminate_thread ();
56 }
57
58 int
59 Butler::start_thread()
60 {
61         const float rate = (float)_session.frame_rate();
62
63         /* size is in Samples, not bytes */
64         audio_dstream_buffer_size = (uint32_t) floor (Config->get_audio_track_buffer_seconds() * rate);
65
66         /* size is in bytes
67          * XXX: Jack needs to tell us the MIDI buffer size
68          * (i.e. how many MIDI bytes we might see in a cycle)
69          */
70         midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
71
72         MidiDiskstream::set_readahead_frames ((nframes_t)(Config->get_midi_readahead() * rate));
73
74         Crossfade::set_buffer_size (audio_dstream_buffer_size);
75
76         should_run = false;
77
78         if (pipe (request_pipe)) {
79                 error << string_compose(_("Cannot create transport request signal pipe (%1)"),
80                                 strerror (errno)) << endmsg;
81                 return -1;
82         }
83
84         if (fcntl (request_pipe[0], F_SETFL, O_NONBLOCK)) {
85                 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
86                                 strerror (errno)) << endmsg;
87                 return -1;
88         }
89
90         if (fcntl (request_pipe[1], F_SETFL, O_NONBLOCK)) {
91                 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"),
92                                 strerror (errno)) << endmsg;
93                 return -1;
94         }
95
96         if (pthread_create_and_store ("disk butler", &thread, _thread_work, this)) {
97                 error << _("Session: could not create butler thread") << endmsg;
98                 return -1;
99         }
100
101         //pthread_detach (thread);
102
103         return 0;
104 }
105
106 void
107 Butler::terminate_thread ()
108 {
109         if (thread) {
110                 void* status;
111                 const char c = Request::Quit;
112                 ::write (request_pipe[1], &c, 1);
113                 pthread_join (thread, &status);
114         }
115 }
116
117 void *
118 Butler::_thread_work (void* arg)
119 {
120         SessionEvent::create_per_thread_pool ("butler events", 64);
121         pthread_set_name (X_("butler"));
122         return ((Butler *) arg)->thread_work ();
123 }
124
125 void *
126 Butler::thread_work ()
127 {
128         uint32_t err = 0;
129         int32_t bytes;
130         bool compute_io;
131         microseconds_t begin, end;
132
133         struct pollfd pfd[1];
134         bool disk_work_outstanding = false;
135         RouteList::iterator i;
136
137         while (true) {
138                 pfd[0].fd = request_pipe[0];
139                 pfd[0].events = POLLIN|POLLERR|POLLHUP;
140
141                 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
142
143                         if (errno == EINTR) {
144                                 continue;
145                         }
146
147                         error << string_compose (_("poll on butler request pipe failed (%1)"),
148                                           strerror (errno))
149                               << endmsg;
150                         break;
151                 }
152
153                 if (pfd[0].revents & ~POLLIN) {
154                         error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
155                         break;
156                 }
157
158                 if (pfd[0].revents & POLLIN) {
159
160                         char req;
161
162                         /* empty the pipe of all current requests */
163
164                         while (1) {
165                                 size_t nread = ::read (request_pipe[0], &req, sizeof (req));
166                                 if (nread == 1) {
167
168                                         switch ((Request::Type) req) {
169
170                                         case Request::Wake:
171                                                 break;
172
173                                         case Request::Run:
174                                                 should_run = true;
175                                                 break;
176
177                                         case Request::Pause:
178                                                 should_run = false;
179                                                 break;
180
181                                         case Request::Quit:
182                                                 pthread_exit_pbd (0);
183                                                 /*NOTREACHED*/
184                                                 break;
185
186                                         default:
187                                                 break;
188                                         }
189
190                                 } else if (nread == 0) {
191                                         break;
192                                 } else if (errno == EAGAIN) {
193                                         break;
194                                 } else {
195                                         fatal << _("Error reading from butler request pipe") << endmsg;
196                                         /*NOTREACHED*/
197                                 }
198                         }
199                 }
200
201                 if (transport_work_requested()) {
202                         _session.butler_transport_work ();
203                 }
204
205                 disk_work_outstanding = false;
206                 bytes = 0;
207                 compute_io = true;
208
209                 begin = get_microseconds();
210
211                 boost::shared_ptr<RouteList> rl = _session.get_routes();
212
213 //              for (i = dsl->begin(); i != dsl->end(); ++i) {
214 //                      cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
215 //              }
216
217                 for (i = rl->begin(); !transport_work_requested() && should_run && i != rl->end(); ++i) {
218
219                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
220                         if (!tr) {
221                                 continue;
222                         }
223
224                         /* don't read inactive tracks */
225
226                         boost::shared_ptr<IO> io = tr->input ();
227
228                         if (io && !io->active()) {
229                                 continue;
230                         }
231
232                         switch (tr->do_refill ()) {
233                         case 0:
234                                 bytes += tr->read_data_count();
235                                 break;
236                         case 1:
237                                 bytes += tr->read_data_count();
238                                 disk_work_outstanding = true;
239                                 break;
240
241                         default:
242                                 compute_io = false;
243                                 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
244                                 break;
245                         }
246
247                 }
248
249                 if (i != rl->begin() && i != rl->end()) {
250                         /* we didn't get to all the streams */
251                         disk_work_outstanding = true;
252                 }
253
254                 if (!err && transport_work_requested()) {
255                         continue;
256                 }
257
258                 if (compute_io) {
259                         end = get_microseconds();
260                         if (end - begin > 0) {
261                                 _read_data_rate = (float) bytes / (float) (end - begin);
262                         } else {
263                                 _read_data_rate = 0; // infinity better
264                         }
265                 }
266
267                 bytes = 0;
268                 compute_io = true;
269                 begin = get_microseconds();
270
271                 for (i = rl->begin(); !transport_work_requested() && should_run && i != rl->end(); ++i) {
272                         // cerr << "write behind for " << (*i)->name () << endl;
273
274                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
275                         if (!tr) {
276                                 continue;
277                         }
278                         
279                         /* note that we still try to flush diskstreams attached to inactive routes
280                          */
281
282                         switch (tr->do_flush (ButlerContext)) {
283                         case 0:
284                                 bytes += tr->write_data_count();
285                                 break;
286                         case 1:
287                                 bytes += tr->write_data_count();
288                                 disk_work_outstanding = true;
289                                 break;
290
291                         default:
292                                 err++;
293                                 compute_io = false;
294                                 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
295                                 /* don't break - try to flush all streams in case they
296                                    are split across disks.
297                                 */
298                         }
299                 }
300
301                 if (err && _session.actively_recording()) {
302                         /* stop the transport and try to catch as much possible
303                            captured state as we can.
304                         */
305                         _session.request_stop ();
306                 }
307
308                 if (i != rl->begin() && i != rl->end()) {
309                         /* we didn't get to all the streams */
310                         disk_work_outstanding = true;
311                 }
312
313                 if (!err && transport_work_requested()) {
314                         continue;
315                 }
316
317                 if (compute_io) {
318                         // there are no apparent users for this calculation?
319                         end = get_microseconds();
320                         if (end - begin > 0) {
321                                 _write_data_rate = (float) bytes / (float) (end - begin);
322                         } else {
323                                 _write_data_rate = 0; // Well, infinity would be better
324                         }
325                 }
326
327                 if (!disk_work_outstanding) {
328                         _session.refresh_disk_space ();
329                 }
330
331
332                 {
333                         Glib::Mutex::Lock lm (request_lock);
334
335                         if (should_run && (disk_work_outstanding || transport_work_requested())) {
336 //                              for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
337 //                                      cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
338 //                              }
339
340                                 continue;
341                         }
342
343                         paused.signal();
344                 }
345
346                 empty_pool_trash ();
347         }
348
349         pthread_exit_pbd (0);
350         /*NOTREACHED*/
351         return (0);
352 }
353
354 void
355 Butler::schedule_transport_work ()
356 {
357         g_atomic_int_inc (&should_do_transport_work);
358         summon ();
359 }
360
361 void
362 Butler::summon ()
363 {
364         char c = Request::Run;
365         ::write (request_pipe[1], &c, 1);
366 }
367
368 void
369 Butler::stop ()
370 {
371         Glib::Mutex::Lock lm (request_lock);
372         char c = Request::Pause;
373         ::write (request_pipe[1], &c, 1);
374         paused.wait(request_lock);
375 }
376
377 void
378 Butler::wait_until_finished ()
379 {
380         Glib::Mutex::Lock lm (request_lock);
381         char c = Request::Wake;
382         ::write (request_pipe[1], &c, 1);
383         paused.wait(request_lock);
384 }
385
386 bool
387 Butler::transport_work_requested () const
388 {
389         return g_atomic_int_get(&should_do_transport_work);
390 }
391
392 float
393 Butler::read_data_rate () const
394 {
395         /* disk i/o in excess of 10000MB/sec indicate the buffer cache
396            in action. ignore it.
397         */
398         return _read_data_rate > 10485.7600000f ? 0.0f : _read_data_rate;
399 }
400
401 float
402 Butler::write_data_rate () const
403 {
404         /* disk i/o in excess of 10000MB/sec indicate the buffer cache
405            in action. ignore it.
406         */
407         return _write_data_rate > 10485.7600000f ? 0.0f : _write_data_rate;
408 }
409
410 void
411 Butler::empty_pool_trash ()
412 {
413         /* look in the trash, deleting empty pools until we come to one that is not empty */
414         
415         RingBuffer<CrossThreadPool*>::rw_vector vec;
416         pool_trash.get_read_vector (&vec);
417
418         guint deleted = 0;
419         
420         for (int i = 0; i < 2; ++i) {
421                 for (guint j = 0; j < vec.len[i]; ++j) {
422                         if (vec.buf[i][j]->empty()) {
423                                 delete vec.buf[i][j];
424                                 ++deleted;
425                         } else {
426                                 /* found a non-empty pool, so stop deleting */
427                                 if (deleted) {
428                                         pool_trash.increment_read_idx (deleted);
429                                 }
430                                 return;
431                         }
432                 }
433         }
434
435         if (deleted) {
436                 pool_trash.increment_read_idx (deleted);
437         }
438 }
439
440 void
441 Butler::drop_references ()
442 {
443         SessionEvent::pool->set_trash (0);
444 }
445
446
447 } // namespace ARDOUR
448