blob: 6924ed84ce9398b56e278f97d9de1e82feebc24f [file] [log] [blame]
Avi Drissman3f7a9d82022-09-08 20:55:421// Copyright 2014 The Chromium Authors
[email protected]e68684a2013-06-04 04:33:512// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]eade06d2014-02-04 07:04:325#ifndef CC_BASE_ROLLING_TIME_DELTA_HISTORY_H_
6#define CC_BASE_ROLLING_TIME_DELTA_HISTORY_H_
[email protected]e68684a2013-06-04 04:33:517
avi02a4d172015-12-21 06:14:368#include <stddef.h>
9
chongjiang6da29e42016-08-03 01:05:5410#include <set>
[email protected]e68684a2013-06-04 04:33:5111
Brett Wilson55ff1475e2017-09-26 00:28:4812#include "base/containers/circular_deque.h"
Khushal17b77d92017-12-11 19:54:0213#include "base/containers/flat_map.h"
[email protected]1b0df502013-06-27 23:39:5814#include "base/time/time.h"
chrishtrac41ff92017-03-17 05:07:3015#include "cc/base/base_export.h"
[email protected]e68684a2013-06-04 04:33:5116
17namespace 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.
chrishtrac41ff92017-03-17 05:07:3021class CC_BASE_EXPORT RollingTimeDeltaHistory {
[email protected]e68684a2013-06-04 04:33:5122 public:
23 explicit RollingTimeDeltaHistory(size_t max_size);
Vladimir Levinf06d1cd72019-03-13 18:24:1024 RollingTimeDeltaHistory(const RollingTimeDeltaHistory&) = delete;
[email protected]e68684a2013-06-04 04:33:5125
26 ~RollingTimeDeltaHistory();
27
Vladimir Levinf06d1cd72019-03-13 18:24:1028 RollingTimeDeltaHistory& operator=(const RollingTimeDeltaHistory&) = delete;
29
[email protected]e68684a2013-06-04 04:33:5130 void InsertSample(base::TimeDelta time);
behdada572c15d2019-06-20 19:42:3731 void RemoveOldestSample();
Khushal2f9cdf22018-01-08 21:47:0832 size_t sample_count() const { return sample_set_.size(); }
[email protected]e68684a2013-06-04 04:33:5133
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:
chongjiang6da29e42016-08-03 01:05:5441 typedef std::multiset<base::TimeDelta> TimeDeltaMultiset;
42
Khushal17b77d92017-12-11 19:54:0243 base::TimeDelta ComputePercentile(double percent) const;
44
chongjiang6da29e42016-08-03 01:05:5445 TimeDeltaMultiset sample_set_;
Brett Wilson55ff1475e2017-09-26 00:28:4846 base::circular_deque<TimeDeltaMultiset::iterator> chronological_sample_deque_;
[email protected]e68684a2013-06-04 04:33:5147 size_t max_size_;
48
Khushal17b77d92017-12-11 19:54:0249 mutable base::flat_map<double, base::TimeDelta> percentile_cache_;
[email protected]e68684a2013-06-04 04:33:5150};
51
52} // namespace cc
53
[email protected]eade06d2014-02-04 07:04:3254#endif // CC_BASE_ROLLING_TIME_DELTA_HISTORY_H_