forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpower.py
196 lines (161 loc) · 7.2 KB
/
power.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import time
from telemetry.core.platform import process_statistic_timeline_data
from telemetry.value import scalar
from metrics import Metric
class PowerMetric(Metric):
"""A metric for measuring power usage."""
# System power draw while idle.
_quiescent_power_draw_mwh = 0
def __init__(self, platform, quiescent_measurement_time_s=0):
"""PowerMetric Constructor.
Args:
platform: platform object to use.
quiescent_measurement_time_s: time to measure quiescent power,
in seconds. 0 means don't measure quiescent power."""
super(PowerMetric, self).__init__()
self._browser = None
self._platform = platform
self._running = False
self._starting_cpu_stats = None
self._results = None
self._MeasureQuiescentPower(quiescent_measurement_time_s)
def __del__(self):
# TODO(jeremy): Remove once crbug.com/350841 is fixed.
# Don't leave power monitoring processes running on the system.
self._StopInternal()
parent = super(PowerMetric, self)
if hasattr(parent, '__del__'):
parent.__del__()
def _StopInternal(self):
"""Stop monitoring power if measurement is running. This function is
idempotent."""
if not self._running:
return
self._running = False
self._results = self._platform.StopMonitoringPower()
if self._results: # StopMonitoringPower() can return None.
self._results['cpu_stats'] = (
_SubtractCpuStats(self._browser.cpu_stats, self._starting_cpu_stats))
def _MeasureQuiescentPower(self, measurement_time_s):
"""Measure quiescent power draw for the system."""
if not self._platform.CanMonitorPower() or \
self._platform.CanMeasurePerApplicationPower() or \
not measurement_time_s:
return
# Only perform quiescent measurement once per run.
if PowerMetric._quiescent_power_draw_mwh:
return
self._platform.StartMonitoringPower(self._browser)
time.sleep(measurement_time_s)
power_results = self._platform.StopMonitoringPower()
PowerMetric._quiescent_power_draw_mwh = (
power_results.get('energy_consumption_mwh', 0))
def Start(self, _, tab):
self._browser = tab.browser
if not self._platform.CanMonitorPower():
return
self._results = None
self._StopInternal()
# This line invokes top a few times, call before starting power measurement.
self._starting_cpu_stats = self._browser.cpu_stats
self._platform.StartMonitoringPower(self._browser)
self._running = True
def Stop(self, _, tab):
if not self._platform.CanMonitorPower():
return
self._StopInternal()
def AddResults(self, _, results):
"""Add the collected power data into the results object.
This function needs to be robust in the face of differing power data on
various platforms. Therefore data existence needs to be checked when
building up the results. Additionally 0 is a valid value for many of the
metrics here which is why there are plenty of checks for 'is not None'
below.
"""
if not self._results:
return
application_energy_consumption_mwh = (
self._results.get('application_energy_consumption_mwh'))
total_energy_consumption_mwh = self._results.get('energy_consumption_mwh')
if (PowerMetric._quiescent_power_draw_mwh and
application_energy_consumption_mwh is None and
total_energy_consumption_mwh is not None):
application_energy_consumption_mwh = max(
total_energy_consumption_mwh - PowerMetric._quiescent_power_draw_mwh,
0)
if total_energy_consumption_mwh is not None:
results.AddValue(scalar.ScalarValue(
results.current_page, 'energy_consumption_mwh', 'mWh',
total_energy_consumption_mwh))
if application_energy_consumption_mwh is not None:
results.AddValue(scalar.ScalarValue(
results.current_page, 'application_energy_consumption_mwh', 'mWh',
application_energy_consumption_mwh))
component_utilization = self._results.get('component_utilization', {})
# GPU Frequency.
gpu_power = component_utilization.get('gpu', {})
gpu_freq_hz = gpu_power.get('average_frequency_hz')
if gpu_freq_hz is not None:
results.AddValue(scalar.ScalarValue(
results.current_page, 'gpu_average_frequency_hz', 'hz', gpu_freq_hz,
important=False))
# Add idle wakeup numbers for all processes.
for (process_type, stats) in self._results.get('cpu_stats', {}).items():
trace_name_for_process = 'idle_wakeups_%s' % (process_type.lower())
results.AddValue(scalar.ScalarValue(
results.current_page, trace_name_for_process, 'count', stats,
important=False))
# Add temperature measurements.
whole_package_utilization = component_utilization.get('whole_package', {})
board_temperature_c = whole_package_utilization.get('average_temperature_c')
if board_temperature_c is not None:
results.AddValue(scalar.ScalarValue(
results.current_page, 'board_temperature', 'celsius',
board_temperature_c, important=False))
# Add CPU frequency measurements.
frequency_hz = whole_package_utilization.get('frequency_percent')
if frequency_hz is not None:
frequency_sum = 0.0
for freq, percent in frequency_hz.iteritems():
frequency_sum += freq * (percent / 100.0)
results.AddValue(scalar.ScalarValue(
results.current_page, 'cpu_average_frequency_hz', 'Hz',
frequency_sum, important=False))
# Add CPU c-state residency measurements.
cstate_percent = whole_package_utilization.get('cstate_residency_percent')
if cstate_percent is not None:
for state, percent in cstate_percent.iteritems():
results.AddValue(scalar.ScalarValue(
results.current_page, 'cpu_cstate_%s_residency_percent' % state,
'%', percent, important=False))
self._results = None
def _SubtractCpuStats(cpu_stats, start_cpu_stats):
"""Computes number of idle wakeups that occurred over measurement period.
Each of the two cpu_stats arguments is a dict as returned by the
Browser.cpu_stats call.
Returns:
A dict of process type names (Browser, Renderer, etc.) to idle wakeup count
over the period recorded by the input.
"""
cpu_delta = {}
total = 0
for process_type in cpu_stats:
assert process_type in start_cpu_stats, 'Mismatching process types'
# Skip any process_types that are empty.
if (not cpu_stats[process_type]) or (not start_cpu_stats[process_type]):
continue
# Skip if IdleWakeupCount is not present.
if (('IdleWakeupCount' not in cpu_stats[process_type]) or
('IdleWakeupCount' not in start_cpu_stats[process_type])):
continue
assert isinstance(cpu_stats[process_type]['IdleWakeupCount'],
process_statistic_timeline_data.IdleWakeupTimelineData)
idle_wakeup_delta = (cpu_stats[process_type]['IdleWakeupCount'] -
start_cpu_stats[process_type]['IdleWakeupCount'])
cpu_delta[process_type] = idle_wakeup_delta.total_sum()
total = total + cpu_delta[process_type]
cpu_delta['Total'] = total
return cpu_delta