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 */
72 audio_dstream_buffer_size = (uint32_t) floor (Config->get_audio_track_buffer_seconds() * (float) frame_rate());
75 * XXX: Jack needs to tell us the MIDI buffer size
76 * (i.e. how many MIDI bytes we might see in a cycle)
78 midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * (float)frame_rate());
80 Crossfade::set_buffer_size (audio_dstream_buffer_size);
82 butler_should_run = false;
84 if (pipe (butler_request_pipe)) {
85 error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
89 if (fcntl (butler_request_pipe[0], F_SETFL, O_NONBLOCK)) {
90 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"), strerror (errno)) << endmsg;
94 if (fcntl (butler_request_pipe[1], F_SETFL, O_NONBLOCK)) {
95 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"), strerror (errno)) << endmsg;
99 if (pthread_create_and_store ("disk butler", &butler_thread, 0, _butler_thread_work, this)) {
100 error << _("Session: could not create butler thread") << endmsg;
104 // pthread_detach (butler_thread);
110 Session::terminate_butler_thread ()
114 char c = ButlerRequest::Quit;
115 ::write (butler_request_pipe[1], &c, 1);
116 pthread_join (butler_thread, &status);
121 Session::schedule_butler_transport_work ()
123 g_atomic_int_inc (&butler_should_do_transport_work);
128 Session::schedule_curve_reallocation ()
130 post_transport_work = PostTransportWork (post_transport_work | PostTransportCurveRealloc);
131 schedule_butler_transport_work ();
135 Session::summon_butler ()
137 char c = ButlerRequest::Run;
138 ::write (butler_request_pipe[1], &c, 1);
139 // PBD::stacktrace (cerr);
143 Session::stop_butler ()
145 Glib::Mutex::Lock lm (butler_request_lock);
146 char c = ButlerRequest::Pause;
147 ::write (butler_request_pipe[1], &c, 1);
148 butler_paused.wait(butler_request_lock);
152 Session::wait_till_butler_finished ()
154 Glib::Mutex::Lock lm (butler_request_lock);
155 char c = ButlerRequest::Wake;
156 ::write (butler_request_pipe[1], &c, 1);
157 butler_paused.wait(butler_request_lock);
161 Session::_butler_thread_work (void* arg)
163 PBD::ThreadCreated (pthread_self(), X_("Butler"));
164 return ((Session *) arg)->butler_thread_work ();
169 Session::butler_thread_work ()
174 struct timeval begin, end;
175 struct pollfd pfd[1];
176 bool disk_work_outstanding = false;
177 DiskstreamList::iterator i;
180 pfd[0].fd = butler_request_pipe[0];
181 pfd[0].events = POLLIN|POLLERR|POLLHUP;
183 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
185 if (errno == EINTR) {
189 error << string_compose (_("poll on butler request pipe failed (%1)"),
195 if (pfd[0].revents & ~POLLIN) {
196 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
200 if (pfd[0].revents & POLLIN) {
204 /* empty the pipe of all current requests */
207 size_t nread = ::read (butler_request_pipe[0], &req, sizeof (req));
210 switch ((ButlerRequest::Type) req) {
212 case ButlerRequest::Wake:
215 case ButlerRequest::Run:
216 butler_should_run = true;
219 case ButlerRequest::Pause:
220 butler_should_run = false;
223 case ButlerRequest::Quit:
224 pthread_exit_pbd (0);
232 } else if (nread == 0) {
234 } else if (errno == EAGAIN) {
237 fatal << _("Error reading from butler request pipe") << endmsg;
243 if (transport_work_requested()) {
244 butler_transport_work ();
247 disk_work_outstanding = false;
251 gettimeofday (&begin, 0);
253 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader ();
255 // for (i = dsl->begin(); i != dsl->end(); ++i) {
256 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
259 for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
261 boost::shared_ptr<Diskstream> ds = *i;
263 /* don't read inactive tracks */
267 if (io && !io->active()) {
271 switch (ds->do_refill ()) {
273 bytes += ds->read_data_count();
276 bytes += ds->read_data_count();
277 disk_work_outstanding = true;
282 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
288 if (i != dsl->end()) {
289 /* we didn't get to all the streams */
290 disk_work_outstanding = true;
293 if (!err && transport_work_requested()) {
298 gettimeofday (&end, 0);
300 double b = begin.tv_sec + (begin.tv_usec/1000000.0);
301 double e = end.tv_sec + (end.tv_usec / 1000000.0);
303 _read_data_rate = bytes / (e - b);
308 gettimeofday (&begin, 0);
310 for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
311 // cerr << "write behind for " << (*i)->name () << endl;
313 /* note that we still try to flush diskstreams attached to inactive routes
316 switch ((*i)->do_flush (Session::ButlerContext)) {
318 bytes += (*i)->write_data_count();
321 bytes += (*i)->write_data_count();
322 disk_work_outstanding = true;
328 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
329 /* don't break - try to flush all streams in case they
330 are split across disks.
335 if (err && actively_recording()) {
336 /* stop the transport and try to catch as much possible
337 captured state as we can.
342 if (i != dsl->end()) {
343 /* we didn't get to all the streams */
344 disk_work_outstanding = true;
347 if (!err && transport_work_requested()) {
352 gettimeofday (&end, 0);
354 double b = begin.tv_sec + (begin.tv_usec/1000000.0);
355 double e = end.tv_sec + (end.tv_usec / 1000000.0);
357 _write_data_rate = bytes / (e - b);
360 if (!disk_work_outstanding) {
361 refresh_disk_space ();
366 Glib::Mutex::Lock lm (butler_request_lock);
368 if (butler_should_run && (disk_work_outstanding || transport_work_requested())) {
369 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
370 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
376 butler_paused.signal();
380 pthread_exit_pbd (0);
387 Session::request_overwrite_buffer (Diskstream* stream)
389 Event *ev = new Event (Event::Overwrite, Event::Add, Event::Immediate, 0, 0, 0.0);
390 ev->set_ptr (stream);
394 /** Process thread. */
396 Session::overwrite_some_buffers (Diskstream* ds)
398 if (actively_recording()) {
404 ds->set_pending_overwrite (true);
408 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
409 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
410 (*i)->set_pending_overwrite (true);
414 post_transport_work = PostTransportWork (post_transport_work | PostTransportOverWrite);
415 schedule_butler_transport_work ();
419 Session::read_data_rate () const
421 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
422 in action. ignore it.
424 return _read_data_rate > 10485760000.0f ? 0.0f : _read_data_rate;
428 Session::write_data_rate () const
430 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
431 in action. ignore it.
433 return _write_data_rate > 10485760000.0f ? 0.0f : _write_data_rate;
437 Session::playback_load ()
439 return (uint32_t) g_atomic_int_get (&_playback_load);
443 Session::capture_load ()
445 return (uint32_t) g_atomic_int_get (&_capture_load);
449 Session::playback_load_min ()
451 return (uint32_t) g_atomic_int_get (&_playback_load_min);
455 Session::capture_load_min ()
457 return (uint32_t) g_atomic_int_get (&_capture_load_min);
461 Session::reset_capture_load_min ()
463 g_atomic_int_set (&_capture_load_min, 100);
468 Session::reset_playback_load_min ()
470 g_atomic_int_set (&_playback_load_min, 100);