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 , _quit_threads (false)
58 , _execution_sem ("graph_execution", 0)
59 , _callback_start_sem ("graph_start", 0)
60 , _callback_done_sem ("graph_done", 0)
61 , _cleanup_sem ("graph_cleanup", 0)
63 pthread_mutex_init( &_trigger_mutex, NULL);
65 /* XXX: rather hacky `fix' to stop _trigger_queue.push_back() allocating
66 memory in the RT thread.
68 _trigger_queue.reserve (8192);
70 _execution_tokens = 0;
75 _quit_threads = false;
80 Config->ParameterChanged.connect_same_thread (processor_usage_connection, boost::bind (&Graph::parameter_changed, this, _1));
84 pbd_alloc_allowed = &::alloc_allowed;
89 Graph::parameter_changed (std::string param)
91 if (param == X_("processor-usage")) {
97 Graph::reset_thread_list ()
99 int num_cpu = hardware_concurrency();
100 int pu = Config->get_processor_usage ();
102 uint32_t num_threads = max (num_cpu - 1, 2); // default to number of cpus minus one, or 2, whichever is larger
105 /* pu is negative: use "pu" less cores for DSP than appear to be available
109 num_threads = num_cpu + pu;
112 } else if (pu == 0) {
114 num_threads = num_cpu;
117 /* use "pu" cores, if available
120 num_threads = min (num_cpu, pu);
123 if (_thread_list.size() == num_threads) {
127 Glib::Mutex::Lock lm (_session.engine().process_lock());
129 if (!_thread_list.empty()) {
133 if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::main_thread, this), &a_thread, 100000) == 0) {
134 _thread_list.push_back (a_thread);
137 for (uint32_t i = 1; i < num_threads; ++i) {
138 if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::helper_thread, this), &a_thread, 100000) == 0) {
139 _thread_list.push_back (a_thread);
143 info << string_compose (_("Using %1 threads for DSP on %2 CPUs"), _thread_list.size(), num_cpu) << endmsg;
144 cerr << string_compose (_("Using %1 threads for DSP on %2 CPUs"), _thread_list.size(), num_cpu) << endl;
148 Graph::session_going_away()
152 // now drop all references on the nodes.
153 _nodes_rt[0].clear();
154 _nodes_rt[1].clear();
155 _init_trigger_list[0].clear();
156 _init_trigger_list[1].clear();
157 _trigger_queue.clear();
161 Graph::drop_threads ()
163 _quit_threads = true;
165 for (unsigned int i=0; i< _thread_list.size(); i++) {
166 _execution_sem.signal ();
169 _callback_start_sem.signal ();
171 for (list<pthread_t>::iterator i = _thread_list.begin(); i != _thread_list.end(); ++i) {
173 pthread_join (*i, &status);
176 _thread_list.clear ();
178 _quit_threads = false;
182 Graph::clear_other_chain ()
184 Glib::Mutex::Lock ls (_swap_mutex);
187 if (_setup_chain != _pending_chain) {
189 for (node_list_t::iterator ni=_nodes_rt[_setup_chain].begin(); ni!=_nodes_rt[_setup_chain].end(); ni++) {
190 (*ni)->_activation_set[_setup_chain].clear();
193 _nodes_rt[_setup_chain].clear ();
194 _init_trigger_list[_setup_chain].clear ();
197 /* setup chain == pending chain - we have
198 to wait till this is no longer true.
200 _cleanup_cond.wait (_swap_mutex);
207 node_list_t::iterator i;
210 if (_swap_mutex.trylock()) {
211 // we got the swap mutex.
212 if (_current_chain != _pending_chain)
214 // printf ("chain swap ! %d -> %d\n", _current_chain, _pending_chain);
215 _setup_chain = _current_chain;
216 _current_chain = _pending_chain;
217 _cleanup_cond.signal ();
219 _swap_mutex.unlock ();
222 chain = _current_chain;
225 for (i=_nodes_rt[chain].begin(); i!=_nodes_rt[chain].end(); i++) {
227 _graph_empty = false;
229 _finished_refcount = _init_finished_refcount[chain];
231 for (i=_init_trigger_list[chain].begin(); i!=_init_trigger_list[chain].end(); i++) {
232 this->trigger( i->get() );
237 Graph::trigger (GraphNode* n)
239 pthread_mutex_lock (&_trigger_mutex);
240 _trigger_queue.push_back (n);
241 pthread_mutex_unlock (&_trigger_mutex);
247 if (g_atomic_int_dec_and_test (&_finished_refcount)) {
249 // ok... this cycle is finished now.
250 // we are the only thread alive.
252 this->restart_cycle();
257 Graph::restart_cycle()
259 // we are through. wakeup our caller.
262 _callback_done_sem.signal ();
264 // block until we are triggered.
265 _callback_start_sem.wait();
277 // returning will restart the cycle.
278 // starting with waking up the others.
282 is_feedback (boost::shared_ptr<RouteList> routelist, Route* from, boost::shared_ptr<Route> to)
284 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
285 if ((*ri).get() == from)
295 is_feedback (boost::shared_ptr<RouteList> routelist, boost::shared_ptr<Route> from, Route* to)
297 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
298 if ((*ri).get() == to)
308 Graph::rechain (boost::shared_ptr<RouteList> routelist)
310 node_list_t::iterator ni;
311 Glib::Mutex::Lock ls (_swap_mutex);
313 int chain = _setup_chain;
314 DEBUG_TRACE (DEBUG::Graph, string_compose ("============== setup %1\n", chain));
315 // set all refcounts to 0;
317 _init_finished_refcount[chain] = 0;
318 _init_trigger_list[chain].clear();
320 _nodes_rt[chain].clear();
322 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
323 node_ptr_t n = boost::dynamic_pointer_cast<GraphNode> (*ri);
325 n->_init_refcount[chain] = 0;
326 n->_activation_set[chain].clear();
327 _nodes_rt[chain].push_back(n);
330 // now add refs for the connections.
332 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
333 bool has_input = false;
334 bool has_output = false;
336 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
338 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
339 if (rp->direct_feeds (*ri)) {
340 if (is_feedback (routelist, rp.get(), *ri)) {
345 (*ni)->_activation_set[chain].insert (boost::dynamic_pointer_cast<GraphNode> (*ri) );
349 for (Route::FedBy::iterator fi=rp->fed_by().begin(); fi!=rp->fed_by().end(); fi++) {
350 if (boost::shared_ptr<Route> r = fi->r.lock()) {
351 if (!is_feedback (routelist, r, rp.get())) {
357 for (node_set_t::iterator ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
358 (*ai)->_init_refcount[chain] += 1;
362 _init_trigger_list[chain].push_back (*ni);
365 _init_finished_refcount[chain] += 1;
368 _pending_chain = chain;
377 pthread_mutex_lock (&_trigger_mutex);
378 if (_trigger_queue.size()) {
379 to_run = _trigger_queue.back();
380 _trigger_queue.pop_back();
385 int et = _execution_tokens;
386 int ts = _trigger_queue.size();
388 int wakeup = min (et, ts);
389 _execution_tokens -= wakeup;
391 DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 signals %2\n", pthread_self(), wakeup));
393 for (int i = 0; i < wakeup; i++) {
394 _execution_sem.signal ();
397 while (to_run == 0) {
398 _execution_tokens += 1;
399 pthread_mutex_unlock (&_trigger_mutex);
400 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_self()));
401 _execution_sem.wait ();
405 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_self()));
406 pthread_mutex_lock (&_trigger_mutex);
407 if (_trigger_queue.size()) {
408 to_run = _trigger_queue.back();
409 _trigger_queue.pop_back();
412 pthread_mutex_unlock (&_trigger_mutex);
415 to_run->finish (_current_chain);
417 DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 has finished run_one()\n", pthread_self()));
424 if (!jack_is_realtime (AudioEngine::instance()->jack())) {
428 int priority = jack_client_real_time_priority (AudioEngine::instance()->jack());
431 struct sched_param rtparam;
433 memset (&rtparam, 0, sizeof (rtparam));
434 rtparam.sched_priority = priority;
436 pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam);
441 Graph::helper_thread()
443 suspend_rt_malloc_checks ();
444 ProcessThread* pt = new ProcessThread ();
445 resume_rt_malloc_checks ();
462 suspend_rt_malloc_checks ();
463 ProcessThread* pt = new ProcessThread ();
464 resume_rt_malloc_checks ();
470 _callback_start_sem.wait ();
471 DEBUG_TRACE(DEBUG::Graph, "main thread is awake\n");
479 if (_graph_empty && !_quit_threads) {
480 _callback_done_sem.signal ();
481 DEBUG_TRACE(DEBUG::Graph, "main thread sees graph done, goes back to slee\n");
486 DEBUG_TRACE(DEBUG::Graph, "main thread runs one graph node\n");
496 Graph::dump (int chain)
499 node_list_t::iterator ni;
500 node_set_t::iterator ai;
502 chain = _pending_chain;
504 DEBUG_TRACE (DEBUG::Graph, "--------------------------------------------Graph dump:\n");
505 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
506 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
507 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", rp->name().c_str(), (*ni)->_init_refcount[chain]));
508 for (ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
509 DEBUG_TRACE (DEBUG::Graph, string_compose (" triggers: %1\n", boost::dynamic_pointer_cast<Route>(*ai)->name().c_str()));
513 DEBUG_TRACE (DEBUG::Graph, "------------- trigger list:\n");
514 for (ni=_init_trigger_list[chain].begin(); ni!=_init_trigger_list[chain].end(); ni++) {
515 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", boost::dynamic_pointer_cast<Route>(*ni)->name().c_str(), (*ni)->_init_refcount[chain]));
518 DEBUG_TRACE (DEBUG::Graph, string_compose ("final activation refcount: %1\n", _init_finished_refcount[chain]));
523 Graph::silent_process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
524 bool can_record, bool rec_monitors_input, bool& need_butler)
526 _process_nframes = nframes;
527 _process_start_frame = start_frame;
528 _process_end_frame = end_frame;
529 _process_can_record = can_record;
530 _process_rec_monitors_input = rec_monitors_input;
532 _process_silent = true;
533 _process_noroll = false;
535 _process_need_butler = false;
538 DEBUG_TRACE(DEBUG::Graph, "wake graph for silent process\n");
539 _callback_start_sem.signal ();
540 _callback_done_sem.wait ();
543 need_butler = _process_need_butler;
545 return _process_retval;
549 Graph::process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick,
550 bool can_record, bool rec_monitors_input, bool& need_butler)
552 DEBUG_TRACE (DEBUG::Graph, string_compose ("graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
554 _process_nframes = nframes;
555 _process_start_frame = start_frame;
556 _process_end_frame = end_frame;
557 _process_can_record = can_record;
558 _process_rec_monitors_input = rec_monitors_input;
559 _process_declick = declick;
561 _process_silent = false;
562 _process_noroll = false;
564 _process_need_butler = false;
566 DEBUG_TRACE(DEBUG::Graph, "wake graph for non-silent process\n");
567 _callback_start_sem.signal ();
568 _callback_done_sem.wait ();
570 DEBUG_TRACE (DEBUG::Graph, "graph execution complete\n");
572 need_butler = _process_need_butler;
574 return _process_retval;
578 Graph::routes_no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
579 bool non_rt_pending, bool can_record, int declick)
581 DEBUG_TRACE (DEBUG::Graph, string_compose ("no-roll graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
583 _process_nframes = nframes;
584 _process_start_frame = start_frame;
585 _process_end_frame = end_frame;
586 _process_can_record = can_record;
587 _process_declick = declick;
588 _process_non_rt_pending = non_rt_pending;
590 _process_silent = false;
591 _process_noroll = true;
593 _process_need_butler = false;
595 DEBUG_TRACE(DEBUG::Graph, "wake graph for no-roll process\n");
596 _callback_start_sem.signal ();
597 _callback_done_sem.wait ();
599 return _process_retval;
602 Graph::process_one_route (Route* route)
604 bool need_butler = false;
609 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_self(), route->name()));
611 if (_process_silent) {
612 retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_can_record, _process_rec_monitors_input, need_butler);
613 } else if (_process_noroll) {
614 route->set_pending_declick (_process_declick);
615 retval = route->no_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_non_rt_pending, _process_can_record, _process_declick);
617 route->set_pending_declick (_process_declick);
618 retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, _process_can_record, _process_rec_monitors_input, need_butler);
622 _process_retval = retval;
626 _process_need_butler = true;
631 Graph::in_process_thread () const
633 list<pthread_t>::const_iterator i = _thread_list.begin ();
634 while (i != _thread_list.end() && *i != pthread_self ()) {
638 return i != _thread_list.end ();