|
| 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 "PotRegistry.h" |
| 13 | +#include <fstream> |
| 14 | +#include <iomanip> |
| 15 | +#include <sstream> |
| 16 | + |
| 17 | +namespace eonc { |
| 18 | + |
| 19 | +namespace { |
| 20 | +std::string format_timepoint(PotRegistry::TimePoint tp) { |
| 21 | + auto time_t_val = PotRegistry::Clock::to_time_t(tp); |
| 22 | + auto us = std::chrono::duration_cast<std::chrono::microseconds>( |
| 23 | + tp.time_since_epoch()) % |
| 24 | + std::chrono::seconds{1}; |
| 25 | + std::tm tm_buf{}; |
| 26 | +#ifdef _WIN32 |
| 27 | + localtime_s(&tm_buf, &time_t_val); |
| 28 | +#else |
| 29 | + localtime_r(&time_t_val, &tm_buf); |
| 30 | +#endif |
| 31 | + std::ostringstream oss; |
| 32 | + oss << std::put_time(&tm_buf, "%Y-%m-%dT%H:%M:%S"); |
| 33 | + oss << '.' << std::setfill('0') << std::setw(6) << us.count(); |
| 34 | + return oss.str(); |
| 35 | +} |
| 36 | + |
| 37 | +// Escape a string for JSON (handles quotes and backslashes) |
| 38 | +std::string json_escape(std::string_view sv) { |
| 39 | + std::string out; |
| 40 | + out.reserve(sv.size()); |
| 41 | + for (char c : sv) { |
| 42 | + if (c == '"' || c == '\\') |
| 43 | + out += '\\'; |
| 44 | + out += c; |
| 45 | + } |
| 46 | + return out; |
| 47 | +} |
| 48 | +} // namespace |
| 49 | + |
| 50 | +PotRegistry &PotRegistry::get() noexcept { |
| 51 | + static PotRegistry instance; |
| 52 | + return instance; |
| 53 | +} |
| 54 | + |
| 55 | +void PotRegistry::reset() { |
| 56 | + for (auto &ts : m_type_stats) { |
| 57 | + ts.force_calls.store(0, std::memory_order_relaxed); |
| 58 | + ts.created.store(0, std::memory_order_relaxed); |
| 59 | + ts.alive.store(0, std::memory_order_relaxed); |
| 60 | + } |
| 61 | + std::lock_guard<std::mutex> lock(m_records_mutex); |
| 62 | + m_records.clear(); |
| 63 | + m_next_id.store(1, std::memory_order_relaxed); |
| 64 | +} |
| 65 | + |
| 66 | +uint64_t PotRegistry::on_created(PotType t) noexcept { |
| 67 | + auto idx = static_cast<size_t>(magic_enum::enum_index(t).value_or(0)); |
| 68 | + m_type_stats[idx].created.fetch_add(1, std::memory_order_relaxed); |
| 69 | + m_type_stats[idx].alive.fetch_add(1, std::memory_order_relaxed); |
| 70 | + return m_next_id.fetch_add(1, std::memory_order_relaxed); |
| 71 | +} |
| 72 | + |
| 73 | +void PotRegistry::on_destroyed(uint64_t id, PotType t, size_t force_calls, |
| 74 | + TimePoint created_at) { |
| 75 | + auto idx = static_cast<size_t>(magic_enum::enum_index(t).value_or(0)); |
| 76 | + m_type_stats[idx].alive.fetch_sub(1, std::memory_order_relaxed); |
| 77 | + |
| 78 | + InstanceRecord rec{id, t, created_at, Clock::now(), force_calls}; |
| 79 | + std::lock_guard<std::mutex> lock(m_records_mutex); |
| 80 | + m_records.push_back(rec); |
| 81 | +} |
| 82 | + |
| 83 | +void PotRegistry::on_force_call(PotType t) noexcept { |
| 84 | + auto idx = static_cast<size_t>(magic_enum::enum_index(t).value_or(0)); |
| 85 | + m_type_stats[idx].force_calls.fetch_add(1, std::memory_order_relaxed); |
| 86 | +} |
| 87 | + |
| 88 | +size_t PotRegistry::type_force_calls(PotType t) const noexcept { |
| 89 | + auto idx = static_cast<size_t>(magic_enum::enum_index(t).value_or(0)); |
| 90 | + return m_type_stats[idx].force_calls.load(std::memory_order_relaxed); |
| 91 | +} |
| 92 | + |
| 93 | +size_t PotRegistry::total_force_calls() const noexcept { |
| 94 | + size_t total = 0; |
| 95 | + for (const auto &ts : m_type_stats) { |
| 96 | + total += ts.force_calls.load(std::memory_order_relaxed); |
| 97 | + } |
| 98 | + return total; |
| 99 | +} |
| 100 | + |
| 101 | +size_t PotRegistry::type_alive(PotType t) const noexcept { |
| 102 | + auto idx = static_cast<size_t>(magic_enum::enum_index(t).value_or(0)); |
| 103 | + return m_type_stats[idx].alive.load(std::memory_order_relaxed); |
| 104 | +} |
| 105 | + |
| 106 | +void PotRegistry::write_summary(const std::string &path) const { |
| 107 | + std::vector<InstanceRecord> snapshot; |
| 108 | + { |
| 109 | + std::lock_guard<std::mutex> lock(const_cast<std::mutex &>(m_records_mutex)); |
| 110 | + snapshot = m_records; |
| 111 | + } |
| 112 | + |
| 113 | + std::ofstream ofs(path); |
| 114 | + if (!ofs.is_open()) |
| 115 | + return; |
| 116 | + |
| 117 | + ofs << "[\n"; |
| 118 | + for (size_t i = 0; i < snapshot.size(); ++i) { |
| 119 | + const auto &r = snapshot[i]; |
| 120 | + auto type_name = magic_enum::enum_name(r.type); |
| 121 | + ofs << " {\n"; |
| 122 | + ofs << " \"id\": " << r.id << ",\n"; |
| 123 | + ofs << " \"type\": \"" << json_escape(type_name) << "\",\n"; |
| 124 | + ofs << " \"created_at\": \"" << format_timepoint(r.created_at) |
| 125 | + << "\",\n"; |
| 126 | + ofs << " \"destroyed_at\": \"" << format_timepoint(r.destroyed_at) |
| 127 | + << "\",\n"; |
| 128 | + ofs << " \"force_calls\": " << r.force_calls << "\n"; |
| 129 | + ofs << " }"; |
| 130 | + if (i + 1 < snapshot.size()) |
| 131 | + ofs << ','; |
| 132 | + ofs << '\n'; |
| 133 | + } |
| 134 | + ofs << "]\n"; |
| 135 | +} |
| 136 | + |
| 137 | +} // namespace eonc |
0 commit comments