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

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

Issue 2808503003: Fix static initializers in the Chrome OS ResourceReporter. (Closed)
Patch Set: Created 3 years, 8 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
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 65
66 // Key used to store the last time a Rappor report was recorded in local_state. 66 // Key used to store the last time a Rappor report was recorded in local_state.
67 constexpr char kLastRapporReportTimeKey[] = 67 constexpr char kLastRapporReportTimeKey[] =
68 "resource_reporter.last_report_time"; 68 "resource_reporter.last_report_time";
69 69
70 // To keep privacy guarantees of Rappor, we limit the reports to at most once 70 // To keep privacy guarantees of Rappor, we limit the reports to at most once
71 // per day. 71 // per day.
72 constexpr base::TimeDelta kMinimumTimeBetweenReports = 72 constexpr base::TimeDelta kMinimumTimeBetweenReports =
73 base::TimeDelta::FromDays(1); 73 base::TimeDelta::FromDays(1);
74 74
75 // Gets the memory usage threshold of a process beyond which the process is 75 constexpr double kTaskCpuThresholdForReporting = 70.0;
76 // considered memory-intensive on the current device it's running on.
77 int64_t GetMemoryThresholdForDeviceInBytes() {
78 const int64_t bytes_per_cpu = base::SysInfo::AmountOfPhysicalMemory() /
79 base::SysInfo::NumberOfProcessors();
80
81 return bytes_per_cpu * 0.6;
82 }
83 76
84 } // namespace 77 } // namespace
85 78
86 ResourceReporter::TaskRecord::TaskRecord(task_manager::TaskId task_id) 79 ResourceReporter::TaskRecord::TaskRecord(task_manager::TaskId task_id)
87 : id(task_id), cpu_percent(0.0), memory_bytes(0), is_background(false) {} 80 : id(task_id), cpu_percent(0.0), memory_bytes(0), is_background(false) {}
88 81
89 ResourceReporter::TaskRecord::TaskRecord(task_manager::TaskId the_id, 82 ResourceReporter::TaskRecord::TaskRecord(task_manager::TaskId the_id,
90 const std::string& task_name, 83 const std::string& task_name,
91 double cpu_percent, 84 double cpu_percent,
92 int64_t memory_bytes, 85 int64_t memory_bytes,
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 last_browser_process_memory_ = memory_usage >= 0 ? memory_usage : 0; 161 last_browser_process_memory_ = memory_usage >= 0 ? memory_usage : 0;
169 break; 162 break;
170 163
171 case task_manager::Task::GPU: 164 case task_manager::Task::GPU:
172 last_gpu_process_cpu_ = cpu_usage; 165 last_gpu_process_cpu_ = cpu_usage;
173 last_gpu_process_memory_ = memory_usage >= 0 ? memory_usage : 0; 166 last_gpu_process_memory_ = memory_usage >= 0 ? memory_usage : 0;
174 break; 167 break;
175 168
176 default: 169 default:
177 // Other tasks types will be reported using Rappor. 170 // Other tasks types will be reported using Rappor.
178 if (memory_usage < kTaskMemoryThresholdForReporting && 171 if (memory_usage < GetTaskMemoryThresholdForReporting() &&
179 cpu_usage < kTaskCpuThresholdForReporting) { 172 cpu_usage < GetTaskCpuThresholdForReporting()) {
180 // We only care about CPU and memory intensive tasks. 173 // We only care about CPU and memory intensive tasks.
181 break; 174 break;
182 } 175 }
183 176
184 task_records_.emplace_back( 177 task_records_.emplace_back(
185 id, observed_task_manager()->GetTaskNameForRappor(id), cpu_usage, 178 id, observed_task_manager()->GetTaskNameForRappor(id), cpu_usage,
186 memory_usage, 179 memory_usage,
187 observed_task_manager()->IsTaskOnBackgroundedProcess(id)); 180 observed_task_manager()->IsTaskOnBackgroundedProcess(id));
188 } 181 }
189 } 182 }
190 183
191 // Now that we got the data, we don't need the task manager anymore. 184 // Now that we got the data, we don't need the task manager anymore.
192 if (base::MemoryPressureMonitor::Get()->GetCurrentPressureLevel() != 185 if (base::MemoryPressureMonitor::Get()->GetCurrentPressureLevel() !=
193 MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL || 186 MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL ||
194 !task_records_.empty()) { 187 !task_records_.empty()) {
195 // The memory pressure events are emitted once per second. In order to avoid 188 // The memory pressure events are emitted once per second. In order to avoid
196 // unsubscribing and then resubscribing to the task manager again on the 189 // unsubscribing and then resubscribing to the task manager again on the
197 // next event, we keep listening to the task manager as long as the memory 190 // next event, we keep listening to the task manager as long as the memory
198 // pressure level is critical AND we couldn't find any violators yet. 191 // pressure level is critical AND we couldn't find any violators yet.
199 StopRecordingCurrentState(); 192 StopRecordingCurrentState();
200 } 193 }
201 194
202 // Schedule reporting the samples. 195 // Schedule reporting the samples.
203 base::ThreadTaskRunnerHandle::Get()->PostTask( 196 base::ThreadTaskRunnerHandle::Get()->PostTask(
204 FROM_HERE, 197 FROM_HERE,
205 base::Bind(&ResourceReporter::ReportSamples, base::Unretained(this))); 198 base::Bind(&ResourceReporter::ReportSamples, base::Unretained(this)));
206 } 199 }
207 200
208 // static
209 const double ResourceReporter::kTaskCpuThresholdForReporting = 70.0;
210
211 // static
212 const int64_t ResourceReporter::kTaskMemoryThresholdForReporting =
Lei Zhang 2017/04/07 19:12:43 This constant has to be computed at startup.
213 GetMemoryThresholdForDeviceInBytes();
214
215 ResourceReporter::ResourceReporter() 201 ResourceReporter::ResourceReporter()
216 : TaskManagerObserver(base::TimeDelta::FromSeconds(kRefreshIntervalSeconds), 202 : TaskManagerObserver(base::TimeDelta::FromSeconds(kRefreshIntervalSeconds),
217 task_manager::REFRESH_TYPE_CPU | 203 task_manager::REFRESH_TYPE_CPU |
218 task_manager::REFRESH_TYPE_MEMORY | 204 task_manager::REFRESH_TYPE_MEMORY |
219 task_manager::REFRESH_TYPE_PRIORITY), 205 task_manager::REFRESH_TYPE_PRIORITY),
220 task_manager_to_observe_(nullptr), 206 task_manager_to_observe_(nullptr),
221 system_cpu_cores_range_(GetCurrentSystemCpuCoresRange()), 207 system_cpu_cores_range_(GetCurrentSystemCpuCoresRange()),
222 last_browser_process_cpu_(0.0), 208 last_browser_process_cpu_(0.0),
223 last_gpu_process_cpu_(0.0), 209 last_gpu_process_cpu_(0.0),
224 last_browser_process_memory_(0), 210 last_browser_process_memory_(0),
225 last_gpu_process_memory_(0), 211 last_gpu_process_memory_(0),
226 is_monitoring_(false) {} 212 is_monitoring_(false) {}
227 213
228 // static 214 // static
215 double ResourceReporter::GetTaskCpuThresholdForReporting() {
216 return kTaskCpuThresholdForReporting;
217 }
218
219 // static
220 int64_t ResourceReporter::GetTaskMemoryThresholdForReporting() {
221 static const int64_t threshold = 0.6 *
Lei Zhang 2017/04/07 19:12:43 Now it's computed at first use.
222 base::SysInfo::AmountOfPhysicalMemory() /
223 base::SysInfo::NumberOfProcessors();
224 return threshold;
225 }
226
227 // static
229 std::unique_ptr<rappor::Sample> ResourceReporter::CreateRapporSample( 228 std::unique_ptr<rappor::Sample> ResourceReporter::CreateRapporSample(
230 rappor::RapporServiceImpl* rappor_service, 229 rappor::RapporServiceImpl* rappor_service,
231 const ResourceReporter::TaskRecord& task_record) { 230 const ResourceReporter::TaskRecord& task_record) {
232 std::unique_ptr<rappor::Sample> sample( 231 std::unique_ptr<rappor::Sample> sample(
233 rappor_service->CreateSample(rappor::UMA_RAPPOR_TYPE)); 232 rappor_service->CreateSample(rappor::UMA_RAPPOR_TYPE));
234 sample->SetStringField(kRapporTaskStringField, 233 sample->SetStringField(kRapporTaskStringField,
235 task_record.task_name_for_rappor); 234 task_record.task_name_for_rappor);
236 sample->SetFlagsField(kRapporPriorityFlagsField, 235 sample->SetFlagsField(kRapporPriorityFlagsField,
237 task_record.is_background ? 236 task_record.is_background ?
238 GET_ENUM_VAL(TaskProcessPriority::BACKGROUND) : 237 GET_ENUM_VAL(TaskProcessPriority::BACKGROUND) :
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 break; 443 break;
445 case base::MemoryState::SUSPENDED: 444 case base::MemoryState::SUSPENDED:
446 // Note: Not supported at present. Fall through. 445 // Note: Not supported at present. Fall through.
447 case base::MemoryState::UNKNOWN: 446 case base::MemoryState::UNKNOWN:
448 NOTREACHED(); 447 NOTREACHED();
449 break; 448 break;
450 } 449 }
451 } 450 }
452 451
453 } // namespace chromeos 452 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698