blob: 1f5ae60f822ab9d28b84c37b37465398544616b5 [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;
jvanverth4736e142014-11-07 07:12:46 -0800286 bool useDFText;
bsalomonc2553372014-07-22 13:09:05 -0700287#else
288 int bogusInt;
jvanverth4736e142014-11-07 07:12:46 -0800289 bool bogusBool;
bsalomonc2553372014-07-22 13:09:05 -0700290#endif
291};
292
293struct Target {
294 explicit Target(const Config& c) : config(c) {}
295 const Config config;
mtkleinbb6a0282014-07-01 08:43:42 -0700296 SkAutoTDelete<SkSurface> surface;
297#if SK_SUPPORT_GPU
kkinnunen9e61bb72014-10-09 05:24:15 -0700298 SkGLContext* gl;
mtkleinbb6a0282014-07-01 08:43:42 -0700299#endif
300};
mtkleinf3723212014-06-25 14:08:00 -0700301
bsalomonc2553372014-07-22 13:09:05 -0700302static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700303 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700304 if (to_lower(FLAGS_config[i]).equals(name)) {
305 return true;
mtkleinf3723212014-06-25 14:08:00 -0700306 }
307 }
bsalomonc2553372014-07-22 13:09:05 -0700308 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700309}
310
bsalomonc2553372014-07-22 13:09:05 -0700311#if SK_SUPPORT_GPU
312static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
313 int sampleCnt) {
314 if (!is_cpu_config_allowed(name)) {
315 return false;
316 }
krajcevski69a55602014-08-13 10:46:31 -0700317 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700318 return sampleCnt <= ctx->getMaxSampleCount();
319 }
320 return false;
321}
322#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700323
bsalomonc2553372014-07-22 13:09:05 -0700324#if SK_SUPPORT_GPU
325#define kBogusGLContextType GrContextFactory::kNative_GLContextType
326#else
327#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700328#endif
bsalomonc2553372014-07-22 13:09:05 -0700329
330// Append all configs that are enabled and supported.
331static void create_configs(SkTDArray<Config>* configs) {
jvanverth4736e142014-11-07 07:12:46 -0800332 #define CPU_CONFIG(name, backend, color, alpha) \
333 if (is_cpu_config_allowed(#name)) { \
334 Config config = { #name, Benchmark::backend, color, alpha, 0, \
335 kBogusGLContextType, false }; \
336 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700337 }
mtkleine714e752014-07-31 12:13:48 -0700338
mtklein40b32be2014-07-09 08:46:49 -0700339 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700340 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
341 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
342 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700343 }
mtkleinbb6a0282014-07-01 08:43:42 -0700344
345#if SK_SUPPORT_GPU
jvanverth4736e142014-11-07 07:12:46 -0800346 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
bsalomonc2553372014-07-22 13:09:05 -0700347 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
348 Config config = { \
349 #name, \
350 Benchmark::kGPU_Backend, \
351 kN32_SkColorType, \
352 kPremul_SkAlphaType, \
353 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800354 GrContextFactory::ctxType, \
355 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700356 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700357 }
mtkleine714e752014-07-31 12:13:48 -0700358
mtklein40b32be2014-07-09 08:46:49 -0700359 if (FLAGS_gpu) {
jvanverth4736e142014-11-07 07:12:46 -0800360 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
361 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
362 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
363 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
364 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
365 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
366 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
367 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700368#ifdef SK_ANGLE
jvanverth4736e142014-11-07 07:12:46 -0800369 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700370#endif
mtklein40b32be2014-07-09 08:46:49 -0700371 }
mtkleinbb6a0282014-07-01 08:43:42 -0700372#endif
mtkleinf3723212014-06-25 14:08:00 -0700373}
374
bsalomonc2553372014-07-22 13:09:05 -0700375// If bench is enabled for config, returns a Target* for it, otherwise NULL.
376static Target* is_enabled(Benchmark* bench, const Config& config) {
377 if (!bench->isSuitableFor(config.backend)) {
378 return NULL;
379 }
380
reede5ea5002014-09-03 11:54:58 -0700381 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
382 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700383
384 Target* target = new Target(config);
385
386 if (Benchmark::kRaster_Backend == config.backend) {
387 target->surface.reset(SkSurface::NewRaster(info));
388 }
389#if SK_SUPPORT_GPU
390 else if (Benchmark::kGPU_Backend == config.backend) {
jvanverth4736e142014-11-07 07:12:46 -0800391 uint32_t flags = config.useDFText ? SkSurfaceProps::kUseDistanceFieldFonts_Flag : 0;
392 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
krajcevski69a55602014-08-13 10:46:31 -0700393 target->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(config.ctxType), info,
jvanverth4736e142014-11-07 07:12:46 -0800394 config.samples, &props));
krajcevski69a55602014-08-13 10:46:31 -0700395 target->gl = gGrFactory->getGLContext(config.ctxType);
bsalomonc2553372014-07-22 13:09:05 -0700396 }
397#endif
398
399 if (Benchmark::kNonRendering_Backend != config.backend && !target->surface.get()) {
400 delete target;
401 return NULL;
402 }
403 return target;
404}
405
406// Creates targets for a benchmark and a set of configs.
407static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
408 const SkTDArray<Config>& configs) {
409 for (int i = 0; i < configs.count(); ++i) {
410 if (Target* t = is_enabled(b, configs[i])) {
411 targets->push(t);
412 }
mtkleine714e752014-07-31 12:13:48 -0700413
bsalomonc2553372014-07-22 13:09:05 -0700414 }
415}
416
jcgregoriobf5e5232014-07-17 13:14:16 -0700417#if SK_SUPPORT_GPU
kkinnunen9e61bb72014-10-09 05:24:15 -0700418static void fill_gpu_options(ResultsWriter* log, SkGLContext* ctx) {
jcgregorio05c45602014-07-17 13:54:36 -0700419 const GrGLubyte* version;
jcgregoriobf5e5232014-07-17 13:14:16 -0700420 SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION));
421 log->configOption("GL_VERSION", (const char*)(version));
422
423 SK_GL_RET(*ctx, version, GetString(GR_GL_RENDERER));
424 log->configOption("GL_RENDERER", (const char*) version);
425
426 SK_GL_RET(*ctx, version, GetString(GR_GL_VENDOR));
427 log->configOption("GL_VENDOR", (const char*) version);
428
429 SK_GL_RET(*ctx, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
430 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
431}
432#endif
433
mtkleine714e752014-07-31 12:13:48 -0700434class BenchmarkStream {
435public:
mtklein92007582014-08-01 07:46:52 -0700436 BenchmarkStream() : fBenches(BenchRegistry::Head())
437 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700438 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700439 , fCurrentScale(0)
440 , fCurrentSKP(0) {
441 for (int i = 0; i < FLAGS_skps.count(); i++) {
442 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
443 fSKPs.push_back() = FLAGS_skps[i];
444 } else {
445 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
446 SkString path;
447 while (it.next(&path)) {
448 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
449 }
450 }
451 }
mtkleine714e752014-07-31 12:13:48 -0700452
mtklein92007582014-08-01 07:46:52 -0700453 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
454 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
455 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
456 exit(1);
457 }
458
459 for (int i = 0; i < FLAGS_scales.count(); i++) {
460 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
461 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
462 exit(1);
463 }
464 }
465 }
466
mtkleinfd731ce2014-09-10 12:19:30 -0700467 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
468 // Not strictly necessary, as it will be checked again later,
469 // but helps to avoid a lot of pointless work if we're going to skip it.
470 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
471 return false;
472 }
473
474 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
475 if (stream.get() == NULL) {
476 SkDebugf("Could not read %s.\n", path);
477 return false;
478 }
479
mtklein963504b2014-09-17 06:58:39 -0700480 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700481 if (pic->get() == NULL) {
482 SkDebugf("Could not read %s as an SkPicture.\n", path);
483 return false;
484 }
485 return true;
486 }
487
mtklein92007582014-08-01 07:46:52 -0700488 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700489 if (fBenches) {
490 Benchmark* bench = fBenches->factory()(NULL);
491 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700492 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700493 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700494 return bench;
495 }
mtklein92007582014-08-01 07:46:52 -0700496
mtkleine714e752014-07-31 12:13:48 -0700497 while (fGMs) {
498 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
499 fGMs = fGMs->next();
500 if (gm->getFlags() & skiagm::GM::kAsBench_Flag) {
mtklein92007582014-08-01 07:46:52 -0700501 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700502 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700503 return SkNEW_ARGS(GMBench, (gm.detach()));
504 }
505 }
mtklein92007582014-08-01 07:46:52 -0700506
mtkleinfd731ce2014-09-10 12:19:30 -0700507 // First add all .skps as RecordingBenches.
508 while (fCurrentRecording < fSKPs.count()) {
509 const SkString& path = fSKPs[fCurrentRecording++];
510 SkAutoTUnref<SkPicture> pic;
511 if (!ReadPicture(path.c_str(), &pic)) {
512 continue;
513 }
514 SkString name = SkOSPath::Basename(path.c_str());
515 fSourceType = "skp";
516 fBenchType = "recording";
517 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
518 }
519
520 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700521 while (fCurrentScale < fScales.count()) {
522 while (fCurrentSKP < fSKPs.count()) {
523 const SkString& path = fSKPs[fCurrentSKP++];
mtkleinfd731ce2014-09-10 12:19:30 -0700524 SkAutoTUnref<SkPicture> pic;
525 if (!ReadPicture(path.c_str(), &pic)) {
mtklein92007582014-08-01 07:46:52 -0700526 continue;
527 }
mtklein20840502014-08-21 15:51:22 -0700528 if (FLAGS_bbh) {
529 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
mtklein6838d852014-10-29 14:15:10 -0700530 SkRTreeFactory factory;
mtklein20840502014-08-21 15:51:22 -0700531 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700532 pic->playback(recorder.beginRecording(pic->cullRect().width(),
mtkleinea65bfa2014-09-09 07:59:46 -0700533 pic->cullRect().height(),
robertphillipsc5ba71d2014-09-04 08:42:50 -0700534 &factory));
mtklein20840502014-08-21 15:51:22 -0700535 pic.reset(recorder.endRecording());
536 }
mtkleinfd731ce2014-09-10 12:19:30 -0700537 SkString name = SkOSPath::Basename(path.c_str());
mtklein92007582014-08-01 07:46:52 -0700538 fSourceType = "skp";
mtkleinfd731ce2014-09-10 12:19:30 -0700539 fBenchType = "playback";
mtklein92007582014-08-01 07:46:52 -0700540 return SkNEW_ARGS(SKPBench,
541 (name.c_str(), pic.get(), fClip, fScales[fCurrentScale]));
542 }
543 fCurrentSKP = 0;
544 fCurrentScale++;
545 }
546
mtkleine714e752014-07-31 12:13:48 -0700547 return NULL;
548 }
mtklein92007582014-08-01 07:46:52 -0700549
550 void fillCurrentOptions(ResultsWriter* log) const {
551 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700552 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700553 if (0 == strcmp(fSourceType, "skp")) {
554 log->configOption("clip",
555 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
556 fClip.fRight, fClip.fBottom).c_str());
557 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
558 }
559 }
560
mtkleine714e752014-07-31 12:13:48 -0700561private:
562 const BenchRegistry* fBenches;
563 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700564 SkIRect fClip;
565 SkTArray<SkScalar> fScales;
566 SkTArray<SkString> fSKPs;
567
mtkleinfd731ce2014-09-10 12:19:30 -0700568 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
569 const char* fBenchType; // How we bench it: micro, recording, playback, ...
570 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700571 int fCurrentScale;
572 int fCurrentSKP;
mtkleine714e752014-07-31 12:13:48 -0700573};
574
jcgregorio3b27ade2014-11-13 08:06:40 -0800575int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700576int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800577 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700578 SkAutoGraphics ag;
mtkleinf3723212014-06-25 14:08:00 -0700579
krajcevski69a55602014-08-13 10:46:31 -0700580#if SK_SUPPORT_GPU
krajcevski12b35442014-08-13 12:06:26 -0700581 GrContext::Options grContextOpts;
582 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
583 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700584#endif
585
bsalomon06cddec2014-10-24 10:40:50 -0700586 if (FLAGS_veryVerbose) {
587 FLAGS_verbose = true;
588 }
589
bsalomon6eb03cc2014-08-07 14:28:50 -0700590 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700591 FLAGS_samples = 1;
592 FLAGS_gpuFrameLag = 0;
593 }
594
bsalomon6eb03cc2014-08-07 14:28:50 -0700595 if (!FLAGS_writePath.isEmpty()) {
596 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
597 if (!sk_mkdir(FLAGS_writePath[0])) {
598 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
599 FLAGS_writePath.set(0, NULL);
600 }
601 }
602
mtklein1915b622014-08-20 11:45:00 -0700603 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700604 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700605 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700606 }
mtklein1915b622014-08-20 11:45:00 -0700607
608 if (1 == FLAGS_properties.count() % 2) {
609 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
610 return 1;
611 }
612 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
613 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
614 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700615
616 if (1 == FLAGS_key.count() % 2) {
617 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
618 return 1;
619 }
620 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700621 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700622 }
mtklein60317d0f2014-07-14 11:30:37 -0700623
mtkleinf3723212014-06-25 14:08:00 -0700624 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700625 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400626
mtkleinbb6a0282014-07-01 08:43:42 -0700627 SkAutoTMalloc<double> samples(FLAGS_samples);
628
bsalomon6eb03cc2014-08-07 14:28:50 -0700629 if (kAutoTuneLoops != FLAGS_loops) {
630 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700631 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700632 // No header.
633 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700634 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700635 } else {
qiankun.miao8247ec32014-09-09 19:24:36 -0700636 SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
637 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700638 }
639
bsalomonc2553372014-07-22 13:09:05 -0700640 SkTDArray<Config> configs;
641 create_configs(&configs);
642
mtkleine070c2b2014-10-14 08:40:43 -0700643 int runs = 0;
mtklein92007582014-08-01 07:46:52 -0700644 BenchmarkStream benchStream;
645 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700646 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -0700647 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -0700648 continue;
649 }
650
mtkleinbb6a0282014-07-01 08:43:42 -0700651 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700652 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700653
jcgregoriobf5e5232014-07-17 13:14:16 -0700654 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -0700655 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700656 bench->preDraw();
657 }
mtkleinbb6a0282014-07-01 08:43:42 -0700658 for (int j = 0; j < targets.count(); j++) {
659 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
bsalomonc2553372014-07-22 13:09:05 -0700660 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700661
mtkleinbb6a0282014-07-01 08:43:42 -0700662 const int loops =
663#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700664 Benchmark::kGPU_Backend == targets[j]->config.backend
mtkleinbb6a0282014-07-01 08:43:42 -0700665 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
666 :
667#endif
668 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700669
bsalomon49f085d2014-09-05 13:34:00 -0700670 if (canvas && !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700671 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -0700672 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -0700673 pngFilename.append(".png");
674 write_canvas_png(canvas, pngFilename);
675 }
676
677 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700678 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400679 continue;
680 }
681
mtkleinf3723212014-06-25 14:08:00 -0700682 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700683 log->config(config);
mtklein96289052014-09-10 12:05:59 -0700684 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -0700685 benchStream.fillCurrentOptions(log.get());
jcgregoriobf5e5232014-07-17 13:14:16 -0700686#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700687 if (Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein1915b622014-08-20 11:45:00 -0700688 fill_gpu_options(log.get(), targets[j]->gl);
jcgregoriobf5e5232014-07-17 13:14:16 -0700689 }
690#endif
mtklein1915b622014-08-20 11:45:00 -0700691 log->timer("min_ms", stats.min);
692 log->timer("median_ms", stats.median);
693 log->timer("mean_ms", stats.mean);
694 log->timer("max_ms", stats.max);
695 log->timer("stddev_ms", sqrt(stats.var));
mtkleine070c2b2014-10-14 08:40:43 -0700696 if (runs++ % FLAGS_flushEvery == 0) {
697 log->flush();
698 }
mtklein60317d0f2014-07-14 11:30:37 -0700699
bsalomon6eb03cc2014-08-07 14:28:50 -0700700 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700701 if (targets.count() == 1) {
702 config = ""; // Only print the config if we run the same bench on more than one.
703 }
mtklein53d25622014-09-18 07:39:42 -0700704 SkDebugf("%4dM\t%s\t%s\n"
705 , sk_tools::getMaxResidentSetSizeMB()
706 , bench->getUniqueName()
707 , config);
mtkleina189ccd2014-07-14 12:28:47 -0700708 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700709 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700710 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700711 }
mtklein96289052014-09-10 12:05:59 -0700712 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -0700713 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700714 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700715 config = ""; // Only print the config if we run the same bench on more than one.
716 }
mtklein96289052014-09-10 12:05:59 -0700717 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700718 } else {
719 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleinafb43792014-08-19 15:55:55 -0700720 SkDebugf("%4dM\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
721 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700722 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700723 , HUMANIZE(stats.min)
724 , HUMANIZE(stats.median)
725 , HUMANIZE(stats.mean)
726 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700727 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700728 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700729 , config
mtklein96289052014-09-10 12:05:59 -0700730 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -0700731 );
732 }
bsalomon06cddec2014-10-24 10:40:50 -0700733#if SK_SUPPORT_GPU && GR_CACHE_STATS
734 if (FLAGS_veryVerbose &&
735 Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein6838d852014-10-29 14:15:10 -0700736 gGrFactory->get(targets[j]->config.ctxType)->printCacheStats();
bsalomon06cddec2014-10-24 10:40:50 -0700737 }
738#endif
mtkleinf3723212014-06-25 14:08:00 -0700739 }
mtkleinbb6a0282014-07-01 08:43:42 -0700740 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400741
bsalomon06cddec2014-10-24 10:40:50 -0700742#if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700743 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700744 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700745 }
746 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700747 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400748 }
bsalomon06cddec2014-10-24 10:40:50 -0700749#endif
mtkleinf3723212014-06-25 14:08:00 -0700750 }
751
752 return 0;
753}
754
jcgregorio3b27ade2014-11-13 08:06:40 -0800755#if !defined SK_BUILD_FOR_IOS
756int main(int argc, char** argv) {
757 SkCommandLineFlags::Parse(argc, argv);
758 return nanobench_main();
759}
760#endif