blob: aade5905410a31538cd38fa82e77ad73d5f3cd5f [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
mtklein6838d852014-10-29 14:15:10 -070020#include "SkBBoxHierarchy.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?");
mtkleine070c2b2014-10-14 08:40:43 -070073DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein92007582014-08-01 07:46:52 -070074
mtkleinf3723212014-06-25 14:08:00 -070075static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -070076 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
77 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3);
78 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6);
mtklein62386882014-07-15 10:30:31 -070079#ifdef SK_BUILD_FOR_WIN
mtkleindc5bbab2014-09-24 06:34:09 -070080 if (ms < 1) return SkStringPrintf("%.3gus", ms*1e3);
mtklein62386882014-07-15 10:30:31 -070081#else
mtkleindc5bbab2014-09-24 06:34:09 -070082 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3);
mtklein62386882014-07-15 10:30:31 -070083#endif
mtkleinf3723212014-06-25 14:08:00 -070084 return SkStringPrintf("%.3gms", ms);
85}
mtklein55b0ffc2014-07-17 08:38:23 -070086#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -070087
kkinnunen9e61bb72014-10-09 05:24:15 -070088static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContext* gl) {
bsalomon6eb03cc2014-08-07 14:28:50 -070089 if (canvas) {
90 canvas->clear(SK_ColorWHITE);
91 }
mtkleinbb6a0282014-07-01 08:43:42 -070092 WallTimer timer;
93 timer.start();
94 if (bench) {
95 bench->draw(loops, canvas);
96 }
97 if (canvas) {
98 canvas->flush();
99 }
100#if SK_SUPPORT_GPU
101 if (gl) {
102 SK_GL(*gl, Flush());
103 gl->swapBuffers();
104 }
105#endif
106 timer.end();
107 return timer.fWall;
108}
109
mtkleinf3723212014-06-25 14:08:00 -0700110static double estimate_timer_overhead() {
111 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700112 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -0700113 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -0700114 }
115 return overhead / FLAGS_overheadLoops;
116}
117
reed53249782014-10-10 09:09:52 -0700118static int detect_forever_loops(int loops) {
119 // look for a magic run-forever value
120 if (loops < 0) {
121 loops = SK_MaxS32;
122 }
123 return loops;
124}
125
mtklein55b0ffc2014-07-17 08:38:23 -0700126static int clamp_loops(int loops) {
127 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800128 SkDebugf("ERROR: clamping loops from %d to 1. "
129 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700130 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 {
mtklein527930f2014-11-06 08:04:34 -0800232 if (1<<30 == loops) {
233 // We're about to wrap. Something's wrong with the bench.
234 loops = 0;
235 break;
236 }
mtkleina189ccd2014-07-14 12:28:47 -0700237 loops *= 2;
238 // If the GPU lets frames lag at all, we need to make sure we're timing
239 // _this_ round, not still timing last round. We force this by looping
240 // more times than any reasonable GPU will allow frames to lag.
241 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
242 elapsed = time(loops, bench, canvas, gl);
243 }
244 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700245
mtkleina189ccd2014-07-14 12:28:47 -0700246 // We've overshot at least a little. Scale back linearly.
247 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700248 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700249
mtkleina189ccd2014-07-14 12:28:47 -0700250 // Might as well make sure we're not still timing our calibration.
251 SK_GL(*gl, Finish());
reed53249782014-10-10 09:09:52 -0700252 } else {
253 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700254 }
mtkleinbb6a0282014-07-01 08:43:42 -0700255
256 // Pretty much the same deal as the calibration: do some warmup to make
257 // sure we're timing steady-state pipelined frames.
258 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
259 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700260 }
mtkleinbb6a0282014-07-01 08:43:42 -0700261
262 // Now, actually do the timing!
263 for (int i = 0; i < FLAGS_samples; i++) {
264 samples[i] = time(loops, bench, canvas, gl) / loops;
265 }
266 return loops;
267}
268#endif
269
270static SkString to_lower(const char* str) {
271 SkString lower(str);
272 for (size_t i = 0; i < lower.size(); i++) {
273 lower[i] = tolower(lower[i]);
274 }
275 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700276}
277
bsalomonc2553372014-07-22 13:09:05 -0700278struct Config {
279 const char* name;
mtkleinbb6a0282014-07-01 08:43:42 -0700280 Benchmark::Backend backend;
bsalomonc2553372014-07-22 13:09:05 -0700281 SkColorType color;
282 SkAlphaType alpha;
283 int samples;
284#if SK_SUPPORT_GPU
285 GrContextFactory::GLContextType ctxType;
286#else
287 int bogusInt;
288#endif
289};
290
291struct Target {
292 explicit Target(const Config& c) : config(c) {}
293 const Config config;
mtkleinbb6a0282014-07-01 08:43:42 -0700294 SkAutoTDelete<SkSurface> surface;
295#if SK_SUPPORT_GPU
kkinnunen9e61bb72014-10-09 05:24:15 -0700296 SkGLContext* gl;
mtkleinbb6a0282014-07-01 08:43:42 -0700297#endif
298};
mtkleinf3723212014-06-25 14:08:00 -0700299
bsalomonc2553372014-07-22 13:09:05 -0700300static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700301 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700302 if (to_lower(FLAGS_config[i]).equals(name)) {
303 return true;
mtkleinf3723212014-06-25 14:08:00 -0700304 }
305 }
bsalomonc2553372014-07-22 13:09:05 -0700306 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700307}
308
bsalomonc2553372014-07-22 13:09:05 -0700309#if SK_SUPPORT_GPU
310static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
311 int sampleCnt) {
312 if (!is_cpu_config_allowed(name)) {
313 return false;
314 }
krajcevski69a55602014-08-13 10:46:31 -0700315 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700316 return sampleCnt <= ctx->getMaxSampleCount();
317 }
318 return false;
319}
320#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700321
bsalomonc2553372014-07-22 13:09:05 -0700322#if SK_SUPPORT_GPU
323#define kBogusGLContextType GrContextFactory::kNative_GLContextType
324#else
325#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700326#endif
bsalomonc2553372014-07-22 13:09:05 -0700327
328// Append all configs that are enabled and supported.
329static void create_configs(SkTDArray<Config>* configs) {
330 #define CPU_CONFIG(name, backend, color, alpha) \
331 if (is_cpu_config_allowed(#name)) { \
332 Config config = { #name, Benchmark::backend, color, alpha, 0, kBogusGLContextType }; \
333 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700334 }
mtkleine714e752014-07-31 12:13:48 -0700335
mtklein40b32be2014-07-09 08:46:49 -0700336 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700337 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
338 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
339 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700340 }
mtkleinbb6a0282014-07-01 08:43:42 -0700341
342#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700343 #define GPU_CONFIG(name, ctxType, samples) \
344 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
345 Config config = { \
346 #name, \
347 Benchmark::kGPU_Backend, \
348 kN32_SkColorType, \
349 kPremul_SkAlphaType, \
350 samples, \
351 GrContextFactory::ctxType }; \
352 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700353 }
mtkleine714e752014-07-31 12:13:48 -0700354
mtklein40b32be2014-07-09 08:46:49 -0700355 if (FLAGS_gpu) {
bsalomonc2553372014-07-22 13:09:05 -0700356 GPU_CONFIG(gpu, kNative_GLContextType, 0)
357 GPU_CONFIG(msaa4, kNative_GLContextType, 4)
358 GPU_CONFIG(msaa16, kNative_GLContextType, 16)
359 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4)
360 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16)
361 GPU_CONFIG(debug, kDebug_GLContextType, 0)
362 GPU_CONFIG(nullgpu, kNull_GLContextType, 0)
bsalomon3b4d0772014-08-06 10:52:33 -0700363#ifdef SK_ANGLE
364 GPU_CONFIG(angle, kANGLE_GLContextType, 0)
365#endif
mtklein40b32be2014-07-09 08:46:49 -0700366 }
mtkleinbb6a0282014-07-01 08:43:42 -0700367#endif
mtkleinf3723212014-06-25 14:08:00 -0700368}
369
bsalomonc2553372014-07-22 13:09:05 -0700370// If bench is enabled for config, returns a Target* for it, otherwise NULL.
371static Target* is_enabled(Benchmark* bench, const Config& config) {
372 if (!bench->isSuitableFor(config.backend)) {
373 return NULL;
374 }
375
reede5ea5002014-09-03 11:54:58 -0700376 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
377 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700378
379 Target* target = new Target(config);
380
381 if (Benchmark::kRaster_Backend == config.backend) {
382 target->surface.reset(SkSurface::NewRaster(info));
383 }
384#if SK_SUPPORT_GPU
385 else if (Benchmark::kGPU_Backend == config.backend) {
krajcevski69a55602014-08-13 10:46:31 -0700386 target->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(config.ctxType), info,
bsalomonc2553372014-07-22 13:09:05 -0700387 config.samples));
krajcevski69a55602014-08-13 10:46:31 -0700388 target->gl = gGrFactory->getGLContext(config.ctxType);
bsalomonc2553372014-07-22 13:09:05 -0700389 }
390#endif
391
392 if (Benchmark::kNonRendering_Backend != config.backend && !target->surface.get()) {
393 delete target;
394 return NULL;
395 }
396 return target;
397}
398
399// Creates targets for a benchmark and a set of configs.
400static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
401 const SkTDArray<Config>& configs) {
402 for (int i = 0; i < configs.count(); ++i) {
403 if (Target* t = is_enabled(b, configs[i])) {
404 targets->push(t);
405 }
mtkleine714e752014-07-31 12:13:48 -0700406
bsalomonc2553372014-07-22 13:09:05 -0700407 }
408}
409
jcgregoriobf5e5232014-07-17 13:14:16 -0700410#if SK_SUPPORT_GPU
kkinnunen9e61bb72014-10-09 05:24:15 -0700411static void fill_gpu_options(ResultsWriter* log, SkGLContext* ctx) {
jcgregorio05c45602014-07-17 13:54:36 -0700412 const GrGLubyte* version;
jcgregoriobf5e5232014-07-17 13:14:16 -0700413 SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION));
414 log->configOption("GL_VERSION", (const char*)(version));
415
416 SK_GL_RET(*ctx, version, GetString(GR_GL_RENDERER));
417 log->configOption("GL_RENDERER", (const char*) version);
418
419 SK_GL_RET(*ctx, version, GetString(GR_GL_VENDOR));
420 log->configOption("GL_VENDOR", (const char*) version);
421
422 SK_GL_RET(*ctx, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
423 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
424}
425#endif
426
mtkleine714e752014-07-31 12:13:48 -0700427class BenchmarkStream {
428public:
mtklein92007582014-08-01 07:46:52 -0700429 BenchmarkStream() : fBenches(BenchRegistry::Head())
430 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700431 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700432 , fCurrentScale(0)
433 , fCurrentSKP(0) {
434 for (int i = 0; i < FLAGS_skps.count(); i++) {
435 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
436 fSKPs.push_back() = FLAGS_skps[i];
437 } else {
438 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
439 SkString path;
440 while (it.next(&path)) {
441 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
442 }
443 }
444 }
mtkleine714e752014-07-31 12:13:48 -0700445
mtklein92007582014-08-01 07:46:52 -0700446 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
447 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
448 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
449 exit(1);
450 }
451
452 for (int i = 0; i < FLAGS_scales.count(); i++) {
453 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
454 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
455 exit(1);
456 }
457 }
458 }
459
mtkleinfd731ce2014-09-10 12:19:30 -0700460 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
461 // Not strictly necessary, as it will be checked again later,
462 // but helps to avoid a lot of pointless work if we're going to skip it.
463 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
464 return false;
465 }
466
467 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
468 if (stream.get() == NULL) {
469 SkDebugf("Could not read %s.\n", path);
470 return false;
471 }
472
mtklein963504b2014-09-17 06:58:39 -0700473 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700474 if (pic->get() == NULL) {
475 SkDebugf("Could not read %s as an SkPicture.\n", path);
476 return false;
477 }
478 return true;
479 }
480
mtklein92007582014-08-01 07:46:52 -0700481 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700482 if (fBenches) {
483 Benchmark* bench = fBenches->factory()(NULL);
484 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700485 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700486 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700487 return bench;
488 }
mtklein92007582014-08-01 07:46:52 -0700489
mtkleine714e752014-07-31 12:13:48 -0700490 while (fGMs) {
491 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
492 fGMs = fGMs->next();
493 if (gm->getFlags() & skiagm::GM::kAsBench_Flag) {
mtklein92007582014-08-01 07:46:52 -0700494 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700495 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700496 return SkNEW_ARGS(GMBench, (gm.detach()));
497 }
498 }
mtklein92007582014-08-01 07:46:52 -0700499
mtkleinfd731ce2014-09-10 12:19:30 -0700500 // First add all .skps as RecordingBenches.
501 while (fCurrentRecording < fSKPs.count()) {
502 const SkString& path = fSKPs[fCurrentRecording++];
503 SkAutoTUnref<SkPicture> pic;
504 if (!ReadPicture(path.c_str(), &pic)) {
505 continue;
506 }
507 SkString name = SkOSPath::Basename(path.c_str());
508 fSourceType = "skp";
509 fBenchType = "recording";
510 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
511 }
512
513 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700514 while (fCurrentScale < fScales.count()) {
515 while (fCurrentSKP < fSKPs.count()) {
516 const SkString& path = fSKPs[fCurrentSKP++];
mtkleinfd731ce2014-09-10 12:19:30 -0700517 SkAutoTUnref<SkPicture> pic;
518 if (!ReadPicture(path.c_str(), &pic)) {
mtklein92007582014-08-01 07:46:52 -0700519 continue;
520 }
mtklein20840502014-08-21 15:51:22 -0700521 if (FLAGS_bbh) {
522 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
mtklein6838d852014-10-29 14:15:10 -0700523 SkRTreeFactory factory;
mtklein20840502014-08-21 15:51:22 -0700524 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) {
mtklein6838d852014-10-29 14:15:10 -0700729 gGrFactory->get(targets[j]->config.ctxType)->printCacheStats();
bsalomon06cddec2014-10-24 10:40:50 -0700730 }
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