blob: a3dbf22306105e9754df5fec0ed6c3cdcef4429f [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
mtkleinf3723212014-06-25 14:08:00 -070041
42static SkString humanize(double ms) {
43 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3);
44 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6);
45 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3);
46 return SkStringPrintf("%.3gms", ms);
47}
48
mtkleinbb6a0282014-07-01 08:43:42 -070049static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHelper* gl) {
50 WallTimer timer;
51 timer.start();
52 if (bench) {
53 bench->draw(loops, canvas);
54 }
55 if (canvas) {
56 canvas->flush();
57 }
58#if SK_SUPPORT_GPU
59 if (gl) {
60 SK_GL(*gl, Flush());
61 gl->swapBuffers();
62 }
63#endif
64 timer.end();
65 return timer.fWall;
66}
67
mtkleinf3723212014-06-25 14:08:00 -070068static double estimate_timer_overhead() {
69 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -070070 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -070071 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -070072 }
73 return overhead / FLAGS_overheadLoops;
74}
75
mtkleinbb6a0282014-07-01 08:43:42 -070076static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
77 // First figure out approximately how many loops of bench it takes to make overhead negligible.
78 double bench_plus_overhead;
mtkleinf3723212014-06-25 14:08:00 -070079 do {
mtkleinbb6a0282014-07-01 08:43:42 -070080 bench_plus_overhead = time(1, bench, canvas, NULL);
81 } while (bench_plus_overhead < overhead); // Shouldn't normally happen.
mtkleinf3723212014-06-25 14:08:00 -070082
mtkleinbb6a0282014-07-01 08:43:42 -070083 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -070084 // We'll pick N to make timer overhead negligible:
85 //
mtkleinbb6a0282014-07-01 08:43:42 -070086 // overhead
87 // ------------------------- < FLAGS_overheadGoal
88 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -070089 //
mtkleinbb6a0282014-07-01 08:43:42 -070090 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -070091 //
92 // Doing some math, we get:
93 //
mtkleinbb6a0282014-07-01 08:43:42 -070094 // (overhead / FLAGS_overheadGoal) - overhead
95 // ------------------------------------------ < N
96 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -070097 //
98 // Luckily, this also works well in practice. :)
99 const double numer = overhead / FLAGS_overheadGoal - overhead;
mtkleinbb6a0282014-07-01 08:43:42 -0700100 const double denom = bench_plus_overhead - overhead;
101 const int loops = (int)ceil(numer / denom);
102
103 for (int i = 0; i < FLAGS_samples; i++) {
104 samples[i] = time(loops, bench, canvas, NULL) / loops;
105 }
106 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700107}
108
mtkleinbb6a0282014-07-01 08:43:42 -0700109#if SK_SUPPORT_GPU
110static int gpu_bench(SkGLContextHelper* gl,
111 Benchmark* bench,
112 SkCanvas* canvas,
113 double* samples) {
114 // Make sure we're done with whatever came before.
115 SK_GL(*gl, Finish);
116
117 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
118 int loops = 1;
119 double elapsed = 0;
120 do {
121 loops *= 2;
122 // If the GPU lets frames lag at all, we need to make sure we're timing
123 // _this_ round, not still timing last round. We force this by looping
124 // more times than any reasonable GPU will allow frames to lag.
125 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
126 elapsed = time(loops, bench, canvas, gl);
127 }
128 } while (elapsed < FLAGS_gpuMs);
129
130 // We've overshot at least a little. Scale back linearly.
131 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
132
133 // Might as well make sure we're not still timing our calibration.
134 SK_GL(*gl, Finish);
135
136 // Pretty much the same deal as the calibration: do some warmup to make
137 // sure we're timing steady-state pipelined frames.
138 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
139 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700140 }
mtkleinbb6a0282014-07-01 08:43:42 -0700141
142 // Now, actually do the timing!
143 for (int i = 0; i < FLAGS_samples; i++) {
144 samples[i] = time(loops, bench, canvas, gl) / loops;
145 }
146 return loops;
147}
148#endif
149
150static SkString to_lower(const char* str) {
151 SkString lower(str);
152 for (size_t i = 0; i < lower.size(); i++) {
153 lower[i] = tolower(lower[i]);
154 }
155 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700156}
157
mtkleinbb6a0282014-07-01 08:43:42 -0700158struct Target {
159 const char* config;
160 Benchmark::Backend backend;
161 SkAutoTDelete<SkSurface> surface;
162#if SK_SUPPORT_GPU
163 SkGLContextHelper* gl;
164#endif
165};
mtkleinf3723212014-06-25 14:08:00 -0700166
mtkleinbb6a0282014-07-01 08:43:42 -0700167// If bench is enabled for backend/config, returns a Target* for them, otherwise NULL.
168static Target* is_enabled(Benchmark* bench, Benchmark::Backend backend, const char* config) {
169 if (!bench->isSuitableFor(backend)) {
170 return NULL;
mtkleinf3723212014-06-25 14:08:00 -0700171 }
172
mtkleinbb6a0282014-07-01 08:43:42 -0700173 for (int i = 0; i < FLAGS_config.count(); i++) {
174 if (to_lower(FLAGS_config[i]).equals(config)) {
175 Target* target = new Target;
176 target->config = config;
177 target->backend = backend;
178 return target;
mtkleinf3723212014-06-25 14:08:00 -0700179 }
180 }
mtkleinbb6a0282014-07-01 08:43:42 -0700181 return NULL;
182}
183
184// Append all targets that are suitable for bench.
185static void create_targets(Benchmark* bench, SkTDArray<Target*>* targets) {
186 const int w = bench->getSize().fX,
187 h = bench->getSize().fY;
188 const SkImageInfo _8888 = { w, h, kN32_SkColorType, kPremul_SkAlphaType },
189 _565 = { w, h, kRGB_565_SkColorType, kOpaque_SkAlphaType };
190
191 #define CPU_TARGET(config, backend, code) \
192 if (Target* t = is_enabled(bench, Benchmark::backend, #config)) { \
193 t->surface.reset(code); \
194 targets->push(t); \
195 }
196 CPU_TARGET(nonrendering, kNonRendering_Backend, NULL)
197 CPU_TARGET(8888, kRaster_Backend, SkSurface::NewRaster(_8888))
198 CPU_TARGET(565, kRaster_Backend, SkSurface::NewRaster(_565))
199
200#if SK_SUPPORT_GPU
201 #define GPU_TARGET(config, ctxType, info, samples) \
202 if (Target* t = is_enabled(bench, Benchmark::kGPU_Backend, #config)) { \
203 t->surface.reset(SkSurface::NewRenderTarget(gGrFactory.get(ctxType), info, samples)); \
204 t->gl = gGrFactory.getGLContext(ctxType); \
205 targets->push(t); \
206 }
207 GPU_TARGET(gpu, GrContextFactory::kNative_GLContextType, _8888, 0)
208 GPU_TARGET(msaa4, GrContextFactory::kNative_GLContextType, _8888, 4)
209 GPU_TARGET(msaa16, GrContextFactory::kNative_GLContextType, _8888, 16)
210 GPU_TARGET(nvprmsaa4, GrContextFactory::kNVPR_GLContextType, _8888, 4)
211 GPU_TARGET(nvprmsaa16, GrContextFactory::kNVPR_GLContextType, _8888, 16)
212 GPU_TARGET(debug, GrContextFactory::kDebug_GLContextType, _8888, 0)
213 GPU_TARGET(nullgpu, GrContextFactory::kNull_GLContextType, _8888, 0)
214 #if SK_ANGLE
215 GPU_TARGET(angle, GrContextFactory::kANGLE_GLContextType, _8888, 0)
216 #endif
217#endif
mtkleinf3723212014-06-25 14:08:00 -0700218}
219
220int tool_main(int argc, char** argv);
221int tool_main(int argc, char** argv) {
222 SetupCrashHandler();
223 SkAutoGraphics ag;
224 SkCommandLineFlags::Parse(argc, argv);
225
226 const double overhead = estimate_timer_overhead();
mtkleinbb6a0282014-07-01 08:43:42 -0700227 SkAutoTMalloc<double> samples(FLAGS_samples);
228
229 // TODO: display add median, use it in --quiet mode
mtkleinf3723212014-06-25 14:08:00 -0700230
231 if (FLAGS_verbose) {
232 // No header.
233 } else if (FLAGS_quiet) {
234 SkDebugf("min\tbench\tconfig\n");
235 } else {
mtkleinbb6a0282014-07-01 08:43:42 -0700236 SkDebugf("loops\tmin\tmean\tmax\tstddev\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -0700237 }
238
239 for (const BenchRegistry* r = BenchRegistry::Head(); r != NULL; r = r->next()) {
240 SkAutoTDelete<Benchmark> bench(r->factory()(NULL));
241 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
242 continue;
243 }
244
mtkleinbb6a0282014-07-01 08:43:42 -0700245 SkTDArray<Target*> targets;
246 create_targets(bench.get(), &targets);
mtkleinf3723212014-06-25 14:08:00 -0700247
248 bench->preDraw();
mtkleinbb6a0282014-07-01 08:43:42 -0700249 for (int j = 0; j < targets.count(); j++) {
250 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
mtkleinf3723212014-06-25 14:08:00 -0700251
mtkleinbb6a0282014-07-01 08:43:42 -0700252 const int loops =
253#if SK_SUPPORT_GPU
254 Benchmark::kGPU_Backend == targets[j]->backend
255 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
256 :
257#endif
258 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700259
260 Stats stats(samples.get(), FLAGS_samples);
261
mtkleinbb6a0282014-07-01 08:43:42 -0700262 const char* config = targets[j]->config;
mtkleinf3723212014-06-25 14:08:00 -0700263 if (FLAGS_verbose) {
264 for (int i = 0; i < FLAGS_samples; i++) {
265 SkDebugf("%s ", humanize(samples[i]).c_str());
266 }
267 SkDebugf("%s\n", bench->getName());
268 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700269 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700270 config = ""; // Only print the config if we run the same bench on more than one.
271 }
272 SkDebugf("%s\t%s\t%s\n", humanize(stats.min).c_str(), bench->getName(), config);
273 } else {
274 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
275 SkDebugf("%d\t%s\t%s\t%s\t%.0f%%\t%s\t%s\n"
276 , loops
277 , humanize(stats.min).c_str()
278 , humanize(stats.mean).c_str()
279 , humanize(stats.max).c_str()
280 , stddev_percent
mtkleinf3723212014-06-25 14:08:00 -0700281 , config
mtkleinbb6a0282014-07-01 08:43:42 -0700282 , bench->getName()
mtkleinf3723212014-06-25 14:08:00 -0700283 );
284 }
285 }
mtkleinbb6a0282014-07-01 08:43:42 -0700286 targets.deleteAll();
mtkleinf3723212014-06-25 14:08:00 -0700287 }
288
289 return 0;
290}
291
292#if !defined SK_BUILD_FOR_IOS
293int main(int argc, char * const argv[]) {
294 return tool_main(argc, (char**) argv);
295}
296#endif