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 if (transport_work_requested()) {
237 butler_transport_work ();
240 disk_work_outstanding = false;
244 gettimeofday (&begin, 0);
246 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader ();
248 // for (i = dsl->begin(); i != dsl->end(); ++i) {
249 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
252 for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
254 boost::shared_ptr<Diskstream> ds = *i;
256 /* don't read inactive tracks */
260 if (io && !io->active()) {
264 switch (ds->do_refill ()) {
266 bytes += ds->read_data_count();
269 bytes += ds->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 != dsl->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 = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
304 // cerr << "write behind for " << (*i)->name () << endl;
306 /* note that we still try to flush diskstreams attached to inactive routes
309 switch ((*i)->do_flush (Session::ButlerContext)) {
311 bytes += (*i)->write_data_count();
314 bytes += (*i)->write_data_count();
315 disk_work_outstanding = true;
321 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
322 /* don't break - try to flush all streams in case they
323 are split across disks.
328 if (err && actively_recording()) {
329 /* stop the transport and try to catch as much possible
330 captured state as we can.
335 if (i != dsl->end()) {
336 /* we didn't get to all the streams */
337 disk_work_outstanding = true;
340 if (!err && transport_work_requested()) {
345 gettimeofday (&end, 0);
347 double b = begin.tv_sec + (begin.tv_usec/1000000.0);
348 double e = end.tv_sec + (end.tv_usec / 1000000.0);
350 _write_data_rate = bytes / (e - b);
353 if (!disk_work_outstanding) {
354 refresh_disk_space ();
359 Glib::Mutex::Lock lm (butler_request_lock);
361 if (butler_should_run && (disk_work_outstanding || transport_work_requested())) {
362 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
363 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
369 butler_paused.signal();
373 pthread_exit_pbd (0);
380 Session::request_overwrite_buffer (Diskstream* stream)
382 Event *ev = new Event (Event::Overwrite, Event::Add, Event::Immediate, 0, 0, 0.0);
383 ev->set_ptr (stream);
387 /** Process thread. */
389 Session::overwrite_some_buffers (Diskstream* ds)
391 if (actively_recording()) {
397 ds->set_pending_overwrite (true);
401 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
402 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
403 (*i)->set_pending_overwrite (true);
407 post_transport_work = PostTransportWork (post_transport_work | PostTransportOverWrite);
408 schedule_butler_transport_work ();
412 Session::read_data_rate () const
414 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
415 in action. ignore it.
417 return _read_data_rate > 10485760000.0f ? 0.0f : _read_data_rate;
421 Session::write_data_rate () const
423 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
424 in action. ignore it.
426 return _write_data_rate > 10485760000.0f ? 0.0f : _write_data_rate;
430 Session::playback_load ()
432 return (uint32_t) g_atomic_int_get (&_playback_load);
436 Session::capture_load ()
438 return (uint32_t) g_atomic_int_get (&_capture_load);
442 Session::playback_load_min ()
444 return (uint32_t) g_atomic_int_get (&_playback_load_min);
448 Session::capture_load_min ()
450 return (uint32_t) g_atomic_int_get (&_capture_load_min);
454 Session::reset_capture_load_min ()
456 g_atomic_int_set (&_capture_load_min, 100);
461 Session::reset_playback_load_min ()
463 g_atomic_int_set (&_playback_load_min, 100);