blob: 7e40df4ba5e4c7e8e7d184aa9a5466b9b2586ad8 [file] [log] [blame]
csmartdalton4b5179b2016-09-19 11:03:58 -07001#!/usr/bin/env python
2
3# Copyright 2016 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8from __future__ import print_function
csmartdaltond7a9db62016-09-22 05:10:02 -07009from _adb import Adb
csmartdalton4b5179b2016-09-19 11:03:58 -070010from _benchresult import BenchResult
csmartdaltond7a9db62016-09-22 05:10:02 -070011from _hardware import HardwareException, Hardware
csmartdalton4b5179b2016-09-19 11:03:58 -070012from argparse import ArgumentParser
csmartdalton5745d792016-09-22 12:37:21 -070013from multiprocessing import Queue
csmartdaltond7a9db62016-09-22 05:10:02 -070014from threading import Thread, Timer
csmartdalton4b5179b2016-09-19 11:03:58 -070015import collections
16import glob
17import math
18import re
19import subprocess
20import sys
csmartdaltond7a9db62016-09-22 05:10:02 -070021import time
csmartdalton4b5179b2016-09-19 11:03:58 -070022
csmartdaltonbf41fa82016-09-23 11:36:11 -070023__argparse = ArgumentParser(description="""
csmartdalton4b5179b2016-09-19 11:03:58 -070024
25Executes the skpbench binary with various configs and skps.
26
27Also monitors the output in order to filter out and re-run results that have an
28unacceptable stddev.
29
csmartdaltonbf41fa82016-09-23 11:36:11 -070030""")
csmartdalton4b5179b2016-09-19 11:03:58 -070031
csmartdalton2087dda2016-11-09 16:34:53 -050032__argparse.add_argument('skpbench',
Kevin Lubick9c2249f2016-11-10 14:19:00 -050033 help="path to the skpbench binary")
csmartdalton0262b5c2016-09-19 12:04:56 -070034__argparse.add_argument('--adb',
Kevin Lubick9c2249f2016-11-10 14:19:00 -050035 action='store_true', help="execute skpbench over adb")
Chris Daltonacb549f2017-10-19 14:11:58 -060036__argparse.add_argument('--adb_binary', default='adb',
37 help="The name of the adb binary to use.")
csmartdalton0262b5c2016-09-19 12:04:56 -070038__argparse.add_argument('-s', '--device-serial',
Kevin Lubick9c2249f2016-11-10 14:19:00 -050039 help="if using adb, ID of the specific device to target "
40 "(only required if more than 1 device is attached)")
csmartdalton4b5179b2016-09-19 11:03:58 -070041__argparse.add_argument('-m', '--max-stddev',
Kevin Lubick9c2249f2016-11-10 14:19:00 -050042 type=float, default=4,
43 help="initial max allowable relative standard deviation")
csmartdalton4b5179b2016-09-19 11:03:58 -070044__argparse.add_argument('-x', '--suffix',
Kevin Lubick9c2249f2016-11-10 14:19:00 -050045 help="suffix to append on config (e.g. '_before', '_after')")
csmartdalton4b5179b2016-09-19 11:03:58 -070046__argparse.add_argument('-w','--write-path',
Kevin Lubick9c2249f2016-11-10 14:19:00 -050047 help="directory to save .png proofs to disk.")
csmartdalton4b5179b2016-09-19 11:03:58 -070048__argparse.add_argument('-v','--verbosity',
Kevin Lubick9c2249f2016-11-10 14:19:00 -050049 type=int, default=1, help="level of verbosity (0=none to 5=debug)")
csmartdalton037adf32016-09-28 13:56:01 -070050__argparse.add_argument('-d', '--duration',
Kevin Lubick9c2249f2016-11-10 14:19:00 -050051 type=int, help="number of milliseconds to run each benchmark")
csmartdalton037adf32016-09-28 13:56:01 -070052__argparse.add_argument('-l', '--sample-ms',
Kevin Lubick9c2249f2016-11-10 14:19:00 -050053 type=int, help="duration of a sample (minimum)")
csmartdaltonc6618dd2016-10-05 08:42:03 -070054__argparse.add_argument('--gpu',
Kevin Lubick9c2249f2016-11-10 14:19:00 -050055 action='store_true',
56 help="perform timing on the gpu clock instead of cpu (gpu work only)")
csmartdalton4b5179b2016-09-19 11:03:58 -070057__argparse.add_argument('--fps',
Kevin Lubick9c2249f2016-11-10 14:19:00 -050058 action='store_true', help="use fps instead of ms")
Chris Daltonacb549f2017-10-19 14:11:58 -060059__argparse.add_argument('--pr',
60 help="comma- or space-separated list of GPU path renderers, including: "
61 "[[~]all [~]default [~]dashline [~]nvpr [~]msaa [~]aaconvex "
62 "[~]aalinearizing [~]small [~]tess]")
Chris Daltona8fbeba2019-03-30 00:31:23 -060063__argparse.add_argument('--cc',
64 action='store_true', help="allow coverage counting shortcuts to render paths")
Chris Daltonacb549f2017-10-19 14:11:58 -060065__argparse.add_argument('--nocache',
66 action='store_true', help="disable caching of path mask textures")
csmartdalton4b5179b2016-09-19 11:03:58 -070067__argparse.add_argument('-c', '--config',
Brian Salomon50f66d82017-03-17 14:32:05 -040068 default='gl', help="comma- or space-separated list of GPU configs")
csmartdalton49df7702016-11-10 10:36:28 -050069__argparse.add_argument('-a', '--resultsfile',
Kevin Lubick9c2249f2016-11-10 14:19:00 -050070 help="optional file to append results into")
Robert Phillips9619c492018-05-31 08:12:33 -040071__argparse.add_argument('--ddl',
72 action='store_true', help="record the skp into DDLs before rendering")
73__argparse.add_argument('--ddlNumAdditionalThreads',
74 type=int, default=0,
75 help="number of DDL recording threads in addition to main one")
76__argparse.add_argument('--ddlTilingWidthHeight',
77 type=int, default=0, help="number of tiles along one edge when in DDL mode")
Robert Phillips65eb4fb2018-05-31 13:27:52 -040078__argparse.add_argument('--ddlRecordTime',
79 action='store_true', help="report just the cpu time spent recording DDLs")
Robert Phillipsd5506cb2018-06-26 08:49:38 -040080__argparse.add_argument('--gpuThreads',
81 type=int, default=-1,
82 help="Create this many extra threads to assist with GPU work, including"
83 " software path rendering. Defaults to two.")
Chris Daltona4f5ce02018-06-26 10:13:06 -060084__argparse.add_argument('srcs',
Kevin Lubick9c2249f2016-11-10 14:19:00 -050085 nargs='+',
Chris Daltona4f5ce02018-06-26 10:13:06 -060086 help=".skp files or directories to expand for .skp files, and/or .svg files")
csmartdalton4b5179b2016-09-19 11:03:58 -070087
88FLAGS = __argparse.parse_args()
csmartdalton0262b5c2016-09-19 12:04:56 -070089if FLAGS.adb:
90 import _adb_path as _path
Kevin Lubickcccaef12017-10-13 08:15:09 -040091 _path.init(FLAGS.device_serial, FLAGS.adb_binary)
csmartdalton0262b5c2016-09-19 12:04:56 -070092else:
93 import _os_path as _path
csmartdalton4b5179b2016-09-19 11:03:58 -070094
csmartdalton5772eaa2016-10-11 18:28:54 -070095def dump_commandline_if_verbose(commandline):
csmartdaltone4fd0782016-11-09 08:41:23 -080096 if FLAGS.verbosity >= 5:
csmartdalton5772eaa2016-10-11 18:28:54 -070097 quoted = ['\'%s\'' % re.sub(r'([\\\'])', r'\\\1', x) for x in commandline]
98 print(' '.join(quoted), file=sys.stderr)
99
csmartdalton4b5179b2016-09-19 11:03:58 -0700100
101class StddevException(Exception):
102 pass
103
104class Message:
105 READLINE = 0,
csmartdaltond7a9db62016-09-22 05:10:02 -0700106 POLL_HARDWARE = 1,
107 EXIT = 2
csmartdalton4b5179b2016-09-19 11:03:58 -0700108 def __init__(self, message, value=None):
109 self.message = message
110 self.value = value
111
csmartdaltond7a9db62016-09-22 05:10:02 -0700112class SubprocessMonitor(Thread):
113 def __init__(self, queue, proc):
114 self._queue = queue
115 self._proc = proc
116 Thread.__init__(self)
117
118 def run(self):
csmartdaltonbf41fa82016-09-23 11:36:11 -0700119 """Runs on the background thread."""
csmartdaltond7a9db62016-09-22 05:10:02 -0700120 for line in iter(self._proc.stdout.readline, b''):
121 self._queue.put(Message(Message.READLINE, line.decode('utf-8').rstrip()))
122 self._queue.put(Message(Message.EXIT))
123
124class SKPBench:
csmartdalton2087dda2016-11-09 16:34:53 -0500125 ARGV = [FLAGS.skpbench, '--verbosity', str(FLAGS.verbosity)]
csmartdalton037adf32016-09-28 13:56:01 -0700126 if FLAGS.duration:
127 ARGV.extend(['--duration', str(FLAGS.duration)])
csmartdalton4b5179b2016-09-19 11:03:58 -0700128 if FLAGS.sample_ms:
129 ARGV.extend(['--sampleMs', str(FLAGS.sample_ms)])
csmartdaltonc6618dd2016-10-05 08:42:03 -0700130 if FLAGS.gpu:
131 ARGV.extend(['--gpuClock', 'true'])
csmartdalton4b5179b2016-09-19 11:03:58 -0700132 if FLAGS.fps:
133 ARGV.extend(['--fps', 'true'])
Chris Daltonacb549f2017-10-19 14:11:58 -0600134 if FLAGS.pr:
135 ARGV.extend(['--pr'] + re.split(r'[ ,]', FLAGS.pr))
Chris Daltona8fbeba2019-03-30 00:31:23 -0600136 if FLAGS.cc:
137 ARGV.extend(['--cc', 'true'])
Chris Daltonacb549f2017-10-19 14:11:58 -0600138 if FLAGS.nocache:
139 ARGV.extend(['--cachePathMasks', 'false'])
Robert Phillipsd5506cb2018-06-26 08:49:38 -0400140 if FLAGS.gpuThreads != -1:
141 ARGV.extend(['--gpuThreads', str(FLAGS.gpuThreads)])
Robert Phillips9619c492018-05-31 08:12:33 -0400142
143 # DDL parameters
144 if FLAGS.ddl:
145 ARGV.extend(['--ddl', 'true'])
146 if FLAGS.ddlNumAdditionalThreads:
147 ARGV.extend(['--ddlNumAdditionalThreads',
148 str(FLAGS.ddlNumAdditionalThreads)])
149 if FLAGS.ddlTilingWidthHeight:
150 ARGV.extend(['--ddlTilingWidthHeight', str(FLAGS.ddlTilingWidthHeight)])
Robert Phillips65eb4fb2018-05-31 13:27:52 -0400151 if FLAGS.ddlRecordTime:
152 ARGV.extend(['--ddlRecordTime', 'true'])
Robert Phillips9619c492018-05-31 08:12:33 -0400153
csmartdalton0262b5c2016-09-19 12:04:56 -0700154 if FLAGS.adb:
155 if FLAGS.device_serial is None:
Kevin Lubickcccaef12017-10-13 08:15:09 -0400156 ARGV[:0] = [FLAGS.adb_binary, 'shell']
csmartdalton0262b5c2016-09-19 12:04:56 -0700157 else:
Kevin Lubickcccaef12017-10-13 08:15:09 -0400158 ARGV[:0] = [FLAGS.adb_binary, '-s', FLAGS.device_serial, 'shell']
csmartdalton4b5179b2016-09-19 11:03:58 -0700159
160 @classmethod
csmartdalton49df7702016-11-10 10:36:28 -0500161 def get_header(cls, outfile=sys.stdout):
csmartdalton5772eaa2016-10-11 18:28:54 -0700162 commandline = cls.ARGV + ['--duration', '0']
163 dump_commandline_if_verbose(commandline)
csmartdalton49df7702016-11-10 10:36:28 -0500164 out = subprocess.check_output(commandline, stderr=subprocess.STDOUT)
165 return out.rstrip()
csmartdalton5772eaa2016-10-11 18:28:54 -0700166
167 @classmethod
Brian Salomon50f66d82017-03-17 14:32:05 -0400168 def run_warmup(cls, warmup_time, config):
csmartdalton5772eaa2016-10-11 18:28:54 -0700169 if not warmup_time:
170 return
csmartdalton310d72c2016-10-18 09:19:50 -0700171 print('running %i second warmup...' % warmup_time, file=sys.stderr)
csmartdalton5772eaa2016-10-11 18:28:54 -0700172 commandline = cls.ARGV + ['--duration', str(warmup_time * 1000),
Brian Salomon50f66d82017-03-17 14:32:05 -0400173 '--config', config,
Chris Daltona4f5ce02018-06-26 10:13:06 -0600174 '--src', 'warmup']
csmartdalton5772eaa2016-10-11 18:28:54 -0700175 dump_commandline_if_verbose(commandline)
csmartdaltonf2b024d2016-11-09 13:25:23 -0800176 output = subprocess.check_output(commandline, stderr=subprocess.STDOUT)
177
csmartdalton5772eaa2016-10-11 18:28:54 -0700178 # validate the warmup run output.
csmartdaltonf2b024d2016-11-09 13:25:23 -0800179 for line in output.decode('utf-8').split('\n'):
csmartdalton5772eaa2016-10-11 18:28:54 -0700180 match = BenchResult.match(line.rstrip())
181 if match and match.bench == 'warmup':
182 return
183 raise Exception('Invalid warmup output:\n%s' % output)
csmartdalton4b5179b2016-09-19 11:03:58 -0700184
Chris Daltona4f5ce02018-06-26 10:13:06 -0600185 def __init__(self, src, config, max_stddev, best_result=None):
186 self.src = src
csmartdalton4b5179b2016-09-19 11:03:58 -0700187 self.config = config
188 self.max_stddev = max_stddev
189 self.best_result = best_result
190 self._queue = Queue()
csmartdaltond7a9db62016-09-22 05:10:02 -0700191 self._proc = None
192 self._monitor = None
193 self._hw_poll_timer = None
csmartdalton4b5179b2016-09-19 11:03:58 -0700194
csmartdaltond7a9db62016-09-22 05:10:02 -0700195 def __enter__(self):
196 return self
197
198 def __exit__(self, exception_type, exception_value, traceback):
199 if self._proc:
200 self.terminate()
201 if self._hw_poll_timer:
202 self._hw_poll_timer.cancel()
203
204 def execute(self, hardware):
205 hardware.sanity_check()
206 self._schedule_hardware_poll()
207
208 commandline = self.ARGV + ['--config', self.config,
Chris Daltona4f5ce02018-06-26 10:13:06 -0600209 '--src', self.src,
csmartdaltond7a9db62016-09-22 05:10:02 -0700210 '--suppressHeader', 'true']
211 if FLAGS.write_path:
212 pngfile = _path.join(FLAGS.write_path, self.config,
Chris Daltona4f5ce02018-06-26 10:13:06 -0600213 _path.basename(self.src) + '.png')
csmartdaltond7a9db62016-09-22 05:10:02 -0700214 commandline.extend(['--png', pngfile])
csmartdalton5772eaa2016-10-11 18:28:54 -0700215 dump_commandline_if_verbose(commandline)
csmartdaltonf2b024d2016-11-09 13:25:23 -0800216 self._proc = subprocess.Popen(commandline, stdout=subprocess.PIPE,
217 stderr=subprocess.STDOUT)
csmartdaltond7a9db62016-09-22 05:10:02 -0700218 self._monitor = SubprocessMonitor(self._queue, self._proc)
219 self._monitor.start()
220
csmartdalton4b5179b2016-09-19 11:03:58 -0700221 while True:
222 message = self._queue.get()
223 if message.message == Message.READLINE:
224 result = BenchResult.match(message.value)
225 if result:
csmartdaltond7a9db62016-09-22 05:10:02 -0700226 hardware.sanity_check()
227 self._process_result(result)
csmartdaltonf2b024d2016-11-09 13:25:23 -0800228 elif hardware.filter_line(message.value):
csmartdalton310d72c2016-10-18 09:19:50 -0700229 print(message.value, file=sys.stderr)
csmartdalton4b5179b2016-09-19 11:03:58 -0700230 continue
csmartdaltond7a9db62016-09-22 05:10:02 -0700231 if message.message == Message.POLL_HARDWARE:
232 hardware.sanity_check()
233 self._schedule_hardware_poll()
234 continue
csmartdalton4b5179b2016-09-19 11:03:58 -0700235 if message.message == Message.EXIT:
csmartdaltond7a9db62016-09-22 05:10:02 -0700236 self._monitor.join()
237 self._proc.wait()
238 if self._proc.returncode != 0:
239 raise Exception("skpbench exited with nonzero exit code %i" %
240 self._proc.returncode)
241 self._proc = None
csmartdalton4b5179b2016-09-19 11:03:58 -0700242 break
243
csmartdaltond7a9db62016-09-22 05:10:02 -0700244 def _schedule_hardware_poll(self):
245 if self._hw_poll_timer:
246 self._hw_poll_timer.cancel()
247 self._hw_poll_timer = \
248 Timer(1, lambda: self._queue.put(Message(Message.POLL_HARDWARE)))
249 self._hw_poll_timer.start()
250
251 def _process_result(self, result):
csmartdalton4b5179b2016-09-19 11:03:58 -0700252 if not self.best_result or result.stddev <= self.best_result.stddev:
253 self.best_result = result
csmartdaltond7a9db62016-09-22 05:10:02 -0700254 elif FLAGS.verbosity >= 2:
255 print("reusing previous result for %s/%s with lower stddev "
256 "(%s%% instead of %s%%)." %
csmartdalton4b5179b2016-09-19 11:03:58 -0700257 (result.config, result.bench, self.best_result.stddev,
258 result.stddev), file=sys.stderr)
259 if self.max_stddev and self.best_result.stddev > self.max_stddev:
260 raise StddevException()
csmartdalton4b5179b2016-09-19 11:03:58 -0700261
csmartdaltond7a9db62016-09-22 05:10:02 -0700262 def terminate(self):
263 if self._proc:
csmartdaltonc6618dd2016-10-05 08:42:03 -0700264 self._proc.terminate()
csmartdaltond7a9db62016-09-22 05:10:02 -0700265 self._monitor.join()
266 self._proc.wait()
267 self._proc = None
csmartdalton4b5179b2016-09-19 11:03:58 -0700268
csmartdalton49df7702016-11-10 10:36:28 -0500269def emit_result(line, resultsfile=None):
270 print(line)
271 sys.stdout.flush()
272 if resultsfile:
273 print(line, file=resultsfile)
274 resultsfile.flush()
csmartdalton4b5179b2016-09-19 11:03:58 -0700275
Chris Daltona4f5ce02018-06-26 10:13:06 -0600276def run_benchmarks(configs, srcs, hardware, resultsfile=None):
Chris Dalton34d90552017-10-20 09:58:32 -0600277 hasheader = False
Chris Daltona4f5ce02018-06-26 10:13:06 -0600278 benches = collections.deque([(src, config, FLAGS.max_stddev)
279 for src in srcs
csmartdalton4b5179b2016-09-19 11:03:58 -0700280 for config in configs])
281 while benches:
Chris Dalton34d90552017-10-20 09:58:32 -0600282 try:
283 with hardware:
Brian Salomon50f66d82017-03-17 14:32:05 -0400284 SKPBench.run_warmup(hardware.warmup_time, configs[0])
Chris Dalton34d90552017-10-20 09:58:32 -0600285 if not hasheader:
286 emit_result(SKPBench.get_header(), resultsfile)
287 hasheader = True
288 while benches:
289 benchargs = benches.popleft()
290 with SKPBench(*benchargs) as skpbench:
291 try:
292 skpbench.execute(hardware)
293 if skpbench.best_result:
294 emit_result(skpbench.best_result.format(FLAGS.suffix),
295 resultsfile)
296 else:
297 print("WARNING: no result for %s with config %s" %
Chris Daltona4f5ce02018-06-26 10:13:06 -0600298 (skpbench.src, skpbench.config), file=sys.stderr)
Chris Dalton34d90552017-10-20 09:58:32 -0600299
300 except StddevException:
301 retry_max_stddev = skpbench.max_stddev * math.sqrt(2)
302 if FLAGS.verbosity >= 1:
303 print("stddev is too high for %s/%s (%s%%, max=%.2f%%), "
304 "re-queuing with max=%.2f%%." %
305 (skpbench.best_result.config, skpbench.best_result.bench,
306 skpbench.best_result.stddev, skpbench.max_stddev,
307 retry_max_stddev),
308 file=sys.stderr)
Chris Daltona4f5ce02018-06-26 10:13:06 -0600309 benches.append((skpbench.src, skpbench.config, retry_max_stddev,
Chris Dalton34d90552017-10-20 09:58:32 -0600310 skpbench.best_result))
311
312 except HardwareException as exception:
313 skpbench.terminate()
314 if FLAGS.verbosity >= 4:
315 hardware.print_debug_diagnostics()
316 if FLAGS.verbosity >= 1:
Chris Dalton49b7ed32017-10-23 17:19:37 -0600317 print("%s; rebooting and taking a %i second nap..." %
Chris Dalton34d90552017-10-20 09:58:32 -0600318 (exception.message, exception.sleeptime), file=sys.stderr)
319 benches.appendleft(benchargs) # retry the same bench next time.
320 raise # wake hw up from benchmarking mode before the nap.
321
322 except HardwareException as exception:
323 time.sleep(exception.sleeptime)
csmartdaltond7a9db62016-09-22 05:10:02 -0700324
csmartdaltond7a9db62016-09-22 05:10:02 -0700325def main():
326 # Delimiter is ',' or ' ', skip if nested inside parens (e.g. gpu(a=b,c=d)).
327 DELIMITER = r'[, ](?!(?:[^(]*\([^)]*\))*[^()]*\))'
328 configs = re.split(DELIMITER, FLAGS.config)
Chris Daltona4f5ce02018-06-26 10:13:06 -0600329 srcs = _path.find_skps(FLAGS.srcs)
Nathaniel Nifongf7cf7942019-09-11 14:00:20 -0400330 assert srcs
csmartdaltond7a9db62016-09-22 05:10:02 -0700331
332 if FLAGS.adb:
Kevin Lubickcccaef12017-10-13 08:15:09 -0400333 adb = Adb(FLAGS.device_serial, FLAGS.adb_binary,
334 echo=(FLAGS.verbosity >= 5))
csmartdaltone4fd0782016-11-09 08:41:23 -0800335 model = adb.check('getprop ro.product.model').strip()
csmartdaltonbf41fa82016-09-23 11:36:11 -0700336 if model == 'Pixel C':
337 from _hardware_pixel_c import HardwarePixelC
338 hardware = HardwarePixelC(adb)
Chris Dalton34d90552017-10-20 09:58:32 -0600339 elif model == 'Pixel':
340 from _hardware_pixel import HardwarePixel
341 hardware = HardwarePixel(adb)
Chris Dalton117d9722018-04-27 17:10:39 -0600342 elif model == 'Pixel 2':
343 from _hardware_pixel2 import HardwarePixel2
344 hardware = HardwarePixel2(adb)
csmartdalton310d72c2016-10-18 09:19:50 -0700345 elif model == 'Nexus 6P':
346 from _hardware_nexus_6p import HardwareNexus6P
347 hardware = HardwareNexus6P(adb)
csmartdaltond7a9db62016-09-22 05:10:02 -0700348 else:
349 from _hardware_android import HardwareAndroid
350 print("WARNING: %s: don't know how to monitor this hardware; results "
351 "may be unreliable." % model, file=sys.stderr)
352 hardware = HardwareAndroid(adb)
353 else:
354 hardware = Hardware()
355
Chris Dalton34d90552017-10-20 09:58:32 -0600356 if FLAGS.resultsfile:
357 with open(FLAGS.resultsfile, mode='a+') as resultsfile:
Chris Daltona4f5ce02018-06-26 10:13:06 -0600358 run_benchmarks(configs, srcs, hardware, resultsfile=resultsfile)
Chris Dalton34d90552017-10-20 09:58:32 -0600359 else:
Chris Daltona4f5ce02018-06-26 10:13:06 -0600360 run_benchmarks(configs, srcs, hardware)
csmartdalton4b5179b2016-09-19 11:03:58 -0700361
362
363if __name__ == '__main__':
364 main()