File: oom_kills_monitor.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 (110 lines) | stat: -rw-r--r-- 3,318 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2022 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_MEMORY_OOM_KILLS_MONITOR_H_
#define CHROME_BROWSER_MEMORY_OOM_KILLS_MONITOR_H_

#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/no_destructor.h"
#include "base/synchronization/atomic_flag.h"
#include "base/timer/timer.h"
#include "components/metrics/daily_event.h"

class PrefService;

namespace memory {

// Traces Linux Out-of-memory killer invocation count. It checks the oom_kill
// field in /proc/vmstat periodically.
class OOMKillsMonitor {
 public:
  OOMKillsMonitor(const OOMKillsMonitor&) = delete;
  OOMKillsMonitor& operator=(const OOMKillsMonitor&) = delete;

  ~OOMKillsMonitor();

  static OOMKillsMonitor& GetInstance();

  static void RegisterPrefs(PrefRegistrySimple* registry);

  void Initialize(PrefService* pref_service);

  void LogArcOOMKill(unsigned long current_oom_kills);

  // The following items are public for testing.

  // Difference for the following 2 histograms: OOMKillsCount logs the
  // cumulative OOM kills count since browser start on every OOM kills,
  // OOMKillsDaily is the total OOM kills count in a day. E.g., if there are 100
  // OOM kills in a day, OOMKillsCount logs 1 to the 0, 1, 2, 3, ...., 100
  // buckets, and OOMKillsDaily logs 1 to the 100 bucket.

  // OOM kills count histogram name.
  static const char kOOMKillsCountHistogramName[];

  // Daily OOM kills histogram name.
  static const char kOOMKillsDailyHistogramName[];

  // Stops timers to avoid unexpected timer fire in test.
  void StopTimersForTesting();

  // Trigger daily event manually for testing.
  void TriggerDailyEventForTesting();

 private:
  FRIEND_TEST_ALL_PREFIXES(OOMKillsMonitorTest, TestHistograms);

  friend class base::NoDestructor<OOMKillsMonitor>;

  class DailyEventObserver;

  OOMKillsMonitor();

  // Checks system OOM count.
  void CheckOOMKill();

  // Splits CheckOOMKill and CheckOOMKillImpl for testing.
  void CheckOOMKillImpl(unsigned long current_oom_kills);

  // Reports OOM Kills to histogram.
  void ReportOOMKills(unsigned long oom_kills_delta);

  void ReportDailyMetrics(metrics::DailyEvent::IntervalType type);

  // Member variables.

  // The number of OOM kills since monitoring is started.
  unsigned long oom_kills_count_ = 0;

  // The number of OOM kills per day.
  unsigned long oom_kills_daily_count_ = 0;

  // A flag set when Initialize() is called to indicate that monitoring has
  // been started.
  bool monitoring_started_ = false;

  // The last oom kills count.
  unsigned long last_oom_kills_count_ = 0;

  // The last ARCVM OOM kills count.
  unsigned long last_arc_oom_kills_count_ = 0;

  base::RepeatingTimer checking_timer_;

  std::unique_ptr<metrics::DailyEvent> daily_event_;

  // Instructs |daily_event_| to check if a day has passed.
  base::RepeatingTimer daily_event_timer_;

  // The name of the histogram used to report that the daily event happened.
  static const char kDailyEventHistogramName[];

  // A raw pointer to the PrefService used to read and write the statistics.
  raw_ptr<PrefService, LeakedDanglingUntriaged> pref_service_;
};

}  // namespace memory

#endif  // CHROME_BROWSER_MEMORY_OOM_KILLS_MONITOR_H_