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/midi_diskstream.h>
39 #include <ardour/crossfade.h>
40 #include <ardour/timestamps.h>
45 using namespace ARDOUR;
48 static float _read_data_rate;
49 static float _write_data_rate;
51 /* XXX put this in the right place */
53 static inline uint32_t next_power_of_two (uint32_t n)
65 /*---------------------------------------------------------------------------
67 ---------------------------------------------------------------------------*/
70 Session::start_butler_thread ()
72 /* size is in Samples, not bytes */
73 audio_dstream_buffer_size = (uint32_t) floor (Config->get_audio_track_buffer_seconds() * (float) frame_rate());
76 * XXX: Jack needs to tell us the MIDI buffer size
77 * (i.e. how many MIDI bytes we might see in a cycle)
79 midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * (float)frame_rate());
80 MidiDiskstream::set_readahed_frames ((nframes_t) (Config->get_midi_readahead() * (float) frame_rate()));
82 Crossfade::set_buffer_size (audio_dstream_buffer_size);
84 butler_should_run = false;
86 if (pipe (butler_request_pipe)) {
87 error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
91 if (fcntl (butler_request_pipe[0], F_SETFL, O_NONBLOCK)) {
92 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"), strerror (errno)) << endmsg;
96 if (fcntl (butler_request_pipe[1], F_SETFL, O_NONBLOCK)) {
97 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"), strerror (errno)) << endmsg;
101 if (pthread_create_and_store ("disk butler", &butler_thread, 0, _butler_thread_work, this)) {
102 error << _("Session: could not create butler thread") << endmsg;
106 // pthread_detach (butler_thread);
112 Session::terminate_butler_thread ()
116 char c = ButlerRequest::Quit;
117 ::write (butler_request_pipe[1], &c, 1);
118 pthread_join (butler_thread, &status);
123 Session::schedule_butler_transport_work ()
125 g_atomic_int_inc (&butler_should_do_transport_work);
130 Session::schedule_curve_reallocation ()
132 post_transport_work = PostTransportWork (post_transport_work | PostTransportCurveRealloc);
133 schedule_butler_transport_work ();
137 Session::summon_butler ()
139 char c = ButlerRequest::Run;
140 ::write (butler_request_pipe[1], &c, 1);
141 // PBD::stacktrace (cerr);
145 Session::stop_butler ()
147 Glib::Mutex::Lock lm (butler_request_lock);
148 char c = ButlerRequest::Pause;
149 ::write (butler_request_pipe[1], &c, 1);
150 butler_paused.wait(butler_request_lock);
154 Session::wait_till_butler_finished ()
156 Glib::Mutex::Lock lm (butler_request_lock);
157 char c = ButlerRequest::Wake;
158 ::write (butler_request_pipe[1], &c, 1);
159 butler_paused.wait(butler_request_lock);
163 Session::_butler_thread_work (void* arg)
165 PBD::notify_gui_about_thread_creation (pthread_self(), X_("Butler"));
166 return ((Session *) arg)->butler_thread_work ();
171 Session::butler_thread_work ()
176 microseconds_t begin, end;
178 struct pollfd pfd[1];
179 bool disk_work_outstanding = false;
180 DiskstreamList::iterator i;
183 pfd[0].fd = butler_request_pipe[0];
184 pfd[0].events = POLLIN|POLLERR|POLLHUP;
186 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
188 if (errno == EINTR) {
192 error << string_compose (_("poll on butler request pipe failed (%1)"),
198 if (pfd[0].revents & ~POLLIN) {
199 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
203 if (pfd[0].revents & POLLIN) {
207 /* empty the pipe of all current requests */
210 size_t nread = ::read (butler_request_pipe[0], &req, sizeof (req));
213 switch ((ButlerRequest::Type) req) {
215 case ButlerRequest::Wake:
218 case ButlerRequest::Run:
219 butler_should_run = true;
222 case ButlerRequest::Pause:
223 butler_should_run = false;
226 case ButlerRequest::Quit:
227 pthread_exit_pbd (0);
235 } else if (nread == 0) {
237 } else if (errno == EAGAIN) {
240 fatal << _("Error reading from butler request pipe") << endmsg;
246 if (transport_work_requested()) {
247 butler_transport_work ();
250 disk_work_outstanding = false;
254 begin = get_microseconds();
256 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader ();
258 // for (i = dsl->begin(); i != dsl->end(); ++i) {
259 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
262 for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
264 boost::shared_ptr<Diskstream> ds = *i;
266 /* don't read inactive tracks */
270 if (io && !io->active()) {
274 switch (ds->do_refill ()) {
276 bytes += ds->read_data_count();
279 bytes += ds->read_data_count();
280 disk_work_outstanding = true;
285 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
291 if (i != dsl->end()) {
292 /* we didn't get to all the streams */
293 disk_work_outstanding = true;
296 if (!err && transport_work_requested()) {
301 end = get_microseconds();
303 _read_data_rate = (float) bytes / (float) (end - begin);
304 } else { _read_data_rate = 0; // infinity better
310 begin = get_microseconds();
312 for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
313 // cerr << "write behind for " << (*i)->name () << endl;
315 /* note that we still try to flush diskstreams attached to inactive routes
318 switch ((*i)->do_flush (Session::ButlerContext)) {
320 bytes += (*i)->write_data_count();
323 bytes += (*i)->write_data_count();
324 disk_work_outstanding = true;
330 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
331 /* don't break - try to flush all streams in case they
332 are split across disks.
337 if (err && actively_recording()) {
338 /* stop the transport and try to catch as much possible
339 captured state as we can.
344 if (i != dsl->end()) {
345 /* we didn't get to all the streams */
346 disk_work_outstanding = true;
349 if (!err && transport_work_requested()) {
354 // there are no apparent users for this calculation?
355 end = get_microseconds();
357 _write_data_rate = (float) bytes / (float) (end - begin);
359 _write_data_rate = 0; // Well, infinity would be better
363 if (!disk_work_outstanding) {
364 refresh_disk_space ();
369 Glib::Mutex::Lock lm (butler_request_lock);
371 if (butler_should_run && (disk_work_outstanding || transport_work_requested())) {
372 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
373 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
379 butler_paused.signal();
383 pthread_exit_pbd (0);
390 Session::request_overwrite_buffer (Diskstream* stream)
392 Event *ev = new Event (Event::Overwrite, Event::Add, Event::Immediate, 0, 0, 0.0);
393 ev->set_ptr (stream);
397 /** Process thread. */
399 Session::overwrite_some_buffers (Diskstream* ds)
401 if (actively_recording()) {
407 ds->set_pending_overwrite (true);
411 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
412 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
413 (*i)->set_pending_overwrite (true);
417 post_transport_work = PostTransportWork (post_transport_work | PostTransportOverWrite);
418 schedule_butler_transport_work ();
422 Session::read_data_rate () const
424 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
425 in action. ignore it.
427 return _read_data_rate > 10485.7600000f ? 0.0f : _read_data_rate;
431 Session::write_data_rate () const
433 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
434 in action. ignore it.
436 return _write_data_rate > 10485.7600000f ? 0.0f : _write_data_rate;
440 Session::playback_load ()
442 return (uint32_t) g_atomic_int_get (&_playback_load);
446 Session::capture_load ()
448 return (uint32_t) g_atomic_int_get (&_capture_load);
452 Session::playback_load_min ()
454 return (uint32_t) g_atomic_int_get (&_playback_load_min);
458 Session::capture_load_min ()
460 return (uint32_t) g_atomic_int_get (&_capture_load_min);
464 Session::reset_capture_load_min ()
466 g_atomic_int_set (&_capture_load_min, 100);
471 Session::reset_playback_load_min ()
473 g_atomic_int_set (&_playback_load_min, 100);