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 DiskstreamList::iterator i;
174 pfd[0].fd = butler_request_pipe[0];
175 pfd[0].events = POLLIN|POLLERR|POLLHUP;
177 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
179 if (errno == EINTR) {
183 error << string_compose (_("poll on butler request pipe failed (%1)"),
189 if (pfd[0].revents & ~POLLIN) {
190 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
194 if (pfd[0].revents & POLLIN) {
198 /* empty the pipe of all current requests */
201 size_t nread = ::read (butler_request_pipe[0], &req, sizeof (req));
204 switch ((ButlerRequest::Type) req) {
206 case ButlerRequest::Wake:
209 case ButlerRequest::Run:
210 butler_should_run = true;
213 case ButlerRequest::Pause:
214 butler_should_run = false;
217 case ButlerRequest::Quit:
218 pthread_exit_pbd (0);
226 } else if (nread == 0) {
228 } else if (errno == EAGAIN) {
231 fatal << _("Error reading from butler request pipe") << endmsg;
237 //for (i = diskstreams.begin(); i != diskstreams.end(); ++i) {
238 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
241 if (transport_work_requested()) {
242 butler_transport_work ();
245 disk_work_outstanding = false;
249 gettimeofday (&begin, 0);
251 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader ();
253 for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
255 boost::shared_ptr<Diskstream> ds = *i;
257 switch (ds->do_refill ()) {
259 bytes += ds->read_data_count();
262 bytes += ds->read_data_count();
263 disk_work_outstanding = true;
268 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
274 if (i != dsl->end()) {
275 /* we didn't get to all the streams */
276 disk_work_outstanding = true;
279 if (!err && transport_work_requested()) {
284 gettimeofday (&end, 0);
286 double b = begin.tv_sec + (begin.tv_usec/1000000.0);
287 double e = end.tv_sec + (end.tv_usec / 1000000.0);
289 _read_data_rate = bytes / (e - b);
294 gettimeofday (&begin, 0);
296 for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
297 // cerr << "write behind for " << (*i)->name () << endl;
299 switch ((*i)->do_flush (Session::ButlerContext)) {
301 bytes += (*i)->write_data_count();
304 bytes += (*i)->write_data_count();
305 disk_work_outstanding = true;
311 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
312 /* don't break - try to flush all streams in case they
313 are split across disks.
318 if (err && actively_recording()) {
319 /* stop the transport and try to catch as much possible
320 captured state as we can.
325 if (i != dsl->end()) {
326 /* we didn't get to all the streams */
327 disk_work_outstanding = true;
330 if (!err && transport_work_requested()) {
335 gettimeofday (&end, 0);
337 double b = begin.tv_sec + (begin.tv_usec/1000000.0);
338 double e = end.tv_sec + (end.tv_usec / 1000000.0);
340 _write_data_rate = bytes / (e - b);
343 if (!disk_work_outstanding) {
344 refresh_disk_space ();
349 Glib::Mutex::Lock lm (butler_request_lock);
351 if (butler_should_run && (disk_work_outstanding || transport_work_requested())) {
352 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
353 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
359 butler_paused.signal();
363 pthread_exit_pbd (0);
370 Session::request_overwrite_buffer (Diskstream* stream)
372 Event *ev = new Event (Event::Overwrite, Event::Add, Event::Immediate, 0, 0, 0.0);
373 ev->set_ptr (stream);
377 /** Process thread. */
379 Session::overwrite_some_buffers (Diskstream* ds)
381 if (actively_recording()) {
387 ds->set_pending_overwrite (true);
391 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
392 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
393 (*i)->set_pending_overwrite (true);
397 post_transport_work = PostTransportWork (post_transport_work | PostTransportOverWrite);
398 schedule_butler_transport_work ();
402 Session::read_data_rate () const
404 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
405 in action. ignore it.
407 return _read_data_rate > 10485760000.0f ? 0.0f : _read_data_rate;
411 Session::write_data_rate () const
413 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
414 in action. ignore it.
416 return _write_data_rate > 10485760000.0f ? 0.0f : _write_data_rate;
420 Session::playback_load ()
422 return (uint32_t) g_atomic_int_get (&_playback_load);
426 Session::capture_load ()
428 return (uint32_t) g_atomic_int_get (&_capture_load);
432 Session::playback_load_min ()
434 return (uint32_t) g_atomic_int_get (&_playback_load_min);
438 Session::capture_load_min ()
440 return (uint32_t) g_atomic_int_get (&_capture_load_min);
444 Session::reset_capture_load_min ()
446 g_atomic_int_set (&_capture_load_min, 100);
451 Session::reset_playback_load_min ()
453 g_atomic_int_set (&_playback_load_min, 100);