OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_ARC_METRICS_OOM_KILLS_MONITOR_H_ | 5 #ifndef COMPONENTS_ARC_METRICS_OOM_KILLS_MONITOR_H_ |
6 #define COMPONENTS_ARC_METRICS_OOM_KILLS_MONITOR_H_ | 6 #define COMPONENTS_ARC_METRICS_OOM_KILLS_MONITOR_H_ |
7 | 7 |
8 #include "base/lazy_instance.h" | |
8 #include "base/macros.h" | 9 #include "base/macros.h" |
9 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 11 #include "base/threading/thread.h" |
11 | 12 |
12 namespace arc { | 13 namespace arc { |
13 | 14 |
14 // Traces kernel OOM kill events and lowmemorykiller (if enabled) events. | 15 // Traces kernel OOM kill events and lowmemorykiller (if enabled) events. |
15 // | 16 // |
16 // OomKillsMonitor listens to kernel messages for both OOM kills and | 17 // OomKillsMonitor listens to kernel messages for both OOM kills and |
17 // lowmemorykiller kills, then reports to UMA. | 18 // lowmemorykiller kills, then reports to UMA. It uses a non-joinable thread |
19 // in order to avoid blocking shutdown. | |
18 // | 20 // |
19 // Note: There should be only one OomKillsMonitor instance globally at any given | 21 // Note: There should be only one OomKillsMonitor instance globally at any given |
20 // time, otherwise UMA would receive duplicate events. | 22 // time, otherwise UMA would receive duplicate events. |
21 class OomKillsMonitor { | 23 class OomKillsMonitor : public base::Thread { |
22 public: | 24 public: |
23 OomKillsMonitor(); | 25 // A handle representing the OomKillsMonitor's lifetime (the monitor itself |
24 ~OomKillsMonitor(); | 26 // can't be destroyed per being a non-joinable Thread). |
27 class OomKillsMonitorHandle { | |
28 public: | |
29 // Constructs a handle that will flag |outer| as shutting down on | |
30 // destruction. | |
31 explicit OomKillsMonitorHandle(OomKillsMonitor* outer); | |
25 | 32 |
26 void Start(); | 33 ~OomKillsMonitorHandle(); |
27 void Stop(); | 34 |
35 private: | |
36 OomKillsMonitor* const outer_; | |
Luis Héctor Chávez
2016/08/01 15:48:14
Now that I think of it, doesn't this also need a f
gab
2016/08/04 22:42:36
Inner classes have access to their Outer class' pr
| |
37 } | |
38 | |
39 // The instance has to be leaked on shutdown per being a non-joinable Thread. | |
40 ~OomKillsMonitor() = delete; | |
41 | |
42 // Instantiates the OomKillsMonitor instance and starts it. This can only | |
43 // be invoked once per process. | |
44 static OomKillsMonitorHandle StartMonitoring(); | |
28 | 45 |
29 private: | 46 private: |
30 // Keeps a reference to worker_pool_ in case |this| is deleted in | 47 friend struct base::internal::LeakyLazyInstanceTraits; |
31 // shutdown process while this thread returns from a blocking read. | 48 OomKillsMonitor(); |
32 static void Run(scoped_refptr<base::SequencedWorkerPool> worker_pool); | |
33 | 49 |
34 scoped_refptr<base::SequencedWorkerPool> worker_pool_; | 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 unecessary work, not | |
54 // because it blocks shutdown). | |
55 base::AtomicFlag is_shutting_down_; | |
35 | 56 |
36 DISALLOW_COPY_AND_ASSIGN(OomKillsMonitor); | 57 DISALLOW_COPY_AND_ASSIGN(OomKillsMonitor); |
37 }; | 58 }; |
38 | 59 |
39 } // namespace arc | 60 } // namespace arc |
40 | 61 |
41 #endif // COMPONENTS_ARC_METRICS_OOM_KILLS_MONITOR_H_ | 62 #endif // COMPONENTS_ARC_METRICS_OOM_KILLS_MONITOR_H_ |
OLD | NEW |