Fx2D
A C++20 2D rigid-body physics engine
Loading...
Searching...
No Matches
Profile.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <cstddef>
4
5
// Per-phase step profiler. The counters always exist so tools can link, but are only written
6
// when built with -DFX2D_PROFILE=ON; otherwise FX2D_PROF_SCOPE expands to nothing. Phases are
7
// inclusive and partition one step, summing to just under StepTotal.
8
namespace
FxProfile
{
9
10
enum
Slot
:
int
{
11
BroadPhase
= 0,
// tree sync + pair query
12
NarrowPhase
,
// collision_check / speculative_contact_check, warm-start cache lookup
13
PositionSolve
,
// resolve_penetration
14
Constraints
,
// FxConstraint::resolve over the constraint registry
15
Integration
,
// entity->step(): forces, pose integration, shape world-pose refresh
16
VelocityDerive
,
// (pose - prev_pose) / dt
17
VelocityPasses
,
// init_velocity_pass, warm_start, resolve_velocities sweeps
18
Bookkeeping
,
// impulse cache write-back, cache eviction, sleep timers, events
19
StepTotal
,
// the whole of FxScene::step
20
SlotCount
21
};
22
23
// True when the library was compiled with the profiler enabled.
24
bool
enabled
();
25
26
// Human-readable name for a slot, for report tables.
27
const
char
*
slot_name
(
Slot
slot);
28
29
// Accumulated milliseconds in a slot since the last reset().
30
double
ms
(
Slot
slot);
31
32
// Number of completed steps since the last reset(), so callers can report per-step figures.
33
std::size_t
steps
();
34
35
// Zero every accumulator and the step count. Call between measured runs.
36
void
reset
();
37
38
// Adds `elapsed_ms` to a slot. Called by the scope timer; public so tools can fold in their own
39
// measurements if they ever need to.
40
void
add
(
Slot
slot,
double
elapsed_ms);
41
42
// Marks one completed step.
43
void
count_step
();
44
45
}
// namespace FxProfile
46
47
#ifdef FX2D_PROFILE
48
#include <chrono>
49
50
namespace
FxProfile
{
51
// Accumulates its lifetime into a slot. Scope-based so an early return still records.
52
class
ScopeTimer {
53
public
:
54
explicit
ScopeTimer(Slot slot) : m_slot(slot), m_start(std::chrono::steady_clock::now()) {}
55
~ScopeTimer() {
56
add
(m_slot,
57
std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - m_start)
58
.count());
59
}
60
ScopeTimer(
const
ScopeTimer&) =
delete
;
61
ScopeTimer& operator=(
const
ScopeTimer&) =
delete
;
62
63
private
:
64
Slot
m_slot;
65
std::chrono::steady_clock::time_point m_start;
66
};
67
}
// namespace FxProfile
68
69
#define FX2D_PROF_CONCAT_INNER(a, b) a##b
70
#define FX2D_PROF_CONCAT(a, b) FX2D_PROF_CONCAT_INNER(a, b)
71
#define FX2D_PROF_SCOPE(slot) \
72
::FxProfile::ScopeTimer FX2D_PROF_CONCAT(fx2d_prof_timer_, __LINE__)(::FxProfile::slot)
73
#define FX2D_PROF_STEP() ::FxProfile::count_step()
74
#else
75
#define FX2D_PROF_SCOPE(slot) ((void)0)
76
#define FX2D_PROF_STEP() ((void)0)
77
#endif
FxProfile
Definition
Profile.h:8
FxProfile::steps
std::size_t steps()
FxProfile::slot_name
const char * slot_name(Slot slot)
FxProfile::add
void add(Slot slot, double elapsed_ms)
FxProfile::count_step
void count_step()
FxProfile::Slot
Slot
Definition
Profile.h:10
FxProfile::NarrowPhase
@ NarrowPhase
Definition
Profile.h:12
FxProfile::StepTotal
@ StepTotal
Definition
Profile.h:19
FxProfile::SlotCount
@ SlotCount
Definition
Profile.h:20
FxProfile::BroadPhase
@ BroadPhase
Definition
Profile.h:11
FxProfile::Constraints
@ Constraints
Definition
Profile.h:14
FxProfile::VelocityDerive
@ VelocityDerive
Definition
Profile.h:16
FxProfile::Bookkeeping
@ Bookkeeping
Definition
Profile.h:18
FxProfile::PositionSolve
@ PositionSolve
Definition
Profile.h:13
FxProfile::Integration
@ Integration
Definition
Profile.h:15
FxProfile::VelocityPasses
@ VelocityPasses
Definition
Profile.h:17
FxProfile::ms
double ms(Slot slot)
FxProfile::enabled
bool enabled()
FxProfile::reset
void reset()
include
Fx2D
Profile.h
Generated by
1.9.8