| 1 |
# Copyright 2024 Google LLC |
| 2 |
# |
| 3 |
# Use of this source code is governed by a BSD-style license that can be |
| 4 |
# found in the LICENSE file. |
| 5 |
|
| 6 |
import os |
| 7 |
import subprocess |
| 8 |
import sys |
| 9 |
import time |
| 10 |
|
| 11 |
ADB = sys.argv[1] |
| 12 |
target_percent = float(sys.argv[2]) |
| 13 |
cpu = int(sys.argv[3]) |
| 14 |
log = subprocess.check_output([ADB, 'root']).decode('utf-8') |
| 15 |
# check for message like 'adbd cannot run as root in production builds' |
| 16 |
print(log) |
| 17 |
if 'cannot' in log: |
| 18 |
raise Exception('adb root failed') |
| 19 |
|
| 20 |
root = '/sys/devices/system/cpu/cpu%d/cpufreq' %cpu |
| 21 |
|
| 22 |
# All devices we test on give a list of their available frequencies. |
| 23 |
available_freqs = subprocess.check_output([ADB, 'shell', |
| 24 |
'cat %s/scaling_available_frequencies' % root]).decode('utf-8') |
| 25 |
|
| 26 |
# Check for message like '/system/bin/sh: file not found' |
| 27 |
if available_freqs and '/system/bin/sh' not in available_freqs: |
| 28 |
available_freqs = sorted( |
| 29 |
int(i) for i in available_freqs.strip().split()) |
| 30 |
else: |
| 31 |
raise Exception('Could not get list of available frequencies: %s' % |
| 32 |
available_freqs) |
| 33 |
|
| 34 |
maxfreq = available_freqs[-1] |
| 35 |
target = int(round(maxfreq * target_percent)) |
| 36 |
freq = maxfreq |
| 37 |
for f in reversed(available_freqs): |
| 38 |
if f <= target: |
| 39 |
freq = f |
| 40 |
break |
| 41 |
|
| 42 |
print('Setting frequency to %d' % freq) |
| 43 |
|
| 44 |
# If scaling_max_freq is lower than our attempted setting, it won't take. |
| 45 |
# We must set min first, because if we try to set max to be less than min |
| 46 |
# (which sometimes happens after certain devices reboot) it returns a |
| 47 |
# perplexing permissions error. |
| 48 |
subprocess.check_call([ADB, 'shell', 'echo 0 > ' |
| 49 |
'%s/scaling_min_freq' % root]) |
| 50 |
subprocess.check_call([ADB, 'shell', 'echo %d > ' |
| 51 |
'%s/scaling_max_freq' % (freq, root)]) |
| 52 |
subprocess.check_call([ADB, 'shell', 'echo %d > ' |
| 53 |
'%s/scaling_setspeed' % (freq, root)]) |
| 54 |
time.sleep(5) |
| 55 |
actual_freq = subprocess.check_output([ADB, 'shell', 'cat ' |
| 56 |
'%s/scaling_cur_freq' % root]).decode('utf-8').strip() |
| 57 |
if actual_freq != str(freq): |
| 58 |
raise Exception('(actual, expected) (%s, %d)' |
| 59 |
% (actual_freq, freq)) |