util: a log_once for the warnings that speak once per process

Three sites -- the kvstream clamp (L-02), the batch overlap guard and
the bidi-plane belt -- each open-coded the same four lines: a function-
local static atomic<bool>, a bool expected, and a compare_exchange to
decide which of the many steps gets to print. The pattern is the point,
not the flag name (said, said2), so it moved to rad_internal.h as
log_once(flag): true on the first call, false after.

No behaviour change -- same first-of-many warnings, same compare-exchange
thread-safety, exactly one winner per flag -- only the idiom is now named
instead of re-typed where a fourth once-warning is ever needed.
This commit is contained in:
fl0l0u committed 2026-11-15 22:02:29 +00:00
1 parent b9ecbde0d6
commit c3a4775f54
3 files changed
+12 -6

No files matched your search

+1 -2
View File
@@ -223,8 +223,7 @@ bool stage_src(Ctx* ctx, int group, const RadKVGroupBatch* kvb, int layer, Stage
* attention then reads a row that is part copy and part stale. The clamp is the
* memory-safe answer; this is the promise that it is never a silent one. */
static std::atomic<bool> said{false};
bool expected = false;
if (said.compare_exchange_strong(expected, true))
if (log_once(said))
RAD_WARN("kvstream: group %d stage runs reach past the host layer's end (%lld of "
"%lld bytes): the stage drops what lies past it, and staged attention "
"reads stale bytes there (first of possibly many)",
+9
View File
@@ -10,6 +10,7 @@
#include <cstdio>
#include <cstdarg>
#include <atomic>
#include <string>
#include <string_view>
#include <vector>
@@ -35,6 +36,14 @@ uint64_t log_last_line_ns();
* Multi-byte UTF-8 is left alone; only control bytes are replaced. */
std::string log_sanitize(const std::string& s);
/* The once-log idiom: true on the first call for this flag, false forever after -- a warning
* that would otherwise repeat every step logs its first occurrence and stays quiet. Thread-safe
* by compare_exchange, so exactly one thread wins. */
inline bool log_once(std::atomic<bool>& flag) {
bool expected = false;
return flag.compare_exchange_strong(expected, true);
}
/* The whole text of a file, refused BEFORE it is read when it is bigger than `cap`. Files a
* container supplies are screened by size as well as by JSON budget: the bounded read counts
* values, not bytes, and the slurp used to be the unbounded step -- a multi-gigabyte
+2 -4
View File
@@ -1082,8 +1082,7 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv,
if (a >= b || pm.items[(size_t)run.item].item->kind == mm::Kind::Audio) continue;
if (a < prev_b) {
static std::atomic<bool> said{false};
bool expected = false;
if (said.compare_exchange_strong(expected, true))
if (log_once(said))
RAD_WARN("step batch: a rope layout overlaps at token %lld; that run is "
"not staged bidirectionally (first of possibly many)",
(long long)a);
@@ -1094,8 +1093,7 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv,
/* Belt: the disjoint check makes this unreachable; the slice says so
* rather than letting an overrun reach the card. */
static std::atomic<bool> said2{false};
bool expected = false;
if (said2.compare_exchange_strong(expected, true))
if (log_once(said2))
RAD_WARN("step batch: bidi runs overflowed the %lld-token plane "
"(first of possibly many)", (long long)T);
break;