-
Notifications
You must be signed in to change notification settings - Fork 0
Task 08: Utilities RNG + logging + stats #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fc4af45
0f70ae7
13642a1
f35fed5
f126a55
7f38d29
e4cd879
543b8da
c010a39
12e851a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| #include "data_types/dds.h" // THREADMEM_* defaults | ||
| #include <cstdlib> | ||
| #include <iostream> | ||
| #include <cstdio> | ||
| #include <unordered_map> | ||
|
|
||
| namespace { | ||
|
|
@@ -85,6 +86,20 @@ TransTable* SolverContext::transTable() const | |
| created->SetMemoryMaximum(maxMB); | ||
| created->MakeTT(); | ||
|
|
||
| #ifdef DDS_UTILITIES_LOG | ||
| // Append a tiny debug entry indicating TT creation and chosen kind/sizes. | ||
| { | ||
| char buf[96]; | ||
| const char kch = (kind == TTKind::Small ? 'S' : 'L'); | ||
| std::snprintf(buf, sizeof(buf), "tt:create|%c|%d|%d", kch, defMB, maxMB); | ||
|
Comment on lines
+92
to
+94
|
||
| utilities().logAppend(std::string(buf)); | ||
| } | ||
| #endif | ||
|
|
||
| #ifdef DDS_UTILITIES_STATS | ||
| utilities().util().stats().tt_creates++; | ||
| #endif | ||
|
|
||
| // Attach to registry | ||
| registry()[thr_] = created; | ||
| } | ||
|
|
@@ -148,6 +163,13 @@ void SolverContext::DisposeTransTable() const | |
| auto it = registry().find(thr_); | ||
| if (it != registry().end()) | ||
| { | ||
| #ifdef DDS_UTILITIES_LOG | ||
| // Append a tiny debug entry indicating TT disposal. | ||
| utilities().logAppend("tt:dispose"); | ||
| #endif | ||
| #ifdef DDS_UTILITIES_STATS | ||
| utilities().util().stats().tt_disposes++; | ||
| #endif | ||
| delete it->second; | ||
| it->second = nullptr; | ||
| registry().erase(it); | ||
|
|
@@ -158,6 +180,9 @@ SolverContext::~SolverContext() = default; | |
|
|
||
| void SolverContext::ResetForSolve() const | ||
| { | ||
| #ifdef DDS_UTILITIES_LOG | ||
| utilities().logAppend("ctx:reset_for_solve"); | ||
| #endif | ||
| if (auto* tt = maybeTransTable()) | ||
| tt->ResetMemory(TT_RESET_FREE_MEMORY); | ||
| if (!thr_) return; | ||
|
|
@@ -185,12 +210,22 @@ void SolverContext::ResetForSolve() const | |
|
|
||
| void SolverContext::ClearTT() const | ||
| { | ||
| #ifdef DDS_UTILITIES_LOG | ||
| utilities().logAppend("tt:clear"); | ||
| #endif | ||
| if (auto* tt = maybeTransTable()) | ||
| tt->ReturnAllMemory(); | ||
| } | ||
|
|
||
| void SolverContext::ResizeTT(int defMB, int maxMB) const | ||
| { | ||
| #ifdef DDS_UTILITIES_LOG | ||
| { | ||
| char buf[64]; | ||
|
||
| std::snprintf(buf, sizeof(buf), "tt:resize|%d|%d", defMB, maxMB); | ||
|
Comment on lines
+224
to
+225
|
||
| utilities().logAppend(std::string(buf)); | ||
| } | ||
| #endif | ||
| if (auto* tt = maybeTransTable()) | ||
| { | ||
| if (maxMB < defMB) maxMB = defMB; | ||
|
|
@@ -202,6 +237,9 @@ void SolverContext::ResizeTT(int defMB, int maxMB) const | |
| // Lightweight reset matching legacy ResetBestMoves semantics. | ||
| void SolverContext::ResetBestMovesLite() const | ||
| { | ||
| #ifdef DDS_UTILITIES_LOG | ||
| utilities().logAppend("ctx:reset_best_moves_lite"); | ||
| #endif | ||
| if (!thr_) return; | ||
| for (int d = 0; d <= 49; ++d) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #ifndef DDS_SYSTEM_UTIL_UTILITIES_H | ||
| #define DDS_SYSTEM_UTIL_UTILITIES_H | ||
|
|
||
| #include <cstdint> | ||
| #include <random> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace dds { | ||
|
|
||
| // A tiny, instance-scoped utility bundle holding RNG and a simple log buffer. | ||
| class Utilities { | ||
| public: | ||
| Utilities() = default; | ||
|
|
||
| // Compile-time feature detection helpers for tests and guarded code paths. | ||
| static constexpr bool log_enabled() { | ||
| #ifdef DDS_UTILITIES_LOG | ||
| return true; | ||
| #else | ||
| return false; | ||
| #endif | ||
| } | ||
|
|
||
| static constexpr bool stats_enabled() { | ||
| #ifdef DDS_UTILITIES_STATS | ||
| return true; | ||
| #else | ||
| return false; | ||
| #endif | ||
| } | ||
|
|
||
| // RNG: mt19937 seeded with a 64-bit seed for determinism across platforms. | ||
| void seed(uint64_t seed_value) { | ||
| rng_.seed(static_cast<std::mt19937::result_type>(seed_value)); | ||
| } | ||
|
|
||
| std::mt19937& rng() { return rng_; } | ||
| const std::mt19937& rng() const { return rng_; } | ||
|
|
||
| // Logging: a very simple append-only buffer; callers can flush and clear. | ||
| void log_append(const std::string& s) { log_.push_back(s); } | ||
| const std::vector<std::string>& log_buffer() const { return log_; } | ||
| size_t log_size() const { return log_.size(); } | ||
| bool log_contains(const std::string& prefix) const { | ||
| for (const auto& line : log_) { | ||
| if (line.rfind(prefix, 0) == 0) return true; // prefix match | ||
| } | ||
| return false; | ||
| } | ||
| void log_clear() { log_.clear(); } | ||
|
|
||
| // Minimal stats: opt-in counters for smoke validation in tests. | ||
| struct Stats { | ||
| unsigned tt_creates = 0; | ||
| unsigned tt_disposes = 0; | ||
| }; | ||
|
|
||
| const Stats& stats() const { return stats_; } | ||
| Stats& stats() { return stats_; } | ||
| Stats stats_snapshot() const { return stats_; } | ||
| void stats_reset() { stats_ = Stats{}; } | ||
|
|
||
| private: | ||
| std::mt19937 rng_{}; // default-constructed; seed via seed() | ||
| std::vector<std::string> log_{}; // minimal structured log lines | ||
| Stats stats_{}; // optional counters | ||
| }; | ||
|
|
||
| } // namespace dds | ||
|
|
||
| #endif // DDS_SYSTEM_UTIL_UTILITIES_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Magic number 96 for buffer size should be defined as a named constant to improve maintainability and make the buffer size choice explicit.