blob: be8cc4a45f911df1fab12fc5db0ae6c7f65029aa [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.");
52
mtkleinf3723212014-06-25 14:08:00 -070053
54static SkString humanize(double ms) {
55 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3);
56 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6);
57 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3);
58 return SkStringPrintf("%.3gms", ms);
59}
60
mtkleinbb6a0282014-07-01 08:43:42 -070061static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHelper* gl) {
62 WallTimer timer;
63 timer.start();
64 if (bench) {
65 bench->draw(loops, canvas);
66 }
67 if (canvas) {
68 canvas->flush();
69 }
70#if SK_SUPPORT_GPU
71 if (gl) {
72 SK_GL(*gl, Flush());
73 gl->swapBuffers();
74 }
75#endif
76 timer.end();
77 return timer.fWall;
78}
79
mtkleinf3723212014-06-25 14:08:00 -070080static double estimate_timer_overhead() {
81 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -070082 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -070083 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -070084 }
85 return overhead / FLAGS_overheadLoops;
86}
87
mtkleinbb6a0282014-07-01 08:43:42 -070088static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
89 // First figure out approximately how many loops of bench it takes to make overhead negligible.
90 double bench_plus_overhead;
mtkleinf3723212014-06-25 14:08:00 -070091 do {
mtkleinbb6a0282014-07-01 08:43:42 -070092 bench_plus_overhead = time(1, bench, canvas, NULL);
93 } while (bench_plus_overhead < overhead); // Shouldn't normally happen.
mtkleinf3723212014-06-25 14:08:00 -070094
mtkleinbb6a0282014-07-01 08:43:42 -070095 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -070096 // We'll pick N to make timer overhead negligible:
97 //
mtkleinbb6a0282014-07-01 08:43:42 -070098 // overhead
99 // ------------------------- < FLAGS_overheadGoal
100 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700101 //
mtkleinbb6a0282014-07-01 08:43:42 -0700102 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700103 //
104 // Doing some math, we get:
105 //
mtkleinbb6a0282014-07-01 08:43:42 -0700106 // (overhead / FLAGS_overheadGoal) - overhead
107 // ------------------------------------------ < N
108 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700109 //
110 // Luckily, this also works well in practice. :)
111 const double numer = overhead / FLAGS_overheadGoal - overhead;
mtkleinbb6a0282014-07-01 08:43:42 -0700112 const double denom = bench_plus_overhead - overhead;
mtkleina189ccd2014-07-14 12:28:47 -0700113 const int loops = FLAGS_runOnce ? 1 : (int)ceil(numer / denom);
mtkleinbb6a0282014-07-01 08:43:42 -0700114
115 for (int i = 0; i < FLAGS_samples; i++) {
116 samples[i] = time(loops, bench, canvas, NULL) / loops;
117 }
118 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700119}
120
mtkleinbb6a0282014-07-01 08:43:42 -0700121#if SK_SUPPORT_GPU
122static int gpu_bench(SkGLContextHelper* gl,
123 Benchmark* bench,
124 SkCanvas* canvas,
125 double* samples) {
126 // Make sure we're done with whatever came before.
mtklein9bc86ed2014-07-01 10:02:42 -0700127 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700128
129 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
130 int loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700131 if (!FLAGS_runOnce) {
132 double elapsed = 0;
133 do {
134 loops *= 2;
135 // If the GPU lets frames lag at all, we need to make sure we're timing
136 // _this_ round, not still timing last round. We force this by looping
137 // more times than any reasonable GPU will allow frames to lag.
138 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
139 elapsed = time(loops, bench, canvas, gl);
140 }
141 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700142
mtkleina189ccd2014-07-14 12:28:47 -0700143 // We've overshot at least a little. Scale back linearly.
144 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
mtkleinbb6a0282014-07-01 08:43:42 -0700145
mtkleina189ccd2014-07-14 12:28:47 -0700146 // Might as well make sure we're not still timing our calibration.
147 SK_GL(*gl, Finish());
148 }
mtkleinbb6a0282014-07-01 08:43:42 -0700149
150 // Pretty much the same deal as the calibration: do some warmup to make
151 // sure we're timing steady-state pipelined frames.
152 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
153 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700154 }
mtkleinbb6a0282014-07-01 08:43:42 -0700155
156 // Now, actually do the timing!
157 for (int i = 0; i < FLAGS_samples; i++) {
158 samples[i] = time(loops, bench, canvas, gl) / loops;
159 }
160 return loops;
161}
162#endif
163
164static SkString to_lower(const char* str) {
165 SkString lower(str);
166 for (size_t i = 0; i < lower.size(); i++) {
167 lower[i] = tolower(lower[i]);
168 }
169 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700170}
171
mtkleinbb6a0282014-07-01 08:43:42 -0700172struct Target {
173 const char* config;
174 Benchmark::Backend backend;
175 SkAutoTDelete<SkSurface> surface;
176#if SK_SUPPORT_GPU
177 SkGLContextHelper* gl;
178#endif
179};
mtkleinf3723212014-06-25 14:08:00 -0700180
mtkleinbb6a0282014-07-01 08:43:42 -0700181// If bench is enabled for backend/config, returns a Target* for them, otherwise NULL.
182static Target* is_enabled(Benchmark* bench, Benchmark::Backend backend, const char* config) {
183 if (!bench->isSuitableFor(backend)) {
184 return NULL;
mtkleinf3723212014-06-25 14:08:00 -0700185 }
186
mtkleinbb6a0282014-07-01 08:43:42 -0700187 for (int i = 0; i < FLAGS_config.count(); i++) {
188 if (to_lower(FLAGS_config[i]).equals(config)) {
189 Target* target = new Target;
190 target->config = config;
191 target->backend = backend;
192 return target;
mtkleinf3723212014-06-25 14:08:00 -0700193 }
194 }
mtkleinbb6a0282014-07-01 08:43:42 -0700195 return NULL;
196}
197
198// Append all targets that are suitable for bench.
199static void create_targets(Benchmark* bench, SkTDArray<Target*>* targets) {
200 const int w = bench->getSize().fX,
201 h = bench->getSize().fY;
202 const SkImageInfo _8888 = { w, h, kN32_SkColorType, kPremul_SkAlphaType },
203 _565 = { w, h, kRGB_565_SkColorType, kOpaque_SkAlphaType };
204
205 #define CPU_TARGET(config, backend, code) \
206 if (Target* t = is_enabled(bench, Benchmark::backend, #config)) { \
207 t->surface.reset(code); \
208 targets->push(t); \
209 }
mtklein40b32be2014-07-09 08:46:49 -0700210 if (FLAGS_cpu) {
211 CPU_TARGET(nonrendering, kNonRendering_Backend, NULL)
212 CPU_TARGET(8888, kRaster_Backend, SkSurface::NewRaster(_8888))
213 CPU_TARGET(565, kRaster_Backend, SkSurface::NewRaster(_565))
214 }
mtkleinbb6a0282014-07-01 08:43:42 -0700215
216#if SK_SUPPORT_GPU
217 #define GPU_TARGET(config, ctxType, info, samples) \
218 if (Target* t = is_enabled(bench, Benchmark::kGPU_Backend, #config)) { \
219 t->surface.reset(SkSurface::NewRenderTarget(gGrFactory.get(ctxType), info, samples)); \
220 t->gl = gGrFactory.getGLContext(ctxType); \
221 targets->push(t); \
222 }
mtklein40b32be2014-07-09 08:46:49 -0700223 if (FLAGS_gpu) {
224 GPU_TARGET(gpu, GrContextFactory::kNative_GLContextType, _8888, 0)
225 GPU_TARGET(msaa4, GrContextFactory::kNative_GLContextType, _8888, 4)
226 GPU_TARGET(msaa16, GrContextFactory::kNative_GLContextType, _8888, 16)
227 GPU_TARGET(nvprmsaa4, GrContextFactory::kNVPR_GLContextType, _8888, 4)
228 GPU_TARGET(nvprmsaa16, GrContextFactory::kNVPR_GLContextType, _8888, 16)
229 GPU_TARGET(debug, GrContextFactory::kDebug_GLContextType, _8888, 0)
230 GPU_TARGET(nullgpu, GrContextFactory::kNull_GLContextType, _8888, 0)
231 #if SK_ANGLE
232 GPU_TARGET(angle, GrContextFactory::kANGLE_GLContextType, _8888, 0)
233 #endif
234 }
mtkleinbb6a0282014-07-01 08:43:42 -0700235#endif
mtkleinf3723212014-06-25 14:08:00 -0700236}
237
mtklein60317d0f2014-07-14 11:30:37 -0700238static void fill_static_options(ResultsWriter* log) {
239#if defined(SK_BUILD_FOR_WIN32)
240 log->option("system", "WIN32");
241#elif defined(SK_BUILD_FOR_MAC)
242 log->option("system", "MAC");
243#elif defined(SK_BUILD_FOR_ANDROID)
244 log->option("system", "ANDROID");
245#elif defined(SK_BUILD_FOR_UNIX)
246 log->option("system", "UNIX");
247#else
248 log->option("system", "other");
249#endif
250#if defined(SK_DEBUG)
251 log->option("build", "DEBUG");
252#else
253 log->option("build", "RELEASE");
254#endif
255}
256
mtkleinf3723212014-06-25 14:08:00 -0700257int tool_main(int argc, char** argv);
258int tool_main(int argc, char** argv) {
259 SetupCrashHandler();
260 SkAutoGraphics ag;
261 SkCommandLineFlags::Parse(argc, argv);
262
mtkleina189ccd2014-07-14 12:28:47 -0700263 if (FLAGS_runOnce) {
264 FLAGS_samples = 1;
265 FLAGS_gpuFrameLag = 0;
266 }
267
mtklein60317d0f2014-07-14 11:30:37 -0700268 MultiResultsWriter log;
269 SkAutoTDelete<JSONResultsWriter> json;
270 if (!FLAGS_outResultsFile.isEmpty()) {
271 json.reset(SkNEW(JSONResultsWriter(FLAGS_outResultsFile[0])));
272 log.add(json.get());
273 }
274 CallEnd<MultiResultsWriter> ender(log);
275 fill_static_options(&log);
276
mtkleinf3723212014-06-25 14:08:00 -0700277 const double overhead = estimate_timer_overhead();
mtkleinbb6a0282014-07-01 08:43:42 -0700278 SkAutoTMalloc<double> samples(FLAGS_samples);
279
mtkleina189ccd2014-07-14 12:28:47 -0700280 if (FLAGS_runOnce) {
281 SkDebugf("--runOnce is true; times would only be misleading so we won't print them.\n");
282 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700283 // No header.
284 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700285 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700286 } else {
mtklein5d9d10e2014-07-11 11:57:07 -0700287 SkDebugf("loops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -0700288 }
289
290 for (const BenchRegistry* r = BenchRegistry::Head(); r != NULL; r = r->next()) {
291 SkAutoTDelete<Benchmark> bench(r->factory()(NULL));
292 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
293 continue;
294 }
mtklein60317d0f2014-07-14 11:30:37 -0700295 log.bench(bench->getName(), bench->getSize().fX, bench->getSize().fY);
mtkleinf3723212014-06-25 14:08:00 -0700296
mtkleinbb6a0282014-07-01 08:43:42 -0700297 SkTDArray<Target*> targets;
298 create_targets(bench.get(), &targets);
mtkleinf3723212014-06-25 14:08:00 -0700299
300 bench->preDraw();
mtkleinbb6a0282014-07-01 08:43:42 -0700301 for (int j = 0; j < targets.count(); j++) {
302 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
mtkleinf3723212014-06-25 14:08:00 -0700303
mtkleinbb6a0282014-07-01 08:43:42 -0700304 const int loops =
305#if SK_SUPPORT_GPU
306 Benchmark::kGPU_Backend == targets[j]->backend
307 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
308 :
309#endif
310 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700311
312 Stats stats(samples.get(), FLAGS_samples);
313
mtkleinbb6a0282014-07-01 08:43:42 -0700314 const char* config = targets[j]->config;
mtklein60317d0f2014-07-14 11:30:37 -0700315
316 log.config(config);
317 log.timer("min_ms", stats.min);
318 log.timer("median_ms", stats.median);
319 log.timer("mean_ms", stats.mean);
320 log.timer("max_ms", stats.max);
321 log.timer("stddev_ms", sqrt(stats.var));
322
mtkleina189ccd2014-07-14 12:28:47 -0700323 if (FLAGS_runOnce) {
324 if (targets.count() == 1) {
325 config = ""; // Only print the config if we run the same bench on more than one.
326 }
327 SkDebugf("%s\t%s\n", bench->getName(), config);
328 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700329 for (int i = 0; i < FLAGS_samples; i++) {
330 SkDebugf("%s ", humanize(samples[i]).c_str());
331 }
332 SkDebugf("%s\n", bench->getName());
333 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700334 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700335 config = ""; // Only print the config if we run the same bench on more than one.
336 }
mtklein40b32be2014-07-09 08:46:49 -0700337 SkDebugf("%s\t%s\t%s\n", humanize(stats.median).c_str(), bench->getName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700338 } else {
339 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtklein5d9d10e2014-07-11 11:57:07 -0700340 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 -0700341 , loops
342 , humanize(stats.min).c_str()
mtklein40b32be2014-07-09 08:46:49 -0700343 , humanize(stats.median).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700344 , humanize(stats.mean).c_str()
345 , humanize(stats.max).c_str()
346 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700347 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700348 , config
mtkleinbb6a0282014-07-01 08:43:42 -0700349 , bench->getName()
mtkleinf3723212014-06-25 14:08:00 -0700350 );
351 }
352 }
mtkleinbb6a0282014-07-01 08:43:42 -0700353 targets.deleteAll();
mtkleinf3723212014-06-25 14:08:00 -0700354 }
355
356 return 0;
357}
358
359#if !defined SK_BUILD_FOR_IOS
360int main(int argc, char * const argv[]) {
361 return tool_main(argc, (char**) argv);
362}
363#endif