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

Side by Side Diff: chrome/browser/chromeos/resource_reporter/resource_reporter.h

Issue 1374283003: Reporting top cpu and memory consumers via rappor on chromeos (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add OWNERS file. Created 5 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 2015 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_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_
6 #define CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include "base/gtest_prod_util.h"
13 #include "base/macros.h"
14 #include "base/memory/memory_pressure_listener.h"
15 #include "base/time/time.h"
16 #include "chrome/browser/task_management/task_manager_observer.h"
17 #include "components/metrics/metrics_service.h"
18 #include "components/rappor/sample.h"
19
20 namespace base {
21 template <typename T>
22 struct DefaultSingletonTraits;
23 }
Daniel Erat 2015/11/23 23:57:20 i think it's generally considered fine (or even pr
afakhry 2015/11/24 01:52:33 Done.
24
25 using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel;
Daniel Erat 2015/11/23 23:57:20 please don't pull things out into the top-level na
afakhry 2015/11/24 01:52:33 Done.
26
27 namespace chromeos {
28
29 // A system that tracks the top |kTopConsumersCount| CPU and memory consumer
30 // Chrome tasks and reports a weighted random sample of them via Rappor whenever
31 // memory pressure is moderate or higher. The reporting is limited to once per
32 // |kMinimumTimeBetweenReportsInMS|.
33 class ResourceReporter : public task_management::TaskManagerObserver {
34 public:
35 // A collection of the data of a task manager's task that the ResourceReporter
36 // is interested in.
37 struct TaskRecord {
38 explicit TaskRecord(task_management::TaskId the_id);
Daniel Erat 2015/11/23 23:57:20 s/the_id/task_id/ ?
afakhry 2015/11/24 01:52:34 Done.
39
40 TaskRecord(task_management::TaskId the_id,
Daniel Erat 2015/11/23 23:57:20 same here
afakhry 2015/11/24 01:52:33 Done.
41 const std::string& task_name,
42 double the_cpu,
Daniel Erat 2015/11/23 23:57:20 why "the_" on all of these? something like "cpu_pe
afakhry 2015/11/24 01:52:33 Done.
43 int64_t the_memory,
Daniel Erat 2015/11/23 23:57:20 include the units here. "memory_bytes"? "memory_kb
afakhry 2015/11/24 01:52:34 Done.
44 bool background);
45
46 // The ID of the task.
47 task_management::TaskId id;
48
49 // The canonicalized task name to be used to represent the task in a Rappor
50 // sample.
51 std::string task_name_for_rappor;
52
53 // The CPU usage of the task from the most recent task manager refresh in
54 // percentage.
55 double cpu;
56
57 // The physical memory usage of the task from the most recent task manager
58 // refresh in bytes. It doesn't include shared memory. A value of -1 is
59 // invalid. See TaskManagerInterface::GetPhysicalMemoryUsage().
Daniel Erat 2015/11/23 23:57:20 does this mean "-1 is used to represent missing va
afakhry 2015/11/24 01:52:33 It means that the memory usage calculation for thi
60 int64_t memory;
61
62 // True if the task is running on a process at background priority.
63 bool is_background;
64 };
65
66 ~ResourceReporter() override;
67
68 // The singleton instance.
69 static ResourceReporter* GetInstance();
70
71 // Start / stop observing the task manager and the memory pressure events.
72 void StartMonitoring();
73 void StopMonitoring();
74
75 // task_management::TaskManagerObserver:
76 void OnTaskAdded(task_management::TaskId id) override;
77 void OnTaskToBeRemoved(task_management::TaskId id) override;
78 void OnTasksRefreshed(const task_management::TaskIdList& task_ids) override;
79
80 private:
81 friend struct base::DefaultSingletonTraits<ResourceReporter>;
82 friend class ResourceReporterTest;
83 FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestGetCpuRapporMetricName);
84 FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestGetMemoryRapporMetricName);
85 FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestAll);
86
87 // WARNING: The below enum MUST never be renamed, modified or reordered, as
88 // they're written to logs. You can only insert a new element immediately
89 // before the last.
90 enum TaskProcessPriority {
Daniel Erat 2015/11/23 23:57:20 please use enum classes for all of these to make i
afakhry 2015/11/24 01:52:33 I think it would be better not to use enum classes
91 FOREGROUND = 0,
92 BACKGROUND,
Daniel Erat 2015/11/23 23:57:20 i'd recommend explicitly assigning e.g. BACKGROUND
afakhry 2015/11/24 01:52:33 Done.
93 PRIORITIES_NUM,
94 };
95
96 // WARNING: The below enum MUST never be renamed, modified or reordered, as
97 // they're written to logs. You can only insert a new element immediately
98 // before the last.
99 enum CpuUsageRange {
100 RANGE_0_TO_10_PERCENT = 0,
101 RANGE_10_TO_30_PERCENT,
102 RANGE_30_TO_60_PERCENT,
103 RANGE_ABOVE_60_PERCENT,
104 CPU_RANGES_NUM,
105 };
106
107 // WARNING: The below enum MUST never be renamed, modified or reordered, as
108 // they're written to logs. You can only insert a new element immediately
109 // before the last.
110 enum MemoryUsageRange {
111 RANGE_0_TO_200_MB = 0,
112 RANGE_200_TO_400_MB,
113 RANGE_400_TO_600_MB,
114 RANGE_600_TO_800_MB,
115 RANGE_800_TO_1_GB,
116 RANGE_ABOVE_1_GB,
117 MEMORY_RANGES_NUM,
118 };
119
120 // WARNING: The below enum MUST never be renamed, modified or reordered, as
121 // they're written to logs. You can only insert a new element immediately
122 // before the last.
123 enum CpuCoresNumberRange {
124 RANGE_CORES_NA = 0,
125 RANGE_CORES_1_CORE,
126 RANGE_CORES_2_CORES,
127 RANGE_CORES_3_TO_4_CORES,
128 RANGE_CORES_5_TO_8_CORES,
129 RANGE_CORES_9_TO_16_CORES,
130 RANGE_CORES_ABOVE_16_CORES,
131 CORES_RANGES_NUM,
132 };
133
134 // The maximum number of top consumer tasks of each resource that we're
135 // interested in reporting.
136 static const size_t kTopConsumersCount;
137
138 ResourceReporter();
139
140 // Creates a Rappor sample for the given |task_record|.
141 static scoped_ptr<rappor::Sample> CreateRapporSample(
142 rappor::RapporService* rappor_service,
143 const TaskRecord& task_record);
144
145 // Gets the CPU/memory usage ranges given the |cpu| / |memory_in_bytes|
146 // values.
147 static CpuUsageRange GetCpuUsageRange(double cpu);
Daniel Erat 2015/11/23 23:57:20 any reason not to just put these into the anonymou
afakhry 2015/11/24 01:52:33 These are exposed for testing.
148 static MemoryUsageRange GetMemoryUsageRange(int64_t memory_in_bytes);
149
150 // Gets the bucket in which the current system's number of CPU cores fall
151 // into.
Daniel Erat 2015/11/23 23:57:20 grammar nit: remove "into" since this comment alre
afakhry 2015/11/24 01:52:33 Done.
152 static CpuCoresNumberRange GetCurrentSystemCpuCoresRange();
153
154 // Determines if we're allowed to record Rappor samples when a memory pressure
155 // event greater than or equal to a moderate pressure is received.
156 bool ShouldRecordSamples();
157
158 // Perform a weighted random sampling to select a task by its CPU or memory
159 // usage weights so that we can record samples for them via Rappor.
160 // They return nullptr if no TaskRecord has been selected.
161 const TaskRecord* SampleTaskByCpu() const;
162 const TaskRecord* SampleTaskByMemory() const;
163
164 // The callback function that will be invoked on memory pressure events.
165 void OnMemoryPressure(MemoryPressureLevel memory_pressure_level);
166
167 // We'll use this to watch for memory pressure events so that we can trigger
168 // Rappor sampling at moderate memory pressure or higher.
169 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_;
170
171 // Contains the collected data about the currently running tasks from the most
172 // recent task manager refresh.
173 std::map<task_management::TaskId, scoped_ptr<TaskRecord>> task_records_;
174
175 // Contains the top |kTopConsumerCount| CPU consumer tasks sorted in a
176 // descending order by their CPU usage.
177 std::vector<TaskRecord*> task_records_by_cpu_;
178
179 // Contains the top |kTopConsumerCount| memory consumer tasks sorted in a
180 // descending order by their memory usage.
181 std::vector<TaskRecord*> task_records_by_memory_;
182
183 // The time at which the previous memory pressure event (moderate or higher)
184 // was received at which we recorded Rappor samples. This is used to limit
185 // generating a Rappor report to once per |kMinimumTimeBetweenReportsInMS|.
186 // This is needed to avoid generating a lot of samples that can lower the
187 // Rappor privacy guarantees.
188 base::TimeTicks last_memory_pressure_event_time_;
189
190 const CpuCoresNumberRange system_cpu_cores_range_;
Daniel Erat 2015/11/23 23:57:20 what does this represent? the range that includes
afakhry 2015/11/24 01:52:33 Yes. I added a comment.
191
192 // the most recent reading for the browser and GPU processes to be reported as
Daniel Erat 2015/11/23 23:57:20 nit: s/the/The/
afakhry 2015/11/24 01:52:33 Done.
193 // UMA histograms when the system is under moderate memory pressure or higher.
194 double last_browser_process_cpu_ = 0.0;
195 double last_gpu_process_cpu_ = 0.0;
196 int64_t last_browser_process_memory_ = 0;
197 int64_t last_gpu_process_memory_ = 0;
198
199 // Tracks whether monitoring started or not.
200 bool is_monitoring_ = false;
201
202 // Whether we ever received a memory pressure event for the first time that is
203 // greater than or equal to a moderate pressure.
204 bool is_first_memory_pressure_event_ = true;
205
206 DISALLOW_COPY_AND_ASSIGN(ResourceReporter);
207 };
208
209 } // namespace chromeos
210
211 #endif // CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698