OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import time | 5 import time |
6 | 6 |
7 from metrics import Metric | 7 from metrics import Metric |
8 from telemetry.core.platform import process_statistic_timeline_data | 8 from telemetry.core.platform import process_statistic_timeline_data |
9 from telemetry.value import scalar | 9 from telemetry.value import scalar |
10 | 10 |
11 | 11 |
12 class PowerMetric(Metric): | 12 class PowerMetric(Metric): |
13 """A metric for measuring power usage.""" | 13 """A metric for measuring power usage.""" |
14 | 14 |
15 # System power draw while idle. | 15 # System power draw while idle. |
16 _quiescent_power_draw_mwh = 0 | 16 _quiescent_power_draw_mwh = 0 |
17 | 17 |
18 def __init__(self, platform, quiescent_measurement_time_s=0): | 18 def __init__(self, browser, quiescent_measurement_time_s=0): |
19 """PowerMetric Constructor. | 19 """PowerMetric Constructor. |
20 | 20 |
21 Args: | 21 Args: |
22 platform: platform object to use. | 22 browser: browser object to use. |
23 quiescent_measurement_time_s: time to measure quiescent power, | 23 quiescent_measurement_time_s: time to measure quiescent power, |
24 in seconds. 0 means don't measure quiescent power.""" | 24 in seconds. 0 means don't measure quiescent power.""" |
25 super(PowerMetric, self).__init__() | 25 super(PowerMetric, self).__init__() |
26 self._browser = None | 26 self._browser = browser |
27 self._platform = platform | |
28 self._running = False | 27 self._running = False |
29 self._starting_cpu_stats = None | 28 self._starting_cpu_stats = None |
30 self._results = None | 29 self._results = None |
31 self._MeasureQuiescentPower(quiescent_measurement_time_s) | 30 self._MeasureQuiescentPower(quiescent_measurement_time_s) |
32 | 31 |
33 def __del__(self): | 32 def __del__(self): |
34 # TODO(jeremy): Remove once crbug.com/350841 is fixed. | 33 # TODO(jeremy): Remove once crbug.com/350841 is fixed. |
35 # Don't leave power monitoring processes running on the system. | 34 # Don't leave power monitoring processes running on the system. |
36 self._StopInternal() | 35 self._StopInternal() |
37 parent = super(PowerMetric, self) | 36 parent = super(PowerMetric, self) |
38 if hasattr(parent, '__del__'): | 37 if hasattr(parent, '__del__'): |
39 parent.__del__() | 38 parent.__del__() |
40 | 39 |
41 def _StopInternal(self): | 40 def _StopInternal(self): |
42 """Stop monitoring power if measurement is running. This function is | 41 """Stop monitoring power if measurement is running. This function is |
43 idempotent.""" | 42 idempotent.""" |
44 if not self._running: | 43 if not self._running: |
45 return | 44 return |
46 self._running = False | 45 self._running = False |
47 self._results = self._platform.StopMonitoringPower() | 46 self._results = self._browser.platform.StopMonitoringPower() |
48 if self._results: # StopMonitoringPower() can return None. | 47 if self._results: # StopMonitoringPower() can return None. |
49 self._results['cpu_stats'] = ( | 48 self._results['cpu_stats'] = ( |
50 _SubtractCpuStats(self._browser.cpu_stats, self._starting_cpu_stats)) | 49 _SubtractCpuStats(self._browser.cpu_stats, self._starting_cpu_stats)) |
51 | 50 |
52 def _MeasureQuiescentPower(self, measurement_time_s): | 51 def _MeasureQuiescentPower(self, measurement_time_s): |
53 """Measure quiescent power draw for the system.""" | 52 """Measure quiescent power draw for the system.""" |
54 if not self._platform.CanMonitorPower() or \ | 53 platform = self._browser.platform |
55 self._platform.CanMeasurePerApplicationPower() or \ | 54 if not platform.CanMonitorPower() or \ |
| 55 platform.CanMeasurePerApplicationPower() or \ |
56 not measurement_time_s: | 56 not measurement_time_s: |
57 return | 57 return |
58 | 58 |
59 # Only perform quiescent measurement once per run. | 59 # Only perform quiescent measurement once per run. |
60 if PowerMetric._quiescent_power_draw_mwh: | 60 if PowerMetric._quiescent_power_draw_mwh: |
61 return | 61 return |
62 | 62 |
63 self._platform.StartMonitoringPower(self._browser) | 63 platform.StartMonitoringPower(self._browser) |
64 time.sleep(measurement_time_s) | 64 time.sleep(measurement_time_s) |
65 power_results = self._platform.StopMonitoringPower() | 65 power_results = platform.StopMonitoringPower() |
66 PowerMetric._quiescent_power_draw_mwh = ( | 66 PowerMetric._quiescent_power_draw_mwh = ( |
67 power_results.get('energy_consumption_mwh', 0)) | 67 power_results.get('energy_consumption_mwh', 0)) |
68 | 68 |
69 def Start(self, _, tab): | 69 def Start(self, _, tab): |
70 self._browser = tab.browser | 70 if not tab.browser.platform.CanMonitorPower(): |
71 | |
72 if not self._platform.CanMonitorPower(): | |
73 return | 71 return |
74 | 72 |
75 self._results = None | 73 self._results = None |
76 self._StopInternal() | 74 self._StopInternal() |
77 | 75 |
78 # This line invokes top a few times, call before starting power measurement. | 76 # This line invokes top a few times, call before starting power measurement. |
79 self._starting_cpu_stats = self._browser.cpu_stats | 77 self._starting_cpu_stats = self._browser.cpu_stats |
80 self._platform.StartMonitoringPower(self._browser) | 78 self._browser.platform.StartMonitoringPower(self._browser) |
81 self._running = True | 79 self._running = True |
82 | 80 |
83 def Stop(self, _, tab): | 81 def Stop(self, _, tab): |
84 if not self._platform.CanMonitorPower(): | 82 if not tab.browser.platform.CanMonitorPower(): |
85 return | 83 return |
86 | 84 |
87 self._StopInternal() | 85 self._StopInternal() |
88 | 86 |
89 def AddResults(self, _, results): | 87 def AddResults(self, _, results): |
90 """Add the collected power data into the results object. | 88 """Add the collected power data into the results object. |
91 | 89 |
92 This function needs to be robust in the face of differing power data on | 90 This function needs to be robust in the face of differing power data on |
93 various platforms. Therefore data existence needs to be checked when | 91 various platforms. Therefore data existence needs to be checked when |
94 building up the results. Additionally 0 is a valid value for many of the | 92 building up the results. Additionally 0 is a valid value for many of the |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 if (('IdleWakeupCount' not in cpu_stats[process_type]) or | 179 if (('IdleWakeupCount' not in cpu_stats[process_type]) or |
182 ('IdleWakeupCount' not in start_cpu_stats[process_type])): | 180 ('IdleWakeupCount' not in start_cpu_stats[process_type])): |
183 continue | 181 continue |
184 | 182 |
185 assert isinstance(cpu_stats[process_type]['IdleWakeupCount'], | 183 assert isinstance(cpu_stats[process_type]['IdleWakeupCount'], |
186 process_statistic_timeline_data.IdleWakeupTimelineData) | 184 process_statistic_timeline_data.IdleWakeupTimelineData) |
187 idle_wakeup_delta = (cpu_stats[process_type]['IdleWakeupCount'] - | 185 idle_wakeup_delta = (cpu_stats[process_type]['IdleWakeupCount'] - |
188 start_cpu_stats[process_type]['IdleWakeupCount']) | 186 start_cpu_stats[process_type]['IdleWakeupCount']) |
189 cpu_delta[process_type] = idle_wakeup_delta.total_sum() | 187 cpu_delta[process_type] = idle_wakeup_delta.total_sum() |
190 return cpu_delta | 188 return cpu_delta |
OLD | NEW |