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/iallocator.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 the 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 audio::SampleSpec& sample_spec,
70 ProfilerConfig profiler_config);
71
72 //! Check if the profiler was succefully constructed.
73 bool valid() const;
74
75 //! Profile frame speed
76 void add_frame(size_t frame_size, core::nanoseconds_t elapsed);
77
78 //! For Testing Only
80
81private:
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.
Profiler(core::IAllocator &allocator, const audio::SampleSpec &sample_spec, ProfilerConfig profiler_config)
Initialization.
float get_moving_avg()
For Testing Only.
bool valid() const
Check if the profiler was succefully constructed.
Sample stream specification. Defines sample rate and channel layout.
Definition: sample_spec.h:24
Dynamic array.
Definition: array.h:38
Memory allocator interface.
Definition: iallocator.h:23
Base class for non-copyable objects.
Definition: noncopyable.h:23
Audio frame.
Memory allocator interface.
Intrusive doubly-linked list.
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.