File: rolling_time_delta_history.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (86 lines) | stat: -rw-r--r-- 2,227 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>

#include <cmath>
#include <utility>

#include "cc/base/rolling_time_delta_history.h"

namespace cc {

RollingTimeDeltaHistory::RollingTimeDeltaHistory(size_t max_size)
    : max_size_(max_size) {}

RollingTimeDeltaHistory::~RollingTimeDeltaHistory() = default;

void RollingTimeDeltaHistory::InsertSample(base::TimeDelta time) {
  if (max_size_ == 0)
    return;

  if (sample_set_.size() == max_size_) {
    sample_set_.erase(chronological_sample_deque_.front());
    chronological_sample_deque_.pop_front();
  }

  auto it = sample_set_.insert(time);
  chronological_sample_deque_.push_back(it);
  percentile_cache_.clear();
}

void RollingTimeDeltaHistory::RemoveOldestSample() {
  if (sample_set_.size() > 0) {
    sample_set_.erase(chronological_sample_deque_.front());
    chronological_sample_deque_.pop_front();
  }
}

void RollingTimeDeltaHistory::Clear() {
  chronological_sample_deque_.clear();
  sample_set_.clear();
  percentile_cache_.clear();
}

base::TimeDelta RollingTimeDeltaHistory::Percentile(double percent) const {
  auto pair =
      percentile_cache_.insert(std::make_pair(percent, base::TimeDelta()));
  auto it = pair.first;
  bool inserted = pair.second;
  if (inserted)
    it->second = ComputePercentile(percent);
  return it->second;
}

base::TimeDelta RollingTimeDeltaHistory::ComputePercentile(
    double percent) const {
  if (sample_set_.size() == 0)
    return base::TimeDelta();

  double fraction = percent / 100.0;

  if (fraction <= 0.0)
    return *(sample_set_.begin());

  if (fraction >= 1.0)
    return *(sample_set_.rbegin());

  size_t num_smaller_samples =
      static_cast<size_t>(std::ceil(fraction * sample_set_.size())) - 1;

  if (num_smaller_samples > sample_set_.size() / 2) {
    size_t num_larger_samples = sample_set_.size() - num_smaller_samples - 1;
    auto it = sample_set_.rbegin();
    for (size_t i = 0; i < num_larger_samples; i++)
      it++;
    return *it;
  }

  auto it = sample_set_.begin();
  for (size_t i = 0; i < num_smaller_samples; i++)
    it++;
  return *it;
}

}  // namespace cc