OLD | NEW |
---|---|
(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 #include "chrome/browser/chromeos/resource_reporter/resource_reporter.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <queue> | |
10 #include <string> | |
11 | |
12 #include "base/bind.h" | |
13 #include "base/memory/singleton.h" | |
14 #include "base/strings/utf_string_conversions.h" | |
15 #include "base/sys_info.h" | |
16 #include "chrome/browser/browser_process.h" | |
17 #include "chrome/browser/task_management/task_manager_interface.h" | |
18 #include "components/rappor/rappor_service.h" | |
19 #include "content/public/browser/browser_thread.h" | |
20 | |
21 namespace chromeos { | |
22 | |
23 namespace { | |
24 | |
25 // The task manager refresh interval, currently at 1 minute. | |
26 const int64_t kRefreshIntervalSeconds = 60; | |
27 | |
28 // 1 GB in bytes. | |
29 const int64_t kMemory1GB = 1024 * 1024 * 1024; | |
30 | |
31 // 800 MB in bytes. | |
32 const int64_t kMemory800MB = 800 * 1024 * 1024; | |
33 | |
34 // 600 MB in bytes. | |
35 const int64_t kMemory600MB = 600 * 1024 * 1024; | |
36 | |
37 // 400 MB in bytes. | |
38 const int64_t kMemory400MB = 400 * 1024 * 1024; | |
39 | |
40 // 200 MB in bytes. | |
41 const int64_t kMemory200MB = 200 * 1024 * 1024; | |
42 | |
43 // The name of the Rappor metric to report the CPU usage. | |
44 const char kCpuRapporMetric[] = "ResourceReporter.Cpu"; | |
45 | |
46 // The name of the Rappor metric to report the memory usage. | |
47 const char kMemoryRapporMetric[] = "ResourceReporter.Memory"; | |
48 | |
49 // The name of the string field of the Rappor metrics in which we'll record the | |
50 // task's Rappor sample name. | |
51 const char kRapporTaskStringField[] = "task"; | |
52 | |
53 // The name of the flags field of the Rappor metrics in which we'll store the | |
54 // priority of the process on which the task is running. | |
55 const char kRapporPriorityFlagsField[] = "priority"; | |
56 | |
57 // The name of the flags field of the CPU usage Rappor metrics in which we'll | |
58 // record the number of cores in the current system. | |
59 const char kRapporNumCoresRangeFlagsField[] = "num_cores_range"; | |
60 | |
61 // The name of the flags field of the Rappor metrics in which we'll store the | |
62 // CPU / memory usage ranges. | |
63 const char kRapporUsageRangeFlagsField[] = "usage_range"; | |
64 | |
65 // A functor to sort the TaskRecords by their |cpu|. | |
66 struct TaskRecordByCpuSorter { | |
67 bool operator()(ResourceReporter::TaskRecord* const& lhs, | |
68 ResourceReporter::TaskRecord* const& rhs) const { | |
69 if (lhs->cpu == rhs->cpu) | |
70 return lhs->id < rhs->id; | |
71 return lhs->cpu < rhs->cpu; | |
72 } | |
73 }; | |
74 | |
75 // A functor to sort the TaskRecords by their |memory|. | |
76 struct TaskRecordByMemorySorter { | |
77 bool operator()(ResourceReporter::TaskRecord* const& lhs, | |
78 ResourceReporter::TaskRecord* const& rhs) const { | |
79 if (lhs->memory == rhs->memory) | |
80 return lhs->id < rhs->id; | |
81 return lhs->memory < rhs->memory; | |
82 } | |
83 }; | |
84 | |
85 } // namespace | |
86 | |
87 ResourceReporter::TaskRecord::TaskRecord(task_management::TaskId the_id) | |
88 : id(the_id), cpu(0.0), memory(0), is_background(false) { | |
89 } | |
90 | |
91 ResourceReporter::TaskRecord::TaskRecord(task_management::TaskId the_id, | |
92 const std::string& the_sample, | |
93 double the_cpu, | |
94 int64_t the_memory, | |
95 bool background) | |
96 : id(the_id), | |
97 rappor_sample(the_sample), | |
98 cpu(the_cpu), | |
99 memory(the_memory), | |
100 is_background(background) { | |
101 } | |
102 | |
103 ResourceReporter::~ResourceReporter() { | |
104 } | |
105 | |
106 // static | |
107 ResourceReporter* ResourceReporter::GetInstance() { | |
108 return base::Singleton<ResourceReporter>::get(); | |
109 } | |
110 | |
111 // static | |
112 void ResourceReporter::StartObservingMetricsService() { | |
113 auto metrics_service = g_browser_process->metrics_service(); | |
114 DCHECK(metrics_service); | |
115 metrics_service->AddObserver(ResourceReporter::GetInstance()); | |
116 } | |
117 | |
118 // static | |
119 void ResourceReporter::StopObservingMetricsService() { | |
120 // In case we were still monitoring the task manager, we must stop monitoring | |
121 // here, since we stop observing the MetricService we won't be able to | |
122 // receive any notifications from it when it actually stops. | |
123 ResourceReporter::GetInstance()->StopMonitoring(); | |
124 auto metrics_service = g_browser_process->metrics_service(); | |
125 DCHECK(metrics_service); | |
126 metrics_service->RemoveObserver(ResourceReporter::GetInstance()); | |
127 } | |
128 | |
129 void ResourceReporter::OnTaskAdded(task_management::TaskId id) { | |
130 // Ignore this event. | |
131 } | |
132 | |
133 void ResourceReporter::OnTaskToBeRemoved(task_management::TaskId id) { | |
134 auto itr = task_records_.find(id); | |
135 if (itr == task_records_.end()) | |
136 return; | |
137 | |
138 // Must be erased from the sorted set first. | |
139 // Note: this could mean that the sorted records are now less than | |
140 // |kTopConsumerCount| with other records in |task_records_| that can be | |
141 // added now. That's ok, we ignore this case. | |
142 auto cpu_itr = std::find(task_records_by_cpu_.begin(), | |
143 task_records_by_cpu_.end(), | |
144 itr->second.get()); | |
145 if (cpu_itr != task_records_by_cpu_.end()) | |
146 task_records_by_cpu_.erase(cpu_itr); | |
147 | |
148 auto memory_itr = std::find(task_records_by_memory_.begin(), | |
149 task_records_by_memory_.end(), | |
150 itr->second.get()); | |
151 if (memory_itr != task_records_by_memory_.end()) | |
152 task_records_by_memory_.erase(memory_itr); | |
153 | |
154 task_records_.erase(itr); | |
155 } | |
156 | |
157 void ResourceReporter::OnTasksRefreshed( | |
158 const task_management::TaskIdList& task_ids) { | |
159 // A priority queue to sort the task records by their |cpu|. Greatest |cpu| | |
160 // first. | |
161 std::priority_queue<TaskRecord*, | |
162 std::vector<TaskRecord*>, | |
163 TaskRecordByCpuSorter> records_by_cpu_queue; | |
164 // A priority queue to sort the task records by their |memory|. Greatest | |
165 // |memory| first. | |
166 std::priority_queue<TaskRecord*, | |
167 std::vector<TaskRecord*>, | |
168 TaskRecordByMemorySorter> records_by_memory_queue; | |
169 task_records_by_cpu_.clear(); | |
170 task_records_by_cpu_.reserve(kTopConsumersCount); | |
171 task_records_by_memory_.clear(); | |
172 task_records_by_memory_.reserve(kTopConsumersCount); | |
173 | |
174 for (const auto& id : task_ids) { | |
175 const double cpu_usage = observed_task_manager()->GetCpuUsage(id); | |
176 const int64_t memory_usage = | |
177 observed_task_manager()->GetPhysicalMemoryUsage(id); | |
178 | |
179 // Browser and GPU processes are reported later using UMA histograms as they | |
180 // don't have any privacy issues. | |
181 const auto task_type = observed_task_manager()->GetType(id); | |
182 switch (task_type) { | |
183 case task_management::Task::BROWSER: | |
184 last_browser_process_cpu_ = cpu_usage; | |
185 last_browser_process_memory_ = memory_usage != -1 ? memory_usage : 0; | |
186 break; | |
187 | |
188 case task_management::Task::GPU: | |
189 last_gpu_process_cpu_ = cpu_usage; | |
190 last_gpu_process_memory_ = memory_usage != -1 ? memory_usage : 0; | |
191 break; | |
192 | |
193 default: | |
ncarter (slow)
2015/11/13 22:44:32
Are you sure you want to sample all task types?
I
afakhry
2015/11/16 18:44:19
Done.
| |
194 // Other tasks types will be reported using Rappor. | |
195 TaskRecord* task_data = nullptr; | |
196 auto itr = task_records_.find(id); | |
197 if (itr == task_records_.end()) { | |
198 task_data = new TaskRecord(id); | |
199 task_records_[id] = make_scoped_ptr(task_data); | |
200 } else { | |
201 task_data = itr->second.get(); | |
202 } | |
203 | |
204 DCHECK_EQ(task_data->id, id); | |
205 task_data->rappor_sample = | |
206 observed_task_manager()->GetRapporSampleName(id); | |
207 task_data->cpu = cpu_usage; | |
208 task_data->memory = memory_usage; | |
209 task_data->is_background = | |
210 observed_task_manager()->IsTaskOnBackgroundedProcess(id); | |
211 | |
212 // Push only valid or useful data to both priority queues. They might | |
213 // end up having more records than |kTopConsumerCount|, that's fine. | |
214 // We'll take care of that next. | |
215 if (task_data->cpu > 0) | |
216 records_by_cpu_queue.push(task_data); | |
217 if (task_data->memory > 0) | |
218 records_by_memory_queue.push(task_data); | |
219 } | |
220 } | |
221 | |
222 while (!records_by_cpu_queue.empty() && | |
223 task_records_by_cpu_.size() < kTopConsumersCount) { | |
224 task_records_by_cpu_.push_back(records_by_cpu_queue.top()); | |
225 records_by_cpu_queue.pop(); | |
226 } | |
227 | |
228 while (!records_by_memory_queue.empty() && | |
229 task_records_by_memory_.size() < kTopConsumersCount) { | |
230 task_records_by_memory_.push_back(records_by_memory_queue.top()); | |
231 records_by_memory_queue.pop(); | |
232 } | |
233 } | |
234 | |
235 void ResourceReporter::OnMetricsServiceStart() { | |
236 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
Steven Holte
2015/11/13 20:32:25
Why are you observing the metrics service? I woul
afakhry
2015/11/13 20:56:06
The collection activities is done only for the pur
afakhry
2015/11/16 18:44:19
There are two things here. First thing is the stat
| |
237 | |
238 StartMonitoring(); | |
239 } | |
240 | |
241 void ResourceReporter::OnMetricsServiceStop() { | |
242 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
243 | |
244 StopMonitoring(); | |
245 } | |
246 | |
247 // static | |
248 const size_t ResourceReporter::kTopConsumersCount = 10U; | |
249 | |
250 ResourceReporter::ResourceReporter() | |
251 : TaskManagerObserver(base::TimeDelta::FromSeconds(kRefreshIntervalSeconds), | |
252 task_management::REFRESH_TYPE_CPU | | |
253 task_management::REFRESH_TYPE_MEMORY), | |
254 system_cpu_cores_range_(GetCurrentSystemCpuCoresRange()), | |
255 is_monitoring_(false) { | |
256 } | |
257 | |
258 // static | |
259 scoped_ptr<rappor::Sample> ResourceReporter::CreateRapporSample( | |
260 rappor::RapporService* rappor_service, | |
261 const ResourceReporter::TaskRecord& task_record) { | |
262 scoped_ptr<rappor::Sample> sample(rappor_service->CreateSample( | |
263 rappor::UMA_RAPPOR_TYPE)); | |
264 sample->SetStringField(kRapporTaskStringField, task_record.rappor_sample); | |
265 sample->SetFlagsField(kRapporPriorityFlagsField, | |
266 task_record.is_background ? BACKGROUND : FOREGROUND, | |
267 PRIORITIES_NUM); | |
268 return sample.Pass(); | |
269 } | |
270 | |
271 // static | |
272 ResourceReporter::CpuUsageRange | |
273 ResourceReporter::GetCpuUsageRange(double cpu) { | |
274 if (cpu > 60.0) | |
275 return RANGE_ABOVE_60_PERCENT; | |
276 if (cpu > 30.0) | |
277 return RANGE_30_TO_60_PERCENT; | |
278 if (cpu > 10.0) | |
279 return RANGE_10_TO_30_PERCENT; | |
280 | |
281 return RANGE_0_TO_10_PERCENT; | |
282 } | |
283 | |
284 // static | |
285 ResourceReporter::MemoryUsageRange | |
286 ResourceReporter::GetMemoryUsageRange(int64_t memory_in_bytes) { | |
287 if (memory_in_bytes > kMemory1GB) | |
288 return RANGE_ABOVE_1_GB; | |
289 if (memory_in_bytes > kMemory800MB) | |
290 return RANGE_800_TO_1_GB; | |
291 if (memory_in_bytes > kMemory600MB) | |
292 return RANGE_600_TO_800_MB; | |
293 if (memory_in_bytes > kMemory400MB) | |
294 return RANGE_400_TO_600_MB; | |
295 if (memory_in_bytes > kMemory200MB) | |
296 return RANGE_200_TO_400_MB; | |
297 | |
298 return RANGE_0_TO_200_MB; | |
299 } | |
300 | |
301 // static | |
302 ResourceReporter::CpuCoresNumberRange | |
303 ResourceReporter::GetCurrentSystemCpuCoresRange() { | |
304 const int cpus = base::SysInfo::NumberOfProcessors(); | |
305 | |
306 if (cpus > 16) | |
307 return RANGE_CORES_ABOVE_16_CORES; | |
308 if (cpus > 8) | |
309 return RANGE_CORES_9_TO_16_CORES; | |
310 if (cpus > 4) | |
311 return RANGE_CORES_5_TO_8_CORES; | |
312 if (cpus > 2) | |
313 return RANGE_CORES_3_TO_4_CORES; | |
314 if (cpus == 2) | |
315 return RANGE_CORES_2_CORES; | |
316 if (cpus == 1) | |
317 return RANGE_CORES_1_CORE; | |
318 | |
319 NOTREACHED(); | |
320 return RANGE_CORES_NA; | |
321 } | |
322 | |
323 void ResourceReporter::StartMonitoring() { | |
324 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
325 | |
326 if (is_monitoring_) | |
327 return; | |
328 | |
329 is_monitoring_ = true; | |
330 task_management::TaskManagerInterface::GetTaskManager()->AddObserver(this); | |
331 memory_pressure_listener_.reset(new base::MemoryPressureListener( | |
332 base::Bind(&ResourceReporter::OnMemoryPressure, base::Unretained(this)))); | |
333 } | |
334 | |
335 void ResourceReporter::StopMonitoring() { | |
336 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
337 | |
338 if (!is_monitoring_) | |
339 return; | |
340 | |
341 is_monitoring_ = false; | |
342 memory_pressure_listener_.reset(); | |
343 task_management::TaskManagerInterface::GetTaskManager()->RemoveObserver(this); | |
344 } | |
345 | |
346 void ResourceReporter::OnMemoryPressure( | |
347 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | |
348 // TODO(afakhry): Double check if we will ever receive a notification when | |
349 // we don't have memory pressure at all. The target is to report only once | |
350 // per each level value. | |
351 if (memory_pressure_level >= | |
352 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE && | |
353 memory_pressure_level > previous_memory_pressure_level_) { | |
354 // Report browser and GPU processes usage using UMA histograms. | |
355 UMA_HISTOGRAM_ENUMERATION("ResourceReporter.BrowserProcess.CpuUsage", | |
356 GetCpuUsageRange(last_browser_process_cpu_), | |
357 CPU_RANGES_NUM); | |
358 UMA_HISTOGRAM_ENUMERATION("ResourceReporter.BrowserProcess.MemoryUsage", | |
359 GetMemoryUsageRange(last_browser_process_memory_), | |
360 MEMORY_RANGES_NUM); | |
361 UMA_HISTOGRAM_ENUMERATION("ResourceReporter.GpuProcess.CpuUsage", | |
362 GetCpuUsageRange(last_gpu_process_cpu_), | |
363 CPU_RANGES_NUM); | |
364 UMA_HISTOGRAM_ENUMERATION("ResourceReporter.GpuProcess.MemoryUsage", | |
365 GetMemoryUsageRange(last_gpu_process_memory_), | |
366 MEMORY_RANGES_NUM); | |
367 | |
368 // For the rest of tasks, report them using Rappor. | |
369 auto rappor_service = g_browser_process->rappor_service(); | |
370 | |
371 for (const auto& task_data : task_records_by_cpu_) { | |
Steven Holte
2015/11/13 21:42:18
The code for creating the samples looks fine, but
ncarter (slow)
2015/11/13 22:44:32
Feel free to include me in this meeting, if it hap
| |
372 scoped_ptr<rappor::Sample> sample(CreateRapporSample(rappor_service, | |
373 *task_data)); | |
374 sample->SetFlagsField(kRapporNumCoresRangeFlagsField, | |
375 system_cpu_cores_range_, | |
376 CORES_RANGES_NUM); | |
377 sample->SetFlagsField(kRapporUsageRangeFlagsField, | |
378 GetCpuUsageRange(task_data->cpu), | |
379 CPU_RANGES_NUM); | |
ncarter (slow)
2015/11/13 22:44:32
Dumb question: would it make sense to record the m
afakhry
2015/11/16 18:44:19
I thought about that, but since top CPU tasks are
| |
380 rappor_service->RecordSampleObj(kCpuRapporMetric, sample.Pass()); | |
381 } | |
382 | |
383 for (const auto& task_data : task_records_by_memory_) { | |
384 scoped_ptr<rappor::Sample> sample(CreateRapporSample(rappor_service, | |
385 *task_data)); | |
386 sample->SetFlagsField(kRapporUsageRangeFlagsField, | |
387 GetMemoryUsageRange(task_data->memory), | |
388 MEMORY_RANGES_NUM); | |
389 rappor_service->RecordSampleObj(kMemoryRapporMetric, sample.Pass()); | |
390 } | |
391 } | |
392 | |
393 previous_memory_pressure_level_ = memory_pressure_level; | |
394 } | |
395 | |
396 } // namespace chromeos | |
OLD | NEW |