Blender V5.0
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#pragma once
6
7#include <cassert>
8
9#include "util/thread.h"
10#include "util/unique_ptr.h"
11#include "util/vector.h"
12
14
47
48/* Contains the current execution state of a worker thread.
49 * These values are constantly updated by the worker.
50 * Periodically the profiler thread will wake up, read them
51 * and update its internal counters based on it.
52 *
53 * Atomics aren't needed here since we're only doing direct
54 * writes and reads to (4-byte-aligned) uint32_t, which is
55 * guaranteed to be atomic on x86 since the 486.
56 * Memory ordering is not guaranteed but does not matter.
57 *
58 * And even on other architectures, the extremely rare corner
59 * case of reading an intermediate state could at worst result
60 * in a single incorrect sample. */
62 volatile uint32_t event = PROFILING_UNKNOWN;
63 volatile int32_t shader = -1;
64 volatile int32_t object = -1;
65 volatile bool active = false;
66
69};
70
71class Profiler {
72 public:
73 Profiler();
74 ~Profiler();
75
76 void reset(const int num_shaders, const int num_objects);
77
78 void start();
79 void stop();
80
83
85 bool get_shader(const int shader, uint64_t &samples, uint64_t &hits);
86 bool get_object(const int object, uint64_t &samples, uint64_t &hits);
87
88 bool active() const;
89
90 protected:
91 void run();
92
93 /* Tracks how often the worker was in each ProfilingEvent while sampling,
94 * so multiplying the values by the sample frequency (currently 1ms)
95 * gives the approximate time spent in each state. */
99
100 /* Tracks the total amounts every object/shader was hit.
101 * Used to evaluate relative cost, written by the render thread.
102 * Indexed by the shader and object IDs that the kernel also uses
103 * to index __object_flag and __shaders. */
106
107 volatile bool do_stop_worker;
109
112};
113
115 public:
117 {
118 previous_event = state->event;
119 state->event = event;
120 }
121
123 {
124 state->event = previous_event;
125 }
126
128 {
129 state->event = event;
130 }
131
132 protected:
135};
136
138 public:
143
145 {
146 state->object = -1;
147 state->shader = -1;
148 }
149
150 void set_shader(const int object, const int shader)
151 {
152 if (state->active) {
153 state->shader = shader;
154 state->object = object;
155
156 if (shader >= 0) {
157 assert(shader < int(state->shader_hits.size()));
158 state->shader_hits[shader]++;
159 }
160
161 if (object >= 0) {
162 assert(object < int(state->object_hits.size()));
163 state->object_hits[object]++;
164 }
165 }
166 }
167};
168
unsigned long long int uint64_t
void reset()
clear internal cached data and reset random seed
vector< uint64_t > shader_hits
Definition profiling.h:104
bool get_shader(const int shader, uint64_t &samples, uint64_t &hits)
vector< uint64_t > object_hits
Definition profiling.h:105
vector< uint64_t > event_samples
Definition profiling.h:96
uint64_t get_event(ProfilingEvent event)
void add_state(ProfilingState *state)
Definition profiling.cpp:94
vector< ProfilingState * > states
Definition profiling.h:111
volatile bool do_stop_worker
Definition profiling.h:107
void remove_state(ProfilingState *state)
vector< uint64_t > object_samples
Definition profiling.h:98
void run()
Definition profiling.cpp:21
thread_mutex mutex
Definition profiling.h:110
void start()
Definition profiling.cpp:77
bool active() const
unique_ptr< thread > worker
Definition profiling.h:108
bool get_object(const int object, uint64_t &samples, uint64_t &hits)
vector< uint64_t > shader_samples
Definition profiling.h:97
void stop()
Definition profiling.cpp:84
ProfilingHelper(ProfilingState *state, ProfilingEvent event)
Definition profiling.h:116
ProfilingState * state
Definition profiling.h:133
void set_event(ProfilingEvent event)
Definition profiling.h:127
uint32_t previous_event
Definition profiling.h:134
void set_shader(const int object, const int shader)
Definition profiling.h:150
ProfilingWithShaderHelper(ProfilingState *state, ProfilingEvent event)
Definition profiling.h:139
#define CCL_NAMESPACE_END
#define assert(assertion)
static ulong state[N]
ProfilingEvent
Definition profiling.h:15
@ PROFILING_SHADE_SURFACE_SETUP
Definition profiling.h:25
@ PROFILING_NUM_EVENTS
Definition profiling.h:45
@ 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
vector< uint64_t > object_hits
Definition profiling.h:68
volatile int32_t shader
Definition profiling.h:63
vector< uint64_t > shader_hits
Definition profiling.h:67
volatile bool active
Definition profiling.h:65
std::mutex thread_mutex
Definition thread.h:27