Debian Codegraph @

infra/bots/recipes/perf_skottietrace.py

raw
1 # Copyright 2019 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 # Recipe which runs DM with trace flag on lottie files and then parses the
6 # trace output into output JSON files to ingest to perf.skia.org.
7 # Design doc: go/skottie-tracing
8
9
10 import calendar
11 import json
12 import re
13 import string
14
15
16 DEPS = [
17 'flavor',
18 'infra',
19 'recipe_engine/context',
20 'recipe_engine/file',
21 'recipe_engine/json',
22 'recipe_engine/path',
23 'recipe_engine/step',
24 'recipe_engine/time',
25 'recipe_engine/properties',
26 'recipe_engine/raw_io',
27 'run',
28 'vars',
29 ]
30
31 SEEK_TRACE_NAME = 'skottie::Animation::seek'
32 RENDER_TRACE_NAME = 'skottie::Animation::render'
33 EXPECTED_DM_FRAMES = 25
34
35
36 def perf_steps(api):
37 """Run DM on lottie files with tracing turned on and then parse the output."""
38 api.flavor.create_clean_device_dir(
39 api.flavor.device_dirs.dm_dir)
40 lotties_host = api.path.start_dir.joinpath('lotties_with_assets')
41 lotties_device = api.path.start_dir.joinpath('lotties_with_assets')
42 if 'Android' in api.vars.builder_cfg.get('extra_config'):
43 # Due to http://b/72366966 and the fact that CIPD symlinks files in by default, we ran into
44 # a strange "Function not implemented" error when trying to copy folders that contained
45 # symlinked files. It is not easy to change the CIPD "InstallMode" from symlink to copy, so
46 # we use shutil (file.copytree) to make a local copy of the files on the host, which removes
47 # the symlinks and adb push --sync works as expected.
48 lotties_device = api.flavor.device_path_join(api.flavor.device_dirs.tmp_dir, 'lotties_with_assets')
49 api.flavor.create_clean_device_dir(lotties_device)
50
51 # Make a temp directory and then copy to a *non-existing* subfolder (otherwise copytree crashes).
52 lotties_no_symlinks = api.path.mkdtemp('lwa').joinpath('nosymlinks')
53 api.file.copytree('Copying files on host to remove symlinks', lotties_host, lotties_no_symlinks)
54 lotties_host = lotties_no_symlinks
55 api.flavor.copy_directory_contents_to_device(lotties_host, lotties_device)
56
57 # We expect this to be a bunch of folders that contain a data.json and optionally
58 # an images/ subfolder with image assets required.
59 lottie_files = api.file.listdir(
60 'list lottie files', lotties_host,
61 test_data=['skottie_asset_000', 'skottie_asset_001', 'skottie_asset_002'])
62 perf_results = {}
63 # Run DM on each lottie file and parse the trace files.
64 for idx, lottie_file in enumerate(lottie_files):
65 lottie_name = api.path.basename(lottie_file)
66 lottie_folder = api.flavor.device_path_join(lotties_device, lottie_name)
67
68 trace_output_path = api.flavor.device_path_join(
69 api.flavor.device_dirs.dm_dir, '%s.json' % (idx + 1))
70 # See go/skottie-tracing for how these flags were selected.
71 dm_args = [
72 'dm',
73 '--resourcePath', api.flavor.device_dirs.resource_dir,
74 '--lotties', lottie_folder,
75 '--src', 'lottie',
76 '--nonativeFonts',
77 '--verbose',
78 '--traceMatch', 'skottie', # recipe can OOM without this.
79 '--trace', trace_output_path,
80 '--match', get_trace_match(
81 'data.json', 'Android' in api.properties['buildername']),
82 ]
83 if api.vars.builder_cfg.get('cpu_or_gpu') == 'GPU':
84 dm_args.extend(['--config', 'gles', '--nocpu'])
85 elif api.vars.builder_cfg.get('cpu_or_gpu') == 'CPU':
86 dm_args.extend(['--config', '8888', '--nogpu'])
87 api.run(api.flavor.step, 'dm', cmd=dm_args, abort_on_failure=False)
88
89 trace_test_data = api.properties.get('trace_test_data', '{}')
90 trace_file_content = api.flavor.read_file_on_device(trace_output_path)
91 if not trace_file_content and trace_test_data:
92 trace_file_content = trace_test_data
93
94 key = 'gles'
95 if api.vars.builder_cfg.get('cpu_or_gpu') == 'CPU':
96 key = '8888'
97 perf_results[lottie_name] = {
98 key: parse_trace(trace_file_content, lottie_name, api),
99 }
100 api.flavor.remove_file_on_device(trace_output_path)
101
102 # Construct contents of the output JSON.
103 perf_json = {
104 'gitHash': api.properties['revision'],
105 'swarming_bot_id': api.vars.swarming_bot_id,
106 'swarming_task_id': api.vars.swarming_task_id,
107 'renderer': 'skottie',
108 'key': {
109 'bench_type': 'tracing',
110 'source_type': 'skottie',
111 },
112 'results': perf_results,
113 }
114 if api.vars.is_trybot:
115 perf_json['issue'] = api.vars.issue
116 perf_json['patchset'] = api.vars.patchset
117 perf_json['patch_storage'] = api.vars.patch_storage
118 # Add tokens from the builder name to the key.
119 reg = re.compile('Perf-(?P<os>[A-Za-z0-9_]+)-'
120 '(?P<compiler>[A-Za-z0-9_]+)-'
121 '(?P<model>[A-Za-z0-9_]+)-'
122 '(?P<cpu_or_gpu>[A-Z]+)-'
123 '(?P<cpu_or_gpu_value>[A-Za-z0-9_]+)-'
124 '(?P<arch>[A-Za-z0-9_]+)-'
125 '(?P<configuration>[A-Za-z0-9_]+)-'
126 'All(-(?P<extra_config>[A-Za-z0-9_]+)|)')
127 m = reg.match(api.properties['buildername'])
128 keys = ['os', 'compiler', 'model', 'cpu_or_gpu', 'cpu_or_gpu_value', 'arch',
129 'configuration', 'extra_config']
130 for k in keys:
131 perf_json['key'][k] = m.group(k)
132
133 # Create the output JSON file in perf_data_dir for the Upload task to upload.
134 api.file.ensure_directory(
135 'makedirs perf_dir',
136 api.flavor.host_dirs.perf_data_dir)
137 now = api.time.utcnow()
138 ts = int(calendar.timegm(now.utctimetuple()))
139 json_path = api.flavor.host_dirs.perf_data_dir.joinpath(
140 'perf_%s_%d.json' % (api.properties['revision'], ts))
141 json_contents = json.dumps(
142 perf_json, indent=4, sort_keys=True, separators=(',', ': '))
143 api.file.write_text('write output JSON', json_path, json_contents)
144
145
146 def get_trace_match(lottie_filename, is_android):
147 """Returns the DM regex to match the specified lottie file name."""
148 trace_match = '^%s$' % lottie_filename
149 if is_android and ' ' not in trace_match:
150 # Punctuation characters confuse DM when shelled out over adb, so escape
151 # them. Do not need to do this when there is a space in the match because
152 # subprocess.list2cmdline automatically adds quotes in that case.
153 for sp_char in string.punctuation:
154 if sp_char == '\\':
155 # No need to escape the escape char.
156 continue
157 trace_match = trace_match.replace(sp_char, '\%s' % sp_char)
158 return trace_match
159
160
161 def parse_trace(trace_json, lottie_filename, api):
162 """parse_trace parses the specified trace JSON.
163
164 Parses the trace JSON and calculates the time of a single frame. Frame time is
165 considered the same as seek time + render time.
166 Note: The first seek is ignored because it is a constructor call.
167
168 A dictionary is returned that has the following structure:
169 {
170 'frame_max_us': 100,
171 'frame_min_us': 90,
172 'frame_avg_us': 95,
173 }
174 """
175 script = api.infra.resource('parse_skottie_trace.py')
176 step_result = api.run(
177 api.step,
178 'parse %s trace' % lottie_filename,
179 cmd=['python3', script, trace_json, lottie_filename, api.json.output(),
180 SEEK_TRACE_NAME, RENDER_TRACE_NAME, EXPECTED_DM_FRAMES])
181
182 # Sanitize float outputs to 2 precision points.
183 output = dict(step_result.json.output)
184 output['frame_max_us'] = float("%.2f" % output['frame_max_us'])
185 output['frame_min_us'] = float("%.2f" % output['frame_min_us'])
186 output['frame_avg_us'] = float("%.2f" % output['frame_avg_us'])
187 return output
188
189
190 def RunSteps(api):
191 api.vars.setup()
192 api.file.ensure_directory('makedirs tmp_dir', api.vars.tmp_dir)
193 api.flavor.setup('dm')
194
195 with api.context():
196 try:
197 api.flavor.install(resources=True, lotties=True)
198 perf_steps(api)
199 finally:
200 api.flavor.cleanup_steps()
201 api.run.check_failure()
202
203
204 def GenTests(api):
205 trace_output = """
206 [{"ph":"X","name":"void skottie::Animation::seek(SkScalar)","ts":452,"dur":2.57,"tid":1,"pid":0},{"ph":"X","name":"void SkCanvas::drawPaint(const SkPaint &)","ts":473,"dur":2.67e+03,"tid":1,"pid":0},{"ph":"X","name":"void skottie::Animation::seek(SkScalar)","ts":3.15e+03,"dur":2.25,"tid":1,"pid":0},{"ph":"X","name":"void skottie::Animation::render(SkCanvas *, const SkRect *, RenderFlags) const","ts":3.15e+03,"dur":216,"tid":1,"pid":0},{"ph":"X","name":"void SkCanvas::drawPath(const SkPath &, const SkPaint &)","ts":3.35e+03,"dur":15.1,"tid":1,"pid":0},{"ph":"X","name":"void skottie::Animation::seek(SkScalar)","ts":3.37e+03,"dur":1.17,"tid":1,"pid":0},{"ph":"X","name":"void skottie::Animation::render(SkCanvas *, const SkRect *, RenderFlags) const","ts":3.37e+03,"dur":140,"tid":1,"pid":0}]
207 """
208 dm_json_test_data = """
209 {
210 "gitHash": "bac53f089dbc473862bc5a2e328ba7600e0ed9c4",
211 "swarming_bot_id": "skia-rpi-094",
212 "swarming_task_id": "438f11c0e19eab11",
213 "key": {
214 "arch": "arm",
215 "compiler": "Clang",
216 "cpu_or_gpu": "GPU",
217 "cpu_or_gpu_value": "Mali400MP2",
218 "extra_config": "Android",
219 "model": "AndroidOne",
220 "os": "Android"
221 },
222 "results": {
223 }
224 }
225 """
226 parse_trace_json = {
227 'frame_avg_us': 179.71,
228 'frame_min_us': 141.17,
229 'frame_max_us': 218.25
230 }
231 android_buildername = ('Perf-Android-Clang-AndroidOne-GPU-Mali400MP2-arm-'
232 'Release-All-Android_SkottieTracing')
233 gpu_buildername = ('Perf-Debian10-Clang-NUC7i5BNK-GPU-IntelIris640-x86_64-'
234 'Release-All-SkottieTracing')
235 cpu_buildername = ('Perf-Debian10-Clang-GCE-CPU-AVX2-x86_64-Release-All-'
236 'SkottieTracing')
237 yield (
238 api.test(android_buildername) +
239 api.properties(buildername=android_buildername,
240 repository='https://skia.googlesource.com/skia.git',
241 revision='abc123',
242 task_id='abc123',
243 trace_test_data=trace_output,
244 dm_json_test_data=dm_json_test_data,
245 path_config='kitchen',
246 swarm_out_dir='[SWARM_OUT_DIR]') +
247 api.step_data('parse skottie_asset_000 trace',
248 api.json.output(parse_trace_json)) +
249 api.step_data('parse skottie_asset_001 trace',
250 api.json.output(parse_trace_json)) +
251 api.step_data('parse skottie_asset_002 trace',
252 api.json.output(parse_trace_json))
253 )
254 yield (
255 api.test(gpu_buildername) +
256 api.properties(buildername=gpu_buildername,
257 repository='https://skia.googlesource.com/skia.git',
258 revision='abc123',
259 task_id='abc123',
260 trace_test_data=trace_output,
261 dm_json_test_data=dm_json_test_data,
262 path_config='kitchen',
263 swarm_out_dir='[SWARM_OUT_DIR]') +
264 api.step_data('parse skottie_asset_000 trace',
265 api.json.output(parse_trace_json)) +
266 api.step_data('parse skottie_asset_001 trace',
267 api.json.output(parse_trace_json)) +
268 api.step_data('parse skottie_asset_002 trace',
269 api.json.output(parse_trace_json))
270 )
271 yield (
272 api.test(cpu_buildername) +
273 api.properties(buildername=cpu_buildername,
274 repository='https://skia.googlesource.com/skia.git',
275 revision='abc123',
276 task_id='abc123',
277 trace_test_data=trace_output,
278 dm_json_test_data=dm_json_test_data,
279 path_config='kitchen',
280 swarm_out_dir='[SWARM_OUT_DIR]') +
281 api.step_data('parse skottie_asset_000 trace',
282 api.json.output(parse_trace_json)) +
283 api.step_data('parse skottie_asset_001 trace',
284 api.json.output(parse_trace_json)) +
285 api.step_data('parse skottie_asset_002 trace',
286 api.json.output(parse_trace_json))
287 )
288 yield (
289 api.test('skottietracing_parse_trace_error') +
290 api.properties(buildername=android_buildername,
291 repository='https://skia.googlesource.com/skia.git',
292 revision='abc123',
293 task_id='abc123',
294 trace_test_data=trace_output,
295 dm_json_test_data=dm_json_test_data,
296 path_config='kitchen',
297 swarm_out_dir='[SWARM_OUT_DIR]') +
298 api.step_data('parse skottie_asset_000 trace',
299 api.json.output(parse_trace_json), retcode=1)
300 )
301 yield (
302 api.test('skottietracing_trybot') +
303 api.properties(buildername=android_buildername,
304 repository='https://skia.googlesource.com/skia.git',
305 revision='abc123',
306 task_id='abc123',
307 trace_test_data=trace_output,
308 dm_json_test_data=dm_json_test_data,
309 path_config='kitchen',
310 swarm_out_dir='[SWARM_OUT_DIR]',
311 patch_ref='89/456789/12',
312 patch_repo='https://skia.googlesource.com/skia.git',
313 patch_storage='gerrit',
314 patch_set=7,
315 patch_issue=1234,
316 gerrit_project='skia',
317 gerrit_url='https://skia-review.googlesource.com/') +
318 api.step_data('parse skottie_asset_000 trace',
319 api.json.output(parse_trace_json)) +
320 api.step_data('parse skottie_asset_001 trace',
321 api.json.output(parse_trace_json)) +
322 api.step_data('parse skottie_asset_002 trace',
323 api.json.output(parse_trace_json))
324 )