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.
8namespace FxProfile {
9
10enum 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
21};
22
23// True when the library was compiled with the profiler enabled.
24bool enabled();
25
26// Human-readable name for a slot, for report tables.
27const char* slot_name(Slot slot);
28
29// Accumulated milliseconds in a slot since the last reset().
30double ms(Slot slot);
31
32// Number of completed steps since the last reset(), so callers can report per-step figures.
33std::size_t steps();
34
35// Zero every accumulator and the step count. Call between measured runs.
36void 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.
40void add(Slot slot, double elapsed_ms);
41
42// Marks one completed step.
44
45} // namespace FxProfile
46
47#ifdef FX2D_PROFILE
48#include <chrono>
49
50namespace FxProfile {
51// Accumulates its lifetime into a slot. Scope-based so an early return still records.
52class 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
std::size_t steps()
const char * slot_name(Slot slot)
void add(Slot slot, double elapsed_ms)
void count_step()
@ NarrowPhase
Definition Profile.h:12
@ StepTotal
Definition Profile.h:19
@ SlotCount
Definition Profile.h:20
@ BroadPhase
Definition Profile.h:11
@ Constraints
Definition Profile.h:14
@ VelocityDerive
Definition Profile.h:16
@ Bookkeeping
Definition Profile.h:18
@ PositionSolve
Definition Profile.h:13
@ Integration
Definition Profile.h:15
@ VelocityPasses
Definition Profile.h:17
double ms(Slot slot)
bool enabled()
void reset()