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