Loading...
Searching...
No Matches
PotRegistry.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12#include "eon/PotRegistry.h"
13#include <fstream>
14#include <iomanip>
15#include <memory>
16#include <sstream>
17
18namespace eonc {
19
20namespace {
21std::string format_timepoint(PotRegistry::TimePoint tp) {
22 auto time_t_val = PotRegistry::Clock::to_time_t(tp);
23 auto us = std::chrono::duration_cast<std::chrono::microseconds>(
24 tp.time_since_epoch()) %
25 std::chrono::seconds{1};
26 std::tm tm_buf{};
27#ifdef _WIN32
28 localtime_s(&tm_buf, &time_t_val);
29#else
30 localtime_r(&time_t_val, &tm_buf);
31#endif
32 std::ostringstream oss;
33 oss << std::put_time(&tm_buf, "%Y-%m-%dT%H:%M:%S");
34 oss << '.' << std::setfill('0') << std::setw(6) << us.count();
35 return oss.str();
36}
37
38// Escape a string for JSON (handles quotes and backslashes)
39std::string json_escape(std::string_view sv) {
40 std::string out;
41 out.reserve(sv.size());
42 for (char c : sv) {
43 if (c == '"' || c == '\\')
44 out += '\\';
45 out += c;
46 }
47 return out;
48}
49} // namespace
50
52 // Heap-allocated and never deleted. Potential destructors call
53 // on_destroyed, and those objects can outlive C++ static
54 // destructors (interpreter finalization, leaked shared_ptrs). A
55 // function-local static would already be destroyed on those paths,
56 // so on_destroyed would lock a destroyed mutex.
57 static PotRegistry *instance = std::make_unique<PotRegistry>().release();
58 return *instance;
59}
60
62 for (auto &ts : m_type_stats) {
63 ts.force_calls.store(0, std::memory_order_relaxed);
64 ts.created.store(0, std::memory_order_relaxed);
65 ts.alive.store(0, std::memory_order_relaxed);
66 }
67 std::lock_guard<std::mutex> lock(m_records_mutex);
68 m_records.clear();
69 m_next_id.store(1, std::memory_order_relaxed);
70}
71
72uint64_t PotRegistry::on_created(PotType t) noexcept {
73 auto idx = static_cast<size_t>(magic_enum::enum_index(t).value_or(0));
74 m_type_stats[idx].created.fetch_add(1, std::memory_order_relaxed);
75 m_type_stats[idx].alive.fetch_add(1, std::memory_order_relaxed);
76 return m_next_id.fetch_add(1, std::memory_order_relaxed);
77}
78
79void PotRegistry::on_destroyed(uint64_t id, PotType t, size_t force_calls,
80 TimePoint created_at) {
81 auto idx = static_cast<size_t>(magic_enum::enum_index(t).value_or(0));
82 m_type_stats[idx].alive.fetch_sub(1, std::memory_order_relaxed);
83
84 InstanceRecord rec{id, t, created_at, Clock::now(), force_calls};
85 std::lock_guard<std::mutex> lock(m_records_mutex);
86 m_records.push_back(rec);
87}
88
90 auto idx = static_cast<size_t>(magic_enum::enum_index(t).value_or(0));
91 m_type_stats[idx].force_calls.fetch_add(1, std::memory_order_relaxed);
92}
93
94size_t PotRegistry::type_force_calls(PotType t) const noexcept {
95 auto idx = static_cast<size_t>(magic_enum::enum_index(t).value_or(0));
96 return m_type_stats[idx].force_calls.load(std::memory_order_relaxed);
97}
98
99size_t PotRegistry::total_force_calls() const noexcept {
100 size_t total = 0;
101 for (const auto &ts : m_type_stats) {
102 total += ts.force_calls.load(std::memory_order_relaxed);
103 }
104 return total;
105}
106
107size_t PotRegistry::type_alive(PotType t) const noexcept {
108 auto idx = static_cast<size_t>(magic_enum::enum_index(t).value_or(0));
109 return m_type_stats[idx].alive.load(std::memory_order_relaxed);
110}
111
112void PotRegistry::write_summary(const std::string &path) const {
113 std::vector<InstanceRecord> snapshot;
114 {
115 std::lock_guard<std::mutex> lock(const_cast<std::mutex &>(m_records_mutex));
116 snapshot = m_records;
117 }
118
119 std::ofstream ofs(path);
120 if (!ofs.is_open())
121 return;
122
123 ofs << "[\n";
124 for (size_t i = 0; i < snapshot.size(); ++i) {
125 const auto &r = snapshot[i];
126 auto type_name = magic_enum::enum_name(r.type);
127 ofs << " {\n";
128 ofs << " \"id\": " << r.id << ",\n";
129 ofs << " \"type\": \"" << json_escape(type_name) << "\",\n";
130 ofs << " \"created_at\": \"" << format_timepoint(r.created_at)
131 << "\",\n";
132 ofs << " \"destroyed_at\": \"" << format_timepoint(r.destroyed_at)
133 << "\",\n";
134 ofs << " \"force_calls\": " << r.force_calls << "\n";
135 ofs << " }";
136 if (i + 1 < snapshot.size())
137 ofs << ',';
138 ofs << '\n';
139 }
140 ofs << "]\n";
141}
142
143} // namespace eonc
std::atomic< uint64_t > m_next_id
Definition PotRegistry.h:86
std::array< TypeStats, magic_enum::enum_count< PotType >()> m_type_stats
Definition PotRegistry.h:81
PotRegistry()=default
void on_force_call(PotType t) noexcept override
static PotRegistry & get() noexcept
Process-lifetime singleton.
size_t type_force_calls(PotType t) const noexcept
size_t total_force_calls() const noexcept
std::mutex m_records_mutex
Definition PotRegistry.h:83
size_t type_alive(PotType t) const noexcept
uint64_t on_created(PotType t) noexcept override
void write_summary(const std::string &path="_potcalls.json") const
std::vector< InstanceRecord > m_records
Definition PotRegistry.h:84
void on_destroyed(uint64_t id, PotType t, size_t force_calls, TimePoint created_at) override
IPotRegistry::TimePoint TimePoint
Definition PotRegistry.h:65
RAII resource manager for the ARTn C library with global synchronization.