blob: 6f21c6804152a7928c32d9e7702d1d99528e747b [file] [log] [blame]
joshualitt189aef72015-09-08 07:08:11 -07001/*
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
joshualitt7d4b4582015-09-24 08:08:23 -070029DEFINE_int32(maxWarmupFrames, 100, "maxmium frames to try and tune for sane timings");
joshualitt189aef72015-09-08 07:08:11 -070030DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
31DEFINE_int32(samples, 10, "Number of times to time each skp.");
32DEFINE_int32(frames, 5, "Number of frames of each skp to render per sample.");
33DEFINE_double(loopMs, 5, "Target loop time in millseconds.");
34DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
joshualitt189aef72015-09-08 07:08:11 -070035DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtkleinca6f43b2015-09-15 13:29:20 -070036DEFINE_string(key, "",
37 "Space-separated key/value pairs to add to JSON identifying this builder.");
joshualitt189aef72015-09-08 07:08:11 -070038DEFINE_string(properties, "",
39 "Space-separated key/value pairs to add to JSON identifying this run.");
40
41static 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
joshualitt7d4b4582015-09-24 08:08:23 -070050// A trivial bench to warm up the gpu
51class WarmupBench : public Benchmark {
52public:
53private:
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
joshualitt189aef72015-09-08 07:08:11 -070062VisualLightweightBenchModule::VisualLightweightBenchModule(VisualBench* owner)
63 : fCurrentSample(0)
64 , fCurrentFrame(0)
65 , fLoops(1)
joshualitt7d4b4582015-09-24 08:08:23 -070066 , fState(kWarmup_State)
joshualitt189aef72015-09-08 07:08:11 -070067 , fBenchmark(nullptr)
68 , fOwner(SkRef(owner))
69 , fResults(new ResultsWriter) {
70 fBenchmarkStream.reset(new VisualBenchmarkStream);
71
72 // Print header
joshualitt7d4b4582015-09-24 08:08:23 -070073 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tbench\n", FLAGS_samples,
74 "samples");
joshualitt189aef72015-09-08 07:08:11 -070075
76 // setup json logging if required
77 if (!FLAGS_outResultsFile.isEmpty()) {
78 fResults.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
79 }
80
mtkleinca6f43b2015-09-15 13:29:20 -070081 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
joshualitt189aef72015-09-08 07:08:11 -070089 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
98inline void VisualLightweightBenchModule::renderFrame(SkCanvas* canvas) {
99 fBenchmark->draw(fLoops, canvas);
100 canvas->flush();
101 fOwner->present();
102}
103
104void 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());
joshualitt7d4b4582015-09-24 08:08:23 -0700117 fResults->configOption("name", shortName);
joshualitt189aef72015-09-08 07:08:11 -0700118 SkASSERT(measurements.count());
119 Stats stats(measurements);
joshualitt7d4b4582015-09-24 08:08:23 -0700120 fResults->metric("min_ms", stats.min);
joshualitt189aef72015-09-08 07:08:11 -0700121
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;
joshualitt7d4b4582015-09-24 08:08:23 -0700130 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\n",
joshualitt189aef72015-09-08 07:08:11 -0700131 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,
joshualitt7d4b4582015-09-24 08:08:23 -0700139 stats.plot.c_str(),
joshualitt189aef72015-09-08 07:08:11 -0700140 shortName);
141 }
142}
143
144bool VisualLightweightBenchModule::advanceRecordIfNecessary(SkCanvas* canvas) {
joshualitt7d4b4582015-09-24 08:08:23 -0700145 if (!fBenchmark && fState == kWarmup_State) {
146 fBenchmark.reset(new WarmupBench);
147 return true;
148 }
149
joshualitt189aef72015-09-08 07:08:11 -0700150 if (fBenchmark) {
151 return true;
152 }
153
154 fBenchmark.reset(fBenchmarkStream->next());
155 if (!fBenchmark) {
156 return false;
157 }
158
jvanverthf5d1b2d2015-09-15 07:40:56 -0700159 fOwner->clear(canvas, SK_ColorWHITE, 2);
160
joshualitt189aef72015-09-08 07:08:11 -0700161 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
170inline void VisualLightweightBenchModule::nextState(State nextState) {
171 fState = nextState;
172}
173
174void VisualLightweightBenchModule::perCanvasPreDraw(SkCanvas* canvas, State nextState) {
175 fBenchmark->perCanvasPreDraw(canvas);
176 fCurrentFrame = 0;
177 this->nextState(nextState);
178}
179
joshualitt7d4b4582015-09-24 08:08:23 -0700180void 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
joshualitt189aef72015-09-08 07:08:11 -0700201void 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
212void 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) {
joshualitt7d4b4582015-09-24 08:08:23 -0700220 case kWarmup_State: {
221 this->warmup(canvas);
222 break;
223 }
joshualitt189aef72015-09-08 07:08:11 -0700224 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
251inline double VisualLightweightBenchModule::elapsed() {
252 fTimer.end();
253 return fTimer.fWall;
254}
255
256void VisualLightweightBenchModule::resetTimingState() {
257 fCurrentFrame = 0;
258 fTimer = WallTimer();
259 fOwner->reset();
260}
261
262void VisualLightweightBenchModule::scaleLoops(double elapsedMs) {
263 // Scale back the number of loops
264 fLoops = (int)ceil(fLoops * FLAGS_loopMs / elapsedMs);
265}
266
267inline 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");
joshualitt7d4b4582015-09-24 08:08:23 -0700271 fLoops = 1;
joshualitt189aef72015-09-08 07:08:11 -0700272 } 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
285void VisualLightweightBenchModule::recordMeasurement() {
286 double measurement = this->elapsed() / (FLAGS_frames * fLoops);
287 fRecords.back().fMeasurements.push_back(measurement);
288}
289
290void VisualLightweightBenchModule::postDraw(SkCanvas* canvas) {
291 fBenchmark->perCanvasPostDraw(canvas);
292 fBenchmark.reset(nullptr);
293 fCurrentSample = 0;
294 fLoops = 1;
295}
296
297inline 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}
jvanverthf5d1b2d2015-09-15 07:40:56 -0700312
313bool VisualLightweightBenchModule::onHandleChar(SkUnichar c) {
314 return true;
315}