mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 8 | #include <ctype.h> |
| 9 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 10 | #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 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 22 | #if SK_SUPPORT_GPU |
| 23 | #include "GrContextFactory.h" |
| 24 | GrContextFactory gGrFactory; |
| 25 | #endif |
| 26 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 27 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 28 | |
| 29 | DEFINE_int32(samples, 10, "Number of samples to measure for each bench."); |
| 30 | DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead."); |
| 31 | DEFINE_double(overheadGoal, 0.0001, |
| 32 | "Loop until timer overhead is at most this fraction of our measurments."); |
| 33 | DEFINE_string(match, "", "The usual filters on file names of benchmarks to measure."); |
| 34 | DEFINE_bool2(quiet, q, false, "Print only bench name and minimum sample."); |
| 35 | DEFINE_bool2(verbose, v, false, "Print all samples."); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 36 | DEFINE_string(config, "nonrendering 8888 gpu", "Configs to measure. Options: " |
| 37 | "565 8888 gpu nonrendering debug nullgpu msaa4 msaa16 nvprmsaa4 nvprmsaa16 angle"); |
| 38 | DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU."); |
| 39 | DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag."); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 40 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 41 | |
| 42 | static 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 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 49 | static 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 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 68 | static double estimate_timer_overhead() { |
| 69 | double overhead = 0; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 70 | for (int i = 0; i < FLAGS_overheadLoops; i++) { |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 71 | overhead += time(1, NULL, NULL, NULL); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 72 | } |
| 73 | return overhead / FLAGS_overheadLoops; |
| 74 | } |
| 75 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 76 | static 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; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 79 | do { |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 80 | bench_plus_overhead = time(1, bench, canvas, NULL); |
| 81 | } while (bench_plus_overhead < overhead); // Shouldn't normally happen. |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 82 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 83 | // Later we'll just start and stop the timer once but loop N times. |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 84 | // We'll pick N to make timer overhead negligible: |
| 85 | // |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 86 | // overhead |
| 87 | // ------------------------- < FLAGS_overheadGoal |
| 88 | // overhead + N * Bench Time |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 89 | // |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 90 | // where bench_plus_overhead ≈ overhead + Bench Time. |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 91 | // |
| 92 | // Doing some math, we get: |
| 93 | // |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 94 | // (overhead / FLAGS_overheadGoal) - overhead |
| 95 | // ------------------------------------------ < N |
| 96 | // bench_plus_overhead - overhead) |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 97 | // |
| 98 | // Luckily, this also works well in practice. :) |
| 99 | const double numer = overhead / FLAGS_overheadGoal - overhead; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 100 | 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; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 107 | } |
| 108 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 109 | #if SK_SUPPORT_GPU |
| 110 | static 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); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 140 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 141 | |
| 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 | |
| 150 | static 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; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 156 | } |
| 157 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 158 | struct Target { |
| 159 | const char* config; |
| 160 | Benchmark::Backend backend; |
| 161 | SkAutoTDelete<SkSurface> surface; |
| 162 | #if SK_SUPPORT_GPU |
| 163 | SkGLContextHelper* gl; |
| 164 | #endif |
| 165 | }; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 166 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 167 | // If bench is enabled for backend/config, returns a Target* for them, otherwise NULL. |
| 168 | static Target* is_enabled(Benchmark* bench, Benchmark::Backend backend, const char* config) { |
| 169 | if (!bench->isSuitableFor(backend)) { |
| 170 | return NULL; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 171 | } |
| 172 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 173 | 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; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | // Append all targets that are suitable for bench. |
| 185 | static 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 |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | int tool_main(int argc, char** argv); |
| 221 | int tool_main(int argc, char** argv) { |
| 222 | SetupCrashHandler(); |
| 223 | SkAutoGraphics ag; |
| 224 | SkCommandLineFlags::Parse(argc, argv); |
| 225 | |
| 226 | const double overhead = estimate_timer_overhead(); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 227 | SkAutoTMalloc<double> samples(FLAGS_samples); |
| 228 | |
| 229 | // TODO: display add median, use it in --quiet mode |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 230 | |
| 231 | if (FLAGS_verbose) { |
| 232 | // No header. |
| 233 | } else if (FLAGS_quiet) { |
| 234 | SkDebugf("min\tbench\tconfig\n"); |
| 235 | } else { |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 236 | SkDebugf("loops\tmin\tmean\tmax\tstddev\tconfig\tbench\n"); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 237 | } |
| 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 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 245 | SkTDArray<Target*> targets; |
| 246 | create_targets(bench.get(), &targets); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 247 | |
| 248 | bench->preDraw(); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 249 | for (int j = 0; j < targets.count(); j++) { |
| 250 | SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 251 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 252 | 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()); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 259 | |
| 260 | Stats stats(samples.get(), FLAGS_samples); |
| 261 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 262 | const char* config = targets[j]->config; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 263 | 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) { |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 269 | if (targets.count() == 1) { |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 270 | 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 |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 281 | , config |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 282 | , bench->getName() |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 283 | ); |
| 284 | } |
| 285 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame^] | 286 | targets.deleteAll(); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | #if !defined SK_BUILD_FOR_IOS |
| 293 | int main(int argc, char * const argv[]) { |
| 294 | return tool_main(argc, (char**) argv); |
| 295 | } |
| 296 | #endif |