Blender  V3.3
util/profiling.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #ifndef __UTIL_PROFILING_H__
5 #define __UTIL_PROFILING_H__
6 
7 #include <atomic>
8 
9 #include "util/map.h"
10 #include "util/thread.h"
11 #include "util/vector.h"
12 
14 
18 
23 
30 
35 
39 
42 
44 };
45 
46 /* Contains the current execution state of a worker thread.
47  * These values are constantly updated by the worker.
48  * Periodically the profiler thread will wake up, read them
49  * and update its internal counters based on it.
50  *
51  * Atomics aren't needed here since we're only doing direct
52  * writes and reads to (4-byte-aligned) uint32_t, which is
53  * guaranteed to be atomic on x86 since the 486.
54  * Memory ordering is not guaranteed but does not matter.
55  *
56  * And even on other architectures, the extremely rare corner
57  * case of reading an intermediate state could at worst result
58  * in a single incorrect sample. */
60  volatile uint32_t event = PROFILING_UNKNOWN;
61  volatile int32_t shader = -1;
62  volatile int32_t object = -1;
63  volatile bool active = false;
64 
67 };
68 
69 class Profiler {
70  public:
71  Profiler();
72  ~Profiler();
73 
74  void reset(int num_shaders, int num_objects);
75 
76  void start();
77  void stop();
78 
81 
83  bool get_shader(int shader, uint64_t &samples, uint64_t &hits);
84  bool get_object(int object, uint64_t &samples, uint64_t &hits);
85 
86  bool active() const;
87 
88  protected:
89  void run();
90 
91  /* Tracks how often the worker was in each ProfilingEvent while sampling,
92  * so multiplying the values by the sample frequency (currently 1ms)
93  * gives the approximate time spent in each state. */
97 
98  /* Tracks the total amounts every object/shader was hit.
99  * Used to evaluate relative cost, written by the render thread.
100  * Indexed by the shader and object IDs that the kernel also uses
101  * to index __object_flag and __shaders. */
104 
105  volatile bool do_stop_worker;
107 
110 };
111 
113  public:
115  {
117  state->event = event;
118  }
119 
121  {
123  }
124 
125  inline void set_event(ProfilingEvent event)
126  {
127  state->event = event;
128  }
129 
130  protected:
133 };
134 
136  public:
138  : ProfilingHelper(state, event)
139  {
140  }
141 
143  {
144  state->object = -1;
145  state->shader = -1;
146  }
147 
148  inline void set_shader(int object, int shader)
149  {
150  if (state->active) {
151  state->shader = shader;
152  state->object = object;
153 
154  if (shader >= 0) {
155  assert(shader < state->shader_hits.size());
156  state->shader_hits[shader]++;
157  }
158 
159  if (object >= 0) {
160  assert(object < state->object_hits.size());
161  state->object_hits[object]++;
162  }
163  }
164  }
165 };
166 
168 
169 #endif /* __UTIL_PROFILING_H__ */
vector< uint64_t > shader_hits
vector< uint64_t > object_hits
vector< uint64_t > event_samples
uint64_t get_event(ProfilingEvent event)
Definition: profiling.cpp:133
void add_state(ProfilingState *state)
Definition: profiling.cpp:94
vector< ProfilingState * > states
volatile bool do_stop_worker
void remove_state(ProfilingState *state)
Definition: profiling.cpp:113
vector< uint64_t > object_samples
void run()
Definition: profiling.cpp:20
thread_mutex mutex
void start()
Definition: profiling.cpp:76
bool get_shader(int shader, uint64_t &samples, uint64_t &hits)
Definition: profiling.cpp:139
bool active() const
Definition: profiling.cpp:161
void reset(int num_shaders, int num_objects)
Definition: profiling.cpp:56
thread * worker
bool get_object(int object, uint64_t &samples, uint64_t &hits)
Definition: profiling.cpp:150
vector< uint64_t > shader_samples
void stop()
Definition: profiling.cpp:83
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)
Definition: thread.h:34
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
const int state
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:27
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_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_INTERSECT_VOLUME_STACK
@ PROFILING_SHADE_VOLUME_DIRECT_LIGHT
@ PROFILING_UNKNOWN
@ PROFILING_SHADE_SURFACE_PASSES
@ PROFILING_SHADE_LIGHT_EVAL
@ PROFILING_SHADE_VOLUME_INDIRECT_LIGHT