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 class PerProcessData { | |
27 public: | |
28 PerProcessData(scoped_ptr<base::ProcessMetrics> metrics, | |
29 const GURL& origin, | |
30 Profile* profile); | |
31 PerProcessData(); | |
32 ~PerProcessData(); | |
33 | |
34 // Accessors | |
35 base::ProcessMetrics* metrics() const { return metrics_.get(); } | |
36 Profile* profile() const { return profile_; } | |
37 GURL last_origin() const { return last_origin_; } | |
38 int last_cpu_percent() const { return last_cpu_percent_; } | |
39 bool seen_this_cycle() const { return seen_this_cycle_; } | |
40 | |
41 void set_last_cpu_percent(int new_cpu) { last_cpu_percent_ = new_cpu; } | |
42 void set_seen_this_cycle(bool seen) { seen_this_cycle_ = seen; } | |
43 | |
44 private: | |
45 // |metrics_| holds the ProcessMetrics information for the given process. | |
46 scoped_ptr<base::ProcessMetrics> metrics_; | |
47 | |
48 // |profile| is the profile that is visiting the |last_origin_|. | |
49 // It is not owned by PerProcessData. | |
50 Profile* profile_; | |
Daniel Erat
2014/08/20 17:47:59
nit: mind adding a blank line above all of these c
Daniel Nishi
2014/08/20 18:13:35
I don't mind at all.
| |
51 // |last_origin_| is the last origin visited by the process. | |
52 GURL last_origin_; | |
53 // |last_cpu_percent_| is the proportion of the CPU used since the last | |
54 // query. | |
55 int last_cpu_percent_; | |
56 // |seen_this_cycle| represents if the process still exists in this cycle. | |
57 // If it doesn't, we erase the PerProcessData. | |
58 bool seen_this_cycle_; | |
59 | |
60 DISALLOW_COPY_AND_ASSIGN(PerProcessData); | |
61 }; | |
62 | |
63 ProcessPowerCollector(); | |
64 ~ProcessPowerCollector(); | |
65 | |
66 // A map from all process handles to a metric. | |
67 typedef std::map<base::ProcessHandle, linked_ptr<PerProcessData> > | |
68 ProcessMetricsMap; | |
69 | |
70 // Begin periodically updating the power consumption numbers by profile. | |
71 void StartUpdating(); | |
72 | |
73 ProcessMetricsMap* metrics_map_for_testing() { return &metrics_map_; } | |
74 | |
75 private: | |
76 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, NoSite); | |
77 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, OneSite); | |
78 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, MultipleSites); | |
79 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, | |
80 IncognitoDoesntRecordPowerUsage); | |
81 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, | |
82 MultipleProfilesRecordSeparately); | |
83 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, AppsRecordPowerUsage); | |
84 | |
85 // Callback from the timer to update. Invokes the update power consumption | |
86 // procedure. | |
87 void UpdatePowerConsumptionCallback(); | |
88 | |
89 // Synchronizes the currently active processes to the |metrics_map_| and | |
90 // returns the total amount of cpu usage in the cycle. | |
91 double SynchronizeProcesses(); | |
92 | |
93 // Attributes the power usage to the profiles and origins using the | |
94 // information from CollectCpuUsageByOrigin() given a total amount | |
95 // of CPU used in this cycle, |cpu_cycle|. | |
96 void RecordCpuUsageByOrigin(double cpu_cycle); | |
97 | |
98 // Iterates over all tabs to determine the power usage since the last sweep. | |
99 void UpdatePowerConsumption(); | |
Daniel Erat
2014/08/20 17:47:59
nit: think this is gone now
Daniel Nishi
2014/08/20 18:13:35
Done.
| |
100 | |
101 // Adds the information from a given RenderProcessHost to the |metrics_map_| | |
102 // for a given origin. | |
103 void AddProcessToMap(const content::RenderProcessHost* render_process, | |
104 const GURL& origin); | |
105 | |
106 ProcessMetricsMap metrics_map_; | |
107 base::RepeatingTimer<ProcessPowerCollector> timer_; | |
108 | |
109 DISALLOW_COPY_AND_ASSIGN(ProcessPowerCollector); | |
110 }; | |
111 | |
112 #endif // CHROME_BROWSER_POWER_PROCESS_POWER_COLLECTOR_H_ | |
OLD | NEW |