Chromium Code Reviews| 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 base::ProcessMetrics* metrics() const { return metrics_.get(); } | |
| 35 Profile* profile() const { return profile_; } | |
| 36 GURL last_origin() const { return last_origin_; } | |
| 37 int last_cpu_percent() const { return last_cpu_percent_; } | |
| 38 bool seen_this_cycle() const { return seen_this_cycle_; } | |
| 39 void set_last_cpu_percent(int new_cpu) { last_cpu_percent_ = new_cpu; } | |
| 40 void set_seen_this_cycle(bool seen) { seen_this_cycle_ = seen; } | |
| 41 | |
| 42 private: | |
| 43 // |metrics_| holds the ProcessMetrics information for the given process. | |
| 44 scoped_ptr<base::ProcessMetrics> metrics_; | |
| 45 | |
| 46 // |profile| is the profile that is visiting the |last_origin_|. | |
| 47 // It is not owned by PerProcessData. | |
| 48 Profile* profile_; | |
| 49 | |
| 50 // |last_origin_| is the last origin visited by the process. | |
| 51 GURL last_origin_; | |
| 52 | |
| 53 // |last_cpu_percent_| is the proportion of the CPU used since the last | |
| 54 // query. | |
| 55 int last_cpu_percent_; | |
| 56 | |
| 57 // |seen_this_cycle| represents if the process still exists in this cycle. | |
| 58 // If it doesn't, we erase the PerProcessData. | |
| 59 bool seen_this_cycle_; | |
| 60 | |
| 61 DISALLOW_COPY_AND_ASSIGN(PerProcessData); | |
| 62 }; | |
| 63 | |
| 64 ProcessPowerCollector(); | |
| 65 ~ProcessPowerCollector(); | |
| 66 | |
| 67 // A map from all process handles to a metric. | |
| 68 typedef std::map<base::ProcessHandle, linked_ptr<PerProcessData> > | |
| 69 ProcessMetricsMap; | |
| 70 // A callback used to define mock CPU usage for testing. | |
| 71 typedef base::Callback<int(base::ProcessHandle)> CpuUsageCallback; | |
| 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 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.
| |
| 79 | |
| 80 // Returns the total CPU percent for the cycle. | |
| 81 double UpdatePowerConsumptionForTesting(); | |
| 82 | |
| 83 private: | |
| 84 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.
| |
| 85 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, OneSite); | |
| 86 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, MultipleSites); | |
| 87 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, | |
| 88 IncognitoDoesntRecordPowerUsage); | |
| 89 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, | |
| 90 MultipleProfilesRecordSeparately); | |
| 91 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, AppsRecordPowerUsage); | |
| 92 | |
| 93 // Callback from the timer to update. Invokes the update power consumption | |
| 94 // procedure. | |
| 95 void UpdatePowerConsumptionCallback(); | |
| 96 | |
| 97 // Synchronizes the currently active processes to the |metrics_map_| and | |
| 98 // returns the total amount of cpu usage in the cycle. | |
| 99 double SynchronizeProcesses(); | |
| 100 | |
| 101 // Attributes the power usage to the profiles and origins using the | |
| 102 // information from CollectCpuUsageByOrigin() given a total amount | |
| 103 // of CPU used in this cycle, |total_cpu_percent|. | |
| 104 void RecordCpuUsageByOrigin(double total_cpu_percent); | |
| 105 | |
| 106 // Adds the information from a given RenderProcessHost to the |metrics_map_| | |
| 107 // for a given origin. Called by SynchronizedProcesses(). | |
| 108 void UpdateProcessInMap(const content::RenderProcessHost* render_process, | |
| 109 const GURL& origin); | |
| 110 | |
| 111 ProcessMetricsMap metrics_map_; | |
| 112 base::RepeatingTimer<ProcessPowerCollector> timer_; | |
| 113 | |
| 114 // Callback to use to get CPU usage if set. | |
| 115 CpuUsageCallback cpu_usage_callback_; | |
| 116 | |
| 117 DISALLOW_COPY_AND_ASSIGN(ProcessPowerCollector); | |
| 118 }; | |
| 119 | |
| 120 #endif // CHROME_BROWSER_POWER_PROCESS_POWER_COLLECTOR_H_ | |
| OLD | NEW |