Blender V5.3
stats.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "scene/stats.h"
6#include "scene/object.h"
7#include "util/algorithm.h"
8
9#include "util/string.h"
10
11#include <cinttypes>
12
14
15static int kIndentNumSpaces = 2;
16
17/* Named size entry. */
18
19namespace {
20
21bool namedSizeEntryComparator(const NamedSizeEntry &a, const NamedSizeEntry &b)
22{
23 /* We sort in descending order. */
24 return a.size > b.size;
25}
26
27bool namedTimeEntryComparator(const NamedTimeEntry &a, const NamedTimeEntry &b)
28{
29 /* We sort in descending order. */
30 return a.time > b.time;
31}
32
33bool namedTimeSampleEntryComparator(const NamedNestedSampleStats &a,
35{
36 return a.sum_samples > b.sum_samples;
37}
38
39bool namedSampleCountPairComparator(const NamedSampleCountPair &a, const NamedSampleCountPair &b)
40{
41 return a.samples > b.samples;
42}
43
44} // namespace
45
47
48NamedSizeEntry::NamedSizeEntry(const string &name, const size_t size) : name(name), size(size) {}
49
51
52NamedTimeEntry::NamedTimeEntry(const string &name, const double time) : name(name), time(time) {}
53
54/* Named size statistics. */
55
57
59{
60 total_size += entry.size;
61 entries.push_back(entry);
62}
63
64string NamedSizeStats::full_report(const int indent_level)
65{
66 const string indent(indent_level * kIndentNumSpaces, ' ');
67 const string double_indent = indent + indent;
68 string result;
69 result += string_printf("%sTotal memory: %s (%s)\n",
70 indent.c_str(),
73 sort(entries.begin(), entries.end(), namedSizeEntryComparator);
74 for (const NamedSizeEntry &entry : entries) {
75 result += string_printf("%s%-32s %s (%s)\n",
76 double_indent.c_str(),
77 entry.name.c_str(),
78 string_human_readable_size(entry.size).c_str(),
79 string_human_readable_number(entry.size).c_str());
80 }
81 return result;
82}
83
84string NamedTimeStats::full_report(const int indent_level)
85{
86 const string indent(indent_level * kIndentNumSpaces, ' ');
87 const string double_indent = indent + indent;
88 string result;
89 result += string_printf("%sTotal time: %fs\n", indent.c_str(), total_time);
90 sort(entries.begin(), entries.end(), namedTimeEntryComparator);
91 for (const NamedTimeEntry &entry : entries) {
92 result += string_printf(
93 "%s%-40s %fs\n", double_indent.c_str(), entry.name.c_str(), entry.time);
94 }
95 return result;
96}
97
98/* Named time sample statistics. */
99
101
103 : name(name), self_samples(samples), sum_samples(samples)
104{
105}
106
108{
109 entries.push_back(NamedNestedSampleStats(name_, samples_));
110 return entries[entries.size() - 1];
111}
112
114{
116 for (NamedNestedSampleStats &entry : entries) {
117 entry.update_sum();
118 sum_samples += entry.sum_samples;
119 }
120}
121
122string NamedNestedSampleStats::full_report(const int indent_level, uint64_t total_samples)
123{
124 update_sum();
125
126 if (total_samples == 0) {
127 total_samples = sum_samples;
128 }
129
130 const string indent(indent_level * kIndentNumSpaces, ' ');
131
132 const double sum_percent = 100 * ((double)sum_samples) / total_samples;
133 const double sum_seconds = sum_samples * 0.001;
134 const double self_percent = 100 * ((double)self_samples) / total_samples;
135 const double self_seconds = self_samples * 0.001;
136 const string info = string_printf("%-32s: Total %3.2f%% (%.2fs), Self %3.2f%% (%.2fs)\n",
137 name.c_str(),
138 sum_percent,
139 sum_seconds,
140 self_percent,
141 self_seconds);
142 string result = indent + info;
143
144 sort(entries.begin(), entries.end(), namedTimeSampleEntryComparator);
145 for (NamedNestedSampleStats &entry : entries) {
146 result += entry.full_report(indent_level + 1, total_samples);
147 }
148 return result;
149}
150
151/* Named sample count pairs. */
152
159
161
162void NamedSampleCountStats::add(const ustring &name, const uint64_t samples, const uint64_t hits)
163{
164 const entry_map::iterator entry = entries.find(name);
165 if (entry != entries.end()) {
166 entry->second.samples += samples;
167 entry->second.hits += hits;
168 return;
169 }
170 entries.emplace(name, NamedSampleCountPair(name, samples, hits));
171}
172
173string NamedSampleCountStats::full_report(const int indent_level)
174{
175 const string indent(indent_level * kIndentNumSpaces, ' ');
176
177 vector<NamedSampleCountPair> sorted_entries;
178 sorted_entries.reserve(entries.size());
179
180 uint64_t total_hits = 0;
181 uint64_t total_samples = 0;
182 for (entry_map::const_reference entry : entries) {
183 const NamedSampleCountPair &pair = entry.second;
184
185 total_hits += pair.hits;
186 total_samples += pair.samples;
187
188 sorted_entries.push_back(pair);
189 }
190 const double avg_samples_per_hit = ((double)total_samples) / total_hits;
191
192 sort(sorted_entries.begin(), sorted_entries.end(), namedSampleCountPairComparator);
193
194 string result;
195 for (const NamedSampleCountPair &entry : sorted_entries) {
196 const double seconds = entry.samples * 0.001;
197 const double relative = ((double)entry.samples) / (entry.hits * avg_samples_per_hit);
198
199 result += indent +
201 "%-32s: %.2fs (Relative cost: %.2f)\n", entry.name.c_str(), seconds, relative);
202 }
203 return result;
204}
205
206/* Mesh statistics. */
207
208MeshStats::MeshStats() = default;
209
210string MeshStats::full_report(const int indent_level)
211{
212 const string indent(indent_level * kIndentNumSpaces, ' ');
213 string result;
214 result += indent + "Geometry:\n" + geometry.full_report(indent_level + 1);
215 return result;
216}
217
218/* Image statistics. */
219
220ImageStats::ImageStats() = default;
221
222string ImageStats::full_report(const int indent_level)
223{
224 const string indent(indent_level * kIndentNumSpaces, ' ');
225 const string double_indent = indent + string(kIndentNumSpaces, ' ');
226 const string triple_indent = double_indent + string(kIndentNumSpaces, ' ');
227 string result;
228
229 /* Full images (non-tiled). */
230 if (full_images.total_size > 0) {
231 result += indent + "Full Images:\n";
233 sort(sorted_entries.begin(), sorted_entries.end(), namedSizeEntryComparator);
234 for (const NamedSizeEntry &entry : sorted_entries) {
235 result += string_printf("%s%-32s %s (%s)\n",
236 double_indent.c_str(),
237 entry.name.c_str(),
238 string_human_readable_size(entry.size).c_str(),
239 string_human_readable_number(entry.size).c_str());
240 }
241 }
242
243 /* Tiled image statistics. */
244 if (!tiled_images.empty()) {
245 result += indent + "Tiled Images:\n";
246 for (const ImageTileStats &img : tiled_images) {
247 result += string_printf("%s%-32s %s (%s)\n",
248 double_indent.c_str(),
249 img.name.c_str(),
250 string_human_readable_size(img.size).c_str(),
251 string_human_readable_number(img.size).c_str());
252 for (const ImageMipLevelStats &mip : img.mip_levels) {
253 const double mip_percent = (mip.tiles_total > 0) ?
254 (100.0 * mip.tiles_loaded / mip.tiles_total) :
255 0.0;
256 result += string_printf("%s%dx%d: %d / %d tiles (%.1f%%)\n",
257 triple_indent.c_str(),
258 mip.width,
259 mip.height,
260 mip.tiles_loaded,
261 mip.tiles_total,
262 mip_percent);
263 }
264 }
265 }
266
267 /* Summary. */
268 if (full_images.total_size > 0) {
269 result += string_printf("%sFull image memory: %s (%s)\n",
270 indent.c_str(),
273 }
274 if (tiled_images_size > 0) {
275 result += string_printf("%sTiled image memory: %s (%s)\n",
276 indent.c_str(),
279 }
280 if (tiled_images_peak_size > 0) {
281 result += string_printf("%sPeak tiled image memory: %s (%s)\n",
282 indent.c_str(),
285 }
286 if (overhead_size > 0) {
287 result += string_printf("%sOverhead image memory: %s (%s)\n",
288 indent.c_str(),
291 }
292 const size_t total_memory = full_images.total_size + tiled_images_size + overhead_size;
293 result += string_printf("%sTotal image memory: %s (%s)\n",
294 indent.c_str(),
295 string_human_readable_size(total_memory).c_str(),
296 string_human_readable_number(total_memory).c_str());
297 const size_t peak_total_memory = full_images.total_size + tiled_images_peak_size + overhead_size;
298 if (tiled_images_peak_size > 0) {
299 result += string_printf("%sPeak total image memory: %s (%s)\n",
300 indent.c_str(),
301 string_human_readable_size(peak_total_memory).c_str(),
302 string_human_readable_number(peak_total_memory).c_str());
303 }
304
305 if (eviction.tiles_loaded > 0) {
306 result += string_printf(
307 "%sTiles:\n"
308 "%sLoaded: %" PRId64
309 "\n"
310 "%sPeak: %" PRId64
311 " (%.1f%%)\n"
312 "%sEvicted: %" PRId64
313 " (%.1f%%)\n"
314 "%sReloaded: %" PRId64 " (%.1f%%)\n",
315 indent.c_str(),
316 double_indent.c_str(),
318 double_indent.c_str(),
321 double_indent.c_str(),
324 double_indent.c_str(),
327 }
328
329 return result;
330}
331
332/* Overall statistics. */
333
338
340{
341 has_profiling = true;
342
343 kernel = NamedNestedSampleStats("Total render time", prof.get_event(PROFILING_UNKNOWN));
344 kernel.add_entry("Ray setup", prof.get_event(PROFILING_RAY_SETUP));
345 kernel.add_entry("Intersect Closest", prof.get_event(PROFILING_INTERSECT_CLOSEST));
346 kernel.add_entry("Intersect Shadow", prof.get_event(PROFILING_INTERSECT_SHADOW));
347 kernel.add_entry("Intersect Subsurface", prof.get_event(PROFILING_INTERSECT_SUBSURFACE));
348 kernel.add_entry("Intersect Volume Stack", prof.get_event(PROFILING_INTERSECT_VOLUME_STACK));
349 kernel.add_entry("Intersect Blocked Light", prof.get_event(PROFILING_INTERSECT_DEDICATED_LIGHT));
350
351 NamedNestedSampleStats &surface = kernel.add_entry("Shade Surface", 0);
352 surface.add_entry("Setup", prof.get_event(PROFILING_SHADE_SURFACE_SETUP));
353 surface.add_entry("Shader Evaluation", prof.get_event(PROFILING_SHADE_SURFACE_EVAL));
354 surface.add_entry("Render Passes", prof.get_event(PROFILING_SHADE_SURFACE_PASSES));
355 surface.add_entry("Direct Light", prof.get_event(PROFILING_SHADE_SURFACE_DIRECT_LIGHT));
356 surface.add_entry("Indirect Light", prof.get_event(PROFILING_SHADE_SURFACE_INDIRECT_LIGHT));
357 surface.add_entry("Ambient Occlusion", prof.get_event(PROFILING_SHADE_SURFACE_AO));
358
359 NamedNestedSampleStats &volume = kernel.add_entry("Shade Volume", 0);
360 volume.add_entry("Setup", prof.get_event(PROFILING_SHADE_VOLUME_SETUP));
361 volume.add_entry("Integrate", prof.get_event(PROFILING_SHADE_VOLUME_INTEGRATE));
362 volume.add_entry("Direct Light", prof.get_event(PROFILING_SHADE_VOLUME_DIRECT_LIGHT));
363 volume.add_entry("Indirect Light", prof.get_event(PROFILING_SHADE_VOLUME_INDIRECT_LIGHT));
364
365 NamedNestedSampleStats &shadow = kernel.add_entry("Shade Shadow", 0);
366 shadow.add_entry("Setup", prof.get_event(PROFILING_SHADE_SHADOW_SETUP));
367 shadow.add_entry("Surface", prof.get_event(PROFILING_SHADE_SHADOW_SURFACE));
368 shadow.add_entry("Volume", prof.get_event(PROFILING_SHADE_SHADOW_VOLUME));
369 shadow.add_entry("Blocked Light", prof.get_event(PROFILING_SHADE_DEDICATED_LIGHT));
370
371 NamedNestedSampleStats &light = kernel.add_entry("Shade Light", 0);
372 light.add_entry("Setup", prof.get_event(PROFILING_SHADE_LIGHT_SETUP));
373 light.add_entry("Shader Evaluation", prof.get_event(PROFILING_SHADE_LIGHT_EVAL));
374
375 shaders.entries.clear();
376 for (Shader *shader : scene->shaders) {
377 uint64_t samples;
378 uint64_t hits;
379 if (prof.get_shader(shader->id, samples, hits)) {
380 shaders.add(shader->name, samples, hits);
381 }
382 }
383
384 objects.entries.clear();
385 for (Object *object : scene->objects) {
386 uint64_t samples;
387 uint64_t hits;
388 if (prof.get_object(object->get_device_index(), samples, hits)) {
389 objects.add(object->name, samples, hits);
390 }
391 }
392}
393
395{
396 string result;
397 result += "Mesh statistics:\n" + mesh.full_report(1);
398 result += "Image statistics:\n" + image.full_report(1);
399 if (has_profiling) {
400 result += "Kernel statistics:\n" + kernel.full_report(1);
401 result += "Shader statistics:\n" + shaders.full_report(1);
402 result += "Object statistics:\n" + objects.full_report(1);
403 }
404 else {
405 result += "Profiling information not available (only works with CPU rendering)";
406 }
407 return result;
408}
409
411
412string UpdateTimeStats::full_report(const int indent_level)
413{
414 return times.full_report(indent_level + 1);
415}
416
418
420{
421 string result;
422 result += "Scene:\n" + scene.full_report(1);
423 result += "Geometry:\n" + geometry.full_report(1);
424 result += "Light:\n" + light.full_report(1);
425 result += "Object:\n" + object.full_report(1);
426 result += "Image:\n" + image.full_report(1);
427 result += "Background:\n" + background.full_report(1);
428 result += "Bake:\n" + bake.full_report(1);
429 result += "Camera:\n" + camera.full_report(1);
430 result += "Film:\n" + film.full_report(1);
431 result += "Integrator:\n" + integrator.full_report(1);
432 result += "OSL:\n" + osl.full_report(1);
433 result += "Particles:\n" + particles.full_report(1);
434 result += "SVM:\n" + svm.full_report(1);
435 result += "Tables:\n" + tables.full_report(1);
436 result += "Procedurals:\n" + procedurals.full_report(1);
437 return result;
438}
439
441{
443 image.times.clear();
444 light.times.clear();
445 object.times.clear();
447 bake.times.clear();
449 film.times.clear();
451 osl.times.clear();
453 scene.times.clear();
454 svm.times.clear();
457}
458
unsigned long long int uint64_t
void sort(btMatrix3x3 &U, btVector3 &sigma, btMatrix3x3 &V, int t)
Helper function of 3X3 SVD for sorting singular values.
vector< ImageTileStats > tiled_images
size_t tiled_images_peak_size
ImageEvictionStats eviction
size_t tiled_images_size
string full_report(const int indent_level=0)
Definition stats.cpp:222
NamedSizeStats full_images
size_t overhead_size
string full_report(const int indent_level=0)
Definition stats.cpp:210
NamedSizeStats geometry
vector< NamedNestedSampleStats > entries
NamedNestedSampleStats & add_entry(const string &name, const uint64_t samples)
Definition stats.cpp:107
string full_report(const int indent_level=0, const uint64_t total_samples=0)
Definition stats.cpp:122
NamedSampleCountPair(const ustring &name, const uint64_t samples, const uint64_t hits)
Definition stats.cpp:153
string full_report(const int indent_level=0)
Definition stats.cpp:173
void add(const ustring &name, const uint64_t samples, const uint64_t hits)
Definition stats.cpp:162
void add_entry(const NamedSizeEntry &entry)
Definition stats.cpp:58
string full_report(const int indent_level=0)
Definition stats.cpp:64
size_t total_size
Definition scene/stats.h:54
vector< NamedSizeEntry > entries
Definition scene/stats.h:59
double total_time
Definition scene/stats.h:77
string full_report(const int indent_level=0)
Definition stats.cpp:84
vector< NamedTimeEntry > entries
Definition scene/stats.h:82
int get_device_index() const
bool get_shader(const int shader, uint64_t &samples, uint64_t &hits)
uint64_t get_event(ProfilingEvent event)
bool get_object(const int object, uint64_t &samples, uint64_t &hits)
void collect_profiling(Scene *scene, Profiler &prof)
Definition stats.cpp:339
bool has_profiling
string full_report()
Definition stats.cpp:394
NamedSampleCountStats shaders
NamedNestedSampleStats kernel
MeshStats mesh
ImageStats image
NamedSampleCountStats objects
UpdateTimeStats tables
UpdateTimeStats image
UpdateTimeStats background
UpdateTimeStats light
UpdateTimeStats bake
UpdateTimeStats film
UpdateTimeStats camera
UpdateTimeStats geometry
string full_report()
Definition stats.cpp:419
UpdateTimeStats particles
UpdateTimeStats procedurals
UpdateTimeStats integrator
UpdateTimeStats osl
UpdateTimeStats scene
UpdateTimeStats svm
Definition scene.h:133
unique_ptr_vector< Shader > shaders
Definition scene.h:158
unique_ptr_vector< Object > objects
Definition scene.h:162
string full_report(const int indent_level=0)
Definition stats.cpp:412
NamedTimeStats times
#define CCL_NAMESPACE_END
#define PRId64
Definition inttypes.h:78
@ PROFILING_SHADE_SURFACE_SETUP
Definition profiling.h:25
@ PROFILING_SHADE_SHADOW_SURFACE
Definition profiling.h:39
@ PROFILING_SHADE_VOLUME_INTEGRATE
Definition profiling.h:34
@ PROFILING_SHADE_SHADOW_VOLUME
Definition profiling.h:40
@ PROFILING_INTERSECT_SUBSURFACE
Definition profiling.h:20
@ PROFILING_INTERSECT_SHADOW
Definition profiling.h:21
@ PROFILING_INTERSECT_CLOSEST
Definition profiling.h:19
@ PROFILING_INTERSECT_DEDICATED_LIGHT
Definition profiling.h:23
@ PROFILING_SHADE_VOLUME_SETUP
Definition profiling.h:33
@ PROFILING_SHADE_SURFACE_EVAL
Definition profiling.h:26
@ PROFILING_SHADE_SURFACE_INDIRECT_LIGHT
Definition profiling.h:28
@ PROFILING_SHADE_SURFACE_AO
Definition profiling.h:29
@ PROFILING_SHADE_SURFACE_DIRECT_LIGHT
Definition profiling.h:27
@ PROFILING_SHADE_LIGHT_SETUP
Definition profiling.h:42
@ PROFILING_RAY_SETUP
Definition profiling.h:17
@ PROFILING_SHADE_SHADOW_SETUP
Definition profiling.h:38
@ PROFILING_SHADE_DEDICATED_LIGHT
Definition profiling.h:31
@ PROFILING_INTERSECT_VOLUME_STACK
Definition profiling.h:22
@ PROFILING_SHADE_VOLUME_DIRECT_LIGHT
Definition profiling.h:35
@ PROFILING_UNKNOWN
Definition profiling.h:16
@ PROFILING_SHADE_SURFACE_PASSES
Definition profiling.h:30
@ PROFILING_SHADE_LIGHT_EVAL
Definition profiling.h:43
@ PROFILING_SHADE_VOLUME_INDIRECT_LIGHT
Definition profiling.h:36
static CCL_NAMESPACE_BEGIN int kIndentNumSpaces
Definition stats.cpp:15
string string_human_readable_size(size_t size)
Definition string.cpp:333
string string_human_readable_number(size_t num)
Definition string.cpp:352
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
ustring name
Definition graph/node.h:178