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 | 8db65a6 | 2015-09-24 12:42:02 -0700 | [diff] [blame] | 50 | // We draw a big nonAA path to warmup the gpu / cpu |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 51 | class WarmupBench : public Benchmark { |
| 52 | public: |
joshualitt | 8db65a6 | 2015-09-24 12:42:02 -0700 | [diff] [blame] | 53 | WarmupBench() { |
| 54 | make_path(fPath); |
| 55 | } |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 56 | private: |
joshualitt | 8db65a6 | 2015-09-24 12:42:02 -0700 | [diff] [blame] | 57 | static void make_path(SkPath& path) { |
| 58 | #include "BigPathBench.inc" |
| 59 | } |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 60 | const char* onGetName() override { return "warmupbench"; } |
| 61 | void onDraw(const int loops, SkCanvas* canvas) override { |
joshualitt | 8db65a6 | 2015-09-24 12:42:02 -0700 | [diff] [blame] | 62 | SkPaint paint; |
| 63 | paint.setStyle(SkPaint::kStroke_Style); |
| 64 | paint.setStrokeWidth(2); |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 65 | for (int i = 0; i < loops; i++) { |
joshualitt | 8db65a6 | 2015-09-24 12:42:02 -0700 | [diff] [blame] | 66 | canvas->drawPath(fPath, paint); |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
joshualitt | 8db65a6 | 2015-09-24 12:42:02 -0700 | [diff] [blame] | 69 | SkPath fPath; |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 72 | VisualLightweightBenchModule::VisualLightweightBenchModule(VisualBench* owner) |
| 73 | : fCurrentSample(0) |
| 74 | , fCurrentFrame(0) |
| 75 | , fLoops(1) |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 76 | , fState(kWarmup_State) |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 77 | , fBenchmark(nullptr) |
| 78 | , fOwner(SkRef(owner)) |
| 79 | , fResults(new ResultsWriter) { |
| 80 | fBenchmarkStream.reset(new VisualBenchmarkStream); |
| 81 | |
| 82 | // Print header |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 83 | SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tbench\n", FLAGS_samples, |
| 84 | "samples"); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 85 | |
| 86 | // setup json logging if required |
| 87 | if (!FLAGS_outResultsFile.isEmpty()) { |
| 88 | fResults.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0])); |
| 89 | } |
| 90 | |
mtklein | ca6f43b | 2015-09-15 13:29:20 -0700 | [diff] [blame] | 91 | if (1 == FLAGS_key.count() % 2) { |
| 92 | SkDebugf("ERROR: --key must be passed with an even number of arguments.\n"); |
| 93 | } else { |
| 94 | for (int i = 1; i < FLAGS_key.count(); i += 2) { |
| 95 | fResults->key(FLAGS_key[i - 1], FLAGS_key[i]); |
| 96 | } |
| 97 | } |
| 98 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 99 | if (1 == FLAGS_properties.count() % 2) { |
| 100 | SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n"); |
| 101 | } else { |
| 102 | for (int i = 1; i < FLAGS_properties.count(); i += 2) { |
| 103 | fResults->property(FLAGS_properties[i - 1], FLAGS_properties[i]); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | inline void VisualLightweightBenchModule::renderFrame(SkCanvas* canvas) { |
| 109 | fBenchmark->draw(fLoops, canvas); |
| 110 | canvas->flush(); |
| 111 | fOwner->present(); |
| 112 | } |
| 113 | |
| 114 | void VisualLightweightBenchModule::printStats() { |
| 115 | const SkTArray<double>& measurements = fRecords.back().fMeasurements; |
| 116 | const char* shortName = fBenchmark->getUniqueName(); |
| 117 | |
| 118 | // update log |
| 119 | // Note: We currently log only the minimum. It would be interesting to log more information |
| 120 | SkString configName; |
| 121 | if (FLAGS_msaa > 0) { |
| 122 | configName.appendf("msaa_%d", FLAGS_msaa); |
| 123 | } else { |
| 124 | configName.appendf("gpu"); |
| 125 | } |
| 126 | fResults->config(configName.c_str()); |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 127 | fResults->configOption("name", shortName); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 128 | SkASSERT(measurements.count()); |
| 129 | Stats stats(measurements); |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 130 | fResults->metric("min_ms", stats.min); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 131 | |
| 132 | // Print output |
| 133 | if (FLAGS_verbose) { |
| 134 | for (int i = 0; i < measurements.count(); i++) { |
| 135 | SkDebugf("%s ", HUMANIZE(measurements[i])); |
| 136 | } |
| 137 | SkDebugf("%s\n", shortName); |
| 138 | } else { |
| 139 | const double stdDevPercent = 100 * sqrt(stats.var) / stats.mean; |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 140 | 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] | 141 | sk_tools::getCurrResidentSetSizeMB(), |
| 142 | sk_tools::getMaxResidentSetSizeMB(), |
| 143 | fLoops, |
| 144 | HUMANIZE(stats.min), |
| 145 | HUMANIZE(stats.median), |
| 146 | HUMANIZE(stats.mean), |
| 147 | HUMANIZE(stats.max), |
| 148 | stdDevPercent, |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 149 | stats.plot.c_str(), |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 150 | shortName); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | bool VisualLightweightBenchModule::advanceRecordIfNecessary(SkCanvas* canvas) { |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 155 | if (!fBenchmark && fState == kWarmup_State) { |
joshualitt | 8db65a6 | 2015-09-24 12:42:02 -0700 | [diff] [blame] | 156 | fOwner->clear(canvas, SK_ColorWHITE, 2); |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 157 | fBenchmark.reset(new WarmupBench); |
| 158 | return true; |
| 159 | } |
| 160 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 161 | if (fBenchmark) { |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | fBenchmark.reset(fBenchmarkStream->next()); |
| 166 | if (!fBenchmark) { |
| 167 | return false; |
| 168 | } |
| 169 | |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 170 | fOwner->clear(canvas, SK_ColorWHITE, 2); |
| 171 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame^] | 172 | fBenchmark->delayedSetup(); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 173 | fRecords.push_back(); |
| 174 | |
| 175 | // Log bench name |
| 176 | fResults->bench(fBenchmark->getUniqueName(), fBenchmark->getSize().fX, |
| 177 | fBenchmark->getSize().fY); |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | inline void VisualLightweightBenchModule::nextState(State nextState) { |
| 182 | fState = nextState; |
| 183 | } |
| 184 | |
| 185 | void VisualLightweightBenchModule::perCanvasPreDraw(SkCanvas* canvas, State nextState) { |
| 186 | fBenchmark->perCanvasPreDraw(canvas); |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame^] | 187 | fBenchmark->preDraw(canvas); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 188 | fCurrentFrame = 0; |
| 189 | this->nextState(nextState); |
| 190 | } |
| 191 | |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 192 | void VisualLightweightBenchModule::warmup(SkCanvas* canvas) { |
| 193 | if (fCurrentFrame >= FLAGS_maxWarmupFrames) { |
| 194 | this->nextState(kPreWarmLoopsPerCanvasPreDraw_State); |
| 195 | fBenchmark.reset(nullptr); |
| 196 | this->resetTimingState(); |
| 197 | fLoops = 1; |
| 198 | } else { |
| 199 | bool isEven = (fCurrentFrame++ % 2) == 0; |
| 200 | if (isEven) { |
| 201 | fTimer.start(); |
| 202 | } else { |
| 203 | double elapsedMs = this->elapsed(); |
| 204 | if (elapsedMs < FLAGS_loopMs) { |
| 205 | fLoops *= 2; |
| 206 | } |
| 207 | fTimer = WallTimer(); |
| 208 | fOwner->reset(); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 213 | void VisualLightweightBenchModule::preWarm(State nextState) { |
| 214 | if (fCurrentFrame >= FLAGS_gpuFrameLag) { |
| 215 | // we currently time across all frames to make sure we capture all GPU work |
| 216 | this->nextState(nextState); |
| 217 | fCurrentFrame = 0; |
| 218 | fTimer.start(); |
| 219 | } else { |
| 220 | fCurrentFrame++; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void VisualLightweightBenchModule::draw(SkCanvas* canvas) { |
| 225 | if (!this->advanceRecordIfNecessary(canvas)) { |
| 226 | SkDebugf("Exiting VisualBench successfully\n"); |
| 227 | fOwner->closeWindow(); |
| 228 | return; |
| 229 | } |
| 230 | this->renderFrame(canvas); |
| 231 | switch (fState) { |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 232 | case kWarmup_State: { |
| 233 | this->warmup(canvas); |
| 234 | break; |
| 235 | } |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 236 | case kPreWarmLoopsPerCanvasPreDraw_State: { |
| 237 | this->perCanvasPreDraw(canvas, kPreWarmLoops_State); |
| 238 | break; |
| 239 | } |
| 240 | case kPreWarmLoops_State: { |
| 241 | this->preWarm(kTuneLoops_State); |
| 242 | break; |
| 243 | } |
| 244 | case kTuneLoops_State: { |
| 245 | this->tuneLoops(); |
| 246 | break; |
| 247 | } |
| 248 | case kPreWarmTimingPerCanvasPreDraw_State: { |
| 249 | this->perCanvasPreDraw(canvas, kPreWarmTiming_State); |
| 250 | break; |
| 251 | } |
| 252 | case kPreWarmTiming_State: { |
| 253 | this->preWarm(kTiming_State); |
| 254 | break; |
| 255 | } |
| 256 | case kTiming_State: { |
| 257 | this->timing(canvas); |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | inline double VisualLightweightBenchModule::elapsed() { |
| 264 | fTimer.end(); |
| 265 | return fTimer.fWall; |
| 266 | } |
| 267 | |
| 268 | void VisualLightweightBenchModule::resetTimingState() { |
| 269 | fCurrentFrame = 0; |
| 270 | fTimer = WallTimer(); |
| 271 | fOwner->reset(); |
| 272 | } |
| 273 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 274 | inline void VisualLightweightBenchModule::tuneLoops() { |
| 275 | if (1 << 30 == fLoops) { |
| 276 | // We're about to wrap. Something's wrong with the bench. |
| 277 | SkDebugf("InnerLoops wrapped\n"); |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 278 | fLoops = 1; |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 279 | } else { |
| 280 | double elapsedMs = this->elapsed(); |
| 281 | if (elapsedMs > FLAGS_loopMs) { |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 282 | this->nextState(kPreWarmTimingPerCanvasPreDraw_State); |
| 283 | } else { |
| 284 | fLoops *= 2; |
| 285 | this->nextState(kPreWarmLoops_State); |
| 286 | } |
| 287 | this->resetTimingState(); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void VisualLightweightBenchModule::recordMeasurement() { |
| 292 | double measurement = this->elapsed() / (FLAGS_frames * fLoops); |
| 293 | fRecords.back().fMeasurements.push_back(measurement); |
| 294 | } |
| 295 | |
| 296 | void VisualLightweightBenchModule::postDraw(SkCanvas* canvas) { |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame^] | 297 | fBenchmark->postDraw(canvas); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 298 | fBenchmark->perCanvasPostDraw(canvas); |
| 299 | fBenchmark.reset(nullptr); |
| 300 | fCurrentSample = 0; |
| 301 | fLoops = 1; |
| 302 | } |
| 303 | |
| 304 | inline void VisualLightweightBenchModule::timing(SkCanvas* canvas) { |
| 305 | if (fCurrentFrame >= FLAGS_frames) { |
| 306 | this->recordMeasurement(); |
| 307 | if (fCurrentSample++ >= FLAGS_samples) { |
| 308 | this->printStats(); |
| 309 | this->postDraw(canvas); |
| 310 | this->nextState(kPreWarmLoopsPerCanvasPreDraw_State); |
| 311 | } else { |
| 312 | this->nextState(kPreWarmTimingPerCanvasPreDraw_State); |
| 313 | } |
| 314 | this->resetTimingState(); |
| 315 | } else { |
| 316 | fCurrentFrame++; |
| 317 | } |
| 318 | } |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 319 | |
| 320 | bool VisualLightweightBenchModule::onHandleChar(SkUnichar c) { |
| 321 | return true; |
| 322 | } |