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>
32 #include <pbd/stacktrace.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 ()
109 char c = ButlerRequest::Quit;
110 ::write (butler_request_pipe[1], &c, 1);
111 pthread_join (butler_thread, &status);
116 Session::schedule_butler_transport_work ()
118 g_atomic_int_inc (&butler_should_do_transport_work);
123 Session::schedule_curve_reallocation ()
125 post_transport_work = PostTransportWork (post_transport_work | PostTransportCurveRealloc);
126 schedule_butler_transport_work ();
130 Session::summon_butler ()
132 char c = ButlerRequest::Run;
133 ::write (butler_request_pipe[1], &c, 1);
134 // PBD::stacktrace (cerr);
138 Session::stop_butler ()
140 Glib::Mutex::Lock lm (butler_request_lock);
141 char c = ButlerRequest::Pause;
142 ::write (butler_request_pipe[1], &c, 1);
143 butler_paused.wait(butler_request_lock);
147 Session::wait_till_butler_finished ()
149 Glib::Mutex::Lock lm (butler_request_lock);
150 char c = ButlerRequest::Wake;
151 ::write (butler_request_pipe[1], &c, 1);
152 butler_paused.wait(butler_request_lock);
156 Session::_butler_thread_work (void* arg)
158 PBD::notify_gui_about_thread_creation (pthread_self(), X_("Butler"));
159 return ((Session *) arg)->butler_thread_work ();
164 Session::butler_thread_work ()
169 microseconds_t begin, end;
171 struct pollfd pfd[1];
172 bool disk_work_outstanding = false;
173 DiskstreamList::iterator i;
176 pfd[0].fd = butler_request_pipe[0];
177 pfd[0].events = POLLIN|POLLERR|POLLHUP;
179 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
181 if (errno == EINTR) {
185 error << string_compose (_("poll on butler request pipe failed (%1)"),
191 if (pfd[0].revents & ~POLLIN) {
192 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
196 if (pfd[0].revents & POLLIN) {
200 /* empty the pipe of all current requests */
203 size_t nread = ::read (butler_request_pipe[0], &req, sizeof (req));
206 switch ((ButlerRequest::Type) req) {
208 case ButlerRequest::Wake:
211 case ButlerRequest::Run:
212 butler_should_run = true;
215 case ButlerRequest::Pause:
216 butler_should_run = false;
219 case ButlerRequest::Quit:
220 pthread_exit_pbd (0);
228 } else if (nread == 0) {
230 } else if (errno == EAGAIN) {
233 fatal << _("Error reading from butler request pipe") << endmsg;
239 if (transport_work_requested()) {
240 butler_transport_work ();
243 disk_work_outstanding = false;
247 begin = get_microseconds();
249 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader ();
251 // for (i = dsl->begin(); i != dsl->end(); ++i) {
252 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
255 for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
257 boost::shared_ptr<Diskstream> ds = *i;
259 /* don't read inactive tracks */
263 if (io && !io->active()) {
267 switch (ds->do_refill ()) {
269 bytes += ds->read_data_count();
272 bytes += ds->read_data_count();
273 disk_work_outstanding = true;
278 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
284 if (i != dsl->end()) {
285 /* we didn't get to all the streams */
286 disk_work_outstanding = true;
289 if (!err && transport_work_requested()) {
294 end = get_microseconds();
296 _read_data_rate = (float) bytes / (float) (end - begin);
297 } else { _read_data_rate = 0; // infinity better
303 begin = get_microseconds();
305 for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
306 // cerr << "write behind for " << (*i)->name () << endl;
308 /* note that we still try to flush diskstreams attached to inactive routes
311 switch ((*i)->do_flush (Session::ButlerContext)) {
313 bytes += (*i)->write_data_count();
316 bytes += (*i)->write_data_count();
317 disk_work_outstanding = true;
323 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
324 /* don't break - try to flush all streams in case they
325 are split across disks.
330 if (err && actively_recording()) {
331 /* stop the transport and try to catch as much possible
332 captured state as we can.
337 if (i != dsl->end()) {
338 /* we didn't get to all the streams */
339 disk_work_outstanding = true;
342 if (!err && transport_work_requested()) {
347 // there are no apparent users for this calculation?
348 end = get_microseconds();
350 _write_data_rate = (float) bytes / (float) (end - begin);
352 _write_data_rate = 0; // Well, infinity would be better
356 if (!disk_work_outstanding) {
357 refresh_disk_space ();
362 Glib::Mutex::Lock lm (butler_request_lock);
364 if (butler_should_run && (disk_work_outstanding || transport_work_requested())) {
365 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
366 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
372 butler_paused.signal();
376 pthread_exit_pbd (0);
383 Session::request_overwrite_buffer (Diskstream* stream)
385 Event *ev = new Event (Event::Overwrite, Event::Add, Event::Immediate, 0, 0, 0.0);
386 ev->set_ptr (stream);
390 /** Process thread. */
392 Session::overwrite_some_buffers (Diskstream* ds)
394 if (actively_recording()) {
400 ds->set_pending_overwrite (true);
404 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
405 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
406 (*i)->set_pending_overwrite (true);
410 post_transport_work = PostTransportWork (post_transport_work | PostTransportOverWrite);
411 schedule_butler_transport_work ();
415 Session::read_data_rate () const
417 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
418 in action. ignore it.
420 return _read_data_rate > 10485.7600000f ? 0.0f : _read_data_rate;
424 Session::write_data_rate () const
426 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
427 in action. ignore it.
429 return _write_data_rate > 10485.7600000f ? 0.0f : _write_data_rate;
433 Session::playback_load ()
435 return (uint32_t) g_atomic_int_get (&_playback_load);
439 Session::capture_load ()
441 return (uint32_t) g_atomic_int_get (&_capture_load);
445 Session::playback_load_min ()
447 return (uint32_t) g_atomic_int_get (&_playback_load_min);
451 Session::capture_load_min ()
453 return (uint32_t) g_atomic_int_get (&_capture_load_min);
457 Session::reset_capture_load_min ()
459 g_atomic_int_set (&_capture_load_min, 100);
464 Session::reset_playback_load_min ()
466 g_atomic_int_set (&_playback_load_min, 100);