asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef VIDEO_STATS_COUNTER_H_ |
| 12 | #define VIDEO_STATS_COUNTER_H_ |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 13 | |
| 14 | #include <memory> |
asapersson | 43cb716 | 2016-11-15 16:20:48 | [diff] [blame] | 15 | #include <string> |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 16 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 17 | #include "rtc_base/constructor_magic.h" |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | class AggregatedCounter; |
| 22 | class Clock; |
asapersson | fe647f4 | 2016-11-01 07:21:34 | [diff] [blame] | 23 | class Samples; |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 24 | |
| 25 | // |StatsCounterObserver| is called periodically when a metric is updated. |
| 26 | class StatsCounterObserver { |
| 27 | public: |
| 28 | virtual void OnMetricUpdated(int sample) = 0; |
| 29 | |
| 30 | virtual ~StatsCounterObserver() {} |
| 31 | }; |
| 32 | |
| 33 | struct AggregatedStats { |
asapersson | 43cb716 | 2016-11-15 16:20:48 | [diff] [blame] | 34 | std::string ToString() const; |
asapersson | 076c011 | 2016-11-30 13:17:16 | [diff] [blame] | 35 | std::string ToStringWithMultiplier(int multiplier) const; |
asapersson | 43cb716 | 2016-11-15 16:20:48 | [diff] [blame] | 36 | |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 37 | int64_t num_samples = 0; |
| 38 | int min = -1; |
| 39 | int max = -1; |
| 40 | int average = -1; |
| 41 | // TODO(asapersson): Consider adding median/percentiles. |
| 42 | }; |
| 43 | |
| 44 | // Classes which periodically computes a metric. |
| 45 | // |
| 46 | // During a period, |kProcessIntervalMs|, different metrics can be computed e.g: |
| 47 | // - |AvgCounter|: average of samples |
| 48 | // - |PercentCounter|: percentage of samples |
| 49 | // - |PermilleCounter|: permille of samples |
| 50 | // |
| 51 | // Each periodic metric can be either: |
| 52 | // - reported to an |observer| each period |
| 53 | // - aggregated during the call (e.g. min, max, average) |
| 54 | // |
| 55 | // periodically computed |
| 56 | // GetMetric() GetMetric() => AggregatedStats |
| 57 | // ^ ^ (e.g. min/max/avg) |
| 58 | // | | |
| 59 | // | * * * * | ** * * * * | ... |
| 60 | // |<- process interval ->| |
| 61 | // |
| 62 | // (*) - samples |
| 63 | // |
| 64 | // |
| 65 | // Example usage: |
| 66 | // |
| 67 | // AvgCounter counter(&clock, nullptr); |
| 68 | // counter.Add(5); |
| 69 | // counter.Add(1); |
| 70 | // counter.Add(6); // process interval passed -> GetMetric() avg:4 |
| 71 | // counter.Add(7); |
| 72 | // counter.Add(3); // process interval passed -> GetMetric() avg:5 |
| 73 | // counter.Add(10); |
| 74 | // counter.Add(20); // process interval passed -> GetMetric() avg:15 |
| 75 | // AggregatedStats stats = counter.GetStats(); |
| 76 | // stats: {min:4, max:15, avg:8} |
| 77 | // |
| 78 | |
| 79 | // Note: StatsCounter takes ownership of |observer|. |
| 80 | |
| 81 | class StatsCounter { |
| 82 | public: |
| 83 | virtual ~StatsCounter(); |
| 84 | |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 85 | // Gets metric within an interval. Returns true on success false otherwise. |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 86 | virtual bool GetMetric(int* metric) const = 0; |
| 87 | |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 88 | // Gets the value to use for an interval without samples. |
| 89 | virtual int GetValueForEmptyInterval() const = 0; |
| 90 | |
| 91 | // Gets aggregated stats (i.e. aggregate of periodically computed metrics). |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 92 | AggregatedStats GetStats(); |
| 93 | |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 94 | // Reports metrics for elapsed intervals to AggregatedCounter and GetStats. |
| 95 | AggregatedStats ProcessAndGetStats(); |
| 96 | |
| 97 | // Reports metrics for elapsed intervals to AggregatedCounter and pauses stats |
| 98 | // (i.e. empty intervals will be discarded until next sample is added). |
| 99 | void ProcessAndPause(); |
| 100 | |
asapersson | 93e1e23 | 2017-02-06 13:18:35 | [diff] [blame] | 101 | // As above with a minimum pause time. Added samples within this interval will |
| 102 | // not resume the stats (i.e. stop the pause). |
| 103 | void ProcessAndPauseForDuration(int64_t min_pause_time_ms); |
| 104 | |
| 105 | // Reports metrics for elapsed intervals to AggregatedCounter and stops pause. |
| 106 | void ProcessAndStopPause(); |
| 107 | |
asapersson | 250fd97 | 2016-09-08 07:07:21 | [diff] [blame] | 108 | // Checks if a sample has been added (i.e. Add or Set called). |
| 109 | bool HasSample() const; |
| 110 | |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 111 | protected: |
| 112 | StatsCounter(Clock* clock, |
asapersson | e402a14 | 2016-10-07 06:39:15 | [diff] [blame] | 113 | int64_t process_intervals_ms, |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 114 | bool include_empty_intervals, |
| 115 | StatsCounterObserver* observer); |
| 116 | |
| 117 | void Add(int sample); |
asapersson | 93e1e23 | 2017-02-06 13:18:35 | [diff] [blame] | 118 | void Set(int64_t sample, uint32_t stream_id); |
| 119 | void SetLast(int64_t sample, uint32_t stream_id); |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 120 | |
asapersson | fe647f4 | 2016-11-01 07:21:34 | [diff] [blame] | 121 | const bool include_empty_intervals_; |
asapersson | e402a14 | 2016-10-07 06:39:15 | [diff] [blame] | 122 | const int64_t process_intervals_ms_; |
asapersson | fe647f4 | 2016-11-01 07:21:34 | [diff] [blame] | 123 | const std::unique_ptr<AggregatedCounter> aggregated_counter_; |
| 124 | const std::unique_ptr<Samples> samples_; |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 125 | |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 126 | private: |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 127 | bool TimeToProcess(int* num_elapsed_intervals); |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 128 | void TryProcess(); |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 129 | void ReportMetricToAggregatedCounter(int value, int num_values_to_add) const; |
| 130 | bool IncludeEmptyIntervals() const; |
asapersson | 93e1e23 | 2017-02-06 13:18:35 | [diff] [blame] | 131 | void Resume(); |
| 132 | void ResumeIfMinTimePassed(); |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 133 | |
| 134 | Clock* const clock_; |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 135 | const std::unique_ptr<StatsCounterObserver> observer_; |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 136 | int64_t last_process_time_ms_; |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 137 | bool paused_; |
asapersson | 93e1e23 | 2017-02-06 13:18:35 | [diff] [blame] | 138 | int64_t pause_time_ms_; |
| 139 | int64_t min_pause_time_ms_; |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | // AvgCounter: average of samples |
| 143 | // |
| 144 | // | * * * | * * | ... |
| 145 | // | Add(5) Add(1) Add(6) | Add(5) Add(5) | |
| 146 | // GetMetric | (5 + 1 + 6) / 3 | (5 + 5) / 2 | |
| 147 | // |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 148 | // |include_empty_intervals|: If set, intervals without samples will be included |
| 149 | // in the stats. The value for an interval is |
| 150 | // determined by GetValueForEmptyInterval(). |
| 151 | // |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 152 | class AvgCounter : public StatsCounter { |
| 153 | public: |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 154 | AvgCounter(Clock* clock, |
| 155 | StatsCounterObserver* observer, |
| 156 | bool include_empty_intervals); |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 157 | ~AvgCounter() override {} |
| 158 | |
| 159 | void Add(int sample); |
| 160 | |
| 161 | private: |
| 162 | bool GetMetric(int* metric) const override; |
| 163 | |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 164 | // Returns the last computed metric (i.e. from GetMetric). |
| 165 | int GetValueForEmptyInterval() const override; |
| 166 | |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 167 | RTC_DISALLOW_COPY_AND_ASSIGN(AvgCounter); |
| 168 | }; |
| 169 | |
| 170 | // MaxCounter: maximum of samples |
| 171 | // |
| 172 | // | * * * | * * | ... |
| 173 | // | Add(5) Add(1) Add(6) | Add(5) Add(5) | |
| 174 | // GetMetric | max: (5, 1, 6) | max: (5, 5) | |
| 175 | // |
| 176 | class MaxCounter : public StatsCounter { |
| 177 | public: |
asapersson | e402a14 | 2016-10-07 06:39:15 | [diff] [blame] | 178 | MaxCounter(Clock* clock, |
| 179 | StatsCounterObserver* observer, |
| 180 | int64_t process_intervals_ms); |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 181 | ~MaxCounter() override {} |
| 182 | |
| 183 | void Add(int sample); |
| 184 | |
| 185 | private: |
| 186 | bool GetMetric(int* metric) const override; |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 187 | int GetValueForEmptyInterval() const override; |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 188 | |
| 189 | RTC_DISALLOW_COPY_AND_ASSIGN(MaxCounter); |
| 190 | }; |
| 191 | |
| 192 | // PercentCounter: percentage of samples |
| 193 | // |
| 194 | // | * * * | * * | ... |
| 195 | // | Add(T) Add(F) Add(T) | Add(F) Add(T) | |
| 196 | // GetMetric | 100 * 2 / 3 | 100 * 1 / 2 | |
| 197 | // |
| 198 | class PercentCounter : public StatsCounter { |
| 199 | public: |
| 200 | PercentCounter(Clock* clock, StatsCounterObserver* observer); |
| 201 | ~PercentCounter() override {} |
| 202 | |
| 203 | void Add(bool sample); |
| 204 | |
| 205 | private: |
| 206 | bool GetMetric(int* metric) const override; |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 207 | int GetValueForEmptyInterval() const override; |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 208 | |
| 209 | RTC_DISALLOW_COPY_AND_ASSIGN(PercentCounter); |
| 210 | }; |
| 211 | |
| 212 | // PermilleCounter: permille of samples |
| 213 | // |
| 214 | // | * * * | * * | ... |
| 215 | // | Add(T) Add(F) Add(T) | Add(F) Add(T) | |
| 216 | // GetMetric | 1000 * 2 / 3 | 1000 * 1 / 2 | |
| 217 | // |
| 218 | class PermilleCounter : public StatsCounter { |
| 219 | public: |
| 220 | PermilleCounter(Clock* clock, StatsCounterObserver* observer); |
| 221 | ~PermilleCounter() override {} |
| 222 | |
| 223 | void Add(bool sample); |
| 224 | |
| 225 | private: |
| 226 | bool GetMetric(int* metric) const override; |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 227 | int GetValueForEmptyInterval() const override; |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 228 | |
| 229 | RTC_DISALLOW_COPY_AND_ASSIGN(PermilleCounter); |
| 230 | }; |
| 231 | |
| 232 | // RateCounter: units per second |
| 233 | // |
| 234 | // | * * * | * * | ... |
| 235 | // | Add(5) Add(1) Add(6) | Add(5) Add(5) | |
| 236 | // |<------ 2 sec ------->| | |
| 237 | // GetMetric | (5 + 1 + 6) / 2 | (5 + 5) / 2 | |
| 238 | // |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 239 | // |include_empty_intervals|: If set, intervals without samples will be included |
| 240 | // in the stats. The value for an interval is |
| 241 | // determined by GetValueForEmptyInterval(). |
| 242 | // |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 243 | class RateCounter : public StatsCounter { |
| 244 | public: |
asapersson | 5093b38 | 2016-08-15 08:20:30 | [diff] [blame] | 245 | RateCounter(Clock* clock, |
| 246 | StatsCounterObserver* observer, |
| 247 | bool include_empty_intervals); |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 248 | ~RateCounter() override {} |
| 249 | |
| 250 | void Add(int sample); |
| 251 | |
| 252 | private: |
| 253 | bool GetMetric(int* metric) const override; |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 254 | int GetValueForEmptyInterval() const override; // Returns zero. |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 255 | |
| 256 | RTC_DISALLOW_COPY_AND_ASSIGN(RateCounter); |
| 257 | }; |
| 258 | |
| 259 | // RateAccCounter: units per second (used for counters) |
| 260 | // |
| 261 | // | * * * | * * | ... |
| 262 | // | Set(5) Set(6) Set(8) | Set(11) Set(13) | |
| 263 | // |<------ 2 sec ------->| | |
asapersson | 93e1e23 | 2017-02-06 13:18:35 | [diff] [blame] | 264 | // GetMetric | (8 - 0) / 2 | (13 - 8) / 2 | |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 265 | // |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 266 | // |include_empty_intervals|: If set, intervals without samples will be included |
| 267 | // in the stats. The value for an interval is |
| 268 | // determined by GetValueForEmptyInterval(). |
| 269 | // |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 270 | class RateAccCounter : public StatsCounter { |
| 271 | public: |
asapersson | 5093b38 | 2016-08-15 08:20:30 | [diff] [blame] | 272 | RateAccCounter(Clock* clock, |
| 273 | StatsCounterObserver* observer, |
| 274 | bool include_empty_intervals); |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 275 | ~RateAccCounter() override {} |
| 276 | |
asapersson | 93e1e23 | 2017-02-06 13:18:35 | [diff] [blame] | 277 | void Set(int64_t sample, uint32_t stream_id); |
| 278 | |
| 279 | // Sets the value for previous interval. |
| 280 | // To be used if a value other than zero is initially required. |
| 281 | void SetLast(int64_t sample, uint32_t stream_id); |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 282 | |
| 283 | private: |
| 284 | bool GetMetric(int* metric) const override; |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 285 | int GetValueForEmptyInterval() const override; // Returns zero. |
asapersson | 0e9d6d9 | 2016-05-23 13:07:55 | [diff] [blame] | 286 | |
| 287 | RTC_DISALLOW_COPY_AND_ASSIGN(RateAccCounter); |
| 288 | }; |
| 289 | |
| 290 | } // namespace webrtc |
| 291 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 292 | #endif // VIDEO_STATS_COUNTER_H_ |