OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 // TODO: clean up before submitting | |
9 #ifndef GR_GL_NO_ERROR | |
10 #define GR_GL_NO_ERROR 0 | |
11 #endif | |
12 | |
13 #include <ctype.h> | |
14 | |
8 #include "Benchmark.h" | 15 #include "Benchmark.h" |
9 #include "CrashHandler.h" | 16 #include "CrashHandler.h" |
10 #include "Stats.h" | 17 #include "Stats.h" |
11 #include "Timer.h" | 18 #include "Timer.h" |
12 | 19 |
13 #include "SkCanvas.h" | 20 #include "SkCanvas.h" |
14 #include "SkCommandLineFlags.h" | 21 #include "SkCommandLineFlags.h" |
15 #include "SkForceLinking.h" | 22 #include "SkForceLinking.h" |
16 #include "SkGraphics.h" | 23 #include "SkGraphics.h" |
17 #include "SkString.h" | 24 #include "SkString.h" |
18 #include "SkSurface.h" | 25 #include "SkSurface.h" |
19 | 26 |
27 #if SK_SUPPORT_GPU | |
28 #include "GrContextFactory.h" | |
29 GrContextFactory gGrFactory; | |
30 static const GrContextFactory::GLContextType kNative = GrContextFactory::kNa tive_GLContextType | |
31 , kNVPR = GrContextFactory::kNV PR_GLContextType | |
32 , kDebug = GrContextFactory::kDe bug_GLContextType | |
33 , kNull = GrContextFactory::kNu ll_GLContextType | |
34 #if SK_ANGLE | |
35 , kANGLE = GrContextFactory::kAN GLE_GLContextType | |
36 #endif | |
37 ; | |
38 #endif | |
39 | |
20 __SK_FORCE_IMAGE_DECODER_LINKING; | 40 __SK_FORCE_IMAGE_DECODER_LINKING; |
21 | 41 |
22 DEFINE_int32(samples, 10, "Number of samples to measure for each bench."); | 42 DEFINE_int32(samples, 10, "Number of samples to measure for each bench."); |
23 DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead."); | 43 DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead."); |
24 DEFINE_double(overheadGoal, 0.0001, | 44 DEFINE_double(overheadGoal, 0.0001, |
25 "Loop until timer overhead is at most this fraction of our measurm ents."); | 45 "Loop until timer overhead is at most this fraction of our measurm ents."); |
26 DEFINE_string(match, "", "The usual filters on file names of benchmarks to measu re."); | 46 DEFINE_string(match, "", "The usual filters on file names of benchmarks to measu re."); |
27 DEFINE_bool2(quiet, q, false, "Print only bench name and minimum sample."); | 47 DEFINE_bool2(quiet, q, false, "Print only bench name and minimum sample."); |
28 DEFINE_bool2(verbose, v, false, "Print all samples."); | 48 DEFINE_bool2(verbose, v, false, "Print all samples."); |
29 DEFINE_string(config, "8888 nonrendering", | 49 DEFINE_string(config, "nonrendering 8888 gpu", "Configs to measure. Options: " |
30 "Configs to measure. Options: 565 8888 nonrendering"); | 50 "565 8888 gpu nonrendering debug nullgpu msaa4 msaa16 nvprmsaa4 nv prmsaa16 angle"); |
51 DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU."); | |
52 DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allow s to lag."); | |
31 | 53 |
32 // TODO: GPU benches | |
33 | 54 |
34 static SkString humanize(double ms) { | 55 static SkString humanize(double ms) { |
35 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3); | 56 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3); |
36 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6); | 57 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6); |
37 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3); | 58 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3); |
38 return SkStringPrintf("%.3gms", ms); | 59 return SkStringPrintf("%.3gms", ms); |
39 } | 60 } |
40 | 61 |
62 static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHel per* gl) { | |
63 WallTimer timer; | |
64 timer.start(); | |
65 if (bench) { | |
66 bench->draw(loops, canvas); | |
67 } | |
68 if (canvas) { | |
69 canvas->flush(); | |
70 } | |
71 #if SK_SUPPORT_GPU | |
72 if (gl) { | |
73 SK_GL(*gl, Flush()); | |
74 gl->swapBuffers(); | |
75 } | |
76 #endif | |
77 timer.end(); | |
78 return timer.fWall; | |
79 } | |
80 | |
41 static double estimate_timer_overhead() { | 81 static double estimate_timer_overhead() { |
42 double overhead = 0; | 82 double overhead = 0; |
43 WallTimer timer; | |
44 for (int i = 0; i < FLAGS_overheadLoops; i++) { | 83 for (int i = 0; i < FLAGS_overheadLoops; i++) { |
45 timer.start(); | 84 overhead += time(1, NULL, NULL, NULL); |
46 timer.end(); | |
47 overhead += timer.fWall; | |
48 } | 85 } |
49 return overhead / FLAGS_overheadLoops; | 86 return overhead / FLAGS_overheadLoops; |
50 } | 87 } |
51 | 88 |
52 static void safe_flush(SkCanvas* canvas) { | 89 static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) { |
53 if (canvas) { | 90 // First figure out approximately how many loops of bench it takes to make o verhead negligible. |
54 canvas->flush(); | 91 double bench_plus_overhead; |
55 } | 92 do { |
56 } | 93 bench_plus_overhead = time(1, bench, canvas, NULL); |
94 } while (bench_plus_overhead < overhead); // Shouldn't normally happen. | |
57 | 95 |
58 static int guess_loops(double overhead, Benchmark* bench, SkCanvas* canvas) { | 96 // Later we'll just start and stop the timer once but loop N times. |
59 WallTimer timer; | |
60 | |
61 // Measure timer overhead and bench time together. | |
62 do { | |
63 timer.start(); | |
64 bench->draw(1, canvas); | |
65 safe_flush(canvas); | |
66 timer.end(); | |
67 } while (timer.fWall < overhead); // Shouldn't normally happen. | |
68 | |
69 // Later we'll just start and stop the timer once, but loop N times. | |
70 // We'll pick N to make timer overhead negligible: | 97 // We'll pick N to make timer overhead negligible: |
71 // | 98 // |
72 // Timer Overhead | 99 // overhead |
73 // ------------------------------- < FLAGS_overheadGoal | 100 // ------------------------- < FLAGS_overheadGoal |
74 // Timer Overhead + N * Bench Time | 101 // overhead + N * Bench Time |
75 // | 102 // |
76 // where timer.fWall ≈ Timer Overhead + Bench Time. | 103 // where bench_plus_overhead ≈ overhead + Bench Time. |
77 // | 104 // |
78 // Doing some math, we get: | 105 // Doing some math, we get: |
79 // | 106 // |
80 // (Timer Overhead / FLAGS_overheadGoal) - Timer Overhead | 107 // (overhead / FLAGS_overheadGoal) - overhead |
81 // ----------------------------------------------------- < N | 108 // ------------------------------------------ < N |
82 // (timer.fWall - Timer Overhead) | 109 // bench_plus_overhead - overhead) |
83 // | 110 // |
84 // Luckily, this also works well in practice. :) | 111 // Luckily, this also works well in practice. :) |
85 const double numer = overhead / FLAGS_overheadGoal - overhead; | 112 const double numer = overhead / FLAGS_overheadGoal - overhead; |
86 const double denom = timer.fWall - overhead; | 113 const double denom = bench_plus_overhead - overhead; |
87 return (int)ceil(numer / denom); | 114 const int loops = ceil(numer / denom); |
115 | |
116 for (int i = 0; i < FLAGS_samples; i++) { | |
117 samples[i] = time(loops, bench, canvas, NULL) / loops; | |
118 } | |
119 return loops; | |
88 } | 120 } |
89 | 121 |
90 static bool push_config_if_enabled(const char* config, SkTDArray<const char*>* c onfigs) { | 122 #if SK_SUPPORT_GPU |
91 if (FLAGS_config.contains(config)) { | 123 static int gpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) { |
92 configs->push(config); | 124 SkGLContextHelper* gl = gGrFactory.getGLContext(GrContextFactory::kNative_GL ContextType); |
bsalomon
2014/07/01 13:23:49
The GL context type here needs to match that of th
mtklein
2014/07/01 14:02:43
Oh, duh. Done.
| |
93 return true; | 125 SK_GL(*gl, Finish); |
126 | |
127 // First, figure out how many loops it'll take to get up to FLAGS_gpuMs. | |
128 int loops = 1; | |
129 double elapsed = 0; | |
130 do { | |
131 loops *= 2; | |
132 // TODO: explain | |
133 for (int i = 0; i < FLAGS_gpuFrameLag; i++) { | |
134 elapsed = time(loops, bench, canvas, gl); | |
135 } | |
136 } while (elapsed < FLAGS_gpuMs); | |
137 | |
138 // We've overshot at least a little. Scale back linearly. | |
139 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed); | |
140 | |
141 // TODO: explain | |
142 SK_GL(*gl, Finish); | |
143 | |
144 // TODO: explain | |
145 for (int i = 0; i < FLAGS_gpuFrameLag; i++) { | |
146 time(loops, bench, canvas, gl); | |
94 } | 147 } |
95 return false; | 148 |
149 for (int i = 0; i < FLAGS_samples; i++) { | |
150 samples[i] = time(loops, bench, canvas, gl) / loops; | |
151 } | |
152 return loops; | |
153 } | |
154 #endif | |
155 | |
156 static SkString to_lower(const char* str) { | |
157 SkString lower(str); | |
158 for (size_t i = 0; i < lower.size(); i++) { | |
159 lower[i] = tolower(lower[i]); | |
160 } | |
161 return lower; | |
96 } | 162 } |
97 | 163 |
98 static void create_surfaces(Benchmark* bench, | 164 struct Target { |
99 SkTDArray<SkSurface*>* surfaces, | 165 SkAutoTDelete<SkSurface> surface; |
100 SkTDArray<const char*>* configs) { | 166 const char* config; |
167 Benchmark::Backend backend; | |
168 }; | |
101 | 169 |
102 if (bench->isSuitableFor(Benchmark::kNonRendering_Backend) | 170 // If bench is enabled for backend/config, returns a Target* for them, otherwise NULL. |
103 && push_config_if_enabled("nonrendering", configs)) { | 171 static Target* is_enabled(Benchmark* bench, Benchmark::Backend backend, const ch ar* config) { |
104 surfaces->push(NULL); | 172 if (!bench->isSuitableFor(backend)) { |
173 return NULL; | |
105 } | 174 } |
106 | 175 |
107 if (bench->isSuitableFor(Benchmark::kRaster_Backend)) { | 176 for (int i = 0; i < FLAGS_config.count(); i++) { |
108 const int w = bench->getSize().fX, | 177 if (to_lower(FLAGS_config[i]).equals(config)) { |
109 h = bench->getSize().fY; | 178 Target* target = new Target; |
179 target->config = config; | |
180 target->backend = backend; | |
181 return target; | |
182 } | |
183 } | |
184 return NULL; | |
185 } | |
110 | 186 |
111 if (push_config_if_enabled("8888", configs)) { | 187 // Append all targets that are suitable for bench. |
112 const SkImageInfo info = { w, h, kN32_SkColorType, kPremul_SkAlphaTy pe }; | 188 static void create_targets(Benchmark* bench, SkTDArray<Target*>* targets) { |
113 surfaces->push(SkSurface::NewRaster(info)); | 189 const int w = bench->getSize().fX, |
190 h = bench->getSize().fY; | |
191 const SkImageInfo _8888 = { w, h, kN32_SkColorType, kPremul_SkAlphaType }, | |
192 _565 = { w, h, kRGB_565_SkColorType, kOpaque_SkAlphaType }; | |
193 | |
194 #define TARGET(config, backend, code) \ | |
195 if (Target* t = is_enabled(bench, Benchmark::backend, #config)) { \ | |
196 t->surface.reset(code); \ | |
197 targets->push(t); \ | |
114 } | 198 } |
115 | 199 |
116 if (push_config_if_enabled("565", configs)) { | 200 TARGET(nonrendering, kNonRendering_Backend, NULL); |
117 const SkImageInfo info = { w, h, kRGB_565_SkColorType, kOpaque_SkAlp haType }; | 201 TARGET(8888, kRaster_Backend, SkSurface::NewRaster(_8888)); |
118 surfaces->push(SkSurface::NewRaster(info)); | 202 TARGET(565, kRaster_Backend, SkSurface::NewRaster(_565)); |
119 } | 203 |
120 } | 204 #if SK_SUPPORT_GPU |
205 TARGET(gpu, kGPU_Backend, SkSurface::NewRenderTarget(gGrFactory.get(k Native), _8888, 0)); | |
206 TARGET(msaa4, kGPU_Backend, SkSurface::NewRenderTarget(gGrFactory.get(k Native), _8888, 4)); | |
207 TARGET(msaa16, kGPU_Backend, SkSurface::NewRenderTarget(gGrFactory.get(k Native), _8888, 16)); | |
208 | |
209 TARGET(nvprmsaa4, kGPU_Backend, SkSurface::NewRenderTarget(gGrFactory.g et(kNVPR), _8888, 4)); | |
210 TARGET(nvprmsaa16, kGPU_Backend, SkSurface::NewRenderTarget(gGrFactory.g et(kNVPR), _8888, 16)); | |
211 | |
212 TARGET(debug, kGPU_Backend, SkSurface::NewRenderTarget(gGrFactory.get( kDebug), _8888, 0)); | |
213 TARGET(nullgpu, kGPU_Backend, SkSurface::NewRenderTarget(gGrFactory.get( kNull), _8888, 0)); | |
214 #endif | |
215 | |
216 #if SK_ANGLE | |
217 TARGET(angle, kGPU_Backend, SkSurface::NewRenderTarget(gGrFactory.get(kA NGLE), _8888, 0)); | |
218 #endif | |
219 | |
220 #undef TARGET | |
121 } | 221 } |
122 | 222 |
123 int tool_main(int argc, char** argv); | 223 int tool_main(int argc, char** argv); |
124 int tool_main(int argc, char** argv) { | 224 int tool_main(int argc, char** argv) { |
125 SetupCrashHandler(); | 225 SetupCrashHandler(); |
126 SkAutoGraphics ag; | 226 SkAutoGraphics ag; |
127 SkCommandLineFlags::Parse(argc, argv); | 227 SkCommandLineFlags::Parse(argc, argv); |
128 | 228 |
129 const double overhead = estimate_timer_overhead(); | 229 const double overhead = estimate_timer_overhead(); |
230 SkAutoTMalloc<double> samples(FLAGS_samples); | |
231 | |
232 // TODO: display add median, use it in --quiet mode | |
130 | 233 |
131 if (FLAGS_verbose) { | 234 if (FLAGS_verbose) { |
132 // No header. | 235 // No header. |
133 } else if (FLAGS_quiet) { | 236 } else if (FLAGS_quiet) { |
134 SkDebugf("min\tbench\tconfig\n"); | 237 SkDebugf("min\tbench\tconfig\n"); |
135 } else { | 238 } else { |
136 SkDebugf("loops\tmin\tmean\tmax\tstddev\tbench\tconfig\n"); | 239 SkDebugf("loops\tmin\tmean\tmax\tstddev\tconfig\tbench\n"); |
137 } | 240 } |
138 | 241 |
139 for (const BenchRegistry* r = BenchRegistry::Head(); r != NULL; r = r->next( )) { | 242 for (const BenchRegistry* r = BenchRegistry::Head(); r != NULL; r = r->next( )) { |
140 SkAutoTDelete<Benchmark> bench(r->factory()(NULL)); | 243 SkAutoTDelete<Benchmark> bench(r->factory()(NULL)); |
141 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) { | 244 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) { |
142 continue; | 245 continue; |
143 } | 246 } |
144 | 247 |
145 SkTDArray<SkSurface*> surfaces; | 248 SkTDArray<Target*> targets; |
146 SkTDArray<const char*> configs; | 249 create_targets(bench.get(), &targets); |
147 create_surfaces(bench.get(), &surfaces, &configs); | |
148 | 250 |
149 bench->preDraw(); | 251 bench->preDraw(); |
150 for (int j = 0; j < surfaces.count(); j++) { | 252 for (int j = 0; j < targets.count(); j++) { |
151 SkCanvas* canvas = surfaces[j] ? surfaces[j]->getCanvas() : NULL; | 253 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface-> getCanvas() : NULL; |
152 const char* config = configs[j]; | 254 const char* config = targets[j]->config; |
255 Benchmark::Backend backend = targets[j]->backend; | |
153 | 256 |
154 bench->draw(1, canvas); // Just paranoid warmup. | 257 const int loops = |
155 safe_flush(canvas); | 258 #if SK_SUPPORT_GPU |
156 const int loops = guess_loops(overhead, bench.get(), canvas); | 259 backend == Benchmark::kGPU_Backend |
157 | 260 ? gpu_bench(overhead, bench.get(), canvas, samples.g et()) |
158 SkAutoTMalloc<double> samples(FLAGS_samples); | 261 : |
159 WallTimer timer; | 262 #endif |
160 for (int i = 0; i < FLAGS_samples; i++) { | 263 cpu_bench(overhead, bench.get(), canvas, samples.ge t()); |
161 timer.start(); | |
162 bench->draw(loops, canvas); | |
163 safe_flush(canvas); | |
164 timer.end(); | |
165 samples[i] = timer.fWall / loops; | |
166 } | |
167 | |
168 Stats stats(samples.get(), FLAGS_samples); | 264 Stats stats(samples.get(), FLAGS_samples); |
169 | 265 |
170 if (FLAGS_verbose) { | 266 if (FLAGS_verbose) { |
171 for (int i = 0; i < FLAGS_samples; i++) { | 267 for (int i = 0; i < FLAGS_samples; i++) { |
172 SkDebugf("%s ", humanize(samples[i]).c_str()); | 268 SkDebugf("%s ", humanize(samples[i]).c_str()); |
173 } | 269 } |
174 SkDebugf("%s\n", bench->getName()); | 270 SkDebugf("%s\n", bench->getName()); |
175 } else if (FLAGS_quiet) { | 271 } else if (FLAGS_quiet) { |
176 if (configs.count() == 1) { | 272 if (targets.count() == 1) { |
177 config = ""; // Only print the config if we run the same ben ch on more than one. | 273 config = ""; // Only print the config if we run the same ben ch on more than one. |
178 } | 274 } |
179 SkDebugf("%s\t%s\t%s\n", humanize(stats.min).c_str(), bench->get Name(), config); | 275 SkDebugf("%s\t%s\t%s\n", humanize(stats.min).c_str(), bench->get Name(), config); |
180 } else { | 276 } else { |
181 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean ; | 277 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean ; |
182 SkDebugf("%d\t%s\t%s\t%s\t%.0f%%\t%s\t%s\n" | 278 SkDebugf("%d\t%s\t%s\t%s\t%.0f%%\t%s\t%s\n" |
183 , loops | 279 , loops |
184 , humanize(stats.min).c_str() | 280 , humanize(stats.min).c_str() |
185 , humanize(stats.mean).c_str() | 281 , humanize(stats.mean).c_str() |
186 , humanize(stats.max).c_str() | 282 , humanize(stats.max).c_str() |
187 , stddev_percent | 283 , stddev_percent |
284 , config | |
188 , bench->getName() | 285 , bench->getName() |
189 , config | |
190 ); | 286 ); |
191 } | 287 } |
192 } | 288 } |
193 surfaces.deleteAll(); | 289 targets.deleteAll(); |
194 } | 290 } |
195 | 291 |
196 return 0; | 292 return 0; |
197 } | 293 } |
198 | 294 |
199 #if !defined SK_BUILD_FOR_IOS | 295 #if !defined SK_BUILD_FOR_IOS |
200 int main(int argc, char * const argv[]) { | 296 int main(int argc, char * const argv[]) { |
201 return tool_main(argc, (char**) argv); | 297 return tool_main(argc, (char**) argv); |
202 } | 298 } |
203 #endif | 299 #endif |
OLD | NEW |