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/time/time.h" |
| 16 #include "chrome/browser/task_management/task_manager_observer.h" |
| 17 #include "components/metrics/metrics_service.h" |
| 18 #include "components/rappor/sample.h" |
| 19 |
| 20 namespace base { |
| 21 template <typename T> |
| 22 struct DefaultSingletonTraits; |
| 23 } |
| 24 |
| 25 using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel; |
| 26 |
| 27 namespace chromeos { |
| 28 |
| 29 // A system that tracks the top |kTopConsumersCount| CPU and memory consumer |
| 30 // Chrome tasks and reports a weighted random sample of them via Rappor whenever |
| 31 // memory pressure is moderate or higher. The reporting is limited to once per |
| 32 // |kMinimumTimeBetweenReportsInMS|. |
| 33 class ResourceReporter : public task_management::TaskManagerObserver { |
| 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 // The weights of this task in terms of its CPU and memory usages. These |
| 63 // weights will be used to randomly select one of the |kTopConsumersCount| |
| 64 // task to be reporter via Rappor. |
| 65 int cpu_weight = 0; |
| 66 int memory_weight = 0; |
| 67 |
| 68 // True if the task is running on a process at background priority. |
| 69 bool is_background; |
| 70 }; |
| 71 |
| 72 ~ResourceReporter() override; |
| 73 |
| 74 // The singleton instance. |
| 75 static ResourceReporter* GetInstance(); |
| 76 |
| 77 // Start / stop observing the task manager and the memory pressure events. |
| 78 void StartMonitoring(); |
| 79 void StopMonitoring(); |
| 80 |
| 81 // task_management::TaskManagerObserver: |
| 82 void OnTaskAdded(task_management::TaskId id) override; |
| 83 void OnTaskToBeRemoved(task_management::TaskId id) override; |
| 84 void OnTasksRefreshed(const task_management::TaskIdList& task_ids) 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 // WARNING: The below enum MUST never be renamed, modified or reordered, as |
| 94 // they're written to logs. You can only insert a new element immediately |
| 95 // before the last. |
| 96 enum TaskProcessPriority { |
| 97 FOREGROUND = 0, |
| 98 BACKGROUND, |
| 99 PRIORITIES_NUM, |
| 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 CpuUsageRange { |
| 106 RANGE_0_TO_10_PERCENT = 0, |
| 107 RANGE_10_TO_30_PERCENT, |
| 108 RANGE_30_TO_60_PERCENT, |
| 109 RANGE_ABOVE_60_PERCENT, |
| 110 CPU_RANGES_NUM, |
| 111 }; |
| 112 |
| 113 // WARNING: The below enum MUST never be renamed, modified or reordered, as |
| 114 // they're written to logs. You can only insert a new element immediately |
| 115 // before the last. |
| 116 enum MemoryUsageRange { |
| 117 RANGE_0_TO_200_MB = 0, |
| 118 RANGE_200_TO_400_MB, |
| 119 RANGE_400_TO_600_MB, |
| 120 RANGE_600_TO_800_MB, |
| 121 RANGE_800_TO_1_GB, |
| 122 RANGE_ABOVE_1_GB, |
| 123 MEMORY_RANGES_NUM, |
| 124 }; |
| 125 |
| 126 // WARNING: The below enum MUST never be renamed, modified or reordered, as |
| 127 // they're written to logs. You can only insert a new element immediately |
| 128 // before the last. |
| 129 enum CpuCoresNumberRange { |
| 130 RANGE_CORES_NA = 0, |
| 131 RANGE_CORES_1_CORE, |
| 132 RANGE_CORES_2_CORES, |
| 133 RANGE_CORES_3_TO_4_CORES, |
| 134 RANGE_CORES_5_TO_8_CORES, |
| 135 RANGE_CORES_9_TO_16_CORES, |
| 136 RANGE_CORES_ABOVE_16_CORES, |
| 137 CORES_RANGES_NUM, |
| 138 }; |
| 139 |
| 140 // The maximum number of top consumer tasks of each resource that we're |
| 141 // interested in reporting. |
| 142 static const size_t kTopConsumersCount; |
| 143 |
| 144 ResourceReporter(); |
| 145 |
| 146 // Creates a Rappor sample for the given |task_record|. |
| 147 static scoped_ptr<rappor::Sample> CreateRapporSample( |
| 148 rappor::RapporService* rappor_service, |
| 149 const TaskRecord& task_record); |
| 150 |
| 151 // Gets the CPU/memory usage ranges given the |cpu| / |memory_in_bytes| |
| 152 // values. |
| 153 static CpuUsageRange GetCpuUsageRange(double cpu); |
| 154 static MemoryUsageRange GetMemoryUsageRange(int64_t memory_in_bytes); |
| 155 |
| 156 // Gets the bucket in which the current system's number of CPU cores fall |
| 157 // into. |
| 158 static CpuCoresNumberRange GetCurrentSystemCpuCoresRange(); |
| 159 |
| 160 // Determines if we're allowed to record Rappor samples when a memory pressure |
| 161 // event greater than or equal to a moderate pressure is received. |
| 162 bool ShouldRecordSamples(); |
| 163 |
| 164 // The callback function that will be invoked on memory pressure events. |
| 165 void OnMemoryPressure(MemoryPressureLevel memory_pressure_level); |
| 166 |
| 167 // We'll use this to watch for memory pressure events so that we can trigger |
| 168 // Rappor sampling at moderate memory pressure or higher. |
| 169 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_; |
| 170 |
| 171 // Contains the collected data about the currently running tasks from the most |
| 172 // recent task manager refresh. |
| 173 std::map<task_management::TaskId, scoped_ptr<TaskRecord>> task_records_; |
| 174 |
| 175 // Contains the top |kTopConsumerCount| CPU consumer tasks sorted in a |
| 176 // descending order by their CPU usage. |
| 177 std::vector<TaskRecord*> task_records_by_cpu_; |
| 178 |
| 179 // Contains the top |kTopConsumerCount| memory consumer tasks sorted in a |
| 180 // descending order by their memory usage. |
| 181 std::vector<TaskRecord*> task_records_by_memory_; |
| 182 |
| 183 // The time at which the previous memory pressure event (moderate or higher) |
| 184 // was received at which we recorded Rappor samples. This is used to limit |
| 185 // generating a Rappor report to once per |kMinimumTimeBetweenReportsInMS|. |
| 186 // This is needed to avoid generating a lot of samples that can lower the |
| 187 // Rappor privacy guarantees. |
| 188 base::TimeTicks last_memory_pressure_event_time_; |
| 189 |
| 190 const CpuCoresNumberRange system_cpu_cores_range_; |
| 191 |
| 192 // the most recent reading for the browser and GPU processes to be reported as |
| 193 // UMA histograms when the system is under moderate memory pressure or higher. |
| 194 double last_browser_process_cpu_ = 0.0; |
| 195 double last_gpu_process_cpu_ = 0.0; |
| 196 int64_t last_browser_process_memory_ = 0; |
| 197 int64_t last_gpu_process_memory_ = 0; |
| 198 |
| 199 // Tracks whether monitoring started or not. |
| 200 bool is_monitoring_ = false; |
| 201 |
| 202 // Whether we ever received a memory pressure event for the first time that is |
| 203 // greater than or equal to a moderate pressure. |
| 204 bool is_first_memory_pressure_event_ = true; |
| 205 |
| 206 DISALLOW_COPY_AND_ASSIGN(ResourceReporter); |
| 207 }; |
| 208 |
| 209 } // namespace chromeos |
| 210 |
| 211 #endif // CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_ |
OLD | NEW |