blob: 6968d6dac21deb835f11063a4731b3077673398e [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"
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
mtklein92007582014-08-01 07:46:52 -070019#include "SkOSFile.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"
24#include "SkString.h"
25#include "SkSurface.h"
26
mtkleinbb6a0282014-07-01 08:43:42 -070027#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070028 #include "gl/GrGLDefines.h"
mtkleinbb6a0282014-07-01 08:43:42 -070029 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070030 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070031#endif
32
mtkleinf3723212014-06-25 14:08:00 -070033__SK_FORCE_IMAGE_DECODER_LINKING;
34
bsalomon6eb03cc2014-08-07 14:28:50 -070035static const int kAutoTuneLoops = -1;
36
mtkleinb5110422014-08-07 15:20:02 -070037static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070038#ifdef SK_DEBUG
39 1;
mtkleina189ccd2014-07-14 12:28:47 -070040#else
bsalomon6eb03cc2014-08-07 14:28:50 -070041 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070042#endif
43
bsalomon6eb03cc2014-08-07 14:28:50 -070044static SkString loops_help_txt() {
45 SkString help;
46 help.printf("Number of times to run each bench. Set this to %d to auto-"
47 "tune for each bench. Timings are only reported when auto-tuning.",
48 kAutoTuneLoops);
49 return help;
50}
51
52DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
53
mtkleinf3723212014-06-25 14:08:00 -070054DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
55DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
56DEFINE_double(overheadGoal, 0.0001,
57 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070058DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
59DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070060DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
61 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070062
mtklein60317d0f2014-07-14 11:30:37 -070063DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070064DEFINE_int32(maxCalibrationAttempts, 3,
65 "Try up to this many times to guess loops for a bench, or skip the bench.");
66DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein1915b622014-08-20 11:45:00 -070067DEFINE_string(properties, "",
68 "Space-separated key/value pairs to add to JSON identifying this nanobench run.");
mtklein94e51562014-08-19 12:41:53 -070069DEFINE_string(key, "",
70 "Space-separated key/value pairs to add to JSON identifying this bench config.");
mtklein60317d0f2014-07-14 11:30:37 -070071
mtklein92007582014-08-01 07:46:52 -070072DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
73DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
74
mtkleinf3723212014-06-25 14:08:00 -070075static SkString humanize(double ms) {
76 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3);
77 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6);
mtklein62386882014-07-15 10:30:31 -070078#ifdef SK_BUILD_FOR_WIN
79 if (ms < 1) return SkStringPrintf("%.3gus", ms*1e3);
80#else
mtkleinf3723212014-06-25 14:08:00 -070081 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3);
mtklein62386882014-07-15 10:30:31 -070082#endif
mtkleinf3723212014-06-25 14:08:00 -070083 return SkStringPrintf("%.3gms", ms);
84}
mtklein55b0ffc2014-07-17 08:38:23 -070085#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -070086
mtkleinbb6a0282014-07-01 08:43:42 -070087static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHelper* gl) {
bsalomon6eb03cc2014-08-07 14:28:50 -070088 if (canvas) {
89 canvas->clear(SK_ColorWHITE);
90 }
mtkleinbb6a0282014-07-01 08:43:42 -070091 WallTimer timer;
92 timer.start();
93 if (bench) {
94 bench->draw(loops, canvas);
95 }
96 if (canvas) {
97 canvas->flush();
98 }
99#if SK_SUPPORT_GPU
100 if (gl) {
101 SK_GL(*gl, Flush());
102 gl->swapBuffers();
103 }
104#endif
105 timer.end();
106 return timer.fWall;
107}
108
mtkleinf3723212014-06-25 14:08:00 -0700109static double estimate_timer_overhead() {
110 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700111 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -0700112 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -0700113 }
114 return overhead / FLAGS_overheadLoops;
115}
116
mtklein55b0ffc2014-07-17 08:38:23 -0700117static int clamp_loops(int loops) {
118 if (loops < 1) {
119 SkDebugf("ERROR: clamping loops from %d to 1.\n", loops);
120 return 1;
121 }
122 if (loops > FLAGS_maxLoops) {
123 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
124 return FLAGS_maxLoops;
125 }
126 return loops;
127}
128
bsalomon6eb03cc2014-08-07 14:28:50 -0700129static bool write_canvas_png(SkCanvas* canvas, const SkString& filename) {
130 if (filename.isEmpty()) {
131 return false;
132 }
133 if (kUnknown_SkColorType == canvas->imageInfo().fColorType) {
134 return false;
135 }
136 SkBitmap bmp;
137 bmp.setInfo(canvas->imageInfo());
138 if (!canvas->readPixels(&bmp, 0, 0)) {
139 SkDebugf("Can't read canvas pixels.\n");
140 return false;
141 }
142 SkString dir = SkOSPath::Dirname(filename.c_str());
143 if (!sk_mkdir(dir.c_str())) {
144 SkDebugf("Can't make dir %s.\n", dir.c_str());
145 return false;
146 }
147 SkFILEWStream stream(filename.c_str());
148 if (!stream.isValid()) {
149 SkDebugf("Can't write %s.\n", filename.c_str());
150 return false;
151 }
152 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
153 SkDebugf("Can't encode a PNG.\n");
154 return false;
155 }
156 return true;
157}
158
159static int kFailedLoops = -2;
mtkleinbb6a0282014-07-01 08:43:42 -0700160static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
161 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700162 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700163 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700164 if (kAutoTuneLoops == FLAGS_loops) {
165 while (bench_plus_overhead < overhead) {
166 if (round++ == FLAGS_maxCalibrationAttempts) {
167 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
168 bench->getName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
169 return kFailedLoops;
170 }
171 bench_plus_overhead = time(1, bench, canvas, NULL);
mtklein55b0ffc2014-07-17 08:38:23 -0700172 }
mtklein2069e222014-08-04 13:57:39 -0700173 }
mtkleinf3723212014-06-25 14:08:00 -0700174
mtkleinbb6a0282014-07-01 08:43:42 -0700175 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700176 // We'll pick N to make timer overhead negligible:
177 //
mtkleinbb6a0282014-07-01 08:43:42 -0700178 // overhead
179 // ------------------------- < FLAGS_overheadGoal
180 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700181 //
mtkleinbb6a0282014-07-01 08:43:42 -0700182 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700183 //
184 // Doing some math, we get:
185 //
mtkleinbb6a0282014-07-01 08:43:42 -0700186 // (overhead / FLAGS_overheadGoal) - overhead
187 // ------------------------------------------ < N
188 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700189 //
190 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700191 int loops = FLAGS_loops;
192 if (kAutoTuneLoops == loops) {
193 const double numer = overhead / FLAGS_overheadGoal - overhead;
194 const double denom = bench_plus_overhead - overhead;
195 loops = (int)ceil(numer / denom);
196 }
197 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700198
199 for (int i = 0; i < FLAGS_samples; i++) {
200 samples[i] = time(loops, bench, canvas, NULL) / loops;
201 }
202 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700203}
204
mtkleinbb6a0282014-07-01 08:43:42 -0700205#if SK_SUPPORT_GPU
206static int gpu_bench(SkGLContextHelper* gl,
207 Benchmark* bench,
208 SkCanvas* canvas,
209 double* samples) {
bsalomonc2553372014-07-22 13:09:05 -0700210 gl->makeCurrent();
mtkleinbb6a0282014-07-01 08:43:42 -0700211 // Make sure we're done with whatever came before.
mtklein9bc86ed2014-07-01 10:02:42 -0700212 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700213
214 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700215 int loops = FLAGS_loops;
216 if (kAutoTuneLoops == loops) {
217 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700218 double elapsed = 0;
219 do {
220 loops *= 2;
221 // If the GPU lets frames lag at all, we need to make sure we're timing
222 // _this_ round, not still timing last round. We force this by looping
223 // more times than any reasonable GPU will allow frames to lag.
224 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
225 elapsed = time(loops, bench, canvas, gl);
226 }
227 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700228
mtkleina189ccd2014-07-14 12:28:47 -0700229 // We've overshot at least a little. Scale back linearly.
230 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
mtkleinbb6a0282014-07-01 08:43:42 -0700231
mtkleina189ccd2014-07-14 12:28:47 -0700232 // Might as well make sure we're not still timing our calibration.
233 SK_GL(*gl, Finish());
234 }
mtklein55b0ffc2014-07-17 08:38:23 -0700235 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700236
237 // Pretty much the same deal as the calibration: do some warmup to make
238 // sure we're timing steady-state pipelined frames.
239 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
240 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700241 }
mtkleinbb6a0282014-07-01 08:43:42 -0700242
243 // Now, actually do the timing!
244 for (int i = 0; i < FLAGS_samples; i++) {
245 samples[i] = time(loops, bench, canvas, gl) / loops;
246 }
247 return loops;
248}
249#endif
250
251static SkString to_lower(const char* str) {
252 SkString lower(str);
253 for (size_t i = 0; i < lower.size(); i++) {
254 lower[i] = tolower(lower[i]);
255 }
256 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700257}
258
bsalomonc2553372014-07-22 13:09:05 -0700259struct Config {
260 const char* name;
mtkleinbb6a0282014-07-01 08:43:42 -0700261 Benchmark::Backend backend;
bsalomonc2553372014-07-22 13:09:05 -0700262 SkColorType color;
263 SkAlphaType alpha;
264 int samples;
265#if SK_SUPPORT_GPU
266 GrContextFactory::GLContextType ctxType;
267#else
268 int bogusInt;
269#endif
270};
271
272struct Target {
273 explicit Target(const Config& c) : config(c) {}
274 const Config config;
mtkleinbb6a0282014-07-01 08:43:42 -0700275 SkAutoTDelete<SkSurface> surface;
276#if SK_SUPPORT_GPU
277 SkGLContextHelper* gl;
278#endif
279};
mtkleinf3723212014-06-25 14:08:00 -0700280
bsalomonc2553372014-07-22 13:09:05 -0700281static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700282 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700283 if (to_lower(FLAGS_config[i]).equals(name)) {
284 return true;
mtkleinf3723212014-06-25 14:08:00 -0700285 }
286 }
bsalomonc2553372014-07-22 13:09:05 -0700287 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700288}
289
bsalomonc2553372014-07-22 13:09:05 -0700290#if SK_SUPPORT_GPU
291static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
292 int sampleCnt) {
293 if (!is_cpu_config_allowed(name)) {
294 return false;
295 }
krajcevski69a55602014-08-13 10:46:31 -0700296 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700297 return sampleCnt <= ctx->getMaxSampleCount();
298 }
299 return false;
300}
301#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700302
bsalomonc2553372014-07-22 13:09:05 -0700303#if SK_SUPPORT_GPU
304#define kBogusGLContextType GrContextFactory::kNative_GLContextType
305#else
306#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700307#endif
bsalomonc2553372014-07-22 13:09:05 -0700308
309// Append all configs that are enabled and supported.
310static void create_configs(SkTDArray<Config>* configs) {
311 #define CPU_CONFIG(name, backend, color, alpha) \
312 if (is_cpu_config_allowed(#name)) { \
313 Config config = { #name, Benchmark::backend, color, alpha, 0, kBogusGLContextType }; \
314 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700315 }
mtkleine714e752014-07-31 12:13:48 -0700316
mtklein40b32be2014-07-09 08:46:49 -0700317 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700318 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
319 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
320 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700321 }
mtkleinbb6a0282014-07-01 08:43:42 -0700322
323#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700324 #define GPU_CONFIG(name, ctxType, samples) \
325 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
326 Config config = { \
327 #name, \
328 Benchmark::kGPU_Backend, \
329 kN32_SkColorType, \
330 kPremul_SkAlphaType, \
331 samples, \
332 GrContextFactory::ctxType }; \
333 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700334 }
mtkleine714e752014-07-31 12:13:48 -0700335
mtklein40b32be2014-07-09 08:46:49 -0700336 if (FLAGS_gpu) {
bsalomonc2553372014-07-22 13:09:05 -0700337 GPU_CONFIG(gpu, kNative_GLContextType, 0)
338 GPU_CONFIG(msaa4, kNative_GLContextType, 4)
339 GPU_CONFIG(msaa16, kNative_GLContextType, 16)
340 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4)
341 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16)
342 GPU_CONFIG(debug, kDebug_GLContextType, 0)
343 GPU_CONFIG(nullgpu, kNull_GLContextType, 0)
bsalomon3b4d0772014-08-06 10:52:33 -0700344#ifdef SK_ANGLE
345 GPU_CONFIG(angle, kANGLE_GLContextType, 0)
346#endif
mtklein40b32be2014-07-09 08:46:49 -0700347 }
mtkleinbb6a0282014-07-01 08:43:42 -0700348#endif
mtkleinf3723212014-06-25 14:08:00 -0700349}
350
bsalomonc2553372014-07-22 13:09:05 -0700351// If bench is enabled for config, returns a Target* for it, otherwise NULL.
352static Target* is_enabled(Benchmark* bench, const Config& config) {
353 if (!bench->isSuitableFor(config.backend)) {
354 return NULL;
355 }
356
357 SkImageInfo info;
358 info.fAlphaType = config.alpha;
359 info.fColorType = config.color;
360 info.fWidth = bench->getSize().fX;
361 info.fHeight = bench->getSize().fY;
362
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
484 fSourceType = "skp";
485 return SkNEW_ARGS(SKPBench,
486 (name.c_str(), pic.get(), fClip, fScales[fCurrentScale]));
487 }
488 fCurrentSKP = 0;
489 fCurrentScale++;
490 }
491
mtkleine714e752014-07-31 12:13:48 -0700492 return NULL;
493 }
mtklein92007582014-08-01 07:46:52 -0700494
495 void fillCurrentOptions(ResultsWriter* log) const {
496 log->configOption("source_type", fSourceType);
497 if (0 == strcmp(fSourceType, "skp")) {
498 log->configOption("clip",
499 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
500 fClip.fRight, fClip.fBottom).c_str());
501 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
502 }
503 }
504
mtkleine714e752014-07-31 12:13:48 -0700505private:
506 const BenchRegistry* fBenches;
507 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700508 SkIRect fClip;
509 SkTArray<SkScalar> fScales;
510 SkTArray<SkString> fSKPs;
511
512 const char* fSourceType;
513 int fCurrentScale;
514 int fCurrentSKP;
mtkleine714e752014-07-31 12:13:48 -0700515};
516
caryclark17f0b6d2014-07-22 10:15:34 -0700517int nanobench_main();
518int nanobench_main() {
mtkleinf3723212014-06-25 14:08:00 -0700519 SetupCrashHandler();
520 SkAutoGraphics ag;
mtkleinf3723212014-06-25 14:08:00 -0700521
krajcevski69a55602014-08-13 10:46:31 -0700522#if SK_SUPPORT_GPU
krajcevski12b35442014-08-13 12:06:26 -0700523 GrContext::Options grContextOpts;
524 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
525 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700526#endif
527
bsalomon6eb03cc2014-08-07 14:28:50 -0700528 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700529 FLAGS_samples = 1;
530 FLAGS_gpuFrameLag = 0;
531 }
532
bsalomon6eb03cc2014-08-07 14:28:50 -0700533 if (!FLAGS_writePath.isEmpty()) {
534 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
535 if (!sk_mkdir(FLAGS_writePath[0])) {
536 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
537 FLAGS_writePath.set(0, NULL);
538 }
539 }
540
mtklein1915b622014-08-20 11:45:00 -0700541 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700542 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700543 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700544 }
mtklein1915b622014-08-20 11:45:00 -0700545
546 if (1 == FLAGS_properties.count() % 2) {
547 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
548 return 1;
549 }
550 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
551 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
552 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700553
554 if (1 == FLAGS_key.count() % 2) {
555 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
556 return 1;
557 }
558 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700559 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700560 }
mtklein60317d0f2014-07-14 11:30:37 -0700561
mtkleinf3723212014-06-25 14:08:00 -0700562 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700563 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400564
mtkleinbb6a0282014-07-01 08:43:42 -0700565 SkAutoTMalloc<double> samples(FLAGS_samples);
566
bsalomon6eb03cc2014-08-07 14:28:50 -0700567 if (kAutoTuneLoops != FLAGS_loops) {
568 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700569 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700570 // No header.
571 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700572 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700573 } else {
mtkleinafb43792014-08-19 15:55:55 -0700574 SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -0700575 }
576
bsalomonc2553372014-07-22 13:09:05 -0700577 SkTDArray<Config> configs;
578 create_configs(&configs);
579
mtklein92007582014-08-01 07:46:52 -0700580 BenchmarkStream benchStream;
581 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700582 SkAutoTDelete<Benchmark> bench(b);
mtkleinf3723212014-06-25 14:08:00 -0700583 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
584 continue;
585 }
586
mtkleinbb6a0282014-07-01 08:43:42 -0700587 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700588 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700589
jcgregoriobf5e5232014-07-17 13:14:16 -0700590 if (!targets.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700591 log->bench(bench->getName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700592 bench->preDraw();
593 }
mtkleinbb6a0282014-07-01 08:43:42 -0700594 for (int j = 0; j < targets.count(); j++) {
595 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
bsalomonc2553372014-07-22 13:09:05 -0700596 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700597
mtklein753b8702014-08-20 07:38:46 -0700598#ifdef SK_DEBUG
mtklein92007582014-08-01 07:46:52 -0700599 // skia:2797 Some SKPs SkASSERT in debug mode. Skip them for now.
mtklein6e33e232014-08-01 08:23:39 -0700600 if (0 == strcmp("565", config) && SkStrContains(bench->getName(), ".skp")) {
601 SkDebugf("Skipping 565 %s. See skia:2797\n", bench->getName());
mtklein92007582014-08-01 07:46:52 -0700602 continue;
603 }
604#endif
605
mtkleinbb6a0282014-07-01 08:43:42 -0700606 const int loops =
607#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700608 Benchmark::kGPU_Backend == targets[j]->config.backend
mtkleinbb6a0282014-07-01 08:43:42 -0700609 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
610 :
611#endif
612 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700613
bsalomon6eb03cc2014-08-07 14:28:50 -0700614 if (canvas && !FLAGS_writePath.isEmpty() && NULL != FLAGS_writePath[0]) {
615 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
616 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getName());
617 pngFilename.append(".png");
618 write_canvas_png(canvas, pngFilename);
619 }
620
621 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700622 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400623 continue;
624 }
625
mtkleinf3723212014-06-25 14:08:00 -0700626 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700627 log->config(config);
628 benchStream.fillCurrentOptions(log.get());
jcgregoriobf5e5232014-07-17 13:14:16 -0700629#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700630 if (Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein1915b622014-08-20 11:45:00 -0700631 fill_gpu_options(log.get(), targets[j]->gl);
jcgregoriobf5e5232014-07-17 13:14:16 -0700632 }
633#endif
mtklein1915b622014-08-20 11:45:00 -0700634 log->timer("min_ms", stats.min);
635 log->timer("median_ms", stats.median);
636 log->timer("mean_ms", stats.mean);
637 log->timer("max_ms", stats.max);
638 log->timer("stddev_ms", sqrt(stats.var));
mtklein60317d0f2014-07-14 11:30:37 -0700639
bsalomon6eb03cc2014-08-07 14:28:50 -0700640 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700641 if (targets.count() == 1) {
642 config = ""; // Only print the config if we run the same bench on more than one.
643 }
644 SkDebugf("%s\t%s\n", bench->getName(), config);
645 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700646 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700647 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700648 }
649 SkDebugf("%s\n", bench->getName());
650 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700651 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700652 config = ""; // Only print the config if we run the same bench on more than one.
653 }
mtklein55b0ffc2014-07-17 08:38:23 -0700654 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700655 } else {
656 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleinafb43792014-08-19 15:55:55 -0700657 SkDebugf("%4dM\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
658 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700659 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700660 , HUMANIZE(stats.min)
661 , HUMANIZE(stats.median)
662 , HUMANIZE(stats.mean)
663 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700664 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700665 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700666 , config
mtkleinbb6a0282014-07-01 08:43:42 -0700667 , bench->getName()
mtkleinf3723212014-06-25 14:08:00 -0700668 );
669 }
670 }
mtkleinbb6a0282014-07-01 08:43:42 -0700671 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400672
673 #if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700674 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700675 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700676 }
677 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700678 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400679 }
680 #endif
mtkleinf3723212014-06-25 14:08:00 -0700681 }
682
683 return 0;
684}
685
686#if !defined SK_BUILD_FOR_IOS
caryclark17f0b6d2014-07-22 10:15:34 -0700687int main(int argc, char** argv) {
688 SkCommandLineFlags::Parse(argc, argv);
689 return nanobench_main();
mtkleinf3723212014-06-25 14:08:00 -0700690}
691#endif