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 | |
Daniel Erat
2014/08/20 19:55:08
nit: delete unnecessary comment
Daniel Nishi
2014/08/20 20:56:34
Done.
| |
35 base::ProcessMetrics* metrics() const { return metrics_.get(); } | |
36 | |
Daniel Erat
2014/08/20 19:55:07
nit: delete blank line
Daniel Nishi
2014/08/20 20:56:34
Done.
| |
37 Profile* profile() const { return profile_; } | |
38 GURL last_origin() const { return last_origin_; } | |
39 int last_cpu_percent() const { return last_cpu_percent_; } | |
40 bool seen_this_cycle() const { return seen_this_cycle_; } | |
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_; | |
51 | |
52 // |last_origin_| is the last origin visited by the process. | |
53 GURL last_origin_; | |
54 | |
55 // |last_cpu_percent_| is the proportion of the CPU used since the last | |
56 // query. | |
57 int last_cpu_percent_; | |
58 | |
59 // |seen_this_cycle| represents if the process still exists in this cycle. | |
60 // If it doesn't, we erase the PerProcessData. | |
61 bool seen_this_cycle_; | |
62 | |
63 DISALLOW_COPY_AND_ASSIGN(PerProcessData); | |
64 }; | |
65 | |
66 ProcessPowerCollector(); | |
67 ~ProcessPowerCollector(); | |
68 | |
69 // A map from all process handles to a metric. | |
70 typedef std::map<base::ProcessHandle, linked_ptr<PerProcessData> > | |
71 ProcessMetricsMap; | |
72 | |
73 // Begin periodically updating the power consumption numbers by profile. | |
74 void StartUpdating(); | |
75 | |
76 ProcessMetricsMap* metrics_map_for_testing() { return &metrics_map_; } | |
77 | |
78 private: | |
79 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, NoSite); | |
80 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, OneSite); | |
81 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, MultipleSites); | |
82 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, | |
83 IncognitoDoesntRecordPowerUsage); | |
84 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, | |
85 MultipleProfilesRecordSeparately); | |
86 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, AppsRecordPowerUsage); | |
87 | |
88 // Callback from the timer to update. Invokes the update power consumption | |
89 // procedure. | |
90 void UpdatePowerConsumptionCallback(); | |
91 | |
92 // Synchronizes the currently active processes to the |metrics_map_| and | |
93 // returns the total amount of cpu usage in the cycle. | |
94 double SynchronizeProcesses(); | |
95 | |
96 // Attributes the power usage to the profiles and origins using the | |
97 // information from CollectCpuUsageByOrigin() given a total amount | |
98 // of CPU used in this cycle, |total_cpu_percent|. | |
99 void RecordCpuUsageByOrigin(double total_cpu_percent); | |
100 | |
101 // Adds the information from a given RenderProcessHost to the |metrics_map_| | |
102 // for a given origin. | |
Daniel Erat
2014/08/20 19:55:07
nit: add to comment: "Called by SynchronizeProcess
Daniel Nishi
2014/08/20 20:56:34
Done.
| |
103 void UpdateProcessInMap(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 |