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 #include "Benchmark.h" | 8 #include "Benchmark.h" |
9 #include "CrashHandler.h" | 9 #include "CrashHandler.h" |
10 #include "Stats.h" | 10 #include "Stats.h" |
11 #include "Timer.h" | 11 #include "Timer.h" |
12 | 12 |
13 #include "SkCanvas.h" | 13 #include "SkCanvas.h" |
14 #include "SkCommandLineFlags.h" | 14 #include "SkCommandLineFlags.h" |
15 #include "SkForceLinking.h" | 15 #include "SkForceLinking.h" |
16 #include "SkGraphics.h" | 16 #include "SkGraphics.h" |
17 #include "SkString.h" | 17 #include "SkString.h" |
18 #include "SkSurface.h" | 18 #include "SkSurface.h" |
19 | 19 |
20 #if SK_SUPPORT_GPU | |
21 #include "GrContextFactory.h" | |
22 GrContextFactory gContextFactory; | |
23 #endif | |
24 | |
20 __SK_FORCE_IMAGE_DECODER_LINKING; | 25 __SK_FORCE_IMAGE_DECODER_LINKING; |
21 | 26 |
22 DEFINE_int32(samples, 10, "Number of samples to measure for each bench."); | 27 DEFINE_int32(samples, 10, "Number of samples to measure for each bench."); |
23 DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead."); | 28 DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead."); |
24 DEFINE_double(overheadGoal, 0.0001, | 29 DEFINE_double(overheadGoal, 0.0001, |
25 "Loop until timer overhead is at most this fraction of our measurm ents."); | 30 "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."); | 31 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."); | 32 DEFINE_bool2(quiet, q, false, "Print only bench name and minimum sample."); |
28 DEFINE_bool2(verbose, v, false, "Print all samples."); | 33 DEFINE_bool2(verbose, v, false, "Print all samples."); |
29 DEFINE_string(config, "8888 nonrendering", | 34 DEFINE_string(config, "565 8888 gpu nonrendering", |
bsalomon
2014/06/30 19:58:12
Does regular bench have the other gpu configs (msa
mtklein_C
2014/06/30 23:05:29
Yup, done.
| |
30 "Configs to measure. Options: 565 8888 nonrendering"); | 35 "Configs to measure. Options: 565 8888 gpu nonrendering"); |
36 DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU."); | |
37 DEFINE_int32(gpuFrameLag, 10, "Overestimate of maximum number of frames GPU allo ws to lag."); | |
bsalomon
2014/06/30 19:58:12
I'd default to 5 or fewer.
mtklein_C
2014/06/30 23:05:29
You got it.
| |
31 | 38 |
32 // TODO: GPU benches | |
33 | 39 |
34 static SkString humanize(double ms) { | 40 static SkString humanize(double ms) { |
35 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3); | 41 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3); |
36 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6); | 42 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6); |
37 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3); | 43 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3); |
38 return SkStringPrintf("%.3gms", ms); | 44 return SkStringPrintf("%.3gms", ms); |
39 } | 45 } |
40 | 46 |
47 static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHel per* gl) { | |
48 WallTimer timer; | |
49 timer.start(); | |
50 if (bench) { | |
51 bench->draw(loops, canvas); | |
52 } | |
53 if (canvas) { | |
54 canvas->flush(); | |
55 } | |
56 if (gl) { | |
57 SK_GL(*gl, Flush()); | |
58 gl->swapBuffers(); | |
59 } | |
60 timer.end(); | |
61 return timer.fWall; | |
62 } | |
63 | |
41 static double estimate_timer_overhead() { | 64 static double estimate_timer_overhead() { |
42 double overhead = 0; | 65 double overhead = 0; |
43 WallTimer timer; | |
44 for (int i = 0; i < FLAGS_overheadLoops; i++) { | 66 for (int i = 0; i < FLAGS_overheadLoops; i++) { |
45 timer.start(); | 67 overhead += time(1, NULL, NULL, NULL); |
46 timer.end(); | |
47 overhead += timer.fWall; | |
48 } | 68 } |
49 return overhead / FLAGS_overheadLoops; | 69 return overhead / FLAGS_overheadLoops; |
50 } | 70 } |
51 | 71 |
52 static void safe_flush(SkCanvas* canvas) { | 72 static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) { |
53 if (canvas) { | 73 // First figure out approximately how many loops of bench it takes to make o verhead negligible. |
54 canvas->flush(); | 74 double bench_plus_overhead; |
55 } | 75 do { |
56 } | 76 bench_plus_overhead = time(1, bench, canvas, NULL); |
77 } while (bench_plus_overhead < overhead); // Shouldn't normally happen. | |
57 | 78 |
58 static int guess_loops(double overhead, Benchmark* bench, SkCanvas* canvas) { | 79 // 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: | 80 // We'll pick N to make timer overhead negligible: |
71 // | 81 // |
72 // Timer Overhead | 82 // overhead |
73 // ------------------------------- < FLAGS_overheadGoal | 83 // ------------------------- < FLAGS_overheadGoal |
74 // Timer Overhead + N * Bench Time | 84 // overhead + N * Bench Time |
75 // | 85 // |
76 // where timer.fWall ≈ Timer Overhead + Bench Time. | 86 // where bench_plus_overhead ≈ overhead + Bench Time. |
77 // | 87 // |
78 // Doing some math, we get: | 88 // Doing some math, we get: |
79 // | 89 // |
80 // (Timer Overhead / FLAGS_overheadGoal) - Timer Overhead | 90 // (overhead / FLAGS_overheadGoal) - overhead |
81 // ----------------------------------------------------- < N | 91 // ------------------------------------------ < N |
82 // (timer.fWall - Timer Overhead) | 92 // bench_plus_overhead - overhead) |
83 // | 93 // |
84 // Luckily, this also works well in practice. :) | 94 // Luckily, this also works well in practice. :) |
85 const double numer = overhead / FLAGS_overheadGoal - overhead; | 95 const double numer = overhead / FLAGS_overheadGoal - overhead; |
86 const double denom = timer.fWall - overhead; | 96 const double denom = bench_plus_overhead - overhead; |
87 return (int)ceil(numer / denom); | 97 const int loops = ceil(numer / denom); |
98 | |
99 for (int i = 0; i < FLAGS_samples; i++) { | |
100 samples[i] = time(loops, bench, canvas, NULL) / loops; | |
101 } | |
102 return loops; | |
103 } | |
104 | |
105 static int gpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) { | |
106 SkGLContextHelper* gl = gContextFactory.getGLContext(GrContextFactory::kNati ve_GLContextType); | |
107 SK_GL(*gl, Finish); | |
108 | |
109 // First, figure out how many loops it'll take to get up to FLAGS_gpuMs. | |
110 int loops = 1; | |
111 double elapsed = 0; | |
112 do { | |
113 loops *= 2; | |
114 // TODO: explain | |
115 for (int i = 0; i < FLAGS_gpuFrameLag; i++) { | |
116 elapsed = time(loops, bench, canvas, gl); | |
117 } | |
118 } while (elapsed < FLAGS_gpuMs); | |
119 | |
120 // We've overshot at least a little. Scale back linearly. | |
121 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed); | |
122 | |
123 // TODO: explain | |
124 for (int i = 0; i < FLAGS_gpuFrameLag; i++) { | |
125 time(loops, bench, canvas, gl); | |
126 } | |
127 | |
128 for (int i = 0; i < FLAGS_samples; i++) { | |
129 samples[i] = time(loops, bench, canvas, gl) / loops; | |
130 } | |
131 return loops; | |
88 } | 132 } |
89 | 133 |
90 static bool push_config_if_enabled(const char* config, SkTDArray<const char*>* c onfigs) { | 134 static bool push_config_if_enabled(const char* config, SkTDArray<const char*>* c onfigs) { |
91 if (FLAGS_config.contains(config)) { | 135 if (FLAGS_config.contains(config)) { |
92 configs->push(config); | 136 configs->push(config); |
93 return true; | 137 return true; |
94 } | 138 } |
95 return false; | 139 return false; |
96 } | 140 } |
97 | 141 |
98 static void create_surfaces(Benchmark* bench, | 142 static void create_surfaces(Benchmark* bench, |
99 SkTDArray<SkSurface*>* surfaces, | 143 SkTDArray<SkSurface*>* surfaces, |
100 SkTDArray<const char*>* configs) { | 144 SkTDArray<const char*>* configs) { |
101 | 145 |
102 if (bench->isSuitableFor(Benchmark::kNonRendering_Backend) | 146 if (bench->isSuitableFor(Benchmark::kNonRendering_Backend) |
103 && push_config_if_enabled("nonrendering", configs)) { | 147 && push_config_if_enabled("nonrendering", configs)) { |
104 surfaces->push(NULL); | 148 surfaces->push(NULL); |
105 } | 149 } |
106 | 150 |
151 const int w = bench->getSize().fX, | |
152 h = bench->getSize().fY; | |
153 const SkImageInfo _8888 = { w, h, kN32_SkColorType, kPremul_SkAlphaType }, | |
154 _565 = { w, h, kRGB_565_SkColorType, kOpaque_SkAlphaType }; | |
155 | |
107 if (bench->isSuitableFor(Benchmark::kRaster_Backend)) { | 156 if (bench->isSuitableFor(Benchmark::kRaster_Backend)) { |
108 const int w = bench->getSize().fX, | |
109 h = bench->getSize().fY; | |
110 | |
111 if (push_config_if_enabled("8888", configs)) { | 157 if (push_config_if_enabled("8888", configs)) { |
112 const SkImageInfo info = { w, h, kN32_SkColorType, kPremul_SkAlphaTy pe }; | 158 surfaces->push(SkSurface::NewRaster(_8888)); |
113 surfaces->push(SkSurface::NewRaster(info)); | |
114 } | 159 } |
115 | 160 |
116 if (push_config_if_enabled("565", configs)) { | 161 if (push_config_if_enabled("565", configs)) { |
117 const SkImageInfo info = { w, h, kRGB_565_SkColorType, kOpaque_SkAlp haType }; | 162 surfaces->push(SkSurface::NewRaster(_565)); |
118 surfaces->push(SkSurface::NewRaster(info)); | |
119 } | 163 } |
120 } | 164 } |
165 #if SK_SUPPORT_GPU | |
166 if (bench->isSuitableFor(Benchmark::kGPU_Backend) | |
bsalomon
2014/06/30 19:58:12
style nit, don't we do && on prev line?
mtklein_C
2014/06/30 23:05:29
Dunno, but Done.
| |
167 && push_config_if_enabled("gpu", configs)) { | |
168 surfaces->push(SkSurface::NewRenderTarget( | |
169 gContextFactory.get(GrContextFactory::kNative_GLContextType) , _8888, 0)); | |
170 } | |
171 #endif | |
121 } | 172 } |
122 | 173 |
174 | |
123 int tool_main(int argc, char** argv); | 175 int tool_main(int argc, char** argv); |
124 int tool_main(int argc, char** argv) { | 176 int tool_main(int argc, char** argv) { |
125 SetupCrashHandler(); | 177 SetupCrashHandler(); |
126 SkAutoGraphics ag; | 178 SkAutoGraphics ag; |
127 SkCommandLineFlags::Parse(argc, argv); | 179 SkCommandLineFlags::Parse(argc, argv); |
128 | 180 |
129 const double overhead = estimate_timer_overhead(); | 181 const double overhead = estimate_timer_overhead(); |
182 SkAutoTMalloc<double> samples(FLAGS_samples); | |
183 | |
184 // TODO: display add median, use it in --quiet mode | |
130 | 185 |
131 if (FLAGS_verbose) { | 186 if (FLAGS_verbose) { |
132 // No header. | 187 // No header. |
133 } else if (FLAGS_quiet) { | 188 } else if (FLAGS_quiet) { |
134 SkDebugf("min\tbench\tconfig\n"); | 189 SkDebugf("min\tbench\tconfig\n"); |
135 } else { | 190 } else { |
136 SkDebugf("loops\tmin\tmean\tmax\tstddev\tbench\tconfig\n"); | 191 SkDebugf("loops\tmin\tmean\tmax\tstddev\tconfig\tbench\n"); |
137 } | 192 } |
138 | 193 |
139 for (const BenchRegistry* r = BenchRegistry::Head(); r != NULL; r = r->next( )) { | 194 for (const BenchRegistry* r = BenchRegistry::Head(); r != NULL; r = r->next( )) { |
140 SkAutoTDelete<Benchmark> bench(r->factory()(NULL)); | 195 SkAutoTDelete<Benchmark> bench(r->factory()(NULL)); |
141 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) { | 196 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) { |
142 continue; | 197 continue; |
143 } | 198 } |
144 | 199 |
145 SkTDArray<SkSurface*> surfaces; | 200 SkTDArray<SkSurface*> surfaces; |
146 SkTDArray<const char*> configs; | 201 SkTDArray<const char*> configs; |
147 create_surfaces(bench.get(), &surfaces, &configs); | 202 create_surfaces(bench.get(), &surfaces, &configs); |
148 | 203 |
149 bench->preDraw(); | 204 bench->preDraw(); |
150 for (int j = 0; j < surfaces.count(); j++) { | 205 for (int j = 0; j < surfaces.count(); j++) { |
151 SkCanvas* canvas = surfaces[j] ? surfaces[j]->getCanvas() : NULL; | 206 SkCanvas* canvas = surfaces[j] ? surfaces[j]->getCanvas() : NULL; |
152 const char* config = configs[j]; | 207 const char* config = configs[j]; |
153 | 208 |
154 bench->draw(1, canvas); // Just paranoid warmup. | 209 const int loops = 0 == strcmp("gpu", config) |
155 safe_flush(canvas); | 210 ? gpu_bench(overhead, bench.get(), canvas, samples.g et()) |
156 const int loops = guess_loops(overhead, bench.get(), canvas); | 211 : cpu_bench(overhead, bench.get(), canvas, samples.g et()); |
157 | |
158 SkAutoTMalloc<double> samples(FLAGS_samples); | |
159 WallTimer timer; | |
160 for (int i = 0; i < FLAGS_samples; i++) { | |
161 timer.start(); | |
162 bench->draw(loops, canvas); | |
163 safe_flush(canvas); | |
164 timer.end(); | |
165 samples[i] = timer.fWall / loops; | |
166 } | |
167 | 212 |
168 Stats stats(samples.get(), FLAGS_samples); | 213 Stats stats(samples.get(), FLAGS_samples); |
169 | 214 |
170 if (FLAGS_verbose) { | 215 if (FLAGS_verbose) { |
171 for (int i = 0; i < FLAGS_samples; i++) { | 216 for (int i = 0; i < FLAGS_samples; i++) { |
172 SkDebugf("%s ", humanize(samples[i]).c_str()); | 217 SkDebugf("%s ", humanize(samples[i]).c_str()); |
173 } | 218 } |
174 SkDebugf("%s\n", bench->getName()); | 219 SkDebugf("%s\n", bench->getName()); |
175 } else if (FLAGS_quiet) { | 220 } else if (FLAGS_quiet) { |
176 if (configs.count() == 1) { | 221 if (configs.count() == 1) { |
177 config = ""; // Only print the config if we run the same ben ch on more than one. | 222 config = ""; // Only print the config if we run the same ben ch on more than one. |
178 } | 223 } |
179 SkDebugf("%s\t%s\t%s\n", humanize(stats.min).c_str(), bench->get Name(), config); | 224 SkDebugf("%s\t%s\t%s\n", humanize(stats.min).c_str(), bench->get Name(), config); |
180 } else { | 225 } else { |
181 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean ; | 226 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" | 227 SkDebugf("%d\t%s\t%s\t%s\t%.0f%%\t%s\t%s\n" |
183 , loops | 228 , loops |
184 , humanize(stats.min).c_str() | 229 , humanize(stats.min).c_str() |
185 , humanize(stats.mean).c_str() | 230 , humanize(stats.mean).c_str() |
186 , humanize(stats.max).c_str() | 231 , humanize(stats.max).c_str() |
187 , stddev_percent | 232 , stddev_percent |
233 , config | |
188 , bench->getName() | 234 , bench->getName() |
189 , config | |
190 ); | 235 ); |
191 } | 236 } |
192 } | 237 } |
193 surfaces.deleteAll(); | 238 surfaces.deleteAll(); |
194 } | 239 } |
195 | 240 |
196 return 0; | 241 return 0; |
197 } | 242 } |
198 | 243 |
199 #if !defined SK_BUILD_FOR_IOS | 244 #if !defined SK_BUILD_FOR_IOS |
200 int main(int argc, char * const argv[]) { | 245 int main(int argc, char * const argv[]) { |
201 return tool_main(argc, (char**) argv); | 246 return tool_main(argc, (char**) argv); |
202 } | 247 } |
203 #endif | 248 #endif |
OLD | NEW |