blob: a9862c6ef01008a59e2c029bdd81e2ca977bc226 [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"
mtklein60317d0f2014-07-14 11:30:37 -070012#include "ResultsWriter.h"
mtkleinf3723212014-06-25 14:08:00 -070013#include "Stats.h"
14#include "Timer.h"
15
16#include "SkCanvas.h"
17#include "SkCommandLineFlags.h"
18#include "SkForceLinking.h"
19#include "SkGraphics.h"
20#include "SkString.h"
21#include "SkSurface.h"
22
mtkleinbb6a0282014-07-01 08:43:42 -070023#if SK_SUPPORT_GPU
24 #include "GrContextFactory.h"
25 GrContextFactory gGrFactory;
26#endif
27
mtkleinf3723212014-06-25 14:08:00 -070028__SK_FORCE_IMAGE_DECODER_LINKING;
29
mtkleina189ccd2014-07-14 12:28:47 -070030#if SK_DEBUG
31 DEFINE_bool(runOnce, true, "Run each benchmark just once?");
32#else
33 DEFINE_bool(runOnce, false, "Run each benchmark just once?");
34#endif
35
mtkleinf3723212014-06-25 14:08:00 -070036DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
37DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
38DEFINE_double(overheadGoal, 0.0001,
39 "Loop until timer overhead is at most this fraction of our measurments.");
40DEFINE_string(match, "", "The usual filters on file names of benchmarks to measure.");
41DEFINE_bool2(quiet, q, false, "Print only bench name and minimum sample.");
42DEFINE_bool2(verbose, v, false, "Print all samples.");
mtkleinbb6a0282014-07-01 08:43:42 -070043DEFINE_string(config, "nonrendering 8888 gpu", "Configs to measure. Options: "
44 "565 8888 gpu nonrendering debug nullgpu msaa4 msaa16 nvprmsaa4 nvprmsaa16 angle");
45DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
46DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -070047
mtklein40b32be2014-07-09 08:46:49 -070048DEFINE_bool(cpu, true, "Master switch for CPU-bound work.");
49DEFINE_bool(gpu, true, "Master switch for GPU-bound work.");
50
mtklein60317d0f2014-07-14 11:30:37 -070051DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein1e319f72014-07-15 08:27:06 -070052DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each bench.");
mtklein60317d0f2014-07-14 11:30:37 -070053
mtkleinf3723212014-06-25 14:08:00 -070054
55static SkString humanize(double ms) {
56 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3);
57 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6);
mtklein62386882014-07-15 10:30:31 -070058#ifdef SK_BUILD_FOR_WIN
59 if (ms < 1) return SkStringPrintf("%.3gus", ms*1e3);
60#else
mtkleinf3723212014-06-25 14:08:00 -070061 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3);
mtklein62386882014-07-15 10:30:31 -070062#endif
mtkleinf3723212014-06-25 14:08:00 -070063 return SkStringPrintf("%.3gms", ms);
64}
65
mtkleinbb6a0282014-07-01 08:43:42 -070066static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHelper* gl) {
67 WallTimer timer;
68 timer.start();
69 if (bench) {
70 bench->draw(loops, canvas);
71 }
72 if (canvas) {
73 canvas->flush();
74 }
75#if SK_SUPPORT_GPU
76 if (gl) {
77 SK_GL(*gl, Flush());
78 gl->swapBuffers();
79 }
80#endif
81 timer.end();
82 return timer.fWall;
83}
84
mtkleinf3723212014-06-25 14:08:00 -070085static double estimate_timer_overhead() {
86 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -070087 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -070088 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -070089 }
90 return overhead / FLAGS_overheadLoops;
91}
92
mtkleinbb6a0282014-07-01 08:43:42 -070093static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
94 // First figure out approximately how many loops of bench it takes to make overhead negligible.
95 double bench_plus_overhead;
mtkleinf3723212014-06-25 14:08:00 -070096 do {
mtkleinbb6a0282014-07-01 08:43:42 -070097 bench_plus_overhead = time(1, bench, canvas, NULL);
Mike Kleine3631362014-07-15 17:56:37 -040098 } while (bench_plus_overhead < overhead);
mtkleinf3723212014-06-25 14:08:00 -070099
mtkleinbb6a0282014-07-01 08:43:42 -0700100 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700101 // We'll pick N to make timer overhead negligible:
102 //
mtkleinbb6a0282014-07-01 08:43:42 -0700103 // overhead
104 // ------------------------- < FLAGS_overheadGoal
105 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700106 //
mtkleinbb6a0282014-07-01 08:43:42 -0700107 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700108 //
109 // Doing some math, we get:
110 //
mtkleinbb6a0282014-07-01 08:43:42 -0700111 // (overhead / FLAGS_overheadGoal) - overhead
112 // ------------------------------------------ < N
113 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700114 //
115 // Luckily, this also works well in practice. :)
116 const double numer = overhead / FLAGS_overheadGoal - overhead;
mtkleinbb6a0282014-07-01 08:43:42 -0700117 const double denom = bench_plus_overhead - overhead;
mtkleina189ccd2014-07-14 12:28:47 -0700118 const int loops = FLAGS_runOnce ? 1 : (int)ceil(numer / denom);
mtkleinbb6a0282014-07-01 08:43:42 -0700119
120 for (int i = 0; i < FLAGS_samples; i++) {
121 samples[i] = time(loops, bench, canvas, NULL) / loops;
122 }
123 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700124}
125
mtkleinbb6a0282014-07-01 08:43:42 -0700126#if SK_SUPPORT_GPU
127static int gpu_bench(SkGLContextHelper* gl,
128 Benchmark* bench,
129 SkCanvas* canvas,
130 double* samples) {
131 // Make sure we're done with whatever came before.
mtklein9bc86ed2014-07-01 10:02:42 -0700132 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700133
134 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
135 int loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700136 if (!FLAGS_runOnce) {
137 double elapsed = 0;
138 do {
139 loops *= 2;
140 // If the GPU lets frames lag at all, we need to make sure we're timing
141 // _this_ round, not still timing last round. We force this by looping
142 // more times than any reasonable GPU will allow frames to lag.
143 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
144 elapsed = time(loops, bench, canvas, gl);
145 }
146 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700147
mtkleina189ccd2014-07-14 12:28:47 -0700148 // We've overshot at least a little. Scale back linearly.
149 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
mtkleinbb6a0282014-07-01 08:43:42 -0700150
mtkleina189ccd2014-07-14 12:28:47 -0700151 // Might as well make sure we're not still timing our calibration.
152 SK_GL(*gl, Finish());
153 }
mtkleinbb6a0282014-07-01 08:43:42 -0700154
155 // Pretty much the same deal as the calibration: do some warmup to make
156 // sure we're timing steady-state pipelined frames.
157 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
158 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700159 }
mtkleinbb6a0282014-07-01 08:43:42 -0700160
161 // Now, actually do the timing!
162 for (int i = 0; i < FLAGS_samples; i++) {
163 samples[i] = time(loops, bench, canvas, gl) / loops;
164 }
165 return loops;
166}
167#endif
168
169static SkString to_lower(const char* str) {
170 SkString lower(str);
171 for (size_t i = 0; i < lower.size(); i++) {
172 lower[i] = tolower(lower[i]);
173 }
174 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700175}
176
mtkleinbb6a0282014-07-01 08:43:42 -0700177struct Target {
178 const char* config;
179 Benchmark::Backend backend;
180 SkAutoTDelete<SkSurface> surface;
181#if SK_SUPPORT_GPU
182 SkGLContextHelper* gl;
183#endif
184};
mtkleinf3723212014-06-25 14:08:00 -0700185
mtkleinbb6a0282014-07-01 08:43:42 -0700186// If bench is enabled for backend/config, returns a Target* for them, otherwise NULL.
187static Target* is_enabled(Benchmark* bench, Benchmark::Backend backend, const char* config) {
188 if (!bench->isSuitableFor(backend)) {
189 return NULL;
mtkleinf3723212014-06-25 14:08:00 -0700190 }
191
mtkleinbb6a0282014-07-01 08:43:42 -0700192 for (int i = 0; i < FLAGS_config.count(); i++) {
193 if (to_lower(FLAGS_config[i]).equals(config)) {
194 Target* target = new Target;
195 target->config = config;
196 target->backend = backend;
197 return target;
mtkleinf3723212014-06-25 14:08:00 -0700198 }
199 }
mtkleinbb6a0282014-07-01 08:43:42 -0700200 return NULL;
201}
202
203// Append all targets that are suitable for bench.
204static void create_targets(Benchmark* bench, SkTDArray<Target*>* targets) {
205 const int w = bench->getSize().fX,
206 h = bench->getSize().fY;
207 const SkImageInfo _8888 = { w, h, kN32_SkColorType, kPremul_SkAlphaType },
208 _565 = { w, h, kRGB_565_SkColorType, kOpaque_SkAlphaType };
209
210 #define CPU_TARGET(config, backend, code) \
211 if (Target* t = is_enabled(bench, Benchmark::backend, #config)) { \
212 t->surface.reset(code); \
213 targets->push(t); \
214 }
mtklein40b32be2014-07-09 08:46:49 -0700215 if (FLAGS_cpu) {
216 CPU_TARGET(nonrendering, kNonRendering_Backend, NULL)
217 CPU_TARGET(8888, kRaster_Backend, SkSurface::NewRaster(_8888))
218 CPU_TARGET(565, kRaster_Backend, SkSurface::NewRaster(_565))
219 }
mtkleinbb6a0282014-07-01 08:43:42 -0700220
221#if SK_SUPPORT_GPU
mtklein1e319f72014-07-15 08:27:06 -0700222
mtkleinbb6a0282014-07-01 08:43:42 -0700223 #define GPU_TARGET(config, ctxType, info, samples) \
224 if (Target* t = is_enabled(bench, Benchmark::kGPU_Backend, #config)) { \
225 t->surface.reset(SkSurface::NewRenderTarget(gGrFactory.get(ctxType), info, samples)); \
226 t->gl = gGrFactory.getGLContext(ctxType); \
227 targets->push(t); \
228 }
mtklein40b32be2014-07-09 08:46:49 -0700229 if (FLAGS_gpu) {
230 GPU_TARGET(gpu, GrContextFactory::kNative_GLContextType, _8888, 0)
231 GPU_TARGET(msaa4, GrContextFactory::kNative_GLContextType, _8888, 4)
232 GPU_TARGET(msaa16, GrContextFactory::kNative_GLContextType, _8888, 16)
233 GPU_TARGET(nvprmsaa4, GrContextFactory::kNVPR_GLContextType, _8888, 4)
234 GPU_TARGET(nvprmsaa16, GrContextFactory::kNVPR_GLContextType, _8888, 16)
235 GPU_TARGET(debug, GrContextFactory::kDebug_GLContextType, _8888, 0)
236 GPU_TARGET(nullgpu, GrContextFactory::kNull_GLContextType, _8888, 0)
237 #if SK_ANGLE
238 GPU_TARGET(angle, GrContextFactory::kANGLE_GLContextType, _8888, 0)
239 #endif
240 }
mtkleinbb6a0282014-07-01 08:43:42 -0700241#endif
mtkleinf3723212014-06-25 14:08:00 -0700242}
243
mtklein60317d0f2014-07-14 11:30:37 -0700244static void fill_static_options(ResultsWriter* log) {
245#if defined(SK_BUILD_FOR_WIN32)
246 log->option("system", "WIN32");
247#elif defined(SK_BUILD_FOR_MAC)
248 log->option("system", "MAC");
249#elif defined(SK_BUILD_FOR_ANDROID)
250 log->option("system", "ANDROID");
251#elif defined(SK_BUILD_FOR_UNIX)
252 log->option("system", "UNIX");
253#else
254 log->option("system", "other");
255#endif
256#if defined(SK_DEBUG)
257 log->option("build", "DEBUG");
258#else
259 log->option("build", "RELEASE");
260#endif
261}
262
mtkleinf3723212014-06-25 14:08:00 -0700263int tool_main(int argc, char** argv);
264int tool_main(int argc, char** argv) {
265 SetupCrashHandler();
266 SkAutoGraphics ag;
267 SkCommandLineFlags::Parse(argc, argv);
268
mtkleina189ccd2014-07-14 12:28:47 -0700269 if (FLAGS_runOnce) {
270 FLAGS_samples = 1;
271 FLAGS_gpuFrameLag = 0;
272 }
273
mtklein60317d0f2014-07-14 11:30:37 -0700274 MultiResultsWriter log;
275 SkAutoTDelete<JSONResultsWriter> json;
276 if (!FLAGS_outResultsFile.isEmpty()) {
277 json.reset(SkNEW(JSONResultsWriter(FLAGS_outResultsFile[0])));
278 log.add(json.get());
279 }
280 CallEnd<MultiResultsWriter> ender(log);
281 fill_static_options(&log);
282
mtkleinf3723212014-06-25 14:08:00 -0700283 const double overhead = estimate_timer_overhead();
Mike Klein91294772014-07-16 19:59:32 -0400284 SkDebugf("Timer overhead: %s\n", humanize(overhead).c_str());
285
mtkleinbb6a0282014-07-01 08:43:42 -0700286 SkAutoTMalloc<double> samples(FLAGS_samples);
287
mtkleina189ccd2014-07-14 12:28:47 -0700288 if (FLAGS_runOnce) {
289 SkDebugf("--runOnce is true; times would only be misleading so we won't print them.\n");
290 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700291 // No header.
292 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700293 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700294 } else {
mtklein5d9d10e2014-07-11 11:57:07 -0700295 SkDebugf("loops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -0700296 }
297
298 for (const BenchRegistry* r = BenchRegistry::Head(); r != NULL; r = r->next()) {
299 SkAutoTDelete<Benchmark> bench(r->factory()(NULL));
300 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
301 continue;
302 }
mtklein60317d0f2014-07-14 11:30:37 -0700303 log.bench(bench->getName(), bench->getSize().fX, bench->getSize().fY);
mtkleinf3723212014-06-25 14:08:00 -0700304
mtkleinbb6a0282014-07-01 08:43:42 -0700305 SkTDArray<Target*> targets;
306 create_targets(bench.get(), &targets);
mtkleinf3723212014-06-25 14:08:00 -0700307
308 bench->preDraw();
mtkleinbb6a0282014-07-01 08:43:42 -0700309 for (int j = 0; j < targets.count(); j++) {
310 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
Mike Kleine3631362014-07-15 17:56:37 -0400311 const char* config = targets[j]->config;
mtkleinf3723212014-06-25 14:08:00 -0700312
mtkleinbb6a0282014-07-01 08:43:42 -0700313 const int loops =
314#if SK_SUPPORT_GPU
315 Benchmark::kGPU_Backend == targets[j]->backend
316 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
317 :
318#endif
319 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700320
Mike Kleine3631362014-07-15 17:56:37 -0400321 if (loops == 0) {
322 SkDebugf("Unable to time %s\t%s (overhead %s)\n",
323 bench->getName(), config, humanize(overhead).c_str());
324 continue;
325 }
326
mtkleinf3723212014-06-25 14:08:00 -0700327 Stats stats(samples.get(), FLAGS_samples);
mtklein60317d0f2014-07-14 11:30:37 -0700328 log.config(config);
329 log.timer("min_ms", stats.min);
330 log.timer("median_ms", stats.median);
331 log.timer("mean_ms", stats.mean);
332 log.timer("max_ms", stats.max);
333 log.timer("stddev_ms", sqrt(stats.var));
334
mtkleina189ccd2014-07-14 12:28:47 -0700335 if (FLAGS_runOnce) {
336 if (targets.count() == 1) {
337 config = ""; // Only print the config if we run the same bench on more than one.
338 }
339 SkDebugf("%s\t%s\n", bench->getName(), config);
340 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700341 for (int i = 0; i < FLAGS_samples; i++) {
342 SkDebugf("%s ", humanize(samples[i]).c_str());
343 }
344 SkDebugf("%s\n", bench->getName());
345 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700346 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700347 config = ""; // Only print the config if we run the same bench on more than one.
348 }
mtklein40b32be2014-07-09 08:46:49 -0700349 SkDebugf("%s\t%s\t%s\n", humanize(stats.median).c_str(), bench->getName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700350 } else {
351 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtklein5d9d10e2014-07-11 11:57:07 -0700352 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 -0700353 , loops
354 , humanize(stats.min).c_str()
mtklein40b32be2014-07-09 08:46:49 -0700355 , humanize(stats.median).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700356 , humanize(stats.mean).c_str()
357 , humanize(stats.max).c_str()
358 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700359 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700360 , config
mtkleinbb6a0282014-07-01 08:43:42 -0700361 , bench->getName()
mtkleinf3723212014-06-25 14:08:00 -0700362 );
363 }
364 }
mtkleinbb6a0282014-07-01 08:43:42 -0700365 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400366
367 #if SK_SUPPORT_GPU
368 if (FLAGS_resetGpuContext) {
369 gGrFactory.destroyContexts();
370 }
371 #endif
mtkleinf3723212014-06-25 14:08:00 -0700372 }
373
374 return 0;
375}
376
377#if !defined SK_BUILD_FOR_IOS
378int main(int argc, char * const argv[]) {
379 return tool_main(argc, (char**) argv);
380}
381#endif