diff --git a/core/build/rad_graph.cpp b/core/build/rad_graph.cpp index 5ed7e372..0b0b7d2c 100644 --- a/core/build/rad_graph.cpp +++ b/core/build/rad_graph.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -650,7 +651,9 @@ std::string dump_graph_json(const Program& p) { j["geometry"] = o.base.str(); j["ranged"] = o.ranged_key; if (!o.ranged_key.empty()) { j["range_lo"] = o.range_lo; j["range_hi"] = o.range_hi; } - j["issued"] = o.issued; + /* Read through an atomic_ref: the step path stores this flag from the engine thread + * (Ctx::flush_issued); a plain read here would race it. */ + j["issued"] = std::atomic_ref(o.issued).load(std::memory_order_relaxed); j["schema"] = schema_json(o.schema ? o.schema : reg.schema(o.op)); json ws = json::array(); diff --git a/core/engine.cpp b/core/engine.cpp index 75443104..b2c84939 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -2320,8 +2320,8 @@ int Engine::run_server() { s.experts.n_experts = n_expert; s.experts.tier.assign((size_t)n_layer * (size_t)n_expert, (uint8_t)Tier::VRAM); /* The live tier when the heat engine runs, else the plan's (where a unit started). - * HeatEngine::tier is a plain vector written by the dispatch thread at step - * boundaries, so a racing read sees either side of a single store. */ + * tier_published is the HTTP-readable mirror HeatEngine::set_tier keeps; reading + * the dispatch thread's own tier_ here would be a data race. */ const HeatEngine& heat = ranks_[0]->heat; const bool live_tier = heat.enabled(); s.experts.heat_enabled = live_tier; @@ -2330,7 +2330,7 @@ int Engine::run_server() { for (size_t u = 0; u < p.units.size(); ++u) { const MoveUnit& mu = p.units[u]; if (mu.expert < 0 || mu.layer < 0) continue; - const Tier t = live_tier ? heat.tier((int32_t)u) : mu.tier; + const Tier t = live_tier ? heat.tier_published((int32_t)u) : mu.tier; s.experts.tier[(size_t)mu.layer * (size_t)n_expert + (size_t)mu.expert] = (uint8_t)t; /* Count residency over units, not plane cells: the plane is a rectangle a diff --git a/core/place/heat.cpp b/core/place/heat.cpp index 301cbde0..f1b2b764 100644 --- a/core/place/heat.cpp +++ b/core/place/heat.cpp @@ -16,10 +16,11 @@ constexpr int32_t kBlock = 64; int HeatEngine::init(const Plan& plan, const HeatParams& params) { params_ = params; - stats_ = HeatStats{}; + stats_.reset(); const size_t n = plan.units.size(); tier_.assign(n, Tier::VRAM); + tier_pub_.assign(n); busy_.assign(n, 0u); movable_.assign(n, 0u); pinned_.assign(n, 0u); @@ -59,6 +60,7 @@ int HeatEngine::init(const Plan& plan, const HeatParams& params) { for (size_t u = 0; u < n; ++u) { const MoveUnit& mu = plan.units[u]; tier_[u] = mu.tier; + tier_pub_.set(u, (uint8_t)mu.tier); layer_of_[u] = mu.layer; bytes_[u] = mu.bytes; movable_[u] = mu.movable ? 1u : 0u; @@ -259,6 +261,7 @@ void HeatEngine::set_tier(int32_t unit, Tier t) { else if (was != Tier::VRAM && t == Tier::VRAM) ++res_count_[(size_t)l]; } tier_[(size_t)unit] = t; + tier_pub_.set((size_t)unit, (uint8_t)t); set_place(unit); end_invalidate(unit); diff --git a/core/place/heat.h b/core/place/heat.h index df044000..f57eff5a 100644 --- a/core/place/heat.h +++ b/core/place/heat.h @@ -31,6 +31,8 @@ #include "planner.h" #include +#include +#include #include namespace rad { @@ -111,6 +113,100 @@ struct HeatStats { uint64_t routed = 0, routed_resident = 0, stream_bytes = 0; }; +/* The live twin of HeatStats: every counter an atomic, so the HTTP thread's /stats and LiveView + * reads are relaxed loads beside the dispatch thread's writes rather than a data race. Written + * as plain counters are (++ and +=), read through snapshot(). See MoverCounters, same reasoning. + */ +struct HeatCounters { + std::atomic dispatches{0}; + std::atomic proposed{0}, promotions{0}, demotions{0}; + std::atomic refused{0}, no_candidate{0}; + std::atomic distinct_promoted{0}, readmits{0}; + std::atomic routed{0}, routed_resident{0}, stream_bytes{0}; + + /* HeatEngine is copyable — place_test snapshots engines — so its counters must copy too. + * A copy takes the current values: relaxed loads into the copy's own atomics. */ + HeatCounters() = default; + HeatCounters(const HeatCounters& o) { *this = o; } + HeatCounters& operator=(const HeatCounters& o) { + dispatches.store(o.dispatches.load(std::memory_order_relaxed), std::memory_order_relaxed); + proposed.store(o.proposed.load(std::memory_order_relaxed), std::memory_order_relaxed); + promotions.store(o.promotions.load(std::memory_order_relaxed), std::memory_order_relaxed); + demotions.store(o.demotions.load(std::memory_order_relaxed), std::memory_order_relaxed); + refused.store(o.refused.load(std::memory_order_relaxed), std::memory_order_relaxed); + no_candidate.store(o.no_candidate.load(std::memory_order_relaxed), + std::memory_order_relaxed); + distinct_promoted.store(o.distinct_promoted.load(std::memory_order_relaxed), + std::memory_order_relaxed); + readmits.store(o.readmits.load(std::memory_order_relaxed), std::memory_order_relaxed); + routed.store(o.routed.load(std::memory_order_relaxed), std::memory_order_relaxed); + routed_resident.store(o.routed_resident.load(std::memory_order_relaxed), + std::memory_order_relaxed); + stream_bytes.store(o.stream_bytes.load(std::memory_order_relaxed), + std::memory_order_relaxed); + return *this; + } + + void reset() { + /* Field by field: atomics are not copy-assignable. */ + dispatches.store(0, std::memory_order_relaxed); + proposed.store(0, std::memory_order_relaxed); + promotions.store(0, std::memory_order_relaxed); + demotions.store(0, std::memory_order_relaxed); + refused.store(0, std::memory_order_relaxed); + no_candidate.store(0, std::memory_order_relaxed); + distinct_promoted.store(0, std::memory_order_relaxed); + readmits.store(0, std::memory_order_relaxed); + routed.store(0, std::memory_order_relaxed); + routed_resident.store(0, std::memory_order_relaxed); + stream_bytes.store(0, std::memory_order_relaxed); + } + + HeatStats snapshot() const { + HeatStats s; + s.dispatches = dispatches.load(std::memory_order_relaxed); + s.proposed = proposed.load(std::memory_order_relaxed); + s.promotions = promotions.load(std::memory_order_relaxed); + s.demotions = demotions.load(std::memory_order_relaxed); + s.refused = refused.load(std::memory_order_relaxed); + s.no_candidate = no_candidate.load(std::memory_order_relaxed); + s.distinct_promoted = distinct_promoted.load(std::memory_order_relaxed); + s.readmits = readmits.load(std::memory_order_relaxed); + s.routed = routed.load(std::memory_order_relaxed); + s.routed_resident = routed_resident.load(std::memory_order_relaxed); + s.stream_bytes = stream_bytes.load(std::memory_order_relaxed); + return s; + } +}; + +/* The per-unit tier mirror HeatEngine keeps for the HTTP thread: one relaxed atomic byte per + * unit, written by set_tier and read by tier_published. A type of its own because it must be + * copyable — HeatEngine is copyable, and a copy takes the tier values into an array of its + * own — which a bare unique_ptr of atomics is not. */ +struct TierMirror { + std::unique_ptr[]> a; + size_t n = 0; + + void assign(size_t m) { + n = m; + a.reset(m ? new std::atomic[m] : nullptr); + } + void set(size_t i, uint8_t v) const { a[i].store(v, std::memory_order_relaxed); } + uint8_t get(size_t i) const { return a[i].load(std::memory_order_relaxed); } + + TierMirror() = default; + TierMirror(const TierMirror& o) { + assign(o.n); + for (size_t i = 0; i < n; ++i) set(i, o.get(i)); + } + TierMirror& operator=(const TierMirror& o) { + if (this == &o) return *this; + assign(o.n); + for (size_t i = 0; i < n; ++i) set(i, o.get(i)); + return *this; + } +}; + class HeatEngine { public: /* Binds to the plan. A static plan is a mode, not a failure: init returns RAD_OK and @@ -145,6 +241,12 @@ public: void set_tier(int32_t unit, Tier t); void set_busy(int32_t unit, bool busy); Tier tier(int32_t unit) const { return tier_[(size_t)unit]; } + /* The tier as the HTTP thread may safely read it: set_tier mirrors every change into a + * relaxed atomic, and /stats and LiveView read the mirror. tier_ itself belongs to the + * dispatch thread; reading it from another thread mid-step is a data race. */ + Tier tier_published(int32_t unit) const { + return (Tier)tier_pub_.get((size_t)unit); + } bool busy(int32_t unit) const { return busy_[(size_t)unit] != 0; } /* The best swap available, or false when the guards refuse. */ @@ -165,7 +267,9 @@ public: const int32_t l = (size_t)unit < layer_of_.size() ? layer_of_[(size_t)unit] : -1; return l >= 0 && (size_t)l < res_count_.size() ? res_count_[(size_t)l] : 0; } - const HeatStats& stats() const { return stats_; } + /* A snapshot by value: the counters are atomics read while the dispatch thread writes them. + * Engine-internal decisions must use the individual counters (`warm`), not a snapshot. */ + HeatStats stats() const { return stats_.snapshot(); } /* Exit banner, ending with the per-layer resident counts, which show whether every layer kept * its share of slots. */ @@ -175,7 +279,7 @@ private: HeatParams params_; bool enabled_ = false; int32_t n_layer_ = 0; - HeatStats stats_; + HeatCounters stats_; /* Heat by position, not by unit. Each layer owns a run of positions whose front is indexed by * expert: expert e of layer l is at slot_lo_[l] + e, whether or not the plan has a unit for it @@ -208,6 +312,10 @@ private: /* Unit bytes from the plan, so a miss is priced where it is detected. */ std::vector bytes_; std::vector tier_; + /* Per unit, the tier as a relaxed atomic mirror, for the HTTP thread's reads (tier_published + * reads, set_tier and init write). Sized with tier_; element type uint8_t to keep the read + * lock-free and the mirror one byte per unit. */ + TierMirror tier_pub_; std::vector busy_; std::vector movable_; /* --expert-pin-top: a decode-hot resident the planner pinned is never a demotion candidate, diff --git a/core/place/mover.cpp b/core/place/mover.cpp index ea1043d3..f2aa60db 100644 --- a/core/place/mover.cpp +++ b/core/place/mover.cpp @@ -111,7 +111,7 @@ int Mover::init(const Program& prog, const Plan& plan, const MoverConfig& cfg, P plan_ = &plan; cfg_ = cfg; budget_ = budget; - stats_ = MoverStats{}; + stats_.reset(); if (!budget_) RAD_WARN("mover: no Pools given; allocating the slab and host pool directly (test only)"); diff --git a/core/place/mover.h b/core/place/mover.h index aff35c53..59f75059 100644 --- a/core/place/mover.h +++ b/core/place/mover.h @@ -349,6 +349,93 @@ struct MoverStats { uint64_t file_read_ns = 0, file_wait_ns = 0; }; +/* The live twin of MoverStats: every counter an atomic. The mover writes these through the + * step on its rank thread while the HTTP thread reads them for /stats and LiveView, and reading + * a plain field another thread writes is a data race (UB). Relaxed atomics give exactly the + * "either side of a store" telemetry promises, at no cost on the writer. snapshot() loads them + * all into a plain MoverStats; same reasoning that made flex_used_ atomic, for the same reader. + */ +struct MoverCounters { + std::atomic dispatches{0}; + std::atomic promotions{0}, demotions{0}; + std::atomic h2d_bytes{0}, d2h_bytes{0}; + std::atomic prefetched{0}, prefetch_bytes{0}; + std::atomic no_record{0}, starved_slab{0}, starved_pool{0}; + std::atomic quarantined{0}; + std::atomic dropped{0}; + std::atomic donated{0}, relocated{0}, stranded{0}; + std::atomic ssd_reads{0}, ssd_bytes{0}; + std::atomic stage_layers{0}, staged_units{0}, staged_bytes{0}, stage_overflow{0}; + std::atomic stage_copies{0}; + std::atomic file_layers{0}, file_units{0}, file_bytes{0}, file_preads{0}; + std::atomic file_read_ns{0}, file_wait_ns{0}; + + void reset() { + /* Field by field: atomics are not copy-assignable. */ + dispatches.store(0, std::memory_order_relaxed); + promotions.store(0, std::memory_order_relaxed); + demotions.store(0, std::memory_order_relaxed); + h2d_bytes.store(0, std::memory_order_relaxed); + d2h_bytes.store(0, std::memory_order_relaxed); + prefetched.store(0, std::memory_order_relaxed); + prefetch_bytes.store(0, std::memory_order_relaxed); + no_record.store(0, std::memory_order_relaxed); + starved_slab.store(0, std::memory_order_relaxed); + starved_pool.store(0, std::memory_order_relaxed); + quarantined.store(0, std::memory_order_relaxed); + dropped.store(0, std::memory_order_relaxed); + donated.store(0, std::memory_order_relaxed); + relocated.store(0, std::memory_order_relaxed); + stranded.store(0, std::memory_order_relaxed); + ssd_reads.store(0, std::memory_order_relaxed); + ssd_bytes.store(0, std::memory_order_relaxed); + stage_layers.store(0, std::memory_order_relaxed); + staged_units.store(0, std::memory_order_relaxed); + staged_bytes.store(0, std::memory_order_relaxed); + stage_overflow.store(0, std::memory_order_relaxed); + stage_copies.store(0, std::memory_order_relaxed); + file_layers.store(0, std::memory_order_relaxed); + file_units.store(0, std::memory_order_relaxed); + file_bytes.store(0, std::memory_order_relaxed); + file_preads.store(0, std::memory_order_relaxed); + file_read_ns.store(0, std::memory_order_relaxed); + file_wait_ns.store(0, std::memory_order_relaxed); + } + + MoverStats snapshot() const { + MoverStats s; + s.dispatches = dispatches.load(std::memory_order_relaxed); + s.promotions = promotions.load(std::memory_order_relaxed); + s.demotions = demotions.load(std::memory_order_relaxed); + s.h2d_bytes = h2d_bytes.load(std::memory_order_relaxed); + s.d2h_bytes = d2h_bytes.load(std::memory_order_relaxed); + s.prefetched = prefetched.load(std::memory_order_relaxed); + s.prefetch_bytes = prefetch_bytes.load(std::memory_order_relaxed); + s.no_record = no_record.load(std::memory_order_relaxed); + s.starved_slab = starved_slab.load(std::memory_order_relaxed); + s.starved_pool = starved_pool.load(std::memory_order_relaxed); + s.quarantined = quarantined.load(std::memory_order_relaxed); + s.dropped = dropped.load(std::memory_order_relaxed); + s.donated = donated.load(std::memory_order_relaxed); + s.relocated = relocated.load(std::memory_order_relaxed); + s.stranded = stranded.load(std::memory_order_relaxed); + s.ssd_reads = ssd_reads.load(std::memory_order_relaxed); + s.ssd_bytes = ssd_bytes.load(std::memory_order_relaxed); + s.stage_layers = stage_layers.load(std::memory_order_relaxed); + s.staged_units = staged_units.load(std::memory_order_relaxed); + s.staged_bytes = staged_bytes.load(std::memory_order_relaxed); + s.stage_overflow = stage_overflow.load(std::memory_order_relaxed); + s.stage_copies = stage_copies.load(std::memory_order_relaxed); + s.file_layers = file_layers.load(std::memory_order_relaxed); + s.file_units = file_units.load(std::memory_order_relaxed); + s.file_bytes = file_bytes.load(std::memory_order_relaxed); + s.file_preads = file_preads.load(std::memory_order_relaxed); + s.file_read_ns = file_read_ns.load(std::memory_order_relaxed); + s.file_wait_ns = file_wait_ns.load(std::memory_order_relaxed); + return s; + } +}; + /* ------------------------------------------------------------------ the mover */ class Mover { public: @@ -543,7 +630,8 @@ public: } int64_t file_buffer_bytes() const { return file_vram_ ? file_buf_bytes_ : 0; } - const MoverStats& stats() const { return stats_; } + /* A snapshot by value: the counters are atomics read while the rank thread writes them. */ + MoverStats stats() const { return stats_.snapshot(); } std::string report() const; private: @@ -624,7 +712,7 @@ private: const Plan* plan_ = nullptr; MoverConfig cfg_; Residency res_; - MoverStats stats_; + MoverCounters stats_; Pools* budget_ = nullptr; /* True only when this object called the allocator itself (the test path). A Pool * reservation belongs to the Pool; freeing it here would be a double free. */ diff --git a/core/runtime/ctx.cpp b/core/runtime/ctx.cpp index ff0f746c..968c4a25 100644 --- a/core/runtime/ctx.cpp +++ b/core/runtime/ctx.cpp @@ -1392,7 +1392,9 @@ void Ctx::flush_issued() { for (size_t r = 0; r < unflushed_.size(); ++r) { const uint32_t i = unflushed_[r]; if (i < plans_.size() && i < program_->ops.size() && plans_[i].issued) - program_->ops[i].issued = true; + /* /graph reads this same bool from the HTTP thread: an atomic_ref store keeps that + * pair a relaxed exchange rather than a data race (ctx.h OpInfo::issued). */ + std::atomic_ref(program_->ops[i].issued).store(true, std::memory_order_relaxed); else unflushed_[w++] = i; } diff --git a/core/runtime/ctx.h b/core/runtime/ctx.h index db6bf847..a4d82d9d 100644 --- a/core/runtime/ctx.h +++ b/core/runtime/ctx.h @@ -80,7 +80,9 @@ struct OpPlan { * leave out. */ int32_t n_opd_schema = -1; int32_t n_opd_required = -1; - bool issued = false; /* did the run phase ever issue it (spec §16's graph dump) */ + bool issued = false; /* did the run phase ever issue it (spec §16's graph dump); + the step path and the HTTP graph dump meet on it through + std::atomic_ref (Ctx::flush_issued, rad_graph.cpp) */ /* Whether issuing it puts bytes on the interconnect. Set at bind from the resolved rows * (rad_kernel_collective) and the conventional all_reduce. The weight stager's copies share * the link and give way to it while it runs (Ctx::issue_body). */ diff --git a/core/server/server.h b/core/server/server.h index cccc7804..7200e80f 100644 --- a/core/server/server.h +++ b/core/server/server.h @@ -134,9 +134,10 @@ struct ServerOptions { * a string rendered once: the dump includes `OpInfo::issued`, which a pre-first-step snapshot * would report as false for every op, hiding dead declarations. * - * Regenerating reads the Program on the HTTP thread while the step path writes that field. It - * is a monotonic false -> true bool, so at worst a mid-step fetch shows an op unissued one step - * early. Registry::select is called with record = false and mutates nothing else. */ + * Regenerating reads the Program on the HTTP thread while the step path writes that field; + * both sides go through std::atomic_ref (Ctx::flush_issued, rad_graph.cpp), so the pairing is + * a relaxed exchange rather than a data race. Registry::select is called with record = false + * and mutates nothing else. */ std::function graph_json; /* The startup report as JSON (core/diag/diag.h): options in force, the machine as it is now * (the provider may reuse a reading for a second), and what on it costs speed. Without an API