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 #if defined(OS_CHROMEOS) | |
28 namespace power_manager { | |
29 class PowerSupplyProperties; | |
30 } | |
31 #endif | |
32 | |
33 // Manages regular updates of the profile power consumption. | |
34 class ProcessPowerCollector | |
35 #if defined(OS_CHROMEOS) | |
36 : public chromeos::PowerManagerClient::Observer | |
37 #endif | |
38 { | |
39 public: | |
40 class PerProcessData { | |
41 public: | |
42 PerProcessData(scoped_ptr<base::ProcessMetrics> metrics, | |
43 const GURL& origin, | |
44 Profile* profile); | |
45 PerProcessData(); | |
46 ~PerProcessData(); | |
47 | |
48 base::ProcessMetrics* metrics() const { return metrics_.get(); } | |
49 Profile* profile() const { return profile_; } | |
50 GURL last_origin() const { return last_origin_; } | |
51 int last_cpu_percent() const { return last_cpu_percent_; } | |
52 bool seen_this_cycle() const { return seen_this_cycle_; } | |
53 void set_last_cpu_percent(double new_cpu) { last_cpu_percent_ = new_cpu; } | |
54 void set_seen_this_cycle(bool seen) { seen_this_cycle_ = seen; } | |
55 | |
56 private: | |
57 // |metrics_| holds the ProcessMetrics information for the given process. | |
58 scoped_ptr<base::ProcessMetrics> metrics_; | |
59 | |
60 // |profile| is the profile that is visiting the |last_origin_|. | |
61 // It is not owned by PerProcessData. | |
62 Profile* profile_; | |
63 | |
64 // |last_origin_| is the last origin visited by the process. | |
65 GURL last_origin_; | |
66 | |
67 // |last_cpu_percent_| is the proportion of the CPU used since the last | |
68 // query. | |
69 double last_cpu_percent_; | |
70 | |
71 // |seen_this_cycle| represents if the process still exists in this cycle. | |
72 // If it doesn't, we erase the PerProcessData. | |
73 bool seen_this_cycle_; | |
74 | |
75 DISALLOW_COPY_AND_ASSIGN(PerProcessData); | |
76 }; | |
77 | |
78 // A map from all process handles to a metric. | |
79 typedef std::map<base::ProcessHandle, linked_ptr<PerProcessData> > | |
80 ProcessMetricsMap; | |
81 // A callback used to define mock CPU usage for testing. | |
82 typedef base::Callback<double(base::ProcessHandle)> CpuUsageCallback; | |
83 | |
84 ProcessPowerCollector(); | |
85 // On Chrome OS, should only be destroyed before DBusThreadManager is. | |
86 virtual ~ProcessPowerCollector(); | |
87 | |
88 void set_cpu_usage_callback_for_testing(const CpuUsageCallback& callback) { | |
89 cpu_usage_callback_ = callback; | |
90 } | |
91 | |
92 #if defined(OS_CHROMEOS) | |
93 // PowerManagerClient::Observer implementation: | |
94 virtual void PowerChanged( | |
95 const power_manager::PowerSupplyProperties& prop) OVERRIDE; | |
96 #endif | |
97 | |
98 // Begin periodically updating the power consumption numbers by profile. | |
99 // On Chrome OS only, can only be called after the DBusThreadManager has been | |
100 // initialized. | |
101 void Initialize(); | |
102 | |
103 ProcessMetricsMap* metrics_map_for_testing() { return &metrics_map_; } | |
Daniel Erat
2014/08/22 22:41:38
nit: move this up below set_cpu_usage_callback_for
Daniel Nishi
2014/08/22 23:21:32
Done.
| |
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 | |
Daniel Erat
2014/08/22 22:41:38
nit: s/CollectCpuUsageByOrigin/SynchronizeProcesse
Daniel Nishi
2014/08/22 23:21:32
Done.
| |
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(). | |
Daniel Erat
2014/08/22 22:41:38
nit: s/Synchronized/Synchronize/
Daniel Nishi
2014/08/22 23:21:32
Done.
| |
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 |