blob: a5a96aa6131ae45028d0c9ed6c455630286997f8 [file] [log] [blame]
mtkleinf3723212014-06-25 14:08:00 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
mtkleinbb6a0282014-07-01 08:43:42 -07008#include <ctype.h>
9
mtkleinf3723212014-06-25 14:08:00 -070010#include "Benchmark.h"
11#include "CrashHandler.h"
mtkleine714e752014-07-31 12:13:48 -070012#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070013#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070014#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070015#include "RecordingBench.h"
mtklein92007582014-08-01 07:46:52 -070016#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070017#include "Stats.h"
18#include "Timer.h"
19
mtklein20840502014-08-21 15:51:22 -070020#include "SkBBHFactory.h"
mtkleinf3723212014-06-25 14:08:00 -070021#include "SkCanvas.h"
caryclark17f0b6d2014-07-22 10:15:34 -070022#include "SkCommonFlags.h"
mtkleinf3723212014-06-25 14:08:00 -070023#include "SkForceLinking.h"
24#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070025#include "SkOSFile.h"
26#include "SkPictureRecorder.h"
mtkleinf3723212014-06-25 14:08:00 -070027#include "SkString.h"
28#include "SkSurface.h"
29
mtkleinbb6a0282014-07-01 08:43:42 -070030#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070031 #include "gl/GrGLDefines.h"
mtkleinbb6a0282014-07-01 08:43:42 -070032 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070033 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070034#endif
35
mtkleinf3723212014-06-25 14:08:00 -070036__SK_FORCE_IMAGE_DECODER_LINKING;
37
reed53249782014-10-10 09:09:52 -070038static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070039
mtkleinb5110422014-08-07 15:20:02 -070040static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070041#ifdef SK_DEBUG
42 1;
mtkleina189ccd2014-07-14 12:28:47 -070043#else
bsalomon6eb03cc2014-08-07 14:28:50 -070044 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070045#endif
46
bsalomon6eb03cc2014-08-07 14:28:50 -070047static SkString loops_help_txt() {
48 SkString help;
49 help.printf("Number of times to run each bench. Set this to %d to auto-"
50 "tune for each bench. Timings are only reported when auto-tuning.",
51 kAutoTuneLoops);
52 return help;
53}
54
55DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
56
mtkleinf3723212014-06-25 14:08:00 -070057DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
58DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
59DEFINE_double(overheadGoal, 0.0001,
60 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070061DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
62DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070063DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
64 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070065
mtklein60317d0f2014-07-14 11:30:37 -070066DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070067DEFINE_int32(maxCalibrationAttempts, 3,
68 "Try up to this many times to guess loops for a bench, or skip the bench.");
69DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -070070DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
71DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
mtklein20840502014-08-21 15:51:22 -070072DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtkleinc7f7f462014-10-21 12:29:25 -070073DEFINE_int32(benchTile, 256, "Tile dimension used for SKP playback.");
mtkleine070c2b2014-10-14 08:40:43 -070074DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein92007582014-08-01 07:46:52 -070075
mtkleinf3723212014-06-25 14:08:00 -070076static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -070077 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
78 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3);
79 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6);
mtklein62386882014-07-15 10:30:31 -070080#ifdef SK_BUILD_FOR_WIN
mtkleindc5bbab2014-09-24 06:34:09 -070081 if (ms < 1) return SkStringPrintf("%.3gus", ms*1e3);
mtklein62386882014-07-15 10:30:31 -070082#else
mtkleindc5bbab2014-09-24 06:34:09 -070083 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3);
mtklein62386882014-07-15 10:30:31 -070084#endif
mtkleinf3723212014-06-25 14:08:00 -070085 return SkStringPrintf("%.3gms", ms);
86}
mtklein55b0ffc2014-07-17 08:38:23 -070087#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -070088
kkinnunen9e61bb72014-10-09 05:24:15 -070089static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContext* gl) {
bsalomon6eb03cc2014-08-07 14:28:50 -070090 if (canvas) {
91 canvas->clear(SK_ColorWHITE);
92 }
mtkleinbb6a0282014-07-01 08:43:42 -070093 WallTimer timer;
94 timer.start();
95 if (bench) {
96 bench->draw(loops, canvas);
97 }
98 if (canvas) {
99 canvas->flush();
100 }
101#if SK_SUPPORT_GPU
102 if (gl) {
103 SK_GL(*gl, Flush());
104 gl->swapBuffers();
105 }
106#endif
107 timer.end();
108 return timer.fWall;
109}
110
mtkleinf3723212014-06-25 14:08:00 -0700111static double estimate_timer_overhead() {
112 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700113 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -0700114 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -0700115 }
116 return overhead / FLAGS_overheadLoops;
117}
118
reed53249782014-10-10 09:09:52 -0700119static int detect_forever_loops(int loops) {
120 // look for a magic run-forever value
121 if (loops < 0) {
122 loops = SK_MaxS32;
123 }
124 return loops;
125}
126
mtklein55b0ffc2014-07-17 08:38:23 -0700127static int clamp_loops(int loops) {
128 if (loops < 1) {
129 SkDebugf("ERROR: clamping loops from %d to 1.\n", loops);
130 return 1;
131 }
132 if (loops > FLAGS_maxLoops) {
133 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
134 return FLAGS_maxLoops;
135 }
136 return loops;
137}
138
bsalomon6eb03cc2014-08-07 14:28:50 -0700139static bool write_canvas_png(SkCanvas* canvas, const SkString& filename) {
140 if (filename.isEmpty()) {
141 return false;
142 }
reede5ea5002014-09-03 11:54:58 -0700143 if (kUnknown_SkColorType == canvas->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700144 return false;
145 }
146 SkBitmap bmp;
147 bmp.setInfo(canvas->imageInfo());
148 if (!canvas->readPixels(&bmp, 0, 0)) {
149 SkDebugf("Can't read canvas pixels.\n");
150 return false;
151 }
152 SkString dir = SkOSPath::Dirname(filename.c_str());
153 if (!sk_mkdir(dir.c_str())) {
154 SkDebugf("Can't make dir %s.\n", dir.c_str());
155 return false;
156 }
157 SkFILEWStream stream(filename.c_str());
158 if (!stream.isValid()) {
159 SkDebugf("Can't write %s.\n", filename.c_str());
160 return false;
161 }
162 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
163 SkDebugf("Can't encode a PNG.\n");
164 return false;
165 }
166 return true;
167}
168
169static int kFailedLoops = -2;
mtkleinbb6a0282014-07-01 08:43:42 -0700170static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
171 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700172 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700173 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700174 if (kAutoTuneLoops == FLAGS_loops) {
175 while (bench_plus_overhead < overhead) {
176 if (round++ == FLAGS_maxCalibrationAttempts) {
177 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700178 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700179 return kFailedLoops;
180 }
181 bench_plus_overhead = time(1, bench, canvas, NULL);
mtklein55b0ffc2014-07-17 08:38:23 -0700182 }
mtklein2069e222014-08-04 13:57:39 -0700183 }
mtkleinf3723212014-06-25 14:08:00 -0700184
mtkleinbb6a0282014-07-01 08:43:42 -0700185 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700186 // We'll pick N to make timer overhead negligible:
187 //
mtkleinbb6a0282014-07-01 08:43:42 -0700188 // overhead
189 // ------------------------- < FLAGS_overheadGoal
190 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700191 //
mtkleinbb6a0282014-07-01 08:43:42 -0700192 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700193 //
194 // Doing some math, we get:
195 //
mtkleinbb6a0282014-07-01 08:43:42 -0700196 // (overhead / FLAGS_overheadGoal) - overhead
197 // ------------------------------------------ < N
198 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700199 //
200 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700201 int loops = FLAGS_loops;
202 if (kAutoTuneLoops == loops) {
203 const double numer = overhead / FLAGS_overheadGoal - overhead;
204 const double denom = bench_plus_overhead - overhead;
205 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700206 loops = clamp_loops(loops);
207 } else {
208 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700209 }
mtkleinbb6a0282014-07-01 08:43:42 -0700210
211 for (int i = 0; i < FLAGS_samples; i++) {
212 samples[i] = time(loops, bench, canvas, NULL) / loops;
213 }
214 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700215}
216
mtkleinbb6a0282014-07-01 08:43:42 -0700217#if SK_SUPPORT_GPU
kkinnunen9e61bb72014-10-09 05:24:15 -0700218static int gpu_bench(SkGLContext* gl,
mtkleinbb6a0282014-07-01 08:43:42 -0700219 Benchmark* bench,
220 SkCanvas* canvas,
221 double* samples) {
bsalomonc2553372014-07-22 13:09:05 -0700222 gl->makeCurrent();
mtkleinbb6a0282014-07-01 08:43:42 -0700223 // Make sure we're done with whatever came before.
mtklein9bc86ed2014-07-01 10:02:42 -0700224 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700225
226 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700227 int loops = FLAGS_loops;
228 if (kAutoTuneLoops == loops) {
229 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700230 double elapsed = 0;
231 do {
232 loops *= 2;
233 // If the GPU lets frames lag at all, we need to make sure we're timing
234 // _this_ round, not still timing last round. We force this by looping
235 // more times than any reasonable GPU will allow frames to lag.
236 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
237 elapsed = time(loops, bench, canvas, gl);
238 }
239 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700240
mtkleina189ccd2014-07-14 12:28:47 -0700241 // We've overshot at least a little. Scale back linearly.
242 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700243 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700244
mtkleina189ccd2014-07-14 12:28:47 -0700245 // Might as well make sure we're not still timing our calibration.
246 SK_GL(*gl, Finish());
reed53249782014-10-10 09:09:52 -0700247 } else {
248 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700249 }
mtkleinbb6a0282014-07-01 08:43:42 -0700250
251 // Pretty much the same deal as the calibration: do some warmup to make
252 // sure we're timing steady-state pipelined frames.
253 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
254 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700255 }
mtkleinbb6a0282014-07-01 08:43:42 -0700256
257 // Now, actually do the timing!
258 for (int i = 0; i < FLAGS_samples; i++) {
259 samples[i] = time(loops, bench, canvas, gl) / loops;
260 }
261 return loops;
262}
263#endif
264
265static SkString to_lower(const char* str) {
266 SkString lower(str);
267 for (size_t i = 0; i < lower.size(); i++) {
268 lower[i] = tolower(lower[i]);
269 }
270 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700271}
272
bsalomonc2553372014-07-22 13:09:05 -0700273struct Config {
274 const char* name;
mtkleinbb6a0282014-07-01 08:43:42 -0700275 Benchmark::Backend backend;
bsalomonc2553372014-07-22 13:09:05 -0700276 SkColorType color;
277 SkAlphaType alpha;
278 int samples;
279#if SK_SUPPORT_GPU
280 GrContextFactory::GLContextType ctxType;
281#else
282 int bogusInt;
283#endif
284};
285
286struct Target {
287 explicit Target(const Config& c) : config(c) {}
288 const Config config;
mtkleinbb6a0282014-07-01 08:43:42 -0700289 SkAutoTDelete<SkSurface> surface;
290#if SK_SUPPORT_GPU
kkinnunen9e61bb72014-10-09 05:24:15 -0700291 SkGLContext* gl;
mtkleinbb6a0282014-07-01 08:43:42 -0700292#endif
293};
mtkleinf3723212014-06-25 14:08:00 -0700294
bsalomonc2553372014-07-22 13:09:05 -0700295static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700296 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700297 if (to_lower(FLAGS_config[i]).equals(name)) {
298 return true;
mtkleinf3723212014-06-25 14:08:00 -0700299 }
300 }
bsalomonc2553372014-07-22 13:09:05 -0700301 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700302}
303
bsalomonc2553372014-07-22 13:09:05 -0700304#if SK_SUPPORT_GPU
305static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
306 int sampleCnt) {
307 if (!is_cpu_config_allowed(name)) {
308 return false;
309 }
krajcevski69a55602014-08-13 10:46:31 -0700310 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700311 return sampleCnt <= ctx->getMaxSampleCount();
312 }
313 return false;
314}
315#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700316
bsalomonc2553372014-07-22 13:09:05 -0700317#if SK_SUPPORT_GPU
318#define kBogusGLContextType GrContextFactory::kNative_GLContextType
319#else
320#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700321#endif
bsalomonc2553372014-07-22 13:09:05 -0700322
323// Append all configs that are enabled and supported.
324static void create_configs(SkTDArray<Config>* configs) {
325 #define CPU_CONFIG(name, backend, color, alpha) \
326 if (is_cpu_config_allowed(#name)) { \
327 Config config = { #name, Benchmark::backend, color, alpha, 0, kBogusGLContextType }; \
328 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700329 }
mtkleine714e752014-07-31 12:13:48 -0700330
mtklein40b32be2014-07-09 08:46:49 -0700331 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700332 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
333 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
334 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700335 }
mtkleinbb6a0282014-07-01 08:43:42 -0700336
337#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700338 #define GPU_CONFIG(name, ctxType, samples) \
339 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
340 Config config = { \
341 #name, \
342 Benchmark::kGPU_Backend, \
343 kN32_SkColorType, \
344 kPremul_SkAlphaType, \
345 samples, \
346 GrContextFactory::ctxType }; \
347 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700348 }
mtkleine714e752014-07-31 12:13:48 -0700349
mtklein40b32be2014-07-09 08:46:49 -0700350 if (FLAGS_gpu) {
bsalomonc2553372014-07-22 13:09:05 -0700351 GPU_CONFIG(gpu, kNative_GLContextType, 0)
352 GPU_CONFIG(msaa4, kNative_GLContextType, 4)
353 GPU_CONFIG(msaa16, kNative_GLContextType, 16)
354 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4)
355 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16)
356 GPU_CONFIG(debug, kDebug_GLContextType, 0)
357 GPU_CONFIG(nullgpu, kNull_GLContextType, 0)
bsalomon3b4d0772014-08-06 10:52:33 -0700358#ifdef SK_ANGLE
359 GPU_CONFIG(angle, kANGLE_GLContextType, 0)
360#endif
mtklein40b32be2014-07-09 08:46:49 -0700361 }
mtkleinbb6a0282014-07-01 08:43:42 -0700362#endif
mtkleinf3723212014-06-25 14:08:00 -0700363}
364
bsalomonc2553372014-07-22 13:09:05 -0700365// If bench is enabled for config, returns a Target* for it, otherwise NULL.
366static Target* is_enabled(Benchmark* bench, const Config& config) {
367 if (!bench->isSuitableFor(config.backend)) {
368 return NULL;
369 }
370
reede5ea5002014-09-03 11:54:58 -0700371 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
372 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700373
374 Target* target = new Target(config);
375
376 if (Benchmark::kRaster_Backend == config.backend) {
377 target->surface.reset(SkSurface::NewRaster(info));
378 }
379#if SK_SUPPORT_GPU
380 else if (Benchmark::kGPU_Backend == config.backend) {
krajcevski69a55602014-08-13 10:46:31 -0700381 target->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(config.ctxType), info,
bsalomonc2553372014-07-22 13:09:05 -0700382 config.samples));
krajcevski69a55602014-08-13 10:46:31 -0700383 target->gl = gGrFactory->getGLContext(config.ctxType);
bsalomonc2553372014-07-22 13:09:05 -0700384 }
385#endif
386
387 if (Benchmark::kNonRendering_Backend != config.backend && !target->surface.get()) {
388 delete target;
389 return NULL;
390 }
391 return target;
392}
393
394// Creates targets for a benchmark and a set of configs.
395static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
396 const SkTDArray<Config>& configs) {
397 for (int i = 0; i < configs.count(); ++i) {
398 if (Target* t = is_enabled(b, configs[i])) {
399 targets->push(t);
400 }
mtkleine714e752014-07-31 12:13:48 -0700401
bsalomonc2553372014-07-22 13:09:05 -0700402 }
403}
404
jcgregoriobf5e5232014-07-17 13:14:16 -0700405#if SK_SUPPORT_GPU
kkinnunen9e61bb72014-10-09 05:24:15 -0700406static void fill_gpu_options(ResultsWriter* log, SkGLContext* ctx) {
jcgregorio05c45602014-07-17 13:54:36 -0700407 const GrGLubyte* version;
jcgregoriobf5e5232014-07-17 13:14:16 -0700408 SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION));
409 log->configOption("GL_VERSION", (const char*)(version));
410
411 SK_GL_RET(*ctx, version, GetString(GR_GL_RENDERER));
412 log->configOption("GL_RENDERER", (const char*) version);
413
414 SK_GL_RET(*ctx, version, GetString(GR_GL_VENDOR));
415 log->configOption("GL_VENDOR", (const char*) version);
416
417 SK_GL_RET(*ctx, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
418 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
419}
420#endif
421
mtkleine714e752014-07-31 12:13:48 -0700422class BenchmarkStream {
423public:
mtklein92007582014-08-01 07:46:52 -0700424 BenchmarkStream() : fBenches(BenchRegistry::Head())
425 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700426 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700427 , fCurrentScale(0)
428 , fCurrentSKP(0) {
429 for (int i = 0; i < FLAGS_skps.count(); i++) {
430 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
431 fSKPs.push_back() = FLAGS_skps[i];
432 } else {
433 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
434 SkString path;
435 while (it.next(&path)) {
436 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
437 }
438 }
439 }
mtkleine714e752014-07-31 12:13:48 -0700440
mtklein92007582014-08-01 07:46:52 -0700441 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
442 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
443 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
444 exit(1);
445 }
446
447 for (int i = 0; i < FLAGS_scales.count(); i++) {
448 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
449 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
450 exit(1);
451 }
452 }
453 }
454
mtkleinfd731ce2014-09-10 12:19:30 -0700455 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
456 // Not strictly necessary, as it will be checked again later,
457 // but helps to avoid a lot of pointless work if we're going to skip it.
458 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
459 return false;
460 }
461
462 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
463 if (stream.get() == NULL) {
464 SkDebugf("Could not read %s.\n", path);
465 return false;
466 }
467
mtklein963504b2014-09-17 06:58:39 -0700468 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700469 if (pic->get() == NULL) {
470 SkDebugf("Could not read %s as an SkPicture.\n", path);
471 return false;
472 }
473 return true;
474 }
475
mtklein92007582014-08-01 07:46:52 -0700476 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700477 if (fBenches) {
478 Benchmark* bench = fBenches->factory()(NULL);
479 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700480 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700481 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700482 return bench;
483 }
mtklein92007582014-08-01 07:46:52 -0700484
mtkleine714e752014-07-31 12:13:48 -0700485 while (fGMs) {
486 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
487 fGMs = fGMs->next();
488 if (gm->getFlags() & skiagm::GM::kAsBench_Flag) {
mtklein92007582014-08-01 07:46:52 -0700489 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700490 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700491 return SkNEW_ARGS(GMBench, (gm.detach()));
492 }
493 }
mtklein92007582014-08-01 07:46:52 -0700494
mtkleinfd731ce2014-09-10 12:19:30 -0700495 // First add all .skps as RecordingBenches.
496 while (fCurrentRecording < fSKPs.count()) {
497 const SkString& path = fSKPs[fCurrentRecording++];
498 SkAutoTUnref<SkPicture> pic;
499 if (!ReadPicture(path.c_str(), &pic)) {
500 continue;
501 }
502 SkString name = SkOSPath::Basename(path.c_str());
503 fSourceType = "skp";
504 fBenchType = "recording";
505 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
506 }
507
508 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700509 while (fCurrentScale < fScales.count()) {
510 while (fCurrentSKP < fSKPs.count()) {
511 const SkString& path = fSKPs[fCurrentSKP++];
mtkleinfd731ce2014-09-10 12:19:30 -0700512 SkAutoTUnref<SkPicture> pic;
513 if (!ReadPicture(path.c_str(), &pic)) {
mtklein92007582014-08-01 07:46:52 -0700514 continue;
515 }
mtklein20840502014-08-21 15:51:22 -0700516 if (FLAGS_bbh) {
517 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
mtklein20840502014-08-21 15:51:22 -0700518 const SkTileGridFactory::TileGridInfo info = {
mtkleinc7f7f462014-10-21 12:29:25 -0700519 SkISize::Make(FLAGS_benchTile, FLAGS_benchTile), // tile interval
520 SkISize::Make(0,0), // margin
521 SkIPoint::Make(0,0), // offset
mtklein20840502014-08-21 15:51:22 -0700522 };
523 SkTileGridFactory factory(info);
524 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700525 pic->playback(recorder.beginRecording(pic->cullRect().width(),
mtkleinea65bfa2014-09-09 07:59:46 -0700526 pic->cullRect().height(),
robertphillipsc5ba71d2014-09-04 08:42:50 -0700527 &factory));
mtklein20840502014-08-21 15:51:22 -0700528 pic.reset(recorder.endRecording());
529 }
mtkleinfd731ce2014-09-10 12:19:30 -0700530 SkString name = SkOSPath::Basename(path.c_str());
mtklein92007582014-08-01 07:46:52 -0700531 fSourceType = "skp";
mtkleinfd731ce2014-09-10 12:19:30 -0700532 fBenchType = "playback";
mtklein92007582014-08-01 07:46:52 -0700533 return SkNEW_ARGS(SKPBench,
534 (name.c_str(), pic.get(), fClip, fScales[fCurrentScale]));
535 }
536 fCurrentSKP = 0;
537 fCurrentScale++;
538 }
539
mtkleine714e752014-07-31 12:13:48 -0700540 return NULL;
541 }
mtklein92007582014-08-01 07:46:52 -0700542
543 void fillCurrentOptions(ResultsWriter* log) const {
544 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700545 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700546 if (0 == strcmp(fSourceType, "skp")) {
547 log->configOption("clip",
548 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
549 fClip.fRight, fClip.fBottom).c_str());
550 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
551 }
552 }
553
mtkleine714e752014-07-31 12:13:48 -0700554private:
555 const BenchRegistry* fBenches;
556 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700557 SkIRect fClip;
558 SkTArray<SkScalar> fScales;
559 SkTArray<SkString> fSKPs;
560
mtkleinfd731ce2014-09-10 12:19:30 -0700561 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
562 const char* fBenchType; // How we bench it: micro, recording, playback, ...
563 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700564 int fCurrentScale;
565 int fCurrentSKP;
mtkleine714e752014-07-31 12:13:48 -0700566};
567
caryclark17f0b6d2014-07-22 10:15:34 -0700568int nanobench_main();
569int nanobench_main() {
mtkleinf3723212014-06-25 14:08:00 -0700570 SetupCrashHandler();
571 SkAutoGraphics ag;
mtkleinf3723212014-06-25 14:08:00 -0700572
krajcevski69a55602014-08-13 10:46:31 -0700573#if SK_SUPPORT_GPU
krajcevski12b35442014-08-13 12:06:26 -0700574 GrContext::Options grContextOpts;
575 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
576 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700577#endif
578
bsalomon06cddec2014-10-24 10:40:50 -0700579 if (FLAGS_veryVerbose) {
580 FLAGS_verbose = true;
581 }
582
bsalomon6eb03cc2014-08-07 14:28:50 -0700583 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700584 FLAGS_samples = 1;
585 FLAGS_gpuFrameLag = 0;
586 }
587
bsalomon6eb03cc2014-08-07 14:28:50 -0700588 if (!FLAGS_writePath.isEmpty()) {
589 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
590 if (!sk_mkdir(FLAGS_writePath[0])) {
591 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
592 FLAGS_writePath.set(0, NULL);
593 }
594 }
595
mtklein1915b622014-08-20 11:45:00 -0700596 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700597 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700598 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700599 }
mtklein1915b622014-08-20 11:45:00 -0700600
601 if (1 == FLAGS_properties.count() % 2) {
602 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
603 return 1;
604 }
605 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
606 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
607 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700608
609 if (1 == FLAGS_key.count() % 2) {
610 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
611 return 1;
612 }
613 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700614 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700615 }
mtklein60317d0f2014-07-14 11:30:37 -0700616
mtkleinf3723212014-06-25 14:08:00 -0700617 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700618 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400619
mtkleinbb6a0282014-07-01 08:43:42 -0700620 SkAutoTMalloc<double> samples(FLAGS_samples);
621
bsalomon6eb03cc2014-08-07 14:28:50 -0700622 if (kAutoTuneLoops != FLAGS_loops) {
623 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700624 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700625 // No header.
626 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700627 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700628 } else {
qiankun.miao8247ec32014-09-09 19:24:36 -0700629 SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
630 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700631 }
632
bsalomonc2553372014-07-22 13:09:05 -0700633 SkTDArray<Config> configs;
634 create_configs(&configs);
635
mtkleine070c2b2014-10-14 08:40:43 -0700636 int runs = 0;
mtklein92007582014-08-01 07:46:52 -0700637 BenchmarkStream benchStream;
638 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700639 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -0700640 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -0700641 continue;
642 }
643
mtkleinbb6a0282014-07-01 08:43:42 -0700644 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700645 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700646
jcgregoriobf5e5232014-07-17 13:14:16 -0700647 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -0700648 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700649 bench->preDraw();
650 }
mtkleinbb6a0282014-07-01 08:43:42 -0700651 for (int j = 0; j < targets.count(); j++) {
652 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
bsalomonc2553372014-07-22 13:09:05 -0700653 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700654
mtkleinbb6a0282014-07-01 08:43:42 -0700655 const int loops =
656#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700657 Benchmark::kGPU_Backend == targets[j]->config.backend
mtkleinbb6a0282014-07-01 08:43:42 -0700658 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
659 :
660#endif
661 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700662
bsalomon49f085d2014-09-05 13:34:00 -0700663 if (canvas && !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700664 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -0700665 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -0700666 pngFilename.append(".png");
667 write_canvas_png(canvas, pngFilename);
668 }
669
670 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700671 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400672 continue;
673 }
674
mtkleinf3723212014-06-25 14:08:00 -0700675 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700676 log->config(config);
mtklein96289052014-09-10 12:05:59 -0700677 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -0700678 benchStream.fillCurrentOptions(log.get());
jcgregoriobf5e5232014-07-17 13:14:16 -0700679#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700680 if (Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein1915b622014-08-20 11:45:00 -0700681 fill_gpu_options(log.get(), targets[j]->gl);
jcgregoriobf5e5232014-07-17 13:14:16 -0700682 }
683#endif
mtklein1915b622014-08-20 11:45:00 -0700684 log->timer("min_ms", stats.min);
685 log->timer("median_ms", stats.median);
686 log->timer("mean_ms", stats.mean);
687 log->timer("max_ms", stats.max);
688 log->timer("stddev_ms", sqrt(stats.var));
mtkleine070c2b2014-10-14 08:40:43 -0700689 if (runs++ % FLAGS_flushEvery == 0) {
690 log->flush();
691 }
mtklein60317d0f2014-07-14 11:30:37 -0700692
bsalomon6eb03cc2014-08-07 14:28:50 -0700693 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700694 if (targets.count() == 1) {
695 config = ""; // Only print the config if we run the same bench on more than one.
696 }
mtklein53d25622014-09-18 07:39:42 -0700697 SkDebugf("%4dM\t%s\t%s\n"
698 , sk_tools::getMaxResidentSetSizeMB()
699 , bench->getUniqueName()
700 , config);
mtkleina189ccd2014-07-14 12:28:47 -0700701 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700702 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700703 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700704 }
mtklein96289052014-09-10 12:05:59 -0700705 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -0700706 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700707 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700708 config = ""; // Only print the config if we run the same bench on more than one.
709 }
mtklein96289052014-09-10 12:05:59 -0700710 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700711 } else {
712 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleinafb43792014-08-19 15:55:55 -0700713 SkDebugf("%4dM\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
714 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700715 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700716 , HUMANIZE(stats.min)
717 , HUMANIZE(stats.median)
718 , HUMANIZE(stats.mean)
719 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700720 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700721 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700722 , config
mtklein96289052014-09-10 12:05:59 -0700723 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -0700724 );
725 }
bsalomon06cddec2014-10-24 10:40:50 -0700726#if SK_SUPPORT_GPU && GR_CACHE_STATS
727 if (FLAGS_veryVerbose &&
728 Benchmark::kGPU_Backend == targets[j]->config.backend) {
729 gGrFactory->get(targets[j]->config.ctxType)->printCacheStats();
730 }
731#endif
mtkleinf3723212014-06-25 14:08:00 -0700732 }
mtkleinbb6a0282014-07-01 08:43:42 -0700733 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400734
bsalomon06cddec2014-10-24 10:40:50 -0700735#if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700736 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700737 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700738 }
739 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700740 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400741 }
bsalomon06cddec2014-10-24 10:40:50 -0700742#endif
mtkleinf3723212014-06-25 14:08:00 -0700743 }
744
745 return 0;
746}
747
748#if !defined SK_BUILD_FOR_IOS
caryclark17f0b6d2014-07-22 10:15:34 -0700749int main(int argc, char** argv) {
750 SkCommandLineFlags::Parse(argc, argv);
751 return nanobench_main();
mtkleinf3723212014-06-25 14:08:00 -0700752}
753#endif