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/audio_diskstream.h"
35 #include "ardour/audioengine.h"
36 #include "ardour/configuration.h"
37 #include "ardour/crossfade.h"
38 #include "ardour/io.h"
39 #include "ardour/midi_diskstream.h"
40 #include "ardour/session.h"
41 #include "ardour/timestamps.h"
46 using namespace ARDOUR;
49 static float _read_data_rate;
50 static float _write_data_rate;
52 /* XXX put this in the right place */
54 static inline uint32_t next_power_of_two (uint32_t n)
66 /*---------------------------------------------------------------------------
68 ---------------------------------------------------------------------------*/
71 Session::start_butler_thread ()
73 /* size is in Samples, not bytes */
74 audio_dstream_buffer_size = (uint32_t) floor (Config->get_audio_track_buffer_seconds() * (float) frame_rate());
77 * XXX: Jack needs to tell us the MIDI buffer size
78 * (i.e. how many MIDI bytes we might see in a cycle)
80 midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * (float)frame_rate());
81 MidiDiskstream::set_readahead_frames ((nframes_t) (Config->get_midi_readahead() * (float) frame_rate()));
83 Crossfade::set_buffer_size (audio_dstream_buffer_size);
85 butler_should_run = false;
87 if (pipe (butler_request_pipe)) {
88 error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
92 if (fcntl (butler_request_pipe[0], F_SETFL, O_NONBLOCK)) {
93 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"), strerror (errno)) << endmsg;
97 if (fcntl (butler_request_pipe[1], F_SETFL, O_NONBLOCK)) {
98 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"), strerror (errno)) << endmsg;
102 if (pthread_create_and_store ("disk butler", &butler_thread, 0, _butler_thread_work, this)) {
103 error << _("Session: could not create butler thread") << endmsg;
107 // pthread_detach (butler_thread);
113 Session::terminate_butler_thread ()
117 char c = ButlerRequest::Quit;
118 ::write (butler_request_pipe[1], &c, 1);
119 pthread_join (butler_thread, &status);
124 Session::schedule_butler_transport_work ()
126 g_atomic_int_inc (&butler_should_do_transport_work);
131 Session::schedule_curve_reallocation ()
133 post_transport_work = PostTransportWork (post_transport_work | PostTransportCurveRealloc);
134 schedule_butler_transport_work ();
138 Session::summon_butler ()
140 char c = ButlerRequest::Run;
141 ::write (butler_request_pipe[1], &c, 1);
142 // PBD::stacktrace (cerr);
146 Session::stop_butler ()
148 Glib::Mutex::Lock lm (butler_request_lock);
149 char c = ButlerRequest::Pause;
150 ::write (butler_request_pipe[1], &c, 1);
151 butler_paused.wait(butler_request_lock);
155 Session::wait_till_butler_finished ()
157 Glib::Mutex::Lock lm (butler_request_lock);
158 char c = ButlerRequest::Wake;
159 ::write (butler_request_pipe[1], &c, 1);
160 butler_paused.wait(butler_request_lock);
164 Session::_butler_thread_work (void* arg)
166 PBD::notify_gui_about_thread_creation (pthread_self(), X_("Butler"));
167 return ((Session *) arg)->butler_thread_work ();
172 Session::butler_thread_work ()
177 microseconds_t begin, end;
179 struct pollfd pfd[1];
180 bool disk_work_outstanding = false;
181 DiskstreamList::iterator i;
184 pfd[0].fd = butler_request_pipe[0];
185 pfd[0].events = POLLIN|POLLERR|POLLHUP;
187 if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
189 if (errno == EINTR) {
193 error << string_compose (_("poll on butler request pipe failed (%1)"),
199 if (pfd[0].revents & ~POLLIN) {
200 error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
204 if (pfd[0].revents & POLLIN) {
208 /* empty the pipe of all current requests */
211 size_t nread = ::read (butler_request_pipe[0], &req, sizeof (req));
214 switch ((ButlerRequest::Type) req) {
216 case ButlerRequest::Wake:
219 case ButlerRequest::Run:
220 butler_should_run = true;
223 case ButlerRequest::Pause:
224 butler_should_run = false;
227 case ButlerRequest::Quit:
228 pthread_exit_pbd (0);
236 } else if (nread == 0) {
238 } else if (errno == EAGAIN) {
241 fatal << _("Error reading from butler request pipe") << endmsg;
247 if (transport_work_requested()) {
248 butler_transport_work ();
251 disk_work_outstanding = false;
255 begin = get_microseconds();
257 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader ();
259 // for (i = dsl->begin(); i != dsl->end(); ++i) {
260 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
263 for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
265 boost::shared_ptr<Diskstream> ds = *i;
267 /* don't read inactive tracks */
271 if (io && !io->active()) {
275 switch (ds->do_refill ()) {
277 bytes += ds->read_data_count();
280 bytes += ds->read_data_count();
281 disk_work_outstanding = true;
286 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
292 if (i != dsl->end()) {
293 /* we didn't get to all the streams */
294 disk_work_outstanding = true;
297 if (!err && transport_work_requested()) {
302 end = get_microseconds();
304 _read_data_rate = (float) bytes / (float) (end - begin);
305 } else { _read_data_rate = 0; // infinity better
311 begin = get_microseconds();
313 for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
314 // cerr << "write behind for " << (*i)->name () << endl;
316 /* note that we still try to flush diskstreams attached to inactive routes
319 switch ((*i)->do_flush (ButlerContext)) {
321 bytes += (*i)->write_data_count();
324 bytes += (*i)->write_data_count();
325 disk_work_outstanding = true;
331 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
332 /* don't break - try to flush all streams in case they
333 are split across disks.
338 if (err && actively_recording()) {
339 /* stop the transport and try to catch as much possible
340 captured state as we can.
345 if (i != dsl->end()) {
346 /* we didn't get to all the streams */
347 disk_work_outstanding = true;
350 if (!err && transport_work_requested()) {
355 // there are no apparent users for this calculation?
356 end = get_microseconds();
358 _write_data_rate = (float) bytes / (float) (end - begin);
360 _write_data_rate = 0; // Well, infinity would be better
364 if (!disk_work_outstanding) {
365 refresh_disk_space ();
370 Glib::Mutex::Lock lm (butler_request_lock);
372 if (butler_should_run && (disk_work_outstanding || transport_work_requested())) {
373 // for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
374 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
380 butler_paused.signal();
384 pthread_exit_pbd (0);
391 Session::request_overwrite_buffer (Diskstream* stream)
393 Event *ev = new Event (Event::Overwrite, Event::Add, Event::Immediate, 0, 0, 0.0);
394 ev->set_ptr (stream);
398 /** Process thread. */
400 Session::overwrite_some_buffers (Diskstream* ds)
402 if (actively_recording()) {
408 ds->set_pending_overwrite (true);
412 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
413 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
414 (*i)->set_pending_overwrite (true);
418 post_transport_work = PostTransportWork (post_transport_work | PostTransportOverWrite);
419 schedule_butler_transport_work ();
423 Session::read_data_rate () const
425 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
426 in action. ignore it.
428 return _read_data_rate > 10485.7600000f ? 0.0f : _read_data_rate;
432 Session::write_data_rate () const
434 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
435 in action. ignore it.
437 return _write_data_rate > 10485.7600000f ? 0.0f : _write_data_rate;
441 Session::playback_load ()
443 return (uint32_t) g_atomic_int_get (&_playback_load);
447 Session::capture_load ()
449 return (uint32_t) g_atomic_int_get (&_capture_load);
453 Session::playback_load_min ()
455 return (uint32_t) g_atomic_int_get (&_playback_load_min);
459 Session::capture_load_min ()
461 return (uint32_t) g_atomic_int_get (&_capture_load_min);
465 Session::reset_capture_load_min ()
467 g_atomic_int_set (&_capture_load_min, 100);
472 Session::reset_playback_load_min ()
474 g_atomic_int_set (&_playback_load_min, 100);