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