Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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_POWER_PROCESS_POWER_COLLECTOR_H_ | |
| 6 #define CHROME_BROWSER_POWER_PROCESS_POWER_COLLECTOR_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 | |
| 10 #include "base/memory/linked_ptr.h" | |
| 11 #include "base/process/process_handle.h" | |
| 12 #include "base/process/process_metrics.h" | |
| 13 #include "base/timer/timer.h" | |
| 14 #include "components/power/origin_power_map_factory.h" | |
| 15 #include "url/gurl.h" | |
| 16 | |
| 17 #if defined(OS_CHROMEOS) | |
| 18 #include "chromeos/dbus/power_manager_client.h" | |
| 19 #endif | |
| 20 | |
| 21 class Profile; | |
| 22 | |
| 23 namespace content { | |
| 24 class RenderProcessHost; | |
| 25 } | |
| 26 | |
| 27 namespace power_manager { | |
|
Daniel Erat
2014/08/22 03:13:49
nit: put this inside an OS_CHROMEOS ifdef
Daniel Nishi
2014/08/22 17:23:12
Done.
| |
| 28 class PowerSupplyProperties; | |
| 29 } | |
| 30 | |
| 31 // Manages regular updates of the profile power consumption. | |
| 32 class ProcessPowerCollector | |
| 33 #if defined(OS_CHROMEOS) | |
| 34 : public chromeos::PowerManagerClient::Observer | |
| 35 #endif | |
| 36 { | |
| 37 public: | |
| 38 class PerProcessData { | |
| 39 public: | |
| 40 PerProcessData(scoped_ptr<base::ProcessMetrics> metrics, | |
| 41 const GURL& origin, | |
| 42 Profile* profile); | |
| 43 PerProcessData(); | |
| 44 ~PerProcessData(); | |
| 45 | |
| 46 base::ProcessMetrics* metrics() const { return metrics_.get(); } | |
| 47 Profile* profile() const { return profile_; } | |
| 48 GURL last_origin() const { return last_origin_; } | |
| 49 int last_cpu_percent() const { return last_cpu_percent_; } | |
| 50 bool seen_this_cycle() const { return seen_this_cycle_; } | |
| 51 void set_last_cpu_percent(double new_cpu) { last_cpu_percent_ = new_cpu; } | |
| 52 void set_seen_this_cycle(bool seen) { seen_this_cycle_ = seen; } | |
| 53 | |
| 54 private: | |
| 55 // |metrics_| holds the ProcessMetrics information for the given process. | |
| 56 scoped_ptr<base::ProcessMetrics> metrics_; | |
| 57 | |
| 58 // |profile| is the profile that is visiting the |last_origin_|. | |
| 59 // It is not owned by PerProcessData. | |
| 60 Profile* profile_; | |
| 61 | |
| 62 // |last_origin_| is the last origin visited by the process. | |
| 63 GURL last_origin_; | |
| 64 | |
| 65 // |last_cpu_percent_| is the proportion of the CPU used since the last | |
| 66 // query. | |
| 67 double last_cpu_percent_; | |
| 68 | |
| 69 // |seen_this_cycle| represents if the process still exists in this cycle. | |
| 70 // If it doesn't, we erase the PerProcessData. | |
| 71 bool seen_this_cycle_; | |
| 72 | |
| 73 DISALLOW_COPY_AND_ASSIGN(PerProcessData); | |
| 74 }; | |
| 75 | |
| 76 // A map from all process handles to a metric. | |
| 77 typedef std::map<base::ProcessHandle, linked_ptr<PerProcessData> > | |
| 78 ProcessMetricsMap; | |
| 79 // A callback used to define mock CPU usage for testing. | |
| 80 typedef base::Callback<double(base::ProcessHandle)> CpuUsageCallback; | |
| 81 | |
| 82 ProcessPowerCollector(); | |
| 83 virtual ~ProcessPowerCollector(); | |
| 84 | |
| 85 void set_cpu_usage_callback_for_testing(const CpuUsageCallback& callback) { | |
| 86 cpu_usage_callback_ = callback; | |
| 87 } | |
| 88 | |
| 89 #if defined(OS_CHROMEOS) | |
| 90 // PowerManagerClient::Observer implementation: | |
| 91 virtual void PowerChanged( | |
| 92 const power_manager::PowerSupplyProperties& prop) OVERRIDE; | |
| 93 #endif | |
| 94 | |
| 95 // Begin periodically updating the power consumption numbers by profile. | |
| 96 // On Chrome OS only, can only be called after the DBusThreadManager has been | |
| 97 // initialized. | |
| 98 void Initialize(); | |
| 99 | |
| 100 // Should only be called before DBusThreadManager is destroyed. | |
| 101 void Shutdown(); | |
| 102 | |
| 103 ProcessMetricsMap* metrics_map_for_testing() { return &metrics_map_; } | |
| 104 | |
| 105 // Calls UpdatePowerConsumption() and returns the total CPU percent. | |
| 106 double UpdatePowerConsumptionForTesting(); | |
| 107 | |
| 108 private: | |
| 109 // Calls SynchronizerProcesses() and RecordCpuUsageByOrigin() to update the | |
| 110 // |metrics_map_| and attribute power consumption. Invoked by |timer_| and as | |
| 111 // a helper method for UpdatePowerConsumptionForTesting(). | |
| 112 double UpdatePowerConsumption(); | |
| 113 | |
| 114 // Calls UpdatePowerConsumption(). Invoked by |timer_|. | |
| 115 void HandleUpdateTimeout(); | |
| 116 | |
| 117 // Synchronizes the currently active processes to the |metrics_map_| and | |
| 118 // returns the total amount of cpu usage in the cycle. | |
| 119 double SynchronizeProcesses(); | |
| 120 | |
| 121 // Attributes the power usage to the profiles and origins using the | |
| 122 // information from CollectCpuUsageByOrigin() given a total amount | |
| 123 // of CPU used in this cycle, |total_cpu_percent|. | |
| 124 void RecordCpuUsageByOrigin(double total_cpu_percent); | |
| 125 | |
| 126 // Adds the information from a given RenderProcessHost to the |metrics_map_| | |
| 127 // for a given origin. Called by SynchronizedProcesses(). | |
| 128 void UpdateProcessInMap(const content::RenderProcessHost* render_process, | |
| 129 const GURL& origin); | |
| 130 | |
| 131 ProcessMetricsMap metrics_map_; | |
| 132 base::RepeatingTimer<ProcessPowerCollector> timer_; | |
| 133 | |
| 134 // Callback to use to get CPU usage if set. | |
| 135 CpuUsageCallback cpu_usage_callback_; | |
| 136 | |
| 137 #if defined(OS_CHROMEOS) | |
| 138 // If we should update the origin map. In Chrome OS, if the battery is plugged | |
| 139 // in, we cannot get valid battery discharge information, so we cannot update | |
| 140 // the power consumption. | |
| 141 bool should_update_; | |
| 142 // The battery discharge rate in W. | |
| 143 double battery_discharge_rate_; | |
| 144 #endif | |
| 145 | |
| 146 DISALLOW_COPY_AND_ASSIGN(ProcessPowerCollector); | |
| 147 }; | |
| 148 | |
| 149 #endif // CHROME_BROWSER_POWER_PROCESS_POWER_COLLECTOR_H_ | |
| OLD | NEW |