| 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, browser, quiescent_measurement_time_s=0): | 18 def __init__(self, platform, quiescent_measurement_time_s=0): |
| 19 """PowerMetric Constructor. | 19 """PowerMetric Constructor. |
| 20 | 20 |
| 21 Args: | 21 Args: |
| 22 browser: browser object to use. | 22 platform: platform 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 = browser | 26 self._browser = None |
| 27 self._platform = platform |
| 27 self._running = False | 28 self._running = False |
| 28 self._starting_cpu_stats = None | 29 self._starting_cpu_stats = None |
| 29 self._results = None | 30 self._results = None |
| 30 self._MeasureQuiescentPower(quiescent_measurement_time_s) | 31 self._MeasureQuiescentPower(quiescent_measurement_time_s) |
| 31 | 32 |
| 32 def __del__(self): | 33 def __del__(self): |
| 33 # TODO(jeremy): Remove once crbug.com/350841 is fixed. | 34 # TODO(jeremy): Remove once crbug.com/350841 is fixed. |
| 34 # Don't leave power monitoring processes running on the system. | 35 # Don't leave power monitoring processes running on the system. |
| 35 self._StopInternal() | 36 self._StopInternal() |
| 36 parent = super(PowerMetric, self) | 37 parent = super(PowerMetric, self) |
| 37 if hasattr(parent, '__del__'): | 38 if hasattr(parent, '__del__'): |
| 38 parent.__del__() | 39 parent.__del__() |
| 39 | 40 |
| 40 def _StopInternal(self): | 41 def _StopInternal(self): |
| 41 """Stop monitoring power if measurement is running. This function is | 42 """Stop monitoring power if measurement is running. This function is |
| 42 idempotent.""" | 43 idempotent.""" |
| 43 if not self._running: | 44 if not self._running: |
| 44 return | 45 return |
| 45 self._running = False | 46 self._running = False |
| 46 self._results = self._browser.platform.StopMonitoringPower() | 47 self._results = self._platform.StopMonitoringPower() |
| 47 if self._results: # StopMonitoringPower() can return None. | 48 if self._results: # StopMonitoringPower() can return None. |
| 48 self._results['cpu_stats'] = ( | 49 self._results['cpu_stats'] = ( |
| 49 _SubtractCpuStats(self._browser.cpu_stats, self._starting_cpu_stats)) | 50 _SubtractCpuStats(self._browser.cpu_stats, self._starting_cpu_stats)) |
| 50 | 51 |
| 51 def _MeasureQuiescentPower(self, measurement_time_s): | 52 def _MeasureQuiescentPower(self, measurement_time_s): |
| 52 """Measure quiescent power draw for the system.""" | 53 """Measure quiescent power draw for the system.""" |
| 53 platform = self._browser.platform | 54 if not self._platform.CanMonitorPower() or \ |
| 54 if not platform.CanMonitorPower() or \ | 55 self._platform.CanMeasurePerApplicationPower() 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 platform.StartMonitoringPower(self._browser) | 63 self._platform.StartMonitoringPower(self._browser) |
| 64 time.sleep(measurement_time_s) | 64 time.sleep(measurement_time_s) |
| 65 power_results = platform.StopMonitoringPower() | 65 power_results = self._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 if not tab.browser.platform.CanMonitorPower(): | 70 self._browser = tab.browser |
| 71 |
| 72 if not self._platform.CanMonitorPower(): |
| 71 return | 73 return |
| 72 | 74 |
| 73 self._results = None | 75 self._results = None |
| 74 self._StopInternal() | 76 self._StopInternal() |
| 75 | 77 |
| 76 # This line invokes top a few times, call before starting power measurement. | 78 # This line invokes top a few times, call before starting power measurement. |
| 77 self._starting_cpu_stats = self._browser.cpu_stats | 79 self._starting_cpu_stats = self._browser.cpu_stats |
| 78 self._browser.platform.StartMonitoringPower(self._browser) | 80 self._platform.StartMonitoringPower(self._browser) |
| 79 self._running = True | 81 self._running = True |
| 80 | 82 |
| 81 def Stop(self, _, tab): | 83 def Stop(self, _, tab): |
| 82 if not tab.browser.platform.CanMonitorPower(): | 84 if not self._platform.CanMonitorPower(): |
| 83 return | 85 return |
| 84 | 86 |
| 85 self._StopInternal() | 87 self._StopInternal() |
| 86 | 88 |
| 87 def AddResults(self, _, results): | 89 def AddResults(self, _, results): |
| 88 """Add the collected power data into the results object. | 90 """Add the collected power data into the results object. |
| 89 | 91 |
| 90 This function needs to be robust in the face of differing power data on | 92 This function needs to be robust in the face of differing power data on |
| 91 various platforms. Therefore data existence needs to be checked when | 93 various platforms. Therefore data existence needs to be checked when |
| 92 building up the results. Additionally 0 is a valid value for many of the | 94 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... |
| 179 if (('IdleWakeupCount' not in cpu_stats[process_type]) or | 181 if (('IdleWakeupCount' not in cpu_stats[process_type]) or |
| 180 ('IdleWakeupCount' not in start_cpu_stats[process_type])): | 182 ('IdleWakeupCount' not in start_cpu_stats[process_type])): |
| 181 continue | 183 continue |
| 182 | 184 |
| 183 assert isinstance(cpu_stats[process_type]['IdleWakeupCount'], | 185 assert isinstance(cpu_stats[process_type]['IdleWakeupCount'], |
| 184 process_statistic_timeline_data.IdleWakeupTimelineData) | 186 process_statistic_timeline_data.IdleWakeupTimelineData) |
| 185 idle_wakeup_delta = (cpu_stats[process_type]['IdleWakeupCount'] - | 187 idle_wakeup_delta = (cpu_stats[process_type]['IdleWakeupCount'] - |
| 186 start_cpu_stats[process_type]['IdleWakeupCount']) | 188 start_cpu_stats[process_type]['IdleWakeupCount']) |
| 187 cpu_delta[process_type] = idle_wakeup_delta.total_sum() | 189 cpu_delta[process_type] = idle_wakeup_delta.total_sum() |
| 188 return cpu_delta | 190 return cpu_delta |
| OLD | NEW |