blob: d5e4e5b8b632eac99cde888cd17f7781808771f8 [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
joshualitt8db65a62015-09-24 12:42:02 -070050// We draw a big nonAA path to warmup the gpu / cpu
joshualitt7d4b4582015-09-24 08:08:23 -070051class WarmupBench : public Benchmark {
52public:
joshualitt8db65a62015-09-24 12:42:02 -070053 WarmupBench() {
54 make_path(fPath);
55 }
joshualitt7d4b4582015-09-24 08:08:23 -070056private:
joshualitt8db65a62015-09-24 12:42:02 -070057 static void make_path(SkPath& path) {
58 #include "BigPathBench.inc"
59 }
joshualitt7d4b4582015-09-24 08:08:23 -070060 const char* onGetName() override { return "warmupbench"; }
61 void onDraw(const int loops, SkCanvas* canvas) override {
joshualitt8db65a62015-09-24 12:42:02 -070062 SkPaint paint;
63 paint.setStyle(SkPaint::kStroke_Style);
64 paint.setStrokeWidth(2);
joshualitt7d4b4582015-09-24 08:08:23 -070065 for (int i = 0; i < loops; i++) {
joshualitt8db65a62015-09-24 12:42:02 -070066 canvas->drawPath(fPath, paint);
joshualitt7d4b4582015-09-24 08:08:23 -070067 }
68 }
joshualitt8db65a62015-09-24 12:42:02 -070069 SkPath fPath;
joshualitt7d4b4582015-09-24 08:08:23 -070070};
71
joshualitt189aef72015-09-08 07:08:11 -070072VisualLightweightBenchModule::VisualLightweightBenchModule(VisualBench* owner)
73 : fCurrentSample(0)
74 , fCurrentFrame(0)
75 , fLoops(1)
joshualitt7d4b4582015-09-24 08:08:23 -070076 , fState(kWarmup_State)
joshualitt189aef72015-09-08 07:08:11 -070077 , fBenchmark(nullptr)
78 , fOwner(SkRef(owner))
79 , fResults(new ResultsWriter) {
80 fBenchmarkStream.reset(new VisualBenchmarkStream);
81
82 // Print header
joshualitt7d4b4582015-09-24 08:08:23 -070083 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tbench\n", FLAGS_samples,
84 "samples");
joshualitt189aef72015-09-08 07:08:11 -070085
86 // setup json logging if required
87 if (!FLAGS_outResultsFile.isEmpty()) {
88 fResults.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
89 }
90
mtkleinca6f43b2015-09-15 13:29:20 -070091 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
joshualitt189aef72015-09-08 07:08:11 -070099 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
108inline void VisualLightweightBenchModule::renderFrame(SkCanvas* canvas) {
109 fBenchmark->draw(fLoops, canvas);
110 canvas->flush();
111 fOwner->present();
112}
113
114void 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());
joshualitt7d4b4582015-09-24 08:08:23 -0700127 fResults->configOption("name", shortName);
joshualitt189aef72015-09-08 07:08:11 -0700128 SkASSERT(measurements.count());
129 Stats stats(measurements);
joshualitt7d4b4582015-09-24 08:08:23 -0700130 fResults->metric("min_ms", stats.min);
joshualitt189aef72015-09-08 07:08:11 -0700131
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;
joshualitt7d4b4582015-09-24 08:08:23 -0700140 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 -0700141 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,
joshualitt7d4b4582015-09-24 08:08:23 -0700149 stats.plot.c_str(),
joshualitt189aef72015-09-08 07:08:11 -0700150 shortName);
151 }
152}
153
154bool VisualLightweightBenchModule::advanceRecordIfNecessary(SkCanvas* canvas) {
joshualitt7d4b4582015-09-24 08:08:23 -0700155 if (!fBenchmark && fState == kWarmup_State) {
joshualitt8db65a62015-09-24 12:42:02 -0700156 fOwner->clear(canvas, SK_ColorWHITE, 2);
joshualitt7d4b4582015-09-24 08:08:23 -0700157 fBenchmark.reset(new WarmupBench);
158 return true;
159 }
160
joshualitt189aef72015-09-08 07:08:11 -0700161 if (fBenchmark) {
162 return true;
163 }
164
165 fBenchmark.reset(fBenchmarkStream->next());
166 if (!fBenchmark) {
167 return false;
168 }
169
jvanverthf5d1b2d2015-09-15 07:40:56 -0700170 fOwner->clear(canvas, SK_ColorWHITE, 2);
171
joshualitt189aef72015-09-08 07:08:11 -0700172 fBenchmark->preDraw();
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
181inline void VisualLightweightBenchModule::nextState(State nextState) {
182 fState = nextState;
183}
184
185void VisualLightweightBenchModule::perCanvasPreDraw(SkCanvas* canvas, State nextState) {
186 fBenchmark->perCanvasPreDraw(canvas);
187 fCurrentFrame = 0;
188 this->nextState(nextState);
189}
190
joshualitt7d4b4582015-09-24 08:08:23 -0700191void VisualLightweightBenchModule::warmup(SkCanvas* canvas) {
192 if (fCurrentFrame >= FLAGS_maxWarmupFrames) {
193 this->nextState(kPreWarmLoopsPerCanvasPreDraw_State);
194 fBenchmark.reset(nullptr);
195 this->resetTimingState();
196 fLoops = 1;
197 } else {
198 bool isEven = (fCurrentFrame++ % 2) == 0;
199 if (isEven) {
200 fTimer.start();
201 } else {
202 double elapsedMs = this->elapsed();
203 if (elapsedMs < FLAGS_loopMs) {
204 fLoops *= 2;
205 }
206 fTimer = WallTimer();
207 fOwner->reset();
208 }
209 }
210}
211
joshualitt189aef72015-09-08 07:08:11 -0700212void VisualLightweightBenchModule::preWarm(State nextState) {
213 if (fCurrentFrame >= FLAGS_gpuFrameLag) {
214 // we currently time across all frames to make sure we capture all GPU work
215 this->nextState(nextState);
216 fCurrentFrame = 0;
217 fTimer.start();
218 } else {
219 fCurrentFrame++;
220 }
221}
222
223void VisualLightweightBenchModule::draw(SkCanvas* canvas) {
224 if (!this->advanceRecordIfNecessary(canvas)) {
225 SkDebugf("Exiting VisualBench successfully\n");
226 fOwner->closeWindow();
227 return;
228 }
229 this->renderFrame(canvas);
230 switch (fState) {
joshualitt7d4b4582015-09-24 08:08:23 -0700231 case kWarmup_State: {
232 this->warmup(canvas);
233 break;
234 }
joshualitt189aef72015-09-08 07:08:11 -0700235 case kPreWarmLoopsPerCanvasPreDraw_State: {
236 this->perCanvasPreDraw(canvas, kPreWarmLoops_State);
237 break;
238 }
239 case kPreWarmLoops_State: {
240 this->preWarm(kTuneLoops_State);
241 break;
242 }
243 case kTuneLoops_State: {
244 this->tuneLoops();
245 break;
246 }
247 case kPreWarmTimingPerCanvasPreDraw_State: {
248 this->perCanvasPreDraw(canvas, kPreWarmTiming_State);
249 break;
250 }
251 case kPreWarmTiming_State: {
252 this->preWarm(kTiming_State);
253 break;
254 }
255 case kTiming_State: {
256 this->timing(canvas);
257 break;
258 }
259 }
260}
261
262inline double VisualLightweightBenchModule::elapsed() {
263 fTimer.end();
264 return fTimer.fWall;
265}
266
267void VisualLightweightBenchModule::resetTimingState() {
268 fCurrentFrame = 0;
269 fTimer = WallTimer();
270 fOwner->reset();
271}
272
joshualitt189aef72015-09-08 07:08:11 -0700273inline void VisualLightweightBenchModule::tuneLoops() {
274 if (1 << 30 == fLoops) {
275 // We're about to wrap. Something's wrong with the bench.
276 SkDebugf("InnerLoops wrapped\n");
joshualitt7d4b4582015-09-24 08:08:23 -0700277 fLoops = 1;
joshualitt189aef72015-09-08 07:08:11 -0700278 } else {
279 double elapsedMs = this->elapsed();
280 if (elapsedMs > FLAGS_loopMs) {
joshualitt189aef72015-09-08 07:08:11 -0700281 this->nextState(kPreWarmTimingPerCanvasPreDraw_State);
282 } else {
283 fLoops *= 2;
284 this->nextState(kPreWarmLoops_State);
285 }
286 this->resetTimingState();
287 }
288}
289
290void VisualLightweightBenchModule::recordMeasurement() {
291 double measurement = this->elapsed() / (FLAGS_frames * fLoops);
292 fRecords.back().fMeasurements.push_back(measurement);
293}
294
295void VisualLightweightBenchModule::postDraw(SkCanvas* canvas) {
296 fBenchmark->perCanvasPostDraw(canvas);
297 fBenchmark.reset(nullptr);
298 fCurrentSample = 0;
299 fLoops = 1;
300}
301
302inline void VisualLightweightBenchModule::timing(SkCanvas* canvas) {
303 if (fCurrentFrame >= FLAGS_frames) {
304 this->recordMeasurement();
305 if (fCurrentSample++ >= FLAGS_samples) {
306 this->printStats();
307 this->postDraw(canvas);
308 this->nextState(kPreWarmLoopsPerCanvasPreDraw_State);
309 } else {
310 this->nextState(kPreWarmTimingPerCanvasPreDraw_State);
311 }
312 this->resetTimingState();
313 } else {
314 fCurrentFrame++;
315 }
316}
jvanverthf5d1b2d2015-09-15 07:40:56 -0700317
318bool VisualLightweightBenchModule::onHandleChar(SkUnichar c) {
319 return true;
320}