2 Copyright (C) 2010 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "pbd/compose.h"
25 #include "pbd/debug_rt_alloc.h"
27 #include "ardour/debug.h"
28 #include "ardour/graph.h"
29 #include "ardour/types.h"
30 #include "ardour/session.h"
31 #include "ardour/route.h"
32 #include "ardour/process_thread.h"
33 #include "ardour/audioengine.h"
35 #include <jack/thread.h>
39 using namespace ARDOUR;
44 static Graph* graph = 0;
49 return !graph->in_process_thread ();
55 Graph::Graph (Session & session)
56 : SessionHandleRef (session)
57 , _execution_sem ("graph_execution", 0)
58 , _callback_start_sem ("graph_start", 0)
59 , _callback_done_sem ("graph_done", 0)
60 , _cleanup_sem ("graph_cleanup", 0)
62 pthread_mutex_init( &_trigger_mutex, NULL);
64 /* XXX: rather hacky `fix' to stop _trigger_queue.push_back() allocating
65 memory in the RT thread.
67 _trigger_queue.reserve (8192);
69 _execution_tokens = 0;
74 _quit_threads = false;
77 int num_cpu = hardware_concurrency();
78 int num_threads = num_cpu;
79 int pu = Config->get_processor_usage ();
82 /* pu is negative: use "pu" less cores for DSP than appear to be available
85 if (-pu < num_threads) {
91 /* use "pu" cores, if available
94 if (pu <= num_threads) {
99 info << string_compose (_("Using %2 threads on %1 CPUs"), num_cpu, num_threads) << endmsg;
103 if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::main_thread, this), &a_thread, 100000) == 0) {
104 _thread_list.push_back (a_thread);
107 for (int i = 1; i < num_threads; ++i) {
108 if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::helper_thread, this), &a_thread, 100000) == 0) {
109 _thread_list.push_back (a_thread);
113 #ifdef DEBUG_RT_ALLOC
115 pbd_alloc_allowed = &::alloc_allowed;
120 Graph::session_going_away()
122 _quit_threads = true;
124 for (unsigned int i=0; i<_thread_list.size(); i++) {
125 _execution_sem.signal ();
128 _callback_start_sem.signal ();
130 for (list<pthread_t>::iterator i = _thread_list.begin(); i != _thread_list.end(); i++) {
132 pthread_join (*i, &status);
135 // now drop all references on the nodes.
136 _nodes_rt[0].clear();
137 _nodes_rt[1].clear();
138 _init_trigger_list[0].clear();
139 _init_trigger_list[1].clear();
140 _trigger_queue.clear();
144 Graph::clear_other_chain ()
146 Glib::Mutex::Lock ls (_swap_mutex);
149 if (_setup_chain != _pending_chain) {
151 for (node_list_t::iterator ni=_nodes_rt[_setup_chain].begin(); ni!=_nodes_rt[_setup_chain].end(); ni++) {
152 (*ni)->_activation_set[_setup_chain].clear();
155 _nodes_rt[_setup_chain].clear ();
156 _init_trigger_list[_setup_chain].clear ();
159 /* setup chain == pending chain - we have
160 to wait till this is no longer true.
162 _cleanup_cond.wait (_swap_mutex);
169 node_list_t::iterator i;
172 if (_swap_mutex.trylock()) {
173 // we got the swap mutex.
174 if (_current_chain != _pending_chain)
176 // printf ("chain swap ! %d -> %d\n", _current_chain, _pending_chain);
177 _setup_chain = _current_chain;
178 _current_chain = _pending_chain;
179 _cleanup_cond.signal ();
181 _swap_mutex.unlock ();
184 chain = _current_chain;
187 for (i=_nodes_rt[chain].begin(); i!=_nodes_rt[chain].end(); i++) {
189 _graph_empty = false;
191 _finished_refcount = _init_finished_refcount[chain];
193 for (i=_init_trigger_list[chain].begin(); i!=_init_trigger_list[chain].end(); i++) {
194 this->trigger( i->get() );
199 Graph::trigger (GraphNode* n)
201 pthread_mutex_lock (&_trigger_mutex);
202 _trigger_queue.push_back( n);
203 pthread_mutex_unlock (&_trigger_mutex);
209 if (g_atomic_int_dec_and_test (&_finished_refcount)) {
211 // ok... this cycle is finished now.
212 // we are the only thread alive.
214 this->restart_cycle();
219 Graph::restart_cycle()
221 //printf( "cycle_done chain: %d\n", _current_chain);
223 // we are through. wakeup our caller.
225 _callback_done_sem.signal ();
227 // block until we are triggered.
228 _callback_start_sem.wait();
232 //printf( "cycle_start\n" );
237 //printf( "cycle_start chain: %d\n", _current_chain);
239 // returning will restart the cycle.
240 // starting with waking up the others.
244 is_feedback (boost::shared_ptr<RouteList> routelist, Route* from, boost::shared_ptr<Route> to)
246 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
247 if ((*ri).get() == from)
257 is_feedback (boost::shared_ptr<RouteList> routelist, boost::shared_ptr<Route> from, Route* to)
259 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
260 if ((*ri).get() == to)
270 Graph::rechain (boost::shared_ptr<RouteList> routelist)
272 node_list_t::iterator ni;
273 Glib::Mutex::Lock ls (_swap_mutex);
275 int chain = _setup_chain;
276 DEBUG_TRACE (DEBUG::Graph, string_compose ("============== setup %1\n", chain));
277 // set all refcounts to 0;
279 _init_finished_refcount[chain] = 0;
280 _init_trigger_list[chain].clear();
282 _nodes_rt[chain].clear();
284 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
285 node_ptr_t n = boost::dynamic_pointer_cast<GraphNode> (*ri);
287 n->_init_refcount[chain] = 0;
288 n->_activation_set[chain].clear();
289 _nodes_rt[chain].push_back(n);
292 // now add refs for the connections.
294 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
295 bool has_input = false;
296 bool has_output = false;
298 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
300 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
301 if (rp->direct_feeds (*ri)) {
302 if (is_feedback (routelist, rp.get(), *ri)) {
307 (*ni)->_activation_set[chain].insert (boost::dynamic_pointer_cast<GraphNode> (*ri) );
311 for (Route::FedBy::iterator fi=rp->fed_by().begin(); fi!=rp->fed_by().end(); fi++) {
312 if (boost::shared_ptr<Route> r = fi->r.lock()) {
313 if (!is_feedback (routelist, r, rp.get())) {
319 for (node_set_t::iterator ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
320 (*ai)->_init_refcount[chain] += 1;
324 _init_trigger_list[chain].push_back (*ni);
327 _init_finished_refcount[chain] += 1;
330 _pending_chain = chain;
339 pthread_mutex_lock (&_trigger_mutex);
340 if (_trigger_queue.size()) {
341 to_run = _trigger_queue.back();
342 _trigger_queue.pop_back();
347 int et = _execution_tokens;
348 int ts = _trigger_queue.size();
350 int wakeup = min (et, ts);
351 _execution_tokens -= wakeup;
353 for (int i=0; i<wakeup; i++ ) {
354 _execution_sem.signal ();
357 while (to_run == 0) {
358 _execution_tokens += 1;
359 pthread_mutex_unlock (&_trigger_mutex);
360 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_self()));
361 _execution_sem.wait ();
364 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_self()));
365 pthread_mutex_lock (&_trigger_mutex);
366 if (_trigger_queue.size()) {
367 to_run = _trigger_queue.back();
368 _trigger_queue.pop_back();
371 pthread_mutex_unlock (&_trigger_mutex);
374 to_run->finish (_current_chain);
381 if (!jack_is_realtime (AudioEngine::instance()->jack())) {
385 int priority = jack_client_real_time_priority (AudioEngine::instance()->jack());
388 struct sched_param rtparam;
390 memset (&rtparam, 0, sizeof (rtparam));
391 rtparam.sched_priority = priority;
393 pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam);
398 Graph::helper_thread()
400 suspend_rt_malloc_checks ();
401 ProcessThread* pt = new ProcessThread ();
402 resume_rt_malloc_checks ();
419 suspend_rt_malloc_checks ();
420 ProcessThread* pt = new ProcessThread ();
421 resume_rt_malloc_checks ();
427 _callback_start_sem.wait ();
428 DEBUG_TRACE(DEBUG::Graph, "main thread is awake\n");
431 if (_graph_empty && !_quit_threads) {
432 _callback_done_sem.signal ();
437 DEBUG_TRACE(DEBUG::Graph, "main thread runs one graph node\n");
447 Graph::dump (int chain)
450 node_list_t::iterator ni;
451 node_set_t::iterator ai;
453 chain = _pending_chain;
455 DEBUG_TRACE (DEBUG::Graph, "--------------------------------------------Graph dump:\n");
456 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
457 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
458 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", rp->name().c_str(), (*ni)->_init_refcount[chain]));
459 for (ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
460 DEBUG_TRACE (DEBUG::Graph, string_compose (" triggers: %1\n", boost::dynamic_pointer_cast<Route>(*ai)->name().c_str()));
464 DEBUG_TRACE (DEBUG::Graph, "------------- trigger list:\n");
465 for (ni=_init_trigger_list[chain].begin(); ni!=_init_trigger_list[chain].end(); ni++) {
466 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", boost::dynamic_pointer_cast<Route>(*ni)->name().c_str(), (*ni)->_init_refcount[chain]));
469 DEBUG_TRACE (DEBUG::Graph, string_compose ("final activation refcount: %1\n", _init_finished_refcount[chain]));
474 Graph::silent_process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
475 bool can_record, bool rec_monitors_input, bool& need_butler)
477 _process_nframes = nframes;
478 _process_start_frame = start_frame;
479 _process_end_frame = end_frame;
480 _process_can_record = can_record;
481 _process_rec_monitors_input = rec_monitors_input;
483 _process_silent = true;
484 _process_noroll = false;
486 _process_need_butler = false;
489 DEBUG_TRACE(DEBUG::Graph, "wake graph for silent process\n");
490 _callback_start_sem.signal ();
491 _callback_done_sem.wait ();
494 need_butler = _process_need_butler;
496 return _process_retval;
500 Graph::process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick,
501 bool can_record, bool rec_monitors_input, bool& need_butler)
503 DEBUG_TRACE (DEBUG::Graph, string_compose ("graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
505 _process_nframes = nframes;
506 _process_start_frame = start_frame;
507 _process_end_frame = end_frame;
508 _process_can_record = can_record;
509 _process_rec_monitors_input = rec_monitors_input;
510 _process_declick = declick;
512 _process_silent = false;
513 _process_noroll = false;
515 _process_need_butler = false;
517 DEBUG_TRACE(DEBUG::Graph, "wake graph for non-silent process\n");
518 _callback_start_sem.signal ();
519 _callback_done_sem.wait ();
521 DEBUG_TRACE (DEBUG::Graph, "graph execution complete\n");
523 need_butler = _process_need_butler;
525 return _process_retval;
529 Graph::routes_no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
530 bool non_rt_pending, bool can_record, int declick)
532 DEBUG_TRACE (DEBUG::Graph, string_compose ("no-roll graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
534 _process_nframes = nframes;
535 _process_start_frame = start_frame;
536 _process_end_frame = end_frame;
537 _process_can_record = can_record;
538 _process_declick = declick;
539 _process_non_rt_pending = non_rt_pending;
541 _process_silent = false;
542 _process_noroll = true;
544 _process_need_butler = false;
546 DEBUG_TRACE(DEBUG::Graph, "wake graph for no-roll process\n");
547 _callback_start_sem.signal ();
548 _callback_done_sem.wait ();
550 return _process_retval;
553 Graph::process_one_route (Route* route)
555 bool need_butler = false;
560 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_self(), route->name()));
562 if (_process_silent) {
563 retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_can_record, _process_rec_monitors_input, need_butler);
564 } else if (_process_noroll) {
565 route->set_pending_declick (_process_declick);
566 retval = route->no_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_non_rt_pending, _process_can_record, _process_declick);
568 route->set_pending_declick (_process_declick);
569 retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, _process_can_record, _process_rec_monitors_input, need_butler);
573 _process_retval = retval;
577 _process_need_butler = true;
582 Graph::in_process_thread () const
584 list<pthread_t>::const_iterator i = _thread_list.begin ();
585 while (i != _thread_list.end() && *i != pthread_self ()) {
589 return i != _thread_list.end ();