Debian Codegraph @

infra/bots/recipes/compute_buildstats.py

raw
1 # Copyright 2018 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 analyzes a compiled binary for information (e.g. file size)
6
7 import ast
8 import json
9
10
11 DEPS = [
12 'checkout',
13 'env',
14 'recipe_engine/context',
15 'recipe_engine/file',
16 'recipe_engine/path',
17 'recipe_engine/properties',
18 'recipe_engine/raw_io',
19 'recipe_engine/step',
20 'run',
21 'vars',
22 ]
23
24
25 MAGIC_SEPERATOR = '#$%^&*'
26 TOTAL_SIZE_BYTES_KEY = "total_size_bytes"
27
28
29 def add_binary_size_output_property(result, source, binary_size):
30 result.presentation.properties['binary_size_%s' % source] = binary_size
31
32
33 def RunSteps(api):
34 api.vars.setup()
35
36 checkout_root = api.checkout.default_checkout_root
37 api.checkout.bot_update(checkout_root=checkout_root)
38
39 out_dir = api.vars.swarming_out_dir
40 # Any binaries to scan should be here.
41 bin_dir = api.vars.build_dir
42
43 api.file.ensure_directory('mkdirs out_dir', out_dir, mode=0o777)
44
45 analyzed = 0
46 with api.context(cwd=bin_dir):
47
48 files = api.file.glob_paths(
49 'find skia library',
50 bin_dir,
51 'libskia.so',
52 test_data=['libskia.so'])
53 analyzed += len(files)
54 if files:
55 analyze_cpp_lib(api, checkout_root, out_dir, files)
56
57 files = api.file.glob_paths(
58 'find skottie_tool',
59 bin_dir,
60 'skottie_tool',
61 test_data=['skottie_tool'])
62 analyzed += len(files)
63 if files:
64 make_treemap(api, checkout_root, out_dir, files)
65
66 files = api.file.glob_paths(
67 'find dm',
68 bin_dir,
69 'dm',
70 test_data=['dm'])
71 analyzed += len(files)
72 if files:
73 make_treemap(api, checkout_root, out_dir, files)
74
75 if not analyzed: # pragma: nocover
76 raise Exception('No files were analyzed!')
77
78
79 def keys_and_props(api):
80 keys = []
81 for k in sorted(api.vars.builder_cfg.keys()):
82 if not k in ['role']:
83 keys.extend([k, api.vars.builder_cfg[k]])
84 keystr = ' '.join(keys)
85
86 props = [
87 'gitHash', api.properties['revision'],
88 'swarming_bot_id', api.vars.swarming_bot_id,
89 'swarming_task_id', api.vars.swarming_task_id,
90 ]
91
92 if api.vars.is_trybot:
93 props.extend([
94 'issue', api.vars.issue,
95 'patchset', api.vars.patchset,
96 'patch_storage', api.vars.patch_storage,
97 ])
98 propstr = ' '.join(str(prop) for prop in props)
99 return (keystr, propstr)
100
101
102 # Get the raw size and a few metrics from bloaty
103 def analyze_cpp_lib(api, checkout_root, out_dir, files):
104 (keystr, propstr) = keys_and_props(api)
105 bloaty_exe = api.path.start_dir.joinpath('bloaty', 'bloaty')
106
107 for f in files:
108 skia_dir = checkout_root.joinpath('skia')
109 with api.context(cwd=skia_dir):
110 script = skia_dir.joinpath('infra', 'bots', 'buildstats',
111 'buildstats_cpp.py')
112 step_data = api.run(api.step, 'Analyze %s' % f,
113 cmd=['python3', script, f, out_dir, keystr, propstr, bloaty_exe,
114 TOTAL_SIZE_BYTES_KEY, MAGIC_SEPERATOR],
115 stdout=api.raw_io.output())
116 if step_data and step_data.stdout:
117 sections = step_data.stdout.decode('utf-8').split(MAGIC_SEPERATOR)
118 result = api.step.active_result
119 logs = result.presentation.logs
120 logs['perf_json'] = sections[2].split('\n')
121
122 add_binary_size_output_property(result, api.path.basename(f), (
123 ast.literal_eval(sections[2])
124 .get('results', {})
125 .get(api.path.basename(f), {})
126 .get('default', {})
127 .get(TOTAL_SIZE_BYTES_KEY, {})))
128
129
130 # make a zip file containing an HTML treemap of the files
131 def make_treemap(api, checkout_root, out_dir, files):
132 for f in files:
133 env = {'DOCKER_CONFIG': '/home/chrome-bot/.docker'}
134 with api.env(env):
135 skia_dir = checkout_root.joinpath('skia')
136 with api.context(cwd=skia_dir):
137 script = skia_dir.joinpath('infra', 'bots', 'buildstats',
138 'make_treemap.py')
139 api.run(api.step, 'Make code size treemap %s' % f,
140 cmd=['python3', script, f, out_dir],
141 stdout=api.raw_io.output())
142
143
144 def GenTests(api):
145 builder = 'BuildStats-Ubuntu24.04-Clang-x86_64-Release'
146 yield (
147 api.test('normal_bot') +
148 api.properties(buildername=builder,
149 repository='https://skia.googlesource.com/skia.git',
150 revision='abc123',
151 swarm_out_dir='[SWARM_OUT_DIR]',
152 path_config='kitchen') +
153 api.step_data('get swarming bot id',
154 stdout=api.raw_io.output('skia-bot-123')) +
155 api.step_data('get swarming task id',
156 stdout=api.raw_io.output('123456abc')) +
157 api.step_data('Analyze [START_DIR]/build/libskia.so',
158 stdout=api.raw_io.output(sample_cpp))
159 )
160
161 yield (
162 api.test('trybot') +
163 api.properties(buildername=builder,
164 repository='https://skia.googlesource.com/skia.git',
165 revision='abc123',
166 swarm_out_dir='[SWARM_OUT_DIR]',
167 patch_repo='https://skia.googlesource.com/skia.git',
168 path_config='kitchen') +
169 api.step_data('get swarming bot id',
170 stdout=api.raw_io.output('skia-bot-123')) +
171 api.step_data('get swarming task id',
172 stdout=api.raw_io.output('123456abc')) +
173 api.properties(patch_storage='gerrit') +
174 api.properties.tryserver(
175 buildername=builder,
176 gerrit_project='skia',
177 gerrit_url='https://skia-review.googlesource.com/',
178 ) +
179 api.step_data('Analyze [START_DIR]/build/libskia.so',
180 stdout=api.raw_io.output(sample_cpp))
181 )
182
183 sample_cpp = """
184 #$%^&*
185 Report A
186 Total size: 50 bytes
187 #$%^&*
188 {
189 "some": "json",
190 "results": {
191 "libskia.so": {
192 "default": {
193 "total_size_bytes": 7391117,
194 "gzip_size_bytes": 2884841
195 }
196 }
197 }
198 }
199 """