| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 // AggregatedStats stats = counter.GetStats(); | 71 // AggregatedStats stats = counter.GetStats(); |
| 72 // stats: {min:4, max:15, avg:8} | 72 // stats: {min:4, max:15, avg:8} |
| 73 // | 73 // |
| 74 | 74 |
| 75 // Note: StatsCounter takes ownership of |observer|. | 75 // Note: StatsCounter takes ownership of |observer|. |
| 76 | 76 |
| 77 class StatsCounter { | 77 class StatsCounter { |
| 78 public: | 78 public: |
| 79 virtual ~StatsCounter(); | 79 virtual ~StatsCounter(); |
| 80 | 80 |
| 81 // Gets metric within an interval. Returns true on success false otherwise. |
| 81 virtual bool GetMetric(int* metric) const = 0; | 82 virtual bool GetMetric(int* metric) const = 0; |
| 82 | 83 |
| 84 // Gets the value to use for an interval without samples. |
| 85 virtual int GetValueForEmptyInterval() const = 0; |
| 86 |
| 87 // Gets aggregated stats (i.e. aggregate of periodically computed metrics). |
| 83 AggregatedStats GetStats(); | 88 AggregatedStats GetStats(); |
| 84 | 89 |
| 90 // Reports metrics for elapsed intervals to AggregatedCounter and GetStats. |
| 91 AggregatedStats ProcessAndGetStats(); |
| 92 |
| 93 // Reports metrics for elapsed intervals to AggregatedCounter and pauses stats |
| 94 // (i.e. empty intervals will be discarded until next sample is added). |
| 95 void ProcessAndPause(); |
| 96 |
| 85 // Checks if a sample has been added (i.e. Add or Set called). | 97 // Checks if a sample has been added (i.e. Add or Set called). |
| 86 bool HasSample() const; | 98 bool HasSample() const; |
| 87 | 99 |
| 88 protected: | 100 protected: |
| 89 StatsCounter(Clock* clock, | 101 StatsCounter(Clock* clock, |
| 90 bool include_empty_intervals, | 102 bool include_empty_intervals, |
| 91 StatsCounterObserver* observer); | 103 StatsCounterObserver* observer); |
| 92 | 104 |
| 93 void Add(int sample); | 105 void Add(int sample); |
| 94 void Set(int sample); | 106 void Set(int sample); |
| 95 | 107 |
| 96 int max_; | 108 int max_; |
| 97 int64_t sum_; | 109 int64_t sum_; |
| 98 int64_t num_samples_; | 110 int64_t num_samples_; |
| 99 int64_t last_sum_; | 111 int64_t last_sum_; |
| 100 | 112 |
| 113 const std::unique_ptr<AggregatedCounter> aggregated_counter_; |
| 114 |
| 101 private: | 115 private: |
| 102 bool TimeToProcess(); | 116 bool TimeToProcess(int* num_elapsed_intervals); |
| 103 void TryProcess(); | 117 void TryProcess(); |
| 118 void ReportMetricToAggregatedCounter(int value, int num_values_to_add) const; |
| 119 bool IncludeEmptyIntervals() const; |
| 104 | 120 |
| 105 Clock* const clock_; | 121 Clock* const clock_; |
| 106 const bool include_empty_intervals_; | 122 const bool include_empty_intervals_; |
| 107 const std::unique_ptr<StatsCounterObserver> observer_; | 123 const std::unique_ptr<StatsCounterObserver> observer_; |
| 108 const std::unique_ptr<AggregatedCounter> aggregated_counter_; | |
| 109 int64_t last_process_time_ms_; | 124 int64_t last_process_time_ms_; |
| 125 bool paused_; |
| 110 }; | 126 }; |
| 111 | 127 |
| 112 // AvgCounter: average of samples | 128 // AvgCounter: average of samples |
| 113 // | 129 // |
| 114 // | * * * | * * | ... | 130 // | * * * | * * | ... |
| 115 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | | 131 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | |
| 116 // GetMetric | (5 + 1 + 6) / 3 | (5 + 5) / 2 | | 132 // GetMetric | (5 + 1 + 6) / 3 | (5 + 5) / 2 | |
| 117 // | 133 // |
| 134 // |include_empty_intervals|: If set, intervals without samples will be included |
| 135 // in the stats. The value for an interval is |
| 136 // determined by GetValueForEmptyInterval(). |
| 137 // |
| 118 class AvgCounter : public StatsCounter { | 138 class AvgCounter : public StatsCounter { |
| 119 public: | 139 public: |
| 120 AvgCounter(Clock* clock, StatsCounterObserver* observer); | 140 AvgCounter(Clock* clock, |
| 141 StatsCounterObserver* observer, |
| 142 bool include_empty_intervals); |
| 121 ~AvgCounter() override {} | 143 ~AvgCounter() override {} |
| 122 | 144 |
| 123 void Add(int sample); | 145 void Add(int sample); |
| 124 | 146 |
| 125 private: | 147 private: |
| 126 bool GetMetric(int* metric) const override; | 148 bool GetMetric(int* metric) const override; |
| 127 | 149 |
| 150 // Returns the last computed metric (i.e. from GetMetric). |
| 151 int GetValueForEmptyInterval() const override; |
| 152 |
| 128 RTC_DISALLOW_COPY_AND_ASSIGN(AvgCounter); | 153 RTC_DISALLOW_COPY_AND_ASSIGN(AvgCounter); |
| 129 }; | 154 }; |
| 130 | 155 |
| 131 // MaxCounter: maximum of samples | 156 // MaxCounter: maximum of samples |
| 132 // | 157 // |
| 133 // | * * * | * * | ... | 158 // | * * * | * * | ... |
| 134 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | | 159 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | |
| 135 // GetMetric | max: (5, 1, 6) | max: (5, 5) | | 160 // GetMetric | max: (5, 1, 6) | max: (5, 5) | |
| 136 // | 161 // |
| 137 class MaxCounter : public StatsCounter { | 162 class MaxCounter : public StatsCounter { |
| 138 public: | 163 public: |
| 139 MaxCounter(Clock* clock, StatsCounterObserver* observer); | 164 MaxCounter(Clock* clock, StatsCounterObserver* observer); |
| 140 ~MaxCounter() override {} | 165 ~MaxCounter() override {} |
| 141 | 166 |
| 142 void Add(int sample); | 167 void Add(int sample); |
| 143 | 168 |
| 144 private: | 169 private: |
| 145 bool GetMetric(int* metric) const override; | 170 bool GetMetric(int* metric) const override; |
| 171 int GetValueForEmptyInterval() const override; |
| 146 | 172 |
| 147 RTC_DISALLOW_COPY_AND_ASSIGN(MaxCounter); | 173 RTC_DISALLOW_COPY_AND_ASSIGN(MaxCounter); |
| 148 }; | 174 }; |
| 149 | 175 |
| 150 // PercentCounter: percentage of samples | 176 // PercentCounter: percentage of samples |
| 151 // | 177 // |
| 152 // | * * * | * * | ... | 178 // | * * * | * * | ... |
| 153 // | Add(T) Add(F) Add(T) | Add(F) Add(T) | | 179 // | Add(T) Add(F) Add(T) | Add(F) Add(T) | |
| 154 // GetMetric | 100 * 2 / 3 | 100 * 1 / 2 | | 180 // GetMetric | 100 * 2 / 3 | 100 * 1 / 2 | |
| 155 // | 181 // |
| 156 class PercentCounter : public StatsCounter { | 182 class PercentCounter : public StatsCounter { |
| 157 public: | 183 public: |
| 158 PercentCounter(Clock* clock, StatsCounterObserver* observer); | 184 PercentCounter(Clock* clock, StatsCounterObserver* observer); |
| 159 ~PercentCounter() override {} | 185 ~PercentCounter() override {} |
| 160 | 186 |
| 161 void Add(bool sample); | 187 void Add(bool sample); |
| 162 | 188 |
| 163 private: | 189 private: |
| 164 bool GetMetric(int* metric) const override; | 190 bool GetMetric(int* metric) const override; |
| 191 int GetValueForEmptyInterval() const override; |
| 165 | 192 |
| 166 RTC_DISALLOW_COPY_AND_ASSIGN(PercentCounter); | 193 RTC_DISALLOW_COPY_AND_ASSIGN(PercentCounter); |
| 167 }; | 194 }; |
| 168 | 195 |
| 169 // PermilleCounter: permille of samples | 196 // PermilleCounter: permille of samples |
| 170 // | 197 // |
| 171 // | * * * | * * | ... | 198 // | * * * | * * | ... |
| 172 // | Add(T) Add(F) Add(T) | Add(F) Add(T) | | 199 // | Add(T) Add(F) Add(T) | Add(F) Add(T) | |
| 173 // GetMetric | 1000 * 2 / 3 | 1000 * 1 / 2 | | 200 // GetMetric | 1000 * 2 / 3 | 1000 * 1 / 2 | |
| 174 // | 201 // |
| 175 class PermilleCounter : public StatsCounter { | 202 class PermilleCounter : public StatsCounter { |
| 176 public: | 203 public: |
| 177 PermilleCounter(Clock* clock, StatsCounterObserver* observer); | 204 PermilleCounter(Clock* clock, StatsCounterObserver* observer); |
| 178 ~PermilleCounter() override {} | 205 ~PermilleCounter() override {} |
| 179 | 206 |
| 180 void Add(bool sample); | 207 void Add(bool sample); |
| 181 | 208 |
| 182 private: | 209 private: |
| 183 bool GetMetric(int* metric) const override; | 210 bool GetMetric(int* metric) const override; |
| 211 int GetValueForEmptyInterval() const override; |
| 184 | 212 |
| 185 RTC_DISALLOW_COPY_AND_ASSIGN(PermilleCounter); | 213 RTC_DISALLOW_COPY_AND_ASSIGN(PermilleCounter); |
| 186 }; | 214 }; |
| 187 | 215 |
| 188 // RateCounter: units per second | 216 // RateCounter: units per second |
| 189 // | 217 // |
| 190 // | * * * | * * | ... | 218 // | * * * | * * | ... |
| 191 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | | 219 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | |
| 192 // |<------ 2 sec ------->| | | 220 // |<------ 2 sec ------->| | |
| 193 // GetMetric | (5 + 1 + 6) / 2 | (5 + 5) / 2 | | 221 // GetMetric | (5 + 1 + 6) / 2 | (5 + 5) / 2 | |
| 194 // | 222 // |
| 223 // |include_empty_intervals|: If set, intervals without samples will be included |
| 224 // in the stats. The value for an interval is |
| 225 // determined by GetValueForEmptyInterval(). |
| 226 // |
| 195 class RateCounter : public StatsCounter { | 227 class RateCounter : public StatsCounter { |
| 196 public: | 228 public: |
| 197 RateCounter(Clock* clock, | 229 RateCounter(Clock* clock, |
| 198 StatsCounterObserver* observer, | 230 StatsCounterObserver* observer, |
| 199 bool include_empty_intervals); | 231 bool include_empty_intervals); |
| 200 ~RateCounter() override {} | 232 ~RateCounter() override {} |
| 201 | 233 |
| 202 void Add(int sample); | 234 void Add(int sample); |
| 203 | 235 |
| 204 private: | 236 private: |
| 205 bool GetMetric(int* metric) const override; | 237 bool GetMetric(int* metric) const override; |
| 238 int GetValueForEmptyInterval() const override; // Returns zero. |
| 206 | 239 |
| 207 RTC_DISALLOW_COPY_AND_ASSIGN(RateCounter); | 240 RTC_DISALLOW_COPY_AND_ASSIGN(RateCounter); |
| 208 }; | 241 }; |
| 209 | 242 |
| 210 // RateAccCounter: units per second (used for counters) | 243 // RateAccCounter: units per second (used for counters) |
| 211 // | 244 // |
| 212 // | * * * | * * | ... | 245 // | * * * | * * | ... |
| 213 // | Set(5) Set(6) Set(8) | Set(11) Set(13) | | 246 // | Set(5) Set(6) Set(8) | Set(11) Set(13) | |
| 214 // |<------ 2 sec ------->| | | 247 // |<------ 2 sec ------->| | |
| 215 // GetMetric | 8 / 2 | (13 - 8) / 2 | | 248 // GetMetric | 8 / 2 | (13 - 8) / 2 | |
| 216 // | 249 // |
| 250 // |include_empty_intervals|: If set, intervals without samples will be included |
| 251 // in the stats. The value for an interval is |
| 252 // determined by GetValueForEmptyInterval(). |
| 253 // |
| 217 class RateAccCounter : public StatsCounter { | 254 class RateAccCounter : public StatsCounter { |
| 218 public: | 255 public: |
| 219 RateAccCounter(Clock* clock, | 256 RateAccCounter(Clock* clock, |
| 220 StatsCounterObserver* observer, | 257 StatsCounterObserver* observer, |
| 221 bool include_empty_intervals); | 258 bool include_empty_intervals); |
| 222 ~RateAccCounter() override {} | 259 ~RateAccCounter() override {} |
| 223 | 260 |
| 224 void Set(int sample); | 261 void Set(int sample); |
| 225 | 262 |
| 226 private: | 263 private: |
| 227 bool GetMetric(int* metric) const override; | 264 bool GetMetric(int* metric) const override; |
| 265 int GetValueForEmptyInterval() const override; // Returns zero. |
| 228 | 266 |
| 229 RTC_DISALLOW_COPY_AND_ASSIGN(RateAccCounter); | 267 RTC_DISALLOW_COPY_AND_ASSIGN(RateAccCounter); |
| 230 }; | 268 }; |
| 231 | 269 |
| 232 } // namespace webrtc | 270 } // namespace webrtc |
| 233 | 271 |
| 234 #endif // WEBRTC_VIDEO_STATS_COUNTER_H_ | 272 #endif // WEBRTC_VIDEO_STATS_COUNTER_H_ |
| OLD | NEW |