blob: 71a412d19df3db9f19b608c0a25a5c44881c5b64 [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"
12#include "Stats.h"
13#include "Timer.h"
14
15#include "SkCanvas.h"
16#include "SkCommandLineFlags.h"
17#include "SkForceLinking.h"
18#include "SkGraphics.h"
19#include "SkString.h"
20#include "SkSurface.h"
21
mtkleinbb6a0282014-07-01 08:43:42 -070022#if SK_SUPPORT_GPU
23 #include "GrContextFactory.h"
24 GrContextFactory gGrFactory;
25#endif
26
mtkleinf3723212014-06-25 14:08:00 -070027__SK_FORCE_IMAGE_DECODER_LINKING;
28
29DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
30DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
31DEFINE_double(overheadGoal, 0.0001,
32 "Loop until timer overhead is at most this fraction of our measurments.");
33DEFINE_string(match, "", "The usual filters on file names of benchmarks to measure.");
34DEFINE_bool2(quiet, q, false, "Print only bench name and minimum sample.");
35DEFINE_bool2(verbose, v, false, "Print all samples.");
mtkleinbb6a0282014-07-01 08:43:42 -070036DEFINE_string(config, "nonrendering 8888 gpu", "Configs to measure. Options: "
37 "565 8888 gpu nonrendering debug nullgpu msaa4 msaa16 nvprmsaa4 nvprmsaa16 angle");
38DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
39DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -070040
mtklein40b32be2014-07-09 08:46:49 -070041DEFINE_bool(cpu, true, "Master switch for CPU-bound work.");
42DEFINE_bool(gpu, true, "Master switch for GPU-bound work.");
43
mtkleinf3723212014-06-25 14:08:00 -070044
45static SkString humanize(double ms) {
46 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3);
47 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6);
48 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3);
49 return SkStringPrintf("%.3gms", ms);
50}
51
mtkleinbb6a0282014-07-01 08:43:42 -070052static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHelper* gl) {
53 WallTimer timer;
54 timer.start();
55 if (bench) {
56 bench->draw(loops, canvas);
57 }
58 if (canvas) {
59 canvas->flush();
60 }
61#if SK_SUPPORT_GPU
62 if (gl) {
63 SK_GL(*gl, Flush());
64 gl->swapBuffers();
65 }
66#endif
67 timer.end();
68 return timer.fWall;
69}
70
mtkleinf3723212014-06-25 14:08:00 -070071static double estimate_timer_overhead() {
72 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -070073 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -070074 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -070075 }
76 return overhead / FLAGS_overheadLoops;
77}
78
mtkleinbb6a0282014-07-01 08:43:42 -070079static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
80 // First figure out approximately how many loops of bench it takes to make overhead negligible.
81 double bench_plus_overhead;
mtkleinf3723212014-06-25 14:08:00 -070082 do {
mtkleinbb6a0282014-07-01 08:43:42 -070083 bench_plus_overhead = time(1, bench, canvas, NULL);
84 } while (bench_plus_overhead < overhead); // Shouldn't normally happen.
mtkleinf3723212014-06-25 14:08:00 -070085
mtkleinbb6a0282014-07-01 08:43:42 -070086 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -070087 // We'll pick N to make timer overhead negligible:
88 //
mtkleinbb6a0282014-07-01 08:43:42 -070089 // overhead
90 // ------------------------- < FLAGS_overheadGoal
91 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -070092 //
mtkleinbb6a0282014-07-01 08:43:42 -070093 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -070094 //
95 // Doing some math, we get:
96 //
mtkleinbb6a0282014-07-01 08:43:42 -070097 // (overhead / FLAGS_overheadGoal) - overhead
98 // ------------------------------------------ < N
99 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700100 //
101 // Luckily, this also works well in practice. :)
102 const double numer = overhead / FLAGS_overheadGoal - overhead;
mtkleinbb6a0282014-07-01 08:43:42 -0700103 const double denom = bench_plus_overhead - overhead;
104 const int loops = (int)ceil(numer / denom);
105
106 for (int i = 0; i < FLAGS_samples; i++) {
107 samples[i] = time(loops, bench, canvas, NULL) / loops;
108 }
109 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700110}
111
mtkleinbb6a0282014-07-01 08:43:42 -0700112#if SK_SUPPORT_GPU
113static int gpu_bench(SkGLContextHelper* gl,
114 Benchmark* bench,
115 SkCanvas* canvas,
116 double* samples) {
117 // Make sure we're done with whatever came before.
mtklein9bc86ed2014-07-01 10:02:42 -0700118 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700119
120 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
121 int loops = 1;
122 double elapsed = 0;
123 do {
124 loops *= 2;
125 // If the GPU lets frames lag at all, we need to make sure we're timing
126 // _this_ round, not still timing last round. We force this by looping
127 // more times than any reasonable GPU will allow frames to lag.
128 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
129 elapsed = time(loops, bench, canvas, gl);
130 }
131 } while (elapsed < FLAGS_gpuMs);
132
133 // We've overshot at least a little. Scale back linearly.
134 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
135
136 // Might as well make sure we're not still timing our calibration.
mtklein9bc86ed2014-07-01 10:02:42 -0700137 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700138
139 // Pretty much the same deal as the calibration: do some warmup to make
140 // sure we're timing steady-state pipelined frames.
141 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
142 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700143 }
mtkleinbb6a0282014-07-01 08:43:42 -0700144
145 // Now, actually do the timing!
146 for (int i = 0; i < FLAGS_samples; i++) {
147 samples[i] = time(loops, bench, canvas, gl) / loops;
148 }
149 return loops;
150}
151#endif
152
153static SkString to_lower(const char* str) {
154 SkString lower(str);
155 for (size_t i = 0; i < lower.size(); i++) {
156 lower[i] = tolower(lower[i]);
157 }
158 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700159}
160
mtkleinbb6a0282014-07-01 08:43:42 -0700161struct Target {
162 const char* config;
163 Benchmark::Backend backend;
164 SkAutoTDelete<SkSurface> surface;
165#if SK_SUPPORT_GPU
166 SkGLContextHelper* gl;
167#endif
168};
mtkleinf3723212014-06-25 14:08:00 -0700169
mtkleinbb6a0282014-07-01 08:43:42 -0700170// If bench is enabled for backend/config, returns a Target* for them, otherwise NULL.
171static Target* is_enabled(Benchmark* bench, Benchmark::Backend backend, const char* config) {
172 if (!bench->isSuitableFor(backend)) {
173 return NULL;
mtkleinf3723212014-06-25 14:08:00 -0700174 }
175
mtkleinbb6a0282014-07-01 08:43:42 -0700176 for (int i = 0; i < FLAGS_config.count(); i++) {
177 if (to_lower(FLAGS_config[i]).equals(config)) {
178 Target* target = new Target;
179 target->config = config;
180 target->backend = backend;
181 return target;
mtkleinf3723212014-06-25 14:08:00 -0700182 }
183 }
mtkleinbb6a0282014-07-01 08:43:42 -0700184 return NULL;
185}
186
187// Append all targets that are suitable for bench.
188static void create_targets(Benchmark* bench, SkTDArray<Target*>* targets) {
189 const int w = bench->getSize().fX,
190 h = bench->getSize().fY;
191 const SkImageInfo _8888 = { w, h, kN32_SkColorType, kPremul_SkAlphaType },
192 _565 = { w, h, kRGB_565_SkColorType, kOpaque_SkAlphaType };
193
194 #define CPU_TARGET(config, backend, code) \
195 if (Target* t = is_enabled(bench, Benchmark::backend, #config)) { \
196 t->surface.reset(code); \
197 targets->push(t); \
198 }
mtklein40b32be2014-07-09 08:46:49 -0700199 if (FLAGS_cpu) {
200 CPU_TARGET(nonrendering, kNonRendering_Backend, NULL)
201 CPU_TARGET(8888, kRaster_Backend, SkSurface::NewRaster(_8888))
202 CPU_TARGET(565, kRaster_Backend, SkSurface::NewRaster(_565))
203 }
mtkleinbb6a0282014-07-01 08:43:42 -0700204
205#if SK_SUPPORT_GPU
206 #define GPU_TARGET(config, ctxType, info, samples) \
207 if (Target* t = is_enabled(bench, Benchmark::kGPU_Backend, #config)) { \
208 t->surface.reset(SkSurface::NewRenderTarget(gGrFactory.get(ctxType), info, samples)); \
209 t->gl = gGrFactory.getGLContext(ctxType); \
210 targets->push(t); \
211 }
mtklein40b32be2014-07-09 08:46:49 -0700212 if (FLAGS_gpu) {
213 GPU_TARGET(gpu, GrContextFactory::kNative_GLContextType, _8888, 0)
214 GPU_TARGET(msaa4, GrContextFactory::kNative_GLContextType, _8888, 4)
215 GPU_TARGET(msaa16, GrContextFactory::kNative_GLContextType, _8888, 16)
216 GPU_TARGET(nvprmsaa4, GrContextFactory::kNVPR_GLContextType, _8888, 4)
217 GPU_TARGET(nvprmsaa16, GrContextFactory::kNVPR_GLContextType, _8888, 16)
218 GPU_TARGET(debug, GrContextFactory::kDebug_GLContextType, _8888, 0)
219 GPU_TARGET(nullgpu, GrContextFactory::kNull_GLContextType, _8888, 0)
220 #if SK_ANGLE
221 GPU_TARGET(angle, GrContextFactory::kANGLE_GLContextType, _8888, 0)
222 #endif
223 }
mtkleinbb6a0282014-07-01 08:43:42 -0700224#endif
mtkleinf3723212014-06-25 14:08:00 -0700225}
226
227int tool_main(int argc, char** argv);
228int tool_main(int argc, char** argv) {
229 SetupCrashHandler();
230 SkAutoGraphics ag;
231 SkCommandLineFlags::Parse(argc, argv);
232
233 const double overhead = estimate_timer_overhead();
mtkleinbb6a0282014-07-01 08:43:42 -0700234 SkAutoTMalloc<double> samples(FLAGS_samples);
235
mtkleinf3723212014-06-25 14:08:00 -0700236 if (FLAGS_verbose) {
237 // No header.
238 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700239 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700240 } else {
mtklein40b32be2014-07-09 08:46:49 -0700241 SkDebugf("loops\tmin\tmedian\tmean\tmax\tstddev\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -0700242 }
243
244 for (const BenchRegistry* r = BenchRegistry::Head(); r != NULL; r = r->next()) {
245 SkAutoTDelete<Benchmark> bench(r->factory()(NULL));
246 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
247 continue;
248 }
249
mtkleinbb6a0282014-07-01 08:43:42 -0700250 SkTDArray<Target*> targets;
251 create_targets(bench.get(), &targets);
mtkleinf3723212014-06-25 14:08:00 -0700252
253 bench->preDraw();
mtkleinbb6a0282014-07-01 08:43:42 -0700254 for (int j = 0; j < targets.count(); j++) {
255 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
mtkleinf3723212014-06-25 14:08:00 -0700256
mtkleinbb6a0282014-07-01 08:43:42 -0700257 const int loops =
258#if SK_SUPPORT_GPU
259 Benchmark::kGPU_Backend == targets[j]->backend
260 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
261 :
262#endif
263 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700264
265 Stats stats(samples.get(), FLAGS_samples);
266
mtkleinbb6a0282014-07-01 08:43:42 -0700267 const char* config = targets[j]->config;
mtkleinf3723212014-06-25 14:08:00 -0700268 if (FLAGS_verbose) {
269 for (int i = 0; i < FLAGS_samples; i++) {
270 SkDebugf("%s ", humanize(samples[i]).c_str());
271 }
272 SkDebugf("%s\n", bench->getName());
273 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700274 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700275 config = ""; // Only print the config if we run the same bench on more than one.
276 }
mtklein40b32be2014-07-09 08:46:49 -0700277 SkDebugf("%s\t%s\t%s\n", humanize(stats.median).c_str(), bench->getName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700278 } else {
279 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtklein40b32be2014-07-09 08:46:49 -0700280 SkDebugf("%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\n"
mtkleinf3723212014-06-25 14:08:00 -0700281 , loops
282 , humanize(stats.min).c_str()
mtklein40b32be2014-07-09 08:46:49 -0700283 , humanize(stats.median).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700284 , humanize(stats.mean).c_str()
285 , humanize(stats.max).c_str()
286 , stddev_percent
mtkleinf3723212014-06-25 14:08:00 -0700287 , config
mtkleinbb6a0282014-07-01 08:43:42 -0700288 , bench->getName()
mtkleinf3723212014-06-25 14:08:00 -0700289 );
290 }
291 }
mtkleinbb6a0282014-07-01 08:43:42 -0700292 targets.deleteAll();
mtkleinf3723212014-06-25 14:08:00 -0700293 }
294
295 return 0;
296}
297
298#if !defined SK_BUILD_FOR_IOS
299int main(int argc, char * const argv[]) {
300 return tool_main(argc, (char**) argv);
301}
302#endif