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 6import os 7import subprocess 8import sys 9import time 10 11ADB = sys.argv[1] 12target_percent = float(sys.argv[2]) 13cpu = int(sys.argv[3]) 14log = subprocess.check_output([ADB, 'root']).decode('utf-8') 15# check for message like 'adbd cannot run as root in production builds' 16print(log) 17if 'cannot' in log: 18 raise Exception('adb root failed') 19 20root = '/sys/devices/system/cpu/cpu%d/cpufreq' %cpu 21 22# All devices we test on give a list of their available frequencies. 23available_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' 27if available_freqs and '/system/bin/sh' not in available_freqs: 28 available_freqs = sorted( 29 int(i) for i in available_freqs.strip().split()) 30else: 31 raise Exception('Could not get list of available frequencies: %s' % 32 available_freqs) 33 34maxfreq = available_freqs[-1] 35target = int(round(maxfreq * target_percent)) 36freq = maxfreq 37for f in reversed(available_freqs): 38 if f <= target: 39 freq = f 40 break 41 42print('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. 48subprocess.check_call([ADB, 'shell', 'echo 0 > ' 49 '%s/scaling_min_freq' % root]) 50subprocess.check_call([ADB, 'shell', 'echo %d > ' 51 '%s/scaling_max_freq' % (freq, root)]) 52subprocess.check_call([ADB, 'shell', 'echo %d > ' 53 '%s/scaling_setspeed' % (freq, root)]) 54time.sleep(5) 55actual_freq = subprocess.check_output([ADB, 'shell', 'cat ' 56 '%s/scaling_cur_freq' % root]).decode('utf-8').strip() 57if actual_freq != str(freq): 58 raise Exception('(actual, expected) (%s, %d)' 59 % (actual_freq, freq)) 60