| 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( | 
|  | 72       task_manager::TaskManagerInterface* task_manager_to_observe); | 
| 71   void StopMonitoring(); | 73   void StopMonitoring(); | 
| 72 | 74 | 
| 73   // task_manager::TaskManagerObserver: | 75   // task_manager::TaskManagerObserver: | 
| 74   void OnTaskAdded(task_manager::TaskId id) override; | 76   void OnTasksRefreshedWithBackgroundCalculations( | 
| 75   void OnTaskToBeRemoved(task_manager::TaskId id) override; | 77       const task_manager::TaskIdList& task_ids) override; | 
| 76   void OnTasksRefreshed(const task_manager::TaskIdList& task_ids) override; |  | 
| 77 | 78 | 
| 78  private: | 79  private: | 
| 79   friend struct base::DefaultSingletonTraits<ResourceReporter>; | 80   friend struct base::DefaultSingletonTraits<ResourceReporter>; | 
| 80   friend class ResourceReporterTest; | 81   friend class ResourceReporterTest; | 
| 81   FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestGetCpuRapporMetricName); | 82   FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestGetCpuRapporMetricName); | 
| 82   FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestGetMemoryRapporMetricName); | 83   FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestGetMemoryRapporMetricName); | 
| 83   FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestAll); | 84   FRIEND_TEST_ALL_PREFIXES(ResourceReporterTest, TestAll); | 
| 84 | 85 | 
| 85   // WARNING: The below enum MUST never be renamed, modified or reordered, as | 86   // 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 | 87   // 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, | 123     RANGE_NA              = 0, | 
| 123     RANGE_1_CORE          = 1 << 0, | 124     RANGE_1_CORE          = 1 << 0, | 
| 124     RANGE_2_CORES         = 1 << 1, | 125     RANGE_2_CORES         = 1 << 1, | 
| 125     RANGE_3_TO_4_CORES    = 1 << 2, | 126     RANGE_3_TO_4_CORES    = 1 << 2, | 
| 126     RANGE_5_TO_8_CORES    = 1 << 3, | 127     RANGE_5_TO_8_CORES    = 1 << 3, | 
| 127     RANGE_9_TO_16_CORES   = 1 << 4, | 128     RANGE_9_TO_16_CORES   = 1 << 4, | 
| 128     RANGE_ABOVE_16_CORES  = 1 << 5, | 129     RANGE_ABOVE_16_CORES  = 1 << 5, | 
| 129     NUM_RANGES            = 7, | 130     NUM_RANGES            = 7, | 
| 130   }; | 131   }; | 
| 131 | 132 | 
| 132   // The maximum number of top consumer tasks of each resource that we're | 133   // The CPU and memory thresholds beyond which the tasks will be reported. | 
| 133   // interested in reporting. | 134   static const double kTaskCpuThresholdForReporting; | 
| 134   static const size_t kTopConsumersCount; | 135   static const int64_t kTaskMemoryThresholdForReporting; | 
| 135 | 136 | 
| 136   ResourceReporter(); | 137   ResourceReporter(); | 
| 137 | 138 | 
| 138   // Creates a Rappor sample for the given |task_record|. | 139   // Creates a Rappor sample for the given |task_record|. | 
| 139   static std::unique_ptr<rappor::Sample> CreateRapporSample( | 140   static std::unique_ptr<rappor::Sample> CreateRapporSample( | 
| 140       rappor::RapporService* rappor_service, | 141       rappor::RapporService* rappor_service, | 
| 141       const TaskRecord& task_record); | 142       const TaskRecord& task_record); | 
| 142 | 143 | 
| 143   // Gets the CPU/memory usage ranges given the |cpu| / |memory_in_bytes| | 144   // Gets the CPU/memory usage ranges given the |cpu| / |memory_in_bytes| | 
| 144   // values. | 145   // values. | 
| 145   static CpuUsageRange GetCpuUsageRange(double cpu); | 146   static CpuUsageRange GetCpuUsageRange(double cpu); | 
| 146   static MemoryUsageRange GetMemoryUsageRange(int64_t memory_in_bytes); | 147   static MemoryUsageRange GetMemoryUsageRange(int64_t memory_in_bytes); | 
| 147 | 148 | 
| 148   // Gets the bucket in which the current system's number of CPU cores fall. | 149   // Gets the bucket in which the current system's number of CPU cores fall. | 
| 149   static CpuCoresNumberRange GetCurrentSystemCpuCoresRange(); | 150   static CpuCoresNumberRange GetCurrentSystemCpuCoresRange(); | 
| 150 | 151 | 
| 151   // Perform a weighted random sampling to select a task by its CPU or memory | 152   // 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. | 153   // usage weights so that we can record samples for them via Rappor. | 
| 153   // They return nullptr if no TaskRecord has been selected. | 154   // They return nullptr if no TaskRecord has been selected. | 
| 154   const TaskRecord* SampleTaskByCpu() const; | 155   const TaskRecord* SampleTaskByCpu() const; | 
| 155   const TaskRecord* SampleTaskByMemory() const; | 156   const TaskRecord* SampleTaskByMemory() const; | 
| 156 | 157 | 
|  | 158   // Does the actual recording of Rappor and UMA samples. | 
|  | 159   void ReportSamples(); | 
|  | 160 | 
| 157   // The callback function that will be invoked on memory pressure events. | 161   // The callback function that will be invoked on memory pressure events. | 
| 158   using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel; | 162   using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel; | 
| 159   void OnMemoryPressure(MemoryPressureLevel memory_pressure_level); | 163   void OnMemoryPressure(MemoryPressureLevel memory_pressure_level); | 
| 160 | 164 | 
| 161   // We'll use this to watch for memory pressure events so that we can trigger | 165   // Monitor memory pressure events. | 
| 162   // Rappor sampling at at the critical memory pressure level. |  | 
| 163   std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; | 166   std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; | 
| 164 | 167 | 
| 165   // Contains the collected data about the currently running tasks from the most | 168   // Contains the data about the most CPU and memory intensive tasks collected | 
| 166   // recent task manager refresh. | 169   // at a critical memory pressure event. | 
| 167   std::map<task_manager::TaskId, std::unique_ptr<TaskRecord>> task_records_; | 170   std::vector<TaskRecord> task_records_; | 
| 168 | 171 | 
| 169   // Contains the top |kTopConsumerCount| CPU consumer tasks sorted in a | 172   // Either the real task manager implementation, or a test implementation in | 
| 170   // descending order by their CPU usage. | 173   // unit tests. | 
| 171   std::vector<TaskRecord*> task_records_by_cpu_; | 174   task_manager::TaskManagerInterface* task_manager_to_observe_; | 
| 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 | 175 | 
| 184   // The range that includes the number of CPU cores in the current system. | 176   // The range that includes the number of CPU cores in the current system. | 
| 185   const CpuCoresNumberRange system_cpu_cores_range_; | 177   const CpuCoresNumberRange system_cpu_cores_range_; | 
| 186 | 178 | 
| 187   // The most recent reading for the browser and GPU processes to be reported as | 179   // 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. | 180   // UMA histograms when the system is under critical memory pressure. | 
| 189   double last_browser_process_cpu_ = 0.0; | 181   double last_browser_process_cpu_; | 
| 190   double last_gpu_process_cpu_ = 0.0; | 182   double last_gpu_process_cpu_; | 
| 191   int64_t last_browser_process_memory_ = 0; | 183   int64_t last_browser_process_memory_; | 
| 192   int64_t last_gpu_process_memory_ = 0; | 184   int64_t last_gpu_process_memory_; | 
| 193 | 185 | 
| 194   // Tracks whether monitoring started or not. | 186   // Tracks whether monitoring started or not. | 
| 195   bool is_monitoring_ = false; | 187   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 | 188 | 
| 203   DISALLOW_COPY_AND_ASSIGN(ResourceReporter); | 189   DISALLOW_COPY_AND_ASSIGN(ResourceReporter); | 
| 204 }; | 190 }; | 
| 205 | 191 | 
| 206 }  // namespace chromeos | 192 }  // namespace chromeos | 
| 207 | 193 | 
| 208 #endif  // CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_ | 194 #endif  // CHROME_BROWSER_CHROMEOS_RESOURCE_REPORTER_RESOURCE_REPORTER_H_ | 
| OLD | NEW | 
|---|