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 CHROME_BROWSER_MEMORY_MEMORY_KILLS_MONITOR_H_ | |
6 #define CHROME_BROWSER_MEMORY_MEMORY_KILLS_MONITOR_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 | |
11 #include "base/files/scoped_file.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/synchronization/atomic_flag.h" | |
15 #include "base/synchronization/lock.h" | |
16 #include "base/threading/simple_thread.h" | |
17 #include "base/time/time.h" | |
18 | |
19 namespace memory { | |
20 | |
21 // Traces kernel OOM kill events and Low memory kill events (by Chrome | |
22 // TabManagr). | |
23 // | |
24 // For OOM kill events, it listens to kernel message (/dev/kmsg) in a blocking | |
25 // manner. It runs in a non-joinable thread in order to avoid blocking shutdown. | |
26 // There should be only one MemoryKillsMonitor instance globally at any given | |
27 // time, otherwise UMA would receive duplicate events. | |
28 // | |
29 // For Low memory kills events, chrome calls the single global instance of | |
30 // MemoryKillsMonitor synchronously. Note that it would be from a browser thread | |
31 // other than the listening thread. | |
32 // | |
33 // For every events, it reports to UMA and optionally a local file specified by | |
34 // --memory-kills-log. The log file is useful if we want to analyze low memory | |
35 // kills. If the flag is not given, it won't write to any file. | |
36 class MemoryKillsMonitor : public base::DelegateSimpleThread::Delegate { | |
37 public: | |
38 // A handle representing the MemoryKillsMonitor's lifetime (the monitor itself | |
39 // can't be destroyed per being a non-joinable Thread). | |
40 class Handle { | |
41 public: | |
42 // Constructs a handle that will flag |outer| as shutting down on | |
43 // destruction. | |
44 explicit Handle(MemoryKillsMonitor* outer); | |
45 | |
46 Handle(Handle&& handle) = default; | |
hidehiko
2016/11/30 09:05:48
Does this actually work? IIUC, ptr move is just a
cylee1
2016/11/30 17:18:57
Re-implemented. I should nullify the moved instanc
| |
47 | |
48 ~Handle(); | |
49 | |
50 private: | |
51 MemoryKillsMonitor* const outer_; | |
52 DISALLOW_COPY_AND_ASSIGN(Handle); | |
53 }; | |
54 | |
55 // Instantiates the MemoryKillsMonitor instance and starts it. This must only | |
56 // be invoked once per process. | |
57 static Handle StartMonitoring(); | |
58 | |
59 // Logs a low memory kill event. | |
60 static void LogLowMemoryKill(const std::string& type, int estimated_freed_kb); | |
61 | |
62 // Writes an OOM kill event or low memory kill event to file. | |
63 void WriteToFile(base::Time time_stamp, const char* event); | |
64 | |
65 private: | |
66 MemoryKillsMonitor(); | |
67 ~MemoryKillsMonitor() override; | |
68 | |
69 // Overridden from base::DelegateSimpleThread::Delegate: | |
70 void Run() override; | |
71 | |
72 // A flag set when MemoryKillsMonitor is shutdown so that its thread can poll | |
73 // it and attempt to wind down from that point (to avoid unnecessary work, not | |
74 // because it blocks shutdown). | |
75 base::AtomicFlag is_shutting_down_; | |
76 | |
77 // The underlying worker thread which is non-joinable to avoid blocking | |
78 // shutdown. | |
79 std::unique_ptr<base::DelegateSimpleThread> non_joinable_worker_thread_; | |
80 | |
81 // The handle to the optional log file to write to. | |
82 base::ScopedFILE log_file_; | |
83 // Multiple threads may want to write to the log file, so we need a lock to | |
84 // ensure thread-safety. | |
85 mutable base::Lock log_file_lock_; | |
86 | |
87 DISALLOW_COPY_AND_ASSIGN(MemoryKillsMonitor); | |
88 }; | |
89 | |
90 } // namespace memory | |
91 | |
92 #endif // CHROME_BROWSER_MEMORY_MEMORY_KILLS_MONITOR_H_ | |
OLD | NEW |