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"
26 #include "ardour/debug.h"
27 #include "ardour/graph.h"
28 #include "ardour/types.h"
29 #include "ardour/session.h"
30 #include "ardour/route.h"
31 #include "ardour/process_thread.h"
32 #include "ardour/audioengine.h"
34 #include <jack/thread.h>
38 using namespace ARDOUR;
43 Graph::Graph (Session & session)
44 : SessionHandleRef (session)
45 , _execution_sem ("graph_execution", 0)
46 , _callback_start_sem ("graph_start", 0)
47 , _callback_done_sem ("graph_done", 0)
48 , _cleanup_sem ("graph_cleanup", 0)
50 pthread_mutex_init( &_trigger_mutex, NULL);
52 _execution_tokens = 0;
57 _quit_threads = false;
60 int num_cpu = hardware_concurrency();
61 int num_threads = num_cpu;
62 int pu = Config->get_processor_usage ();
65 /* pu is negative: use "pu" less cores for DSP than appear to be available
68 if (-pu < num_threads) {
74 /* use "pu" cores, if available
77 if (pu <= num_threads) {
82 info << string_compose (_("Using %2 threads on %1 CPUs"), num_cpu, num_threads) << endmsg;
86 if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::main_thread, this), &a_thread, 100000) == 0) {
87 _thread_list.push_back (a_thread);
90 for (int i = 1; i < num_threads; ++i) {
91 if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::helper_thread, this), &a_thread, 100000) == 0) {
92 _thread_list.push_back (a_thread);
98 Graph::session_going_away()
100 _quit_threads = true;
102 for (unsigned int i=0; i<_thread_list.size(); i++) {
103 _execution_sem.signal ();
106 _callback_start_sem.signal ();
108 for (list<pthread_t>::iterator i = _thread_list.begin(); i != _thread_list.end(); i++) {
110 pthread_join (*i, &status);
113 // now drop all references on the nodes.
114 _nodes_rt[0].clear();
115 _nodes_rt[1].clear();
116 _init_trigger_list[0].clear();
117 _init_trigger_list[1].clear();
118 _trigger_queue.clear();
122 Graph::clear_other_chain ()
124 Glib::Mutex::Lock ls (_swap_mutex);
127 if (_setup_chain != _pending_chain) {
129 for (node_list_t::iterator ni=_nodes_rt[_setup_chain].begin(); ni!=_nodes_rt[_setup_chain].end(); ni++) {
130 (*ni)->_activation_set[_setup_chain].clear();
133 _nodes_rt[_setup_chain].clear ();
134 _init_trigger_list[_setup_chain].clear ();
137 /* setup chain == pending chain - we have
138 to wait till this is no longer true.
140 _cleanup_cond.wait (_swap_mutex);
147 node_list_t::iterator i;
150 if (_swap_mutex.trylock()) {
151 // we got the swap mutex.
152 if (_current_chain != _pending_chain)
154 // printf ("chain swap ! %d -> %d\n", _current_chain, _pending_chain);
155 _setup_chain = _current_chain;
156 _current_chain = _pending_chain;
157 _cleanup_cond.signal ();
159 _swap_mutex.unlock ();
162 chain = _current_chain;
165 for (i=_nodes_rt[chain].begin(); i!=_nodes_rt[chain].end(); i++) {
167 _graph_empty = false;
169 _finished_refcount = _init_finished_refcount[chain];
171 for (i=_init_trigger_list[chain].begin(); i!=_init_trigger_list[chain].end(); i++) {
172 this->trigger( i->get() );
177 Graph::trigger (GraphNode* n)
179 pthread_mutex_lock (&_trigger_mutex);
180 _trigger_queue.push_back( n);
181 pthread_mutex_unlock (&_trigger_mutex);
187 if (g_atomic_int_dec_and_test (&_finished_refcount)) {
189 // ok... this cycle is finished now.
190 // we are the only thread alive.
192 this->restart_cycle();
197 Graph::restart_cycle()
199 //printf( "cycle_done chain: %d\n", _current_chain);
201 // we are through. wakeup our caller.
203 _callback_done_sem.signal ();
205 // block until we are triggered.
206 _callback_start_sem.wait();
210 //printf( "cycle_start\n" );
215 //printf( "cycle_start chain: %d\n", _current_chain);
217 // returning will restart the cycle.
218 // starting with waking up the others.
222 is_feedback (boost::shared_ptr<RouteList> routelist, Route* from, boost::shared_ptr<Route> to)
224 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
225 if ((*ri).get() == from)
235 is_feedback (boost::shared_ptr<RouteList> routelist, boost::shared_ptr<Route> from, Route* to)
237 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
238 if ((*ri).get() == to)
248 Graph::rechain (boost::shared_ptr<RouteList> routelist)
250 node_list_t::iterator ni;
251 Glib::Mutex::Lock ls (_swap_mutex);
253 int chain = _setup_chain;
254 DEBUG_TRACE (DEBUG::Graph, string_compose ("============== setup %1\n", chain));
255 // set all refcounts to 0;
257 _init_finished_refcount[chain] = 0;
258 _init_trigger_list[chain].clear();
260 _nodes_rt[chain].clear();
262 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
263 node_ptr_t n = boost::dynamic_pointer_cast<GraphNode> (*ri);
265 n->_init_refcount[chain] = 0;
266 n->_activation_set[chain].clear();
267 _nodes_rt[chain].push_back(n);
270 // now add refs for the connections.
272 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
273 bool has_input = false;
274 bool has_output = false;
276 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
278 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
279 if (rp->direct_feeds (*ri)) {
280 if (is_feedback (routelist, rp.get(), *ri)) {
285 (*ni)->_activation_set[chain].insert (boost::dynamic_pointer_cast<GraphNode> (*ri) );
289 for (Route::FedBy::iterator fi=rp->fed_by().begin(); fi!=rp->fed_by().end(); fi++) {
290 if (boost::shared_ptr<Route> r = fi->r.lock()) {
291 if (!is_feedback (routelist, r, rp.get())) {
297 for (node_set_t::iterator ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
298 (*ai)->_init_refcount[chain] += 1;
302 _init_trigger_list[chain].push_back (*ni);
305 _init_finished_refcount[chain] += 1;
308 _pending_chain = chain;
317 pthread_mutex_lock (&_trigger_mutex);
318 if (_trigger_queue.size()) {
319 to_run = _trigger_queue.back();
320 _trigger_queue.pop_back();
325 int et = _execution_tokens;
326 int ts = _trigger_queue.size();
328 int wakeup = min (et, ts);
329 _execution_tokens -= wakeup;
331 for (int i=0; i<wakeup; i++ ) {
332 _execution_sem.signal ();
335 while (to_run == 0) {
336 _execution_tokens += 1;
337 pthread_mutex_unlock (&_trigger_mutex);
338 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_self()));
339 _execution_sem.signal ();
342 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_self()));
343 pthread_mutex_lock (&_trigger_mutex);
344 if (_trigger_queue.size()) {
345 to_run = _trigger_queue.back();
346 _trigger_queue.pop_back();
349 pthread_mutex_unlock (&_trigger_mutex);
352 to_run->finish (_current_chain);
359 if (!jack_is_realtime (AudioEngine::instance()->jack())) {
363 int priority = jack_client_real_time_priority (AudioEngine::instance()->jack());
366 struct sched_param rtparam;
368 memset (&rtparam, 0, sizeof (rtparam));
369 rtparam.sched_priority = priority;
371 pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam);
376 Graph::helper_thread()
378 ProcessThread *pt = new ProcessThread;
395 ProcessThread *pt = new ProcessThread;
401 _callback_start_sem.wait ();
402 DEBUG_TRACE(DEBUG::Graph, "main thread is awake\n");
405 if (_graph_empty && !_quit_threads) {
406 _callback_done_sem.signal ();
411 DEBUG_TRACE(DEBUG::Graph, "main thread runs one graph node\n");
421 Graph::dump (int chain)
424 node_list_t::iterator ni;
425 node_set_t::iterator ai;
427 chain = _pending_chain;
429 DEBUG_TRACE (DEBUG::Graph, "--------------------------------------------Graph dump:\n");
430 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
431 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
432 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", rp->name().c_str(), (*ni)->_init_refcount[chain]));
433 for (ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
434 DEBUG_TRACE (DEBUG::Graph, string_compose (" triggers: %1\n", boost::dynamic_pointer_cast<Route>(*ai)->name().c_str()));
438 DEBUG_TRACE (DEBUG::Graph, "------------- trigger list:\n");
439 for (ni=_init_trigger_list[chain].begin(); ni!=_init_trigger_list[chain].end(); ni++) {
440 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", boost::dynamic_pointer_cast<Route>(*ni)->name().c_str(), (*ni)->_init_refcount[chain]));
443 DEBUG_TRACE (DEBUG::Graph, string_compose ("final activation refcount: %1\n", _init_finished_refcount[chain]));
448 Graph::silent_process_routes (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
449 bool can_record, bool rec_monitors_input, bool& need_butler)
451 _process_nframes = nframes;
452 _process_start_frame = start_frame;
453 _process_end_frame = end_frame;
454 _process_can_record = can_record;
455 _process_rec_monitors_input = rec_monitors_input;
457 _process_silent = true;
458 _process_noroll = false;
460 _process_need_butler = false;
463 DEBUG_TRACE(DEBUG::Graph, "wake graph for silent process\n");
464 _callback_start_sem.signal ();
465 _callback_done_sem.wait ();
468 need_butler = _process_need_butler;
470 return _process_retval;
474 Graph::process_routes (nframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick,
475 bool can_record, bool rec_monitors_input, bool& need_butler)
477 DEBUG_TRACE (DEBUG::Graph, string_compose ("graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
479 _process_nframes = nframes;
480 _process_start_frame = start_frame;
481 _process_end_frame = end_frame;
482 _process_can_record = can_record;
483 _process_rec_monitors_input = rec_monitors_input;
484 _process_declick = declick;
486 _process_silent = false;
487 _process_noroll = false;
489 _process_need_butler = false;
491 DEBUG_TRACE(DEBUG::Graph, "wake graph for non-silent process\n");
492 _callback_start_sem.signal ();
493 _callback_done_sem.wait ();
495 DEBUG_TRACE (DEBUG::Graph, "graph execution complete\n");
497 need_butler = _process_need_butler;
499 return _process_retval;
503 Graph::routes_no_roll (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
504 bool non_rt_pending, bool can_record, int declick)
506 DEBUG_TRACE (DEBUG::Graph, string_compose ("no-roll graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
508 _process_nframes = nframes;
509 _process_start_frame = start_frame;
510 _process_end_frame = end_frame;
511 _process_can_record = can_record;
512 _process_declick = declick;
513 _process_non_rt_pending = non_rt_pending;
515 _process_silent = false;
516 _process_noroll = true;
518 _process_need_butler = false;
520 DEBUG_TRACE(DEBUG::Graph, "wake graph for no-roll process\n");
521 _callback_start_sem.signal ();
522 _callback_done_sem.wait ();
524 return _process_retval;
527 Graph::process_one_route (Route* route)
529 bool need_butler = false;
534 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_self(), route->name()));
536 if (_process_silent) {
537 retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_can_record, _process_rec_monitors_input, need_butler);
538 } else if (_process_noroll) {
539 route->set_pending_declick (_process_declick);
540 retval = route->no_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_non_rt_pending, _process_can_record, _process_declick);
542 route->set_pending_declick (_process_declick);
543 retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, _process_can_record, _process_rec_monitors_input, need_butler);
547 _process_retval = retval;
551 _process_need_butler = true;