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