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 <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 "base/memory/singleton.h" | |
| 16 #include "base/time/time.h" | |
| 17 #include "chrome/browser/task_management/task_manager_observer.h" | |
| 18 #include "components/metrics/metrics_service.h" | |
| 19 #include "components/rappor/sample.h" | |
| 20 | |
| 21 namespace chromeos { | |
| 22 | |
| 23 // A system that tracks the top |kTopConsumersCount| CPU and memory consumer | |
| 24 // Chrome tasks and reports a weighted random sample of them via Rappor whenever | |
| 25 // memory pressure is moderate or higher. The reporting is limited to once per | |
| 26 // |kMinimumTimeBetweenReportsInMS|. | |
| 27 class ResourceReporter : public task_management::TaskManagerObserver { | |
| 28 public: | |
| 29 // A collection of the data of a task manager's task that the ResourceReporter | |
| 30 // is interested in. | |
| 31 struct TaskRecord { | |
| 32 explicit TaskRecord(task_management::TaskId task_id); | |
| 33 | |
| 34 TaskRecord(task_management::TaskId task_id, | |
| 35 const std::string& task_name, | |
| 36 double cpu_percent, | |
| 37 int64_t memory_bytes, | |
| 38 bool background); | |
| 39 | |
| 40 // The ID of the task. | |
| 41 task_management::TaskId id; | |
| 42 | |
| 43 // The canonicalized task name to be used to represent the task in a Rappor | |
| 44 // sample. | |
| 45 std::string task_name_for_rappor; | |
| 46 | |
| 47 // The CPU usage of the task from the most recent task manager refresh in | |
| 48 // percentage [0.0, 100.0]. | |
| 49 double cpu_percent; | |
| 50 | |
| 51 // The physical memory usage of the task from the most recent task manager | |
| 52 // refresh in bytes. It doesn't include shared memory. A value of -1 is | |
| 53 // invalid and means that the memory usage measurement for this task is not | |
| 54 // ready yet. See TaskManagerInterface::GetPhysicalMemoryUsage(). | |
| 55 int64_t memory_bytes; | |
| 56 | |
| 57 // True if the task is running on a process at background priority. | |
| 58 bool is_background; | |
| 59 }; | |
| 60 | |
| 61 ~ResourceReporter() override; | |
| 62 | |
| 63 // The singleton instance. | |
| 64 static ResourceReporter* GetInstance(); | |
| 65 | |
| 66 // Start / stop observing the task manager and the memory pressure events. | |
| 67 void StartMonitoring(); | |
| 68 void StopMonitoring(); | |
| 69 | |
| 70 // task_management::TaskManagerObserver: | |
| 71 void OnTaskAdded(task_management::TaskId id) override; | |
| 72 void OnTaskToBeRemoved(task_management::TaskId id) override; | |
| 73 void OnTasksRefreshed(const task_management::TaskIdList& task_ids) override; | |
| 74 | |
| 75 private: | |
| 76 friend struct base::DefaultSingletonTraits<ResourceReporter>; | |
| 77 friend class ResourceReporterTest; | |
| 78 FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestGetCpuRapporMetricName); | |
| 79 FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestGetMemoryRapporMetricName); | |
| 80 FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestAll); | |
| 81 | |
| 82 // WARNING: The below enum MUST never be renamed, modified or reordered, as | |
| 83 // they're written to logs. You can only insert a new element immediately | |
| 84 // before the last. | |
| 85 enum class TaskProcessPriority { | |
| 86 FOREGROUND = 0, | |
| 87 BACKGROUND = 1, | |
| 88 PRIORITIES_NUM = 2, | |
| 89 }; | |
| 90 | |
| 91 // WARNING: The below enum MUST never be renamed, modified or reordered, as | |
| 92 // they're written to logs. You can only insert a new element immediately | |
| 93 // before the last. | |
| 94 enum class CpuUsageRange { | |
| 95 RANGE_0_TO_10_PERCENT = 0, | |
|
Daniel Erat
2015/11/25 01:20:04
i think it's a bit cleaner to make these shorter a
afakhry
2015/11/25 01:43:26
The names can't start with a number though. I'd ha
Daniel Erat
2015/11/25 15:39:28
oh, good point about identifiers not being able to
afakhry
2015/11/25 19:50:41
Done.
| |
| 96 RANGE_10_TO_30_PERCENT = 1, | |
| 97 RANGE_30_TO_60_PERCENT = 2, | |
| 98 RANGE_ABOVE_60_PERCENT = 3, | |
| 99 CPU_RANGES_NUM = 4, | |
| 100 }; | |
| 101 | |
| 102 // WARNING: The below enum MUST never be renamed, modified or reordered, as | |
| 103 // they're written to logs. You can only insert a new element immediately | |
| 104 // before the last. | |
| 105 enum class MemoryUsageRange { | |
| 106 RANGE_0_TO_200_MB = 0, | |
| 107 RANGE_200_TO_400_MB = 1, | |
| 108 RANGE_400_TO_600_MB = 2, | |
| 109 RANGE_600_TO_800_MB = 3, | |
| 110 RANGE_800_TO_1_GB = 4, | |
| 111 RANGE_ABOVE_1_GB = 5, | |
| 112 MEMORY_RANGES_NUM = 6, | |
| 113 }; | |
| 114 | |
| 115 // WARNING: The below enum MUST never be renamed, modified or reordered, as | |
| 116 // they're written to logs. You can only insert a new element immediately | |
| 117 // before the last. | |
| 118 enum class CpuCoresNumberRange { | |
| 119 RANGE_CORES_NA = 0, | |
| 120 RANGE_CORES_1_CORE = 1, | |
| 121 RANGE_CORES_2_CORES = 2, | |
| 122 RANGE_CORES_3_TO_4_CORES = 3, | |
| 123 RANGE_CORES_5_TO_8_CORES = 4, | |
| 124 RANGE_CORES_9_TO_16_CORES = 5, | |
| 125 RANGE_CORES_ABOVE_16_CORES = 6, | |
| 126 CORES_RANGES_NUM = 7, | |
| 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 static CpuCoresNumberRange GetCurrentSystemCpuCoresRange(); | |
| 147 | |
| 148 // Perform a weighted random sampling to select a task by its CPU or memory | |
| 149 // usage weights so that we can record samples for them via Rappor. | |
| 150 // They return nullptr if no TaskRecord has been selected. | |
| 151 const TaskRecord* SampleTaskByCpu() const; | |
| 152 const TaskRecord* SampleTaskByMemory() const; | |
| 153 | |
| 154 // The callback function that will be invoked on memory pressure events. | |
| 155 using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel; | |
| 156 void OnMemoryPressure(MemoryPressureLevel memory_pressure_level); | |
| 157 | |
| 158 // We'll use this to watch for memory pressure events so that we can trigger | |
| 159 // Rappor sampling at moderate memory pressure or higher. | |
| 160 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_; | |
| 161 | |
| 162 // Contains the collected data about the currently running tasks from the most | |
| 163 // recent task manager refresh. | |
| 164 std::map<task_management::TaskId, scoped_ptr<TaskRecord>> task_records_; | |
| 165 | |
| 166 // Contains the top |kTopConsumerCount| CPU consumer tasks sorted in a | |
| 167 // descending order by their CPU usage. | |
| 168 std::vector<TaskRecord*> task_records_by_cpu_; | |
| 169 | |
| 170 // Contains the top |kTopConsumerCount| memory consumer tasks sorted in a | |
| 171 // descending order by their memory usage. | |
| 172 std::vector<TaskRecord*> task_records_by_memory_; | |
| 173 | |
| 174 // The time at which the previous memory pressure event (moderate or higher) | |
| 175 // was received at which we recorded Rappor samples. This is used to limit | |
| 176 // generating a Rappor report to once per |kMinimumTimeBetweenReportsInMS|. | |
| 177 // This is needed to avoid generating a lot of samples that can lower the | |
| 178 // Rappor privacy guarantees. | |
| 179 base::TimeTicks last_memory_pressure_event_time_; | |
| 180 | |
| 181 // The range that includes the number of CPU cores in the current system. | |
| 182 const CpuCoresNumberRange system_cpu_cores_range_; | |
| 183 | |
| 184 // The most recent reading for the browser and GPU processes to be reported as | |
| 185 // UMA histograms when the system is under moderate memory pressure or higher. | |
| 186 double last_browser_process_cpu_ = 0.0; | |
| 187 double last_gpu_process_cpu_ = 0.0; | |
| 188 int64_t last_browser_process_memory_ = 0; | |
| 189 int64_t last_gpu_process_memory_ = 0; | |
| 190 | |
| 191 // Tracks whether monitoring started or not. | |
| 192 bool is_monitoring_ = false; | |
| 193 | |
| 194 // True after we've seen a moderate memory pressure event or higher. | |
| 195 bool have_seen_first_memory_pressure_event_ = false; | |
| 196 | |
| 197 DISALLOW_COPY_AND_ASSIGN(ResourceReporter); | |
| 198 }; | |
| 199 | |
| 200 } // namespace chromeos | |
| 201 | |
| 202 #endif // CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_ | |
| OLD | NEW |