blob: 250f438d6ac4e31c8f8f0941e0e1dfa7710640dc [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?");
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) {
128 SkDebugf("ERROR: clamping loops from %d to 1.\n", loops);
129 return 1;
130 }
131 if (loops > FLAGS_maxLoops) {
132 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
133 return FLAGS_maxLoops;
134 }
135 return loops;
136}
137
bsalomon6eb03cc2014-08-07 14:28:50 -0700138static bool write_canvas_png(SkCanvas* canvas, const SkString& filename) {
139 if (filename.isEmpty()) {
140 return false;
141 }
reede5ea5002014-09-03 11:54:58 -0700142 if (kUnknown_SkColorType == canvas->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700143 return false;
144 }
145 SkBitmap bmp;
146 bmp.setInfo(canvas->imageInfo());
147 if (!canvas->readPixels(&bmp, 0, 0)) {
148 SkDebugf("Can't read canvas pixels.\n");
149 return false;
150 }
151 SkString dir = SkOSPath::Dirname(filename.c_str());
152 if (!sk_mkdir(dir.c_str())) {
153 SkDebugf("Can't make dir %s.\n", dir.c_str());
154 return false;
155 }
156 SkFILEWStream stream(filename.c_str());
157 if (!stream.isValid()) {
158 SkDebugf("Can't write %s.\n", filename.c_str());
159 return false;
160 }
161 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
162 SkDebugf("Can't encode a PNG.\n");
163 return false;
164 }
165 return true;
166}
167
168static int kFailedLoops = -2;
mtkleinbb6a0282014-07-01 08:43:42 -0700169static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
170 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700171 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700172 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700173 if (kAutoTuneLoops == FLAGS_loops) {
174 while (bench_plus_overhead < overhead) {
175 if (round++ == FLAGS_maxCalibrationAttempts) {
176 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700177 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700178 return kFailedLoops;
179 }
180 bench_plus_overhead = time(1, bench, canvas, NULL);
mtklein55b0ffc2014-07-17 08:38:23 -0700181 }
mtklein2069e222014-08-04 13:57:39 -0700182 }
mtkleinf3723212014-06-25 14:08:00 -0700183
mtkleinbb6a0282014-07-01 08:43:42 -0700184 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700185 // We'll pick N to make timer overhead negligible:
186 //
mtkleinbb6a0282014-07-01 08:43:42 -0700187 // overhead
188 // ------------------------- < FLAGS_overheadGoal
189 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700190 //
mtkleinbb6a0282014-07-01 08:43:42 -0700191 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700192 //
193 // Doing some math, we get:
194 //
mtkleinbb6a0282014-07-01 08:43:42 -0700195 // (overhead / FLAGS_overheadGoal) - overhead
196 // ------------------------------------------ < N
197 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700198 //
199 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700200 int loops = FLAGS_loops;
201 if (kAutoTuneLoops == loops) {
202 const double numer = overhead / FLAGS_overheadGoal - overhead;
203 const double denom = bench_plus_overhead - overhead;
204 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700205 loops = clamp_loops(loops);
206 } else {
207 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700208 }
mtkleinbb6a0282014-07-01 08:43:42 -0700209
210 for (int i = 0; i < FLAGS_samples; i++) {
211 samples[i] = time(loops, bench, canvas, NULL) / loops;
212 }
213 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700214}
215
mtkleinbb6a0282014-07-01 08:43:42 -0700216#if SK_SUPPORT_GPU
kkinnunen9e61bb72014-10-09 05:24:15 -0700217static int gpu_bench(SkGLContext* gl,
mtkleinbb6a0282014-07-01 08:43:42 -0700218 Benchmark* bench,
219 SkCanvas* canvas,
220 double* samples) {
bsalomonc2553372014-07-22 13:09:05 -0700221 gl->makeCurrent();
mtkleinbb6a0282014-07-01 08:43:42 -0700222 // Make sure we're done with whatever came before.
mtklein9bc86ed2014-07-01 10:02:42 -0700223 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700224
225 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700226 int loops = FLAGS_loops;
227 if (kAutoTuneLoops == loops) {
228 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700229 double elapsed = 0;
230 do {
231 loops *= 2;
232 // If the GPU lets frames lag at all, we need to make sure we're timing
233 // _this_ round, not still timing last round. We force this by looping
234 // more times than any reasonable GPU will allow frames to lag.
235 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
236 elapsed = time(loops, bench, canvas, gl);
237 }
238 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700239
mtkleina189ccd2014-07-14 12:28:47 -0700240 // We've overshot at least a little. Scale back linearly.
241 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700242 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700243
mtkleina189ccd2014-07-14 12:28:47 -0700244 // Might as well make sure we're not still timing our calibration.
245 SK_GL(*gl, Finish());
reed53249782014-10-10 09:09:52 -0700246 } else {
247 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700248 }
mtkleinbb6a0282014-07-01 08:43:42 -0700249
250 // Pretty much the same deal as the calibration: do some warmup to make
251 // sure we're timing steady-state pipelined frames.
252 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
253 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700254 }
mtkleinbb6a0282014-07-01 08:43:42 -0700255
256 // Now, actually do the timing!
257 for (int i = 0; i < FLAGS_samples; i++) {
258 samples[i] = time(loops, bench, canvas, gl) / loops;
259 }
260 return loops;
261}
262#endif
263
264static SkString to_lower(const char* str) {
265 SkString lower(str);
266 for (size_t i = 0; i < lower.size(); i++) {
267 lower[i] = tolower(lower[i]);
268 }
269 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700270}
271
bsalomonc2553372014-07-22 13:09:05 -0700272struct Config {
273 const char* name;
mtkleinbb6a0282014-07-01 08:43:42 -0700274 Benchmark::Backend backend;
bsalomonc2553372014-07-22 13:09:05 -0700275 SkColorType color;
276 SkAlphaType alpha;
277 int samples;
278#if SK_SUPPORT_GPU
279 GrContextFactory::GLContextType ctxType;
280#else
281 int bogusInt;
282#endif
283};
284
285struct Target {
286 explicit Target(const Config& c) : config(c) {}
287 const Config config;
mtkleinbb6a0282014-07-01 08:43:42 -0700288 SkAutoTDelete<SkSurface> surface;
289#if SK_SUPPORT_GPU
kkinnunen9e61bb72014-10-09 05:24:15 -0700290 SkGLContext* gl;
mtkleinbb6a0282014-07-01 08:43:42 -0700291#endif
292};
mtkleinf3723212014-06-25 14:08:00 -0700293
bsalomonc2553372014-07-22 13:09:05 -0700294static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700295 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700296 if (to_lower(FLAGS_config[i]).equals(name)) {
297 return true;
mtkleinf3723212014-06-25 14:08:00 -0700298 }
299 }
bsalomonc2553372014-07-22 13:09:05 -0700300 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700301}
302
bsalomonc2553372014-07-22 13:09:05 -0700303#if SK_SUPPORT_GPU
304static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
305 int sampleCnt) {
306 if (!is_cpu_config_allowed(name)) {
307 return false;
308 }
krajcevski69a55602014-08-13 10:46:31 -0700309 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700310 return sampleCnt <= ctx->getMaxSampleCount();
311 }
312 return false;
313}
314#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700315
bsalomonc2553372014-07-22 13:09:05 -0700316#if SK_SUPPORT_GPU
317#define kBogusGLContextType GrContextFactory::kNative_GLContextType
318#else
319#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700320#endif
bsalomonc2553372014-07-22 13:09:05 -0700321
322// Append all configs that are enabled and supported.
323static void create_configs(SkTDArray<Config>* configs) {
324 #define CPU_CONFIG(name, backend, color, alpha) \
325 if (is_cpu_config_allowed(#name)) { \
326 Config config = { #name, Benchmark::backend, color, alpha, 0, kBogusGLContextType }; \
327 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700328 }
mtkleine714e752014-07-31 12:13:48 -0700329
mtklein40b32be2014-07-09 08:46:49 -0700330 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700331 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
332 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
333 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700334 }
mtkleinbb6a0282014-07-01 08:43:42 -0700335
336#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700337 #define GPU_CONFIG(name, ctxType, samples) \
338 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
339 Config config = { \
340 #name, \
341 Benchmark::kGPU_Backend, \
342 kN32_SkColorType, \
343 kPremul_SkAlphaType, \
344 samples, \
345 GrContextFactory::ctxType }; \
346 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700347 }
mtkleine714e752014-07-31 12:13:48 -0700348
mtklein40b32be2014-07-09 08:46:49 -0700349 if (FLAGS_gpu) {
bsalomonc2553372014-07-22 13:09:05 -0700350 GPU_CONFIG(gpu, kNative_GLContextType, 0)
351 GPU_CONFIG(msaa4, kNative_GLContextType, 4)
352 GPU_CONFIG(msaa16, kNative_GLContextType, 16)
353 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4)
354 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16)
355 GPU_CONFIG(debug, kDebug_GLContextType, 0)
356 GPU_CONFIG(nullgpu, kNull_GLContextType, 0)
bsalomon3b4d0772014-08-06 10:52:33 -0700357#ifdef SK_ANGLE
358 GPU_CONFIG(angle, kANGLE_GLContextType, 0)
359#endif
mtklein40b32be2014-07-09 08:46:49 -0700360 }
mtkleinbb6a0282014-07-01 08:43:42 -0700361#endif
mtkleinf3723212014-06-25 14:08:00 -0700362}
363
bsalomonc2553372014-07-22 13:09:05 -0700364// If bench is enabled for config, returns a Target* for it, otherwise NULL.
365static Target* is_enabled(Benchmark* bench, const Config& config) {
366 if (!bench->isSuitableFor(config.backend)) {
367 return NULL;
368 }
369
reede5ea5002014-09-03 11:54:58 -0700370 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
371 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700372
373 Target* target = new Target(config);
374
375 if (Benchmark::kRaster_Backend == config.backend) {
376 target->surface.reset(SkSurface::NewRaster(info));
377 }
378#if SK_SUPPORT_GPU
379 else if (Benchmark::kGPU_Backend == config.backend) {
krajcevski69a55602014-08-13 10:46:31 -0700380 target->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(config.ctxType), info,
bsalomonc2553372014-07-22 13:09:05 -0700381 config.samples));
krajcevski69a55602014-08-13 10:46:31 -0700382 target->gl = gGrFactory->getGLContext(config.ctxType);
bsalomonc2553372014-07-22 13:09:05 -0700383 }
384#endif
385
386 if (Benchmark::kNonRendering_Backend != config.backend && !target->surface.get()) {
387 delete target;
388 return NULL;
389 }
390 return target;
391}
392
393// Creates targets for a benchmark and a set of configs.
394static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
395 const SkTDArray<Config>& configs) {
396 for (int i = 0; i < configs.count(); ++i) {
397 if (Target* t = is_enabled(b, configs[i])) {
398 targets->push(t);
399 }
mtkleine714e752014-07-31 12:13:48 -0700400
bsalomonc2553372014-07-22 13:09:05 -0700401 }
402}
403
jcgregoriobf5e5232014-07-17 13:14:16 -0700404#if SK_SUPPORT_GPU
kkinnunen9e61bb72014-10-09 05:24:15 -0700405static void fill_gpu_options(ResultsWriter* log, SkGLContext* ctx) {
jcgregorio05c45602014-07-17 13:54:36 -0700406 const GrGLubyte* version;
jcgregoriobf5e5232014-07-17 13:14:16 -0700407 SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION));
408 log->configOption("GL_VERSION", (const char*)(version));
409
410 SK_GL_RET(*ctx, version, GetString(GR_GL_RENDERER));
411 log->configOption("GL_RENDERER", (const char*) version);
412
413 SK_GL_RET(*ctx, version, GetString(GR_GL_VENDOR));
414 log->configOption("GL_VENDOR", (const char*) version);
415
416 SK_GL_RET(*ctx, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
417 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
418}
419#endif
420
mtkleine714e752014-07-31 12:13:48 -0700421class BenchmarkStream {
422public:
mtklein92007582014-08-01 07:46:52 -0700423 BenchmarkStream() : fBenches(BenchRegistry::Head())
424 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700425 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700426 , fCurrentScale(0)
427 , fCurrentSKP(0) {
428 for (int i = 0; i < FLAGS_skps.count(); i++) {
429 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
430 fSKPs.push_back() = FLAGS_skps[i];
431 } else {
432 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
433 SkString path;
434 while (it.next(&path)) {
435 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
436 }
437 }
438 }
mtkleine714e752014-07-31 12:13:48 -0700439
mtklein92007582014-08-01 07:46:52 -0700440 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
441 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
442 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
443 exit(1);
444 }
445
446 for (int i = 0; i < FLAGS_scales.count(); i++) {
447 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
448 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
449 exit(1);
450 }
451 }
452 }
453
mtkleinfd731ce2014-09-10 12:19:30 -0700454 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
455 // Not strictly necessary, as it will be checked again later,
456 // but helps to avoid a lot of pointless work if we're going to skip it.
457 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
458 return false;
459 }
460
461 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
462 if (stream.get() == NULL) {
463 SkDebugf("Could not read %s.\n", path);
464 return false;
465 }
466
mtklein963504b2014-09-17 06:58:39 -0700467 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700468 if (pic->get() == NULL) {
469 SkDebugf("Could not read %s as an SkPicture.\n", path);
470 return false;
471 }
472 return true;
473 }
474
mtklein92007582014-08-01 07:46:52 -0700475 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700476 if (fBenches) {
477 Benchmark* bench = fBenches->factory()(NULL);
478 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700479 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700480 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700481 return bench;
482 }
mtklein92007582014-08-01 07:46:52 -0700483
mtkleine714e752014-07-31 12:13:48 -0700484 while (fGMs) {
485 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
486 fGMs = fGMs->next();
487 if (gm->getFlags() & skiagm::GM::kAsBench_Flag) {
mtklein92007582014-08-01 07:46:52 -0700488 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700489 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700490 return SkNEW_ARGS(GMBench, (gm.detach()));
491 }
492 }
mtklein92007582014-08-01 07:46:52 -0700493
mtkleinfd731ce2014-09-10 12:19:30 -0700494 // First add all .skps as RecordingBenches.
495 while (fCurrentRecording < fSKPs.count()) {
496 const SkString& path = fSKPs[fCurrentRecording++];
497 SkAutoTUnref<SkPicture> pic;
498 if (!ReadPicture(path.c_str(), &pic)) {
499 continue;
500 }
501 SkString name = SkOSPath::Basename(path.c_str());
502 fSourceType = "skp";
503 fBenchType = "recording";
504 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
505 }
506
507 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700508 while (fCurrentScale < fScales.count()) {
509 while (fCurrentSKP < fSKPs.count()) {
510 const SkString& path = fSKPs[fCurrentSKP++];
mtkleinfd731ce2014-09-10 12:19:30 -0700511 SkAutoTUnref<SkPicture> pic;
512 if (!ReadPicture(path.c_str(), &pic)) {
mtklein92007582014-08-01 07:46:52 -0700513 continue;
514 }
mtklein20840502014-08-21 15:51:22 -0700515 if (FLAGS_bbh) {
516 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
517 // Here we use an SkTileGrid with parameters optimized for FLAGS_clip.
518 const SkTileGridFactory::TileGridInfo info = {
519 SkISize::Make(fClip.width(), fClip.height()), // tile interval
520 SkISize::Make(0,0), // margin
521 SkIPoint::Make(fClip.left(), fClip.top()), // offset
522 };
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
bsalomon6eb03cc2014-08-07 14:28:50 -0700579 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700580 FLAGS_samples = 1;
581 FLAGS_gpuFrameLag = 0;
582 }
583
bsalomon6eb03cc2014-08-07 14:28:50 -0700584 if (!FLAGS_writePath.isEmpty()) {
585 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
586 if (!sk_mkdir(FLAGS_writePath[0])) {
587 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
588 FLAGS_writePath.set(0, NULL);
589 }
590 }
591
mtklein1915b622014-08-20 11:45:00 -0700592 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700593 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700594 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700595 }
mtklein1915b622014-08-20 11:45:00 -0700596
597 if (1 == FLAGS_properties.count() % 2) {
598 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
599 return 1;
600 }
601 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
602 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
603 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700604
605 if (1 == FLAGS_key.count() % 2) {
606 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
607 return 1;
608 }
609 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700610 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700611 }
mtklein60317d0f2014-07-14 11:30:37 -0700612
mtkleinf3723212014-06-25 14:08:00 -0700613 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700614 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400615
mtkleinbb6a0282014-07-01 08:43:42 -0700616 SkAutoTMalloc<double> samples(FLAGS_samples);
617
bsalomon6eb03cc2014-08-07 14:28:50 -0700618 if (kAutoTuneLoops != FLAGS_loops) {
619 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700620 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700621 // No header.
622 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700623 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700624 } else {
qiankun.miao8247ec32014-09-09 19:24:36 -0700625 SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
626 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700627 }
628
bsalomonc2553372014-07-22 13:09:05 -0700629 SkTDArray<Config> configs;
630 create_configs(&configs);
631
mtkleine070c2b2014-10-14 08:40:43 -0700632 int runs = 0;
mtklein92007582014-08-01 07:46:52 -0700633 BenchmarkStream benchStream;
634 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700635 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -0700636 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -0700637 continue;
638 }
639
mtkleinbb6a0282014-07-01 08:43:42 -0700640 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700641 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700642
jcgregoriobf5e5232014-07-17 13:14:16 -0700643 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -0700644 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700645 bench->preDraw();
646 }
mtkleinbb6a0282014-07-01 08:43:42 -0700647 for (int j = 0; j < targets.count(); j++) {
648 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
bsalomonc2553372014-07-22 13:09:05 -0700649 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700650
mtkleinbb6a0282014-07-01 08:43:42 -0700651 const int loops =
652#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700653 Benchmark::kGPU_Backend == targets[j]->config.backend
mtkleinbb6a0282014-07-01 08:43:42 -0700654 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
655 :
656#endif
657 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700658
bsalomon49f085d2014-09-05 13:34:00 -0700659 if (canvas && !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700660 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -0700661 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -0700662 pngFilename.append(".png");
663 write_canvas_png(canvas, pngFilename);
664 }
665
666 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700667 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400668 continue;
669 }
670
mtkleinf3723212014-06-25 14:08:00 -0700671 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700672 log->config(config);
mtklein96289052014-09-10 12:05:59 -0700673 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -0700674 benchStream.fillCurrentOptions(log.get());
jcgregoriobf5e5232014-07-17 13:14:16 -0700675#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700676 if (Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein1915b622014-08-20 11:45:00 -0700677 fill_gpu_options(log.get(), targets[j]->gl);
jcgregoriobf5e5232014-07-17 13:14:16 -0700678 }
679#endif
mtklein1915b622014-08-20 11:45:00 -0700680 log->timer("min_ms", stats.min);
681 log->timer("median_ms", stats.median);
682 log->timer("mean_ms", stats.mean);
683 log->timer("max_ms", stats.max);
684 log->timer("stddev_ms", sqrt(stats.var));
mtkleine070c2b2014-10-14 08:40:43 -0700685 if (runs++ % FLAGS_flushEvery == 0) {
686 log->flush();
687 }
mtklein60317d0f2014-07-14 11:30:37 -0700688
bsalomon6eb03cc2014-08-07 14:28:50 -0700689 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700690 if (targets.count() == 1) {
691 config = ""; // Only print the config if we run the same bench on more than one.
692 }
mtklein53d25622014-09-18 07:39:42 -0700693 SkDebugf("%4dM\t%s\t%s\n"
694 , sk_tools::getMaxResidentSetSizeMB()
695 , bench->getUniqueName()
696 , config);
mtkleina189ccd2014-07-14 12:28:47 -0700697 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700698 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700699 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700700 }
mtklein96289052014-09-10 12:05:59 -0700701 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -0700702 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700703 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700704 config = ""; // Only print the config if we run the same bench on more than one.
705 }
mtklein96289052014-09-10 12:05:59 -0700706 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700707 } else {
708 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleinafb43792014-08-19 15:55:55 -0700709 SkDebugf("%4dM\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
710 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700711 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700712 , HUMANIZE(stats.min)
713 , HUMANIZE(stats.median)
714 , HUMANIZE(stats.mean)
715 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700716 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700717 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700718 , config
mtklein96289052014-09-10 12:05:59 -0700719 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -0700720 );
721 }
722 }
mtkleinbb6a0282014-07-01 08:43:42 -0700723 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400724
725 #if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700726 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700727 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700728 }
729 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700730 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400731 }
732 #endif
mtkleinf3723212014-06-25 14:08:00 -0700733 }
734
735 return 0;
736}
737
738#if !defined SK_BUILD_FOR_IOS
caryclark17f0b6d2014-07-22 10:15:34 -0700739int main(int argc, char** argv) {
740 SkCommandLineFlags::Parse(argc, argv);
741 return nanobench_main();
mtkleinf3723212014-06-25 14:08:00 -0700742}
743#endif