Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/resource_reporter/resource_reporter.h" | 5 #include "chrome/browser/chromeos/resource_reporter/resource_reporter.h" |
| 6 | 6 |
| 7 #include <cstdint> | 7 #include <cstdint> |
| 8 #include <queue> | 8 #include <queue> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/memory/memory_coordinator_client_registry.h" | |
| 12 #include "base/memory/memory_pressure_monitor.h" | 13 #include "base/memory/memory_pressure_monitor.h" |
| 13 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 14 #include "base/rand_util.h" | 15 #include "base/rand_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/sys_info.h" | 17 #include "base/sys_info.h" |
| 17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 18 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
| 19 #include "chrome/browser/task_manager/task_manager_interface.h" | 20 #include "chrome/browser/task_manager/task_manager_interface.h" |
| 20 #include "components/rappor/rappor_service.h" | 21 #include "components/rappor/rappor_service.h" |
| 21 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 113 | 114 |
| 114 if (is_monitoring_) | 115 if (is_monitoring_) |
| 115 return; | 116 return; |
| 116 | 117 |
| 117 task_manager_to_observe_ = task_manager_to_observe; | 118 task_manager_to_observe_ = task_manager_to_observe; |
| 118 DCHECK(task_manager_to_observe_); | 119 DCHECK(task_manager_to_observe_); |
| 119 is_monitoring_ = true; | 120 is_monitoring_ = true; |
| 120 memory_pressure_listener_.reset(new base::MemoryPressureListener( | 121 memory_pressure_listener_.reset(new base::MemoryPressureListener( |
| 121 base::Bind(&ResourceReporter::OnMemoryPressure, base::Unretained(this)))); | 122 base::Bind(&ResourceReporter::OnMemoryPressure, base::Unretained(this)))); |
| 123 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); | |
| 122 } | 124 } |
| 123 | 125 |
| 124 void ResourceReporter::StopMonitoring() { | 126 void ResourceReporter::StopMonitoring() { |
| 125 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 126 | 128 |
| 127 if (!is_monitoring_) | 129 if (!is_monitoring_) |
| 128 return; | 130 return; |
| 129 | 131 |
| 130 // We might be shutting down right after a critical memory pressure event, and | 132 // We might be shutting down right after a critical memory pressure event, and |
| 131 // before we get an update from the task manager with all background | 133 // before we get an update from the task manager with all background |
| 132 // calculations refreshed. In this case we must unregister from the task | 134 // calculations refreshed. In this case we must unregister from the task |
| 133 // manager here. | 135 // manager here. |
| 134 if (observed_task_manager()) | 136 if (observed_task_manager()) |
| 135 observed_task_manager()->RemoveObserver(this); | 137 observed_task_manager()->RemoveObserver(this); |
|
afakhry
2016/10/27 16:16:20
Nit: Since we now have StopRecordingCurrentState()
hajimehoshi
2016/10/31 06:50:51
Done.
| |
| 136 | 138 |
| 137 is_monitoring_ = false; | 139 is_monitoring_ = false; |
| 138 memory_pressure_listener_.reset(); | 140 memory_pressure_listener_.reset(); |
| 141 | |
| 142 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(this); | |
| 139 } | 143 } |
| 140 | 144 |
| 141 void ResourceReporter::OnTasksRefreshedWithBackgroundCalculations( | 145 void ResourceReporter::OnTasksRefreshedWithBackgroundCalculations( |
| 142 const task_manager::TaskIdList& task_ids) { | 146 const task_manager::TaskIdList& task_ids) { |
| 143 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 144 | 148 |
| 145 task_records_.clear(); | 149 task_records_.clear(); |
| 146 task_records_.reserve(task_ids.size()); | 150 task_records_.reserve(task_ids.size()); |
| 147 | 151 |
| 148 for (const auto& id : task_ids) { | 152 for (const auto& id : task_ids) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 184 } | 188 } |
| 185 | 189 |
| 186 // Now that we got the data, we don't need the task manager anymore. | 190 // Now that we got the data, we don't need the task manager anymore. |
| 187 if (base::MemoryPressureMonitor::Get()->GetCurrentPressureLevel() != | 191 if (base::MemoryPressureMonitor::Get()->GetCurrentPressureLevel() != |
| 188 MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL || | 192 MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL || |
| 189 !task_records_.empty()) { | 193 !task_records_.empty()) { |
| 190 // The memory pressure events are emitted once per second. In order to avoid | 194 // The memory pressure events are emitted once per second. In order to avoid |
| 191 // unsubscribing and then resubscribing to the task manager again on the | 195 // unsubscribing and then resubscribing to the task manager again on the |
| 192 // next event, we keep listening to the task manager as long as the memory | 196 // next event, we keep listening to the task manager as long as the memory |
| 193 // pressure level is critical AND we couldn't find any violators yet. | 197 // pressure level is critical AND we couldn't find any violators yet. |
| 194 observed_task_manager()->RemoveObserver(this); | 198 observed_task_manager()->RemoveObserver(this); |
|
afakhry
2016/10/27 16:16:20
Nit: Same reason as above. Let's use StopRecording
hajimehoshi
2016/10/31 06:50:51
Done.
| |
| 195 } | 199 } |
| 196 | 200 |
| 197 // Schedule reporting the samples. | 201 // Schedule reporting the samples. |
| 198 base::ThreadTaskRunnerHandle::Get()->PostTask( | 202 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 199 FROM_HERE, | 203 FROM_HERE, |
| 200 base::Bind(&ResourceReporter::ReportSamples, base::Unretained(this))); | 204 base::Bind(&ResourceReporter::ReportSamples, base::Unretained(this))); |
| 201 } | 205 } |
| 202 | 206 |
| 203 // static | 207 // static |
| 204 const double ResourceReporter::kTaskCpuThresholdForReporting = 70.0; | 208 const double ResourceReporter::kTaskCpuThresholdForReporting = 70.0; |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 384 GET_ENUM_VAL(MemoryUsageRange::NUM_RANGES)); | 388 GET_ENUM_VAL(MemoryUsageRange::NUM_RANGES)); |
| 385 rappor_service->RecordSampleObj(kMemoryRapporMetric, | 389 rappor_service->RecordSampleObj(kMemoryRapporMetric, |
| 386 std::move(memory_sample)); | 390 std::move(memory_sample)); |
| 387 } | 391 } |
| 388 } | 392 } |
| 389 | 393 |
| 390 void ResourceReporter::OnMemoryPressure( | 394 void ResourceReporter::OnMemoryPressure( |
| 391 MemoryPressureLevel memory_pressure_level) { | 395 MemoryPressureLevel memory_pressure_level) { |
| 392 if (memory_pressure_level == | 396 if (memory_pressure_level == |
| 393 MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL) { | 397 MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL) { |
| 394 // If we are already listening to the task manager, then we're waiting for | 398 StartRecordingCurrentState(); |
| 395 // a refresh event. | |
| 396 if (observed_task_manager()) | |
| 397 return; | |
| 398 | |
| 399 // We only record Rappor samples only if it's the first ever critical memory | |
| 400 // pressure event we receive, or it has been more than | |
| 401 // |kMinimumTimeBetweenReportsInMs| since the last time we recorded samples. | |
| 402 if (g_browser_process->local_state()) { | |
| 403 const base::Time now = base::Time::NowFromSystemTime(); | |
| 404 const base::Time last_rappor_report_time = | |
| 405 base::Time::FromDoubleT(g_browser_process->local_state()->GetDouble( | |
| 406 kLastRapporReportTimeKey)); | |
| 407 const base::TimeDelta delta_since_last_report = | |
| 408 now >= last_rappor_report_time ? now - last_rappor_report_time | |
| 409 : base::TimeDelta::Max(); | |
| 410 | |
| 411 if (delta_since_last_report < kMinimumTimeBetweenReports) | |
| 412 return; | |
| 413 } | |
| 414 | |
| 415 // Start listening to the task manager and wait for the first refresh event | |
| 416 // with background calculations completion. | |
| 417 task_manager_to_observe_->AddObserver(this); | |
| 418 } else { | 399 } else { |
| 419 // If we are still listening to the task manager from an earlier critical | 400 StopRecordingCurrentState(); |
| 420 // memory pressure level, we need to stop listening to it. | |
| 421 if (observed_task_manager()) | |
| 422 observed_task_manager()->RemoveObserver(this); | |
| 423 } | 401 } |
| 424 } | 402 } |
| 425 | 403 |
| 404 void ResourceReporter::StartRecordingCurrentState() { | |
| 405 // If we are already listening to the task manager, then we're waiting for | |
| 406 // a refresh event. | |
| 407 if (observed_task_manager()) | |
| 408 return; | |
| 409 | |
| 410 // We only record Rappor samples only if it's the first ever critical memory | |
| 411 // pressure event we receive, or it has been more than | |
| 412 // |kMinimumTimeBetweenReportsInMs| since the last time we recorded samples. | |
| 413 if (g_browser_process->local_state()) { | |
| 414 const base::Time now = base::Time::NowFromSystemTime(); | |
| 415 const base::Time last_rappor_report_time = base::Time::FromDoubleT( | |
| 416 g_browser_process->local_state()->GetDouble(kLastRapporReportTimeKey)); | |
| 417 const base::TimeDelta delta_since_last_report = | |
| 418 now >= last_rappor_report_time ? now - last_rappor_report_time | |
| 419 : base::TimeDelta::Max(); | |
| 420 | |
| 421 if (delta_since_last_report < kMinimumTimeBetweenReports) | |
| 422 return; | |
| 423 } | |
| 424 | |
| 425 // Start listening to the task manager and wait for the first refresh event | |
| 426 // with background calculations completion. | |
| 427 task_manager_to_observe_->AddObserver(this); | |
| 428 } | |
| 429 | |
| 430 void ResourceReporter::StopRecordingCurrentState() { | |
| 431 // If we are still listening to the task manager from an earlier critical | |
| 432 // memory pressure level, we need to stop listening to it. | |
| 433 if (observed_task_manager()) | |
| 434 observed_task_manager()->RemoveObserver(this); | |
| 435 } | |
| 436 | |
| 437 void ResourceReporter::OnMemoryStateChange(base::MemoryState state) { | |
| 438 switch (state) { | |
| 439 case base::MemoryState::NORMAL: | |
| 440 StopRecordingCurrentState(); | |
| 441 break; | |
| 442 case base::MemoryState::THROTTLED: | |
| 443 StartRecordingCurrentState(); | |
| 444 break; | |
| 445 case base::MemoryState::SUSPENDED: | |
| 446 // Note: Not supported at present. Fall through. | |
| 447 case base::MemoryState::UNKNOWN: | |
| 448 NOTREACHED(); | |
| 449 break; | |
| 450 } | |
| 451 } | |
| 452 | |
| 426 } // namespace chromeos | 453 } // namespace chromeos |
| OLD | NEW |