Chromium Code Reviews| 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 #include <string> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/memory/singleton.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "base/sys_info.h" | |
| 14 #include "chrome/browser/browser_process.h" | |
| 15 #include "chrome/browser/task_management/task_manager_interface.h" | |
| 16 #include "components/rappor/rappor_service.h" | |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 | |
| 19 namespace chromeos { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // The task manager refresh interval, currently at 1 minute. | |
| 24 const int64_t kRefreshIntervalSeconds = 60; | |
| 25 | |
| 26 // 1 GB in bytes. | |
| 27 const int64_t kMemory1GB = 1024 * 1024 * 1024; | |
| 28 | |
| 29 // 800 MB in bytes. | |
| 30 const int64_t kMemory800MB = 800 * 1024 * 1024; | |
| 31 | |
| 32 // 600 MB in bytes. | |
| 33 const int64_t kMemory600MB = 600 * 1024 * 1024; | |
| 34 | |
| 35 // 400 MB in bytes. | |
| 36 const int64_t kMemory400MB = 400 * 1024 * 1024; | |
| 37 | |
| 38 // 200 MB in bytes. | |
| 39 const int64_t kMemory200MB = 200 * 1024 * 1024; | |
| 40 | |
| 41 // The name of the Rappor metric to report the CPU usage. | |
| 42 const char kCpuRapporMetric[] = "ResourceReporter.Cpu"; | |
| 43 | |
| 44 // The name of the Rappor metric to report the memory usage. | |
| 45 const char kMemoryRapporMetric[] = "ResourceReporter.Memory"; | |
| 46 | |
| 47 // The name of the string field of the Rappor metrics in which we'll record the | |
| 48 // task's Rappor sample name. | |
| 49 const char kRapporTaskStringField[] = "task"; | |
| 50 | |
| 51 // The name of the flags field of the Rappor metrics in which we'll store the | |
| 52 // priority of the process on which the task is running. | |
| 53 const char kRapporPriorityFlagsField[] = "priority"; | |
| 54 | |
| 55 // The name of the flags field of the CPU usage Rappor metrics in which we'll | |
| 56 // record the number of cores in the current system. | |
| 57 const char kRapporNumCoresRangeFlagsField[] = "num_cores_range"; | |
| 58 | |
| 59 // The name of the flags field of the Rappor metrics in which we'll store the | |
| 60 // CPU / memory usage ranges. | |
| 61 const char kRapporUsageRangeFlagsField[] = "usage_range"; | |
| 62 | |
| 63 // The names of UMA histograms to report the CPU and memory usages of both the | |
| 64 // browser and GPU processes. | |
| 65 const char kBrowserProcessCpuUsageHistogramName[] = | |
| 66 "ResourceReporter.BrowserProcess.CpuUsage"; | |
| 67 const char kBrowserProcessMemoryUsageHistogramName[] = | |
| 68 "ResourceReporter.BrowserProcess.MemoryUsage"; | |
| 69 const char kGpuProcessCpuUsageHistogramName[] = | |
| 70 "ResourceReporter.GpuProcess.CpuUsage"; | |
| 71 const char kGpuProcessMemoryUsageHistogramName[] = | |
| 72 "ResourceReporter.GpuProcess.MemoryUsage"; | |
| 73 | |
| 74 } // namespace | |
| 75 | |
| 76 ResourceReporter::~ResourceReporter() { | |
| 77 } | |
| 78 | |
| 79 // static | |
| 80 ResourceReporter* ResourceReporter::GetInstance() { | |
| 81 return base::Singleton<ResourceReporter>::get(); | |
| 82 } | |
| 83 | |
| 84 // static | |
| 85 void ResourceReporter::StartObservingMetricsService() { | |
| 86 auto metrics_service = g_browser_process->metrics_service(); | |
| 87 DCHECK(metrics_service); | |
| 88 metrics_service->AddObserver(ResourceReporter::GetInstance()); | |
| 89 } | |
| 90 | |
| 91 // static | |
| 92 void ResourceReporter::StopObservingMetricsService() { | |
| 93 auto metrics_service = g_browser_process->metrics_service(); | |
| 94 DCHECK(metrics_service); | |
| 95 metrics_service->RemoveObserver(ResourceReporter::GetInstance()); | |
| 96 } | |
| 97 | |
| 98 void ResourceReporter::OnTaskAdded(task_management::TaskId id) { | |
| 99 // Ignore this event. | |
| 100 } | |
| 101 | |
| 102 void ResourceReporter::OnTaskToBeRemoved(task_management::TaskId id) { | |
| 103 auto itr = task_records_.find(id); | |
| 104 if (itr == task_records_.end()) | |
| 105 return; | |
| 106 | |
| 107 // Must be erased from the sorted set first. | |
| 108 // Note: this could mean that the sorted records are now less than | |
| 109 // |kTopConsumerCount| with other records in |task_records_| that can be | |
| 110 // added now. That's ok, we ignore this case. | |
| 111 task_records_by_cpu_.erase(itr->second.get()); | |
| 112 task_records_by_memory_.erase(itr->second.get()); | |
| 113 | |
| 114 task_records_.erase(itr); | |
| 115 } | |
| 116 | |
| 117 void ResourceReporter::OnTasksRefreshed( | |
| 118 const task_management::TaskIdList& task_ids) { | |
| 119 task_records_by_cpu_.clear(); | |
| 120 task_records_by_memory_.clear(); | |
| 121 | |
| 122 for (const auto& id : task_ids) { | |
| 123 const double cpu_usage = observed_task_manager()->GetCpuUsage(id); | |
| 124 const int64_t memory_usage = | |
| 125 observed_task_manager()->GetPhysicalMemoryUsage(id); | |
| 126 | |
| 127 // Browser and GPU processes are reported later using UMA histograms as they | |
| 128 // don't have any privacy issues. | |
| 129 const auto task_type = observed_task_manager()->GetType(id); | |
| 130 switch (task_type) { | |
| 131 case task_management::Task::BROWSER: | |
| 132 last_browser_process_cpu_ = cpu_usage; | |
| 133 last_browser_process_memory_ = memory_usage != -1 ? memory_usage : 0; | |
| 134 break; | |
| 135 | |
| 136 case task_management::Task::GPU: | |
| 137 last_gpu_process_cpu_ = cpu_usage; | |
| 138 last_gpu_process_memory_ = memory_usage != -1 ? memory_usage : 0; | |
| 139 break; | |
| 140 | |
| 141 default: | |
| 142 // Other tasks types will be reported using Rappor. | |
| 143 TaskRecord* task_data = nullptr; | |
| 144 auto itr = task_records_.find(id); | |
| 145 if (itr == task_records_.end()) { | |
| 146 task_data = new TaskRecord(id); | |
| 147 task_records_[id] = make_scoped_ptr(task_data); | |
| 148 } else { | |
| 149 task_data = itr->second.get(); | |
| 150 } | |
| 151 | |
| 152 DCHECK_EQ(task_data->id, id); | |
| 153 task_data->rappor_sample = | |
| 154 observed_task_manager()->GetRapporSampleName(id); | |
| 155 task_data->cpu = cpu_usage; | |
| 156 task_data->memory = memory_usage; | |
| 157 task_data->is_background = | |
| 158 observed_task_manager()->IsTaskOnBackgroundedProcess(id); | |
| 159 | |
| 160 if (task_records_by_cpu_.size() < kTopConsumersCount && | |
| 161 task_data->cpu > 0) { | |
| 162 task_records_by_cpu_.insert(task_data); | |
| 163 } | |
| 164 | |
| 165 if (task_records_by_memory_.size() < kTopConsumersCount && | |
| 166 task_data->memory > 0) { | |
| 167 task_records_by_memory_.insert(task_data); | |
|
ncarter (slow)
2015/11/10 23:29:56
It looks like this logic means we just count the f
afakhry
2015/11/12 00:21:27
Thank you so much for catching that nasty bug! Ind
| |
| 168 } | |
| 169 } | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 void ResourceReporter::OnMetricsServiceStart() { | |
| 174 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 175 | |
| 176 StartMonitoring(); | |
| 177 } | |
| 178 | |
| 179 void ResourceReporter::OnMetricsServiceStop() { | |
| 180 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 181 | |
| 182 StopMonitoring(); | |
| 183 } | |
| 184 | |
| 185 // static | |
| 186 const size_t ResourceReporter::kTopConsumersCount = 10U; | |
| 187 | |
| 188 ResourceReporter::ResourceReporter() | |
| 189 : TaskManagerObserver(base::TimeDelta::FromSeconds(kRefreshIntervalSeconds), | |
| 190 task_management::REFRESH_TYPE_CPU | | |
| 191 task_management::REFRESH_TYPE_MEMORY), | |
| 192 system_cpu_cores_range_(GetCurrentSystemCpuCoresRange()), | |
| 193 is_monitoring_(false) { | |
| 194 } | |
| 195 | |
| 196 // static | |
| 197 scoped_ptr<rappor::Sample> ResourceReporter::CreateRapporSample( | |
| 198 rappor::RapporService* rappor_service, | |
| 199 const ResourceReporter::TaskRecord& task_record) { | |
| 200 scoped_ptr<rappor::Sample> sample(rappor_service->CreateSample( | |
| 201 rappor::UMA_RAPPOR_TYPE)); | |
| 202 sample->SetStringField(kRapporTaskStringField, task_record.rappor_sample); | |
| 203 sample->SetFlagsField(kRapporPriorityFlagsField, | |
| 204 task_record.is_background ? BACKGROUND : FOREGROUND, | |
| 205 PRIORITIES_NUM); | |
| 206 return sample.Pass(); | |
| 207 } | |
| 208 | |
| 209 // static | |
| 210 ResourceReporter::CpuUsageRange | |
| 211 ResourceReporter::GetCpuUsageRange(double cpu) { | |
| 212 if (cpu > 60.0) | |
| 213 return RANGE_ABOVE_60_PERCENT; | |
| 214 if (cpu > 30.0) | |
| 215 return RANGE_30_TO_60_PERCENT; | |
| 216 if (cpu > 10.0) | |
| 217 return RANGE_10_TO_30_PERCENT; | |
| 218 | |
| 219 return RANGE_0_TO_10_PERCENT; | |
| 220 } | |
| 221 | |
| 222 // static | |
| 223 ResourceReporter::MemoryUsageRange | |
| 224 ResourceReporter::GetMemoryUsageRange(int64_t memory_in_bytes) { | |
| 225 if (memory_in_bytes > kMemory1GB) | |
| 226 return RANGE_ABOVE_1_GB; | |
| 227 if (memory_in_bytes > kMemory800MB) | |
| 228 return RANGE_800_TO_1_GB; | |
| 229 if (memory_in_bytes > kMemory600MB) | |
| 230 return RANGE_600_TO_800_MB; | |
| 231 if (memory_in_bytes > kMemory400MB) | |
| 232 return RANGE_400_TO_600_MB; | |
| 233 if (memory_in_bytes > kMemory200MB) | |
| 234 return RANGE_200_TO_400_MB; | |
| 235 | |
| 236 return RANGE_0_TO_200_MB; | |
| 237 } | |
| 238 | |
| 239 // static | |
| 240 ResourceReporter::CpuCoresNumberRange | |
| 241 ResourceReporter::GetCurrentSystemCpuCoresRange() { | |
| 242 const int cpus = base::SysInfo::NumberOfProcessors(); | |
| 243 | |
| 244 if (cpus > 16) | |
| 245 return RANGE_CORES_ABOVE_16_CORES; | |
| 246 if (cpus > 8) | |
| 247 return RANGE_CORES_8_TO_16_CORES; | |
| 248 if (cpus > 4) | |
| 249 return RANGE_CORES_4_TO_8_CORES; | |
| 250 if (cpus > 2) | |
| 251 return RANGE_CORES_2_TO_4_CORES; | |
| 252 if (cpus >= 1) | |
| 253 return RANGE_CORES_1_TO_2_CORES; | |
| 254 | |
| 255 NOTREACHED(); | |
| 256 return RANGE_CORES_0_CORES; | |
| 257 } | |
| 258 | |
| 259 void ResourceReporter::StartMonitoring() { | |
| 260 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 261 | |
| 262 if (is_monitoring_) | |
| 263 return; | |
| 264 | |
| 265 is_monitoring_ = true; | |
| 266 task_management::TaskManagerInterface::GetTaskManager()->AddObserver(this); | |
| 267 memory_pressure_listener_.reset(new base::MemoryPressureListener( | |
| 268 base::Bind(&ResourceReporter::OnMemoryPressure, base::Unretained(this)))); | |
| 269 } | |
| 270 | |
| 271 void ResourceReporter::StopMonitoring() { | |
| 272 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 273 | |
| 274 if (!is_monitoring_) | |
| 275 return; | |
| 276 | |
| 277 is_monitoring_ = false; | |
| 278 memory_pressure_listener_.reset(); | |
| 279 task_management::TaskManagerInterface::GetTaskManager()->RemoveObserver(this); | |
| 280 } | |
| 281 | |
| 282 void ResourceReporter::OnMemoryPressure( | |
| 283 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | |
| 284 // TODO(afakhry): Double check if we will ever receive a notification when | |
| 285 // we don't have memory pressure at all. The target is to report only once | |
| 286 // per each level value. | |
| 287 if (memory_pressure_level >= | |
| 288 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE && | |
| 289 memory_pressure_level > previous_memory_pressure_level_) { | |
| 290 // Report browser and GPU processes usage using UMA histograms. | |
| 291 UMA_HISTOGRAM_ENUMERATION(kBrowserProcessCpuUsageHistogramName, | |
|
ncarter (slow)
2015/11/10 23:29:56
Don't use a kConstant for the histogram name, just
afakhry
2015/11/12 00:21:28
That's weird! I didn't fail any presubmit checks a
ncarter (slow)
2015/11/12 19:33:27
Ah, I looked at it more closely. The regex include
afakhry
2015/11/13 19:41:13
Done.
| |
| 292 GetCpuUsageRange(last_browser_process_cpu_), | |
| 293 CPU_RANGES_NUM); | |
| 294 UMA_HISTOGRAM_ENUMERATION(kBrowserProcessMemoryUsageHistogramName, | |
| 295 GetMemoryUsageRange(last_browser_process_memory_), | |
| 296 MEMORY_RANGES_NUM); | |
| 297 UMA_HISTOGRAM_ENUMERATION(kGpuProcessCpuUsageHistogramName, | |
| 298 GetCpuUsageRange(last_gpu_process_cpu_), | |
| 299 CPU_RANGES_NUM); | |
| 300 UMA_HISTOGRAM_ENUMERATION(kGpuProcessMemoryUsageHistogramName, | |
| 301 GetMemoryUsageRange(last_gpu_process_memory_), | |
| 302 MEMORY_RANGES_NUM); | |
| 303 | |
| 304 // For the rest of tasks, report them using Rappor. | |
| 305 auto rappor_service = g_browser_process->rappor_service(); | |
| 306 | |
| 307 for (const auto& task_data : task_records_by_cpu_) { | |
| 308 scoped_ptr<rappor::Sample> sample(CreateRapporSample(rappor_service, | |
| 309 *task_data)); | |
| 310 sample->SetFlagsField(kRapporNumCoresRangeFlagsField, | |
| 311 system_cpu_cores_range_, | |
| 312 CORES_RANGES_NUM); | |
| 313 sample->SetFlagsField(kRapporUsageRangeFlagsField, | |
| 314 GetCpuUsageRange(task_data->cpu), | |
| 315 CPU_RANGES_NUM); | |
| 316 rappor_service->RecordSampleObj(kCpuRapporMetric, sample.Pass()); | |
|
ncarter (slow)
2015/11/10 23:29:56
Doesn't this oversample processes with multiple ta
afakhry
2015/11/12 00:21:27
I'm not sure I understand what you need to know, b
ncarter (slow)
2015/11/12 19:33:27
By "oversampling", I mean that now a 100MB process
afakhry
2015/11/13 19:41:12
The top 10 limit is merely for us to limit our att
ncarter (slow)
2015/11/13 22:44:32
The current strategy still seems a little heuristi
| |
| 317 } | |
| 318 | |
| 319 for (const auto& task_data : task_records_by_memory_) { | |
| 320 scoped_ptr<rappor::Sample> sample(CreateRapporSample(rappor_service, | |
| 321 *task_data)); | |
| 322 sample->SetFlagsField(kRapporUsageRangeFlagsField, | |
| 323 GetMemoryUsageRange(task_data->memory), | |
| 324 MEMORY_RANGES_NUM); | |
| 325 rappor_service->RecordSampleObj(kMemoryRapporMetric, sample.Pass()); | |
|
ncarter (slow)
2015/11/10 23:29:56
Same question as above: doesn't this oversample gr
afakhry
2015/11/12 00:21:27
See above.
| |
| 326 } | |
| 327 } | |
| 328 | |
| 329 previous_memory_pressure_level_ = memory_pressure_level; | |
| 330 } | |
| 331 | |
| 332 } // namespace chromeos | |
| OLD | NEW |