Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: chrome/browser/memory/memory_kills_monitor.h

Issue 2527973003: Consolidate code monitoring low memory kills and OOM kills to MemoryKillsMonitor on ChromeOS. (Closed)
Patch Set: review comments 2. fix move constructor Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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).
Georges Khalil 2016/11/30 20:53:35 nit: TabManager.
cylee1 2016/12/01 20:02:45 Done.
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);
47
48 ~Handle();
49
50 private:
51 MemoryKillsMonitor* 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_
OLDNEW
« no previous file with comments | « chrome/browser/memory/memory_kills_histogram.h ('k') | chrome/browser/memory/memory_kills_monitor.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698