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 ();
161 Session::butler_thread_work ()
166 struct timeval begin, end;
167 struct pollfd pfd[1];
168 bool disk_work_outstanding = false;
169 DiskstreamList::iterator i;
172 pfd[0].fd = butler_request_pipe[0];
173 pfd[0].events = POLLIN|POLLERR|POLLHUP;
175 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
177 if (errno == EINTR) {
181 error << string_compose (_("poll on butler request pipe failed (%1)"),
187 if (pfd[0].revents & ~POLLIN) {
188 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
192 if (pfd[0].revents & POLLIN) {
196 /* empty the pipe of all current requests */
199 size_t nread = ::read (butler_request_pipe[0], &req, sizeof (req));
202 switch ((ButlerRequest::Type) req) {
204 case ButlerRequest::Wake:
207 case ButlerRequest::Run:
208 butler_should_run = true;
211 case ButlerRequest::Pause:
212 butler_should_run = false;
215 case ButlerRequest::Quit:
216 pthread_exit_pbd (0);
224 } else if (nread == 0) {
226 } else if (errno == EAGAIN) {
229 fatal << _("Error reading from butler request pipe") << endmsg;
235 //for (i = diskstreams.begin(); i != diskstreams.end(); ++i) {
236 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
239 if (transport_work_requested()) {
240 butler_transport_work ();
243 disk_work_outstanding = false;
247 gettimeofday (&begin, 0);
249 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader ();
251 for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
253 boost::shared_ptr<Diskstream> ds = *i;
255 switch (ds->do_refill ()) {
257 bytes += ds->read_data_count();
260 bytes += ds->read_data_count();
261 disk_work_outstanding = true;
266 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
272 if (i != dsl->end()) {
273 /* we didn't get to all the streams */
274 disk_work_outstanding = true;
277 if (!err && transport_work_requested()) {
282 gettimeofday (&end, 0);
284 double b = begin.tv_sec + (begin.tv_usec/1000000.0);
285 double e = end.tv_sec + (end.tv_usec / 1000000.0);
287 _read_data_rate = bytes / (e - b);
292 gettimeofday (&begin, 0);
294 for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
295 // cerr << "write behind for " << (*i)->name () << endl;
297 switch ((*i)->do_flush (Session::ButlerContext)) {
299 bytes += (*i)->write_data_count();
302 bytes += (*i)->write_data_count();
303 disk_work_outstanding = true;
309 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
310 /* don't break - try to flush all streams in case they
311 are split across disks.
316 if (err && actively_recording()) {
317 /* stop the transport and try to catch as much possible
318 captured state as we can.
323 if (i != dsl->end()) {
324 /* we didn't get to all the streams */
325 disk_work_outstanding = true;
328 if (!err && transport_work_requested()) {
333 gettimeofday (&end, 0);
335 double b = begin.tv_sec + (begin.tv_usec/1000000.0);
336 double e = end.tv_sec + (end.tv_usec / 1000000.0);
338 _write_data_rate = bytes / (e - b);
341 if (!disk_work_outstanding) {
342 refresh_disk_space ();
347 Glib::Mutex::Lock lm (butler_request_lock);
349 if (butler_should_run && (disk_work_outstanding || transport_work_requested())) {
350 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
351 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
357 butler_paused.signal();
361 pthread_exit_pbd (0);
368 Session::request_overwrite_buffer (Diskstream* stream)
370 Event *ev = new Event (Event::Overwrite, Event::Add, Event::Immediate, 0, 0, 0.0);
371 ev->set_ptr (stream);
375 /** Process thread. */
377 Session::overwrite_some_buffers (Diskstream* ds)
379 if (actively_recording()) {
385 ds->set_pending_overwrite (true);
389 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
390 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
391 (*i)->set_pending_overwrite (true);
395 post_transport_work = PostTransportWork (post_transport_work | PostTransportOverWrite);
396 schedule_butler_transport_work ();
400 Session::read_data_rate () const
402 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
403 in action. ignore it.
405 return _read_data_rate > 10485760000.0f ? 0.0f : _read_data_rate;
409 Session::write_data_rate () const
411 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
412 in action. ignore it.
414 return _write_data_rate > 10485760000.0f ? 0.0f : _write_data_rate;
418 Session::playback_load ()
420 return (uint32_t) g_atomic_int_get (&_playback_load);
424 Session::capture_load ()
426 return (uint32_t) g_atomic_int_get (&_capture_load);
430 Session::playback_load_min ()
432 return (uint32_t) g_atomic_int_get (&_playback_load_min);
436 Session::capture_load_min ()
438 return (uint32_t) g_atomic_int_get (&_capture_load_min);
442 Session::reset_capture_load_min ()
444 g_atomic_int_set (&_capture_load_min, 100);
449 Session::reset_playback_load_min ()
451 g_atomic_int_set (&_playback_load_min, 100);