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