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..2b18a26f990f0692612cbd8856926e67b734d339 |
--- /dev/null |
+++ b/chrome/browser/power/process_power_collector.h |
@@ -0,0 +1,120 @@ |
+// 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: |
+ class PerProcessData { |
+ public: |
+ PerProcessData(scoped_ptr<base::ProcessMetrics> metrics, |
+ const GURL& origin, |
+ Profile* profile); |
+ PerProcessData(); |
+ ~PerProcessData(); |
+ |
+ base::ProcessMetrics* metrics() const { return metrics_.get(); } |
+ Profile* profile() const { return profile_; } |
+ GURL last_origin() const { return last_origin_; } |
+ int last_cpu_percent() const { return last_cpu_percent_; } |
+ bool seen_this_cycle() const { return seen_this_cycle_; } |
+ void set_last_cpu_percent(int new_cpu) { last_cpu_percent_ = new_cpu; } |
+ void set_seen_this_cycle(bool seen) { seen_this_cycle_ = seen; } |
+ |
+ 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_; |
+ |
+ // |seen_this_cycle| represents if the process still exists in this cycle. |
+ // If it doesn't, we erase the PerProcessData. |
+ bool seen_this_cycle_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(PerProcessData); |
+ }; |
+ |
+ ProcessPowerCollector(); |
+ ~ProcessPowerCollector(); |
+ |
+ // A map from all process handles to a metric. |
+ typedef std::map<base::ProcessHandle, linked_ptr<PerProcessData> > |
+ ProcessMetricsMap; |
+ // A callback used to define mock CPU usage for testing. |
+ typedef base::Callback<int(base::ProcessHandle)> CpuUsageCallback; |
+ |
+ // Begin periodically updating the power consumption numbers by profile. |
+ void StartUpdating(); |
+ |
+ ProcessMetricsMap* metrics_map_for_testing() { return &metrics_map_; } |
+ |
+ void SetGetCpuUsageCallbackForTesting(const CpuUsageCallback& cpu_callback); |
Daniel Erat
2014/08/20 21:53:14
nit: inline this and name it set_cpu_usage_callbac
Daniel Nishi
2014/08/21 17:53:55
Done.
|
+ |
+ // Returns the total CPU percent for the cycle. |
+ double UpdatePowerConsumptionForTesting(); |
+ |
+ private: |
+ FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, NoSite); |
Daniel Erat
2014/08/20 21:53:14
do you need these still, or can the tests do every
Daniel Nishi
2014/08/21 17:53:55
Whoops. I meant to take those out.
Done.
|
+ 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(); |
+ |
+ // Synchronizes the currently active processes to the |metrics_map_| and |
+ // returns the total amount of cpu usage in the cycle. |
+ double SynchronizeProcesses(); |
+ |
+ // Attributes the power usage to the profiles and origins using the |
+ // information from CollectCpuUsageByOrigin() given a total amount |
+ // of CPU used in this cycle, |total_cpu_percent|. |
+ void RecordCpuUsageByOrigin(double total_cpu_percent); |
+ |
+ // Adds the information from a given RenderProcessHost to the |metrics_map_| |
+ // for a given origin. Called by SynchronizedProcesses(). |
+ void UpdateProcessInMap(const content::RenderProcessHost* render_process, |
+ const GURL& origin); |
+ |
+ ProcessMetricsMap metrics_map_; |
+ base::RepeatingTimer<ProcessPowerCollector> timer_; |
+ |
+ // Callback to use to get CPU usage if set. |
+ CpuUsageCallback cpu_usage_callback_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ProcessPowerCollector); |
+}; |
+ |
+#endif // CHROME_BROWSER_POWER_PROCESS_POWER_COLLECTOR_H_ |