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 <pbd/error.h>
30 #include <pbd/lockmonitor.h>
31 #include <pbd/pthread_utils.h>
33 #include <ardour/configuration.h>
34 #include <ardour/audioengine.h>
35 #include <ardour/session.h>
36 #include <ardour/diskstream.h>
37 #include <ardour/crossfade.h>
38 #include <ardour/timestamps.h>
43 using namespace ARDOUR;
44 //using namespace sigc;
46 static float _read_data_rate;
47 static float _write_data_rate;
49 /* XXX put this in the right place */
51 static inline uint32_t next_power_of_two (uint32_t n)
63 /*---------------------------------------------------------------------------
65 ---------------------------------------------------------------------------*/
68 Session::start_butler_thread ()
70 /* size is in Samples, not bytes */
72 dstream_buffer_size = (uint32_t) floor (Config->get_track_buffer() * (float) frame_rate());
74 Crossfade::set_buffer_size (dstream_buffer_size);
76 pthread_cond_init (&butler_paused, 0);
78 butler_should_run = false;
80 if (pipe (butler_request_pipe)) {
81 error << string_compose(_("Cannot create transport request signal pipe (%1)"), strerror (errno)) << endmsg;
85 if (fcntl (butler_request_pipe[0], F_SETFL, O_NONBLOCK)) {
86 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"), strerror (errno)) << endmsg;
90 if (fcntl (butler_request_pipe[1], F_SETFL, O_NONBLOCK)) {
91 error << string_compose(_("UI: cannot set O_NONBLOCK on butler request pipe (%1)"), strerror (errno)) << endmsg;
95 if (pthread_create_and_store ("disk butler", &butler_thread, 0, _butler_thread_work, this)) {
96 error << _("Session: could not create butler thread") << endmsg;
100 // pthread_detach (butler_thread);
106 Session::terminate_butler_thread ()
109 char c = ButlerRequest::Quit;
110 ::write (butler_request_pipe[1], &c, 1);
111 pthread_join (butler_thread, &status);
115 Session::schedule_butler_transport_work ()
117 atomic_inc (&butler_should_do_transport_work);
122 Session::schedule_curve_reallocation ()
124 post_transport_work = PostTransportWork (post_transport_work | PostTransportCurveRealloc);
125 schedule_butler_transport_work ();
129 Session::summon_butler ()
131 char c = ButlerRequest::Run;
132 ::write (butler_request_pipe[1], &c, 1);
136 Session::stop_butler ()
138 LockMonitor lm (butler_request_lock, __LINE__, __FILE__);
139 char c = ButlerRequest::Pause;
140 ::write (butler_request_pipe[1], &c, 1);
141 pthread_cond_wait (&butler_paused, butler_request_lock.mutex());
145 Session::wait_till_butler_finished ()
147 LockMonitor lm (butler_request_lock, __LINE__, __FILE__);
148 char c = ButlerRequest::Wake;
149 ::write (butler_request_pipe[1], &c, 1);
150 pthread_cond_wait (&butler_paused, butler_request_lock.mutex());
154 Session::_butler_thread_work (void* arg)
156 PBD::ThreadCreated (pthread_self(), X_("Butler"));
158 return ((Session *) arg)->butler_thread_work ();
162 #define transport_work_requested() atomic_read(&butler_should_do_transport_work)
165 Session::butler_thread_work ()
170 struct timeval begin, end;
171 struct pollfd pfd[1];
172 bool disk_work_outstanding = false;
173 DiskStreamList::iterator i;
175 butler_mixdown_buffer = new Sample[DiskStream::disk_io_frames()];
176 butler_gain_buffer = new gain_t[DiskStream::disk_io_frames()];
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 << _("Error on butler thread request pipe") << 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));
211 switch ((ButlerRequest::Type) req) {
213 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 // for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
244 // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
247 if (transport_work_requested()) {
248 butler_transport_work ();
251 disk_work_outstanding = false;
255 gettimeofday (&begin, 0);
257 RWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
259 for (i = diskstreams.begin(); !transport_work_requested() && butler_should_run && i != diskstreams.end(); ++i) {
261 // cerr << "rah fondr " << (*i)->io()->name () << endl;
263 switch ((*i)->do_refill (butler_mixdown_buffer, butler_gain_buffer)) {
265 bytes += (*i)->read_data_count();
268 bytes += (*i)->read_data_count();
269 disk_work_outstanding = true;
274 error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
280 if (i != diskstreams.end()) {
281 /* we didn't get to all the streams */
282 disk_work_outstanding = true;
285 if (!err && transport_work_requested()) {
290 gettimeofday (&end, 0);
292 double b = begin.tv_sec + (begin.tv_usec/1000000.0);
293 double e = end.tv_sec + (end.tv_usec / 1000000.0);
295 _read_data_rate = bytes / (e - b);
300 gettimeofday (&begin, 0);
302 for (i = diskstreams.begin(); !transport_work_requested() && butler_should_run && i != diskstreams.end(); ++i) {
304 // cerr << "write behind for " << (*i)->name () << endl;
306 switch ((*i)->do_flush ()) {
308 bytes += (*i)->write_data_count();
311 bytes += (*i)->write_data_count();
312 disk_work_outstanding = true;
318 error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
319 /* don't break - try to flush all streams in case they
320 are split across disks.
325 if (err && actively_recording()) {
326 /* stop the transport and try to catch as much possible
327 captured state as we can.
332 if (i != diskstreams.end()) {
333 /* we didn't get to all the streams */
334 disk_work_outstanding = true;
337 if (!err && transport_work_requested()) {
342 gettimeofday (&end, 0);
344 double b = begin.tv_sec + (begin.tv_usec/1000000.0);
345 double e = end.tv_sec + (end.tv_usec / 1000000.0);
347 _write_data_rate = bytes / (e - b);
350 if (!disk_work_outstanding) {
351 refresh_disk_space ();
356 LockMonitor lm (butler_request_lock, __LINE__, __FILE__);
358 if (butler_should_run && (disk_work_outstanding || transport_work_requested())) {
359 // for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
360 // cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
366 pthread_cond_signal (&butler_paused);
370 pthread_exit_pbd (0);
377 Session::request_overwrite_buffer (DiskStream* stream)
379 Event *ev = new Event (Event::Overwrite, Event::Add, Event::Immediate, 0, 0, 0.0);
380 ev->set_ptr (stream);
385 Session::overwrite_some_buffers (DiskStream* ds)
387 /* executed by the audio thread */
389 if (actively_recording()) {
395 ds->set_pending_overwrite (true);
399 RWLockMonitor dm (diskstream_lock, false, __LINE__, __FILE__);
400 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
401 (*i)->set_pending_overwrite (true);
405 post_transport_work = PostTransportWork (post_transport_work | PostTransportOverWrite);
406 schedule_butler_transport_work ();
410 Session::read_data_rate () const
412 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
413 in action. ignore it.
415 return _read_data_rate > 10485760000.0f ? 0.0f : _read_data_rate;
419 Session::write_data_rate () const
421 /* disk i/o in excess of 10000MB/sec indicate the buffer cache
422 in action. ignore it.
424 return _write_data_rate > 10485760000.0f ? 0.0f : _write_data_rate;
428 Session::playback_load ()
430 return (uint32_t) atomic_read (&_playback_load);
434 Session::capture_load ()
436 return (uint32_t) atomic_read (&_capture_load);
440 Session::playback_load_min ()
442 return (uint32_t) atomic_read (&_playback_load_min);
446 Session::capture_load_min ()
448 return (uint32_t) atomic_read (&_capture_load_min);
452 Session::reset_capture_load_min ()
454 atomic_set (&_capture_load_min, 100);
459 Session::reset_playback_load_min ()
461 atomic_set (&_playback_load_min, 100);