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..c81eecbbe5dc9bca3f7d163436f6ed7282e893fb |
--- /dev/null |
+++ b/chrome/browser/power/process_power_collector.h |
@@ -0,0 +1,145 @@ |
+// 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" |
+ |
+#if defined(OS_CHROMEOS) |
+#include "chromeos/dbus/power_manager/power_supply_properties.pb.h" |
+#include "chromeos/dbus/power_manager_client.h" |
+#endif |
+ |
+class Profile; |
+ |
+namespace content { |
+class RenderProcessHost; |
+} |
+ |
+// Manages regular updates of the profile power consumption. |
+class ProcessPowerCollector |
+#if defined(OS_CHROMEOS) |
+ : public chromeos::PowerManagerClient::Observer |
+#endif |
+ { |
+ 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(double 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. |
+ double 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(); |
+ virtual ~ProcessPowerCollector(); |
+ |
+#if defined(OS_CHROMEOS) |
+ // PowerManagerClient::Observer implementation: |
+ virtual void PowerChanged( |
+ const power_manager::PowerSupplyProperties& prop) OVERRIDE; |
+#endif |
+ |
+ // 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<double(base::ProcessHandle)> CpuUsageCallback; |
+ |
+ // Begin periodically updating the power consumption numbers by profile. |
+ // Can only be called after the DBusThreadManager has been initialized. |
Daniel Erat
2014/08/21 23:51:21
nit: start this sentence with "On Chrome OS, can o
Daniel Nishi
2014/08/22 01:06:19
Done.
|
+ void Initialize(); |
+ |
+ // Should only be called before DBusThreadManager is destroyed. |
+ void Shutdown(); |
Daniel Erat
2014/08/21 23:51:21
can you move this into the d'tor so you don't need
Daniel Nishi
2014/08/22 01:06:19
I tried that, but during the unittests the Browser
Daniel Erat
2014/08/22 03:13:49
the latter -- store the collector in a scoped_ptr
Daniel Nishi
2014/08/22 17:23:12
Done.
|
+ |
+ ProcessMetricsMap* metrics_map_for_testing() { return &metrics_map_; } |
+ |
+ void set_cpu_usage_callback_for_testing(const CpuUsageCallback& callback) { |
Daniel Erat
2014/08/21 23:51:21
nit: move this after the d'tor -- getters and sett
Daniel Nishi
2014/08/22 01:06:19
Done.
|
+ cpu_usage_callback_ = callback; |
+ } |
+ |
+ // Calls UpdatePowerConsumption() and returns the total CPU percent. |
+ double UpdatePowerConsumptionForTesting(); |
+ |
+ private: |
+ // Calls SynchronizerProcesses() and RecordCpuUsageByOrigin() to update the |
+ // |metrics_map_| and attribute power consumption. Invoked by |timer_| and as |
+ // a helper method for UpdatePowerConsumptionForTesting(). |
+ double UpdatePowerConsumption(); |
+ |
+ // Calls UpdatePowerConsumption(). Invoked by |timer_|. |
+ void HandleUpdateTimeout(); |
+ |
+ // 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_; |
+ |
+#if defined(OS_CHROMEOS) |
+ // If we should update the origin map. In Chrome OS, if the battery is |
+ // plugged in, we cannot get valid battery discharge information, so we cannot |
+ // update the power consumption. |
+ bool should_update; |
Daniel Erat
2014/08/21 23:51:21
nit: should_update_ (with trailing underscore)
yo
Daniel Nishi
2014/08/22 01:06:19
Nit done.
Ultimately, we'd like to be able to cap
|
+ // The battery discharge rate in W. |
+ double battery_discharge_rate_; |
+#endif |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ProcessPowerCollector); |
+}; |
+ |
+#endif // CHROME_BROWSER_POWER_PROCESS_POWER_COLLECTOR_H_ |