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

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

Issue 2388933003: Make ResourceReporter a client of memory coordinator (Closed)
Patch Set: (rebasing) Created 4 years, 2 months 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
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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 task_manager::TaskManagerInterface* task_manager_to_observe) { 112 task_manager::TaskManagerInterface* task_manager_to_observe) {
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))));
afakhry 2016/10/18 16:20:20 Now, I'm a bit confused! I thought we're moving to
hajimehoshi 2016/10/25 07:39:42 Temporarily MemoryCoordinatorClient and MemoryPres
122 } 123 }
123 124
124 void ResourceReporter::StopMonitoring() { 125 void ResourceReporter::StopMonitoring() {
125 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 126 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
126 127
127 if (!is_monitoring_) 128 if (!is_monitoring_)
128 return; 129 return;
129 130
130 // We might be shutting down right after a critical memory pressure event, and 131 // 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 132 // before we get an update from the task manager with all background
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 : TaskManagerObserver(base::TimeDelta::FromSeconds(kRefreshIntervalSeconds), 212 : TaskManagerObserver(base::TimeDelta::FromSeconds(kRefreshIntervalSeconds),
212 task_manager::REFRESH_TYPE_CPU | 213 task_manager::REFRESH_TYPE_CPU |
213 task_manager::REFRESH_TYPE_MEMORY | 214 task_manager::REFRESH_TYPE_MEMORY |
214 task_manager::REFRESH_TYPE_PRIORITY), 215 task_manager::REFRESH_TYPE_PRIORITY),
215 task_manager_to_observe_(nullptr), 216 task_manager_to_observe_(nullptr),
216 system_cpu_cores_range_(GetCurrentSystemCpuCoresRange()), 217 system_cpu_cores_range_(GetCurrentSystemCpuCoresRange()),
217 last_browser_process_cpu_(0.0), 218 last_browser_process_cpu_(0.0),
218 last_gpu_process_cpu_(0.0), 219 last_gpu_process_cpu_(0.0),
219 last_browser_process_memory_(0), 220 last_browser_process_memory_(0),
220 last_gpu_process_memory_(0), 221 last_gpu_process_memory_(0),
221 is_monitoring_(false) {} 222 is_monitoring_(false) {
223 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this);
afakhry 2016/10/18 16:20:20 I believe this line belongs to StartMonitoring().
hajimehoshi 2016/10/25 07:39:42 Done.
224 }
222 225
223 // static 226 // static
224 std::unique_ptr<rappor::Sample> ResourceReporter::CreateRapporSample( 227 std::unique_ptr<rappor::Sample> ResourceReporter::CreateRapporSample(
225 rappor::RapporService* rappor_service, 228 rappor::RapporService* rappor_service,
226 const ResourceReporter::TaskRecord& task_record) { 229 const ResourceReporter::TaskRecord& task_record) {
227 std::unique_ptr<rappor::Sample> sample( 230 std::unique_ptr<rappor::Sample> sample(
228 rappor_service->CreateSample(rappor::UMA_RAPPOR_TYPE)); 231 rappor_service->CreateSample(rappor::UMA_RAPPOR_TYPE));
229 sample->SetStringField(kRapporTaskStringField, 232 sample->SetStringField(kRapporTaskStringField,
230 task_record.task_name_for_rappor); 233 task_record.task_name_for_rappor);
231 sample->SetFlagsField(kRapporPriorityFlagsField, 234 sample->SetFlagsField(kRapporPriorityFlagsField,
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 GET_ENUM_VAL(MemoryUsageRange::NUM_RANGES)); 387 GET_ENUM_VAL(MemoryUsageRange::NUM_RANGES));
385 rappor_service->RecordSampleObj(kMemoryRapporMetric, 388 rappor_service->RecordSampleObj(kMemoryRapporMetric,
386 std::move(memory_sample)); 389 std::move(memory_sample));
387 } 390 }
388 } 391 }
389 392
390 void ResourceReporter::OnMemoryPressure( 393 void ResourceReporter::OnMemoryPressure(
391 MemoryPressureLevel memory_pressure_level) { 394 MemoryPressureLevel memory_pressure_level) {
392 if (memory_pressure_level == 395 if (memory_pressure_level ==
393 MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL) { 396 MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL) {
394 // If we are already listening to the task manager, then we're waiting for 397 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 { 398 } else {
419 // If we are still listening to the task manager from an earlier critical 399 StopRecordingCurrentState();
420 // memory pressure level, we need to stop listening to it.
421 if (observed_task_manager())
422 observed_task_manager()->RemoveObserver(this);
423 } 400 }
424 } 401 }
425 402
403 void ResourceReporter::StartRecordingCurrentState() {
404 // If we are already listening to the task manager, then we're waiting for
405 // a refresh event.
406 if (observed_task_manager())
407 return;
408
409 // We only record Rappor samples only if it's the first ever critical memory
410 // pressure event we receive, or it has been more than
411 // |kMinimumTimeBetweenReportsInMs| since the last time we recorded samples.
412 if (g_browser_process->local_state()) {
413 const base::Time now = base::Time::NowFromSystemTime();
414 const base::Time last_rappor_report_time = base::Time::FromDoubleT(
415 g_browser_process->local_state()->GetDouble(kLastRapporReportTimeKey));
416 const base::TimeDelta delta_since_last_report =
417 now >= last_rappor_report_time ? now - last_rappor_report_time
418 : base::TimeDelta::Max();
419
420 if (delta_since_last_report < kMinimumTimeBetweenReports)
421 return;
422 }
423
424 // Start listening to the task manager and wait for the first refresh event
425 // with background calculations completion.
426 task_manager_to_observe_->AddObserver(this);
427 }
428
429 void ResourceReporter::StopRecordingCurrentState() {
430 // If we are still listening to the task manager from an earlier critical
431 // memory pressure level, we need to stop listening to it.
432 if (observed_task_manager())
433 observed_task_manager()->RemoveObserver(this);
434 }
435
436 void ResourceReporter::OnMemoryStateChange(base::MemoryState state) {
437 // TODO(hajimehoshi): Adjust the size of this memory usage according to
438 // |state|. ResourceReporter doesn't have a feature to limit memory usage at
afakhry 2016/10/18 16:20:20 I don't understand this comment. The ResourceRepor
haraken 2016/10/18 18:52:20 You're correct. ResourceReporter is used only for
hajimehoshi 2016/10/25 07:39:42 Hmm, so this doesn't match with MemoryCoordinator'
hajimehoshi 2016/10/26 07:42:36 On second thought, this starting/stopping recordin
chrisha 2016/10/26 21:32:43 Yeah, this seems like a reasonable use to me, to g
439 // present.
440 switch (state) {
441 case base::MemoryState::NORMAL:
442 StopRecordingCurrentState();
443 break;
444 case base::MemoryState::THROTTLED:
445 StartRecordingCurrentState();
446 break;
447 case base::MemoryState::SUSPENDED:
448 // Note: Not supported at present. Fall through.
449 case base::MemoryState::UNKNOWN:
450 NOTREACHED();
afakhry 2016/10/18 16:20:20 What effect does this NOTREACHED() have on Chrome
hajimehoshi 2016/10/25 07:39:42 Right, SUSPENDED and UNKNOWN never come to this co
451 break;
452 }
453 }
454
426 } // namespace chromeos 455 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698