OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 json |
| 6 import logging |
| 7 import math |
5 import re | 8 import re |
6 import sys | 9 import sys |
7 | 10 |
8 import android_commands | 11 import android_commands |
9 import json | 12 |
10 import logging | 13 from perf import cache_control |
11 import math | 14 from perf import perf_control |
| 15 |
12 | 16 |
13 # Valid values of result type. | 17 # Valid values of result type. |
14 RESULT_TYPES = {'unimportant': 'RESULT ', | 18 RESULT_TYPES = {'unimportant': 'RESULT ', |
15 'default': '*RESULT ', | 19 'default': '*RESULT ', |
16 'informational': '', | 20 'informational': '', |
17 'unimportant-histogram': 'HISTOGRAM ', | 21 'unimportant-histogram': 'HISTOGRAM ', |
18 'histogram': '*HISTOGRAM '} | 22 'histogram': '*HISTOGRAM '} |
19 | 23 |
20 | 24 |
21 def _EscapePerfResult(s): | 25 def _EscapePerfResult(s): |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 if avg: | 143 if avg: |
140 output += '\nAvg %s: %f%s' % (measurement, avg, units) | 144 output += '\nAvg %s: %f%s' % (measurement, avg, units) |
141 if sd: | 145 if sd: |
142 output += '\nSd %s: %f%s' % (measurement, sd, units) | 146 output += '\nSd %s: %f%s' % (measurement, sd, units) |
143 if print_to_stdout: | 147 if print_to_stdout: |
144 print output | 148 print output |
145 sys.stdout.flush() | 149 sys.stdout.flush() |
146 return output | 150 return output |
147 | 151 |
148 | 152 |
149 class CacheControl(object): | 153 # TODO(bulach): remove once all references to PerfControl are fixed. |
150 _DROP_CACHES = '/proc/sys/vm/drop_caches' | 154 class CacheControl(cache_control.CacheControl): |
| 155 def __init__(self, adb): |
| 156 super(CacheControl, self).__init__(adb) |
151 | 157 |
| 158 class PerfControl(perf_control.PerfControl): |
152 def __init__(self, adb): | 159 def __init__(self, adb): |
153 self._adb = adb | 160 super(PerfControl, self).__init__(adb) |
154 | |
155 def DropRamCaches(self): | |
156 """Drops the filesystem ram caches for performance testing.""" | |
157 self._adb.RunShellCommand('su -c sync') | |
158 self._adb.SetProtectedFileContents(CacheControl._DROP_CACHES, '3') | |
159 | |
160 | |
161 class PerfControl(object): | |
162 """Provides methods for setting the performance mode of a device.""" | |
163 _SCALING_GOVERNOR_FMT = ( | |
164 '/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor') | |
165 | |
166 def __init__(self, adb): | |
167 self._adb = adb | |
168 kernel_max = self._adb.GetFileContents('/sys/devices/system/cpu/kernel_max', | |
169 log_result=False) | |
170 assert kernel_max, 'Unable to find /sys/devices/system/cpu/kernel_max' | |
171 self._kernel_max = int(kernel_max[0]) | |
172 logging.info('Maximum CPU index: %d' % self._kernel_max) | |
173 self._original_scaling_governor = self._adb.GetFileContents( | |
174 PerfControl._SCALING_GOVERNOR_FMT % 0, | |
175 log_result=False)[0] | |
176 | |
177 def SetHighPerfMode(self): | |
178 """Sets the highest possible performance mode for the device.""" | |
179 self._SetScalingGovernorInternal('performance') | |
180 | |
181 def SetDefaultPerfMode(self): | |
182 """Sets the performance mode for the device to its default mode.""" | |
183 product_model = self._adb.GetProductModel() | |
184 governor_mode = { | |
185 "GT-I9300" : 'pegasusq', | |
186 "Galaxy Nexus" : 'interactive', | |
187 "Nexus 4" : 'ondemand', | |
188 "Nexus 7" : 'interactive', | |
189 "Nexus 10": 'interactive' | |
190 }.get(product_model, 'ondemand') | |
191 self._SetScalingGovernorInternal(governor_mode) | |
192 | |
193 def RestoreOriginalPerfMode(self): | |
194 """Resets the original performance mode of the device.""" | |
195 self._SetScalingGovernorInternal(self._original_scaling_governor) | |
196 | |
197 def _SetScalingGovernorInternal(self, value): | |
198 for cpu in range(self._kernel_max + 1): | |
199 scaling_governor_file = PerfControl._SCALING_GOVERNOR_FMT % cpu | |
200 if self._adb.FileExistsOnDevice(scaling_governor_file): | |
201 logging.info('Writing scaling governor mode \'%s\' -> %s' % | |
202 (value, scaling_governor_file)) | |
203 self._adb.SetProtectedFileContents(scaling_governor_file, value) | |
OLD | NEW |