blob: 245aa0ed1dda90d9bb845fca28aee230e0b24532 [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"
mtklein60317d0f2014-07-14 11:30:37 -070013#include "ResultsWriter.h"
mtklein92007582014-08-01 07:46:52 -070014#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070015#include "Stats.h"
16#include "Timer.h"
17
mtklein92007582014-08-01 07:46:52 -070018#include "SkOSFile.h"
mtkleinf3723212014-06-25 14:08:00 -070019#include "SkCanvas.h"
caryclark17f0b6d2014-07-22 10:15:34 -070020#include "SkCommonFlags.h"
mtkleinf3723212014-06-25 14:08:00 -070021#include "SkForceLinking.h"
22#include "SkGraphics.h"
23#include "SkString.h"
24#include "SkSurface.h"
25
mtkleinbb6a0282014-07-01 08:43:42 -070026#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070027 #include "gl/GrGLDefines.h"
mtkleinbb6a0282014-07-01 08:43:42 -070028 #include "GrContextFactory.h"
29 GrContextFactory gGrFactory;
30#endif
31
mtkleinf3723212014-06-25 14:08:00 -070032__SK_FORCE_IMAGE_DECODER_LINKING;
33
bsalomon6eb03cc2014-08-07 14:28:50 -070034static const int kAutoTuneLoops = -1;
35
36static const int kDefaultLoops =
37#ifdef SK_DEBUG
38 1;
mtkleina189ccd2014-07-14 12:28:47 -070039#else
bsalomon6eb03cc2014-08-07 14:28:50 -070040 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070041#endif
42
bsalomon6eb03cc2014-08-07 14:28:50 -070043static SkString loops_help_txt() {
44 SkString help;
45 help.printf("Number of times to run each bench. Set this to %d to auto-"
46 "tune for each bench. Timings are only reported when auto-tuning.",
47 kAutoTuneLoops);
48 return help;
49}
50
51DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
52
53DEFINE_string2(writePath, w, "", "If set, write benches here as .pngs.");
54
mtkleinf3723212014-06-25 14:08:00 -070055DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
56DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
57DEFINE_double(overheadGoal, 0.0001,
58 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070059DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
60DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -070061
mtklein60317d0f2014-07-14 11:30:37 -070062DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070063DEFINE_int32(maxCalibrationAttempts, 3,
64 "Try up to this many times to guess loops for a bench, or skip the bench.");
65DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
jcgregoriobf5e5232014-07-17 13:14:16 -070066DEFINE_string(key, "", "Space-separated key/value pairs to add to JSON.");
67DEFINE_string(gitHash, "", "Git hash to add to JSON.");
mtklein60317d0f2014-07-14 11:30:37 -070068
mtklein92007582014-08-01 07:46:52 -070069DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
70DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
71
mtkleinf3723212014-06-25 14:08:00 -070072static SkString humanize(double ms) {
73 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3);
74 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6);
mtklein62386882014-07-15 10:30:31 -070075#ifdef SK_BUILD_FOR_WIN
76 if (ms < 1) return SkStringPrintf("%.3gus", ms*1e3);
77#else
mtkleinf3723212014-06-25 14:08:00 -070078 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3);
mtklein62386882014-07-15 10:30:31 -070079#endif
mtkleinf3723212014-06-25 14:08:00 -070080 return SkStringPrintf("%.3gms", ms);
81}
mtklein55b0ffc2014-07-17 08:38:23 -070082#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -070083
mtkleinbb6a0282014-07-01 08:43:42 -070084static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHelper* gl) {
bsalomon6eb03cc2014-08-07 14:28:50 -070085 if (canvas) {
86 canvas->clear(SK_ColorWHITE);
87 }
mtkleinbb6a0282014-07-01 08:43:42 -070088 WallTimer timer;
89 timer.start();
90 if (bench) {
91 bench->draw(loops, canvas);
92 }
93 if (canvas) {
94 canvas->flush();
95 }
96#if SK_SUPPORT_GPU
97 if (gl) {
98 SK_GL(*gl, Flush());
99 gl->swapBuffers();
100 }
101#endif
102 timer.end();
103 return timer.fWall;
104}
105
mtkleinf3723212014-06-25 14:08:00 -0700106static double estimate_timer_overhead() {
107 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700108 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -0700109 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -0700110 }
111 return overhead / FLAGS_overheadLoops;
112}
113
mtklein55b0ffc2014-07-17 08:38:23 -0700114static int clamp_loops(int loops) {
115 if (loops < 1) {
116 SkDebugf("ERROR: clamping loops from %d to 1.\n", loops);
117 return 1;
118 }
119 if (loops > FLAGS_maxLoops) {
120 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
121 return FLAGS_maxLoops;
122 }
123 return loops;
124}
125
bsalomon6eb03cc2014-08-07 14:28:50 -0700126static bool write_canvas_png(SkCanvas* canvas, const SkString& filename) {
127 if (filename.isEmpty()) {
128 return false;
129 }
130 if (kUnknown_SkColorType == canvas->imageInfo().fColorType) {
131 return false;
132 }
133 SkBitmap bmp;
134 bmp.setInfo(canvas->imageInfo());
135 if (!canvas->readPixels(&bmp, 0, 0)) {
136 SkDebugf("Can't read canvas pixels.\n");
137 return false;
138 }
139 SkString dir = SkOSPath::Dirname(filename.c_str());
140 if (!sk_mkdir(dir.c_str())) {
141 SkDebugf("Can't make dir %s.\n", dir.c_str());
142 return false;
143 }
144 SkFILEWStream stream(filename.c_str());
145 if (!stream.isValid()) {
146 SkDebugf("Can't write %s.\n", filename.c_str());
147 return false;
148 }
149 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
150 SkDebugf("Can't encode a PNG.\n");
151 return false;
152 }
153 return true;
154}
155
156static int kFailedLoops = -2;
mtkleinbb6a0282014-07-01 08:43:42 -0700157static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
158 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700159 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700160 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700161 if (kAutoTuneLoops == FLAGS_loops) {
162 while (bench_plus_overhead < overhead) {
163 if (round++ == FLAGS_maxCalibrationAttempts) {
164 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
165 bench->getName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
166 return kFailedLoops;
167 }
168 bench_plus_overhead = time(1, bench, canvas, NULL);
mtklein55b0ffc2014-07-17 08:38:23 -0700169 }
mtklein2069e222014-08-04 13:57:39 -0700170 }
mtkleinf3723212014-06-25 14:08:00 -0700171
mtkleinbb6a0282014-07-01 08:43:42 -0700172 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700173 // We'll pick N to make timer overhead negligible:
174 //
mtkleinbb6a0282014-07-01 08:43:42 -0700175 // overhead
176 // ------------------------- < FLAGS_overheadGoal
177 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700178 //
mtkleinbb6a0282014-07-01 08:43:42 -0700179 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700180 //
181 // Doing some math, we get:
182 //
mtkleinbb6a0282014-07-01 08:43:42 -0700183 // (overhead / FLAGS_overheadGoal) - overhead
184 // ------------------------------------------ < N
185 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700186 //
187 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700188 int loops = FLAGS_loops;
189 if (kAutoTuneLoops == loops) {
190 const double numer = overhead / FLAGS_overheadGoal - overhead;
191 const double denom = bench_plus_overhead - overhead;
192 loops = (int)ceil(numer / denom);
193 }
194 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700195
196 for (int i = 0; i < FLAGS_samples; i++) {
197 samples[i] = time(loops, bench, canvas, NULL) / loops;
198 }
199 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700200}
201
mtkleinbb6a0282014-07-01 08:43:42 -0700202#if SK_SUPPORT_GPU
203static int gpu_bench(SkGLContextHelper* gl,
204 Benchmark* bench,
205 SkCanvas* canvas,
206 double* samples) {
bsalomonc2553372014-07-22 13:09:05 -0700207 gl->makeCurrent();
mtkleinbb6a0282014-07-01 08:43:42 -0700208 // Make sure we're done with whatever came before.
mtklein9bc86ed2014-07-01 10:02:42 -0700209 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700210
211 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700212 int loops = FLAGS_loops;
213 if (kAutoTuneLoops == loops) {
214 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700215 double elapsed = 0;
216 do {
217 loops *= 2;
218 // If the GPU lets frames lag at all, we need to make sure we're timing
219 // _this_ round, not still timing last round. We force this by looping
220 // more times than any reasonable GPU will allow frames to lag.
221 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
222 elapsed = time(loops, bench, canvas, gl);
223 }
224 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700225
mtkleina189ccd2014-07-14 12:28:47 -0700226 // We've overshot at least a little. Scale back linearly.
227 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
mtkleinbb6a0282014-07-01 08:43:42 -0700228
mtkleina189ccd2014-07-14 12:28:47 -0700229 // Might as well make sure we're not still timing our calibration.
230 SK_GL(*gl, Finish());
231 }
mtklein55b0ffc2014-07-17 08:38:23 -0700232 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700233
234 // Pretty much the same deal as the calibration: do some warmup to make
235 // sure we're timing steady-state pipelined frames.
236 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
237 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700238 }
mtkleinbb6a0282014-07-01 08:43:42 -0700239
240 // Now, actually do the timing!
241 for (int i = 0; i < FLAGS_samples; i++) {
242 samples[i] = time(loops, bench, canvas, gl) / loops;
243 }
244 return loops;
245}
246#endif
247
248static SkString to_lower(const char* str) {
249 SkString lower(str);
250 for (size_t i = 0; i < lower.size(); i++) {
251 lower[i] = tolower(lower[i]);
252 }
253 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700254}
255
bsalomonc2553372014-07-22 13:09:05 -0700256struct Config {
257 const char* name;
mtkleinbb6a0282014-07-01 08:43:42 -0700258 Benchmark::Backend backend;
bsalomonc2553372014-07-22 13:09:05 -0700259 SkColorType color;
260 SkAlphaType alpha;
261 int samples;
262#if SK_SUPPORT_GPU
263 GrContextFactory::GLContextType ctxType;
264#else
265 int bogusInt;
266#endif
267};
268
269struct Target {
270 explicit Target(const Config& c) : config(c) {}
271 const Config config;
mtkleinbb6a0282014-07-01 08:43:42 -0700272 SkAutoTDelete<SkSurface> surface;
273#if SK_SUPPORT_GPU
274 SkGLContextHelper* gl;
275#endif
276};
mtkleinf3723212014-06-25 14:08:00 -0700277
bsalomonc2553372014-07-22 13:09:05 -0700278static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700279 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700280 if (to_lower(FLAGS_config[i]).equals(name)) {
281 return true;
mtkleinf3723212014-06-25 14:08:00 -0700282 }
283 }
bsalomonc2553372014-07-22 13:09:05 -0700284 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700285}
286
bsalomonc2553372014-07-22 13:09:05 -0700287#if SK_SUPPORT_GPU
288static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
289 int sampleCnt) {
290 if (!is_cpu_config_allowed(name)) {
291 return false;
292 }
293 if (const GrContext* ctx = gGrFactory.get(ctxType)) {
294 return sampleCnt <= ctx->getMaxSampleCount();
295 }
296 return false;
297}
298#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700299
bsalomonc2553372014-07-22 13:09:05 -0700300#if SK_SUPPORT_GPU
301#define kBogusGLContextType GrContextFactory::kNative_GLContextType
302#else
303#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700304#endif
bsalomonc2553372014-07-22 13:09:05 -0700305
306// Append all configs that are enabled and supported.
307static void create_configs(SkTDArray<Config>* configs) {
308 #define CPU_CONFIG(name, backend, color, alpha) \
309 if (is_cpu_config_allowed(#name)) { \
310 Config config = { #name, Benchmark::backend, color, alpha, 0, kBogusGLContextType }; \
311 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700312 }
mtkleine714e752014-07-31 12:13:48 -0700313
mtklein40b32be2014-07-09 08:46:49 -0700314 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700315 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
316 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
317 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700318 }
mtkleinbb6a0282014-07-01 08:43:42 -0700319
320#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700321 #define GPU_CONFIG(name, ctxType, samples) \
322 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
323 Config config = { \
324 #name, \
325 Benchmark::kGPU_Backend, \
326 kN32_SkColorType, \
327 kPremul_SkAlphaType, \
328 samples, \
329 GrContextFactory::ctxType }; \
330 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700331 }
mtkleine714e752014-07-31 12:13:48 -0700332
mtklein40b32be2014-07-09 08:46:49 -0700333 if (FLAGS_gpu) {
bsalomonc2553372014-07-22 13:09:05 -0700334 GPU_CONFIG(gpu, kNative_GLContextType, 0)
335 GPU_CONFIG(msaa4, kNative_GLContextType, 4)
336 GPU_CONFIG(msaa16, kNative_GLContextType, 16)
337 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4)
338 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16)
339 GPU_CONFIG(debug, kDebug_GLContextType, 0)
340 GPU_CONFIG(nullgpu, kNull_GLContextType, 0)
bsalomon3b4d0772014-08-06 10:52:33 -0700341#ifdef SK_ANGLE
342 GPU_CONFIG(angle, kANGLE_GLContextType, 0)
343#endif
mtklein40b32be2014-07-09 08:46:49 -0700344 }
mtkleinbb6a0282014-07-01 08:43:42 -0700345#endif
mtkleinf3723212014-06-25 14:08:00 -0700346}
347
bsalomonc2553372014-07-22 13:09:05 -0700348// If bench is enabled for config, returns a Target* for it, otherwise NULL.
349static Target* is_enabled(Benchmark* bench, const Config& config) {
350 if (!bench->isSuitableFor(config.backend)) {
351 return NULL;
352 }
353
354 SkImageInfo info;
355 info.fAlphaType = config.alpha;
356 info.fColorType = config.color;
357 info.fWidth = bench->getSize().fX;
358 info.fHeight = bench->getSize().fY;
359
360 Target* target = new Target(config);
361
362 if (Benchmark::kRaster_Backend == config.backend) {
363 target->surface.reset(SkSurface::NewRaster(info));
364 }
365#if SK_SUPPORT_GPU
366 else if (Benchmark::kGPU_Backend == config.backend) {
367 target->surface.reset(SkSurface::NewRenderTarget(gGrFactory.get(config.ctxType), info,
368 config.samples));
369 target->gl = gGrFactory.getGLContext(config.ctxType);
370 }
371#endif
372
373 if (Benchmark::kNonRendering_Backend != config.backend && !target->surface.get()) {
374 delete target;
375 return NULL;
376 }
377 return target;
378}
379
380// Creates targets for a benchmark and a set of configs.
381static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
382 const SkTDArray<Config>& configs) {
383 for (int i = 0; i < configs.count(); ++i) {
384 if (Target* t = is_enabled(b, configs[i])) {
385 targets->push(t);
386 }
mtkleine714e752014-07-31 12:13:48 -0700387
bsalomonc2553372014-07-22 13:09:05 -0700388 }
389}
390
mtklein60317d0f2014-07-14 11:30:37 -0700391static void fill_static_options(ResultsWriter* log) {
392#if defined(SK_BUILD_FOR_WIN32)
393 log->option("system", "WIN32");
394#elif defined(SK_BUILD_FOR_MAC)
395 log->option("system", "MAC");
396#elif defined(SK_BUILD_FOR_ANDROID)
397 log->option("system", "ANDROID");
398#elif defined(SK_BUILD_FOR_UNIX)
399 log->option("system", "UNIX");
400#else
401 log->option("system", "other");
402#endif
mtklein60317d0f2014-07-14 11:30:37 -0700403}
404
jcgregoriobf5e5232014-07-17 13:14:16 -0700405#if SK_SUPPORT_GPU
406static void fill_gpu_options(ResultsWriter* log, SkGLContextHelper* ctx) {
jcgregorio05c45602014-07-17 13:54:36 -0700407 const GrGLubyte* version;
jcgregoriobf5e5232014-07-17 13:14:16 -0700408 SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION));
409 log->configOption("GL_VERSION", (const char*)(version));
410
411 SK_GL_RET(*ctx, version, GetString(GR_GL_RENDERER));
412 log->configOption("GL_RENDERER", (const char*) version);
413
414 SK_GL_RET(*ctx, version, GetString(GR_GL_VENDOR));
415 log->configOption("GL_VENDOR", (const char*) version);
416
417 SK_GL_RET(*ctx, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
418 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
419}
420#endif
421
mtkleine714e752014-07-31 12:13:48 -0700422class BenchmarkStream {
423public:
mtklein92007582014-08-01 07:46:52 -0700424 BenchmarkStream() : fBenches(BenchRegistry::Head())
425 , fGMs(skiagm::GMRegistry::Head())
426 , 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
454 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700455 if (fBenches) {
456 Benchmark* bench = fBenches->factory()(NULL);
457 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700458 fSourceType = "bench";
mtkleine714e752014-07-31 12:13:48 -0700459 return bench;
460 }
mtklein92007582014-08-01 07:46:52 -0700461
mtkleine714e752014-07-31 12:13:48 -0700462 while (fGMs) {
463 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
464 fGMs = fGMs->next();
465 if (gm->getFlags() & skiagm::GM::kAsBench_Flag) {
mtklein92007582014-08-01 07:46:52 -0700466 fSourceType = "gm";
mtkleine714e752014-07-31 12:13:48 -0700467 return SkNEW_ARGS(GMBench, (gm.detach()));
468 }
469 }
mtklein92007582014-08-01 07:46:52 -0700470
471 while (fCurrentScale < fScales.count()) {
472 while (fCurrentSKP < fSKPs.count()) {
473 const SkString& path = fSKPs[fCurrentSKP++];
474
475 // Not strictly necessary, as it will be checked again later,
476 // but helps to avoid a lot of pointless work if we're going to skip it.
477 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
478 continue;
479 }
480
481 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
482 if (stream.get() == NULL) {
483 SkDebugf("Could not read %s.\n", path.c_str());
484 exit(1);
485 }
486
487 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream.get()));
488 if (pic.get() == NULL) {
489 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
490 exit(1);
491 }
492
493 SkString name = SkOSPath::Basename(path.c_str());
494
495 fSourceType = "skp";
496 return SkNEW_ARGS(SKPBench,
497 (name.c_str(), pic.get(), fClip, fScales[fCurrentScale]));
498 }
499 fCurrentSKP = 0;
500 fCurrentScale++;
501 }
502
mtkleine714e752014-07-31 12:13:48 -0700503 return NULL;
504 }
mtklein92007582014-08-01 07:46:52 -0700505
506 void fillCurrentOptions(ResultsWriter* log) const {
507 log->configOption("source_type", fSourceType);
508 if (0 == strcmp(fSourceType, "skp")) {
509 log->configOption("clip",
510 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
511 fClip.fRight, fClip.fBottom).c_str());
512 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
513 }
514 }
515
mtkleine714e752014-07-31 12:13:48 -0700516private:
517 const BenchRegistry* fBenches;
518 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700519 SkIRect fClip;
520 SkTArray<SkScalar> fScales;
521 SkTArray<SkString> fSKPs;
522
523 const char* fSourceType;
524 int fCurrentScale;
525 int fCurrentSKP;
mtkleine714e752014-07-31 12:13:48 -0700526};
527
caryclark17f0b6d2014-07-22 10:15:34 -0700528int nanobench_main();
529int nanobench_main() {
mtkleinf3723212014-06-25 14:08:00 -0700530 SetupCrashHandler();
531 SkAutoGraphics ag;
mtkleinf3723212014-06-25 14:08:00 -0700532
bsalomon6eb03cc2014-08-07 14:28:50 -0700533 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700534 FLAGS_samples = 1;
535 FLAGS_gpuFrameLag = 0;
536 }
537
bsalomon6eb03cc2014-08-07 14:28:50 -0700538 if (!FLAGS_writePath.isEmpty()) {
539 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
540 if (!sk_mkdir(FLAGS_writePath[0])) {
541 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
542 FLAGS_writePath.set(0, NULL);
543 }
544 }
545
mtklein60317d0f2014-07-14 11:30:37 -0700546 MultiResultsWriter log;
jcgregoriobf5e5232014-07-17 13:14:16 -0700547 SkAutoTDelete<NanoJSONResultsWriter> json;
mtklein60317d0f2014-07-14 11:30:37 -0700548 if (!FLAGS_outResultsFile.isEmpty()) {
jcgregoriobf5e5232014-07-17 13:14:16 -0700549 const char* gitHash = FLAGS_gitHash.isEmpty() ? "unknown-revision" : FLAGS_gitHash[0];
550 json.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0], gitHash)));
mtklein60317d0f2014-07-14 11:30:37 -0700551 log.add(json.get());
552 }
553 CallEnd<MultiResultsWriter> ender(log);
jcgregoriobf5e5232014-07-17 13:14:16 -0700554
555 if (1 == FLAGS_key.count() % 2) {
556 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
557 return 1;
558 }
559 for (int i = 1; i < FLAGS_key.count(); i += 2) {
560 log.key(FLAGS_key[i-1], FLAGS_key[i]);
561 }
mtklein60317d0f2014-07-14 11:30:37 -0700562 fill_static_options(&log);
563
mtkleinf3723212014-06-25 14:08:00 -0700564 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700565 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400566
mtkleinbb6a0282014-07-01 08:43:42 -0700567 SkAutoTMalloc<double> samples(FLAGS_samples);
568
bsalomon6eb03cc2014-08-07 14:28:50 -0700569 if (kAutoTuneLoops != FLAGS_loops) {
570 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700571 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700572 // No header.
573 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700574 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700575 } else {
mtklein5d9d10e2014-07-11 11:57:07 -0700576 SkDebugf("loops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -0700577 }
578
bsalomonc2553372014-07-22 13:09:05 -0700579 SkTDArray<Config> configs;
580 create_configs(&configs);
581
mtklein92007582014-08-01 07:46:52 -0700582 BenchmarkStream benchStream;
583 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700584 SkAutoTDelete<Benchmark> bench(b);
mtkleinf3723212014-06-25 14:08:00 -0700585 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
586 continue;
587 }
588
mtkleinbb6a0282014-07-01 08:43:42 -0700589 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700590 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700591
jcgregoriobf5e5232014-07-17 13:14:16 -0700592 if (!targets.isEmpty()) {
593 log.bench(bench->getName(), bench->getSize().fX, bench->getSize().fY);
594 bench->preDraw();
595 }
mtkleinbb6a0282014-07-01 08:43:42 -0700596 for (int j = 0; j < targets.count(); j++) {
597 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
bsalomonc2553372014-07-22 13:09:05 -0700598 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700599
mtklein92007582014-08-01 07:46:52 -0700600#if SK_DEBUG
601 // skia:2797 Some SKPs SkASSERT in debug mode. Skip them for now.
mtklein6e33e232014-08-01 08:23:39 -0700602 if (0 == strcmp("565", config) && SkStrContains(bench->getName(), ".skp")) {
603 SkDebugf("Skipping 565 %s. See skia:2797\n", bench->getName());
mtklein92007582014-08-01 07:46:52 -0700604 continue;
605 }
606#endif
607
mtkleinbb6a0282014-07-01 08:43:42 -0700608 const int loops =
609#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700610 Benchmark::kGPU_Backend == targets[j]->config.backend
mtkleinbb6a0282014-07-01 08:43:42 -0700611 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
612 :
613#endif
614 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700615
bsalomon6eb03cc2014-08-07 14:28:50 -0700616 if (canvas && !FLAGS_writePath.isEmpty() && NULL != FLAGS_writePath[0]) {
617 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
618 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getName());
619 pngFilename.append(".png");
620 write_canvas_png(canvas, pngFilename);
621 }
622
623 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700624 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400625 continue;
626 }
627
mtkleinf3723212014-06-25 14:08:00 -0700628 Stats stats(samples.get(), FLAGS_samples);
mtklein60317d0f2014-07-14 11:30:37 -0700629 log.config(config);
mtklein92007582014-08-01 07:46:52 -0700630 benchStream.fillCurrentOptions(&log);
jcgregoriobf5e5232014-07-17 13:14:16 -0700631#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700632 if (Benchmark::kGPU_Backend == targets[j]->config.backend) {
jcgregoriobf5e5232014-07-17 13:14:16 -0700633 fill_gpu_options(&log, targets[j]->gl);
634 }
635#endif
mtklein60317d0f2014-07-14 11:30:37 -0700636 log.timer("min_ms", stats.min);
637 log.timer("median_ms", stats.median);
638 log.timer("mean_ms", stats.mean);
639 log.timer("max_ms", stats.max);
640 log.timer("stddev_ms", sqrt(stats.var));
641
bsalomon6eb03cc2014-08-07 14:28:50 -0700642 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700643 if (targets.count() == 1) {
644 config = ""; // Only print the config if we run the same bench on more than one.
645 }
646 SkDebugf("%s\t%s\n", bench->getName(), config);
647 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700648 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700649 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700650 }
651 SkDebugf("%s\n", bench->getName());
652 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700653 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700654 config = ""; // Only print the config if we run the same bench on more than one.
655 }
mtklein55b0ffc2014-07-17 08:38:23 -0700656 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700657 } else {
658 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtklein5d9d10e2014-07-11 11:57:07 -0700659 SkDebugf("%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
mtkleinf3723212014-06-25 14:08:00 -0700660 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700661 , HUMANIZE(stats.min)
662 , HUMANIZE(stats.median)
663 , HUMANIZE(stats.mean)
664 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700665 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700666 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700667 , config
mtkleinbb6a0282014-07-01 08:43:42 -0700668 , bench->getName()
mtkleinf3723212014-06-25 14:08:00 -0700669 );
670 }
671 }
mtkleinbb6a0282014-07-01 08:43:42 -0700672 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400673
674 #if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700675 if (FLAGS_abandonGpuContext) {
676 gGrFactory.abandonContexts();
677 }
678 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
Mike Klein3944a1d2014-07-15 13:40:19 -0400679 gGrFactory.destroyContexts();
680 }
681 #endif
mtkleinf3723212014-06-25 14:08:00 -0700682 }
683
684 return 0;
685}
686
687#if !defined SK_BUILD_FOR_IOS
caryclark17f0b6d2014-07-22 10:15:34 -0700688int main(int argc, char** argv) {
689 SkCommandLineFlags::Parse(argc, argv);
690 return nanobench_main();
mtkleinf3723212014-06-25 14:08:00 -0700691}
692#endif