2 Copyright (C) 1999-2002 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.
29 #include <glibmm/thread.h>
31 #include <pbd/error.h>
32 #include <pbd/pthread_utils.h>
34 #include <ardour/configuration.h>
35 #include <ardour/audioengine.h>
36 #include <ardour/session.h>
37 #include <ardour/audio_diskstream.h>
38 #include <ardour/crossfade.h>
39 #include <ardour/timestamps.h>
44 using namespace ARDOUR;
47 static float _read_data_rate;
48 static float _write_data_rate;
50 /* XXX put this in the right place */
52 static inline uint32_t next_power_of_two (uint32_t n)
64 /*---------------------------------------------------------------------------
66 ---------------------------------------------------------------------------*/
69 Session::start_butler_thread ()
71 /* size is in Samples, not bytes */
73 dstream_buffer_size = (uint32_t) floor (Config->get_track_buffer_seconds() * (float) frame_rate());
75 Crossfade::set_buffer_size (dstream_buffer_size);
77 butler_should_run = false;
79 if (pipe (butler_request_pipe)) {
80 error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
84 if (fcntl (butler_request_pipe[0], F_SETFL, O_NONBLOCK)) {
85 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"), strerror (errno)) << endmsg;
89 if (fcntl (butler_request_pipe[1], F_SETFL, O_NONBLOCK)) {
90 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"), strerror (errno)) << endmsg;
94 if (pthread_create_and_store ("disk butler", &butler_thread, 0, _butler_thread_work, this)) {
95 error << _("Session: could not create butler thread") << endmsg;
99 // pthread_detach (butler_thread);
105 Session::terminate_butler_thread ()
108 char c = ButlerRequest::Quit;
109 ::write (butler_request_pipe[1], &c, 1);
110 pthread_join (butler_thread, &status);
114 Session::schedule_butler_transport_work ()
116 g_atomic_int_inc (&butler_should_do_transport_work);
121 Session::schedule_curve_reallocation ()
123 post_transport_work = PostTransportWork (post_transport_work | PostTransportCurveRealloc);
124 schedule_butler_transport_work ();
128 Session::summon_butler ()
130 char c = ButlerRequest::Run;
131 ::write (butler_request_pipe[1], &c, 1);
135 Session::stop_butler ()
137 Glib::Mutex::Lock lm (butler_request_lock);
138 char c = ButlerRequest::Pause;
139 ::write (butler_request_pipe[1], &c, 1);
140 butler_paused.wait(butler_request_lock);
144 Session::wait_till_butler_finished ()
146 Glib::Mutex::Lock lm (butler_request_lock);
147 char c = ButlerRequest::Wake;
148 ::write (butler_request_pipe[1], &c, 1);
149 butler_paused.wait(butler_request_lock);
153 Session::_butler_thread_work (void* arg)
155 PBD::ThreadCreated (pthread_self(), X_("Butler"));
156 return ((Session *) arg)->butler_thread_work ();
160 #define transport_work_requested() g_atomic_int_get(&butler_should_do_transport_work)
163 Session::butler_thread_work ()
168 struct timeval begin, end;
169 struct pollfd pfd[1];
170 bool disk_work_outstanding = false;
171 AudioDiskstreamList::iterator i;
173 butler_mixdown_buffer = new Sample[AudioDiskstream::disk_io_frames()];
174 butler_gain_buffer = new gain_t[AudioDiskstream::disk_io_frames()];
175 // this buffer is used for temp conversion purposes in filesources
176 char * conv_buffer = conversion_buffer(ButlerContext);
180 pfd[0].fd = butler_request_pipe[0];
181 pfd[0].events = POLLIN|POLLERR|POLLHUP;
183 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
185 if (errno == EINTR) {
189 error << string_compose (_("poll on butler request pipe failed (%1)"),
195 if (pfd[0].revents & ~POLLIN) {
196 error << _("Error on butler thread request pipe") << endmsg;
200 if (pfd[0].revents & POLLIN) {
204 /* empty the pipe of all current requests */
207 size_t nread = ::read (butler_request_pipe[0], &req, sizeof (req));
211 switch ((ButlerRequest::Type) req) {
213 case ButlerRequest::Wake:
216 case ButlerRequest::Run:
217 butler_should_run = true;
220 case ButlerRequest::Pause:
221 butler_should_run = false;
224 case ButlerRequest::Quit:
225 pthread_exit_pbd (0);
233 } else if (nread == 0) {
235 } else if (errno == EAGAIN) {
238 fatal << _("Error reading from butler request pipe") << endmsg;
244 for (i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
245 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
248 if (transport_work_requested()) {
249 butler_transport_work ();
252 disk_work_outstanding = false;
256 gettimeofday (&begin, 0);
258 Glib::RWLock::ReaderLock dsm (diskstream_lock);
260 for (i = audio_diskstreams.begin(); !transport_work_requested() && butler_should_run && i != audio_diskstreams.end(); ++i) {
262 // cerr << "rah fondr " << (*i)->io()->name () << endl;
264 switch ((*i)->do_refill (butler_mixdown_buffer, butler_gain_buffer, conv_buffer)) {
266 bytes += (*i)->read_data_count();
269 bytes += (*i)->read_data_count();
270 disk_work_outstanding = true;
275 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
281 if (i != audio_diskstreams.end()) {
282 /* we didn't get to all the streams */
283 disk_work_outstanding = true;
286 if (!err && transport_work_requested()) {
291 gettimeofday (&end, 0);
293 double b = begin.tv_sec + (begin.tv_usec/1000000.0);
294 double e = end.tv_sec + (end.tv_usec / 1000000.0);
296 _read_data_rate = bytes / (e - b);
301 gettimeofday (&begin, 0);
303 for (i = audio_diskstreams.begin(); !transport_work_requested() && butler_should_run && i != audio_diskstreams.end(); ++i) {
305 // cerr << "write behind for " << (*i)->name () << endl;
307 switch ((*i)->do_flush (conv_buffer)) {
309 bytes += (*i)->write_data_count();
312 bytes += (*i)->write_data_count();
313 disk_work_outstanding = true;
319 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
320 /* don't break - try to flush all streams in case they
321 are split across disks.
326 if (err && actively_recording()) {
327 /* stop the transport and try to catch as much possible
328 captured state as we can.
333 if (i != audio_diskstreams.end()) {
334 /* we didn't get to all the streams */
335 disk_work_outstanding = true;
338 if (!err && transport_work_requested()) {
343 gettimeofday (&end, 0);
345 double b = begin.tv_sec + (begin.tv_usec/1000000.0);
346 double e = end.tv_sec + (end.tv_usec / 1000000.0);
348 _write_data_rate = bytes / (e - b);
351 if (!disk_work_outstanding) {
352 refresh_disk_space ();
357 Glib::Mutex::Lock lm (butler_request_lock);
359 if (butler_should_run && (disk_work_outstanding || transport_work_requested())) {
360 // for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
361 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
367 butler_paused.signal();
371 pthread_exit_pbd (0);
378 Session::request_overwrite_buffer (AudioDiskstream* stream)
380 Event *ev = new Event (Event::Overwrite, Event::Add, Event::Immediate, 0, 0, 0.0);
381 ev->set_ptr (stream);
386 Session::overwrite_some_buffers (AudioDiskstream* ds)
388 /* executed by the audio thread */
390 if (actively_recording()) {
396 ds->set_pending_overwrite (true);
400 Glib::RWLock::ReaderLock dm (diskstream_lock);
401 for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
402 (*i)->set_pending_overwrite (true);
406 post_transport_work = PostTransportWork (post_transport_work | PostTransportOverWrite);
407 schedule_butler_transport_work ();
411 Session::read_data_rate () const
413 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
414 in action. ignore it.
416 return _read_data_rate > 10485760000.0f ? 0.0f : _read_data_rate;
420 Session::write_data_rate () const
422 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
423 in action. ignore it.
425 return _write_data_rate > 10485760000.0f ? 0.0f : _write_data_rate;
429 Session::playback_load ()
431 return (uint32_t) g_atomic_int_get (&_playback_load);
435 Session::capture_load ()
437 return (uint32_t) g_atomic_int_get (&_capture_load);
441 Session::playback_load_min ()
443 return (uint32_t) g_atomic_int_get (&_playback_load_min);
447 Session::capture_load_min ()
449 return (uint32_t) g_atomic_int_get (&_capture_load_min);
453 Session::reset_capture_load_min ()
455 g_atomic_int_set (&_capture_load_min, 100);
460 Session::reset_playback_load_min ()
462 g_atomic_int_set (&_playback_load_min, 100);