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 class Profile; | |
18 | |
19 namespace content { | |
20 class RenderProcessHost; | |
21 } | |
22 | |
23 // Manages regular updates of the profile power consumption. | |
24 class ProcessPowerCollector { | |
25 public: | |
26 ProcessPowerCollector(); | |
27 ~ProcessPowerCollector(); | |
28 | |
29 // Begin periodically updating the power consumption numbers by profile. | |
30 void StartUpdating(); | |
31 | |
32 private: | |
33 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, NoSite); | |
34 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, OneSite); | |
35 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, MultipleSites); | |
36 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, | |
37 IncognitoDoesntRecordPowerUsage); | |
38 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, | |
39 MultipleProfilesRecordSeparately); | |
40 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, AppsRecordPowerUsage); | |
41 | |
42 struct PerProcessData { | |
43 explicit PerProcessData(base::ProcessMetrics* metrics) | |
Daniel Erat
2014/08/18 20:43:05
scoped_ptr
Daniel Nishi
2014/08/19 19:24:20
Done.
| |
44 : metrics(metrics), last_cpu(-1) {} | |
Daniel Erat
2014/08/18 20:43:04
nit: one per line if they don't all fit on the sam
Daniel Nishi
2014/08/19 19:24:20
Done.
| |
45 PerProcessData() {} | |
46 ~PerProcessData(); | |
47 | |
48 // |metrics| is owned by PerProcessData and freed on destruction. | |
49 base::ProcessMetrics* metrics; | |
Daniel Erat
2014/08/18 20:43:04
make this be a scoped_ptr since it's owned by this
Daniel Nishi
2014/08/19 19:24:20
Done.
| |
50 | |
51 // |profile| is not owned by PerProcessData. | |
52 Profile* profile; | |
53 GURL last_origin; | |
Daniel Erat
2014/08/18 20:43:04
please add comments documenting what all of these
Daniel Nishi
2014/08/19 19:24:21
Done.
| |
54 double total_usage; | |
Daniel Erat
2014/08/18 20:43:04
i don't see this member being used anywhere in the
Daniel Nishi
2014/08/19 19:24:20
You're right -- that functionality got moved into
| |
55 int last_cpu; | |
Daniel Erat
2014/08/18 20:43:04
last_cpu_percent?
Daniel Nishi
2014/08/19 19:24:20
Done.
| |
56 }; | |
57 | |
58 // A map from all process handles to a metric. | |
59 typedef std::map<base::ProcessHandle, linked_ptr<PerProcessData> > | |
60 ProcessMetricsMap; | |
61 | |
62 // Callback from the timer to update. Invokes the update power consumption | |
63 // procedure. | |
64 void UpdatePowerConsumptionCallback(); | |
65 | |
66 // Updates the |metrics_map_| with currently active processes. | |
67 void UpdateMetricsMap(); | |
68 | |
69 // Updates the |metrics_map_| with updated CPU usage information and | |
70 // removes all inactive processes from the |metrics_map_|. | |
71 double PopulateCpuUsageByOrigin(); | |
72 | |
73 // Attributes the power usage to the profiles and origins given a total amount | |
74 // of CPU used in this cycle, |cpu_cycle|. | |
75 void RecordCpuUsageByOrigin(double cpu_cycle); | |
Daniel Erat
2014/08/18 20:43:04
i think that the difference between "Populate" and
Daniel Nishi
2014/08/19 19:24:20
I originally had them merged, but I split it into
| |
76 | |
77 // Iterates over all tabs to determine the power usage since the last sweep. | |
78 void UpdatePowerConsumption(); | |
79 | |
80 // Adds the information from a given RenderProcessHost to the map at the for | |
Daniel Erat
2014/08/18 20:43:04
nit: fix grammar in this comment (should it be "..
Daniel Nishi
2014/08/19 19:24:21
Done.
| |
81 // a given origin. | |
82 void AddProcessToMap(const content::RenderProcessHost* render_process, | |
83 const GURL& origin); | |
84 | |
85 ProcessMetricsMap* GetMetricsMapForTesting() { return metrics_map_.get(); } | |
Daniel Erat
2014/08/18 20:43:04
nit: make this public and rename it to metrics_map
Daniel Nishi
2014/08/19 19:24:21
Renamed. This also results in PerProcessData and t
| |
86 | |
87 scoped_ptr<ProcessMetricsMap> metrics_map_; | |
Daniel Erat
2014/08/18 20:43:05
does this really need to be a scoped_ptr? if not,
Daniel Nishi
2014/08/19 19:24:20
Done.
| |
88 base::RepeatingTimer<ProcessPowerCollector> timer_; | |
89 | |
90 DISALLOW_COPY_AND_ASSIGN(ProcessPowerCollector); | |
91 }; | |
92 | |
93 #endif // CHROME_BROWSER_POWER_PROCESS_POWER_COLLECTOR_H_ | |
OLD | NEW |