Index: chrome/browser/power/process_power_collector.h |
diff --git a/chrome/browser/power/process_power_collector.h b/chrome/browser/power/process_power_collector.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9b8181516decaa0c196a260a85c0cb9894e20f85 |
--- /dev/null |
+++ b/chrome/browser/power/process_power_collector.h |
@@ -0,0 +1,109 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CHROME_BROWSER_POWER_PROCESS_POWER_COLLECTOR_H_ |
+#define CHROME_BROWSER_POWER_PROCESS_POWER_COLLECTOR_H_ |
+ |
+#include <map> |
+ |
+#include "base/memory/linked_ptr.h" |
+#include "base/process/process_handle.h" |
+#include "base/process/process_metrics.h" |
+#include "base/timer/timer.h" |
+#include "components/power/origin_power_map_factory.h" |
+#include "url/gurl.h" |
+ |
+class Profile; |
+ |
+namespace content { |
+class RenderProcessHost; |
+} |
+ |
+// Manages regular updates of the profile power consumption. |
+class ProcessPowerCollector { |
+ public: |
+ ProcessPowerCollector(); |
+ ~ProcessPowerCollector(); |
+ |
+ class PerProcessData { |
+ public: |
+ explicit PerProcessData(base::ProcessMetrics* metrics, |
Daniel Erat
2014/08/20 00:10:42
nit: don't need explicit here anymore; also change
Daniel Nishi
2014/08/20 17:16:46
Done.
|
+ const GURL& origin, |
+ Profile* profile); |
+ PerProcessData(); |
+ ~PerProcessData(); |
+ |
+ // Accessors |
+ base::ProcessMetrics* metrics() const { return metrics_.get(); } |
Daniel Erat
2014/08/20 00:10:42
return a pointer-to-const?
Daniel Nishi
2014/08/20 17:16:46
ProcessMetrics::GetCpuUsage() isn't const, so I do
|
+ Profile* profile() const { return profile_; } |
+ GURL last_origin() const { return last_origin_; } |
+ int last_cpu_used() const { return last_cpu_percent_; } |
Daniel Erat
2014/08/20 00:10:42
rename accessor to last_cpu_percent() to match dat
Daniel Nishi
2014/08/20 17:16:46
Done.
|
+ |
+ void SetLastCpuPercent(int new_cpu); |
Daniel Erat
2014/08/20 00:10:42
this is a simple setter; rename it to set_last_cpu
Daniel Nishi
2014/08/20 17:16:46
Done.
|
+ |
+ private: |
+ // |metrics_| holds the ProcessMetrics information for the given process. |
+ scoped_ptr<base::ProcessMetrics> metrics_; |
+ |
+ // |profile| is the profile that is visiting the |last_origin_|. |
+ // It is not owned by PerProcessData. |
+ Profile* profile_; |
+ // |last_origin_| is the last origin visited by the process. |
+ GURL last_origin_; |
+ // |last_cpu_percent_| is the proportion of the CPU used since the last |
+ // query. |
+ int last_cpu_percent_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(PerProcessData); |
+ }; |
+ |
+ // A map from all process handles to a metric. |
+ typedef std::map<base::ProcessHandle, linked_ptr<PerProcessData> > |
+ ProcessMetricsMap; |
+ |
+ // Begin periodically updating the power consumption numbers by profile. |
+ void StartUpdating(); |
+ |
+ ProcessMetricsMap* metrics_map_for_testing() { return &metrics_map_; } |
+ |
+ private: |
+ FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, NoSite); |
+ FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, OneSite); |
+ FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, MultipleSites); |
+ FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, |
+ IncognitoDoesntRecordPowerUsage); |
+ FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, |
+ MultipleProfilesRecordSeparately); |
+ FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, AppsRecordPowerUsage); |
+ |
+ // Callback from the timer to update. Invokes the update power consumption |
+ // procedure. |
+ void UpdatePowerConsumptionCallback(); |
+ |
+ // Updates the |metrics_map_| with currently active processes. |
+ void UpdateMetricsMap(); |
Daniel Erat
2014/08/20 00:10:42
this method name seems too vague; the next two met
Daniel Nishi
2014/08/20 17:16:46
Seems like a good name. Done.
|
+ |
+ // Updates the |metrics_map_| with updated CPU usage information and |
+ // removes all inactive processes from the |metrics_map_|. |
+ double PopulateCpuUsageByOrigin(); |
Daniel Erat
2014/08/20 00:10:42
s/Populate/Collect/, maybe?
Daniel Nishi
2014/08/20 17:16:46
Done.
|
+ |
+ // Attributes the power usage to the profiles and origins given a total amount |
Daniel Erat
2014/08/20 00:10:42
mention that this uses the data written to |metric
Daniel Nishi
2014/08/20 17:16:46
Done.
|
+ // of CPU used in this cycle, |cpu_cycle|. |
+ void RecordCpuUsageByOrigin(double cpu_cycle); |
+ |
+ // Iterates over all tabs to determine the power usage since the last sweep. |
+ void UpdatePowerConsumption(); |
+ |
+ // Adds the information from a given RenderProcessHost to the |metrics_map_| |
+ // for a given origin. |
+ void AddProcessToMap(const content::RenderProcessHost* render_process, |
+ const GURL& origin); |
+ |
+ ProcessMetricsMap metrics_map_; |
+ base::RepeatingTimer<ProcessPowerCollector> timer_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ProcessPowerCollector); |
+}; |
+ |
+#endif // CHROME_BROWSER_POWER_PROCESS_POWER_COLLECTOR_H_ |