blob: ea62570ea0de14888bc3a3cc2e4d5b7b4b6c9665 [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"
mtklein60317d02014-07-14 11:30:37 -070014#include "ResultsWriter.h"
mtklein92007582014-08-01 07:46:52 -070015#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070016#include "Stats.h"
17#include "Timer.h"
18
mtklein20840502014-08-21 15:51:22 -070019#include "SkBBHFactory.h"
mtkleinf3723212014-06-25 14:08:00 -070020#include "SkCanvas.h"
caryclark17f0b6d2014-07-22 10:15:34 -070021#include "SkCommonFlags.h"
mtkleinf3723212014-06-25 14:08:00 -070022#include "SkForceLinking.h"
23#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070024#include "SkOSFile.h"
25#include "SkPictureRecorder.h"
mtkleinf3723212014-06-25 14:08:00 -070026#include "SkString.h"
27#include "SkSurface.h"
28
mtkleinbb6a0282014-07-01 08:43:42 -070029#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070030 #include "gl/GrGLDefines.h"
mtkleinbb6a0282014-07-01 08:43:42 -070031 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070032 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070033#endif
34
mtkleinf3723212014-06-25 14:08:00 -070035__SK_FORCE_IMAGE_DECODER_LINKING;
36
bsalomon6eb03cc2014-08-07 14:28:50 -070037static const int kAutoTuneLoops = -1;
38
mtkleinb5110422014-08-07 15:20:02 -070039static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070040#ifdef SK_DEBUG
41 1;
mtkleina189ccd2014-07-14 12:28:47 -070042#else
bsalomon6eb03cc2014-08-07 14:28:50 -070043 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070044#endif
45
bsalomon6eb03cc2014-08-07 14:28:50 -070046static SkString loops_help_txt() {
47 SkString help;
48 help.printf("Number of times to run each bench. Set this to %d to auto-"
49 "tune for each bench. Timings are only reported when auto-tuning.",
50 kAutoTuneLoops);
51 return help;
52}
53
54DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
55
mtkleinf3723212014-06-25 14:08:00 -070056DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
57DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
58DEFINE_double(overheadGoal, 0.0001,
59 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070060DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
61DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070062DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
63 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070064
mtklein60317d02014-07-14 11:30:37 -070065DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070066DEFINE_int32(maxCalibrationAttempts, 3,
67 "Try up to this many times to guess loops for a bench, or skip the bench.");
68DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein1915b622014-08-20 11:45:00 -070069DEFINE_string(properties, "",
70 "Space-separated key/value pairs to add to JSON identifying this nanobench run.");
mtklein94e51562014-08-19 12:41:53 -070071DEFINE_string(key, "",
72 "Space-separated key/value pairs to add to JSON identifying this bench config.");
mtklein60317d02014-07-14 11:30:37 -070073
mtklein92007582014-08-01 07:46:52 -070074DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
75DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
mtklein20840502014-08-21 15:51:22 -070076DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein92007582014-08-01 07:46:52 -070077
mtkleinf3723212014-06-25 14:08:00 -070078static SkString humanize(double ms) {
79 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3);
80 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6);
mtklein62386882014-07-15 10:30:31 -070081#ifdef SK_BUILD_FOR_WIN
82 if (ms < 1) return SkStringPrintf("%.3gus", ms*1e3);
83#else
mtkleinf3723212014-06-25 14:08:00 -070084 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3);
mtklein62386882014-07-15 10:30:31 -070085#endif
mtkleinf3723212014-06-25 14:08:00 -070086 return SkStringPrintf("%.3gms", ms);
87}
mtklein55b0ffc2014-07-17 08:38:23 -070088#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -070089
mtkleinbb6a0282014-07-01 08:43:42 -070090static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHelper* gl) {
bsalomon6eb03cc2014-08-07 14:28:50 -070091 if (canvas) {
92 canvas->clear(SK_ColorWHITE);
93 }
mtkleinbb6a0282014-07-01 08:43:42 -070094 WallTimer timer;
95 timer.start();
96 if (bench) {
97 bench->draw(loops, canvas);
98 }
99 if (canvas) {
100 canvas->flush();
101 }
102#if SK_SUPPORT_GPU
103 if (gl) {
104 SK_GL(*gl, Flush());
105 gl->swapBuffers();
106 }
107#endif
108 timer.end();
109 return timer.fWall;
110}
111
mtkleinf3723212014-06-25 14:08:00 -0700112static double estimate_timer_overhead() {
113 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700114 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -0700115 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -0700116 }
117 return overhead / FLAGS_overheadLoops;
118}
119
mtklein55b0ffc2014-07-17 08:38:23 -0700120static int clamp_loops(int loops) {
121 if (loops < 1) {
122 SkDebugf("ERROR: clamping loops from %d to 1.\n", loops);
123 return 1;
124 }
125 if (loops > FLAGS_maxLoops) {
126 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
127 return FLAGS_maxLoops;
128 }
129 return loops;
130}
131
bsalomon6eb03cc2014-08-07 14:28:50 -0700132static bool write_canvas_png(SkCanvas* canvas, const SkString& filename) {
133 if (filename.isEmpty()) {
134 return false;
135 }
reede5ea5002014-09-03 11:54:58 -0700136 if (kUnknown_SkColorType == canvas->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700137 return false;
138 }
139 SkBitmap bmp;
140 bmp.setInfo(canvas->imageInfo());
141 if (!canvas->readPixels(&bmp, 0, 0)) {
142 SkDebugf("Can't read canvas pixels.\n");
143 return false;
144 }
145 SkString dir = SkOSPath::Dirname(filename.c_str());
146 if (!sk_mkdir(dir.c_str())) {
147 SkDebugf("Can't make dir %s.\n", dir.c_str());
148 return false;
149 }
150 SkFILEWStream stream(filename.c_str());
151 if (!stream.isValid()) {
152 SkDebugf("Can't write %s.\n", filename.c_str());
153 return false;
154 }
155 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
156 SkDebugf("Can't encode a PNG.\n");
157 return false;
158 }
159 return true;
160}
161
162static int kFailedLoops = -2;
mtkleinbb6a0282014-07-01 08:43:42 -0700163static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
164 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700165 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700166 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700167 if (kAutoTuneLoops == FLAGS_loops) {
168 while (bench_plus_overhead < overhead) {
169 if (round++ == FLAGS_maxCalibrationAttempts) {
170 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
171 bench->getName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
172 return kFailedLoops;
173 }
174 bench_plus_overhead = time(1, bench, canvas, NULL);
mtklein55b0ffc2014-07-17 08:38:23 -0700175 }
mtklein2069e222014-08-04 13:57:39 -0700176 }
mtkleinf3723212014-06-25 14:08:00 -0700177
mtkleinbb6a0282014-07-01 08:43:42 -0700178 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700179 // We'll pick N to make timer overhead negligible:
180 //
mtkleinbb6a0282014-07-01 08:43:42 -0700181 // overhead
182 // ------------------------- < FLAGS_overheadGoal
183 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700184 //
mtkleinbb6a0282014-07-01 08:43:42 -0700185 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700186 //
187 // Doing some math, we get:
188 //
mtkleinbb6a0282014-07-01 08:43:42 -0700189 // (overhead / FLAGS_overheadGoal) - overhead
190 // ------------------------------------------ < N
191 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700192 //
193 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700194 int loops = FLAGS_loops;
195 if (kAutoTuneLoops == loops) {
196 const double numer = overhead / FLAGS_overheadGoal - overhead;
197 const double denom = bench_plus_overhead - overhead;
198 loops = (int)ceil(numer / denom);
199 }
200 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700201
202 for (int i = 0; i < FLAGS_samples; i++) {
203 samples[i] = time(loops, bench, canvas, NULL) / loops;
204 }
205 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700206}
207
mtkleinbb6a0282014-07-01 08:43:42 -0700208#if SK_SUPPORT_GPU
209static int gpu_bench(SkGLContextHelper* gl,
210 Benchmark* bench,
211 SkCanvas* canvas,
212 double* samples) {
bsalomonc2553372014-07-22 13:09:05 -0700213 gl->makeCurrent();
mtkleinbb6a0282014-07-01 08:43:42 -0700214 // Make sure we're done with whatever came before.
mtklein9bc86ed2014-07-01 10:02:42 -0700215 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700216
217 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700218 int loops = FLAGS_loops;
219 if (kAutoTuneLoops == loops) {
220 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700221 double elapsed = 0;
222 do {
223 loops *= 2;
224 // If the GPU lets frames lag at all, we need to make sure we're timing
225 // _this_ round, not still timing last round. We force this by looping
226 // more times than any reasonable GPU will allow frames to lag.
227 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
228 elapsed = time(loops, bench, canvas, gl);
229 }
230 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700231
mtkleina189ccd2014-07-14 12:28:47 -0700232 // We've overshot at least a little. Scale back linearly.
233 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
mtkleinbb6a0282014-07-01 08:43:42 -0700234
mtkleina189ccd2014-07-14 12:28:47 -0700235 // Might as well make sure we're not still timing our calibration.
236 SK_GL(*gl, Finish());
237 }
mtklein55b0ffc2014-07-17 08:38:23 -0700238 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700239
240 // Pretty much the same deal as the calibration: do some warmup to make
241 // sure we're timing steady-state pipelined frames.
242 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
243 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700244 }
mtkleinbb6a0282014-07-01 08:43:42 -0700245
246 // Now, actually do the timing!
247 for (int i = 0; i < FLAGS_samples; i++) {
248 samples[i] = time(loops, bench, canvas, gl) / loops;
249 }
250 return loops;
251}
252#endif
253
254static SkString to_lower(const char* str) {
255 SkString lower(str);
256 for (size_t i = 0; i < lower.size(); i++) {
257 lower[i] = tolower(lower[i]);
258 }
259 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700260}
261
bsalomonc2553372014-07-22 13:09:05 -0700262struct Config {
263 const char* name;
mtkleinbb6a0282014-07-01 08:43:42 -0700264 Benchmark::Backend backend;
bsalomonc2553372014-07-22 13:09:05 -0700265 SkColorType color;
266 SkAlphaType alpha;
267 int samples;
268#if SK_SUPPORT_GPU
269 GrContextFactory::GLContextType ctxType;
270#else
271 int bogusInt;
272#endif
273};
274
275struct Target {
276 explicit Target(const Config& c) : config(c) {}
277 const Config config;
mtkleinbb6a0282014-07-01 08:43:42 -0700278 SkAutoTDelete<SkSurface> surface;
279#if SK_SUPPORT_GPU
280 SkGLContextHelper* gl;
281#endif
282};
mtkleinf3723212014-06-25 14:08:00 -0700283
bsalomonc2553372014-07-22 13:09:05 -0700284static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700285 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700286 if (to_lower(FLAGS_config[i]).equals(name)) {
287 return true;
mtkleinf3723212014-06-25 14:08:00 -0700288 }
289 }
bsalomonc2553372014-07-22 13:09:05 -0700290 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700291}
292
bsalomonc2553372014-07-22 13:09:05 -0700293#if SK_SUPPORT_GPU
294static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
295 int sampleCnt) {
296 if (!is_cpu_config_allowed(name)) {
297 return false;
298 }
krajcevski69a55602014-08-13 10:46:31 -0700299 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700300 return sampleCnt <= ctx->getMaxSampleCount();
301 }
302 return false;
303}
304#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700305
bsalomonc2553372014-07-22 13:09:05 -0700306#if SK_SUPPORT_GPU
307#define kBogusGLContextType GrContextFactory::kNative_GLContextType
308#else
309#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700310#endif
bsalomonc2553372014-07-22 13:09:05 -0700311
312// Append all configs that are enabled and supported.
313static void create_configs(SkTDArray<Config>* configs) {
314 #define CPU_CONFIG(name, backend, color, alpha) \
315 if (is_cpu_config_allowed(#name)) { \
316 Config config = { #name, Benchmark::backend, color, alpha, 0, kBogusGLContextType }; \
317 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700318 }
mtkleine714e752014-07-31 12:13:48 -0700319
mtklein40b32be2014-07-09 08:46:49 -0700320 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700321 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
322 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
323 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700324 }
mtkleinbb6a0282014-07-01 08:43:42 -0700325
326#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700327 #define GPU_CONFIG(name, ctxType, samples) \
328 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
329 Config config = { \
330 #name, \
331 Benchmark::kGPU_Backend, \
332 kN32_SkColorType, \
333 kPremul_SkAlphaType, \
334 samples, \
335 GrContextFactory::ctxType }; \
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_gpu) {
bsalomonc2553372014-07-22 13:09:05 -0700340 GPU_CONFIG(gpu, kNative_GLContextType, 0)
341 GPU_CONFIG(msaa4, kNative_GLContextType, 4)
342 GPU_CONFIG(msaa16, kNative_GLContextType, 16)
343 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4)
344 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16)
345 GPU_CONFIG(debug, kDebug_GLContextType, 0)
346 GPU_CONFIG(nullgpu, kNull_GLContextType, 0)
bsalomon3b4d0772014-08-06 10:52:33 -0700347#ifdef SK_ANGLE
348 GPU_CONFIG(angle, kANGLE_GLContextType, 0)
349#endif
mtklein40b32be2014-07-09 08:46:49 -0700350 }
mtkleinbb6a0282014-07-01 08:43:42 -0700351#endif
mtkleinf3723212014-06-25 14:08:00 -0700352}
353
bsalomonc2553372014-07-22 13:09:05 -0700354// If bench is enabled for config, returns a Target* for it, otherwise NULL.
355static Target* is_enabled(Benchmark* bench, const Config& config) {
356 if (!bench->isSuitableFor(config.backend)) {
357 return NULL;
358 }
359
reede5ea5002014-09-03 11:54:58 -0700360 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
361 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700362
363 Target* target = new Target(config);
364
365 if (Benchmark::kRaster_Backend == config.backend) {
366 target->surface.reset(SkSurface::NewRaster(info));
367 }
368#if SK_SUPPORT_GPU
369 else if (Benchmark::kGPU_Backend == config.backend) {
krajcevski69a55602014-08-13 10:46:31 -0700370 target->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(config.ctxType), info,
bsalomonc2553372014-07-22 13:09:05 -0700371 config.samples));
krajcevski69a55602014-08-13 10:46:31 -0700372 target->gl = gGrFactory->getGLContext(config.ctxType);
bsalomonc2553372014-07-22 13:09:05 -0700373 }
374#endif
375
376 if (Benchmark::kNonRendering_Backend != config.backend && !target->surface.get()) {
377 delete target;
378 return NULL;
379 }
380 return target;
381}
382
383// Creates targets for a benchmark and a set of configs.
384static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
385 const SkTDArray<Config>& configs) {
386 for (int i = 0; i < configs.count(); ++i) {
387 if (Target* t = is_enabled(b, configs[i])) {
388 targets->push(t);
389 }
mtkleine714e752014-07-31 12:13:48 -0700390
bsalomonc2553372014-07-22 13:09:05 -0700391 }
392}
393
jcgregoriobf5e5232014-07-17 13:14:16 -0700394#if SK_SUPPORT_GPU
395static void fill_gpu_options(ResultsWriter* log, SkGLContextHelper* ctx) {
jcgregorio05c45602014-07-17 13:54:36 -0700396 const GrGLubyte* version;
jcgregoriobf5e5232014-07-17 13:14:16 -0700397 SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION));
398 log->configOption("GL_VERSION", (const char*)(version));
399
400 SK_GL_RET(*ctx, version, GetString(GR_GL_RENDERER));
401 log->configOption("GL_RENDERER", (const char*) version);
402
403 SK_GL_RET(*ctx, version, GetString(GR_GL_VENDOR));
404 log->configOption("GL_VENDOR", (const char*) version);
405
406 SK_GL_RET(*ctx, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
407 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
408}
409#endif
410
mtkleine714e752014-07-31 12:13:48 -0700411class BenchmarkStream {
412public:
mtklein92007582014-08-01 07:46:52 -0700413 BenchmarkStream() : fBenches(BenchRegistry::Head())
414 , fGMs(skiagm::GMRegistry::Head())
415 , fCurrentScale(0)
416 , fCurrentSKP(0) {
417 for (int i = 0; i < FLAGS_skps.count(); i++) {
418 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
419 fSKPs.push_back() = FLAGS_skps[i];
420 } else {
421 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
422 SkString path;
423 while (it.next(&path)) {
424 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
425 }
426 }
427 }
mtkleine714e752014-07-31 12:13:48 -0700428
mtklein92007582014-08-01 07:46:52 -0700429 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
430 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
431 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
432 exit(1);
433 }
434
435 for (int i = 0; i < FLAGS_scales.count(); i++) {
436 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
437 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
438 exit(1);
439 }
440 }
441 }
442
443 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700444 if (fBenches) {
445 Benchmark* bench = fBenches->factory()(NULL);
446 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700447 fSourceType = "bench";
mtkleine714e752014-07-31 12:13:48 -0700448 return bench;
449 }
mtklein92007582014-08-01 07:46:52 -0700450
mtkleine714e752014-07-31 12:13:48 -0700451 while (fGMs) {
452 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
453 fGMs = fGMs->next();
454 if (gm->getFlags() & skiagm::GM::kAsBench_Flag) {
mtklein92007582014-08-01 07:46:52 -0700455 fSourceType = "gm";
mtkleine714e752014-07-31 12:13:48 -0700456 return SkNEW_ARGS(GMBench, (gm.detach()));
457 }
458 }
mtklein92007582014-08-01 07:46:52 -0700459
460 while (fCurrentScale < fScales.count()) {
461 while (fCurrentSKP < fSKPs.count()) {
462 const SkString& path = fSKPs[fCurrentSKP++];
463
464 // Not strictly necessary, as it will be checked again later,
465 // but helps to avoid a lot of pointless work if we're going to skip it.
466 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
467 continue;
468 }
469
470 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
471 if (stream.get() == NULL) {
472 SkDebugf("Could not read %s.\n", path.c_str());
473 exit(1);
474 }
475
476 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream.get()));
477 if (pic.get() == NULL) {
478 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
479 exit(1);
480 }
481
482 SkString name = SkOSPath::Basename(path.c_str());
483
mtklein20840502014-08-21 15:51:22 -0700484 if (FLAGS_bbh) {
485 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
486 // Here we use an SkTileGrid with parameters optimized for FLAGS_clip.
487 const SkTileGridFactory::TileGridInfo info = {
488 SkISize::Make(fClip.width(), fClip.height()), // tile interval
489 SkISize::Make(0,0), // margin
490 SkIPoint::Make(fClip.left(), fClip.top()), // offset
491 };
492 SkTileGridFactory factory(info);
493 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700494 pic->playback(recorder.beginRecording(pic->cullRect().width(),
495 pic->cullRect().height(),
496 &factory));
mtklein20840502014-08-21 15:51:22 -0700497 pic.reset(recorder.endRecording());
498 }
499
mtklein92007582014-08-01 07:46:52 -0700500 fSourceType = "skp";
501 return SkNEW_ARGS(SKPBench,
502 (name.c_str(), pic.get(), fClip, fScales[fCurrentScale]));
503 }
504 fCurrentSKP = 0;
505 fCurrentScale++;
506 }
507
mtkleine714e752014-07-31 12:13:48 -0700508 return NULL;
509 }
mtklein92007582014-08-01 07:46:52 -0700510
511 void fillCurrentOptions(ResultsWriter* log) const {
512 log->configOption("source_type", fSourceType);
513 if (0 == strcmp(fSourceType, "skp")) {
514 log->configOption("clip",
515 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
516 fClip.fRight, fClip.fBottom).c_str());
517 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
518 }
519 }
520
mtkleine714e752014-07-31 12:13:48 -0700521private:
522 const BenchRegistry* fBenches;
523 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700524 SkIRect fClip;
525 SkTArray<SkScalar> fScales;
526 SkTArray<SkString> fSKPs;
527
528 const char* fSourceType;
529 int fCurrentScale;
530 int fCurrentSKP;
mtkleine714e752014-07-31 12:13:48 -0700531};
532
caryclark17f0b6d2014-07-22 10:15:34 -0700533int nanobench_main();
534int nanobench_main() {
mtkleinf3723212014-06-25 14:08:00 -0700535 SetupCrashHandler();
536 SkAutoGraphics ag;
mtkleinf3723212014-06-25 14:08:00 -0700537
krajcevski69a55602014-08-13 10:46:31 -0700538#if SK_SUPPORT_GPU
krajcevski12b35442014-08-13 12:06:26 -0700539 GrContext::Options grContextOpts;
540 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
541 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700542#endif
543
bsalomon6eb03cc2014-08-07 14:28:50 -0700544 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700545 FLAGS_samples = 1;
546 FLAGS_gpuFrameLag = 0;
547 }
548
bsalomon6eb03cc2014-08-07 14:28:50 -0700549 if (!FLAGS_writePath.isEmpty()) {
550 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
551 if (!sk_mkdir(FLAGS_writePath[0])) {
552 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
553 FLAGS_writePath.set(0, NULL);
554 }
555 }
556
mtklein1915b622014-08-20 11:45:00 -0700557 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d02014-07-14 11:30:37 -0700558 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700559 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d02014-07-14 11:30:37 -0700560 }
mtklein1915b622014-08-20 11:45:00 -0700561
562 if (1 == FLAGS_properties.count() % 2) {
563 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
564 return 1;
565 }
566 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
567 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
568 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700569
570 if (1 == FLAGS_key.count() % 2) {
571 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
572 return 1;
573 }
574 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700575 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700576 }
mtklein60317d02014-07-14 11:30:37 -0700577
mtkleinf3723212014-06-25 14:08:00 -0700578 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700579 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400580
mtkleinbb6a0282014-07-01 08:43:42 -0700581 SkAutoTMalloc<double> samples(FLAGS_samples);
582
bsalomon6eb03cc2014-08-07 14:28:50 -0700583 if (kAutoTuneLoops != FLAGS_loops) {
584 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700585 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700586 // No header.
587 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700588 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700589 } else {
mtkleinafb43792014-08-19 15:55:55 -0700590 SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -0700591 }
592
bsalomonc2553372014-07-22 13:09:05 -0700593 SkTDArray<Config> configs;
594 create_configs(&configs);
595
mtklein92007582014-08-01 07:46:52 -0700596 BenchmarkStream benchStream;
597 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700598 SkAutoTDelete<Benchmark> bench(b);
mtkleinf3723212014-06-25 14:08:00 -0700599 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
600 continue;
601 }
602
mtkleinbb6a0282014-07-01 08:43:42 -0700603 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700604 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700605
jcgregoriobf5e5232014-07-17 13:14:16 -0700606 if (!targets.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700607 log->bench(bench->getName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700608 bench->preDraw();
609 }
mtkleinbb6a0282014-07-01 08:43:42 -0700610 for (int j = 0; j < targets.count(); j++) {
611 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
bsalomonc2553372014-07-22 13:09:05 -0700612 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700613
mtkleinbb6a0282014-07-01 08:43:42 -0700614 const int loops =
615#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700616 Benchmark::kGPU_Backend == targets[j]->config.backend
mtkleinbb6a0282014-07-01 08:43:42 -0700617 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
618 :
619#endif
620 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700621
bsalomon6eb03cc2014-08-07 14:28:50 -0700622 if (canvas && !FLAGS_writePath.isEmpty() && NULL != FLAGS_writePath[0]) {
623 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
624 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getName());
625 pngFilename.append(".png");
626 write_canvas_png(canvas, pngFilename);
627 }
628
629 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700630 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400631 continue;
632 }
633
mtkleinf3723212014-06-25 14:08:00 -0700634 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700635 log->config(config);
636 benchStream.fillCurrentOptions(log.get());
jcgregoriobf5e5232014-07-17 13:14:16 -0700637#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700638 if (Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein1915b622014-08-20 11:45:00 -0700639 fill_gpu_options(log.get(), targets[j]->gl);
jcgregoriobf5e5232014-07-17 13:14:16 -0700640 }
641#endif
mtklein1915b622014-08-20 11:45:00 -0700642 log->timer("min_ms", stats.min);
643 log->timer("median_ms", stats.median);
644 log->timer("mean_ms", stats.mean);
645 log->timer("max_ms", stats.max);
646 log->timer("stddev_ms", sqrt(stats.var));
mtklein60317d02014-07-14 11:30:37 -0700647
bsalomon6eb03cc2014-08-07 14:28:50 -0700648 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700649 if (targets.count() == 1) {
650 config = ""; // Only print the config if we run the same bench on more than one.
651 }
652 SkDebugf("%s\t%s\n", bench->getName(), config);
653 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700654 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700655 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700656 }
657 SkDebugf("%s\n", bench->getName());
658 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700659 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700660 config = ""; // Only print the config if we run the same bench on more than one.
661 }
mtklein55b0ffc2014-07-17 08:38:23 -0700662 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700663 } else {
664 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleinafb43792014-08-19 15:55:55 -0700665 SkDebugf("%4dM\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
666 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700667 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700668 , HUMANIZE(stats.min)
669 , HUMANIZE(stats.median)
670 , HUMANIZE(stats.mean)
671 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700672 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700673 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700674 , config
mtkleinbb6a0282014-07-01 08:43:42 -0700675 , bench->getName()
mtkleinf3723212014-06-25 14:08:00 -0700676 );
677 }
678 }
mtkleinbb6a0282014-07-01 08:43:42 -0700679 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400680
681 #if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700682 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700683 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700684 }
685 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700686 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400687 }
688 #endif
mtkleinf3723212014-06-25 14:08:00 -0700689 }
690
691 return 0;
692}
693
694#if !defined SK_BUILD_FOR_IOS
caryclark17f0b6d2014-07-22 10:15:34 -0700695int main(int argc, char** argv) {
696 SkCommandLineFlags::Parse(argc, argv);
697 return nanobench_main();
mtkleinf3723212014-06-25 14:08:00 -0700698}
699#endif