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.
28 #include <glibmm/thread.h>
30 #include <pbd/error.h>
31 #include <pbd/pthread_utils.h>
33 #include <ardour/configuration.h>
34 #include <ardour/audioengine.h>
35 #include <ardour/session.h>
36 #include <ardour/audio_diskstream.h>
37 #include <ardour/crossfade.h>
38 #include <ardour/timestamps.h>
43 using namespace ARDOUR;
46 static float _read_data_rate;
47 static float _write_data_rate;
49 /* XXX put this in the right place */
51 static inline uint32_t next_power_of_two (uint32_t n)
63 /*---------------------------------------------------------------------------
65 ---------------------------------------------------------------------------*/
68 Session::start_butler_thread ()
70 /* size is in Samples, not bytes */
72 dstream_buffer_size = (uint32_t) floor (Config->get_track_buffer_seconds() * (float) frame_rate());
74 Crossfade::set_buffer_size (dstream_buffer_size);
76 butler_should_run = false;
78 if (pipe (butler_request_pipe)) {
79 error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
83 if (fcntl (butler_request_pipe[0], F_SETFL, O_NONBLOCK)) {
84 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"), strerror (errno)) << endmsg;
88 if (fcntl (butler_request_pipe[1], F_SETFL, O_NONBLOCK)) {
89 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"), strerror (errno)) << endmsg;
93 if (pthread_create_and_store ("disk butler", &butler_thread, 0, _butler_thread_work, this)) {
94 error << _("Session: could not create butler thread") << endmsg;
98 // pthread_detach (butler_thread);
104 Session::terminate_butler_thread ()
108 char c = ButlerRequest::Quit;
109 ::write (butler_request_pipe[1], &c, 1);
110 pthread_join (butler_thread, &status);
115 Session::schedule_butler_transport_work ()
117 g_atomic_int_inc (&butler_should_do_transport_work);
122 Session::schedule_curve_reallocation ()
124 post_transport_work = PostTransportWork (post_transport_work | PostTransportCurveRealloc);
125 schedule_butler_transport_work ();
129 Session::summon_butler ()
131 char c = ButlerRequest::Run;
132 ::write (butler_request_pipe[1], &c, 1);
136 Session::stop_butler ()
138 Glib::Mutex::Lock lm (butler_request_lock);
139 char c = ButlerRequest::Pause;
140 ::write (butler_request_pipe[1], &c, 1);
141 butler_paused.wait(butler_request_lock);
145 Session::wait_till_butler_finished ()
147 Glib::Mutex::Lock lm (butler_request_lock);
148 char c = ButlerRequest::Wake;
149 ::write (butler_request_pipe[1], &c, 1);
150 butler_paused.wait(butler_request_lock);
154 Session::_butler_thread_work (void* arg)
156 PBD::ThreadCreated (pthread_self(), X_("Butler"));
157 return ((Session *) arg)->butler_thread_work ();
162 Session::butler_thread_work ()
167 struct timeval begin, end;
168 struct pollfd pfd[1];
169 bool disk_work_outstanding = false;
170 DiskstreamList::iterator i;
173 pfd[0].fd = butler_request_pipe[0];
174 pfd[0].events = POLLIN|POLLERR|POLLHUP;
176 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
178 if (errno == EINTR) {
182 error << string_compose (_("poll on butler request pipe failed (%1)"),
188 if (pfd[0].revents & ~POLLIN) {
189 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
193 if (pfd[0].revents & POLLIN) {
197 /* empty the pipe of all current requests */
200 size_t nread = ::read (butler_request_pipe[0], &req, sizeof (req));
203 switch ((ButlerRequest::Type) req) {
205 case ButlerRequest::Wake:
208 case ButlerRequest::Run:
209 butler_should_run = true;
212 case ButlerRequest::Pause:
213 butler_should_run = false;
216 case ButlerRequest::Quit:
217 pthread_exit_pbd (0);
225 } else if (nread == 0) {
227 } else if (errno == EAGAIN) {
230 fatal << _("Error reading from butler request pipe") << endmsg;
236 //for (i = diskstreams.begin(); i != diskstreams.end(); ++i) {
237 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
240 if (transport_work_requested()) {
241 butler_transport_work ();
244 disk_work_outstanding = false;
248 gettimeofday (&begin, 0);
250 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader ();
252 for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
254 boost::shared_ptr<Diskstream> ds = *i;
256 switch (ds->do_refill ()) {
258 bytes += ds->read_data_count();
261 bytes += ds->read_data_count();
262 disk_work_outstanding = true;
267 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
273 if (i != dsl->end()) {
274 /* we didn't get to all the streams */
275 disk_work_outstanding = true;
278 if (!err && transport_work_requested()) {
283 gettimeofday (&end, 0);
285 double b = begin.tv_sec + (begin.tv_usec/1000000.0);
286 double e = end.tv_sec + (end.tv_usec / 1000000.0);
288 _read_data_rate = bytes / (e - b);
293 gettimeofday (&begin, 0);
295 for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
296 // cerr << "write behind for " << (*i)->name () << endl;
298 switch ((*i)->do_flush (Session::ButlerContext)) {
300 bytes += (*i)->write_data_count();
303 bytes += (*i)->write_data_count();
304 disk_work_outstanding = true;
310 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
311 /* don't break - try to flush all streams in case they
312 are split across disks.
317 if (err && actively_recording()) {
318 /* stop the transport and try to catch as much possible
319 captured state as we can.
324 if (i != dsl->end()) {
325 /* we didn't get to all the streams */
326 disk_work_outstanding = true;
329 if (!err && transport_work_requested()) {
334 gettimeofday (&end, 0);
336 double b = begin.tv_sec + (begin.tv_usec/1000000.0);
337 double e = end.tv_sec + (end.tv_usec / 1000000.0);
339 _write_data_rate = bytes / (e - b);
342 if (!disk_work_outstanding) {
343 refresh_disk_space ();
348 Glib::Mutex::Lock lm (butler_request_lock);
350 if (butler_should_run && (disk_work_outstanding || transport_work_requested())) {
351 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
352 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
358 butler_paused.signal();
362 pthread_exit_pbd (0);
369 Session::request_overwrite_buffer (Diskstream* stream)
371 Event *ev = new Event (Event::Overwrite, Event::Add, Event::Immediate, 0, 0, 0.0);
372 ev->set_ptr (stream);
376 /** Process thread. */
378 Session::overwrite_some_buffers (Diskstream* ds)
380 if (actively_recording()) {
386 ds->set_pending_overwrite (true);
390 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
391 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
392 (*i)->set_pending_overwrite (true);
396 post_transport_work = PostTransportWork (post_transport_work | PostTransportOverWrite);
397 schedule_butler_transport_work ();
401 Session::read_data_rate () const
403 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
404 in action. ignore it.
406 return _read_data_rate > 10485760000.0f ? 0.0f : _read_data_rate;
410 Session::write_data_rate () const
412 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
413 in action. ignore it.
415 return _write_data_rate > 10485760000.0f ? 0.0f : _write_data_rate;
419 Session::playback_load ()
421 return (uint32_t) g_atomic_int_get (&_playback_load);
425 Session::capture_load ()
427 return (uint32_t) g_atomic_int_get (&_capture_load);
431 Session::playback_load_min ()
433 return (uint32_t) g_atomic_int_get (&_playback_load_min);
437 Session::capture_load_min ()
439 return (uint32_t) g_atomic_int_get (&_capture_load_min);
443 Session::reset_capture_load_min ()
445 g_atomic_int_set (&_capture_load_min, 100);
450 Session::reset_playback_load_min ()
452 g_atomic_int_set (&_playback_load_min, 100);