Files
Strata/include/strata/core/progress.hpp
T
Niko1221 59375eb069 Integrated gfx11 under Linux: a stop by signal drains the GPU before the process ends
Reset 45 on p2006: SIGTERM 1 s into the first request ended the engine with kernels in
flight and the expert arena still registered; the kernel's exit then evicted the busy
queues ("Failed to evict queue 1", "Failed to quiesce KFD") and MES reset the GPU.

Now SIGTERM, SIGINT and SIGHUP take this order: the handler only writes the signal's
number into a pipe; a thread of its own (strata-stop) sets the serve loop's STOP flag,
releases the verify windows' GPU waits (the #267 release, silent, through a new bounded
hook release_gpu_waits_within), waits for the whole device for what is left of 3 s
(cudaDeviceSynchronize on a helper thread, the wait bounded), drops the arena's
registration only when the device finished (unregister_arena_ranges, pinned.cu, which
unregisters the ranges register_range recorded), and ends the process by the same
signal, so the wait status the caller sees is unchanged. A second signal during the
stop ends the process at once.

The bound: the server stops its engine with STOP + QUIT and waits 20 s, then SIGTERM and
5 s, then SIGKILL (serve/server.py:114, :3011-3034); Control sends SIGTERM to the server
and kills its process group after shutdown_timeout + 8 s (strata_control/runtime.py:
1113-1134); the macOS lifeline waits 10 s after its SIGTERM (serve/lifeline.py:132-145);
Linux children get SIGKILL by PDEATHSIG when the server dies. 3 s stays inside the 5 s.
QUIT was left as it is: it already leaves the serve scope through the destructors (the
verifier syncs its streams, the prefill frees) and ~ArenaExpertSource (the local at
generate.cpp:7898) unregisters the arena before main returns.

Cost by reading: a stop by signal during a request takes up to 3 s longer; nothing while
the engine runs.

Scope: compiled only with STRATA_USE_HIP on __linux__; installed at run time only when
kfd_quiet::class_flag() is set (integrated gfx1103 / gfx1150 / gfx1151, every visible
card), and only for a signal whose action is still the default (nohup's ignored SIGHUP
stays ignored). No signal mask is changed anywhere. Elsewhere the new code is a pointer
stored in Verifier::init and a list of registered range starts kept on Linux; neither is
read without the handler. STRATA_KFD_QUIET=0 or STRATA_KFD_QUIET_EXIT=0 keeps the old
death by signal; STRATA_TRACE prints one line when the handler is in and one at the stop.
Test: the stop's order with a fake GPU layer in tests/core/kfd_quiet_test.cpp.
2026-11-15 01:56:18 +01:00

79 lines
4.1 KiB
C++

// include/strata/core/progress.hpp - issue #29: whether a request is still moving, and where it is.
//
// The engine's host loop and the GPU wait on each other through flags; a protocol bug there does not crash, it
// spins forever (the GPU "100%", one CPU core busy, no tokens). `--serve` runs a watchdog thread over this: a
// request whose heartbeat stops for `STRATA_WATCHDOG_S` seconds (default 60; 0 = off) ends the engine with the
// stage it was stuck in, and the server starts it again instead of hanging. Tokens, prompt chunks and verify
// windows beat; the stage is two relaxed stores per layer, which the token path does not notice.
#pragma once
#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstdio>
namespace strata::core {
struct Progress {
std::atomic<uint64_t> beats{0};
std::atomic<bool> busy{false};
std::atomic<const char*> where{"idle"};
std::atomic<int64_t> detail{-1};
std::atomic<int64_t> chunk{-1}; ///< #251: the prompt chunk (its first position) a batched-read stage is in
std::atomic<int64_t> since_ms{0}; ///< when `where` was set (steady clock): how long a stage has lasted
std::atomic<uint64_t> ticks{0}; ///< layers served: a window that still moves, slowly, against one that stopped
/// a step that blocks in one call (a file flush, a host->device transfer of a whole session) is given until this
/// time (steady ms; 0 = none) instead of the watchdog's limit - an explicit, bounded allowance, not a beat
std::atomic<int64_t> allow_until_ms{0};
};
inline int64_t progress_now_ms() {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch())
.count();
}
/// What the watchdog prints about a part of the engine when it fires (the expert pool, the verify window).
using DiagFn = void (*)(std::FILE*);
inline std::atomic<DiagFn>& diag_pool_fn() { static std::atomic<DiagFn> f{nullptr}; return f; }
inline std::atomic<DiagFn>& diag_verify_fn() { static std::atomic<DiagFn> f{nullptr}; return f; }
/// #267: what a path that ends the engine runs first - it releases the GPU's spin waits on host flags (the verify
/// window's), so no kernel stays resident while the process goes away (on Windows that left the GPU "lost").
inline std::atomic<DiagFn>& release_gpu_fn() { static std::atomic<DiagFn> f{nullptr}; return f; }
inline void release_gpu_waits(std::FILE* f) {
if (auto fn = release_gpu_fn().load()) fn(f);
}
/// The same release with one bound for all live verifiers, silent; true when their streams finished within `ms` (kfd_quiet.hpp,
/// EXIT). True when no verifier is live.
using ReleaseWithinFn = bool (*)(int);
inline std::atomic<ReleaseWithinFn>& release_gpu_within_fn() { static std::atomic<ReleaseWithinFn> f{nullptr}; return f; }
inline bool release_gpu_waits_within(int ms) {
if (auto fn = release_gpu_within_fn().load()) return fn(ms);
return true;
}
/// --pipeline-windows: the pipelined loop's windows and the drafter's chain (set while that loop runs)
inline std::atomic<DiagFn>& diag_pipeline_fn() { static std::atomic<DiagFn> f{nullptr}; return f; }
inline Progress& progress() {
static Progress p;
return p;
}
inline void progress_at(const char* where, int64_t detail = -1, int64_t chunk = -1) {
Progress& p = progress();
p.where.store(where, std::memory_order_relaxed);
p.detail.store(detail, std::memory_order_relaxed);
p.chunk.store(chunk, std::memory_order_relaxed);
p.since_ms.store(progress_now_ms(), std::memory_order_relaxed);
}
inline void progress_beat() { progress().beats.fetch_add(1, std::memory_order_relaxed); }
inline void progress_tick() { progress().ticks.fetch_add(1, std::memory_order_relaxed); }
/// A beat that also sets (seconds > 0) or clears (0) the allowance of a blocking step: the watchdog does not fire
/// before the allowance ends, and fires at its end if nothing beat since.
inline void progress_allow(int64_t seconds) {
progress().allow_until_ms.store(seconds > 0 ? progress_now_ms() + seconds * 1000 : 0, std::memory_order_relaxed);
progress_beat();
}
} // namespace strata::core