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" |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame^] | 12 | #include "GMBench.h" |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 13 | #include "ResultsWriter.h" |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 14 | #include "Stats.h" |
| 15 | #include "Timer.h" |
| 16 | |
| 17 | #include "SkCanvas.h" |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 18 | #include "SkCommonFlags.h" |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 19 | #include "SkForceLinking.h" |
| 20 | #include "SkGraphics.h" |
| 21 | #include "SkString.h" |
| 22 | #include "SkSurface.h" |
| 23 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 24 | #if SK_SUPPORT_GPU |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 25 | #include "gl/GrGLDefines.h" |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 26 | #include "GrContextFactory.h" |
| 27 | GrContextFactory gGrFactory; |
| 28 | #endif |
| 29 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 30 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 31 | |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 32 | #if SK_DEBUG |
| 33 | DEFINE_bool(runOnce, true, "Run each benchmark just once?"); |
| 34 | #else |
| 35 | DEFINE_bool(runOnce, false, "Run each benchmark just once?"); |
| 36 | #endif |
| 37 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 38 | DEFINE_int32(samples, 10, "Number of samples to measure for each bench."); |
| 39 | DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead."); |
| 40 | DEFINE_double(overheadGoal, 0.0001, |
| 41 | "Loop until timer overhead is at most this fraction of our measurments."); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 42 | DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU."); |
| 43 | 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] | 44 | |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 45 | DEFINE_string(outResultsFile, "", "If given, write results here as JSON."); |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 46 | DEFINE_int32(maxCalibrationAttempts, 3, |
| 47 | "Try up to this many times to guess loops for a bench, or skip the bench."); |
| 48 | DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this."); |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 49 | DEFINE_string(key, "", "Space-separated key/value pairs to add to JSON."); |
| 50 | DEFINE_string(gitHash, "", "Git hash to add to JSON."); |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 51 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 52 | static SkString humanize(double ms) { |
| 53 | if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3); |
| 54 | if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6); |
mtklein | 6238688 | 2014-07-15 10:30:31 -0700 | [diff] [blame] | 55 | #ifdef SK_BUILD_FOR_WIN |
| 56 | if (ms < 1) return SkStringPrintf("%.3gus", ms*1e3); |
| 57 | #else |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 58 | if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3); |
mtklein | 6238688 | 2014-07-15 10:30:31 -0700 | [diff] [blame] | 59 | #endif |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 60 | return SkStringPrintf("%.3gms", ms); |
| 61 | } |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 62 | #define HUMANIZE(ms) humanize(ms).c_str() |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 63 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 64 | static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHelper* gl) { |
| 65 | WallTimer timer; |
| 66 | timer.start(); |
| 67 | if (bench) { |
| 68 | bench->draw(loops, canvas); |
| 69 | } |
| 70 | if (canvas) { |
| 71 | canvas->flush(); |
| 72 | } |
| 73 | #if SK_SUPPORT_GPU |
| 74 | if (gl) { |
| 75 | SK_GL(*gl, Flush()); |
| 76 | gl->swapBuffers(); |
| 77 | } |
| 78 | #endif |
| 79 | timer.end(); |
| 80 | return timer.fWall; |
| 81 | } |
| 82 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 83 | static double estimate_timer_overhead() { |
| 84 | double overhead = 0; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 85 | for (int i = 0; i < FLAGS_overheadLoops; i++) { |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 86 | overhead += time(1, NULL, NULL, NULL); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 87 | } |
| 88 | return overhead / FLAGS_overheadLoops; |
| 89 | } |
| 90 | |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 91 | static int clamp_loops(int loops) { |
| 92 | if (loops < 1) { |
| 93 | SkDebugf("ERROR: clamping loops from %d to 1.\n", loops); |
| 94 | return 1; |
| 95 | } |
| 96 | if (loops > FLAGS_maxLoops) { |
| 97 | SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops); |
| 98 | return FLAGS_maxLoops; |
| 99 | } |
| 100 | return loops; |
| 101 | } |
| 102 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 103 | static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) { |
| 104 | // First figure out approximately how many loops of bench it takes to make overhead negligible. |
| 105 | double bench_plus_overhead; |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 106 | int round = 0; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 107 | do { |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 108 | bench_plus_overhead = time(1, bench, canvas, NULL); |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 109 | if (++round == FLAGS_maxCalibrationAttempts) { |
| 110 | SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n", |
| 111 | bench->getName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead)); |
| 112 | return 0; |
| 113 | } |
Mike Klein | e363136 | 2014-07-15 17:56:37 -0400 | [diff] [blame] | 114 | } while (bench_plus_overhead < overhead); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 115 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 116 | // 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] | 117 | // We'll pick N to make timer overhead negligible: |
| 118 | // |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 119 | // overhead |
| 120 | // ------------------------- < FLAGS_overheadGoal |
| 121 | // overhead + N * Bench Time |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 122 | // |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 123 | // where bench_plus_overhead ≈ overhead + Bench Time. |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 124 | // |
| 125 | // Doing some math, we get: |
| 126 | // |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 127 | // (overhead / FLAGS_overheadGoal) - overhead |
| 128 | // ------------------------------------------ < N |
| 129 | // bench_plus_overhead - overhead) |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 130 | // |
| 131 | // Luckily, this also works well in practice. :) |
| 132 | const double numer = overhead / FLAGS_overheadGoal - overhead; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 133 | const double denom = bench_plus_overhead - overhead; |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 134 | const int loops = clamp_loops(FLAGS_runOnce ? 1 : (int)ceil(numer / denom)); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 135 | |
| 136 | for (int i = 0; i < FLAGS_samples; i++) { |
| 137 | samples[i] = time(loops, bench, canvas, NULL) / loops; |
| 138 | } |
| 139 | return loops; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 140 | } |
| 141 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 142 | #if SK_SUPPORT_GPU |
| 143 | static int gpu_bench(SkGLContextHelper* gl, |
| 144 | Benchmark* bench, |
| 145 | SkCanvas* canvas, |
| 146 | double* samples) { |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 147 | gl->makeCurrent(); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 148 | // Make sure we're done with whatever came before. |
mtklein | 9bc86ed | 2014-07-01 10:02:42 -0700 | [diff] [blame] | 149 | SK_GL(*gl, Finish()); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 150 | |
| 151 | // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs. |
| 152 | int loops = 1; |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 153 | if (!FLAGS_runOnce) { |
| 154 | double elapsed = 0; |
| 155 | do { |
| 156 | loops *= 2; |
| 157 | // If the GPU lets frames lag at all, we need to make sure we're timing |
| 158 | // _this_ round, not still timing last round. We force this by looping |
| 159 | // more times than any reasonable GPU will allow frames to lag. |
| 160 | for (int i = 0; i < FLAGS_gpuFrameLag; i++) { |
| 161 | elapsed = time(loops, bench, canvas, gl); |
| 162 | } |
| 163 | } while (elapsed < FLAGS_gpuMs); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 164 | |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 165 | // We've overshot at least a little. Scale back linearly. |
| 166 | loops = (int)ceil(loops * FLAGS_gpuMs / elapsed); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 167 | |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 168 | // Might as well make sure we're not still timing our calibration. |
| 169 | SK_GL(*gl, Finish()); |
| 170 | } |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 171 | loops = clamp_loops(loops); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 172 | |
| 173 | // Pretty much the same deal as the calibration: do some warmup to make |
| 174 | // sure we're timing steady-state pipelined frames. |
| 175 | for (int i = 0; i < FLAGS_gpuFrameLag; i++) { |
| 176 | time(loops, bench, canvas, gl); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 177 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 178 | |
| 179 | // Now, actually do the timing! |
| 180 | for (int i = 0; i < FLAGS_samples; i++) { |
| 181 | samples[i] = time(loops, bench, canvas, gl) / loops; |
| 182 | } |
| 183 | return loops; |
| 184 | } |
| 185 | #endif |
| 186 | |
| 187 | static SkString to_lower(const char* str) { |
| 188 | SkString lower(str); |
| 189 | for (size_t i = 0; i < lower.size(); i++) { |
| 190 | lower[i] = tolower(lower[i]); |
| 191 | } |
| 192 | return lower; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 193 | } |
| 194 | |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 195 | struct Config { |
| 196 | const char* name; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 197 | Benchmark::Backend backend; |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 198 | SkColorType color; |
| 199 | SkAlphaType alpha; |
| 200 | int samples; |
| 201 | #if SK_SUPPORT_GPU |
| 202 | GrContextFactory::GLContextType ctxType; |
| 203 | #else |
| 204 | int bogusInt; |
| 205 | #endif |
| 206 | }; |
| 207 | |
| 208 | struct Target { |
| 209 | explicit Target(const Config& c) : config(c) {} |
| 210 | const Config config; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 211 | SkAutoTDelete<SkSurface> surface; |
| 212 | #if SK_SUPPORT_GPU |
| 213 | SkGLContextHelper* gl; |
| 214 | #endif |
| 215 | }; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 216 | |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 217 | static bool is_cpu_config_allowed(const char* name) { |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 218 | for (int i = 0; i < FLAGS_config.count(); i++) { |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 219 | if (to_lower(FLAGS_config[i]).equals(name)) { |
| 220 | return true; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 223 | return false; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 224 | } |
| 225 | |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 226 | #if SK_SUPPORT_GPU |
| 227 | static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType, |
| 228 | int sampleCnt) { |
| 229 | if (!is_cpu_config_allowed(name)) { |
| 230 | return false; |
| 231 | } |
| 232 | if (const GrContext* ctx = gGrFactory.get(ctxType)) { |
| 233 | return sampleCnt <= ctx->getMaxSampleCount(); |
| 234 | } |
| 235 | return false; |
| 236 | } |
| 237 | #endif |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 238 | |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 239 | #if SK_SUPPORT_GPU |
| 240 | #define kBogusGLContextType GrContextFactory::kNative_GLContextType |
| 241 | #else |
| 242 | #define kBogusGLContextType 0 |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame^] | 243 | #endif |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 244 | |
| 245 | // Append all configs that are enabled and supported. |
| 246 | static void create_configs(SkTDArray<Config>* configs) { |
| 247 | #define CPU_CONFIG(name, backend, color, alpha) \ |
| 248 | if (is_cpu_config_allowed(#name)) { \ |
| 249 | Config config = { #name, Benchmark::backend, color, alpha, 0, kBogusGLContextType }; \ |
| 250 | configs->push(config); \ |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 251 | } |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame^] | 252 | |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 253 | if (FLAGS_cpu) { |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 254 | CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType) |
| 255 | CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType) |
| 256 | CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType) |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 257 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 258 | |
| 259 | #if SK_SUPPORT_GPU |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 260 | #define GPU_CONFIG(name, ctxType, samples) \ |
| 261 | if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \ |
| 262 | Config config = { \ |
| 263 | #name, \ |
| 264 | Benchmark::kGPU_Backend, \ |
| 265 | kN32_SkColorType, \ |
| 266 | kPremul_SkAlphaType, \ |
| 267 | samples, \ |
| 268 | GrContextFactory::ctxType }; \ |
| 269 | configs->push(config); \ |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 270 | } |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame^] | 271 | |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 272 | if (FLAGS_gpu) { |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 273 | GPU_CONFIG(gpu, kNative_GLContextType, 0) |
| 274 | GPU_CONFIG(msaa4, kNative_GLContextType, 4) |
| 275 | GPU_CONFIG(msaa16, kNative_GLContextType, 16) |
| 276 | GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4) |
| 277 | GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16) |
| 278 | GPU_CONFIG(debug, kDebug_GLContextType, 0) |
| 279 | GPU_CONFIG(nullgpu, kNull_GLContextType, 0) |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 280 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 281 | #endif |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 282 | } |
| 283 | |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 284 | // If bench is enabled for config, returns a Target* for it, otherwise NULL. |
| 285 | static Target* is_enabled(Benchmark* bench, const Config& config) { |
| 286 | if (!bench->isSuitableFor(config.backend)) { |
| 287 | return NULL; |
| 288 | } |
| 289 | |
| 290 | SkImageInfo info; |
| 291 | info.fAlphaType = config.alpha; |
| 292 | info.fColorType = config.color; |
| 293 | info.fWidth = bench->getSize().fX; |
| 294 | info.fHeight = bench->getSize().fY; |
| 295 | |
| 296 | Target* target = new Target(config); |
| 297 | |
| 298 | if (Benchmark::kRaster_Backend == config.backend) { |
| 299 | target->surface.reset(SkSurface::NewRaster(info)); |
| 300 | } |
| 301 | #if SK_SUPPORT_GPU |
| 302 | else if (Benchmark::kGPU_Backend == config.backend) { |
| 303 | target->surface.reset(SkSurface::NewRenderTarget(gGrFactory.get(config.ctxType), info, |
| 304 | config.samples)); |
| 305 | target->gl = gGrFactory.getGLContext(config.ctxType); |
| 306 | } |
| 307 | #endif |
| 308 | |
| 309 | if (Benchmark::kNonRendering_Backend != config.backend && !target->surface.get()) { |
| 310 | delete target; |
| 311 | return NULL; |
| 312 | } |
| 313 | return target; |
| 314 | } |
| 315 | |
| 316 | // Creates targets for a benchmark and a set of configs. |
| 317 | static void create_targets(SkTDArray<Target*>* targets, Benchmark* b, |
| 318 | const SkTDArray<Config>& configs) { |
| 319 | for (int i = 0; i < configs.count(); ++i) { |
| 320 | if (Target* t = is_enabled(b, configs[i])) { |
| 321 | targets->push(t); |
| 322 | } |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame^] | 323 | |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 327 | static void fill_static_options(ResultsWriter* log) { |
| 328 | #if defined(SK_BUILD_FOR_WIN32) |
| 329 | log->option("system", "WIN32"); |
| 330 | #elif defined(SK_BUILD_FOR_MAC) |
| 331 | log->option("system", "MAC"); |
| 332 | #elif defined(SK_BUILD_FOR_ANDROID) |
| 333 | log->option("system", "ANDROID"); |
| 334 | #elif defined(SK_BUILD_FOR_UNIX) |
| 335 | log->option("system", "UNIX"); |
| 336 | #else |
| 337 | log->option("system", "other"); |
| 338 | #endif |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 339 | } |
| 340 | |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 341 | #if SK_SUPPORT_GPU |
| 342 | static void fill_gpu_options(ResultsWriter* log, SkGLContextHelper* ctx) { |
jcgregorio | 05c4560 | 2014-07-17 13:54:36 -0700 | [diff] [blame] | 343 | const GrGLubyte* version; |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 344 | SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION)); |
| 345 | log->configOption("GL_VERSION", (const char*)(version)); |
| 346 | |
| 347 | SK_GL_RET(*ctx, version, GetString(GR_GL_RENDERER)); |
| 348 | log->configOption("GL_RENDERER", (const char*) version); |
| 349 | |
| 350 | SK_GL_RET(*ctx, version, GetString(GR_GL_VENDOR)); |
| 351 | log->configOption("GL_VENDOR", (const char*) version); |
| 352 | |
| 353 | SK_GL_RET(*ctx, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION)); |
| 354 | log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version); |
| 355 | } |
| 356 | #endif |
| 357 | |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame^] | 358 | class BenchmarkStream { |
| 359 | public: |
| 360 | BenchmarkStream() : fBenches(BenchRegistry::Head()) , fGMs(skiagm::GMRegistry::Head()) {} |
| 361 | |
| 362 | Benchmark* next(const char** sourceType) { |
| 363 | if (fBenches) { |
| 364 | Benchmark* bench = fBenches->factory()(NULL); |
| 365 | fBenches = fBenches->next(); |
| 366 | *sourceType = "bench"; |
| 367 | return bench; |
| 368 | } |
| 369 | while (fGMs) { |
| 370 | SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL)); |
| 371 | fGMs = fGMs->next(); |
| 372 | if (gm->getFlags() & skiagm::GM::kAsBench_Flag) { |
| 373 | *sourceType = "gm"; |
| 374 | return SkNEW_ARGS(GMBench, (gm.detach())); |
| 375 | } |
| 376 | } |
| 377 | return NULL; |
| 378 | } |
| 379 | private: |
| 380 | const BenchRegistry* fBenches; |
| 381 | const skiagm::GMRegistry* fGMs; |
| 382 | }; |
| 383 | |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 384 | int nanobench_main(); |
| 385 | int nanobench_main() { |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 386 | SetupCrashHandler(); |
| 387 | SkAutoGraphics ag; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 388 | |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 389 | if (FLAGS_runOnce) { |
| 390 | FLAGS_samples = 1; |
| 391 | FLAGS_gpuFrameLag = 0; |
| 392 | } |
| 393 | |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 394 | MultiResultsWriter log; |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 395 | SkAutoTDelete<NanoJSONResultsWriter> json; |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 396 | if (!FLAGS_outResultsFile.isEmpty()) { |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 397 | const char* gitHash = FLAGS_gitHash.isEmpty() ? "unknown-revision" : FLAGS_gitHash[0]; |
| 398 | json.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0], gitHash))); |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 399 | log.add(json.get()); |
| 400 | } |
| 401 | CallEnd<MultiResultsWriter> ender(log); |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 402 | |
| 403 | if (1 == FLAGS_key.count() % 2) { |
| 404 | SkDebugf("ERROR: --key must be passed with an even number of arguments.\n"); |
| 405 | return 1; |
| 406 | } |
| 407 | for (int i = 1; i < FLAGS_key.count(); i += 2) { |
| 408 | log.key(FLAGS_key[i-1], FLAGS_key[i]); |
| 409 | } |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 410 | fill_static_options(&log); |
| 411 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 412 | const double overhead = estimate_timer_overhead(); |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 413 | SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead)); |
Mike Klein | 9129477 | 2014-07-16 19:59:32 -0400 | [diff] [blame] | 414 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 415 | SkAutoTMalloc<double> samples(FLAGS_samples); |
| 416 | |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 417 | if (FLAGS_runOnce) { |
| 418 | SkDebugf("--runOnce is true; times would only be misleading so we won't print them.\n"); |
| 419 | } else if (FLAGS_verbose) { |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 420 | // No header. |
| 421 | } else if (FLAGS_quiet) { |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 422 | SkDebugf("median\tbench\tconfig\n"); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 423 | } else { |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 424 | SkDebugf("loops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n"); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 425 | } |
| 426 | |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 427 | SkTDArray<Config> configs; |
| 428 | create_configs(&configs); |
| 429 | |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame^] | 430 | BenchmarkStream benches; |
| 431 | const char* sourceType; |
| 432 | while (Benchmark* b = benches.next(&sourceType)) { |
| 433 | SkAutoTDelete<Benchmark> bench(b); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 434 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) { |
| 435 | continue; |
| 436 | } |
| 437 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 438 | SkTDArray<Target*> targets; |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 439 | create_targets(&targets, bench.get(), configs); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 440 | |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 441 | if (!targets.isEmpty()) { |
| 442 | log.bench(bench->getName(), bench->getSize().fX, bench->getSize().fY); |
| 443 | bench->preDraw(); |
| 444 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 445 | for (int j = 0; j < targets.count(); j++) { |
| 446 | SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL; |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 447 | const char* config = targets[j]->config.name; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 448 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 449 | const int loops = |
| 450 | #if SK_SUPPORT_GPU |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 451 | Benchmark::kGPU_Backend == targets[j]->config.backend |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 452 | ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get()) |
| 453 | : |
| 454 | #endif |
| 455 | cpu_bench( overhead, bench.get(), canvas, samples.get()); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 456 | |
Mike Klein | e363136 | 2014-07-15 17:56:37 -0400 | [diff] [blame] | 457 | if (loops == 0) { |
mtklein | 04d53a5 | 2014-07-17 11:23:07 -0700 | [diff] [blame] | 458 | SkDebugf("Unable to time %s\t%s (overhead %s)\n", |
| 459 | bench->getName(), config, HUMANIZE(overhead)); |
Mike Klein | e363136 | 2014-07-15 17:56:37 -0400 | [diff] [blame] | 460 | continue; |
| 461 | } |
| 462 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 463 | Stats stats(samples.get(), FLAGS_samples); |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 464 | log.config(config); |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame^] | 465 | log.configOption("source_type", sourceType); |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 466 | #if SK_SUPPORT_GPU |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 467 | if (Benchmark::kGPU_Backend == targets[j]->config.backend) { |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 468 | fill_gpu_options(&log, targets[j]->gl); |
| 469 | } |
| 470 | #endif |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 471 | log.timer("min_ms", stats.min); |
| 472 | log.timer("median_ms", stats.median); |
| 473 | log.timer("mean_ms", stats.mean); |
| 474 | log.timer("max_ms", stats.max); |
| 475 | log.timer("stddev_ms", sqrt(stats.var)); |
| 476 | |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 477 | if (FLAGS_runOnce) { |
| 478 | if (targets.count() == 1) { |
| 479 | config = ""; // Only print the config if we run the same bench on more than one. |
| 480 | } |
| 481 | SkDebugf("%s\t%s\n", bench->getName(), config); |
| 482 | } else if (FLAGS_verbose) { |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 483 | for (int i = 0; i < FLAGS_samples; i++) { |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 484 | SkDebugf("%s ", HUMANIZE(samples[i])); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 485 | } |
| 486 | SkDebugf("%s\n", bench->getName()); |
| 487 | } else if (FLAGS_quiet) { |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 488 | if (targets.count() == 1) { |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 489 | config = ""; // Only print the config if we run the same bench on more than one. |
| 490 | } |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 491 | SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getName(), config); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 492 | } else { |
| 493 | const double stddev_percent = 100 * sqrt(stats.var) / stats.mean; |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 494 | SkDebugf("%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n" |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 495 | , loops |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 496 | , HUMANIZE(stats.min) |
| 497 | , HUMANIZE(stats.median) |
| 498 | , HUMANIZE(stats.mean) |
| 499 | , HUMANIZE(stats.max) |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 500 | , stddev_percent |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 501 | , stats.plot.c_str() |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 502 | , config |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 503 | , bench->getName() |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 504 | ); |
| 505 | } |
| 506 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 507 | targets.deleteAll(); |
Mike Klein | 3944a1d | 2014-07-15 13:40:19 -0400 | [diff] [blame] | 508 | |
| 509 | #if SK_SUPPORT_GPU |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 510 | if (FLAGS_abandonGpuContext) { |
| 511 | gGrFactory.abandonContexts(); |
| 512 | } |
| 513 | if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) { |
Mike Klein | 3944a1d | 2014-07-15 13:40:19 -0400 | [diff] [blame] | 514 | gGrFactory.destroyContexts(); |
| 515 | } |
| 516 | #endif |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | #if !defined SK_BUILD_FOR_IOS |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 523 | int main(int argc, char** argv) { |
| 524 | SkCommandLineFlags::Parse(argc, argv); |
| 525 | return nanobench_main(); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 526 | } |
| 527 | #endif |