Loading...
Searching...
No Matches
eonc::PotRegistry Class Reference

#include <PotRegistry.h>

Inheritance diagram for eonc::PotRegistry:

Classes

struct  InstanceRecord
struct  TypeStats

Public Types

using Clock = IPotRegistry::Clock
using TimePoint = IPotRegistry::TimePoint
Public Types inherited from eonc::IPotRegistry
using Clock = std::chrono::system_clock
using TimePoint = Clock::time_point

Public Member Functions

 PotRegistry ()=default
 ~PotRegistry () override
void reset ()
uint64_t on_created (PotType t) noexcept override
void on_destroyed (uint64_t id, PotType t, size_t force_calls, TimePoint created_at) override
void on_force_call (PotType t) noexcept override
size_t type_force_calls (PotType t) const noexcept
size_t total_force_calls () const noexcept
size_t type_alive (PotType t) const noexcept
void write_summary (const std::string &path="_potcalls.json") const
Public Member Functions inherited from eonc::IPotRegistry
virtual ~IPotRegistry ()
std::shared_ptr< RegistryLifetime > lifetime () const
 IPotRegistry (const IPotRegistry &)=delete
IPotRegistry & operator= (const IPotRegistry &)=delete

Static Public Member Functions

static PotRegistry & get () noexcept
 Process-lifetime singleton.

Private Attributes

std::array< TypeStats, magic_enum::enum_count< PotType >()> m_type_stats {}
std::mutex m_records_mutex
std::vector< InstanceRecord > m_records
std::atomic< uint64_t > m_next_id {1}

Additional Inherited Members

Protected Member Functions inherited from eonc::IPotRegistry
 IPotRegistry ()=default
Protected Attributes inherited from eonc::IPotRegistry
std::shared_ptr< RegistryLifetime > lifetime_

Detailed Description

Definition at line 62 of file PotRegistry.h.

Member Typedef Documentation

◆ Clock

Definition at line 64 of file PotRegistry.h.

◆ TimePoint

Constructor & Destructor Documentation

◆ PotRegistry()

eonc::PotRegistry::PotRegistry ( )
default

◆ ~PotRegistry()

eonc::PotRegistry::~PotRegistry ( )
inlineoverride

Definition at line 90 of file PotRegistry.h.

90 {
91 // Members are still intact. Mark the registry dead before they go,
92 // so a Potential destroyed from here does not lock a dead mutex.
93 if (lifetime_)
94 lifetime_->alive.store(false, std::memory_order_release);
95 }
std::shared_ptr< RegistryLifetime > lifetime_
Definition PotRegistry.h:58

Member Function Documentation

◆ get()

PotRegistry & eonc::PotRegistry::get ( )
staticnoexcept

Process-lifetime singleton.

The instance is allocated on the heap and never destroyed, so Potential destructors can still record teardown after C++ static destruction.

Definition at line 51 of file PotRegistry.cpp.

51 {
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}
PotRegistry()=default

◆ on_created()

uint64_t eonc::PotRegistry::on_created ( PotType t)
nodiscardoverridevirtualnoexcept

Implements eonc::IPotRegistry.

Definition at line 72 of file PotRegistry.cpp.

72 {
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}
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

◆ on_destroyed()

void eonc::PotRegistry::on_destroyed ( uint64_t id,
PotType t,
size_t force_calls,
TimePoint created_at )
overridevirtual

Implements eonc::IPotRegistry.

Definition at line 79 of file PotRegistry.cpp.

80 {
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}
std::mutex m_records_mutex
Definition PotRegistry.h:83
std::vector< InstanceRecord > m_records
Definition PotRegistry.h:84

◆ on_force_call()

void eonc::PotRegistry::on_force_call ( PotType t)
overridevirtualnoexcept

Implements eonc::IPotRegistry.

Definition at line 89 of file PotRegistry.cpp.

89 {
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}

◆ reset()

void eonc::PotRegistry::reset ( )

Definition at line 61 of file PotRegistry.cpp.

61 {
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}

◆ total_force_calls()

size_t eonc::PotRegistry::total_force_calls ( ) const
nodiscardnoexcept

Definition at line 99 of file PotRegistry.cpp.

99 {
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}

◆ type_alive()

size_t eonc::PotRegistry::type_alive ( PotType t) const
nodiscardnoexcept

Definition at line 107 of file PotRegistry.cpp.

107 {
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}

◆ type_force_calls()

size_t eonc::PotRegistry::type_force_calls ( PotType t) const
nodiscardnoexcept

Definition at line 94 of file PotRegistry.cpp.

94 {
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}

◆ write_summary()

void eonc::PotRegistry::write_summary ( const std::string & path = "_potcalls.json") const

Definition at line 112 of file PotRegistry.cpp.

112 {
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}

Member Data Documentation

◆ m_next_id

std::atomic<uint64_t> eonc::PotRegistry::m_next_id {1}
private

Definition at line 86 of file PotRegistry.h.

86{1};

◆ m_records

std::vector<InstanceRecord> eonc::PotRegistry::m_records
private

Definition at line 84 of file PotRegistry.h.

◆ m_records_mutex

std::mutex eonc::PotRegistry::m_records_mutex
private

Definition at line 83 of file PotRegistry.h.

◆ m_type_stats

std::array<TypeStats, magic_enum::enum_count<PotType>()> eonc::PotRegistry::m_type_stats {}
private

Definition at line 81 of file PotRegistry.h.

81{};

The documentation for this class was generated from the following files: