Avi Drissman | 3f7a9d8 | 2022-09-08 20:55:42 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
[email protected] | e68684a | 2013-06-04 04:33:51 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | eade06d | 2014-02-04 07:04:32 | [diff] [blame] | 5 | #ifndef CC_BASE_ROLLING_TIME_DELTA_HISTORY_H_ |
6 | #define CC_BASE_ROLLING_TIME_DELTA_HISTORY_H_ | ||||
[email protected] | e68684a | 2013-06-04 04:33:51 | [diff] [blame] | 7 | |
avi | 02a4d17 | 2015-12-21 06:14:36 | [diff] [blame] | 8 | #include <stddef.h> |
9 | |||||
chongjiang | 6da29e4 | 2016-08-03 01:05:54 | [diff] [blame] | 10 | #include <set> |
[email protected] | e68684a | 2013-06-04 04:33:51 | [diff] [blame] | 11 | |
Brett Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 12 | #include "base/containers/circular_deque.h" |
Khushal | 17b77d9 | 2017-12-11 19:54:02 | [diff] [blame] | 13 | #include "base/containers/flat_map.h" |
[email protected] | 1b0df50 | 2013-06-27 23:39:58 | [diff] [blame] | 14 | #include "base/time/time.h" |
chrishtr | ac41ff9 | 2017-03-17 05:07:30 | [diff] [blame] | 15 | #include "cc/base/base_export.h" |
[email protected] | e68684a | 2013-06-04 04:33:51 | [diff] [blame] | 16 | |
17 | namespace cc { | ||||
18 | |||||
19 | // Stores a limited number of samples. When the maximum size is reached, each | ||||
20 | // insertion results in the deletion of the oldest remaining sample. | ||||
chrishtr | ac41ff9 | 2017-03-17 05:07:30 | [diff] [blame] | 21 | class CC_BASE_EXPORT RollingTimeDeltaHistory { |
[email protected] | e68684a | 2013-06-04 04:33:51 | [diff] [blame] | 22 | public: |
23 | explicit RollingTimeDeltaHistory(size_t max_size); | ||||
Vladimir Levin | f06d1cd7 | 2019-03-13 18:24:10 | [diff] [blame] | 24 | RollingTimeDeltaHistory(const RollingTimeDeltaHistory&) = delete; |
[email protected] | e68684a | 2013-06-04 04:33:51 | [diff] [blame] | 25 | |
26 | ~RollingTimeDeltaHistory(); | ||||
27 | |||||
Vladimir Levin | f06d1cd7 | 2019-03-13 18:24:10 | [diff] [blame] | 28 | RollingTimeDeltaHistory& operator=(const RollingTimeDeltaHistory&) = delete; |
29 | |||||
[email protected] | e68684a | 2013-06-04 04:33:51 | [diff] [blame] | 30 | void InsertSample(base::TimeDelta time); |
behdad | a572c15d | 2019-06-20 19:42:37 | [diff] [blame] | 31 | void RemoveOldestSample(); |
Khushal | 2f9cdf2 | 2018-01-08 21:47:08 | [diff] [blame] | 32 | size_t sample_count() const { return sample_set_.size(); } |
[email protected] | e68684a | 2013-06-04 04:33:51 | [diff] [blame] | 33 | |
34 | void Clear(); | ||||
35 | |||||
36 | // Returns the smallest sample that is greater than or equal to the specified | ||||
37 | // percent of samples. If there aren't any samples, returns base::TimeDelta(). | ||||
38 | base::TimeDelta Percentile(double percent) const; | ||||
39 | |||||
40 | private: | ||||
chongjiang | 6da29e4 | 2016-08-03 01:05:54 | [diff] [blame] | 41 | typedef std::multiset<base::TimeDelta> TimeDeltaMultiset; |
42 | |||||
Khushal | 17b77d9 | 2017-12-11 19:54:02 | [diff] [blame] | 43 | base::TimeDelta ComputePercentile(double percent) const; |
44 | |||||
chongjiang | 6da29e4 | 2016-08-03 01:05:54 | [diff] [blame] | 45 | TimeDeltaMultiset sample_set_; |
Brett Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 46 | base::circular_deque<TimeDeltaMultiset::iterator> chronological_sample_deque_; |
[email protected] | e68684a | 2013-06-04 04:33:51 | [diff] [blame] | 47 | size_t max_size_; |
48 | |||||
Khushal | 17b77d9 | 2017-12-11 19:54:02 | [diff] [blame] | 49 | mutable base::flat_map<double, base::TimeDelta> percentile_cache_; |
[email protected] | e68684a | 2013-06-04 04:33:51 | [diff] [blame] | 50 | }; |
51 | |||||
52 | } // namespace cc | ||||
53 | |||||
[email protected] | eade06d | 2014-02-04 07:04:32 | [diff] [blame] | 54 | #endif // CC_BASE_ROLLING_TIME_DELTA_HISTORY_H_ |