fix num DSP threads computation (TODO: move into a utility function for Sakari's...
[ardour.git] / libs / ardour / graph.cc
1 /*
2   Copyright (C) 2010 Paul Davis
3   Author: Torben Hohn
4
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.
9
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.
14
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.
18
19 */
20 #include <stdio.h>
21 #include <cmath>
22
23 #include "pbd/compose.h"
24 #include "pbd/cpus.h"
25 #include "pbd/debug_rt_alloc.h"
26
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"
34
35 #include <jack/thread.h>
36
37 #include "i18n.h"
38
39 using namespace ARDOUR;
40 using namespace PBD;
41 using namespace std;
42
43 #ifdef DEBUG_RT_ALLOC
44 static Graph* graph = 0;
45 extern "C" {
46
47 int alloc_allowed ()
48 {
49         return !graph->in_process_thread ();
50 }
51
52 }
53 #endif
54
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)
62 {
63         pthread_mutex_init( &_trigger_mutex, NULL);
64
65         /* XXX: rather hacky `fix' to stop _trigger_queue.push_back() allocating
66            memory in the RT thread.
67         */
68         _trigger_queue.reserve (8192);
69
70         _execution_tokens = 0;
71
72         _current_chain = 0;
73         _pending_chain = 0;
74         _setup_chain   = 1;
75         _quit_threads = false;
76         _graph_empty = true;
77         
78         reset_thread_list ();
79
80         Config->ParameterChanged.connect_same_thread (processor_usage_connection, boost::bind (&Graph::parameter_changed, this, _1));
81
82 #ifdef DEBUG_RT_ALLOC   
83         graph = this;
84         pbd_alloc_allowed = &::alloc_allowed;
85 #endif  
86 }
87
88 void
89 Graph::parameter_changed (std::string param)
90 {
91         if (param == X_("processor-usage")) {
92                 reset_thread_list ();
93         }
94 }
95
96 void
97 Graph::reset_thread_list ()
98 {
99         int num_cpu = hardware_concurrency();
100         int pu = Config->get_processor_usage ();
101         pthread_t a_thread;
102         uint32_t num_threads = max (num_cpu - 1, 2); // default to number of cpus minus one, or 2, whichever is larger
103
104         if (pu < 0) {
105                 /* pu is negative: use "pu" less cores for DSP than appear to be available
106                  */
107
108                 if (-pu < num_cpu) {
109                         num_threads = num_cpu + pu;
110                 }
111
112         } else if (pu == 0) {
113
114                 num_threads = num_cpu;
115
116         } else {
117                 /* use "pu" cores, if available
118                  */
119                 
120                 num_threads = min (num_cpu, pu);
121         }
122
123         if (_thread_list.size() == num_threads) {
124                 return;
125         }
126
127         Glib::Mutex::Lock lm (_session.engine().process_lock());
128
129         if (!_thread_list.empty()) {
130                 drop_threads ();
131         }
132
133         if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::main_thread, this), &a_thread, 100000) == 0) {
134                 _thread_list.push_back (a_thread);
135         }
136
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);
140                 }
141         }
142
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;
145 }
146
147 void
148 Graph::session_going_away()
149 {
150         drop_threads ();
151
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();
158 }
159
160 void
161 Graph::drop_threads ()
162 {
163         _quit_threads = true;
164
165         for (unsigned int i=0; i< _thread_list.size(); i++) {
166                 _execution_sem.signal ();
167         }
168
169         _callback_start_sem.signal ();
170
171         for (list<pthread_t>::iterator i = _thread_list.begin(); i != _thread_list.end(); ++i) {
172                 void* status;
173                 pthread_join (*i, &status);
174         }
175
176         _thread_list.clear ();
177
178         _quit_threads = false;
179 }
180
181 void
182 Graph::clear_other_chain ()
183 {
184         Glib::Mutex::Lock ls (_swap_mutex);
185
186         while (1) {
187                 if (_setup_chain != _pending_chain) {
188
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();
191                         }
192
193                         _nodes_rt[_setup_chain].clear ();
194                         _init_trigger_list[_setup_chain].clear ();
195                         break;
196                 }
197                 /* setup chain == pending chain - we have
198                    to wait till this is no longer true.
199                 */
200                 _cleanup_cond.wait (_swap_mutex);                
201         }
202 }
203
204 void
205 Graph::prep()
206 {
207         node_list_t::iterator i;
208         int chain;
209
210         if (_swap_mutex.trylock()) {
211                 // we got the swap mutex.
212                 if (_current_chain != _pending_chain)
213                 {
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 ();
218                 }
219                 _swap_mutex.unlock ();
220         }
221
222         chain = _current_chain;
223
224         _graph_empty = true;
225         for (i=_nodes_rt[chain].begin(); i!=_nodes_rt[chain].end(); i++) {
226                 (*i)->prep( chain);
227                 _graph_empty = false;
228         }
229         _finished_refcount = _init_finished_refcount[chain];
230
231         for (i=_init_trigger_list[chain].begin(); i!=_init_trigger_list[chain].end(); i++) {
232                 this->trigger( i->get() );
233         }
234 }
235
236 void
237 Graph::trigger (GraphNode* n)
238 {
239         pthread_mutex_lock (&_trigger_mutex);
240         _trigger_queue.push_back (n);
241         pthread_mutex_unlock (&_trigger_mutex);
242 }
243
244 void
245 Graph::dec_ref()
246 {
247         if (g_atomic_int_dec_and_test (&_finished_refcount)) {
248
249                 // ok... this cycle is finished now.
250                 // we are the only thread alive.
251         
252                 this->restart_cycle();
253         }
254 }
255
256 void
257 Graph::restart_cycle()
258 {
259         // we are through. wakeup our caller.
260
261   again:
262         _callback_done_sem.signal ();
263
264         // block until we are triggered.
265         _callback_start_sem.wait();
266
267         if (_quit_threads) {
268                 return;
269         }
270
271         this->prep();
272
273         if (_graph_empty) {
274                 goto again;
275         }
276
277         // returning will restart the cycle.
278         // starting with waking up the others.
279 }
280
281 static bool
282 is_feedback (boost::shared_ptr<RouteList> routelist, Route* from, boost::shared_ptr<Route> to)
283 {
284         for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
285                 if ((*ri).get() == from)
286                         return false;
287                 if ((*ri) == to)
288                         return true;
289         }
290         assert(0);
291         return false;
292 }
293
294 static bool
295 is_feedback (boost::shared_ptr<RouteList> routelist, boost::shared_ptr<Route> from, Route* to)
296 {
297         for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
298                 if ((*ri).get() == to)
299                         return true;
300                 if ((*ri) == from)
301                         return false;
302         }
303         assert(0);
304         return false;
305 }
306
307 void
308 Graph::rechain (boost::shared_ptr<RouteList> routelist)
309 {
310         node_list_t::iterator ni;
311         Glib::Mutex::Lock ls (_swap_mutex);
312
313         int chain = _setup_chain;
314         DEBUG_TRACE (DEBUG::Graph, string_compose ("============== setup %1\n", chain));
315         // set all refcounts to 0;
316
317         _init_finished_refcount[chain] = 0;
318         _init_trigger_list[chain].clear();
319
320         _nodes_rt[chain].clear();
321
322         for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
323                 node_ptr_t n = boost::dynamic_pointer_cast<GraphNode> (*ri);
324
325                 n->_init_refcount[chain] = 0;
326                 n->_activation_set[chain].clear();
327                 _nodes_rt[chain].push_back(n);
328         }
329
330         // now add refs for the connections.
331
332         for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
333                 bool has_input  = false;
334                 bool has_output = false;
335
336                 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
337
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)) {
341                                         continue; 
342                                 }
343
344                                 has_output = true;
345                                 (*ni)->_activation_set[chain].insert (boost::dynamic_pointer_cast<GraphNode> (*ri) );
346                         }
347                 }
348
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())) {
352                                         has_input = true;
353                                 }
354                         }
355                 }
356
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;
359                 }
360
361                 if (!has_input)
362                         _init_trigger_list[chain].push_back (*ni);
363
364                 if (!has_output)
365                         _init_finished_refcount[chain] += 1;
366         } 
367
368         _pending_chain = chain;
369         dump(chain);
370 }
371
372 bool
373 Graph::run_one()
374 {
375         GraphNode* to_run;
376
377         pthread_mutex_lock (&_trigger_mutex);
378         if (_trigger_queue.size()) {
379                 to_run = _trigger_queue.back();
380                 _trigger_queue.pop_back();
381         } else {
382                 to_run = 0;
383         }
384
385         int et = _execution_tokens;
386         int ts = _trigger_queue.size();
387
388         int wakeup = min (et, ts);
389         _execution_tokens -= wakeup;
390
391         DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 signals %2\n", pthread_self(), wakeup));
392
393         for (int i = 0; i < wakeup; i++) {
394                 _execution_sem.signal ();
395         }
396
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 ();
402                 if (_quit_threads) {
403                         return true;
404                 }
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();
410                 }
411         }
412         pthread_mutex_unlock (&_trigger_mutex);
413
414         to_run->process();
415         to_run->finish (_current_chain);
416
417         DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 has finished run_one()\n", pthread_self()));
418
419         return false;
420 }
421
422 static void get_rt()
423 {
424         if (!jack_is_realtime (AudioEngine::instance()->jack())) {
425                 return;
426         }
427
428         int priority = jack_client_real_time_priority (AudioEngine::instance()->jack());
429
430         if (priority) {
431                 struct sched_param rtparam;
432         
433                 memset (&rtparam, 0, sizeof (rtparam));
434                 rtparam.sched_priority = priority;
435         
436                 pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam);
437         }
438 }
439
440 void
441 Graph::helper_thread()
442 {
443         suspend_rt_malloc_checks ();
444         ProcessThread* pt = new ProcessThread ();
445         resume_rt_malloc_checks ();
446
447         pt->get_buffers();
448         get_rt();
449
450         while(1) {
451                 if (run_one()) {
452                         break;
453                 }
454         }
455
456         pt->drop_buffers();
457 }
458
459 void
460 Graph::main_thread()
461 {
462         suspend_rt_malloc_checks ();
463         ProcessThread* pt = new ProcessThread ();
464         resume_rt_malloc_checks ();
465
466         pt->get_buffers();
467         get_rt();
468
469   again:
470         _callback_start_sem.wait ();
471         DEBUG_TRACE(DEBUG::Graph, "main thread is awake\n");
472
473         if (_quit_threads) {
474                 return;
475         }
476
477         this->prep();
478
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");
482                 goto again;
483         }
484
485         while (1) {
486                 DEBUG_TRACE(DEBUG::Graph, "main thread runs one graph node\n");
487                 if (run_one()) {
488                         break;
489                 }
490         }
491
492         pt->drop_buffers();
493 }
494
495 void
496 Graph::dump (int chain)
497 {
498 #ifndef NDEBUG
499         node_list_t::iterator ni;
500         node_set_t::iterator ai;
501
502         chain = _pending_chain;
503
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()));
510                 }
511         }
512
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]));
516         }
517
518         DEBUG_TRACE (DEBUG::Graph, string_compose ("final activation refcount: %1\n", _init_finished_refcount[chain]));
519 #endif
520 }
521
522 int
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)
525 {
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;
531
532         _process_silent = true;
533         _process_noroll = false;
534         _process_retval = 0;
535         _process_need_butler = false;
536
537         if (!_graph_empty) {
538                 DEBUG_TRACE(DEBUG::Graph, "wake graph for silent process\n");
539                 _callback_start_sem.signal ();
540                 _callback_done_sem.wait ();
541         }
542
543         need_butler = _process_need_butler;
544
545         return _process_retval;
546 }
547
548 int
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)
551 {
552         DEBUG_TRACE (DEBUG::Graph, string_compose ("graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
553
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;
560
561         _process_silent = false;
562         _process_noroll = false;
563         _process_retval = 0;
564         _process_need_butler = false;
565
566         DEBUG_TRACE(DEBUG::Graph, "wake graph for non-silent process\n");
567         _callback_start_sem.signal ();
568         _callback_done_sem.wait ();
569
570         DEBUG_TRACE (DEBUG::Graph, "graph execution complete\n");
571
572         need_butler = _process_need_butler;
573
574         return _process_retval;
575 }
576
577 int
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)
580 {
581         DEBUG_TRACE (DEBUG::Graph, string_compose ("no-roll graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
582
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;
589
590         _process_silent = false;
591         _process_noroll = true;
592         _process_retval = 0;
593         _process_need_butler = false;
594
595         DEBUG_TRACE(DEBUG::Graph, "wake graph for no-roll process\n");
596         _callback_start_sem.signal ();
597         _callback_done_sem.wait ();
598
599         return _process_retval;
600 }
601 void
602 Graph::process_one_route (Route* route)
603 {
604         bool need_butler = false;
605         int retval;
606
607         assert (route);
608
609         DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_self(), route->name()));
610
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);
616         } else {
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);
619         }
620
621         if (retval) {
622                 _process_retval = retval;
623         }
624     
625         if (need_butler) {
626                 _process_need_butler = true;
627         }
628 }
629
630 bool
631 Graph::in_process_thread () const
632 {
633         list<pthread_t>::const_iterator i = _thread_list.begin ();
634         while (i != _thread_list.end() && *i != pthread_self ()) {
635                 ++i;
636         }
637
638         return i != _thread_list.end ();
639 }