Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
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"
17#include "roc_core/array.h"
18#include "roc_core/iarena.h"
19#include "roc_core/list.h"
22#include "roc_core/time.h"
23#include "roc_packet/units.h"
24
25namespace roc {
26namespace 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.
65class Profiler : public core::NonCopyable<> {
66public:
67 //! Initialization.
69 const 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.
77 core::nanoseconds_t elapsed);
78
79 //! Get computed average.
81
82private:
83 void update_moving_avg_(packet::stream_timestamp_t frame_duration,
84 core::nanoseconds_t elapsed);
85
86 core::RateLimiter rate_limiter_;
87
88 core::nanoseconds_t interval_;
89
90 const size_t chunk_length_;
91 const size_t num_chunks_;
92 core::Array<float> chunks_;
93 size_t first_chunk_num_;
94 size_t last_chunk_num_;
95 size_t last_chunk_samples_;
96
97 float moving_avg_;
98
99 const SampleSpec sample_spec_;
100
101 bool valid_;
102 bool buffer_full_;
103};
104
105} // namespace audio
106} // namespace roc
107
108#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(packet::stream_timestamp_t frame_duration, core::nanoseconds_t elapsed)
Profile frame speed.
Profiler(core::IArena &arena, const SampleSpec &sample_spec, ProfilerConfig profiler_config)
Initialization.
bool is_valid() const
Check if the profiler was succefully constructed.
float get_moving_avg()
Get computed average.
Sample specification. Describes sample rate and channels.
Definition sample_spec.h:30
Dynamic array.
Definition array.h:40
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.
int64_t nanoseconds_t
Nanoseconds.
Definition time.h:58
uint32_t stream_timestamp_t
Packet stream timestamp.
Definition units.h:36
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.