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

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: nick's and holte'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 enum TaskProcessPriority {
95 FOREGROUND = 0,
96 BACKGROUND,
97 PRIORITIES_NUM,
98 };
99
100 enum CpuUsageRange {
Mark P 2015/11/16 20:31:21 please put a warning here that these should be ren
afakhry 2015/11/16 22:33:49 Done.
101 RANGE_0_TO_10_PERCENT = 0,
102 RANGE_10_TO_30_PERCENT,
103 RANGE_30_TO_60_PERCENT,
104 RANGE_ABOVE_60_PERCENT,
105 CPU_RANGES_NUM,
106 };
107
108 enum MemoryUsageRange {
109 RANGE_0_TO_200_MB = 0,
110 RANGE_200_TO_400_MB,
111 RANGE_400_TO_600_MB,
112 RANGE_600_TO_800_MB,
113 RANGE_800_TO_1_GB,
114 RANGE_ABOVE_1_GB,
115 MEMORY_RANGES_NUM,
116 };
117
118 enum CpuCoresNumberRange {
119 RANGE_CORES_NA = 0,
120 RANGE_CORES_1_CORE,
121 RANGE_CORES_2_CORES,
122 RANGE_CORES_3_TO_4_CORES,
123 RANGE_CORES_5_TO_8_CORES,
124 RANGE_CORES_9_TO_16_CORES,
125 RANGE_CORES_ABOVE_16_CORES,
126 CORES_RANGES_NUM,
127 };
128
129 // The maximum number of top consumer tasks of each resource that we're
130 // interested in reporting.
131 static const size_t kTopConsumersCount;
132
133 ResourceReporter();
134
135 // Creates a Rappor sample for the given |task_record|.
136 static scoped_ptr<rappor::Sample> CreateRapporSample(
137 rappor::RapporService* rappor_service,
138 const TaskRecord& task_record);
139
140 // Gets the CPU/memory usage ranges given the |cpu| / |memory_in_bytes|
141 // values.
142 static CpuUsageRange GetCpuUsageRange(double cpu);
143 static MemoryUsageRange GetMemoryUsageRange(int64_t memory_in_bytes);
144
145 // Gets the bucket in which the current system's number of CPU cores fall
146 // into.
147 static CpuCoresNumberRange GetCurrentSystemCpuCoresRange();
148
149 // Start / stop observing the task manager and the memory pressure events.
150 void StartMonitoring();
151 void StopMonitoring();
152
153 // The callback function that will be invoked on memory pressure events.
154 void OnMemoryPressure(MemoryPressureLevel memory_pressure_level);
155
156 // We'll use this to watch for memory pressure events so that we can trigger
157 // Rappor sampling at moderate memory pressure or higher.
158 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_;
159
160 // Contains the collected data about the currently running tasks from the most
161 // recent task manager refresh.
162 std::map<task_management::TaskId, scoped_ptr<TaskRecord>> task_records_;
163
164 // Contains the top |kTopConsumerCount| CPU consumer tasks sorted in a
165 // descending order by their CPU usage.
166 std::vector<TaskRecord*> task_records_by_cpu_;
167
168 // Contains the top |kTopConsumerCount| memory consumer tasks sorted in a
169 // descending order by their memory usage.
170 std::vector<TaskRecord*> task_records_by_memory_;
171
172 const CpuCoresNumberRange system_cpu_cores_range_;
173
174 // the most recent reading for the browser and GPU processes to be reported as
175 // UMA histograms when the system is under moderate memory pressure or higher.
176 double last_browser_process_cpu_ = 0.0;
177 double last_gpu_process_cpu_ = 0.0;
178 int64_t last_browser_process_memory_ = 0;
179 int64_t last_gpu_process_memory_ = 0;
180
181 MemoryPressureLevel previous_memory_pressure_level_ =
182 MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_NONE;
183
184 // MetricsService::Start()/Stop() can be called multiple times which can cause
185 // all sort of trouble. We need to track whether we have already started
186 // monitoring or not using the below flag.
187 bool is_monitoring_;
188
189 DISALLOW_COPY_AND_ASSIGN(ResourceReporter);
190 };
191
192 } // namespace chromeos
193
194 #endif // CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698