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 #ifndef CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_ |
6 #define CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <map> | 11 #include <map> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/memory_coordinator_client.h" |
17 #include "base/memory/memory_pressure_listener.h" | 18 #include "base/memory/memory_pressure_listener.h" |
18 #include "base/memory/singleton.h" | 19 #include "base/memory/singleton.h" |
19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
20 #include "chrome/browser/task_manager/task_manager_observer.h" | 21 #include "chrome/browser/task_manager/task_manager_observer.h" |
21 #include "components/metrics/metrics_service.h" | 22 #include "components/metrics/metrics_service.h" |
22 #include "components/rappor/sample.h" | 23 #include "components/rappor/sample.h" |
23 | 24 |
24 namespace chromeos { | 25 namespace chromeos { |
25 | 26 |
26 // A system that tracks the top |kTopConsumersCount| CPU and memory consumer | 27 // A system that tracks the top |kTopConsumersCount| CPU and memory consumer |
27 // Chrome tasks and reports a weighted random sample of them via Rappor whenever | 28 // Chrome tasks and reports a weighted random sample of them via Rappor whenever |
28 // memory pressure is critical. The reporting is limited to once per | 29 // memory pressure is critical. The reporting is limited to once per |
29 // |kMinimumTimeBetweenReportsInMS|. | 30 // |kMinimumTimeBetweenReportsInMS|. |
30 class ResourceReporter : public task_manager::TaskManagerObserver { | 31 class ResourceReporter : public task_manager::TaskManagerObserver, |
| 32 public base::MemoryCoordinatorClient { |
31 public: | 33 public: |
32 // A collection of the data of a task manager's task that the ResourceReporter | 34 // A collection of the data of a task manager's task that the ResourceReporter |
33 // is interested in. | 35 // is interested in. |
34 struct TaskRecord { | 36 struct TaskRecord { |
35 explicit TaskRecord(task_manager::TaskId task_id); | 37 explicit TaskRecord(task_manager::TaskId task_id); |
36 | 38 |
37 TaskRecord(task_manager::TaskId task_id, | 39 TaskRecord(task_manager::TaskId task_id, |
38 const std::string& task_name, | 40 const std::string& task_name, |
39 double cpu_percent, | 41 double cpu_percent, |
40 int64_t memory_bytes, | 42 int64_t memory_bytes, |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 const TaskRecord& task_record); | 143 const TaskRecord& task_record); |
142 | 144 |
143 // Gets the CPU/memory usage ranges given the |cpu| / |memory_in_bytes| | 145 // Gets the CPU/memory usage ranges given the |cpu| / |memory_in_bytes| |
144 // values. | 146 // values. |
145 static CpuUsageRange GetCpuUsageRange(double cpu); | 147 static CpuUsageRange GetCpuUsageRange(double cpu); |
146 static MemoryUsageRange GetMemoryUsageRange(int64_t memory_in_bytes); | 148 static MemoryUsageRange GetMemoryUsageRange(int64_t memory_in_bytes); |
147 | 149 |
148 // Gets the bucket in which the current system's number of CPU cores fall. | 150 // Gets the bucket in which the current system's number of CPU cores fall. |
149 static CpuCoresNumberRange GetCurrentSystemCpuCoresRange(); | 151 static CpuCoresNumberRange GetCurrentSystemCpuCoresRange(); |
150 | 152 |
| 153 // base::MemoryCoordinatorClient implementation: |
| 154 void OnMemoryStateChange(base::MemoryState state) override; |
| 155 |
151 // Perform a weighted random sampling to select a task by its CPU or memory | 156 // Perform a weighted random sampling to select a task by its CPU or memory |
152 // usage weights so that we can record samples for them via Rappor. | 157 // usage weights so that we can record samples for them via Rappor. |
153 // They return nullptr if no TaskRecord has been selected. | 158 // They return nullptr if no TaskRecord has been selected. |
154 const TaskRecord* SampleTaskByCpu() const; | 159 const TaskRecord* SampleTaskByCpu() const; |
155 const TaskRecord* SampleTaskByMemory() const; | 160 const TaskRecord* SampleTaskByMemory() const; |
156 | 161 |
157 // The callback function that will be invoked on memory pressure events. | 162 // The callback function that will be invoked on memory pressure events. |
158 using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel; | 163 using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel; |
159 void OnMemoryPressure(MemoryPressureLevel memory_pressure_level); | 164 void OnMemoryPressure(MemoryPressureLevel memory_pressure_level); |
160 | 165 |
| 166 // Records the current CPU and memory usage with UMA and Rappor. |
| 167 void RecordCurrentState(); |
| 168 |
161 // We'll use this to watch for memory pressure events so that we can trigger | 169 // We'll use this to watch for memory pressure events so that we can trigger |
162 // Rappor sampling at at the critical memory pressure level. | 170 // Rappor sampling at at the critical memory pressure level. |
163 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; | 171 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; |
164 | 172 |
165 // Contains the collected data about the currently running tasks from the most | 173 // Contains the collected data about the currently running tasks from the most |
166 // recent task manager refresh. | 174 // recent task manager refresh. |
167 std::map<task_manager::TaskId, std::unique_ptr<TaskRecord>> task_records_; | 175 std::map<task_manager::TaskId, std::unique_ptr<TaskRecord>> task_records_; |
168 | 176 |
169 // Contains the top |kTopConsumerCount| CPU consumer tasks sorted in a | 177 // Contains the top |kTopConsumerCount| CPU consumer tasks sorted in a |
170 // descending order by their CPU usage. | 178 // descending order by their CPU usage. |
(...skipping 28 matching lines...) Expand all Loading... |
199 | 207 |
200 // True after the first task manager OnTasksRefreshed() event is received. | 208 // True after the first task manager OnTasksRefreshed() event is received. |
201 bool have_seen_first_task_manager_refresh_ = false; | 209 bool have_seen_first_task_manager_refresh_ = false; |
202 | 210 |
203 DISALLOW_COPY_AND_ASSIGN(ResourceReporter); | 211 DISALLOW_COPY_AND_ASSIGN(ResourceReporter); |
204 }; | 212 }; |
205 | 213 |
206 } // namespace chromeos | 214 } // namespace chromeos |
207 | 215 |
208 #endif // CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_ | 216 #endif // CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_ |
OLD | NEW |