Debian Codegraph @

infra/bots/recipe_modules/flavor/android.py

raw
1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5
6 from recipe_engine import recipe_api
7 from recipe_engine import recipe_test_api
8
9 from . import default
10 import subprocess # TODO(borenet): No! Remove this.
11
12
13 """Android flavor, used for running code on Android."""
14
15
16 class AndroidFlavor(default.DefaultFlavor):
17 def __init__(self, m, app_name):
18 super(AndroidFlavor, self).__init__(m, app_name)
19 self._ever_ran_adb = False
20 if 'skia' in self.m.vars.swarming_bot_id:
21 self.ADB_BINARY = '/usr/bin/adb.1.0.35'
22 self.ADB_PUB_KEY = '/home/chrome-bot/.android/adbkey'
23 elif self.m.vars.swarming_bot_id.startswith('lin-'):
24 self.ADB_BINARY = '/opt/infra-android/tools/adb'
25 self.ADB_PUB_KEY = ('/home/chrome-bot/.android/'
26 'chrome_infrastructure_adbkey')
27 else:
28 self.ADB_BINARY = '/usr/bin/adb'
29 self.ADB_PUB_KEY = ''
30
31 # Data should go in android_data_dir, which may be preserved across runs.
32 android_data_dir = '/sdcard/revenge_of_the_skiabot/'
33 self.device_dirs = default.DeviceDirs(
34 bin_dir = '/data/local/tmp/',
35 dm_dir = android_data_dir + 'dm_out',
36 perf_data_dir = android_data_dir + 'perf',
37 resource_dir = android_data_dir + 'resources',
38 fonts_dir = 'NOT_SUPPORTED',
39 images_dir = android_data_dir + 'images',
40 lotties_dir = android_data_dir + 'lotties',
41 skp_dir = android_data_dir + 'skps',
42 svg_dir = android_data_dir + 'svgs',
43 tmp_dir = android_data_dir,
44 texttraces_dir = android_data_dir + 'text_blob_traces')
45
46 # A list of devices we can't root. If rooting fails and a device is not
47 # on the list, we fail the task to avoid perf inconsistencies.
48 self.cant_root = [
49 'GalaxyS7_G930FD',
50 'GalaxyS9',
51 'GalaxyS20',
52 'GalaxyS24',
53 'JioNext',
54 'MotoG4',
55 'MotoG73',
56 'P30',
57 'Pixel4',
58 'Pixel4XL',
59 'Pixel5',
60 'TecnoSpark3Pro',
61 ]
62
63 self.use_performance_governor_for_dm = [
64 'Pixel3a',
65 'Pixel4',
66 'Pixel4a',
67 'Wembley',
68 'Pixel6',
69 'Pixel7',
70 'Pixel7Pro',
71 'Pixel9',
72 'Pixel10',
73 ]
74
75 self.use_powersave_governor_for_nanobench = [
76 'Pixel6',
77 'Pixel7',
78 'Pixel7Pro',
79 'Pixel9',
80 'Pixel10',
81 ]
82
83 # Maps device type -> CPU ids that should be scaled for nanobench.
84 # Many devices have two (or more) different CPUs (e.g. big.LITTLE
85 # on Nexus5x). The CPUs listed are the biggest cpus on the device.
86 # The CPUs are grouped together, so we only need to scale one of them
87 # (the one listed) in order to scale them all.
88 # E.g. Nexus5x has cpu0-3 as one chip and cpu4-5 as the other. Thus,
89 # if one wants to run a single-threaded application (e.g. nanobench), one
90 # can disable cpu0-3 and scale cpu 4 to have only cpu4 and 5 at the same
91 # frequency. See also disable_for_nanobench.
92 self.cpus_to_scale = {
93 'Nexus5x': [4],
94 'Pixel': [2],
95 }
96
97 # Maps device type -> CPU ids that should be turned off when running
98 # single-threaded applications like nanobench. The devices listed have
99 # multiple, differnt CPUs. We notice a lot of noise that seems to be
100 # caused by nanobench running on the slow CPU, then the big CPU. By
101 # disabling this, we see less of that noise by forcing the same CPU
102 # to be used for the performance testing every time.
103 self.disable_for_nanobench = {
104 'Nexus5x': range(0, 4),
105 'Pixel': range(0, 2),
106 'Pixel6': range(4,8), # Only use the 4 small cores.
107 'Pixel7': range(4,8),
108 'Pixel9': range(4,8),
109 # 'Pixel10': range(1, 8), # TODO(borenet): Which cores should we disable?
110 }
111
112 self.gpu_scaling = {
113 "Nexus5": 450000000,
114 "Nexus5x": 600000000,
115 }
116
117 def wait_for_device(self):
118 cmd = [
119 'python3',
120 self.module.resource('wait_for_device.py'),
121 '--adb', self.ADB_BINARY,
122 ]
123 if self.m.vars.internal_hardware_label == '6':
124 cmd.extend(['--connect-to', 'variable_lab_dut_hostname'])
125 self.m.run(self.m.step,
126 'wait for device',
127 cmd=cmd,
128 timeout=600, abort_on_failure=False,
129 fail_build_on_failure=False)
130
131 def reboot_device_and_wait(self):
132 with self.m.step.nest('reboot device and wait'):
133 self.m.run(self.m.step,
134 'reboot device',
135 cmd=[self.ADB_BINARY, 'reboot'],
136 infra_step=True, timeout=30, abort_on_failure=False,
137 fail_build_on_failure=False)
138 self.wait_for_device()
139
140 def recover_device(self, failed_step, attempt):
141 title = 'recover device after failure of \'%s\' (attempt %d)' % (
142 failed_step, attempt)
143 with self.m.step.nest(title):
144 self.m.run(self.m.step,
145 'adb kill-server',
146 cmd=[self.ADB_BINARY, 'kill-server'],
147 infra_step=True, timeout=30, abort_on_failure=False,
148 fail_build_on_failure=False)
149 self.m.run(self.m.step,
150 'wait for device',
151 cmd=[self.ADB_BINARY, 'wait-for-device'], infra_step=True,
152 timeout=180, abort_on_failure=False,
153 fail_build_on_failure=False)
154 self.m.run(self.m.step,
155 'adb devices -l',
156 cmd=[self.ADB_BINARY, 'devices', '-l'],
157 infra_step=True, timeout=30, abort_on_failure=False,
158 fail_build_on_failure=False)
159 self.reboot_device_and_wait()
160
161 device = self.m.vars.builder_cfg.get('model')
162 if (device in self.cant_root): # pragma: nocover
163 return
164 self.m.run(self.m.step,
165 'adb root',
166 cmd=[
167 self.ADB_BINARY, 'root'
168 ],
169 timeout=180, abort_on_failure=False,
170 fail_build_on_failure=False)
171
172 def recover_device_after(self, title):
173 def rec(attempt):
174 self.recover_device(title, attempt)
175 return rec
176
177 def _adb(self, title, *cmd, **kwargs):
178 # The only non-infra adb steps (dm / nanobench) happen to not use _adb().
179 if 'infra_step' not in kwargs:
180 kwargs['infra_step'] = True
181
182 self._ever_ran_adb = True
183 # ADB seems to be occasionally flaky on every device, so always retry.
184 attempts = kwargs.pop('attempts', 3)
185
186 with self.m.context(cwd=self.m.path.start_dir.joinpath('skia')):
187 with self.m.env({'ADB_VENDOR_KEYS': self.ADB_PUB_KEY}):
188 if attempts == 1:
189 return self.m.run(self.m.step, title,
190 cmd=[self.ADB_BINARY]+list(cmd), **kwargs)
191 else:
192 return self.m.run.with_retry(self.m.step, title, attempts,
193 cmd=[self.ADB_BINARY]+list(cmd),
194 between_attempts_fn=self.recover_device_after(title),
195 **kwargs)
196
197 def _scale_for_dm(self):
198 device = self.m.vars.builder_cfg.get('model')
199 if (device in self.cant_root or
200 self.m.vars.internal_hardware_label):
201 return
202
203 # This is paranoia... any CPUs we disabled while running nanobench
204 # ought to be back online now that we've restarted the device.
205 for i in self.disable_for_nanobench.get(device, []):
206 self._set_cpu_online(i, 1) # enable
207
208 scale_up = self.cpus_to_scale.get(device, [0])
209 # For big.LITTLE devices, make sure we scale the LITTLE cores up;
210 # there is a chance they are still in powersave mode from when
211 # swarming slows things down for cooling down and charging.
212 if 0 not in scale_up:
213 scale_up.append(0)
214 for i in scale_up:
215 # AndroidOne doesn't support ondemand governor. hotplug is similar.
216 if device == 'AndroidOne':
217 self._set_governor(i, 'hotplug')
218 elif device in self.use_performance_governor_for_dm:
219 # Pixel3a/4/4a have userspace powersave performance schedutil.
220 # performance seems like a reasonable choice.
221 self._set_governor(i, 'performance')
222 else:
223 self._set_governor(i, 'ondemand')
224
225 def _scale_for_nanobench(self):
226 device = self.m.vars.builder_cfg.get('model')
227 if (device in self.cant_root or
228 self.m.vars.internal_hardware_label):
229 return
230
231 for i in self.cpus_to_scale.get(device, [0]):
232 if device in self.use_powersave_governor_for_nanobench:
233 self._set_governor(i, 'powersave')
234 elif device not in self.cant_root:
235 self._set_governor(i, 'userspace')
236 self._scale_cpu(i, 0.6)
237
238 for i in self.disable_for_nanobench.get(device, []):
239 self._set_cpu_online(i, 0) # disable
240
241 if device in self.gpu_scaling:
242 #https://developer.qualcomm.com/qfile/28823/lm80-p0436-11_adb_commands.pdf
243 # Section 3.2.1 Commands to put the GPU in performance mode
244 # Nexus 5 is 320000000 by default
245 # Nexus 5x is 180000000 by default
246 gpu_freq = self.gpu_scaling[device]
247 script = self.module.resource('set_gpu_scaling.py')
248 self.m.run.with_retry(self.m.step,
249 "Lock GPU to %d (and other perf tweaks)" % gpu_freq,
250 3, # attempts
251 cmd=['python3', script, self.ADB_BINARY, gpu_freq],
252 infra_step=True,
253 timeout=30)
254
255 def _set_governor(self, cpu, gov):
256 self._ever_ran_adb = True
257 script = self.module.resource('set_cpu_scaling_governor.py')
258 self.m.run.with_retry(self.m.step,
259 "Set CPU %d's governor to %s" % (cpu, gov),
260 3, # attempts
261 cmd=['python3', script, self.ADB_BINARY, cpu, gov],
262 infra_step=True,
263 timeout=30)
264
265
266 def _set_cpu_online(self, cpu, value):
267 """Set /sys/devices/system/cpu/cpu{N}/online to value (0 or 1)."""
268 self._ever_ran_adb = True
269 msg = 'Disabling'
270 if value:
271 msg = 'Enabling'
272
273 script = self.module.resource('set_cpu_online.py')
274 self.m.run.with_retry(self.m.step,
275 '%s CPU %d' % (msg, cpu),
276 3, # attempts
277 cmd=['python3', script, self.ADB_BINARY, cpu, value],
278 infra_step=True,
279 between_attempts_fn=self.recover_device_after("set cpu online"),
280 timeout=30)
281
282
283 def _scale_cpu(self, cpu, target_percent):
284 self._ever_ran_adb = True
285
286 script = self.module.resource('scale_cpu.py')
287 self.m.run.with_retry(self.m.step,
288 'Scale CPU %d to %f' % (cpu, target_percent),
289 3, # attempts
290 cmd=['python3', script, self.ADB_BINARY, str(target_percent), cpu],
291 infra_step=True,
292 between_attempts_fn=self.recover_device_after("scale cpu"),
293 timeout=30)
294
295
296 def _asan_setup_path(self):
297 return self.m.vars.workdir.joinpath(
298 'android_ndk_linux', 'toolchains', 'llvm', 'prebuilt', 'linux-x86_64',
299 'lib', 'clang', '18', 'bin', 'asan_device_setup')
300
301
302 def install(self):
303 self.wait_for_device()
304 self._adb('mkdir ' + self.device_dirs.resource_dir,
305 'shell', 'mkdir', '-p', self.device_dirs.resource_dir)
306 if self.m.vars.builder_cfg.get('model') in ('GalaxyS20', 'GalaxyS9'):
307 # See skbug.com/40041532, should be moot once upgraded to Android 11?
308 self._adb('cp libGLES_mali.so to ' + self.device_dirs.bin_dir,
309 'shell', 'cp',
310 '/vendor/lib64/egl/libGLES_mali.so',
311 self.device_dirs.bin_dir + 'libvulkan.so')
312 if 'ASAN' in self.m.vars.extra_tokens:
313 self._ever_ran_adb = True
314 script = self.module.resource('setup_device_for_asan.py')
315 self.m.run(
316 self.m.step, 'Setting up device to run ASAN',
317 cmd=['python3', script, self.ADB_BINARY, self._asan_setup_path()],
318 infra_step=True,
319 timeout=300,
320 abort_on_failure=True)
321 if self.app_name:
322 if (self.app_name == 'nanobench'):
323 self._scale_for_nanobench()
324 else:
325 self._scale_for_dm()
326 app_path = self.host_dirs.bin_dir.joinpath(self.app_name)
327 self._adb('push %s' % self.app_name,
328 'push', app_path, self.device_dirs.bin_dir)
329
330
331
332 def cleanup_steps(self):
333 script = self.module.resource('dump_adb_log.py')
334 self.m.run(self.m.step, 'dump log',
335 cmd=['python3', script, self.host_dirs.bin_dir, self.ADB_BINARY],
336 infra_step=True,
337 timeout=300,
338 abort_on_failure=False)
339
340 self.reboot_device_and_wait()
341
342 if 'ASAN' in self.m.vars.extra_tokens:
343 self._ever_ran_adb = True
344 # Remove ASAN.
345 self.m.run(self.m.step,
346 'wait for device before uninstalling ASAN',
347 cmd=[self.ADB_BINARY, 'wait-for-device', 'shell',
348 # Wait until the boot is actually complete.
349 # https://android.stackexchange.com/a/164050
350 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done',
351 ], infra_step=True,
352 timeout=180, abort_on_failure=False,
353 fail_build_on_failure=False)
354 self.m.run(self.m.step, 'uninstall ASAN',
355 cmd=[self._asan_setup_path(), '--revert'],
356 infra_step=True, timeout=300,
357 abort_on_failure=False, fail_build_on_failure=False)
358
359 if self._ever_ran_adb:
360 script = self.module.resource('dump_adb_log.py')
361 self.m.run(self.m.step, 'dump adb log',
362 cmd=['python3', script, self.host_dirs.bin_dir, self.ADB_BINARY],
363 infra_step=True,
364 timeout=300,
365 abort_on_failure=False)
366
367 # if self._ever_ran_adb:
368 # self._adb('kill adb server', 'kill-server')
369
370 def step(self, name, cmd):
371 sh = '%s.sh' % cmd[0]
372 self.m.run.writefile(self.m.vars.tmp_dir.joinpath(sh),
373 'set -x; LD_LIBRARY_PATH=%s %s%s; echo $? >%src' % (
374 self.device_dirs.bin_dir,
375 self.device_dirs.bin_dir, subprocess.list2cmdline(map(str, cmd)),
376 self.device_dirs.bin_dir))
377 self._adb('push %s' % sh,
378 'push', self.m.vars.tmp_dir.joinpath(sh), self.device_dirs.bin_dir)
379
380 self._adb('clear log', 'logcat', '-c')
381 script = self.module.resource('run_sh.py')
382 self.m.step('%s' % cmd[0],
383 cmd=['python3', script, self.device_dirs.bin_dir, sh, self.ADB_BINARY])
384
385 def copy_file_to_device(self, host, device):
386 self._adb('push %s %s' % (host, device), 'push', host, device)
387
388 def copy_directory_contents_to_device(self, host, device):
389 with self.m.step.nest('copy %s %s' % (host, device)):
390 contents = self.m.file.glob_paths('ls %s/*' % host,
391 host, '*',
392 test_data=['foo.png', 'bar.jpg'])
393 for file in contents:
394 self._adb('push %s %s' % (file, device), 'push', file, device)
395
396 def copy_directory_contents_to_host(self, device, host):
397 # TODO(borenet): When all of our devices are on Android 6.0 and up, we can
398 # switch to using tar to zip up the results before pulling.
399 with self.m.step.nest('adb pull'):
400 tmp = self.m.path.mkdtemp('adb_pull')
401 self._adb('pull %s' % device, 'pull', device, tmp)
402 paths = self.m.file.glob_paths(
403 'list pulled files',
404 tmp,
405 self.m.path.basename(device) + self.m.path.sep + '*',
406 test_data=['%d.png' % i for i in (1, 2)])
407 for p in paths:
408 self.m.file.copy('copy %s' % self.m.path.basename(p), p, host)
409
410 def read_file_on_device(self, path, **kwargs):
411 testKwargs = {
412 'attempts': 1,
413 'abort_on_failure': False,
414 'fail_build_on_failure': False,
415 }
416 rv = self._adb('check if %s exists' % path,
417 'shell', 'test', '-f', path, **testKwargs)
418 if not rv: # pragma: nocover
419 return None
420
421 rv = self._adb('read %s' % path,
422 'shell', 'cat', path, stdout=self.m.raw_io.output(),
423 **kwargs)
424 return rv.stdout.decode('utf-8').rstrip() if rv and rv.stdout else None
425
426 def remove_file_on_device(self, path):
427 script = self.module.resource('remove_file_on_device.py')
428 self.m.run.with_retry(self.m.step, 'rm %s' % path, 3,
429 cmd=['python3', script, self.ADB_BINARY, path],
430 infra_step=True)
431
432 def create_clean_device_dir(self, path):
433 self.remove_file_on_device(path)
434 self._adb('mkdir %s' % path, 'shell', 'mkdir', '-p', path)