Blender V4.3
util/profiling.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#ifndef __UTIL_PROFILING_H__
6#define __UTIL_PROFILING_H__
7
8#include <atomic>
9
10#include "util/map.h"
11#include "util/thread.h"
12#include "util/vector.h"
13
15
48
49/* Contains the current execution state of a worker thread.
50 * These values are constantly updated by the worker.
51 * Periodically the profiler thread will wake up, read them
52 * and update its internal counters based on it.
53 *
54 * Atomics aren't needed here since we're only doing direct
55 * writes and reads to (4-byte-aligned) uint32_t, which is
56 * guaranteed to be atomic on x86 since the 486.
57 * Memory ordering is not guaranteed but does not matter.
58 *
59 * And even on other architectures, the extremely rare corner
60 * case of reading an intermediate state could at worst result
61 * in a single incorrect sample. */
63 volatile uint32_t event = PROFILING_UNKNOWN;
64 volatile int32_t shader = -1;
65 volatile int32_t object = -1;
66 volatile bool active = false;
67
70};
71
72class Profiler {
73 public:
74 Profiler();
75 ~Profiler();
76
77 void reset(int num_shaders, int num_objects);
78
79 void start();
80 void stop();
81
84
86 bool get_shader(int shader, uint64_t &samples, uint64_t &hits);
87 bool get_object(int object, uint64_t &samples, uint64_t &hits);
88
89 bool active() const;
90
91 protected:
92 void run();
93
94 /* Tracks how often the worker was in each ProfilingEvent while sampling,
95 * so multiplying the values by the sample frequency (currently 1ms)
96 * gives the approximate time spent in each state. */
100
101 /* Tracks the total amounts every object/shader was hit.
102 * Used to evaluate relative cost, written by the render thread.
103 * Indexed by the shader and object IDs that the kernel also uses
104 * to index __object_flag and __shaders. */
107
108 volatile bool do_stop_worker;
110
113};
114
116 public:
122
127
128 inline void set_event(ProfilingEvent event)
129 {
130 state->event = event;
131 }
132
133 protected:
136};
137
139 public:
144
146 {
147 state->object = -1;
148 state->shader = -1;
149 }
150
151 inline void set_shader(int object, int shader)
152 {
153 if (state->active) {
154 state->shader = shader;
155 state->object = object;
156
157 if (shader >= 0) {
158 assert(shader < state->shader_hits.size());
159 state->shader_hits[shader]++;
160 }
161
162 if (object >= 0) {
163 assert(object < state->object_hits.size());
164 state->object_hits[object]++;
165 }
166 }
167 }
168};
169
171
172#endif /* __UTIL_PROFILING_H__ */
void reset()
clear internal cached data and reset random seed
vector< uint64_t > shader_hits
vector< uint64_t > object_hits
vector< uint64_t > event_samples
uint64_t get_event(ProfilingEvent event)
void add_state(ProfilingState *state)
Definition profiling.cpp:93
vector< ProfilingState * > states
volatile bool do_stop_worker
void remove_state(ProfilingState *state)
vector< uint64_t > object_samples
void run()
Definition profiling.cpp:19
thread_mutex mutex
void start()
Definition profiling.cpp:75
bool get_shader(int shader, uint64_t &samples, uint64_t &hits)
bool active() const
thread * worker
bool get_object(int object, uint64_t &samples, uint64_t &hits)
vector< uint64_t > shader_samples
void stop()
Definition profiling.cpp:82
ProfilingHelper(ProfilingState *state, ProfilingEvent event)
ProfilingState * state
void set_event(ProfilingEvent event)
uint32_t previous_event
void set_shader(int object, int shader)
ProfilingWithShaderHelper(ProfilingState *state, ProfilingEvent event)
#define CCL_NAMESPACE_END
static ulong state[N]
unsigned int uint32_t
Definition stdint.h:80
signed int int32_t
Definition stdint.h:77
unsigned __int64 uint64_t
Definition stdint.h:90
vector< uint64_t > object_hits
volatile uint32_t event
volatile int32_t shader
vector< uint64_t > shader_hits
volatile int32_t object
volatile bool active
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition thread.h:29
ProfilingEvent
@ PROFILING_SHADE_SURFACE_SETUP
@ PROFILING_NUM_EVENTS
@ PROFILING_SHADE_SHADOW_SURFACE
@ PROFILING_SHADE_VOLUME_INTEGRATE
@ PROFILING_SHADE_SHADOW_VOLUME
@ PROFILING_INTERSECT_SUBSURFACE
@ PROFILING_INTERSECT_SHADOW
@ PROFILING_INTERSECT_CLOSEST
@ PROFILING_INTERSECT_DEDICATED_LIGHT
@ PROFILING_SHADE_VOLUME_SETUP
@ PROFILING_SHADE_SURFACE_EVAL
@ PROFILING_SHADE_SURFACE_INDIRECT_LIGHT
@ PROFILING_SHADE_SURFACE_AO
@ PROFILING_SHADE_SURFACE_DIRECT_LIGHT
@ PROFILING_SHADE_LIGHT_SETUP
@ PROFILING_RAY_SETUP
@ PROFILING_SHADE_SHADOW_SETUP
@ PROFILING_SHADE_DEDICATED_LIGHT
@ PROFILING_INTERSECT_VOLUME_STACK
@ PROFILING_SHADE_VOLUME_DIRECT_LIGHT
@ PROFILING_UNKNOWN
@ PROFILING_SHADE_SURFACE_PASSES
@ PROFILING_SHADE_LIGHT_EVAL
@ PROFILING_SHADE_VOLUME_INDIRECT_LIGHT