Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(337)

Side by Side Diff: chrome/browser/power/process_power_collector.h

Issue 472383002: Add ProcessPowerCollector to audit power information. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Generalize CrOS values. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // On Chrome OS, can only be initialized after the DBusThreadManager has been
85 // initialized.
86 ProcessPowerCollector();
87 // On Chrome OS, can only be destroyed before DBusThreadManager is.
88 virtual ~ProcessPowerCollector();
89
90 void set_cpu_usage_callback_for_testing(const CpuUsageCallback& callback) {
91 cpu_usage_callback_ = callback;
92 }
93
94 ProcessMetricsMap* metrics_map_for_testing() { return &metrics_map_; }
95
96 #if defined(OS_CHROMEOS)
97 // PowerManagerClient::Observer implementation:
98 virtual void PowerChanged(
99 const power_manager::PowerSupplyProperties& prop) OVERRIDE;
100 #endif
101
102 // Begin periodically updating the power consumption numbers by profile.
103 void Initialize();
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 SynchronizeProcesses() given a total amount
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 SynchronizeProcesses().
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 // The factor to scale the CPU usage by.
138 double scale_factor_;
139
140 bool is_initialized_;
141
142 DISALLOW_COPY_AND_ASSIGN(ProcessPowerCollector);
143 };
144
145 #endif // CHROME_BROWSER_POWER_PROCESS_POWER_COLLECTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698