OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef COMPONENTS_ARC_METRICS_OOM_KILLS_MONITOR_H_ | |
6 #define COMPONENTS_ARC_METRICS_OOM_KILLS_MONITOR_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "base/macros.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/synchronization/atomic_flag.h" | |
13 #include "base/threading/simple_thread.h" | |
14 | |
15 namespace arc { | |
16 | |
17 // Traces kernel OOM kill events and lowmemorykiller (if enabled) events. | |
18 // | |
19 // OomKillsMonitor listens to kernel messages for both OOM kills and | |
20 // lowmemorykiller kills, then reports to UMA. It uses a non-joinable thread | |
21 // in order to avoid blocking shutdown. | |
22 // | |
23 // Note: There should be only one OomKillsMonitor instance globally at any given | |
24 // time, otherwise UMA would receive duplicate events. | |
25 class OomKillsMonitor : public base::DelegateSimpleThread::Delegate { | |
26 public: | |
27 // A handle representing the OomKillsMonitor's lifetime (the monitor itself | |
28 // can't be destroyed per being a non-joinable Thread). | |
29 class Handle { | |
30 public: | |
31 // Constructs a handle that will flag |outer| as shutting down on | |
32 // destruction. | |
33 explicit Handle(OomKillsMonitor* outer); | |
34 | |
35 ~Handle(); | |
36 | |
37 private: | |
38 OomKillsMonitor* const outer_; | |
39 }; | |
40 | |
41 // Instantiates the OomKillsMonitor instance and starts it. This must only | |
42 // be invoked once per process. | |
43 static Handle StartMonitoring(); | |
44 | |
45 private: | |
46 OomKillsMonitor(); | |
47 ~OomKillsMonitor() override; | |
48 | |
49 // Overridden from base::DelegateSimpleThread::Delegate: | |
50 void Run() override; | |
51 | |
52 // A flag set when OomKillsMonitor is shutdown so that its thread can poll | |
53 // it and attempt to wind down from that point (to avoid unnecessary work, not | |
54 // because it blocks shutdown). | |
55 base::AtomicFlag is_shutting_down_; | |
56 | |
57 // The underlying worker thread which is non-joinable to avoid blocking | |
58 // shutdown. | |
59 std::unique_ptr<base::DelegateSimpleThread> non_joinable_worker_thread_; | |
60 | |
61 DISALLOW_COPY_AND_ASSIGN(OomKillsMonitor); | |
62 }; | |
63 | |
64 } // namespace arc | |
65 | |
66 #endif // COMPONENTS_ARC_METRICS_OOM_KILLS_MONITOR_H_ | |
OLD | NEW |