CoriEngine
Loading...
Searching...
No Matches
InstanceMetrics.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace Cori {
4 namespace Profiling {
9 template<typename T>
11 public:
12 using DerivedMetricsReporter = std::function<void()>;
13 using DerivedMetricsProvider = std::function<std::pair<int64_t, int64_t>()>;
14
21 static int64_t GetDirectAliveCount() {
22 return s_AliveCount.load(std::memory_order_relaxed);
23 }
24
31 static int64_t GetDirectTotalCreatedCount() {
32 return s_TotalCreatedCount.load(std::memory_order_relaxed);
33 }
34
41 static int64_t GetAliveCount() {
42 int64_t totalAlive = GetDirectAliveCount();
43 for (const auto& val : s_DerivedMetricsProviders | std::views::values) {
44 totalAlive += val().first;
45 }
46 return totalAlive;
47 }
48
55 static int64_t GetTotalCreatedCount() {
56 int64_t totalCreatedSum = GetDirectTotalCreatedCount();
57 for (const auto& val : s_DerivedMetricsProviders | std::views::values) {
58 totalCreatedSum += val().second;
59 }
60 return totalCreatedSum;
61 }
62
69 static void Report(const std::string& indent = "") {
71 if (GetDirectAliveCount() != 0 || GetDirectTotalCreatedCount() != 0 || s_DerivedMetricsReporters.empty()) {
75 }
76
77 if (!s_DerivedMetricsReporters.empty()) {
78 CORI_CORE_DEBUG_TAGGED({ Logger::Tags::Profiler::Self, Logger::Tags::Profiler::InstanceMetrics }, "{} Reporting for Registered Derived Types of: <{}>", indent, CORI_CLEAN_TYPE_NAME(T));
79 for (const auto& val : s_DerivedMetricsReporters | std::views::values) {
80 val();
81 }
83 }
85 }
86
93 static std::vector<std::pair<std::string, int64_t>> GetAliveCountData() {
94 std::vector<std::pair<std::string, int64_t>> counts;
95
96 if (GetDirectAliveCount() > 0 || s_DerivedMetricsProviders.empty()) {
97 counts.push_back({ CORI_CLEAN_TYPE_NAME(T), GetDirectAliveCount() });
98 }
99
100 for (const auto& [type, func] : s_DerivedMetricsProviders) {
101 auto [createdCount, _] = func();
102 if (createdCount > 0) {
103 counts.push_back({ type.name(), createdCount });
104 }
105 }
106 return counts;
107 }
108
115 static std::vector<std::pair<std::string, int64_t>> GetTotalCreatedCountData() {
116 std::vector<std::pair<std::string, int64_t>> counts;
117
118 if (GetDirectTotalCreatedCount() > 0 || s_DerivedMetricsProviders.empty()) {
119 counts.push_back({ CORI_CLEAN_TYPE_NAME(T), GetDirectTotalCreatedCount() });
120 }
121
122 for (const auto& [type, func] : s_DerivedMetricsProviders) {
123 auto [_, aliveCount] = func();
124 if (aliveCount > 0) {
125 counts.push_back({ type.name(), aliveCount });
126 }
127 }
128 return counts;
129 }
130
131 protected:
132 template<typename DerivedType, typename... BasePack>
133 friend class Trackable;
134
135 static void Increment() {
136 s_AliveCount.fetch_add(1, std::memory_order_relaxed);
137 s_TotalCreatedCount.fetch_add(1, std::memory_order_relaxed);
138 }
139
140 static void Decrement() {
141 s_AliveCount.fetch_sub(1, std::memory_order_relaxed);
142 }
143
144 static void RegisterDerivedMetricsProvider(const std::type_index& derivedTypeId, DerivedMetricsProvider provider) {
145 if (!s_DerivedMetricsProviders.contains(derivedTypeId)) {
146 s_DerivedMetricsProviders.insert({ derivedTypeId, provider });
147 CORI_CORE_DEBUG_TAGGED({ Logger::Tags::Profiler::Self, Logger::Tags::Profiler::InstanceMetrics }, "<{}>: Registered metrics provider for derived type: <{}>", CORI_CLEAN_TYPE_NAME(T), CORI_DEMANGLE(derivedTypeId.name()));
148 }
149 }
150
151 static void RegisterDerivedReporter(const std::type_index& derivedTypeId, DerivedMetricsReporter reporter) {
152 if (!s_DerivedMetricsReporters.contains(derivedTypeId)) {
153 s_DerivedMetricsReporters.insert({ derivedTypeId, reporter });
154 CORI_CORE_INFO_TAGGED({ Logger::Tags::Profiler::Self, Logger::Tags::Profiler::InstanceMetrics }, "<{}>: Registered reporter for derived type: <{}>", CORI_CLEAN_TYPE_NAME(T), CORI_DEMANGLE(derivedTypeId.name()));
155 }
156 }
157
158 private:
159
160 inline static std::atomic<int64_t> s_AliveCount{ 0 };
161 inline static std::atomic<int64_t> s_TotalCreatedCount{ 0 };
162
163 inline static std::map<std::type_index, DerivedMetricsReporter> s_DerivedMetricsReporters;
164 inline static std::map<std::type_index, DerivedMetricsProvider> s_DerivedMetricsProviders;
165 };
166
167 }
168
169 /*
170 InstanceMetric usage example:
171
172 InstanceMetrics<ExampleClass>::Report();
173 // this will log all the available instance metrics (Currently Alive, Total Created)
174
175 InstanceMetrics<ExampleClass>::GetAliveInstances();
176 // will return the number of currently alive instances of the ExampleClass
177
178 InstanceMetrics<ExampleClass>::GetTotalCreated();
179 // will return the total number of created instances of the ExampleClass
180 */
181}
#define CORI_DEMANGLE(name)
#define CORI_CLEAN_TYPE_NAME(tn)
#define CORI_CORE_DEBUG_TAGGED(...)
Definition Logger.hpp:1026
#define CORI_CORE_INFO_TAGGED(...)
Definition Logger.hpp:1027
This utility lets you count class instances in various ways. @detials You can see how much instances ...
static void RegisterDerivedReporter(const std::type_index &derivedTypeId, DerivedMetricsReporter reporter)
static std::vector< std::pair< std::string, int64_t > > GetAliveCountData()
Retries the data about alive instances of type T including derived instances.
static int64_t GetDirectAliveCount()
Checks how many instances of type T is alive, not including derived classes.
std::function< void()> DerivedMetricsReporter
static int64_t GetTotalCreatedCount()
Checks how many instances of type T were created, including derived classes.
std::function< std::pair< int64_t, int64_t >()> DerivedMetricsProvider
static int64_t GetAliveCount()
Checks how many instances of type T is alive, including derived classes.
static int64_t GetDirectTotalCreatedCount()
Checks how many instances of type T were created, not including derived classes.
static std::vector< std::pair< std::string, int64_t > > GetTotalCreatedCountData()
Retries the data about created instances of type T including derived instances.
static void RegisterDerivedMetricsProvider(const std::type_index &derivedTypeId, DerivedMetricsProvider provider)
static void Report(const std::string &indent="")
Will report the data about type T instances.
Profiling tools are in this namespace.
Global engine namespace.
static constexpr char Self[]
Definition Logger.hpp:144
static constexpr char InstanceMetrics[]
Definition Logger.hpp:146