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