Move implementation for DSPLoadCalculator back into header
[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_us(uint64_t max_time_us) {
40                 assert(max_time_us != 0);
41                 m_max_time_us = max_time_us;
42         }
43
44         int64_t get_max_time_us() const { return m_max_time_us; }
45
46         void set_start_timestamp_us(int64_t start_timestamp_us)
47         {
48                 m_start_timestamp_us = start_timestamp_us;
49         }
50
51         void set_stop_timestamp_us(int64_t stop_timestamp_us)
52         {
53                 m_stop_timestamp_us = stop_timestamp_us;
54
55                 /* querying the performance counter can fail occasionally (-1).
56                  * Also on some multi-core systems, timers are CPU specific and not
57                  * synchronized. We assume they differ more than a few milliseconds
58                  * (4 * nominal cycle time) and simply ignore cases where the
59                  * execution switches cores.
60                  */
61                 if (m_start_timestamp_us < 0 || m_stop_timestamp_us < 0 ||
62                     m_start_timestamp_us > m_stop_timestamp_us ||
63                     elapsed_time_us() > max_timer_error()) {
64                         return;
65                 }
66
67                 if (elapsed_time_us() > m_max_time_us) {
68                         m_dsp_load = 1.0f;
69                 } else {
70                         const float load = elapsed_time_us() / (float)m_max_time_us;
71                         if (load > m_dsp_load) {
72                                 m_dsp_load = load;
73                         } else {
74                                 const float alpha = 0.2f * (m_max_time_us * 1e-6f);
75                                 m_dsp_load = m_dsp_load + alpha * (load - m_dsp_load) + 1e-12;
76                         }
77                 }
78         }
79
80         int64_t elapsed_time_us()
81         {
82                 return m_stop_timestamp_us - m_start_timestamp_us;
83         }
84
85         /**
86          * @return a decimal value between 0.0 and 1.0 representing the percentage
87          * of time spent between start and stop in proportion to the max expected time
88          * in microseconds(us).
89          */
90         float get_dsp_load() const
91         {
92                 if (m_dsp_load > m_max_time_us) {
93                         return 1.0f;
94                 }
95                 if (m_dsp_load < 0.0f) {
96                         return 0.0f;
97                 }
98                 return m_dsp_load;
99         }
100
101 private: // methods
102         int64_t max_timer_error () { return 4 * m_max_time_us; }
103
104 private: // data
105         int64_t m_max_time_us;
106         int64_t m_start_timestamp_us;
107         int64_t m_stop_timestamp_us;
108         float m_dsp_load;
109 };
110
111 } // namespace ARDOUR
112
113 #endif // ARDOUR_DSP_LOAD_CALCULATOR_H