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