fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / ardour / dsp_load_calculator.h
1 /*
2  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #ifndef ARDOUR_DSP_LOAD_CALCULATOR_H
20 #define ARDOUR_DSP_LOAD_CALCULATOR_H
21
22 #include <stdint.h>
23 #include <cassert>
24 #include <algorithm>
25
26 namespace ARDOUR {
27
28 class DSPLoadCalculator {
29 public:
30         DSPLoadCalculator()
31             : m_max_time_us(0)
32             , m_start_timestamp_us(0)
33             , m_stop_timestamp_us(0)
34             , m_dsp_load(0)
35         {
36
37         }
38
39         void set_max_time(double samplerate, uint32_t period_size) {
40                 m_max_time_us = period_size * 1e6 / samplerate;
41         }
42
43         void set_max_time_us(uint64_t max_time_us) {
44                 assert(max_time_us != 0);
45                 m_max_time_us = max_time_us;
46         }
47
48         int64_t get_max_time_us() const { return m_max_time_us; }
49
50         void set_start_timestamp_us(int64_t start_timestamp_us)
51         {
52                 m_start_timestamp_us = start_timestamp_us;
53         }
54
55         void set_stop_timestamp_us(int64_t stop_timestamp_us)
56         {
57                 m_stop_timestamp_us = stop_timestamp_us;
58
59                 /* querying the performance counter can fail occasionally (-1).
60                  * Also on some multi-core systems, timers are CPU specific and not
61                  * synchronized. We assume they differ more than a few milliseconds
62                  * (4 * nominal cycle time) and simply ignore cases where the
63                  * execution switches cores.
64                  */
65                 if (m_start_timestamp_us < 0 || m_stop_timestamp_us < 0 ||
66                     m_start_timestamp_us > m_stop_timestamp_us ||
67                     elapsed_time_us() > max_timer_error_us()) {
68                         return;
69                 }
70
71                 const float load = (float) elapsed_time_us() / (float)m_max_time_us;
72                 if (load > m_dsp_load || load > 1.0) {
73                         m_dsp_load = load;
74                 } else {
75                         const float alpha = 0.2f * (m_max_time_us * 1e-6f);
76                         m_dsp_load = std::min (1.f, m_dsp_load);
77                         m_dsp_load += alpha * (load - m_dsp_load) + 1e-12;
78                 }
79         }
80
81         int64_t elapsed_time_us()
82         {
83                 return m_stop_timestamp_us - m_start_timestamp_us;
84         }
85
86         /**
87          * @return a decimal value between 0.0 and 1.0 representing the percentage
88          * of time spent between start and stop in proportion to the max expected time
89          * in microseconds(us).
90          */
91         float get_dsp_load() const
92         {
93                 assert (m_dsp_load >= 0.f); // since stop > start is assured this cannot happen.
94                 return std::min (1.f, m_dsp_load);
95         }
96
97         /**
98          * @return an unbound value representing the percentage of time spent between
99          * start and stop in proportion to the max expected time in microseconds(us).
100          * This is useful for cases to estimate overload (e.g. Dummy backend)
101          */
102         float get_dsp_load_unbound() const
103         {
104                 assert (m_dsp_load >= 0.f);
105                 return m_dsp_load;
106         }
107
108         /**
109          * The maximum error in timestamp values that will be tolerated before the
110          * current dsp load sample will be ignored
111          */
112         int64_t max_timer_error_us() { return 4 * m_max_time_us; }
113
114 private: // data
115         int64_t m_max_time_us;
116         int64_t m_start_timestamp_us;
117         int64_t m_stop_timestamp_us;
118         float m_dsp_load;
119 };
120
121 } // namespace ARDOUR
122
123 #endif // ARDOUR_DSP_LOAD_CALCULATOR_H