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 <memory> |
| 9 |
8 #include "base/macros.h" | 10 #include "base/macros.h" |
9 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/synchronization/atomic_flag.h" |
| 13 #include "base/threading/simple_thread.h" |
11 | 14 |
12 namespace arc { | 15 namespace arc { |
13 | 16 |
14 // Traces kernel OOM kill events and lowmemorykiller (if enabled) events. | 17 // Traces kernel OOM kill events and lowmemorykiller (if enabled) events. |
15 // | 18 // |
16 // OomKillsMonitor listens to kernel messages for both OOM kills and | 19 // OomKillsMonitor listens to kernel messages for both OOM kills and |
17 // lowmemorykiller kills, then reports to UMA. | 20 // lowmemorykiller kills, then reports to UMA. It uses a non-joinable thread |
| 21 // in order to avoid blocking shutdown. |
18 // | 22 // |
19 // Note: There should be only one OomKillsMonitor instance globally at any given | 23 // Note: There should be only one OomKillsMonitor instance globally at any given |
20 // time, otherwise UMA would receive duplicate events. | 24 // time, otherwise UMA would receive duplicate events. |
21 class OomKillsMonitor { | 25 class OomKillsMonitor : public base::DelegateSimpleThread::Delegate { |
22 public: | 26 public: |
23 OomKillsMonitor(); | 27 // A handle representing the OomKillsMonitor's lifetime (the monitor itself |
24 ~OomKillsMonitor(); | 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); |
25 | 34 |
26 void Start(); | 35 ~Handle(); |
27 void Stop(); | 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(); |
28 | 44 |
29 private: | 45 private: |
30 // Keeps a reference to worker_pool_ in case |this| is deleted in | 46 OomKillsMonitor(); |
31 // shutdown process while this thread returns from a blocking read. | 47 ~OomKillsMonitor() override; |
32 static void Run(scoped_refptr<base::SequencedWorkerPool> worker_pool); | |
33 | 48 |
34 scoped_refptr<base::SequencedWorkerPool> worker_pool_; | 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 unecessary 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_; |
35 | 60 |
36 DISALLOW_COPY_AND_ASSIGN(OomKillsMonitor); | 61 DISALLOW_COPY_AND_ASSIGN(OomKillsMonitor); |
37 }; | 62 }; |
38 | 63 |
39 } // namespace arc | 64 } // namespace arc |
40 | 65 |
41 #endif // COMPONENTS_ARC_METRICS_OOM_KILLS_MONITOR_H_ | 66 #endif // COMPONENTS_ARC_METRICS_OOM_KILLS_MONITOR_H_ |
OLD | NEW |