Chromium Code Reviews| 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> | |
| 12 #include <string> | 11 #include <string> |
| 13 #include <vector> | 12 #include <vector> |
| 14 | 13 |
| 15 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 16 #include "base/macros.h" | 15 #include "base/macros.h" |
| 17 #include "base/memory/memory_pressure_listener.h" | 16 #include "base/memory/memory_pressure_listener.h" |
| 18 #include "base/memory/singleton.h" | 17 #include "base/memory/singleton.h" |
| 19 #include "base/time/time.h" | |
| 20 #include "chrome/browser/task_manager/task_manager_observer.h" | 18 #include "chrome/browser/task_manager/task_manager_observer.h" |
| 21 #include "components/metrics/metrics_service.h" | 19 #include "components/metrics/metrics_service.h" |
| 20 #include "components/prefs/pref_registry_simple.h" | |
| 22 #include "components/rappor/sample.h" | 21 #include "components/rappor/sample.h" |
| 23 | 22 |
| 24 namespace chromeos { | 23 namespace chromeos { |
| 25 | 24 |
| 26 // A system that tracks the top |kTopConsumersCount| CPU and memory consumer | 25 // 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 | 26 // 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 | 27 // memory pressure is critical. The reporting is limited to once per |
| 29 // |kMinimumTimeBetweenReportsInMS|. | 28 // |kMinimumTimeBetweenReportsInMS|. |
| 30 class ResourceReporter : public task_manager::TaskManagerObserver { | 29 class ResourceReporter : public task_manager::TaskManagerObserver { |
| 31 public: | 30 public: |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 59 | 58 |
| 60 // True if the task is running on a process at background priority. | 59 // True if the task is running on a process at background priority. |
| 61 bool is_background; | 60 bool is_background; |
| 62 }; | 61 }; |
| 63 | 62 |
| 64 ~ResourceReporter() override; | 63 ~ResourceReporter() override; |
| 65 | 64 |
| 66 // The singleton instance. | 65 // The singleton instance. |
| 67 static ResourceReporter* GetInstance(); | 66 static ResourceReporter* GetInstance(); |
| 68 | 67 |
| 68 static void RegisterPrefs(PrefRegistrySimple* registry); | |
| 69 | |
| 69 // Start / stop observing the task manager and the memory pressure events. | 70 // Start / stop observing the task manager and the memory pressure events. |
| 70 void StartMonitoring(); | 71 void StartMonitoring(); |
| 71 void StopMonitoring(); | 72 void StopMonitoring(); |
| 72 | 73 |
| 73 // task_manager::TaskManagerObserver: | 74 // task_manager::TaskManagerObserver: |
| 74 void OnTaskAdded(task_manager::TaskId id) override; | 75 void OnTasksRefreshedWithBackgroundCalculations( |
| 75 void OnTaskToBeRemoved(task_manager::TaskId id) override; | 76 const task_manager::TaskIdList& task_ids) override; |
| 76 void OnTasksRefreshed(const task_manager::TaskIdList& task_ids) override; | |
| 77 | 77 |
| 78 private: | 78 private: |
| 79 friend struct base::DefaultSingletonTraits<ResourceReporter>; | 79 friend struct base::DefaultSingletonTraits<ResourceReporter>; |
| 80 friend class ResourceReporterTest; | 80 friend class ResourceReporterTest; |
| 81 FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestGetCpuRapporMetricName); | 81 FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestGetCpuRapporMetricName); |
| 82 FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestGetMemoryRapporMetricName); | 82 FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestGetMemoryRapporMetricName); |
| 83 FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestAll); | 83 FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestAll); |
| 84 | 84 |
| 85 // WARNING: The below enum MUST never be renamed, modified or reordered, as | 85 // WARNING: The below enum MUST never be renamed, modified or reordered, as |
| 86 // they're written to logs. You can only insert a new element immediately | 86 // they're written to logs. You can only insert a new element immediately |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 RANGE_NA = 0, | 122 RANGE_NA = 0, |
| 123 RANGE_1_CORE = 1 << 0, | 123 RANGE_1_CORE = 1 << 0, |
| 124 RANGE_2_CORES = 1 << 1, | 124 RANGE_2_CORES = 1 << 1, |
| 125 RANGE_3_TO_4_CORES = 1 << 2, | 125 RANGE_3_TO_4_CORES = 1 << 2, |
| 126 RANGE_5_TO_8_CORES = 1 << 3, | 126 RANGE_5_TO_8_CORES = 1 << 3, |
| 127 RANGE_9_TO_16_CORES = 1 << 4, | 127 RANGE_9_TO_16_CORES = 1 << 4, |
| 128 RANGE_ABOVE_16_CORES = 1 << 5, | 128 RANGE_ABOVE_16_CORES = 1 << 5, |
| 129 NUM_RANGES = 7, | 129 NUM_RANGES = 7, |
| 130 }; | 130 }; |
| 131 | 131 |
| 132 // The maximum number of top consumer tasks of each resource that we're | 132 // The CPU and memory thresholds beyond which the tasks will be reported. |
| 133 // interested in reporting. | 133 static const double kTaskCpuThresholdForReporting; |
| 134 static const size_t kTopConsumersCount; | 134 static const int64_t kTaskMemoryThresholdForReporting; |
| 135 | 135 |
| 136 ResourceReporter(); | 136 ResourceReporter(); |
| 137 | 137 |
| 138 // Creates a Rappor sample for the given |task_record|. | 138 // Creates a Rappor sample for the given |task_record|. |
| 139 static std::unique_ptr<rappor::Sample> CreateRapporSample( | 139 static std::unique_ptr<rappor::Sample> CreateRapporSample( |
| 140 rappor::RapporService* rappor_service, | 140 rappor::RapporService* rappor_service, |
| 141 const TaskRecord& task_record); | 141 const TaskRecord& task_record); |
| 142 | 142 |
| 143 // Gets the CPU/memory usage ranges given the |cpu| / |memory_in_bytes| | 143 // Gets the CPU/memory usage ranges given the |cpu| / |memory_in_bytes| |
| 144 // values. | 144 // values. |
| 145 static CpuUsageRange GetCpuUsageRange(double cpu); | 145 static CpuUsageRange GetCpuUsageRange(double cpu); |
| 146 static MemoryUsageRange GetMemoryUsageRange(int64_t memory_in_bytes); | 146 static MemoryUsageRange GetMemoryUsageRange(int64_t memory_in_bytes); |
| 147 | 147 |
| 148 // Gets the bucket in which the current system's number of CPU cores fall. | 148 // Gets the bucket in which the current system's number of CPU cores fall. |
| 149 static CpuCoresNumberRange GetCurrentSystemCpuCoresRange(); | 149 static CpuCoresNumberRange GetCurrentSystemCpuCoresRange(); |
| 150 | 150 |
| 151 // Perform a weighted random sampling to select a task by its CPU or memory | 151 // 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. | 152 // usage weights so that we can record samples for them via Rappor. |
| 153 // They return nullptr if no TaskRecord has been selected. | 153 // They return nullptr if no TaskRecord has been selected. |
| 154 const TaskRecord* SampleTaskByCpu() const; | 154 const TaskRecord* SampleTaskByCpu() const; |
| 155 const TaskRecord* SampleTaskByMemory() const; | 155 const TaskRecord* SampleTaskByMemory() const; |
| 156 | 156 |
| 157 // Does the actual recording of Rappor and UMA samples. | |
| 158 void ReportSamples(); | |
| 159 | |
| 157 // The callback function that will be invoked on memory pressure events. | 160 // The callback function that will be invoked on memory pressure events. |
| 158 using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel; | 161 using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel; |
| 159 void OnMemoryPressure(MemoryPressureLevel memory_pressure_level); | 162 void OnMemoryPressure(MemoryPressureLevel memory_pressure_level); |
| 160 | 163 |
| 161 // We'll use this to watch for memory pressure events so that we can trigger | 164 // Monitor memory pressure events. |
| 162 // Rappor sampling at at the critical memory pressure level. | |
| 163 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; | 165 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; |
| 164 | 166 |
| 165 // Contains the collected data about the currently running tasks from the most | 167 // Contains the data about the most CPU and memory intensive tasks collected |
| 166 // recent task manager refresh. | 168 // at a critical memory pressure event. |
| 167 std::map<task_manager::TaskId, std::unique_ptr<TaskRecord>> task_records_; | 169 std::vector<TaskRecord> task_records_; |
| 168 | 170 |
| 169 // Contains the top |kTopConsumerCount| CPU consumer tasks sorted in a | 171 // The task manager used for unit tests. |
| 170 // descending order by their CPU usage. | 172 task_manager::TaskManagerInterface* task_manager_for_testing_; |
|
ncarter (slow)
2016/10/07 19:04:16
A for_testing_ data member should be a last resort
afakhry
2016/10/12 16:37:36
That's a great suggestion actually! Done.
Thanks!
| |
| 171 std::vector<TaskRecord*> task_records_by_cpu_; | |
| 172 | |
| 173 // Contains the top |kTopConsumerCount| memory consumer tasks sorted in a | |
| 174 // descending order by their memory usage. | |
| 175 std::vector<TaskRecord*> task_records_by_memory_; | |
| 176 | |
| 177 // The time at which the previous critical memory pressure event was received | |
| 178 // at which we recorded Rappor samples. This is used to limit generating a | |
| 179 // Rappor report to once per |kMinimumTimeBetweenReportsInMS|. | |
| 180 // This is needed to avoid generating a lot of samples that can lower the | |
| 181 // Rappor privacy guarantees. | |
| 182 base::TimeTicks last_memory_pressure_event_time_; | |
| 183 | 173 |
| 184 // The range that includes the number of CPU cores in the current system. | 174 // The range that includes the number of CPU cores in the current system. |
| 185 const CpuCoresNumberRange system_cpu_cores_range_; | 175 const CpuCoresNumberRange system_cpu_cores_range_; |
| 186 | 176 |
| 187 // The most recent reading for the browser and GPU processes to be reported as | 177 // The most recent reading for the browser and GPU processes to be reported as |
| 188 // UMA histograms when the system is under critical memory pressure. | 178 // UMA histograms when the system is under critical memory pressure. |
| 189 double last_browser_process_cpu_ = 0.0; | 179 double last_browser_process_cpu_; |
| 190 double last_gpu_process_cpu_ = 0.0; | 180 double last_gpu_process_cpu_; |
| 191 int64_t last_browser_process_memory_ = 0; | 181 int64_t last_browser_process_memory_; |
| 192 int64_t last_gpu_process_memory_ = 0; | 182 int64_t last_gpu_process_memory_; |
| 193 | 183 |
| 194 // Tracks whether monitoring started or not. | 184 // Tracks whether monitoring started or not. |
| 195 bool is_monitoring_ = false; | 185 bool is_monitoring_; |
| 196 | |
| 197 // True after we've seen a critical memory pressure event. | |
| 198 bool have_seen_first_memory_pressure_event_ = false; | |
| 199 | |
| 200 // True after the first task manager OnTasksRefreshed() event is received. | |
| 201 bool have_seen_first_task_manager_refresh_ = false; | |
| 202 | 186 |
| 203 DISALLOW_COPY_AND_ASSIGN(ResourceReporter); | 187 DISALLOW_COPY_AND_ASSIGN(ResourceReporter); |
| 204 }; | 188 }; |
| 205 | 189 |
| 206 } // namespace chromeos | 190 } // namespace chromeos |
| 207 | 191 |
| 208 #endif // CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_ | 192 #endif // CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_ |
| OLD | NEW |