Pioneer
Loading...
Searching...
No Matches
PerfStats.h
Go to the documentation of this file.
1// Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#pragma once
5
6#include <atomic>
7#include <cassert>
8#include <cstddef>
9#include <map>
10#include <mutex>
11#include <set>
12#include <string>
13#include <vector>
14
15namespace Perf {
24 class Stats {
25 public:
26 using FrameInfo = std::map<std::string, uint32_t>;
27
28 // Simple opaque struct to make it more difficult to accidentally clobber memory or threading constraints
29 struct CounterRef {
30 CounterRef() = delete;
31 explicit CounterRef(std::nullptr_t) :
32 id(0) {}
33 size_t id;
34
35 private:
36 friend class Stats;
37 CounterRef(std::size_t t) :
38 id(t) {}
39 };
40
41 CounterRef GetOrCreateCounter(std::string name, bool resetOnNewFrame = true);
42
43 void CounterAdd(CounterRef ref, uint32_t amount = 1) const
44 {
45 assert(ref.id != 0);
46 m_counters.at(ref.id).ctr.fetch_add(amount);
47 }
48
49 void CounterDec(CounterRef ref, uint32_t amount = 1) const
50 {
51 assert(ref.id != 0);
52 m_counters.at(ref.id).ctr.fetch_sub(amount);
53 }
54
55 void CounterSet(CounterRef ref, uint32_t value) const
56 {
57 assert(ref.id != 0);
58 m_counters.at(ref.id).ctr.store(value);
59 }
60
61 void CounterReset(CounterRef ref) const
62 {
63 assert(ref.id != 0);
64 m_counters.at(ref.id).ctr.store(0);
65 }
66
67 void EnableReset(bool enabled) { m_neverReset = !enabled; }
68
69 const FrameInfo &GetFrameStats() const { return m_frameCache; }
70
71 std::string GetNameForCounter(CounterRef ref) const { return m_definedCounters.at(ref.id); }
72
73 // Terminate the current frame and make performance counters available with GetFrameStats().
74 void FlushFrame();
75
76 private:
77 struct Counter {
78 Counter(bool reset) :
79 ctr(0), resetOnNewFrame(reset){};
80 // mutable because the only thing we're modifying via non-const references is the atomic counters
81 mutable std::atomic<uint32_t> ctr;
82 bool resetOnNewFrame;
83 };
84
85 // hashmap of counter ID to counter value
86 std::map<std::size_t, Counter> m_counters;
87
88 // Cache the previous frame
89 FrameInfo m_frameCache;
90
91 bool m_neverReset = false;
92
93 // hashmap of counter IDs to counter names
94 std::map<std::size_t, std::string> m_definedCounters;
95
96 // mutex used to synchronize updates to definedCounters
97 std::mutex m_counterMutex;
98 // store a std::hash instance so we don't have to recreate it every time we want to hash a string.
99 std::hash<std::string> m_strHashFn;
100 };
101
102} // namespace Perf
Definition PerfStats.h:24
void CounterAdd(CounterRef ref, uint32_t amount=1) const
Definition PerfStats.h:43
void CounterReset(CounterRef ref) const
Definition PerfStats.h:61
void CounterDec(CounterRef ref, uint32_t amount=1) const
Definition PerfStats.h:49
const FrameInfo & GetFrameStats() const
Definition PerfStats.h:69
CounterRef GetOrCreateCounter(std::string name, bool resetOnNewFrame=true)
Definition PerfStats.cpp:13
void CounterSet(CounterRef ref, uint32_t value) const
Definition PerfStats.h:55
std::string GetNameForCounter(CounterRef ref) const
Definition PerfStats.h:71
std::map< std::string, uint32_t > FrameInfo
Definition PerfStats.h:26
void FlushFrame()
Definition PerfStats.cpp:32
void EnableReset(bool enabled)
Definition PerfStats.h:67
Definition PerfStats.h:15
Definition PerfStats.h:29
CounterRef(std::nullptr_t)
Definition PerfStats.h:31
size_t id
Definition PerfStats.h:33