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 ProcessPowerCollector(); | |
| 27 ~ProcessPowerCollector(); | |
| 28 | |
| 29 class PerProcessData { | |
| 30 public: | |
| 31 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.
| |
| 32 const GURL& origin, | |
| 33 Profile* profile); | |
| 34 PerProcessData(); | |
| 35 ~PerProcessData(); | |
| 36 | |
| 37 // Accessors | |
| 38 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
| |
| 39 Profile* profile() const { return profile_; } | |
| 40 GURL last_origin() const { return last_origin_; } | |
| 41 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.
| |
| 42 | |
| 43 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.
| |
| 44 | |
| 45 private: | |
| 46 // |metrics_| holds the ProcessMetrics information for the given process. | |
| 47 scoped_ptr<base::ProcessMetrics> metrics_; | |
| 48 | |
| 49 // |profile| is the profile that is visiting the |last_origin_|. | |
| 50 // It is not owned by PerProcessData. | |
| 51 Profile* profile_; | |
| 52 // |last_origin_| is the last origin visited by the process. | |
| 53 GURL last_origin_; | |
| 54 // |last_cpu_percent_| is the proportion of the CPU used since the last | |
| 55 // query. | |
| 56 int last_cpu_percent_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(PerProcessData); | |
| 59 }; | |
| 60 | |
| 61 // A map from all process handles to a metric. | |
| 62 typedef std::map<base::ProcessHandle, linked_ptr<PerProcessData> > | |
| 63 ProcessMetricsMap; | |
| 64 | |
| 65 // Begin periodically updating the power consumption numbers by profile. | |
| 66 void StartUpdating(); | |
| 67 | |
| 68 ProcessMetricsMap* metrics_map_for_testing() { return &metrics_map_; } | |
| 69 | |
| 70 private: | |
| 71 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, NoSite); | |
| 72 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, OneSite); | |
| 73 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, MultipleSites); | |
| 74 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, | |
| 75 IncognitoDoesntRecordPowerUsage); | |
| 76 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, | |
| 77 MultipleProfilesRecordSeparately); | |
| 78 FRIEND_TEST_ALL_PREFIXES(BrowserProcessPowerTest, AppsRecordPowerUsage); | |
| 79 | |
| 80 // Callback from the timer to update. Invokes the update power consumption | |
| 81 // procedure. | |
| 82 void UpdatePowerConsumptionCallback(); | |
| 83 | |
| 84 // Updates the |metrics_map_| with currently active processes. | |
| 85 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.
| |
| 86 | |
| 87 // Updates the |metrics_map_| with updated CPU usage information and | |
| 88 // removes all inactive processes from the |metrics_map_|. | |
| 89 double PopulateCpuUsageByOrigin(); | |
|
Daniel Erat
2014/08/20 00:10:42
s/Populate/Collect/, maybe?
Daniel Nishi
2014/08/20 17:16:46
Done.
| |
| 90 | |
| 91 // 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.
| |
| 92 // of CPU used in this cycle, |cpu_cycle|. | |
| 93 void RecordCpuUsageByOrigin(double cpu_cycle); | |
| 94 | |
| 95 // Iterates over all tabs to determine the power usage since the last sweep. | |
| 96 void UpdatePowerConsumption(); | |
| 97 | |
| 98 // Adds the information from a given RenderProcessHost to the |metrics_map_| | |
| 99 // for a given origin. | |
| 100 void AddProcessToMap(const content::RenderProcessHost* render_process, | |
| 101 const GURL& origin); | |
| 102 | |
| 103 ProcessMetricsMap metrics_map_; | |
| 104 base::RepeatingTimer<ProcessPowerCollector> timer_; | |
| 105 | |
| 106 DISALLOW_COPY_AND_ASSIGN(ProcessPowerCollector); | |
| 107 }; | |
| 108 | |
| 109 #endif // CHROME_BROWSER_POWER_PROCESS_POWER_COLLECTOR_H_ | |
| OLD | NEW |