1 /*
2 * Copyright 2014 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 #include <ctype.h>
9
10 #include "bench/nanobench.h"
11
12 #include "bench/AndroidCodecBench.h"
13 #include "bench/Benchmark.h"
14 #include "bench/CodecBench.h"
15 #include "bench/CodecBenchPriv.h"
16 #include "bench/GMBench.h"
17 #include "bench/MSKPBench.h"
18 #include "bench/RecordingBench.h"
19 #include "bench/ResultsWriter.h"
20 #include "bench/SKPAnimationBench.h"
21 #include "bench/SKPBench.h"
22 #include "bench/SkGlyphCacheBench.h"
23 #include "bench/SkSLBench.h"
24 #include "include/codec/SkAndroidCodec.h"
25 #include "include/codec/SkCodec.h"
26 #include "include/core/SkCanvas.h"
27 #include "include/core/SkData.h"
28 #include "include/core/SkGraphics.h"
29 #include "include/core/SkPictureRecorder.h"
30 #include "include/core/SkString.h"
31 #include "include/core/SkSurface.h"
32 #include "include/core/SkTime.h"
33 #include "src/core/SkAutoMalloc.h"
34 #include "src/core/SkColorSpacePriv.h"
35 #include "src/core/SkLeanWindows.h"
36 #include "src/core/SkOSFile.h"
37 #include "src/core/SkTaskGroup.h"
38 #include "src/core/SkTraceEvent.h"
39 #include "src/gpu/GrShaderUtils.h"
40 #include "src/utils/SkJSONWriter.h"
41 #include "src/utils/SkOSPath.h"
42 #include "tools/AutoreleasePool.h"
43 #include "tools/CrashHandler.h"
44 #include "tools/MSKPPlayer.h"
45 #include "tools/ProcStats.h"
46 #include "tools/Stats.h"
47 #include "tools/flags/CommonFlags.h"
48 #include "tools/flags/CommonFlagsConfig.h"
49 #include "tools/ios_utils.h"
50 #include "tools/trace/EventTracingPriv.h"
51 #include "tools/trace/SkDebugfTracer.h"
52
53 #ifdef SK_XML
54 #include "modules/svg/include/SkSVGDOM.h"
55 #endif // SK_XML
56
57 #ifdef SK_ENABLE_ANDROID_UTILS
58 #include "bench/BitmapRegionDecoderBench.h"
59 #include "client_utils/android/BitmapRegionDecoder.h"
60 #endif
61
62 #include <cinttypes>
63 #include <stdlib.h>
64 #include <memory>
65 #include <thread>
66
67 extern bool gSkForceRasterPipelineBlitter;
68 extern bool gUseSkVMBlitter;
69 extern bool gSkVMAllowJIT;
70 extern bool gSkVMJITViaDylib;
71
72 #ifndef SK_BUILD_FOR_WIN
73 #include <unistd.h>
74
75 #endif
76
77 #include "include/gpu/GrDirectContext.h"
78 #include "src/gpu/GrCaps.h"
79 #include "src/gpu/GrDirectContextPriv.h"
80 #include "src/gpu/SkGr.h"
81 #include "src/gpu/gl/GrGLDefines.h"
82 #include "src/gpu/gl/GrGLGpu.h"
83 #include "src/gpu/gl/GrGLUtil.h"
84 #include "tools/gpu/GrContextFactory.h"
85
86 using sk_gpu_test::ContextInfo;
87 using sk_gpu_test::GrContextFactory;
88 using sk_gpu_test::TestContext;
89
90 GrContextOptions grContextOpts;
91
92 static const int kAutoTuneLoops = 0;
93
loops_help_txt()94 static SkString loops_help_txt() {
95 SkString help;
96 help.printf("Number of times to run each bench. Set this to %d to auto-"
97 "tune for each bench. Timings are only reported when auto-tuning.",
98 kAutoTuneLoops);
99 return help;
100 }
101
to_string(int n)102 static SkString to_string(int n) {
103 SkString str;
104 str.appendS32(n);
105 return str;
106 }
107
108 static DEFINE_int(loops, kAutoTuneLoops, loops_help_txt().c_str());
109
110 static DEFINE_int(samples, 10, "Number of samples to measure for each bench.");
111 static DEFINE_int(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
112 static DEFINE_int(overheadLoops, 100000, "Loops to estimate timer overhead.");
113 static DEFINE_double(overheadGoal, 0.0001,
114 "Loop until timer overhead is at most this fraction of our measurments.");
115 static DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
116 static DEFINE_int(gpuFrameLag, 5,
117 "If unknown, estimated maximum number of frames GPU allows to lag.");
118
119 static DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
120 static DEFINE_int(maxCalibrationAttempts, 3,
121 "Try up to this many times to guess loops for a bench, or skip the bench.");
122 static DEFINE_int(maxLoops, 1000000, "Never run a bench more times than this.");
123 static DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
124 static DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
125 static DEFINE_string(zoom, "1.0,0",
126 "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
127 "function that ping-pongs between 1.0 and zoomMax.");
128 static DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
129 static DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
130 static DEFINE_int(flushEvery, 10, "Flush --outResultsFile every Nth run.");
131 static DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
132 static DEFINE_bool(gpuStatsDump, false, "Dump GPU stats after each benchmark to json");
133 static DEFINE_bool(dmsaaStatsDump, false, "Dump DMSAA stats after each benchmark to json");
134 static DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
135 static DEFINE_bool(csv, false, "Print status in CSV format");
136 static DEFINE_string(sourceType, "",
137 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
138 static DEFINE_string(benchType, "",
139 "Apply usual --match rules to bench type: micro, recording, "
140 "piping, playback, skcodec, etc.");
141
142 static DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
143 static DEFINE_bool(skvm, false, "sets gUseSkVMBlitter");
144 static DEFINE_bool(jit, true, "JIT SkVM?");
145 static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
146
147 static DEFINE_bool2(pre_log, p, false,
148 "Log before running each test. May be incomprehensible when threading");
149
150 static DEFINE_bool(cpu, true, "Run CPU-bound work?");
151 static DEFINE_bool(gpu, true, "Run GPU-bound work?");
152 static DEFINE_bool(dryRun, false,
153 "just print the tests that would be run, without actually running them.");
154 static DEFINE_string(images, "",
155 "List of images and/or directories to decode. A directory with no images"
156 " is treated as a fatal error.");
157 static DEFINE_bool(simpleCodec, false,
158 "Runs of a subset of the codec tests, always N32, Premul or Opaque");
159
160 static DEFINE_string2(match, m, nullptr,
161 "[~][^]substring[$] [...] of name to run.\n"
162 "Multiple matches may be separated by spaces.\n"
163 "~ causes a matching name to always be skipped\n"
164 "^ requires the start of the name to match\n"
165 "$ requires the end of the name to match\n"
166 "^ and $ requires an exact match\n"
167 "If a name does not match any list entry,\n"
168 "it is skipped unless some list entry starts with ~");
169
170 static DEFINE_bool2(quiet, q, false, "if true, don't print status updates.");
171 static DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
172
173
174 static DEFINE_string(skps, "skps", "Directory to read skps from.");
175 static DEFINE_string(mskps, "mskps", "Directory to read mskps from.");
176 static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
177 static DEFINE_string(texttraces, "", "Directory to read TextBlobTrace files from.");
178
179 static DEFINE_int_2(threads, j, -1,
180 "Run threadsafe tests on a threadpool with this many extra threads, "
181 "defaulting to one extra thread per core.");
182
183 static DEFINE_string2(writePath, w, "", "If set, write bitmaps here as .pngs.");
184
185 static DEFINE_string(key, "",
186 "Space-separated key/value pairs to add to JSON identifying this builder.");
187 static DEFINE_string(properties, "",
188 "Space-separated key/value pairs to add to JSON identifying this run.");
189
190 static DEFINE_bool(purgeBetweenBenches, false,
191 "Call SkGraphics::PurgeAllCaches() between each benchmark?");
192
now_ms()193 static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
194
humanize(double ms)195 static SkString humanize(double ms) {
196 if (FLAGS_verbose) return SkStringPrintf("%" PRIu64, (uint64_t)(ms*1e6));
197 return HumanizeMs(ms);
198 }
199 #define HUMANIZE(ms) humanize(ms).c_str()
200
init(SkImageInfo info,Benchmark * bench)201 bool Target::init(SkImageInfo info, Benchmark* bench) {
202 if (Benchmark::kRaster_Backend == config.backend) {
203 this->surface = SkSurface::MakeRaster(info);
204 if (!this->surface) {
205 return false;
206 }
207 }
208 return true;
209 }
capturePixels(SkBitmap * bmp)210 bool Target::capturePixels(SkBitmap* bmp) {
211 SkCanvas* canvas = this->getCanvas();
212 if (!canvas) {
213 return false;
214 }
215 bmp->allocPixels(canvas->imageInfo());
216 if (!canvas->readPixels(*bmp, 0, 0)) {
217 SkDebugf("Can't read canvas pixels.\n");
218 return false;
219 }
220 return true;
221 }
222
223 struct GPUTarget : public Target {
GPUTargetGPUTarget224 explicit GPUTarget(const Config& c) : Target(c) {}
225 ContextInfo contextInfo;
226 std::unique_ptr<GrContextFactory> factory;
227
~GPUTargetGPUTarget228 ~GPUTarget() override {
229 // For Vulkan we need to release all our refs to the GrContext before destroy the vulkan
230 // context which happens at the end of this destructor. Thus we need to release the surface
231 // here which holds a ref to the GrContext.
232 surface.reset();
233 }
234
setupGPUTarget235 void setup() override {
236 this->contextInfo.testContext()->makeCurrent();
237 // Make sure we're done with whatever came before.
238 this->contextInfo.testContext()->finish();
239 }
endTimingGPUTarget240 void endTiming() override {
241 if (this->contextInfo.testContext()) {
242 this->contextInfo.testContext()->flushAndWaitOnSync(contextInfo.directContext());
243 }
244 }
fenceGPUTarget245 void fence() override { this->contextInfo.testContext()->finish(); }
246
needsFrameTimingGPUTarget247 bool needsFrameTiming(int* maxFrameLag) const override {
248 if (!this->contextInfo.testContext()->getMaxGpuFrameLag(maxFrameLag)) {
249 // Frame lag is unknown.
250 *maxFrameLag = FLAGS_gpuFrameLag;
251 }
252 return true;
253 }
initGPUTarget254 bool init(SkImageInfo info, Benchmark* bench) override {
255 GrContextOptions options = grContextOpts;
256 bench->modifyGrContextOptions(&options);
257 this->factory = std::make_unique<GrContextFactory>(options);
258 SkSurfaceProps props(this->config.surfaceFlags, kRGB_H_SkPixelGeometry);
259 this->surface = SkSurface::MakeRenderTarget(
260 this->factory->get(this->config.ctxType, this->config.ctxOverrides),
261 SkBudgeted::kNo, info, this->config.samples, &props);
262 this->contextInfo =
263 this->factory->getContextInfo(this->config.ctxType, this->config.ctxOverrides);
264 if (!this->surface) {
265 return false;
266 }
267 if (!this->contextInfo.testContext()->fenceSyncSupport()) {
268 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
269 "Timings might not be accurate.\n", this->config.name.c_str());
270 }
271 return true;
272 }
fillOptionsGPUTarget273 void fillOptions(NanoJSONResultsWriter& log) override {
274 #ifdef SK_GL
275 const GrGLubyte* version;
276 if (this->contextInfo.backend() == GrBackendApi::kOpenGL) {
277 const GrGLInterface* gl =
278 static_cast<GrGLGpu*>(this->contextInfo.directContext()->priv().getGpu())
279 ->glInterface();
280 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
281 log.appendString("GL_VERSION", (const char*)(version));
282
283 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
284 log.appendString("GL_RENDERER", (const char*) version);
285
286 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
287 log.appendString("GL_VENDOR", (const char*) version);
288
289 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
290 log.appendString("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
291 }
292 #endif
293 }
294
dumpStatsGPUTarget295 void dumpStats() override {
296 auto context = this->contextInfo.directContext();
297
298 context->priv().printCacheStats();
299 context->priv().printGpuStats();
300 context->priv().printContextStats();
301 }
302 };
303
time(int loops,Benchmark * bench,Target * target)304 static double time(int loops, Benchmark* bench, Target* target) {
305 SkCanvas* canvas = target->getCanvas();
306 if (canvas) {
307 canvas->clear(SK_ColorWHITE);
308 }
309 bench->preDraw(canvas);
310 double start = now_ms();
311 canvas = target->beginTiming(canvas);
312 bench->draw(loops, canvas);
313 target->endTiming();
314 double elapsed = now_ms() - start;
315 bench->postDraw(canvas);
316 return elapsed;
317 }
318
estimate_timer_overhead()319 static double estimate_timer_overhead() {
320 double overhead = 0;
321 for (int i = 0; i < FLAGS_overheadLoops; i++) {
322 double start = now_ms();
323 overhead += now_ms() - start;
324 }
325 return overhead / FLAGS_overheadLoops;
326 }
327
detect_forever_loops(int loops)328 static int detect_forever_loops(int loops) {
329 // look for a magic run-forever value
330 if (loops < 0) {
331 loops = SK_MaxS32;
332 }
333 return loops;
334 }
335
clamp_loops(int loops)336 static int clamp_loops(int loops) {
337 if (loops < 1) {
338 SkDebugf("ERROR: clamping loops from %d to 1. "
339 "There's probably something wrong with the bench.\n", loops);
340 return 1;
341 }
342 if (loops > FLAGS_maxLoops) {
343 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
344 return FLAGS_maxLoops;
345 }
346 return loops;
347 }
348
write_canvas_png(Target * target,const SkString & filename)349 static bool write_canvas_png(Target* target, const SkString& filename) {
350
351 if (filename.isEmpty()) {
352 return false;
353 }
354 if (target->getCanvas() &&
355 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
356 return false;
357 }
358
359 SkBitmap bmp;
360
361 if (!target->capturePixels(&bmp)) {
362 return false;
363 }
364
365 SkString dir = SkOSPath::Dirname(filename.c_str());
366 if (!sk_mkdir(dir.c_str())) {
367 SkDebugf("Can't make dir %s.\n", dir.c_str());
368 return false;
369 }
370 SkFILEWStream stream(filename.c_str());
371 if (!stream.isValid()) {
372 SkDebugf("Can't write %s.\n", filename.c_str());
373 return false;
374 }
375 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
376 SkDebugf("Can't encode a PNG.\n");
377 return false;
378 }
379 return true;
380 }
381
382 static int kFailedLoops = -2;
setup_cpu_bench(const double overhead,Target * target,Benchmark * bench)383 static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
384 // First figure out approximately how many loops of bench it takes to make overhead negligible.
385 double bench_plus_overhead = 0.0;
386 int round = 0;
387 int loops = bench->calculateLoops(FLAGS_loops);
388 if (kAutoTuneLoops == loops) {
389 while (bench_plus_overhead < overhead) {
390 if (round++ == FLAGS_maxCalibrationAttempts) {
391 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
392 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
393 return kFailedLoops;
394 }
395 bench_plus_overhead = time(1, bench, target);
396 }
397 }
398
399 // Later we'll just start and stop the timer once but loop N times.
400 // We'll pick N to make timer overhead negligible:
401 //
402 // overhead
403 // ------------------------- < FLAGS_overheadGoal
404 // overhead + N * Bench Time
405 //
406 // where bench_plus_overhead ~=~ overhead + Bench Time.
407 //
408 // Doing some math, we get:
409 //
410 // (overhead / FLAGS_overheadGoal) - overhead
411 // ------------------------------------------ < N
412 // bench_plus_overhead - overhead)
413 //
414 // Luckily, this also works well in practice. :)
415 if (kAutoTuneLoops == loops) {
416 const double numer = overhead / FLAGS_overheadGoal - overhead;
417 const double denom = bench_plus_overhead - overhead;
418 loops = (int)ceil(numer / denom);
419 loops = clamp_loops(loops);
420 } else {
421 loops = detect_forever_loops(loops);
422 }
423
424 return loops;
425 }
426
setup_gpu_bench(Target * target,Benchmark * bench,int maxGpuFrameLag)427 static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
428 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
429 int loops = bench->calculateLoops(FLAGS_loops);
430 if (kAutoTuneLoops == loops) {
431 loops = 1;
432 double elapsed = 0;
433 do {
434 if (1<<30 == loops) {
435 // We're about to wrap. Something's wrong with the bench.
436 loops = 0;
437 break;
438 }
439 loops *= 2;
440 // If the GPU lets frames lag at all, we need to make sure we're timing
441 // _this_ round, not still timing last round.
442 for (int i = 0; i < maxGpuFrameLag; i++) {
443 elapsed = time(loops, bench, target);
444 }
445 } while (elapsed < FLAGS_gpuMs);
446
447 // We've overshot at least a little. Scale back linearly.
448 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
449 loops = clamp_loops(loops);
450
451 // Make sure we're not still timing our calibration.
452 target->fence();
453 } else {
454 loops = detect_forever_loops(loops);
455 }
456 // Pretty much the same deal as the calibration: do some warmup to make
457 // sure we're timing steady-state pipelined frames.
458 for (int i = 0; i < maxGpuFrameLag; i++) {
459 time(loops, bench, target);
460 }
461
462 return loops;
463 }
464
465 #define kBogusContextType GrContextFactory::kGL_ContextType
466 #define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
467
create_config(const SkCommandLineConfig * config,SkTArray<Config> * configs)468 static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
469 if (const auto* gpuConfig = config->asConfigGpu()) {
470 if (!FLAGS_gpu) {
471 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str());
472 return;
473 }
474
475 const auto ctxType = gpuConfig->getContextType();
476 const auto ctxOverrides = gpuConfig->getContextOverrides();
477 const auto sampleCount = gpuConfig->getSamples();
478 const auto colorType = gpuConfig->getColorType();
479 auto colorSpace = gpuConfig->getColorSpace();
480 if (gpuConfig->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) {
481 SkDebugf("This tool only supports the default surface type.");
482 return;
483 }
484
485 GrContextFactory factory(grContextOpts);
486 if (const auto ctx = factory.get(ctxType, ctxOverrides)) {
487 GrBackendFormat format = ctx->defaultBackendFormat(colorType, GrRenderable::kYes);
488 int supportedSampleCount =
489 ctx->priv().caps()->getRenderTargetSampleCount(sampleCount, format);
490 if (sampleCount != supportedSampleCount) {
491 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
492 config->getTag().c_str(), sampleCount);
493 return;
494 }
495 } else {
496 SkDebugf("No context was available matching config '%s'.\n",
497 config->getTag().c_str());
498 return;
499 }
500
501 Config target = {
502 gpuConfig->getTag(),
503 Benchmark::kGPU_Backend,
504 colorType,
505 kPremul_SkAlphaType,
506 sk_ref_sp(colorSpace),
507 sampleCount,
508 ctxType,
509 ctxOverrides,
510 gpuConfig->getSurfaceFlags()
511 };
512
513 configs->push_back(target);
514 return;
515 }
516
517 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
518 if (config->getTag().equals(#name)) { \
519 if (!FLAGS_cpu) { \
520 SkDebugf("Skipping config '%s' as requested.\n", \
521 config->getTag().c_str()); \
522 return; \
523 } \
524 Config config = { \
525 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
526 0, kBogusContextType, kBogusContextOverrides, 0 \
527 }; \
528 configs->push_back(config); \
529 return; \
530 }
531
532 CPU_CONFIG(nonrendering, kNonRendering_Backend,
533 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
534
535 CPU_CONFIG(a8, kRaster_Backend, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr)
536 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType, nullptr)
537 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
538
539 // 'narrow' has a gamut narrower than sRGB, and different transfer function.
540 auto narrow = SkColorSpace::MakeRGB(SkNamedTransferFn::k2Dot2, gNarrow_toXYZD50),
541 srgb = SkColorSpace::MakeSRGB(),
542 srgbLinear = SkColorSpace::MakeSRGBLinear();
543
544 CPU_CONFIG( f16, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinear)
545 CPU_CONFIG( srgb, kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType, srgb )
546 CPU_CONFIG( esrgb, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgb )
547 CPU_CONFIG( narrow, kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType, narrow )
548 CPU_CONFIG(enarrow, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, narrow )
549
550 #undef CPU_CONFIG
551
552 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
553 }
554
555 // Append all configs that are enabled and supported.
create_configs(SkTArray<Config> * configs)556 void create_configs(SkTArray<Config>* configs) {
557 SkCommandLineConfigArray array;
558 ParseConfigs(FLAGS_config, &array);
559 for (int i = 0; i < array.count(); ++i) {
560 create_config(array[i].get(), configs);
561 }
562
563 // If no just default configs were requested, then we're okay.
564 if (array.count() == 0 || FLAGS_config.count() == 0 ||
565 // Otherwise, make sure that all specified configs have been created.
566 array.count() == configs->count()) {
567 return;
568 }
569 exit(1);
570 }
571
572 // disable warning : switch statement contains default but no 'case' labels
573 #if defined _WIN32
574 #pragma warning ( push )
575 #pragma warning ( disable : 4065 )
576 #endif
577
578 // If bench is enabled for config, returns a Target* for it, otherwise nullptr.
is_enabled(Benchmark * bench,const Config & config)579 static Target* is_enabled(Benchmark* bench, const Config& config) {
580 if (!bench->isSuitableFor(config.backend)) {
581 return nullptr;
582 }
583
584 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
585 config.color, config.alpha, config.colorSpace);
586
587 Target* target = nullptr;
588
589 switch (config.backend) {
590 case Benchmark::kGPU_Backend:
591 target = new GPUTarget(config);
592 break;
593 default:
594 target = new Target(config);
595 break;
596 }
597
598 if (!target->init(info, bench)) {
599 delete target;
600 return nullptr;
601 }
602 return target;
603 }
604
605 #if defined _WIN32
606 #pragma warning ( pop )
607 #endif
608
609 #ifdef SK_ENABLE_ANDROID_UTILS
valid_brd_bench(sk_sp<SkData> encoded,SkColorType colorType,uint32_t sampleSize,uint32_t minOutputSize,int * width,int * height)610 static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
611 uint32_t minOutputSize, int* width, int* height) {
612 auto brd = android::skia::BitmapRegionDecoder::Make(encoded);
613 if (nullptr == brd) {
614 // This is indicates that subset decoding is not supported for a particular image format.
615 return false;
616 }
617
618 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
619 (uint32_t) brd->height()) {
620 // This indicates that the image is not large enough to decode a
621 // minOutputSize x minOutputSize subset at the given sampleSize.
622 return false;
623 }
624
625 // Set the image width and height. The calling code will use this to choose subsets to decode.
626 *width = brd->width();
627 *height = brd->height();
628 return true;
629 }
630 #endif
631
cleanup_run(Target * target)632 static void cleanup_run(Target* target) {
633 delete target;
634 }
635
collect_files(const CommandLineFlags::StringArray & paths,const char * ext,SkTArray<SkString> * list)636 static void collect_files(const CommandLineFlags::StringArray& paths,
637 const char* ext,
638 SkTArray<SkString>* list) {
639 for (int i = 0; i < paths.count(); ++i) {
640 if (SkStrEndsWith(paths[i], ext)) {
641 list->push_back(SkString(paths[i]));
642 } else {
643 SkOSFile::Iter it(paths[i], ext);
644 SkString path;
645 while (it.next(&path)) {
646 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
647 }
648 }
649 }
650 }
651
652 class BenchmarkStream {
653 public:
BenchmarkStream()654 BenchmarkStream() : fBenches(BenchRegistry::Head())
655 , fGMs(skiagm::GMRegistry::Head()) {
656 collect_files(FLAGS_skps, ".skp", &fSKPs);
657 collect_files(FLAGS_mskps, ".mskp", &fMSKPs);
658 collect_files(FLAGS_svgs, ".svg", &fSVGs);
659 collect_files(FLAGS_texttraces, ".trace", &fTextBlobTraces);
660
661 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
662 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
663 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
664 exit(1);
665 }
666
667 for (int i = 0; i < FLAGS_scales.count(); i++) {
668 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
669 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
670 exit(1);
671 }
672 }
673
674 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
675 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
676 exit(1);
677 }
678
679 // Prepare the images for decoding
680 if (!CollectImages(FLAGS_images, &fImages)) {
681 exit(1);
682 }
683
684 // Choose the candidate color types for image decoding
685 fColorTypes.push_back(kN32_SkColorType);
686 if (!FLAGS_simpleCodec) {
687 fColorTypes.push_back(kRGB_565_SkColorType);
688 fColorTypes.push_back(kAlpha_8_SkColorType);
689 fColorTypes.push_back(kGray_8_SkColorType);
690 }
691 }
692
ReadPicture(const char * path)693 static sk_sp<SkPicture> ReadPicture(const char* path) {
694 // Not strictly necessary, as it will be checked again later,
695 // but helps to avoid a lot of pointless work if we're going to skip it.
696 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
697 return nullptr;
698 }
699
700 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
701 if (!stream) {
702 SkDebugf("Could not read %s.\n", path);
703 return nullptr;
704 }
705
706 return SkPicture::MakeFromStream(stream.get());
707 }
708
ReadMSKP(const char * path)709 static std::unique_ptr<MSKPPlayer> ReadMSKP(const char* path) {
710 // Not strictly necessary, as it will be checked again later,
711 // but helps to avoid a lot of pointless work if we're going to skip it.
712 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
713 return nullptr;
714 }
715
716 std::unique_ptr<SkStreamSeekable> stream = SkStream::MakeFromFile(path);
717 if (!stream) {
718 SkDebugf("Could not read %s.\n", path);
719 return nullptr;
720 }
721
722 return MSKPPlayer::Make(stream.get());
723 }
724
ReadSVGPicture(const char * path)725 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
726 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
727 return nullptr;
728 }
729 sk_sp<SkData> data(SkData::MakeFromFileName(path));
730 if (!data) {
731 SkDebugf("Could not read %s.\n", path);
732 return nullptr;
733 }
734
735 #ifdef SK_XML
736 SkMemoryStream stream(std::move(data));
737 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
738 if (!svgDom) {
739 SkDebugf("Could not parse %s.\n", path);
740 return nullptr;
741 }
742
743 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
744 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
745 if (svgDom->containerSize().isEmpty()) {
746 svgDom->setContainerSize(kDefaultContainerSize);
747 }
748
749 SkPictureRecorder recorder;
750 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
751 svgDom->containerSize().height()));
752 return recorder.finishRecordingAsPicture();
753 #else
754 return nullptr;
755 #endif // SK_XML
756 }
757
next()758 Benchmark* next() {
759 std::unique_ptr<Benchmark> bench;
760 do {
761 bench.reset(this->rawNext());
762 if (!bench) {
763 return nullptr;
764 }
765 } while (CommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
766 CommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
767 return bench.release();
768 }
769
rawNext()770 Benchmark* rawNext() {
771 if (fBenches) {
772 Benchmark* bench = fBenches->get()(nullptr);
773 fBenches = fBenches->next();
774 fSourceType = "bench";
775 fBenchType = "micro";
776 return bench;
777 }
778
779 while (fGMs) {
780 std::unique_ptr<skiagm::GM> gm = fGMs->get()();
781 fGMs = fGMs->next();
782 if (gm->runAsBench()) {
783 fSourceType = "gm";
784 fBenchType = "micro";
785 return new GMBench(std::move(gm));
786 }
787 }
788
789 while (fCurrentTextBlobTrace < fTextBlobTraces.count()) {
790 SkString path = fTextBlobTraces[fCurrentTextBlobTrace++];
791 SkString basename = SkOSPath::Basename(path.c_str());
792 static constexpr char kEnding[] = ".trace";
793 if (basename.endsWith(kEnding)) {
794 basename.remove(basename.size() - strlen(kEnding), strlen(kEnding));
795 }
796 fSourceType = "texttrace";
797 fBenchType = "micro";
798 return CreateDiffCanvasBench(
799 SkStringPrintf("SkDiffBench-%s", basename.c_str()),
800 [path](){ return SkStream::MakeFromFile(path.c_str()); });
801 }
802
803 // First add all .skps as RecordingBenches.
804 while (fCurrentRecording < fSKPs.count()) {
805 const SkString& path = fSKPs[fCurrentRecording++];
806 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
807 if (!pic) {
808 continue;
809 }
810 SkString name = SkOSPath::Basename(path.c_str());
811 fSourceType = "skp";
812 fBenchType = "recording";
813 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
814 fSKPOps = pic->approximateOpCount();
815 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
816 }
817
818 // Add all .skps as DeserializePictureBenchs.
819 while (fCurrentDeserialPicture < fSKPs.count()) {
820 const SkString& path = fSKPs[fCurrentDeserialPicture++];
821 sk_sp<SkData> data = SkData::MakeFromFileName(path.c_str());
822 if (!data) {
823 continue;
824 }
825 SkString name = SkOSPath::Basename(path.c_str());
826 fSourceType = "skp";
827 fBenchType = "deserial";
828 fSKPBytes = static_cast<double>(data->size());
829 fSKPOps = 0;
830 return new DeserializePictureBench(name.c_str(), std::move(data));
831 }
832
833 // Then once each for each scale as SKPBenches (playback).
834 while (fCurrentScale < fScales.count()) {
835 while (fCurrentSKP < fSKPs.count()) {
836 const SkString& path = fSKPs[fCurrentSKP++];
837 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
838 if (!pic) {
839 continue;
840 }
841
842 if (FLAGS_bbh) {
843 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
844 SkRTreeFactory factory;
845 SkPictureRecorder recorder;
846 pic->playback(recorder.beginRecording(pic->cullRect().width(),
847 pic->cullRect().height(),
848 &factory));
849 pic = recorder.finishRecordingAsPicture();
850 }
851 SkString name = SkOSPath::Basename(path.c_str());
852 fSourceType = "skp";
853 fBenchType = "playback";
854 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
855 FLAGS_loopSKP);
856 }
857
858 while (fCurrentSVG < fSVGs.count()) {
859 const char* path = fSVGs[fCurrentSVG++].c_str();
860 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
861 fSourceType = "svg";
862 fBenchType = "playback";
863 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
864 fScales[fCurrentScale], FLAGS_loopSKP);
865 }
866 }
867
868 fCurrentSKP = 0;
869 fCurrentSVG = 0;
870 fCurrentScale++;
871 }
872
873 // Now loop over each skp again if we have an animation
874 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
875 while (fCurrentAnimSKP < fSKPs.count()) {
876 const SkString& path = fSKPs[fCurrentAnimSKP];
877 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
878 if (!pic) {
879 fCurrentAnimSKP++;
880 continue;
881 }
882
883 fCurrentAnimSKP++;
884 SkString name = SkOSPath::Basename(path.c_str());
885 sk_sp<SKPAnimationBench::Animation> animation =
886 SKPAnimationBench::MakeZoomAnimation(fZoomMax, fZoomPeriodMs);
887 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, std::move(animation),
888 FLAGS_loopSKP);
889 }
890 }
891
892 // Read all MSKPs as benches
893 while (fCurrentMSKP < fMSKPs.count()) {
894 const SkString& path = fMSKPs[fCurrentMSKP++];
895 std::unique_ptr<MSKPPlayer> player = ReadMSKP(path.c_str());
896 if (!player) {
897 continue;
898 }
899 SkString name = SkOSPath::Basename(path.c_str());
900 fSourceType = "mskp";
901 fBenchType = "mskp";
902 return new MSKPBench(std::move(name), std::move(player));
903 }
904
905 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
906 fSourceType = "image";
907 fBenchType = "skcodec";
908 const SkString& path = fImages[fCurrentCodec];
909 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
910 continue;
911 }
912 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
913 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
914 if (!codec) {
915 // Nothing to time.
916 SkDebugf("Cannot find codec for %s\n", path.c_str());
917 continue;
918 }
919
920 while (fCurrentColorType < fColorTypes.count()) {
921 const SkColorType colorType = fColorTypes[fCurrentColorType];
922
923 SkAlphaType alphaType = codec->getInfo().alphaType();
924 if (FLAGS_simpleCodec) {
925 if (kUnpremul_SkAlphaType == alphaType) {
926 alphaType = kPremul_SkAlphaType;
927 }
928
929 fCurrentColorType++;
930 } else {
931 switch (alphaType) {
932 case kOpaque_SkAlphaType:
933 // We only need to test one alpha type (opaque).
934 fCurrentColorType++;
935 break;
936 case kUnpremul_SkAlphaType:
937 case kPremul_SkAlphaType:
938 if (0 == fCurrentAlphaType) {
939 // Test unpremul first.
940 alphaType = kUnpremul_SkAlphaType;
941 fCurrentAlphaType++;
942 } else {
943 // Test premul.
944 alphaType = kPremul_SkAlphaType;
945 fCurrentAlphaType = 0;
946 fCurrentColorType++;
947 }
948 break;
949 default:
950 SkASSERT(false);
951 fCurrentColorType++;
952 break;
953 }
954 }
955
956 // Make sure we can decode to this color type and alpha type.
957 SkImageInfo info =
958 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
959 const size_t rowBytes = info.minRowBytes();
960 SkAutoMalloc storage(info.computeByteSize(rowBytes));
961
962 const SkCodec::Result result = codec->getPixels(
963 info, storage.get(), rowBytes);
964 switch (result) {
965 case SkCodec::kSuccess:
966 case SkCodec::kIncompleteInput:
967 return new CodecBench(SkOSPath::Basename(path.c_str()),
968 encoded.get(), colorType, alphaType);
969 case SkCodec::kInvalidConversion:
970 // This is okay. Not all conversions are valid.
971 break;
972 default:
973 // This represents some sort of failure.
974 SkASSERT(false);
975 break;
976 }
977 }
978 fCurrentColorType = 0;
979 }
980
981 // Run AndroidCodecBenches
982 const int sampleSizes[] = { 2, 4, 8 };
983 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
984 fSourceType = "image";
985 fBenchType = "skandroidcodec";
986
987 const SkString& path = fImages[fCurrentAndroidCodec];
988 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
989 continue;
990 }
991 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
992 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
993 if (!codec) {
994 // Nothing to time.
995 SkDebugf("Cannot find codec for %s\n", path.c_str());
996 continue;
997 }
998
999 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
1000 int sampleSize = sampleSizes[fCurrentSampleSize];
1001 fCurrentSampleSize++;
1002 if (10 * sampleSize > std::min(codec->getInfo().width(), codec->getInfo().height())) {
1003 // Avoid benchmarking scaled decodes of already small images.
1004 break;
1005 }
1006
1007 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
1008 encoded.get(), sampleSize);
1009 }
1010 fCurrentSampleSize = 0;
1011 }
1012
1013 #ifdef SK_ENABLE_ANDROID_UTILS
1014 // Run the BRDBenches
1015 // We intend to create benchmarks that model the use cases in
1016 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
1017 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
1018 // the image. For that reason, we will benchmark decodes in five representative locations
1019 // in the image. Additionally, this use case utilizes power of two scaling, so we will
1020 // test on power of two sample sizes. The output tile is always 512x512, so, when a
1021 // sampleSize is used, the size of the subset that is decoded is always
1022 // (sampleSize*512)x(sampleSize*512).
1023 // There are a few good reasons to only test on power of two sample sizes at this time:
1024 // All use cases we are aware of only scale by powers of two.
1025 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
1026 // these tests are sufficient to provide good coverage of our scaling options.
1027 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
1028 const uint32_t minOutputSize = 512;
1029 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
1030 fSourceType = "image";
1031 fBenchType = "BRD";
1032
1033 const SkString& path = fImages[fCurrentBRDImage];
1034 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
1035 continue;
1036 }
1037
1038 while (fCurrentColorType < fColorTypes.count()) {
1039 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
1040 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
1041
1042 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
1043 const SkColorType colorType = fColorTypes[fCurrentColorType];
1044 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
1045 int currentSubsetType = fCurrentSubsetType++;
1046
1047 int width = 0;
1048 int height = 0;
1049 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
1050 &width, &height)) {
1051 break;
1052 }
1053
1054 SkString basename = SkOSPath::Basename(path.c_str());
1055 SkIRect subset;
1056 const uint32_t subsetSize = sampleSize * minOutputSize;
1057 switch (currentSubsetType) {
1058 case kTopLeft_SubsetType:
1059 basename.append("_TopLeft");
1060 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
1061 break;
1062 case kTopRight_SubsetType:
1063 basename.append("_TopRight");
1064 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
1065 subsetSize);
1066 break;
1067 case kMiddle_SubsetType:
1068 basename.append("_Middle");
1069 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
1070 (height - subsetSize) / 2, subsetSize, subsetSize);
1071 break;
1072 case kBottomLeft_SubsetType:
1073 basename.append("_BottomLeft");
1074 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1075 subsetSize);
1076 break;
1077 case kBottomRight_SubsetType:
1078 basename.append("_BottomRight");
1079 subset = SkIRect::MakeXYWH(width - subsetSize,
1080 height - subsetSize, subsetSize, subsetSize);
1081 break;
1082 default:
1083 SkASSERT(false);
1084 }
1085
1086 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1087 colorType, sampleSize, subset);
1088 }
1089 fCurrentSubsetType = 0;
1090 fCurrentSampleSize++;
1091 }
1092 fCurrentSampleSize = 0;
1093 fCurrentColorType++;
1094 }
1095 fCurrentColorType = 0;
1096 }
1097 #endif // SK_ENABLE_ANDROID_UTILS
1098
1099 return nullptr;
1100 }
1101
fillCurrentOptions(NanoJSONResultsWriter & log) const1102 void fillCurrentOptions(NanoJSONResultsWriter& log) const {
1103 log.appendString("source_type", fSourceType);
1104 log.appendString("bench_type", fBenchType);
1105 if (0 == strcmp(fSourceType, "skp")) {
1106 log.appendString("clip",
1107 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1108 fClip.fRight, fClip.fBottom).c_str());
1109 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
1110 log.appendString("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
1111 }
1112 }
1113
fillCurrentMetrics(NanoJSONResultsWriter & log) const1114 void fillCurrentMetrics(NanoJSONResultsWriter& log) const {
1115 if (0 == strcmp(fBenchType, "recording")) {
1116 log.appendMetric("bytes", fSKPBytes);
1117 log.appendMetric("ops", fSKPOps);
1118 }
1119 }
1120
1121 private:
1122 #ifdef SK_ENABLE_ANDROID_UTILS
1123 enum SubsetType {
1124 kTopLeft_SubsetType = 0,
1125 kTopRight_SubsetType = 1,
1126 kMiddle_SubsetType = 2,
1127 kBottomLeft_SubsetType = 3,
1128 kBottomRight_SubsetType = 4,
1129 kTranslate_SubsetType = 5,
1130 kZoom_SubsetType = 6,
1131 kLast_SubsetType = kZoom_SubsetType,
1132 kLastSingle_SubsetType = kBottomRight_SubsetType,
1133 };
1134 #endif
1135
1136 const BenchRegistry* fBenches;
1137 const skiagm::GMRegistry* fGMs;
1138 SkIRect fClip;
1139 SkTArray<SkScalar> fScales;
1140 SkTArray<SkString> fSKPs;
1141 SkTArray<SkString> fMSKPs;
1142 SkTArray<SkString> fSVGs;
1143 SkTArray<SkString> fTextBlobTraces;
1144 SkTArray<SkString> fImages;
1145 SkTArray<SkColorType, true> fColorTypes;
1146 SkScalar fZoomMax;
1147 double fZoomPeriodMs;
1148
1149 double fSKPBytes, fSKPOps;
1150
1151 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1152 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1153 int fCurrentRecording = 0;
1154 int fCurrentDeserialPicture = 0;
1155 int fCurrentMSKP = 0;
1156 int fCurrentScale = 0;
1157 int fCurrentSKP = 0;
1158 int fCurrentSVG = 0;
1159 int fCurrentTextBlobTrace = 0;
1160 int fCurrentCodec = 0;
1161 int fCurrentAndroidCodec = 0;
1162 #ifdef SK_ENABLE_ANDROID_UTILS
1163 int fCurrentBRDImage = 0;
1164 int fCurrentSubsetType = 0;
1165 #endif
1166 int fCurrentColorType = 0;
1167 int fCurrentAlphaType = 0;
1168 int fCurrentSampleSize = 0;
1169 int fCurrentAnimSKP = 0;
1170 };
1171
1172 // Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1173 // This prints something every once in a while so that it knows we're still working.
start_keepalive()1174 static void start_keepalive() {
1175 static std::thread* intentionallyLeaked = new std::thread([]{
1176 for (;;) {
1177 static const int kSec = 1200;
1178 #if defined(SK_BUILD_FOR_WIN)
1179 Sleep(kSec * 1000);
1180 #else
1181 sleep(kSec);
1182 #endif
1183 SkDebugf("\nBenchmarks still running...\n");
1184 }
1185 });
1186 (void)intentionallyLeaked;
1187 }
1188
1189 class NanobenchShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
compileError(const char * shader,const char * errors)1190 void compileError(const char* shader, const char* errors) override {
1191 // Nanobench should abort if any shader can't compile. Failure is much better than
1192 // reporting meaningless performance metrics.
1193 SkSL::String message = GrShaderUtils::BuildShaderErrorMessage(shader, errors);
1194 SK_ABORT("\n%s", message.c_str());
1195 }
1196 };
1197
main(int argc,char ** argv)1198 int main(int argc, char** argv) {
1199 CommandLineFlags::Parse(argc, argv);
1200
1201 initializeEventTracingForTools();
1202
1203 #if defined(SK_BUILD_FOR_IOS)
1204 cd_Documents();
1205 #endif
1206 SetupCrashHandler();
1207 SkAutoGraphics ag;
1208 SkTaskGroup::Enabler enabled(FLAGS_threads);
1209
1210 SetCtxOptionsFromCommonFlags(&grContextOpts);
1211
1212 NanobenchShaderErrorHandler errorHandler;
1213 grContextOpts.fShaderErrorHandler = &errorHandler;
1214
1215 if (kAutoTuneLoops != FLAGS_loops) {
1216 FLAGS_samples = 1;
1217 FLAGS_gpuFrameLag = 0;
1218 }
1219
1220 if (!FLAGS_writePath.isEmpty()) {
1221 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1222 if (!sk_mkdir(FLAGS_writePath[0])) {
1223 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
1224 FLAGS_writePath.set(0, nullptr);
1225 }
1226 }
1227
1228 std::unique_ptr<SkWStream> logStream(new SkNullWStream);
1229 if (!FLAGS_outResultsFile.isEmpty()) {
1230 #if defined(SK_RELEASE)
1231 // SkJSONWriter uses a 32k in-memory cache, so it only flushes occasionally and is well
1232 // equipped for a stream that re-opens, appends, and closes the file on every write.
1233 logStream.reset(new NanoFILEAppendAndCloseStream(FLAGS_outResultsFile[0]));
1234 #else
1235 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1236 return 1;
1237 #endif
1238 }
1239 NanoJSONResultsWriter log(logStream.get(), SkJSONWriter::Mode::kPretty);
1240 log.beginObject(); // root
1241
1242 if (1 == FLAGS_properties.count() % 2) {
1243 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1244 return 1;
1245 }
1246 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1247 log.appendString(FLAGS_properties[i-1], FLAGS_properties[i]);
1248 }
1249
1250 if (1 == FLAGS_key.count() % 2) {
1251 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1252 return 1;
1253 }
1254 if (FLAGS_key.count()) {
1255 log.beginObject("key");
1256 for (int i = 1; i < FLAGS_key.count(); i += 2) {
1257 log.appendString(FLAGS_key[i - 1], FLAGS_key[i]);
1258 }
1259 log.endObject(); // key
1260 }
1261
1262 const double overhead = estimate_timer_overhead();
1263 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
1264
1265 SkTArray<double> samples;
1266
1267 if (kAutoTuneLoops != FLAGS_loops) {
1268 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
1269 } else if (FLAGS_quiet) {
1270 SkDebugf("! -> high variance, ? -> moderate variance\n");
1271 SkDebugf(" micros \tbench\n");
1272 } else if (FLAGS_ms) {
1273 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
1274 } else {
1275 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
1276 FLAGS_samples, "samples");
1277 }
1278
1279 GrRecordingContextPriv::DMSAAStats combinedDMSAAStats;
1280
1281 SkTArray<Config> configs;
1282 create_configs(&configs);
1283
1284 if (FLAGS_keepAlive) {
1285 start_keepalive();
1286 }
1287
1288 SetAnalyticAAFromCommonFlags();
1289
1290 gSkForceRasterPipelineBlitter = FLAGS_forceRasterPipeline;
1291 gUseSkVMBlitter = FLAGS_skvm;
1292 gSkVMAllowJIT = FLAGS_jit;
1293 gSkVMJITViaDylib = FLAGS_dylib;
1294
1295 int runs = 0;
1296 BenchmarkStream benchStream;
1297 log.beginObject("results");
1298 AutoreleasePool pool;
1299 while (Benchmark* b = benchStream.next()) {
1300 std::unique_ptr<Benchmark> bench(b);
1301 if (CommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
1302 continue;
1303 }
1304
1305 if (!configs.empty()) {
1306 log.beginBench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
1307 bench->delayedSetup();
1308 }
1309 for (int i = 0; i < configs.count(); ++i) {
1310 Target* target = is_enabled(b, configs[i]);
1311 if (!target) {
1312 continue;
1313 }
1314
1315 // During HWUI output this canvas may be nullptr.
1316 SkCanvas* canvas = target->getCanvas();
1317 const char* config = target->config.name.c_str();
1318
1319 if (FLAGS_pre_log || FLAGS_dryRun) {
1320 SkDebugf("Running %s\t%s\n"
1321 , bench->getUniqueName()
1322 , config);
1323 if (FLAGS_dryRun) {
1324 continue;
1325 }
1326 }
1327
1328 if (FLAGS_purgeBetweenBenches) {
1329 SkGraphics::PurgeAllCaches();
1330 }
1331
1332 TRACE_EVENT2("skia", "Benchmark", "name", TRACE_STR_COPY(bench->getUniqueName()),
1333 "config", TRACE_STR_COPY(config));
1334
1335 target->setup();
1336 bench->perCanvasPreDraw(canvas);
1337
1338 int maxFrameLag;
1339 int loops = target->needsFrameTiming(&maxFrameLag)
1340 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1341 : setup_cpu_bench(overhead, target, bench.get());
1342
1343 if (kFailedLoops == loops) {
1344 // Can't be timed. A warning note has already been printed.
1345 cleanup_run(target);
1346 continue;
1347 }
1348
1349 if (runs == 0 && FLAGS_ms < 1000) {
1350 // Run the first bench for 1000ms to warm up the nanobench if FLAGS_ms < 1000.
1351 // Otherwise, the first few benches' measurements will be inaccurate.
1352 auto stop = now_ms() + 1000;
1353 do {
1354 time(loops, bench.get(), target);
1355 pool.drain();
1356 } while (now_ms() < stop);
1357 }
1358
1359 if (FLAGS_ms) {
1360 samples.reset();
1361 auto stop = now_ms() + FLAGS_ms;
1362 do {
1363 samples.push_back(time(loops, bench.get(), target) / loops);
1364 pool.drain();
1365 } while (now_ms() < stop);
1366 } else {
1367 samples.reset(FLAGS_samples);
1368 for (int s = 0; s < FLAGS_samples; s++) {
1369 samples[s] = time(loops, bench.get(), target) / loops;
1370 pool.drain();
1371 }
1372 }
1373
1374 // Scale each result to the benchmark's own units, time/unit.
1375 for (double& sample : samples) {
1376 sample *= (1.0 / bench->getUnits());
1377 }
1378
1379 SkTArray<SkString> keys;
1380 SkTArray<double> values;
1381 if (configs[i].backend == Benchmark::kGPU_Backend) {
1382 if (FLAGS_gpuStatsDump) {
1383 // TODO cache stats
1384 bench->getGpuStats(canvas, &keys, &values);
1385 }
1386 if (FLAGS_dmsaaStatsDump && bench->getDMSAAStats(canvas->recordingContext())) {
1387 const auto& dmsaaStats = canvas->recordingContext()->priv().dmsaaStats();
1388 dmsaaStats.dumpKeyValuePairs(&keys, &values);
1389 dmsaaStats.dump();
1390 combinedDMSAAStats.merge(dmsaaStats);
1391 }
1392 }
1393
1394 bench->perCanvasPostDraw(canvas);
1395
1396 if (Benchmark::kNonRendering_Backend != target->config.backend &&
1397 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
1398 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
1399 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
1400 pngFilename.append(".png");
1401 write_canvas_png(target, pngFilename);
1402 }
1403
1404 // Building stats.plot often shows up in profiles,
1405 // so skip building it when we're not going to print it anyway.
1406 const bool want_plot = !FLAGS_quiet;
1407
1408 Stats stats(samples, want_plot);
1409 log.beginObject(config);
1410
1411 log.beginObject("options");
1412 log.appendString("name", bench->getName());
1413 benchStream.fillCurrentOptions(log);
1414 target->fillOptions(log);
1415 log.endObject(); // options
1416
1417 // Metrics
1418 log.appendMetric("min_ms", stats.min);
1419 log.beginArray("samples");
1420 for (double sample : samples) {
1421 log.appendDoubleDigits(sample, 16);
1422 }
1423 log.endArray(); // samples
1424 benchStream.fillCurrentMetrics(log);
1425 if (!keys.empty()) {
1426 // dump to json, only SKPBench currently returns valid keys / values
1427 SkASSERT(keys.count() == values.count());
1428 for (int i = 0; i < keys.count(); i++) {
1429 log.appendMetric(keys[i].c_str(), values[i]);
1430 }
1431 }
1432
1433 log.endObject(); // config
1434
1435 if (runs++ % FLAGS_flushEvery == 0) {
1436 log.flush();
1437 }
1438
1439 if (kAutoTuneLoops != FLAGS_loops) {
1440 if (configs.count() == 1) {
1441 config = ""; // Only print the config if we run the same bench on more than one.
1442 }
1443 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1444 , sk_tools::getCurrResidentSetSizeMB()
1445 , sk_tools::getMaxResidentSetSizeMB()
1446 , bench->getUniqueName()
1447 , config);
1448 } else if (FLAGS_quiet) {
1449 const char* mark = " ";
1450 const double stddev_percent =
1451 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
1452 if (stddev_percent > 5) mark = "?";
1453 if (stddev_percent > 10) mark = "!";
1454
1455 SkDebugf("%10.2f %s\t%s\t%s\n",
1456 stats.median*1e3, mark, bench->getUniqueName(), config);
1457 } else if (FLAGS_csv) {
1458 const double stddev_percent =
1459 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
1460 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1461 , stats.min
1462 , stats.median
1463 , stats.mean
1464 , stats.max
1465 , stddev_percent
1466 , config
1467 , bench->getUniqueName()
1468 );
1469 } else {
1470 const char* format = "%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n";
1471 const double stddev_percent =
1472 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
1473 SkDebugf(format
1474 , sk_tools::getCurrResidentSetSizeMB()
1475 , sk_tools::getMaxResidentSetSizeMB()
1476 , loops
1477 , HUMANIZE(stats.min)
1478 , HUMANIZE(stats.median)
1479 , HUMANIZE(stats.mean)
1480 , HUMANIZE(stats.max)
1481 , stddev_percent
1482 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
1483 , config
1484 , bench->getUniqueName()
1485 );
1486 }
1487
1488 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
1489 target->dumpStats();
1490 }
1491
1492 if (FLAGS_verbose) {
1493 SkDebugf("Samples: ");
1494 for (int i = 0; i < samples.count(); i++) {
1495 SkDebugf("%s ", HUMANIZE(samples[i]));
1496 }
1497 SkDebugf("%s\n", bench->getUniqueName());
1498 }
1499 cleanup_run(target);
1500 pool.drain();
1501 }
1502 if (!configs.empty()) {
1503 log.endBench();
1504 }
1505 }
1506
1507 if (FLAGS_dmsaaStatsDump) {
1508 SkDebugf("<<Total Combined DMSAA Stats>>\n");
1509 combinedDMSAAStats.dump();
1510 }
1511
1512 SkGraphics::PurgeAllCaches();
1513
1514 log.beginBench("memory_usage", 0, 0);
1515 log.beginObject("meta"); // config
1516 log.appendS32("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1517 log.endObject(); // config
1518 log.endBench();
1519
1520 RunSkSLMemoryBenchmarks(&log);
1521
1522 log.endObject(); // results
1523 log.endObject(); // root
1524 log.flush();
1525
1526 return 0;
1527 }
1528