blob: cc9896ea1bb6e01ea68537e9e4d019971395f572 [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
30DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
31DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
32DEFINE_double(overheadGoal, 0.0001,
33 "Loop until timer overhead is at most this fraction of our measurments.");
34DEFINE_string(match, "", "The usual filters on file names of benchmarks to measure.");
35DEFINE_bool2(quiet, q, false, "Print only bench name and minimum sample.");
36DEFINE_bool2(verbose, v, false, "Print all samples.");
mtkleinbb6a0282014-07-01 08:43:42 -070037DEFINE_string(config, "nonrendering 8888 gpu", "Configs to measure. Options: "
38 "565 8888 gpu nonrendering debug nullgpu msaa4 msaa16 nvprmsaa4 nvprmsaa16 angle");
39DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
40DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -070041
mtklein40b32be2014-07-09 08:46:49 -070042DEFINE_bool(cpu, true, "Master switch for CPU-bound work.");
43DEFINE_bool(gpu, true, "Master switch for GPU-bound work.");
44
mtklein60317d0f2014-07-14 11:30:37 -070045DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
46
mtkleinf3723212014-06-25 14:08:00 -070047
48static SkString humanize(double ms) {
49 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3);
50 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6);
51 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3);
52 return SkStringPrintf("%.3gms", ms);
53}
54
mtkleinbb6a0282014-07-01 08:43:42 -070055static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHelper* gl) {
56 WallTimer timer;
57 timer.start();
58 if (bench) {
59 bench->draw(loops, canvas);
60 }
61 if (canvas) {
62 canvas->flush();
63 }
64#if SK_SUPPORT_GPU
65 if (gl) {
66 SK_GL(*gl, Flush());
67 gl->swapBuffers();
68 }
69#endif
70 timer.end();
71 return timer.fWall;
72}
73
mtkleinf3723212014-06-25 14:08:00 -070074static double estimate_timer_overhead() {
75 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -070076 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -070077 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -070078 }
79 return overhead / FLAGS_overheadLoops;
80}
81
mtkleinbb6a0282014-07-01 08:43:42 -070082static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
83 // First figure out approximately how many loops of bench it takes to make overhead negligible.
84 double bench_plus_overhead;
mtkleinf3723212014-06-25 14:08:00 -070085 do {
mtkleinbb6a0282014-07-01 08:43:42 -070086 bench_plus_overhead = time(1, bench, canvas, NULL);
87 } while (bench_plus_overhead < overhead); // Shouldn't normally happen.
mtkleinf3723212014-06-25 14:08:00 -070088
mtkleinbb6a0282014-07-01 08:43:42 -070089 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -070090 // We'll pick N to make timer overhead negligible:
91 //
mtkleinbb6a0282014-07-01 08:43:42 -070092 // overhead
93 // ------------------------- < FLAGS_overheadGoal
94 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -070095 //
mtkleinbb6a0282014-07-01 08:43:42 -070096 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -070097 //
98 // Doing some math, we get:
99 //
mtkleinbb6a0282014-07-01 08:43:42 -0700100 // (overhead / FLAGS_overheadGoal) - overhead
101 // ------------------------------------------ < N
102 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700103 //
104 // Luckily, this also works well in practice. :)
105 const double numer = overhead / FLAGS_overheadGoal - overhead;
mtkleinbb6a0282014-07-01 08:43:42 -0700106 const double denom = bench_plus_overhead - overhead;
107 const int loops = (int)ceil(numer / denom);
108
109 for (int i = 0; i < FLAGS_samples; i++) {
110 samples[i] = time(loops, bench, canvas, NULL) / loops;
111 }
112 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700113}
114
mtkleinbb6a0282014-07-01 08:43:42 -0700115#if SK_SUPPORT_GPU
116static int gpu_bench(SkGLContextHelper* gl,
117 Benchmark* bench,
118 SkCanvas* canvas,
119 double* samples) {
120 // Make sure we're done with whatever came before.
mtklein9bc86ed2014-07-01 10:02:42 -0700121 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700122
123 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
124 int loops = 1;
125 double elapsed = 0;
126 do {
127 loops *= 2;
128 // If the GPU lets frames lag at all, we need to make sure we're timing
129 // _this_ round, not still timing last round. We force this by looping
130 // more times than any reasonable GPU will allow frames to lag.
131 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
132 elapsed = time(loops, bench, canvas, gl);
133 }
134 } while (elapsed < FLAGS_gpuMs);
135
136 // We've overshot at least a little. Scale back linearly.
137 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
138
139 // Might as well make sure we're not still timing our calibration.
mtklein9bc86ed2014-07-01 10:02:42 -0700140 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700141
142 // Pretty much the same deal as the calibration: do some warmup to make
143 // sure we're timing steady-state pipelined frames.
144 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
145 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700146 }
mtkleinbb6a0282014-07-01 08:43:42 -0700147
148 // Now, actually do the timing!
149 for (int i = 0; i < FLAGS_samples; i++) {
150 samples[i] = time(loops, bench, canvas, gl) / loops;
151 }
152 return loops;
153}
154#endif
155
156static SkString to_lower(const char* str) {
157 SkString lower(str);
158 for (size_t i = 0; i < lower.size(); i++) {
159 lower[i] = tolower(lower[i]);
160 }
161 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700162}
163
mtkleinbb6a0282014-07-01 08:43:42 -0700164struct Target {
165 const char* config;
166 Benchmark::Backend backend;
167 SkAutoTDelete<SkSurface> surface;
168#if SK_SUPPORT_GPU
169 SkGLContextHelper* gl;
170#endif
171};
mtkleinf3723212014-06-25 14:08:00 -0700172
mtkleinbb6a0282014-07-01 08:43:42 -0700173// If bench is enabled for backend/config, returns a Target* for them, otherwise NULL.
174static Target* is_enabled(Benchmark* bench, Benchmark::Backend backend, const char* config) {
175 if (!bench->isSuitableFor(backend)) {
176 return NULL;
mtkleinf3723212014-06-25 14:08:00 -0700177 }
178
mtkleinbb6a0282014-07-01 08:43:42 -0700179 for (int i = 0; i < FLAGS_config.count(); i++) {
180 if (to_lower(FLAGS_config[i]).equals(config)) {
181 Target* target = new Target;
182 target->config = config;
183 target->backend = backend;
184 return target;
mtkleinf3723212014-06-25 14:08:00 -0700185 }
186 }
mtkleinbb6a0282014-07-01 08:43:42 -0700187 return NULL;
188}
189
190// Append all targets that are suitable for bench.
191static void create_targets(Benchmark* bench, SkTDArray<Target*>* targets) {
192 const int w = bench->getSize().fX,
193 h = bench->getSize().fY;
194 const SkImageInfo _8888 = { w, h, kN32_SkColorType, kPremul_SkAlphaType },
195 _565 = { w, h, kRGB_565_SkColorType, kOpaque_SkAlphaType };
196
197 #define CPU_TARGET(config, backend, code) \
198 if (Target* t = is_enabled(bench, Benchmark::backend, #config)) { \
199 t->surface.reset(code); \
200 targets->push(t); \
201 }
mtklein40b32be2014-07-09 08:46:49 -0700202 if (FLAGS_cpu) {
203 CPU_TARGET(nonrendering, kNonRendering_Backend, NULL)
204 CPU_TARGET(8888, kRaster_Backend, SkSurface::NewRaster(_8888))
205 CPU_TARGET(565, kRaster_Backend, SkSurface::NewRaster(_565))
206 }
mtkleinbb6a0282014-07-01 08:43:42 -0700207
208#if SK_SUPPORT_GPU
209 #define GPU_TARGET(config, ctxType, info, samples) \
210 if (Target* t = is_enabled(bench, Benchmark::kGPU_Backend, #config)) { \
211 t->surface.reset(SkSurface::NewRenderTarget(gGrFactory.get(ctxType), info, samples)); \
212 t->gl = gGrFactory.getGLContext(ctxType); \
213 targets->push(t); \
214 }
mtklein40b32be2014-07-09 08:46:49 -0700215 if (FLAGS_gpu) {
216 GPU_TARGET(gpu, GrContextFactory::kNative_GLContextType, _8888, 0)
217 GPU_TARGET(msaa4, GrContextFactory::kNative_GLContextType, _8888, 4)
218 GPU_TARGET(msaa16, GrContextFactory::kNative_GLContextType, _8888, 16)
219 GPU_TARGET(nvprmsaa4, GrContextFactory::kNVPR_GLContextType, _8888, 4)
220 GPU_TARGET(nvprmsaa16, GrContextFactory::kNVPR_GLContextType, _8888, 16)
221 GPU_TARGET(debug, GrContextFactory::kDebug_GLContextType, _8888, 0)
222 GPU_TARGET(nullgpu, GrContextFactory::kNull_GLContextType, _8888, 0)
223 #if SK_ANGLE
224 GPU_TARGET(angle, GrContextFactory::kANGLE_GLContextType, _8888, 0)
225 #endif
226 }
mtkleinbb6a0282014-07-01 08:43:42 -0700227#endif
mtkleinf3723212014-06-25 14:08:00 -0700228}
229
mtklein60317d0f2014-07-14 11:30:37 -0700230static void fill_static_options(ResultsWriter* log) {
231#if defined(SK_BUILD_FOR_WIN32)
232 log->option("system", "WIN32");
233#elif defined(SK_BUILD_FOR_MAC)
234 log->option("system", "MAC");
235#elif defined(SK_BUILD_FOR_ANDROID)
236 log->option("system", "ANDROID");
237#elif defined(SK_BUILD_FOR_UNIX)
238 log->option("system", "UNIX");
239#else
240 log->option("system", "other");
241#endif
242#if defined(SK_DEBUG)
243 log->option("build", "DEBUG");
244#else
245 log->option("build", "RELEASE");
246#endif
247}
248
mtkleinf3723212014-06-25 14:08:00 -0700249int tool_main(int argc, char** argv);
250int tool_main(int argc, char** argv) {
251 SetupCrashHandler();
252 SkAutoGraphics ag;
253 SkCommandLineFlags::Parse(argc, argv);
254
mtklein60317d0f2014-07-14 11:30:37 -0700255 MultiResultsWriter log;
256 SkAutoTDelete<JSONResultsWriter> json;
257 if (!FLAGS_outResultsFile.isEmpty()) {
258 json.reset(SkNEW(JSONResultsWriter(FLAGS_outResultsFile[0])));
259 log.add(json.get());
260 }
261 CallEnd<MultiResultsWriter> ender(log);
262 fill_static_options(&log);
263
mtkleinf3723212014-06-25 14:08:00 -0700264 const double overhead = estimate_timer_overhead();
mtkleinbb6a0282014-07-01 08:43:42 -0700265 SkAutoTMalloc<double> samples(FLAGS_samples);
266
mtkleinf3723212014-06-25 14:08:00 -0700267 if (FLAGS_verbose) {
268 // No header.
269 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700270 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700271 } else {
mtklein5d9d10e2014-07-11 11:57:07 -0700272 SkDebugf("loops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -0700273 }
274
275 for (const BenchRegistry* r = BenchRegistry::Head(); r != NULL; r = r->next()) {
276 SkAutoTDelete<Benchmark> bench(r->factory()(NULL));
277 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
278 continue;
279 }
mtklein60317d0f2014-07-14 11:30:37 -0700280 log.bench(bench->getName(), bench->getSize().fX, bench->getSize().fY);
mtkleinf3723212014-06-25 14:08:00 -0700281
mtkleinbb6a0282014-07-01 08:43:42 -0700282 SkTDArray<Target*> targets;
283 create_targets(bench.get(), &targets);
mtkleinf3723212014-06-25 14:08:00 -0700284
285 bench->preDraw();
mtkleinbb6a0282014-07-01 08:43:42 -0700286 for (int j = 0; j < targets.count(); j++) {
287 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
mtkleinf3723212014-06-25 14:08:00 -0700288
mtkleinbb6a0282014-07-01 08:43:42 -0700289 const int loops =
290#if SK_SUPPORT_GPU
291 Benchmark::kGPU_Backend == targets[j]->backend
292 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
293 :
294#endif
295 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700296
297 Stats stats(samples.get(), FLAGS_samples);
298
mtkleinbb6a0282014-07-01 08:43:42 -0700299 const char* config = targets[j]->config;
mtklein60317d0f2014-07-14 11:30:37 -0700300
301 log.config(config);
302 log.timer("min_ms", stats.min);
303 log.timer("median_ms", stats.median);
304 log.timer("mean_ms", stats.mean);
305 log.timer("max_ms", stats.max);
306 log.timer("stddev_ms", sqrt(stats.var));
307
mtkleinf3723212014-06-25 14:08:00 -0700308 if (FLAGS_verbose) {
309 for (int i = 0; i < FLAGS_samples; i++) {
310 SkDebugf("%s ", humanize(samples[i]).c_str());
311 }
312 SkDebugf("%s\n", bench->getName());
313 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700314 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700315 config = ""; // Only print the config if we run the same bench on more than one.
316 }
mtklein40b32be2014-07-09 08:46:49 -0700317 SkDebugf("%s\t%s\t%s\n", humanize(stats.median).c_str(), bench->getName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700318 } else {
319 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtklein5d9d10e2014-07-11 11:57:07 -0700320 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 -0700321 , loops
322 , humanize(stats.min).c_str()
mtklein40b32be2014-07-09 08:46:49 -0700323 , humanize(stats.median).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700324 , humanize(stats.mean).c_str()
325 , humanize(stats.max).c_str()
326 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700327 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700328 , config
mtkleinbb6a0282014-07-01 08:43:42 -0700329 , bench->getName()
mtkleinf3723212014-06-25 14:08:00 -0700330 );
331 }
332 }
mtkleinbb6a0282014-07-01 08:43:42 -0700333 targets.deleteAll();
mtkleinf3723212014-06-25 14:08:00 -0700334 }
335
336 return 0;
337}
338
339#if !defined SK_BUILD_FOR_IOS
340int main(int argc, char * const argv[]) {
341 return tool_main(argc, (char**) argv);
342}
343#endif