nngn
Loading...
Searching...
No Matches
profile.h
Go to the documentation of this file.
1#ifndef NNGN_TIMING_PROFILE_H
2#define NNGN_TIMING_PROFILE_H
3
4#include <array>
5#include <cstddef>
6#include <cstdint>
7#include <tuple>
8
9#include "utils/utils.h"
10
11#include "stats.h"
12#include "timing.h"
13
14namespace nngn {
15
16#define NNGN_STATS_CONTEXT(c, p) \
17 const auto NNGN_STATS_CONTEXT_VAR(__LINE__) = nngn::Profile::context<c>(p);
18#define NNGN_PROFILE_CONTEXT(p) \
19 NNGN_STATS_CONTEXT(nngn::Profile, &nngn::Profile::stats.p)
20#define NNGN_STATS_CONTEXT_VAR(l) NNGN_STATS_CONTEXT_JOIN(prof_, l)
21#define NNGN_STATS_CONTEXT_JOIN(x, y) x##y
22
23struct ProfileStats : StatsBase<ProfileStats, 2> {
24 std::array<uint64_t, 2>
28 static constexpr std::array names = {
29 "schedule", "socket", "collision_check", "collision_resolve",
30 "collision_lua", "entities", "animations", "parents", "renderers",
31 "renderers_debug", "render", "vsync",
32 };
33 const uint64_t *to_u64() const { return this->schedule.data(); }
34 uint64_t *to_u64() { return this->schedule.data(); }
35};
36
37struct Profile {
39 static constexpr size_t STATS_IDX = 0;
40 template<typename T>
41 struct context {
42 static constexpr auto I = T::STATS_IDX;
43 static constexpr auto N = T::Stats::N_EVENTS;
44 std::array<uint64_t, N> *p;
46 explicit context(std::array<uint64_t, N> *p);
47 ~context(void) { this->end(); }
48 void end();
49 };
51 static Timing::duration::rep sample();
52 static void init();
53 static void swap();
54};
55
56template<typename T>
57inline Profile::context<T>::context(std::array<uint64_t, N> *p_) : p(p_) {
59 this->p->front() = static_cast<uint64_t>(Profile::sample());
60}
61
62template<typename T>
65 if(auto *const op = std::exchange(this->p, nullptr))
66 op->back() = static_cast<uint64_t>(Profile::sample());
67}
68
71
73
74inline Timing::duration::rep Profile::sample()
75 { return nngn::Timing::clock::now().time_since_epoch().count(); }
76
77}
78
79#endif
static bool active(std::size_t i)
Definition stats.h:29
static void reserve(std::size_t i, void *p)
Definition stats.h:60
Definition audio.cpp:7
#define NNGN_MOVE_ONLY(x)
Definition utils.h:39
Definition profile.h:23
std::array< uint64_t, 2 > collision_lua
Definition profile.h:25
uint64_t * to_u64()
Definition profile.h:34
const uint64_t * to_u64() const
Definition profile.h:33
std::array< uint64_t, 2 > entities
Definition profile.h:26
std::array< uint64_t, 2 > renderers
Definition profile.h:26
static constexpr std::array names
Definition profile.h:28
std::array< uint64_t, 2 > render
Definition profile.h:26
std::array< uint64_t, 2 > schedule
Definition profile.h:25
std::array< uint64_t, 2 > renderers_debug
Definition profile.h:26
std::array< uint64_t, 2 > collision_resolve
Definition profile.h:25
std::array< uint64_t, 2 > collision_check
Definition profile.h:25
std::array< uint64_t, 2 > animations
Definition profile.h:26
std::array< uint64_t, 2 > vsync
Definition profile.h:27
std::array< uint64_t, 2 > parents
Definition profile.h:26
std::array< uint64_t, 2 > socket
Definition profile.h:25
Definition profile.h:41
std::array< uint64_t, N > * p
Definition profile.h:44
static constexpr auto I
Definition profile.h:42
~context(void)
Definition profile.h:47
context(std::array< uint64_t, N > *p)
Definition profile.h:57
static constexpr auto N
Definition profile.h:43
void end()
Definition profile.h:63
Definition profile.h:37
static void init()
Definition profile.h:69
static void swap()
Definition profile.h:72
static constexpr size_t STATS_IDX
Definition profile.h:39
static ProfileStats prev
Definition profile.h:50
static ProfileStats stats
Definition profile.h:50
static Timing::duration::rep sample()
Definition profile.h:74
Definition stats.h:15