Debian Codegraph @

tests/hbspeeds

raw
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later
3 # Copyright 2015, SIL International, All rights reserved.
4 '''
5 Tool to read callgrind output and return the net cost of the first call
6 to a given function
7 '''
8
9 import argparse
10 import json
11 import os
12 import re
13 import tempfile
14 from contextlib import suppress
15 from multiprocessing import Pool
16 from pathlib import Path
17 from subprocess import call
18
19 p = argparse.ArgumentParser()
20 p.add_argument('file', nargs='?', type=argparse.FileType('r'),
21 help="control file")
22 p.add_argument('-g', '--graph', action='store_true',
23 help='Display a graph instead of csv results')
24 p.add_argument('-s', '--single', action='store_true',
25 help='Do single processing')
26 p.add_argument('-f', '--font',
27 help='font file to use for a single test')
28 p.add_argument('-t', '--text',
29 help='text file to use for a single test')
30 p.add_argument('-l', '--line', type=int, default=0,
31 help='line in text file for single test')
32 p.add_argument('-n', '--normalize', action="store_true",
33 help="Normalize results")
34 p.add_argument('-k', '--keep',
35 help='Store intermediate callgrind results in '
36 'given directory')
37 p.add_argument('-u', '--usp', type=Path,
38 help="Include usp results")
39 p.add_argument('-v', '--verbose', action='store_true',
40 help='Print the commands executed')
41
42 args = p.parse_args()
43
44
45 def parsecg(fname):
46 idcache = {}
47
48 def parseline(line):
49 (_, s) = line.split('=', 1)
50 s = s.lstrip()
51 m = re.match(r'(?:\((\d+)\))?\s*(.*)$', s)
52 ident = m.group(1)
53 name = m.group(2)
54 if ident and name: idcache[ident] = name
55 if ident: return (ident, idcache.get(ident, name))
56 else: return (None, name)
57
58 fncosts = {}
59 with fname.open() as fh:
60 incfn = False
61 currfn = ''
62 for line in fh:
63 line = line.strip()
64 if incfn:
65 if line.startswith('calls'): continue
66 cost = line.split(' ')[1]
67 fncosts[currfn] = cost
68 incfn = False
69 elif line.startswith('cfn'):
70 (ident, name) = parseline(line)
71 incfn = True
72 currfn = name
73 elif line.startswith('fn'):
74 (id, name) = parseline(line)
75 return fncosts
76
77
78 modes = {
79 'ot': (
80 'ot',
81 # {'ot': 'hb_shape_plan_execute',
82 {'ot': '_hb_ot_shape',
83 'ot_GSUB': 'hb_ot_map_t::substitute(hb_ot_shape_plan_t const*, hb_font_t*, hb_buffer_t*) const',
84 'ot_GPOS': 'hb_ot_map_t::position(hb_ot_shape_plan_t const*, hb_font_t*, hb_buffer_t*) const'}),
85 'gr': (
86 'graphite2',
87 # {'hbgr': 'hb_shape_plan_execute',
88 {'hbgr': '_hb_graphite2_shape',
89 'gr': 'gr_make_seg',
90 'grcoll': 'graphite2::Pass::collisionShift(graphite2::Segment*, int, graphite2::json*) const',
91 'grkern': 'graphite2::Pass::collisionKern(graphite2::Segment*, int, graphite2::json*) const'}),
92 'usp': (
93 'uniscribe',
94 {'usp': '0x61829dc6',
95 'abc': 'GetCharABCWidthsI',
96 'usp_GSUB': '0x6f90e350',
97 'usp_GPOS': '0x6f90e660'},
98 {'valgrind_parms': ['--trace-children=yes', '--combine-dumps=yes'],
99 'dir': args.usp,
100 'exe': ['wine',
101 args.usp and args.usp.joinpath('util', 'hb-shape.exe')],
102 'env': {
103 'WINEPATH': args.usp,
104 'WINEDLLPATH': args.usp,
105 'WINEDLLOVERRIDES': '"usp10=n"',
106 'WINEDEBUG': 'trace+dll'}},
107 {'uspnorm': lambda x: int(x['usp']) - int(x['abc'])}
108 )
109 }
110
111
112 def getval(dat, key, defkey=None):
113 if not hasattr(dat, 'keys'):
114 return dat
115 if defkey is not None:
116 return dat.get(key, dat[defkey])
117 else:
118 return dat.get(key, None)
119
120
121 def dorow(x):
122 (c, r) = x
123 results = r.copy()
124 for k, job in modes.items():
125 if k == 'usp' and not args.usp:
126 for l in ('usp', 'abc', 'uspnorm', 'usp_GSUB', 'usp_GPOS'):
127 results[l] = 0
128 continue
129 resf = tmpd.joinpath('callgrind_'+k+'_'+str(c)+'.out').resolve()
130 with open(r['textfile'], encoding='utf_8') as ft:
131 t = ft.readlines()[int(r['textline'])].strip()
132 tfile = tmpd.joinpath('t'+k+str(c)+'.out').resolve()
133 with tfile.open('w') as ft:
134 ft.write(t+"\n")
135 results['char'] = len(t)
136 font = getval(r['font'], k, defkey='ot')
137 cmd = ['valgrind', '-q',
138 '--tool=callgrind',
139 '--callgrind-out-file='+str(resf),
140 'hb-shape',
141 '--font-file='+font,
142 '--shapers='+job[0],
143 '--text-file='+str(tfile)]
144 cwd = None
145 env = os.environ.copy()
146 if len(job) > 2:
147 cmd = cmd[:4] + job[2]['valgrind_parms'] + job[2]['exe'] + cmd[5:]
148 cwd = job[2]['dir']
149 env.update(job[2]['env'])
150 # copt = 3 # This is never used
151 if 'rtl' in r and r['rtl']:
152 cmd.append('--direction=rtl')
153 if 'cmdext' in r:
154 f = getval(r['cmdext'], k)
155 if f is not None:
156 cmd.append(f)
157 if args.verbose:
158 print(" ".join(cmd))
159
160 with suppress(Exception):
161 call(cmd, stdout=dumpfh, cwd=cwd)
162
163 cfns = parsecg(resf)
164 for rk, rf in job[1].items():
165 if 'ignore' in r and k in r['ignore']:
166 results[rk] = 0
167 else:
168 results[rk] = int(cfns.get(rf, 0))
169 if args.normalize:
170 results[rk] /= results['char']
171 if len(job) > 3:
172 for rk, rl in job[3].items():
173 results[rk] = rl(results)
174 if not args.keep:
175 resf.unlink()
176 tfile.unlink()
177 return results
178
179
180 outtop = 'name, length, ot, ot_GSUB, ot_GPOS, hbgr, gr,' \
181 ' grcoll, grkern, usp, abc, uspnorm, usp_GSUB, usp_GPOS'
182 outformat = '{label}, {char}, {ot}, {ot_GSUB}, {ot_GPOS}, {hbgr}, {gr},' \
183 ' {grcoll}, {grkern}, {usp}, {abc}, {uspnorm}, {usp_GSUB},' \
184 ' {usp_GPOS}'
185
186 tmpd = Path(args.keep if args.keep else tempfile.mkdtemp(prefix='hbspeeds-'))
187 dumpf = tmpd.joinpath('dump.out')
188 with dumpf.open('w') as dumpfh:
189 if args.file:
190 c = json.load(args.file)
191 args.file.close()
192 else:
193 c = [{'font': args.font,
194 'textfile': args.text,
195 'textline': args.line,
196 'label': 'Commandline'}]
197
198 if args.single or args.file is None:
199 res = list(map(dorow, enumerate(c)))
200 else:
201 pool = Pool()
202 res = pool.map(dorow, enumerate(c))
203 pool.close()
204 pool.join()
205 dumpf.unlink()
206
207 if not args.keep:
208 tmpd.rmdir()
209
210 if not args.graph:
211 print(outtop)
212 for r in res:
213 if r is not None:
214 print(outformat.format(**r))
215 else:
216 import matplotlib.pyplot as plt
217
218 def stackplot(xs, ydat, width, **kwargs):
219 res = []
220 h = len(ydat[0])
221 # yacc = [0] * len(ydat) # This is never used
222 for i in range(h):
223 res.append(plt.bar(xs,
224 [y[i] - (y[i-1] if i > 0 else 0) for y in ydat],
225 width,
226 bottom=[y[i-1] if i > 0 else 0 for y in ydat],
227 **kwargs))
228 return res
229
230 width = 0.25
231 fig, ax = plt.subplots(figsize=(10, 5))
232 dat = [
233 [(x['uspnorm'], ) if args.normalize
234 else (x['uspnorm'] / x['char'], ) for x in res],
235 [(x['ot'], ) if args.normalize
236 else (x['ot'] / x['char'], ) for x in res],
237 [(x['gr'], ) if args.normalize
238 else (x['gr'] / x['char'], ) for x in res]]
239 # [(x['usp_GSUB'], x['usp_GSUB'] + x['usp_GPOS'] - x['abc'],
240 # x['uspnorm']) for x in res],
241 # [(x['ot_GSUB'], x['ot_GSUB'] + x['ot_GPOS'], x['ot']) for x in res],
242 # [(x['gr'], x['hbgr']) for x in res]]
243 pos = list(range(len(dat[1])))
244 plots = [stackplot(pos, dat[0], width,
245 alpha=0.6, color='blue', label='usp'),
246 stackplot([p + width for p in pos], dat[1], width,
247 alpha=0.6, color='red', label='ot'),
248 stackplot([p + width * 2 for p in pos], dat[2], width,
249 alpha=0.6, color='green', label='gr')]
250 ax.set_ylabel('Cycles / character')
251 ax.set_title('Font Speed Test')
252 ax.set_xticks([p + 1.5 * width for p in pos])
253 ax.set_xticklabels([x['label'] for x in res])
254 plt.xlim(min(pos) - width, max(pos) + width * 4)
255 # ymax = max([max(x) for x in dat])
256 # plt.ylim(0, ymax)
257 plt.legend([x[0] for x in plots], ['usp', 'ot', 'gr'], loc='upper left')
258 plt.grid()
259 plt.show()