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 |
| 11 #include "base/bind.h" |
| 12 #include "base/memory/singleton.h" |
| 13 #include "base/rand_util.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 // Currently set to be one day. |
| 66 const int kMinimumTimeBetweenReportsInMS = 1 * 24 * 60 * 60 * 1000; |
| 67 |
| 68 // A functor to sort the TaskRecords by their |cpu|. |
| 69 struct TaskRecordByCpuSorter { |
| 70 bool operator()(ResourceReporter::TaskRecord* const& lhs, |
| 71 ResourceReporter::TaskRecord* const& rhs) const { |
| 72 if (lhs->cpu == rhs->cpu) |
| 73 return lhs->id < rhs->id; |
| 74 return lhs->cpu < rhs->cpu; |
| 75 } |
| 76 }; |
| 77 |
| 78 // A functor to sort the TaskRecords by their |memory|. |
| 79 struct TaskRecordByMemorySorter { |
| 80 bool operator()(ResourceReporter::TaskRecord* const& lhs, |
| 81 ResourceReporter::TaskRecord* const& rhs) const { |
| 82 if (lhs->memory == rhs->memory) |
| 83 return lhs->id < rhs->id; |
| 84 return lhs->memory < rhs->memory; |
| 85 } |
| 86 }; |
| 87 |
| 88 } // namespace |
| 89 |
| 90 ResourceReporter::TaskRecord::TaskRecord(task_management::TaskId the_id) |
| 91 : id(the_id), cpu(0.0), memory(0), is_background(false) { |
| 92 } |
| 93 |
| 94 ResourceReporter::TaskRecord::TaskRecord(task_management::TaskId the_id, |
| 95 const std::string& task_name, |
| 96 double the_cpu, |
| 97 int64_t the_memory, |
| 98 bool background) |
| 99 : id(the_id), |
| 100 task_name_for_rappor(task_name), |
| 101 cpu(the_cpu), |
| 102 memory(the_memory), |
| 103 is_background(background) { |
| 104 } |
| 105 |
| 106 ResourceReporter::~ResourceReporter() { |
| 107 } |
| 108 |
| 109 // static |
| 110 ResourceReporter* ResourceReporter::GetInstance() { |
| 111 return base::Singleton<ResourceReporter>::get(); |
| 112 } |
| 113 |
| 114 void ResourceReporter::StartMonitoring() { |
| 115 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 116 |
| 117 if (is_monitoring_) |
| 118 return; |
| 119 |
| 120 is_monitoring_ = true; |
| 121 task_management::TaskManagerInterface::GetTaskManager()->AddObserver(this); |
| 122 memory_pressure_listener_.reset(new base::MemoryPressureListener( |
| 123 base::Bind(&ResourceReporter::OnMemoryPressure, base::Unretained(this)))); |
| 124 } |
| 125 |
| 126 void ResourceReporter::StopMonitoring() { |
| 127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 128 |
| 129 if (!is_monitoring_) |
| 130 return; |
| 131 |
| 132 is_monitoring_ = false; |
| 133 memory_pressure_listener_.reset(); |
| 134 task_management::TaskManagerInterface::GetTaskManager()->RemoveObserver(this); |
| 135 } |
| 136 |
| 137 void ResourceReporter::OnTaskAdded(task_management::TaskId id) { |
| 138 // Ignore this event. |
| 139 } |
| 140 |
| 141 void ResourceReporter::OnTaskToBeRemoved(task_management::TaskId id) { |
| 142 auto itr = task_records_.find(id); |
| 143 if (itr == task_records_.end()) |
| 144 return; |
| 145 |
| 146 // Must be erased from the sorted set first. |
| 147 // Note: this could mean that the sorted records are now less than |
| 148 // |kTopConsumerCount| with other records in |task_records_| that can be |
| 149 // added now. That's ok, we ignore this case. |
| 150 auto cpu_itr = std::find(task_records_by_cpu_.begin(), |
| 151 task_records_by_cpu_.end(), |
| 152 itr->second.get()); |
| 153 if (cpu_itr != task_records_by_cpu_.end()) |
| 154 task_records_by_cpu_.erase(cpu_itr); |
| 155 |
| 156 auto memory_itr = std::find(task_records_by_memory_.begin(), |
| 157 task_records_by_memory_.end(), |
| 158 itr->second.get()); |
| 159 if (memory_itr != task_records_by_memory_.end()) |
| 160 task_records_by_memory_.erase(memory_itr); |
| 161 |
| 162 task_records_.erase(itr); |
| 163 } |
| 164 |
| 165 void ResourceReporter::OnTasksRefreshed( |
| 166 const task_management::TaskIdList& task_ids) { |
| 167 // A priority queue to sort the task records by their |cpu|. Greatest |cpu| |
| 168 // first. |
| 169 std::priority_queue<TaskRecord*, |
| 170 std::vector<TaskRecord*>, |
| 171 TaskRecordByCpuSorter> records_by_cpu_queue; |
| 172 // A priority queue to sort the task records by their |memory|. Greatest |
| 173 // |memory| first. |
| 174 std::priority_queue<TaskRecord*, |
| 175 std::vector<TaskRecord*>, |
| 176 TaskRecordByMemorySorter> records_by_memory_queue; |
| 177 task_records_by_cpu_.clear(); |
| 178 task_records_by_cpu_.reserve(kTopConsumersCount); |
| 179 task_records_by_memory_.clear(); |
| 180 task_records_by_memory_.reserve(kTopConsumersCount); |
| 181 |
| 182 for (const auto& id : task_ids) { |
| 183 const double cpu_usage = observed_task_manager()->GetCpuUsage(id); |
| 184 const int64_t memory_usage = |
| 185 observed_task_manager()->GetPhysicalMemoryUsage(id); |
| 186 |
| 187 // Browser and GPU processes are reported later using UMA histograms as they |
| 188 // don't have any privacy issues. |
| 189 const auto task_type = observed_task_manager()->GetType(id); |
| 190 switch (task_type) { |
| 191 case task_management::Task::UNKNOWN: |
| 192 case task_management::Task::ZYGOTE: |
| 193 break; |
| 194 |
| 195 case task_management::Task::BROWSER: |
| 196 last_browser_process_cpu_ = cpu_usage; |
| 197 last_browser_process_memory_ = memory_usage != -1 ? memory_usage : 0; |
| 198 break; |
| 199 |
| 200 case task_management::Task::GPU: |
| 201 last_gpu_process_cpu_ = cpu_usage; |
| 202 last_gpu_process_memory_ = memory_usage != -1 ? memory_usage : 0; |
| 203 break; |
| 204 |
| 205 default: |
| 206 // Other tasks types will be reported using Rappor. |
| 207 TaskRecord* task_data = nullptr; |
| 208 auto itr = task_records_.find(id); |
| 209 if (itr == task_records_.end()) { |
| 210 task_data = new TaskRecord(id); |
| 211 task_records_[id] = make_scoped_ptr(task_data); |
| 212 } else { |
| 213 task_data = itr->second.get(); |
| 214 } |
| 215 |
| 216 DCHECK_EQ(task_data->id, id); |
| 217 task_data->task_name_for_rappor = |
| 218 observed_task_manager()->GetTaskNameForRappor(id); |
| 219 task_data->cpu = cpu_usage; |
| 220 task_data->memory = memory_usage; |
| 221 task_data->is_background = |
| 222 observed_task_manager()->IsTaskOnBackgroundedProcess(id); |
| 223 |
| 224 // Push only valid or useful data to both priority queues. They might |
| 225 // end up having more records than |kTopConsumerCount|, that's fine. |
| 226 // We'll take care of that next. |
| 227 if (task_data->cpu > 0) |
| 228 records_by_cpu_queue.push(task_data); |
| 229 if (task_data->memory > 0) |
| 230 records_by_memory_queue.push(task_data); |
| 231 } |
| 232 } |
| 233 |
| 234 // Sort the |kTopConsumersCount| task records by their CPU and memory usage. |
| 235 while (!records_by_cpu_queue.empty() && |
| 236 task_records_by_cpu_.size() < kTopConsumersCount) { |
| 237 task_records_by_cpu_.push_back(records_by_cpu_queue.top()); |
| 238 records_by_cpu_queue.pop(); |
| 239 } |
| 240 |
| 241 while (!records_by_memory_queue.empty() && |
| 242 task_records_by_memory_.size() < kTopConsumersCount) { |
| 243 task_records_by_memory_.push_back(records_by_memory_queue.top()); |
| 244 records_by_memory_queue.pop(); |
| 245 } |
| 246 } |
| 247 |
| 248 // static |
| 249 const size_t ResourceReporter::kTopConsumersCount = 10U; |
| 250 |
| 251 ResourceReporter::ResourceReporter() |
| 252 : TaskManagerObserver(base::TimeDelta::FromSeconds(kRefreshIntervalSeconds), |
| 253 task_management::REFRESH_TYPE_CPU | |
| 254 task_management::REFRESH_TYPE_MEMORY | |
| 255 task_management::REFRESH_TYPE_PRIORITY), |
| 256 system_cpu_cores_range_(GetCurrentSystemCpuCoresRange()) { |
| 257 } |
| 258 |
| 259 // static |
| 260 scoped_ptr<rappor::Sample> ResourceReporter::CreateRapporSample( |
| 261 rappor::RapporService* rappor_service, |
| 262 const ResourceReporter::TaskRecord& task_record) { |
| 263 scoped_ptr<rappor::Sample> sample(rappor_service->CreateSample( |
| 264 rappor::UMA_RAPPOR_TYPE)); |
| 265 sample->SetStringField(kRapporTaskStringField, |
| 266 task_record.task_name_for_rappor); |
| 267 sample->SetFlagsField(kRapporPriorityFlagsField, |
| 268 task_record.is_background ? BACKGROUND : FOREGROUND, |
| 269 PRIORITIES_NUM); |
| 270 return sample.Pass(); |
| 271 } |
| 272 |
| 273 // static |
| 274 ResourceReporter::CpuUsageRange |
| 275 ResourceReporter::GetCpuUsageRange(double cpu) { |
| 276 if (cpu > 60.0) |
| 277 return RANGE_ABOVE_60_PERCENT; |
| 278 if (cpu > 30.0) |
| 279 return RANGE_30_TO_60_PERCENT; |
| 280 if (cpu > 10.0) |
| 281 return RANGE_10_TO_30_PERCENT; |
| 282 |
| 283 return RANGE_0_TO_10_PERCENT; |
| 284 } |
| 285 |
| 286 // static |
| 287 ResourceReporter::MemoryUsageRange |
| 288 ResourceReporter::GetMemoryUsageRange(int64_t memory_in_bytes) { |
| 289 if (memory_in_bytes > kMemory1GB) |
| 290 return RANGE_ABOVE_1_GB; |
| 291 if (memory_in_bytes > kMemory800MB) |
| 292 return RANGE_800_TO_1_GB; |
| 293 if (memory_in_bytes > kMemory600MB) |
| 294 return RANGE_600_TO_800_MB; |
| 295 if (memory_in_bytes > kMemory400MB) |
| 296 return RANGE_400_TO_600_MB; |
| 297 if (memory_in_bytes > kMemory200MB) |
| 298 return RANGE_200_TO_400_MB; |
| 299 |
| 300 return RANGE_0_TO_200_MB; |
| 301 } |
| 302 |
| 303 // static |
| 304 ResourceReporter::CpuCoresNumberRange |
| 305 ResourceReporter::GetCurrentSystemCpuCoresRange() { |
| 306 const int cpus = base::SysInfo::NumberOfProcessors(); |
| 307 |
| 308 if (cpus > 16) |
| 309 return RANGE_CORES_ABOVE_16_CORES; |
| 310 if (cpus > 8) |
| 311 return RANGE_CORES_9_TO_16_CORES; |
| 312 if (cpus > 4) |
| 313 return RANGE_CORES_5_TO_8_CORES; |
| 314 if (cpus > 2) |
| 315 return RANGE_CORES_3_TO_4_CORES; |
| 316 if (cpus == 2) |
| 317 return RANGE_CORES_2_CORES; |
| 318 if (cpus == 1) |
| 319 return RANGE_CORES_1_CORE; |
| 320 |
| 321 NOTREACHED(); |
| 322 return RANGE_CORES_NA; |
| 323 } |
| 324 |
| 325 bool ResourceReporter::ShouldRecordSamples() { |
| 326 if (is_first_memory_pressure_event_) { |
| 327 is_first_memory_pressure_event_ = false; |
| 328 return true; |
| 329 } |
| 330 |
| 331 return (base::TimeTicks::Now() - last_memory_pressure_event_time_) >= |
| 332 base::TimeDelta::FromMilliseconds(kMinimumTimeBetweenReportsInMS); |
| 333 } |
| 334 |
| 335 const ResourceReporter::TaskRecord* ResourceReporter::SampleTaskByCpu() const { |
| 336 TaskRecord* sampled_task = nullptr; |
| 337 double cpu_weights_sum = 0; |
| 338 for (const auto& task_data : task_records_by_cpu_) { |
| 339 if ((base::RandDouble() * (cpu_weights_sum + task_data->cpu)) >= |
| 340 cpu_weights_sum) { |
| 341 sampled_task = task_data; |
| 342 } |
| 343 cpu_weights_sum += task_data->cpu; |
| 344 } |
| 345 |
| 346 return sampled_task; |
| 347 } |
| 348 |
| 349 const ResourceReporter::TaskRecord* |
| 350 ResourceReporter::SampleTaskByMemory() const { |
| 351 TaskRecord* sampled_task = nullptr; |
| 352 int64_t memory_weights_sum = 0; |
| 353 for (const auto& task_data : task_records_by_memory_) { |
| 354 if ((base::RandDouble() * |
| 355 (memory_weights_sum + task_data->memory)) >= |
| 356 memory_weights_sum) { |
| 357 sampled_task = task_data; |
| 358 } |
| 359 memory_weights_sum += task_data->memory; |
| 360 } |
| 361 |
| 362 return sampled_task; |
| 363 } |
| 364 |
| 365 void ResourceReporter::OnMemoryPressure( |
| 366 MemoryPressureLevel memory_pressure_level) { |
| 367 if (memory_pressure_level >= |
| 368 MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE) { |
| 369 // Report browser and GPU processes usage using UMA histograms. |
| 370 UMA_HISTOGRAM_ENUMERATION("ResourceReporter.BrowserProcess.CpuUsage", |
| 371 GetCpuUsageRange(last_browser_process_cpu_), |
| 372 CPU_RANGES_NUM); |
| 373 UMA_HISTOGRAM_ENUMERATION("ResourceReporter.BrowserProcess.MemoryUsage", |
| 374 GetMemoryUsageRange(last_browser_process_memory_), |
| 375 MEMORY_RANGES_NUM); |
| 376 UMA_HISTOGRAM_ENUMERATION("ResourceReporter.GpuProcess.CpuUsage", |
| 377 GetCpuUsageRange(last_gpu_process_cpu_), |
| 378 CPU_RANGES_NUM); |
| 379 UMA_HISTOGRAM_ENUMERATION("ResourceReporter.GpuProcess.MemoryUsage", |
| 380 GetMemoryUsageRange(last_gpu_process_memory_), |
| 381 MEMORY_RANGES_NUM); |
| 382 |
| 383 // For the rest of tasks, report them using Rappor. |
| 384 auto rappor_service = g_browser_process->rappor_service(); |
| 385 if (!rappor_service) |
| 386 return; |
| 387 |
| 388 if (!ShouldRecordSamples()) |
| 389 return; |
| 390 last_memory_pressure_event_time_ = base::TimeTicks::Now(); |
| 391 |
| 392 // Use weighted random sampling to select a task to report in the CPU |
| 393 // metric. |
| 394 const TaskRecord* sampled_cpu_task = SampleTaskByCpu(); |
| 395 if (sampled_cpu_task) { |
| 396 scoped_ptr<rappor::Sample> cpu_sample( |
| 397 CreateRapporSample(rappor_service, *sampled_cpu_task)); |
| 398 cpu_sample->SetFlagsField(kRapporNumCoresRangeFlagsField, |
| 399 system_cpu_cores_range_, |
| 400 CORES_RANGES_NUM); |
| 401 cpu_sample->SetFlagsField(kRapporUsageRangeFlagsField, |
| 402 GetCpuUsageRange(sampled_cpu_task->cpu), |
| 403 CPU_RANGES_NUM); |
| 404 rappor_service->RecordSampleObj(kCpuRapporMetric, cpu_sample.Pass()); |
| 405 } |
| 406 |
| 407 // Use weighted random sampling to select a task to report in the memory |
| 408 // metric. |
| 409 const TaskRecord* sampled_memory_task = SampleTaskByMemory(); |
| 410 if (sampled_memory_task) { |
| 411 scoped_ptr<rappor::Sample> memory_sample( |
| 412 CreateRapporSample(rappor_service, *sampled_memory_task)); |
| 413 memory_sample->SetFlagsField( |
| 414 kRapporUsageRangeFlagsField, |
| 415 GetMemoryUsageRange(sampled_memory_task->memory), |
| 416 MEMORY_RANGES_NUM); |
| 417 rappor_service->RecordSampleObj(kMemoryRapporMetric, |
| 418 memory_sample.Pass()); |
| 419 } |
| 420 } |
| 421 } |
| 422 |
| 423 } // namespace chromeos |
OLD | NEW |