Chromium Code Reviews| 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.value import scalar | 8 from telemetry.value import scalar |
| 9 | 9 |
| 10 | 10 |
| 11 class PowerMetric(Metric): | 11 class PowerMetric(Metric): |
| 12 """A metric for measuring power usage.""" | 12 """A metric for measuring power usage.""" |
| 13 | 13 |
| 14 # System power draw while idle. | |
| 15 _quiescent_power_draw_mwh = 0 | |
|
Dominik Grewe
2014/07/08 13:05:19
Would it make sense to set this to 'None' here to
| |
| 16 | |
| 14 def __init__(self, browser, quiescent_measurement_time_s=0): | 17 def __init__(self, browser, quiescent_measurement_time_s=0): |
| 15 """PowerMetric Constructor. | 18 """PowerMetric Constructor. |
| 16 | 19 |
| 17 Args: | 20 Args: |
| 18 browser: browser object to use. | 21 browser: browser object to use. |
| 19 quiescent_measurement_time_s: time to measure quiescent power, | 22 quiescent_measurement_time_s: time to measure quiescent power, |
| 20 in seconds. 0 means don't measure quiescent power.""" | 23 in seconds. 0 means don't measure quiescent power.""" |
| 21 super(PowerMetric, self).__init__() | 24 super(PowerMetric, self).__init__() |
| 22 self._browser = browser | 25 self._browser = browser |
| 23 self._running = False | 26 self._running = False |
| 24 self._starting_cpu_stats = None | 27 self._starting_cpu_stats = None |
| 25 self._results = None | 28 self._results = None |
| 26 self._quiescent_power_draw_mwh = 0 | |
| 27 self._MeasureQuiescentPower(quiescent_measurement_time_s) | 29 self._MeasureQuiescentPower(quiescent_measurement_time_s) |
| 28 | 30 |
| 29 def __del__(self): | 31 def __del__(self): |
| 30 # TODO(jeremy): Remove once crbug.com/350841 is fixed. | 32 # TODO(jeremy): Remove once crbug.com/350841 is fixed. |
| 31 # Don't leave power monitoring processes running on the system. | 33 # Don't leave power monitoring processes running on the system. |
| 32 self._StopInternal() | 34 self._StopInternal() |
| 33 parent = super(PowerMetric, self) | 35 parent = super(PowerMetric, self) |
| 34 if hasattr(parent, '__del__'): | 36 if hasattr(parent, '__del__'): |
| 35 parent.__del__() | 37 parent.__del__() |
| 36 | 38 |
| 37 def _StopInternal(self): | 39 def _StopInternal(self): |
| 38 """Stop monitoring power if measurement is running. This function is | 40 """Stop monitoring power if measurement is running. This function is |
| 39 idempotent.""" | 41 idempotent.""" |
| 40 if not self._running: | 42 if not self._running: |
| 41 return | 43 return |
| 42 self._running = False | 44 self._running = False |
| 43 self._results = self._browser.platform.StopMonitoringPower() | 45 self._results = self._browser.platform.StopMonitoringPower() |
| 44 if self._results: # StopMonitoringPower() can return None. | 46 if self._results: # StopMonitoringPower() can return None. |
| 45 self._results['cpu_stats'] = ( | 47 self._results['cpu_stats'] = ( |
| 46 _SubtractCpuStats(self._browser.cpu_stats, self._starting_cpu_stats)) | 48 _SubtractCpuStats(self._browser.cpu_stats, self._starting_cpu_stats)) |
| 47 | 49 |
| 48 def _MeasureQuiescentPower(self, measurement_time_s): | 50 def _MeasureQuiescentPower(self, measurement_time_s): |
| 49 """Measure quiescent power draw for the system.""" | 51 """Measure quiescent power draw for the system.""" |
| 50 platform = self._browser.platform | 52 platform = self._browser.platform |
| 51 if not platform.CanMonitorPower() or \ | 53 if not platform.CanMonitorPower() or \ |
| 52 platform.CanMeasurePerApplicationPower() or \ | 54 platform.CanMeasurePerApplicationPower() or \ |
| 53 not measurement_time_s: | 55 not measurement_time_s: |
| 54 return | 56 return |
| 55 | 57 |
| 58 # Only perform quiescent measurement once per run. | |
| 59 if PowerMetric._quiescent_power_draw_mwh: | |
|
Dominik Grewe
2014/07/08 13:05:19
I think I'd do this check in __init__ but that's j
| |
| 60 return | |
| 61 | |
| 56 platform.StartMonitoringPower(self._browser) | 62 platform.StartMonitoringPower(self._browser) |
| 57 time.sleep(measurement_time_s) | 63 time.sleep(measurement_time_s) |
| 58 power_results = platform.StopMonitoringPower() | 64 power_results = platform.StopMonitoringPower() |
| 59 self._quiescent_power_draw_mwh = ( | 65 PowerMetric._quiescent_power_draw_mwh = ( |
| 60 power_results.get('energy_consumption_mwh', 0)) | 66 power_results.get('energy_consumption_mwh', 0)) |
| 61 | 67 |
| 62 def Start(self, _, tab): | 68 def Start(self, _, tab): |
| 63 if not tab.browser.platform.CanMonitorPower(): | 69 if not tab.browser.platform.CanMonitorPower(): |
| 64 return | 70 return |
| 65 | 71 |
| 66 self._results = None | 72 self._results = None |
| 67 self._StopInternal() | 73 self._StopInternal() |
| 68 | 74 |
| 69 # This line invokes top a few times, call before starting power measurement. | 75 # This line invokes top a few times, call before starting power measurement. |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 88 """ | 94 """ |
| 89 if not self._results: | 95 if not self._results: |
| 90 return | 96 return |
| 91 | 97 |
| 92 application_energy_consumption_mwh = ( | 98 application_energy_consumption_mwh = ( |
| 93 self._results.get('application_energy_consumption_mwh')) | 99 self._results.get('application_energy_consumption_mwh')) |
| 94 total_energy_consumption_mwh = self._results.get('energy_consumption_mwh') | 100 total_energy_consumption_mwh = self._results.get('energy_consumption_mwh') |
| 95 | 101 |
| 96 if not application_energy_consumption_mwh and total_energy_consumption_mwh: | 102 if not application_energy_consumption_mwh and total_energy_consumption_mwh: |
| 97 application_energy_consumption_mwh = max( | 103 application_energy_consumption_mwh = max( |
| 98 total_energy_consumption_mwh - self._quiescent_power_draw_mwh, 0) | 104 total_energy_consumption_mwh - PowerMetric._quiescent_power_draw_mwh, |
| 105 0) | |
| 99 | 106 |
| 100 if total_energy_consumption_mwh is not None: | 107 if total_energy_consumption_mwh is not None: |
| 101 results.AddValue(scalar.ScalarValue( | 108 results.AddValue(scalar.ScalarValue( |
| 102 results.current_page, 'energy_consumption_mwh', 'mWh', | 109 results.current_page, 'energy_consumption_mwh', 'mWh', |
| 103 total_energy_consumption_mwh)) | 110 total_energy_consumption_mwh)) |
| 104 | 111 |
| 105 if application_energy_consumption_mwh is not None: | 112 if application_energy_consumption_mwh is not None: |
| 106 results.AddValue(scalar.ScalarValue( | 113 results.AddValue(scalar.ScalarValue( |
| 107 results.current_page, 'application_energy_consumption_mwh', 'mWh', | 114 results.current_page, 'application_energy_consumption_mwh', 'mWh', |
| 108 application_energy_consumption_mwh)) | 115 application_energy_consumption_mwh)) |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 if (not cpu_stats[process_type]) or (not start_cpu_stats[process_type]): | 157 if (not cpu_stats[process_type]) or (not start_cpu_stats[process_type]): |
| 151 continue | 158 continue |
| 152 # Skip if IdleWakeupCount is not present. | 159 # Skip if IdleWakeupCount is not present. |
| 153 if (('IdleWakeupCount' not in cpu_stats[process_type]) or | 160 if (('IdleWakeupCount' not in cpu_stats[process_type]) or |
| 154 ('IdleWakeupCount' not in start_cpu_stats[process_type])): | 161 ('IdleWakeupCount' not in start_cpu_stats[process_type])): |
| 155 continue | 162 continue |
| 156 idle_wakeup_delta = (cpu_stats[process_type]['IdleWakeupCount'] - | 163 idle_wakeup_delta = (cpu_stats[process_type]['IdleWakeupCount'] - |
| 157 start_cpu_stats[process_type]['IdleWakeupCount']) | 164 start_cpu_stats[process_type]['IdleWakeupCount']) |
| 158 cpu_delta[process_type] = idle_wakeup_delta | 165 cpu_delta[process_type] = idle_wakeup_delta |
| 159 return cpu_delta | 166 return cpu_delta |
| OLD | NEW |