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 protected: | 97 protected: |
86 StatsCounter(Clock* clock, | 98 StatsCounter(Clock* clock, |
87 bool include_empty_intervals, | 99 bool include_empty_intervals, |
88 StatsCounterObserver* observer); | 100 StatsCounterObserver* observer); |
89 | 101 |
90 void Add(int sample); | 102 void Add(int sample); |
91 void Set(int sample); | 103 void Set(int sample); |
92 | 104 |
93 int max_; | 105 int max_; |
94 int64_t sum_; | 106 int64_t sum_; |
95 int64_t num_samples_; | 107 int64_t num_samples_; |
96 int64_t last_sum_; | 108 int64_t last_sum_; |
97 | 109 |
| 110 const std::unique_ptr<AggregatedCounter> aggregated_counter_; |
| 111 |
98 private: | 112 private: |
99 bool TimeToProcess(); | 113 bool TimeToProcess(int* num_elapsed_intervals); |
100 void TryProcess(); | 114 void TryProcess(); |
| 115 void ReportMetricToAggregatedCounter(int value, int num_values_to_add) const; |
| 116 bool IncludeEmptyIntervals() const; |
101 | 117 |
102 Clock* const clock_; | 118 Clock* const clock_; |
103 const bool include_empty_intervals_; | 119 const bool include_empty_intervals_; |
104 const std::unique_ptr<StatsCounterObserver> observer_; | 120 const std::unique_ptr<StatsCounterObserver> observer_; |
105 const std::unique_ptr<AggregatedCounter> aggregated_counter_; | |
106 int64_t last_process_time_ms_; | 121 int64_t last_process_time_ms_; |
| 122 bool paused_; |
107 }; | 123 }; |
108 | 124 |
109 // AvgCounter: average of samples | 125 // AvgCounter: average of samples |
110 // | 126 // |
111 // | * * * | * * | ... | 127 // | * * * | * * | ... |
112 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | | 128 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | |
113 // GetMetric | (5 + 1 + 6) / 3 | (5 + 5) / 2 | | 129 // GetMetric | (5 + 1 + 6) / 3 | (5 + 5) / 2 | |
114 // | 130 // |
| 131 // |include_empty_intervals|: If set, intervals without samples will be included |
| 132 // in the stats. The value for an interval is |
| 133 // determined by GetValueForEmptyInterval(). |
| 134 // |
115 class AvgCounter : public StatsCounter { | 135 class AvgCounter : public StatsCounter { |
116 public: | 136 public: |
117 AvgCounter(Clock* clock, StatsCounterObserver* observer); | 137 AvgCounter(Clock* clock, |
| 138 StatsCounterObserver* observer, |
| 139 bool include_empty_intervals); |
118 ~AvgCounter() override {} | 140 ~AvgCounter() override {} |
119 | 141 |
120 void Add(int sample); | 142 void Add(int sample); |
121 | 143 |
122 private: | 144 private: |
123 bool GetMetric(int* metric) const override; | 145 bool GetMetric(int* metric) const override; |
124 | 146 |
| 147 // Returns the last computed metric (i.e. from GetMetric). |
| 148 int GetValueForEmptyInterval() const override; |
| 149 |
125 RTC_DISALLOW_COPY_AND_ASSIGN(AvgCounter); | 150 RTC_DISALLOW_COPY_AND_ASSIGN(AvgCounter); |
126 }; | 151 }; |
127 | 152 |
128 // MaxCounter: maximum of samples | 153 // MaxCounter: maximum of samples |
129 // | 154 // |
130 // | * * * | * * | ... | 155 // | * * * | * * | ... |
131 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | | 156 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | |
132 // GetMetric | max: (5, 1, 6) | max: (5, 5) | | 157 // GetMetric | max: (5, 1, 6) | max: (5, 5) | |
133 // | 158 // |
134 class MaxCounter : public StatsCounter { | 159 class MaxCounter : public StatsCounter { |
135 public: | 160 public: |
136 MaxCounter(Clock* clock, StatsCounterObserver* observer); | 161 MaxCounter(Clock* clock, StatsCounterObserver* observer); |
137 ~MaxCounter() override {} | 162 ~MaxCounter() override {} |
138 | 163 |
139 void Add(int sample); | 164 void Add(int sample); |
140 | 165 |
141 private: | 166 private: |
142 bool GetMetric(int* metric) const override; | 167 bool GetMetric(int* metric) const override; |
| 168 int GetValueForEmptyInterval() const override; |
143 | 169 |
144 RTC_DISALLOW_COPY_AND_ASSIGN(MaxCounter); | 170 RTC_DISALLOW_COPY_AND_ASSIGN(MaxCounter); |
145 }; | 171 }; |
146 | 172 |
147 // PercentCounter: percentage of samples | 173 // PercentCounter: percentage of samples |
148 // | 174 // |
149 // | * * * | * * | ... | 175 // | * * * | * * | ... |
150 // | Add(T) Add(F) Add(T) | Add(F) Add(T) | | 176 // | Add(T) Add(F) Add(T) | Add(F) Add(T) | |
151 // GetMetric | 100 * 2 / 3 | 100 * 1 / 2 | | 177 // GetMetric | 100 * 2 / 3 | 100 * 1 / 2 | |
152 // | 178 // |
153 class PercentCounter : public StatsCounter { | 179 class PercentCounter : public StatsCounter { |
154 public: | 180 public: |
155 PercentCounter(Clock* clock, StatsCounterObserver* observer); | 181 PercentCounter(Clock* clock, StatsCounterObserver* observer); |
156 ~PercentCounter() override {} | 182 ~PercentCounter() override {} |
157 | 183 |
158 void Add(bool sample); | 184 void Add(bool sample); |
159 | 185 |
160 private: | 186 private: |
161 bool GetMetric(int* metric) const override; | 187 bool GetMetric(int* metric) const override; |
| 188 int GetValueForEmptyInterval() const override; |
162 | 189 |
163 RTC_DISALLOW_COPY_AND_ASSIGN(PercentCounter); | 190 RTC_DISALLOW_COPY_AND_ASSIGN(PercentCounter); |
164 }; | 191 }; |
165 | 192 |
166 // PermilleCounter: permille of samples | 193 // PermilleCounter: permille of samples |
167 // | 194 // |
168 // | * * * | * * | ... | 195 // | * * * | * * | ... |
169 // | Add(T) Add(F) Add(T) | Add(F) Add(T) | | 196 // | Add(T) Add(F) Add(T) | Add(F) Add(T) | |
170 // GetMetric | 1000 * 2 / 3 | 1000 * 1 / 2 | | 197 // GetMetric | 1000 * 2 / 3 | 1000 * 1 / 2 | |
171 // | 198 // |
172 class PermilleCounter : public StatsCounter { | 199 class PermilleCounter : public StatsCounter { |
173 public: | 200 public: |
174 PermilleCounter(Clock* clock, StatsCounterObserver* observer); | 201 PermilleCounter(Clock* clock, StatsCounterObserver* observer); |
175 ~PermilleCounter() override {} | 202 ~PermilleCounter() override {} |
176 | 203 |
177 void Add(bool sample); | 204 void Add(bool sample); |
178 | 205 |
179 private: | 206 private: |
180 bool GetMetric(int* metric) const override; | 207 bool GetMetric(int* metric) const override; |
| 208 int GetValueForEmptyInterval() const override; |
181 | 209 |
182 RTC_DISALLOW_COPY_AND_ASSIGN(PermilleCounter); | 210 RTC_DISALLOW_COPY_AND_ASSIGN(PermilleCounter); |
183 }; | 211 }; |
184 | 212 |
185 // RateCounter: units per second | 213 // RateCounter: units per second |
186 // | 214 // |
187 // | * * * | * * | ... | 215 // | * * * | * * | ... |
188 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | | 216 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | |
189 // |<------ 2 sec ------->| | | 217 // |<------ 2 sec ------->| | |
190 // GetMetric | (5 + 1 + 6) / 2 | (5 + 5) / 2 | | 218 // GetMetric | (5 + 1 + 6) / 2 | (5 + 5) / 2 | |
191 // | 219 // |
| 220 // |include_empty_intervals|: If set, intervals without samples will be included |
| 221 // in the stats. The value for an interval is |
| 222 // determined by GetValueForEmptyInterval(). |
| 223 // |
192 class RateCounter : public StatsCounter { | 224 class RateCounter : public StatsCounter { |
193 public: | 225 public: |
194 RateCounter(Clock* clock, | 226 RateCounter(Clock* clock, |
195 StatsCounterObserver* observer, | 227 StatsCounterObserver* observer, |
196 bool include_empty_intervals); | 228 bool include_empty_intervals); |
197 ~RateCounter() override {} | 229 ~RateCounter() override {} |
198 | 230 |
199 void Add(int sample); | 231 void Add(int sample); |
200 | 232 |
201 private: | 233 private: |
202 bool GetMetric(int* metric) const override; | 234 bool GetMetric(int* metric) const override; |
| 235 int GetValueForEmptyInterval() const override; // Returns zero. |
203 | 236 |
204 RTC_DISALLOW_COPY_AND_ASSIGN(RateCounter); | 237 RTC_DISALLOW_COPY_AND_ASSIGN(RateCounter); |
205 }; | 238 }; |
206 | 239 |
207 // RateAccCounter: units per second (used for counters) | 240 // RateAccCounter: units per second (used for counters) |
208 // | 241 // |
209 // | * * * | * * | ... | 242 // | * * * | * * | ... |
210 // | Set(5) Set(6) Set(8) | Set(11) Set(13) | | 243 // | Set(5) Set(6) Set(8) | Set(11) Set(13) | |
211 // |<------ 2 sec ------->| | | 244 // |<------ 2 sec ------->| | |
212 // GetMetric | 8 / 2 | (13 - 8) / 2 | | 245 // GetMetric | 8 / 2 | (13 - 8) / 2 | |
213 // | 246 // |
| 247 // |include_empty_intervals|: If set, intervals without samples will be included |
| 248 // in the stats. The value for an interval is |
| 249 // determined by GetValueForEmptyInterval(). |
| 250 // |
214 class RateAccCounter : public StatsCounter { | 251 class RateAccCounter : public StatsCounter { |
215 public: | 252 public: |
216 RateAccCounter(Clock* clock, | 253 RateAccCounter(Clock* clock, |
217 StatsCounterObserver* observer, | 254 StatsCounterObserver* observer, |
218 bool include_empty_intervals); | 255 bool include_empty_intervals); |
219 ~RateAccCounter() override {} | 256 ~RateAccCounter() override {} |
220 | 257 |
221 void Set(int sample); | 258 void Set(int sample); |
222 | 259 |
223 private: | 260 private: |
224 bool GetMetric(int* metric) const override; | 261 bool GetMetric(int* metric) const override; |
| 262 int GetValueForEmptyInterval() const override; // Returns zero. |
225 | 263 |
226 RTC_DISALLOW_COPY_AND_ASSIGN(RateAccCounter); | 264 RTC_DISALLOW_COPY_AND_ASSIGN(RateAccCounter); |
227 }; | 265 }; |
228 | 266 |
229 } // namespace webrtc | 267 } // namespace webrtc |
230 | 268 |
231 #endif // WEBRTC_VIDEO_STATS_COUNTER_H_ | 269 #endif // WEBRTC_VIDEO_STATS_COUNTER_H_ |
OLD | NEW |