joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 8 | |
| 9 | #include "VisualLightweightBenchModule.h" |
| 10 | |
| 11 | #include "ProcStats.h" |
| 12 | #include "SkApplication.h" |
| 13 | #include "SkCanvas.h" |
| 14 | #include "SkCommandLineFlags.h" |
| 15 | #include "SkForceLinking.h" |
| 16 | #include "SkGraphics.h" |
| 17 | #include "SkGr.h" |
| 18 | #include "SkImageDecoder.h" |
| 19 | #include "SkOSFile.h" |
| 20 | #include "SkStream.h" |
| 21 | #include "Stats.h" |
| 22 | #include "gl/GrGLInterface.h" |
| 23 | |
| 24 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 25 | |
| 26 | // Between samples we reset context |
| 27 | // Between frames we swap buffers |
| 28 | |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame^] | 29 | DEFINE_int32(maxWarmupFrames, 100, "maxmium frames to try and tune for sane timings"); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 30 | DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag."); |
| 31 | DEFINE_int32(samples, 10, "Number of times to time each skp."); |
| 32 | DEFINE_int32(frames, 5, "Number of frames of each skp to render per sample."); |
| 33 | DEFINE_double(loopMs, 5, "Target loop time in millseconds."); |
| 34 | DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver."); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 35 | DEFINE_string(outResultsFile, "", "If given, write results here as JSON."); |
mtklein | ca6f43b | 2015-09-15 13:29:20 -0700 | [diff] [blame] | 36 | DEFINE_string(key, "", |
| 37 | "Space-separated key/value pairs to add to JSON identifying this builder."); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 38 | DEFINE_string(properties, "", |
| 39 | "Space-separated key/value pairs to add to JSON identifying this run."); |
| 40 | |
| 41 | static SkString humanize(double ms) { |
| 42 | if (FLAGS_verbose) { |
| 43 | return SkStringPrintf("%llu", (uint64_t)(ms*1e6)); |
| 44 | } |
| 45 | return HumanizeMs(ms); |
| 46 | } |
| 47 | |
| 48 | #define HUMANIZE(time) humanize(time).c_str() |
| 49 | |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame^] | 50 | // A trivial bench to warm up the gpu |
| 51 | class WarmupBench : public Benchmark { |
| 52 | public: |
| 53 | private: |
| 54 | const char* onGetName() override { return "warmupbench"; } |
| 55 | void onDraw(const int loops, SkCanvas* canvas) override { |
| 56 | for (int i = 0; i < loops; i++) { |
| 57 | sk_tool_utils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6, 10); |
| 58 | } |
| 59 | } |
| 60 | }; |
| 61 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 62 | VisualLightweightBenchModule::VisualLightweightBenchModule(VisualBench* owner) |
| 63 | : fCurrentSample(0) |
| 64 | , fCurrentFrame(0) |
| 65 | , fLoops(1) |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame^] | 66 | , fState(kWarmup_State) |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 67 | , fBenchmark(nullptr) |
| 68 | , fOwner(SkRef(owner)) |
| 69 | , fResults(new ResultsWriter) { |
| 70 | fBenchmarkStream.reset(new VisualBenchmarkStream); |
| 71 | |
| 72 | // Print header |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame^] | 73 | SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tbench\n", FLAGS_samples, |
| 74 | "samples"); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 75 | |
| 76 | // setup json logging if required |
| 77 | if (!FLAGS_outResultsFile.isEmpty()) { |
| 78 | fResults.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0])); |
| 79 | } |
| 80 | |
mtklein | ca6f43b | 2015-09-15 13:29:20 -0700 | [diff] [blame] | 81 | if (1 == FLAGS_key.count() % 2) { |
| 82 | SkDebugf("ERROR: --key must be passed with an even number of arguments.\n"); |
| 83 | } else { |
| 84 | for (int i = 1; i < FLAGS_key.count(); i += 2) { |
| 85 | fResults->key(FLAGS_key[i - 1], FLAGS_key[i]); |
| 86 | } |
| 87 | } |
| 88 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 89 | if (1 == FLAGS_properties.count() % 2) { |
| 90 | SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n"); |
| 91 | } else { |
| 92 | for (int i = 1; i < FLAGS_properties.count(); i += 2) { |
| 93 | fResults->property(FLAGS_properties[i - 1], FLAGS_properties[i]); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | inline void VisualLightweightBenchModule::renderFrame(SkCanvas* canvas) { |
| 99 | fBenchmark->draw(fLoops, canvas); |
| 100 | canvas->flush(); |
| 101 | fOwner->present(); |
| 102 | } |
| 103 | |
| 104 | void VisualLightweightBenchModule::printStats() { |
| 105 | const SkTArray<double>& measurements = fRecords.back().fMeasurements; |
| 106 | const char* shortName = fBenchmark->getUniqueName(); |
| 107 | |
| 108 | // update log |
| 109 | // Note: We currently log only the minimum. It would be interesting to log more information |
| 110 | SkString configName; |
| 111 | if (FLAGS_msaa > 0) { |
| 112 | configName.appendf("msaa_%d", FLAGS_msaa); |
| 113 | } else { |
| 114 | configName.appendf("gpu"); |
| 115 | } |
| 116 | fResults->config(configName.c_str()); |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame^] | 117 | fResults->configOption("name", shortName); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 118 | SkASSERT(measurements.count()); |
| 119 | Stats stats(measurements); |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame^] | 120 | fResults->metric("min_ms", stats.min); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 121 | |
| 122 | // Print output |
| 123 | if (FLAGS_verbose) { |
| 124 | for (int i = 0; i < measurements.count(); i++) { |
| 125 | SkDebugf("%s ", HUMANIZE(measurements[i])); |
| 126 | } |
| 127 | SkDebugf("%s\n", shortName); |
| 128 | } else { |
| 129 | const double stdDevPercent = 100 * sqrt(stats.var) / stats.mean; |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame^] | 130 | SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\n", |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 131 | sk_tools::getCurrResidentSetSizeMB(), |
| 132 | sk_tools::getMaxResidentSetSizeMB(), |
| 133 | fLoops, |
| 134 | HUMANIZE(stats.min), |
| 135 | HUMANIZE(stats.median), |
| 136 | HUMANIZE(stats.mean), |
| 137 | HUMANIZE(stats.max), |
| 138 | stdDevPercent, |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame^] | 139 | stats.plot.c_str(), |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 140 | shortName); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | bool VisualLightweightBenchModule::advanceRecordIfNecessary(SkCanvas* canvas) { |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame^] | 145 | if (!fBenchmark && fState == kWarmup_State) { |
| 146 | fBenchmark.reset(new WarmupBench); |
| 147 | return true; |
| 148 | } |
| 149 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 150 | if (fBenchmark) { |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | fBenchmark.reset(fBenchmarkStream->next()); |
| 155 | if (!fBenchmark) { |
| 156 | return false; |
| 157 | } |
| 158 | |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 159 | fOwner->clear(canvas, SK_ColorWHITE, 2); |
| 160 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 161 | fBenchmark->preDraw(); |
| 162 | fRecords.push_back(); |
| 163 | |
| 164 | // Log bench name |
| 165 | fResults->bench(fBenchmark->getUniqueName(), fBenchmark->getSize().fX, |
| 166 | fBenchmark->getSize().fY); |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | inline void VisualLightweightBenchModule::nextState(State nextState) { |
| 171 | fState = nextState; |
| 172 | } |
| 173 | |
| 174 | void VisualLightweightBenchModule::perCanvasPreDraw(SkCanvas* canvas, State nextState) { |
| 175 | fBenchmark->perCanvasPreDraw(canvas); |
| 176 | fCurrentFrame = 0; |
| 177 | this->nextState(nextState); |
| 178 | } |
| 179 | |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame^] | 180 | void VisualLightweightBenchModule::warmup(SkCanvas* canvas) { |
| 181 | if (fCurrentFrame >= FLAGS_maxWarmupFrames) { |
| 182 | this->nextState(kPreWarmLoopsPerCanvasPreDraw_State); |
| 183 | fBenchmark.reset(nullptr); |
| 184 | this->resetTimingState(); |
| 185 | fLoops = 1; |
| 186 | } else { |
| 187 | bool isEven = (fCurrentFrame++ % 2) == 0; |
| 188 | if (isEven) { |
| 189 | fTimer.start(); |
| 190 | } else { |
| 191 | double elapsedMs = this->elapsed(); |
| 192 | if (elapsedMs < FLAGS_loopMs) { |
| 193 | fLoops *= 2; |
| 194 | } |
| 195 | fTimer = WallTimer(); |
| 196 | fOwner->reset(); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 201 | void VisualLightweightBenchModule::preWarm(State nextState) { |
| 202 | if (fCurrentFrame >= FLAGS_gpuFrameLag) { |
| 203 | // we currently time across all frames to make sure we capture all GPU work |
| 204 | this->nextState(nextState); |
| 205 | fCurrentFrame = 0; |
| 206 | fTimer.start(); |
| 207 | } else { |
| 208 | fCurrentFrame++; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | void VisualLightweightBenchModule::draw(SkCanvas* canvas) { |
| 213 | if (!this->advanceRecordIfNecessary(canvas)) { |
| 214 | SkDebugf("Exiting VisualBench successfully\n"); |
| 215 | fOwner->closeWindow(); |
| 216 | return; |
| 217 | } |
| 218 | this->renderFrame(canvas); |
| 219 | switch (fState) { |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame^] | 220 | case kWarmup_State: { |
| 221 | this->warmup(canvas); |
| 222 | break; |
| 223 | } |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 224 | case kPreWarmLoopsPerCanvasPreDraw_State: { |
| 225 | this->perCanvasPreDraw(canvas, kPreWarmLoops_State); |
| 226 | break; |
| 227 | } |
| 228 | case kPreWarmLoops_State: { |
| 229 | this->preWarm(kTuneLoops_State); |
| 230 | break; |
| 231 | } |
| 232 | case kTuneLoops_State: { |
| 233 | this->tuneLoops(); |
| 234 | break; |
| 235 | } |
| 236 | case kPreWarmTimingPerCanvasPreDraw_State: { |
| 237 | this->perCanvasPreDraw(canvas, kPreWarmTiming_State); |
| 238 | break; |
| 239 | } |
| 240 | case kPreWarmTiming_State: { |
| 241 | this->preWarm(kTiming_State); |
| 242 | break; |
| 243 | } |
| 244 | case kTiming_State: { |
| 245 | this->timing(canvas); |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | inline double VisualLightweightBenchModule::elapsed() { |
| 252 | fTimer.end(); |
| 253 | return fTimer.fWall; |
| 254 | } |
| 255 | |
| 256 | void VisualLightweightBenchModule::resetTimingState() { |
| 257 | fCurrentFrame = 0; |
| 258 | fTimer = WallTimer(); |
| 259 | fOwner->reset(); |
| 260 | } |
| 261 | |
| 262 | void VisualLightweightBenchModule::scaleLoops(double elapsedMs) { |
| 263 | // Scale back the number of loops |
| 264 | fLoops = (int)ceil(fLoops * FLAGS_loopMs / elapsedMs); |
| 265 | } |
| 266 | |
| 267 | inline void VisualLightweightBenchModule::tuneLoops() { |
| 268 | if (1 << 30 == fLoops) { |
| 269 | // We're about to wrap. Something's wrong with the bench. |
| 270 | SkDebugf("InnerLoops wrapped\n"); |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame^] | 271 | fLoops = 1; |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 272 | } else { |
| 273 | double elapsedMs = this->elapsed(); |
| 274 | if (elapsedMs > FLAGS_loopMs) { |
| 275 | this->scaleLoops(elapsedMs); |
| 276 | this->nextState(kPreWarmTimingPerCanvasPreDraw_State); |
| 277 | } else { |
| 278 | fLoops *= 2; |
| 279 | this->nextState(kPreWarmLoops_State); |
| 280 | } |
| 281 | this->resetTimingState(); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void VisualLightweightBenchModule::recordMeasurement() { |
| 286 | double measurement = this->elapsed() / (FLAGS_frames * fLoops); |
| 287 | fRecords.back().fMeasurements.push_back(measurement); |
| 288 | } |
| 289 | |
| 290 | void VisualLightweightBenchModule::postDraw(SkCanvas* canvas) { |
| 291 | fBenchmark->perCanvasPostDraw(canvas); |
| 292 | fBenchmark.reset(nullptr); |
| 293 | fCurrentSample = 0; |
| 294 | fLoops = 1; |
| 295 | } |
| 296 | |
| 297 | inline void VisualLightweightBenchModule::timing(SkCanvas* canvas) { |
| 298 | if (fCurrentFrame >= FLAGS_frames) { |
| 299 | this->recordMeasurement(); |
| 300 | if (fCurrentSample++ >= FLAGS_samples) { |
| 301 | this->printStats(); |
| 302 | this->postDraw(canvas); |
| 303 | this->nextState(kPreWarmLoopsPerCanvasPreDraw_State); |
| 304 | } else { |
| 305 | this->nextState(kPreWarmTimingPerCanvasPreDraw_State); |
| 306 | } |
| 307 | this->resetTimingState(); |
| 308 | } else { |
| 309 | fCurrentFrame++; |
| 310 | } |
| 311 | } |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 312 | |
| 313 | bool VisualLightweightBenchModule::onHandleChar(SkUnichar c) { |
| 314 | return true; |
| 315 | } |