eOn 3.2.0
Long-timescale dynamics: aKMC, NEB, parallel replica
☾
Toggle main menu visibility
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
18
namespace
eonc
{
19
20
namespace
{
21
std::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)
39
std::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
51
PotRegistry
&
PotRegistry::get
() noexcept {
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
61
void
PotRegistry::reset
() {
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
72
uint64_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
79
void
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
89
void
PotRegistry::on_force_call
(
PotType
t)
noexcept
{
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
94
size_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
99
size_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
107
size_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
112
void
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
PotRegistry.h
eonc::PotRegistry::m_next_id
std::atomic< uint64_t > m_next_id
Definition
PotRegistry.h:86
eonc::PotRegistry::m_type_stats
std::array< TypeStats, magic_enum::enum_count< PotType >()> m_type_stats
Definition
PotRegistry.h:81
eonc::PotRegistry::PotRegistry
PotRegistry()=default
eonc::PotRegistry::on_force_call
void on_force_call(PotType t) noexcept override
Definition
PotRegistry.cpp:89
eonc::PotRegistry::get
static PotRegistry & get() noexcept
Process-lifetime singleton.
Definition
PotRegistry.cpp:51
eonc::PotRegistry::type_force_calls
size_t type_force_calls(PotType t) const noexcept
Definition
PotRegistry.cpp:94
eonc::PotRegistry::reset
void reset()
Definition
PotRegistry.cpp:61
eonc::PotRegistry::total_force_calls
size_t total_force_calls() const noexcept
Definition
PotRegistry.cpp:99
eonc::PotRegistry::m_records_mutex
std::mutex m_records_mutex
Definition
PotRegistry.h:83
eonc::PotRegistry::type_alive
size_t type_alive(PotType t) const noexcept
Definition
PotRegistry.cpp:107
eonc::PotRegistry::on_created
uint64_t on_created(PotType t) noexcept override
Definition
PotRegistry.cpp:72
eonc::PotRegistry::write_summary
void write_summary(const std::string &path="_potcalls.json") const
Definition
PotRegistry.cpp:112
eonc::PotRegistry::m_records
std::vector< InstanceRecord > m_records
Definition
PotRegistry.h:84
eonc::PotRegistry::on_destroyed
void on_destroyed(uint64_t id, PotType t, size_t force_calls, TimePoint created_at) override
Definition
PotRegistry.cpp:79
eonc::PotRegistry::TimePoint
IPotRegistry::TimePoint TimePoint
Definition
PotRegistry.h:65
eonc
RAII resource manager for the ARTn C library with global synchronization.
Definition
ARTnSaddleSearch.cpp:23
eonc::PotType
PotType
Definition
BaseStructures.h:36
eonc::PotRegistry::InstanceRecord
Definition
PotRegistry.h:67
client
PotRegistry.cpp
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf