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