Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
profiler.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_audio/profiler.h
10 //! @brief Profiler.
11 
12 #ifndef ROC_AUDIO_PROFILER_H_
13 #define ROC_AUDIO_PROFILER_H_
14 
15 #include "roc_audio/frame.h"
16 #include "roc_audio/sample_spec.h"
17 #include "roc_core/array.h"
18 #include "roc_core/iarena.h"
19 #include "roc_core/list.h"
20 #include "roc_core/noncopyable.h"
21 #include "roc_core/rate_limiter.h"
22 #include "roc_core/time.h"
23 #include "roc_packet/units.h"
24 
25 namespace roc {
26 namespace audio {
27 
28 //! Profiler Configuration Parameters.
29 //! Controls profiling interval and duration of each circular buffer chunk.
31  //! Default Initialization.
33  : profiling_interval(core::Second)
34  , chunk_duration(10 * core::Millisecond) {
35  }
36 
37  //! Override Initialization.
39  : profiling_interval(interval)
40  , chunk_duration(duration) {
41  }
42 
43  //! Rolling window duration and reporting interval.
45 
46  //! Duration of samples each chunk can hold in the circular buffer.
48 };
49 
50 //! Profiler
51 //! The role of the profiler is to report the average processing speed (# of samples
52 //! processed per time unit) during the last N seconds. We want to calculate the average
53 //! processing speed efficiently (with O(1) complexity, without allocations, and as
54 //! lightweight as possible). The problems with this are that we have variable-sized
55 //! frames and SMA requires fixed-size chunks. To efficiently perform this calculation a
56 //! ring buffer is employed. The idea behind the ring buffer is that each chunk of the
57 //! buffer is the average speed of 10ms worth of samples. The ring buffer is initialized
58 //! with fixed size (N * 1000)ms / (10ms) chunks. Within each chunk a weighted mean is
59 //! used to calculate the average speed during those 10ms. Each frame will contribute a
60 //! different number of samples to each chunk, the chunk speed is then weighted based on
61 //! how many samples are contributed at what frame speed. As the chunks get populated the
62 //! moving average is calculated. When the buffer is not entirely full the cumulative
63 //! moving average algorithm is used and once the buffer is full the simple moving average
64 //! algorithm is used.
65 class Profiler : public core::NonCopyable<> {
66 public:
67  //! Initialization.
69  const audio::SampleSpec& sample_spec,
70  ProfilerConfig profiler_config);
71 
72  //! Check if the profiler was succefully constructed.
73  bool is_valid() const;
74 
75  //! Profile frame speed.
76  void add_frame(size_t frame_size, core::nanoseconds_t elapsed);
77 
78  //! Get computed average.
79  float get_moving_avg();
80 
81 private:
82  void update_moving_avg_(size_t frame_size, core::nanoseconds_t elapsed);
83 
84  core::RateLimiter rate_limiter_;
85 
86  core::nanoseconds_t interval_;
87 
88  const size_t chunk_length_;
89  const size_t num_chunks_;
90  core::Array<float> chunks_;
91  size_t first_chunk_num_;
92  size_t last_chunk_num_;
93  size_t last_chunk_samples_;
94 
95  float moving_avg_;
96 
97  const audio::SampleSpec sample_spec_;
98 
99  bool valid_;
100  bool buffer_full_;
101 };
102 
103 } // namespace audio
104 } // namespace roc
105 
106 #endif // ROC_AUDIO_PROFILER_H_
Dynamic array.
Profiler The role of the profiler is to report the average processing speed (# of samples processed p...
Definition: profiler.h:65
void add_frame(size_t frame_size, core::nanoseconds_t elapsed)
Profile frame speed.
bool is_valid() const
Check if the profiler was succefully constructed.
float get_moving_avg()
Get computed average.
Profiler(core::IArena &arena, const audio::SampleSpec &sample_spec, ProfilerConfig profiler_config)
Initialization.
Sample specification. Describes sample rate and channels.
Definition: sample_spec.h:26
Memory arena interface.
Definition: iarena.h:23
Base class for non-copyable objects.
Definition: noncopyable.h:23
Audio frame.
Memory arena interface.
Intrusive doubly-linked list.
const nanoseconds_t Millisecond
One millisecond represented in nanoseconds.
Definition: time.h:67
const nanoseconds_t Second
One second represented in nanoseconds.
Definition: time.h:70
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
Root namespace.
Non-copyable object.
Rate limiter.
Sample specifications.
Profiler Configuration Parameters. Controls profiling interval and duration of each circular buffer c...
Definition: profiler.h:30
core::nanoseconds_t chunk_duration
Duration of samples each chunk can hold in the circular buffer.
Definition: profiler.h:47
ProfilerConfig()
Default Initialization.
Definition: profiler.h:32
ProfilerConfig(core::nanoseconds_t interval, core::nanoseconds_t duration)
Override Initialization.
Definition: profiler.h:38
core::nanoseconds_t profiling_interval
Rolling window duration and reporting interval.
Definition: profiler.h:44
Time definitions.
Various units used in packets.