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

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

Issue 1374283003: Reporting top cpu and memory consumers via rappor on chromeos (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: mpearson's comments Created 5 years, 1 month 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
(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 #ifndef CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_
6 #define CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include "base/gtest_prod_util.h"
13 #include "base/macros.h"
14 #include "base/memory/memory_pressure_listener.h"
15 #include "chrome/browser/task_management/task_manager_observer.h"
16 #include "components/metrics/metrics_service.h"
17 #include "components/rappor/sample.h"
18
19 namespace base {
20 template <typename T>
21 struct DefaultSingletonTraits;
22 }
23
24 using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel;
25
26 namespace chromeos {
27
28 // A system that tracks the top |kTopConsumersCount| CPU and memory consumer
29 // Chrome tasks and reports them via Rappor whenever memory pressure is moderate
30 // or higher.
31 class ResourceReporter
32 : public task_management::TaskManagerObserver,
33 public metrics::MetricsServiceObserver {
34 public:
35 // A collection of the data of a task manager's task that the ResourceReporter
36 // is interested in.
37 struct TaskRecord {
38 explicit TaskRecord(task_management::TaskId the_id);
39
40 TaskRecord(task_management::TaskId the_id,
41 const std::string& task_name,
42 double the_cpu,
43 int64_t the_memory,
44 bool background);
45
46 // The ID of the task.
47 task_management::TaskId id;
48
49 // The canonicalized task name to be used to represent the task in a Rappor
50 // sample.
51 std::string task_name_for_rappor;
52
53 // The CPU usage of the task from the most recent task manager refresh in
54 // percentage.
55 double cpu;
56
57 // The physical memory usage of the task from the most recent task manager
58 // refresh in bytes. It doesn't include shared memory. A value of -1 is
59 // invalid. See TaskManagerInterface::GetPhysicalMemoryUsage().
60 int64_t memory;
61
62 // True if the task is running on a process at background priority.
63 bool is_background;
64 };
65
66 ~ResourceReporter() override;
67
68 // The system runs only if the user has opted-in to send anonymous usage
69 // stats. We observe state changes in the MetricsService which will determine
70 // if the ResourceReporter needs to observe the task manager and collect
71 // resource usage stats.
72 static void StartObservingMetricsService();
73 static void StopObservingMetricsService();
74
75 // The singleton instance.
76 static ResourceReporter* GetInstance();
77
78 // task_management::TaskManagerObserver:
79 void OnTaskAdded(task_management::TaskId id) override;
80 void OnTaskToBeRemoved(task_management::TaskId id) override;
81 void OnTasksRefreshed(const task_management::TaskIdList& task_ids) override;
82
83 // metrics::MetricsServiceObserver:
84 void OnMetricsServiceStart() override;
85 void OnMetricsServiceStop() override;
86
87 private:
88 friend struct base::DefaultSingletonTraits<ResourceReporter>;
89 friend class ResourceReporterTest;
90 FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestGetCpuRapporMetricName);
91 FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestGetMemoryRapporMetricName);
92 FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestAll);
93
94 // WARNING: The below enum MUST never be renamed, modified or reordered, as
95 // they're written to logs. You can only insert a new element immediately
96 // before the last.
97 enum TaskProcessPriority {
98 FOREGROUND = 0,
99 BACKGROUND,
100 PRIORITIES_NUM,
101 };
102
103 // WARNING: The below enum MUST never be renamed, modified or reordered, as
104 // they're written to logs. You can only insert a new element immediately
105 // before the last.
106 enum CpuUsageRange {
107 RANGE_0_TO_10_PERCENT = 0,
108 RANGE_10_TO_30_PERCENT,
109 RANGE_30_TO_60_PERCENT,
110 RANGE_ABOVE_60_PERCENT,
111 CPU_RANGES_NUM,
112 };
113
114 // WARNING: The below enum MUST never be renamed, modified or reordered, as
115 // they're written to logs. You can only insert a new element immediately
116 // before the last.
117 enum MemoryUsageRange {
118 RANGE_0_TO_200_MB = 0,
119 RANGE_200_TO_400_MB,
120 RANGE_400_TO_600_MB,
121 RANGE_600_TO_800_MB,
122 RANGE_800_TO_1_GB,
123 RANGE_ABOVE_1_GB,
124 MEMORY_RANGES_NUM,
125 };
126
127 // WARNING: The below enum MUST never be renamed, modified or reordered, as
128 // they're written to logs. You can only insert a new element immediately
129 // before the last.
130 enum CpuCoresNumberRange {
131 RANGE_CORES_NA = 0,
132 RANGE_CORES_1_CORE,
133 RANGE_CORES_2_CORES,
134 RANGE_CORES_3_TO_4_CORES,
135 RANGE_CORES_5_TO_8_CORES,
136 RANGE_CORES_9_TO_16_CORES,
137 RANGE_CORES_ABOVE_16_CORES,
138 CORES_RANGES_NUM,
139 };
140
141 // The maximum number of top consumer tasks of each resource that we're
142 // interested in reporting.
143 static const size_t kTopConsumersCount;
144
145 ResourceReporter();
146
147 // Creates a Rappor sample for the given |task_record|.
148 static scoped_ptr<rappor::Sample> CreateRapporSample(
149 rappor::RapporService* rappor_service,
150 const TaskRecord& task_record);
151
152 // Gets the CPU/memory usage ranges given the |cpu| / |memory_in_bytes|
153 // values.
154 static CpuUsageRange GetCpuUsageRange(double cpu);
155 static MemoryUsageRange GetMemoryUsageRange(int64_t memory_in_bytes);
156
157 // Gets the bucket in which the current system's number of CPU cores fall
158 // into.
159 static CpuCoresNumberRange GetCurrentSystemCpuCoresRange();
160
161 // Start / stop observing the task manager and the memory pressure events.
162 void StartMonitoring();
163 void StopMonitoring();
164
165 // The callback function that will be invoked on memory pressure events.
166 void OnMemoryPressure(MemoryPressureLevel memory_pressure_level);
167
168 // We'll use this to watch for memory pressure events so that we can trigger
169 // Rappor sampling at moderate memory pressure or higher.
170 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_;
171
172 // Contains the collected data about the currently running tasks from the most
173 // recent task manager refresh.
174 std::map<task_management::TaskId, scoped_ptr<TaskRecord>> task_records_;
175
176 // Contains the top |kTopConsumerCount| CPU consumer tasks sorted in a
177 // descending order by their CPU usage.
178 std::vector<TaskRecord*> task_records_by_cpu_;
179
180 // Contains the top |kTopConsumerCount| memory consumer tasks sorted in a
181 // descending order by their memory usage.
182 std::vector<TaskRecord*> task_records_by_memory_;
183
184 const CpuCoresNumberRange system_cpu_cores_range_;
185
186 // the most recent reading for the browser and GPU processes to be reported as
187 // UMA histograms when the system is under moderate memory pressure or higher.
188 double last_browser_process_cpu_ = 0.0;
189 double last_gpu_process_cpu_ = 0.0;
190 int64_t last_browser_process_memory_ = 0;
191 int64_t last_gpu_process_memory_ = 0;
192
193 MemoryPressureLevel previous_memory_pressure_level_ =
194 MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_NONE;
195
196 // MetricsService::Start()/Stop() can be called multiple times which can cause
197 // all sort of trouble. We need to track whether we have already started
198 // monitoring or not using the below flag.
199 bool is_monitoring_;
200
201 DISALLOW_COPY_AND_ASSIGN(ResourceReporter);
202 };
203
204 } // namespace chromeos
205
206 #endif // CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698