File: recent_events_counter.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (75 lines) | stat: -rw-r--r-- 2,845 bytes parent folder | download | duplicates (7)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_POWER_ML_RECENT_EVENTS_COUNTER_H_
#define CHROME_BROWSER_ASH_POWER_ML_RECENT_EVENTS_COUNTER_H_

#include <vector>

#include "base/time/time.h"

namespace ash {
namespace power {
namespace ml {

// RecentEventsCounter keeps a running count of events that occurred in the last
// |duration| period of time. For example, a count of the number of events in
// the last hour.
//
// Rather than remembering the time stamp for each event, the event times are
// bucketed. The number of requested buckets must exactly divide a time period
// of |duration_| (within the precision of TimeDelta), and initially start at
// base::TimeDelta(). For logging at a time later than |duration_|, the buckets
// are reused, using the logging time modulo the |duration_| in the calculation
// of the bucket to be used. The total is calculated by keeping track of the
// |first_bucket_index_| and |first_bucket_time_| and zeroing buckets with stale
// data.
//
// The bucketing determines the time precision of the count. This
// means that the actual time period counted may be up to one bucket length
// shorter than the requested time period. It will never be longer than the
// requested time period.
class RecentEventsCounter {
 public:
  // Count events for a time period of length |duration| using
  // |num_buckets| buckets.
  RecentEventsCounter(base::TimeDelta duration, int num_buckets);

  RecentEventsCounter(const RecentEventsCounter&) = delete;
  RecentEventsCounter& operator=(const RecentEventsCounter&) = delete;

  ~RecentEventsCounter();

  // Log an event at timedelta |timestamp|. |timestamp| cannot be negative.
  void Log(base::TimeDelta timestamp);

  // Return the count of events reported in the |duration_| preceding |now|.
  // |now| must be >= any |timestamp| previously passed to Log().
  int GetTotal(base::TimeDelta now) const;

 private:
  // Return the index of the bucket containing |timestamp|.
  int GetBucketIndex(base::TimeDelta timestamp) const;

  // The length of time that events should be recorded.
  const base::TimeDelta duration_;
  // The number of buckets to use to record the events.
  const int num_buckets_;
  // The number of events in each bucket.
  std::vector<int> event_count_;
  // The index of the first bucket. |event_count_| is a circular array.
  int first_bucket_index_ = 0;
  // The starting time of the first bucket.
  base::TimeDelta first_bucket_time_;
  // The duration of each bucket.
  base::TimeDelta bucket_duration_;
  // The latest timedelta that has been logged.
  base::TimeDelta latest_;
};

}  // namespace ml
}  // namespace power
}  // namespace ash

#endif  // CHROME_BROWSER_ASH_POWER_ML_RECENT_EVENTS_COUNTER_H_