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