Read code changes as Git diffs. Identifying strings are masked; binary files are listed by their full relative names.
sglang · 2 changed files
Base commit: 340db9201005fc64635bb25db1d2538f955761c3
diff --git a/python/sglang/srt/managers/scheduler.py b/python/sglang/srt/managers/scheduler.pyindex a9615a14c512345b2d63323ff3ebd2fe3d9a0dca..7d5e8b0b3708d1a883306c73860575c779b3b510 100644--- a/python/sglang/srt/managers/scheduler.py+++ b/python/sglang/srt/managers/scheduler.py@@ -202,6 +202,7 @@ from sglang.srt.managers.scheduler_components.batch_result_processor import ( ) from sglang.srt.managers.scheduler_components.dp_attn import SchedulerDPAttnAdapter from sglang.srt.managers.scheduler_components.flush_wrapper import SchedulerFlushWrapper+from sglang.srt.managers.scheduler_components.idle_housekeeper import IdleHousekeeper from sglang.srt.managers.scheduler_components.idle_sleeper import ( IdleSleeper, RustServerIdleSleeper,@@ -473,6 +474,7 @@ class Scheduler( # Init inter-process communication self.init_ipc_channels(port_args) self.init_idle_sleeper()+ self.init_idle_housekeeper() # Init ZBAL, switch allocator should before any torch alloc action self.init_zbal_on_npu()@@ -757,6 +759,9 @@ class Scheduler( except Exception as e: logger.warning("load snapshot writer init failed: %s", e) + def init_idle_housekeeper(self) -> None:+ self.idle_housekeeper = IdleHousekeeper()+ def init_idle_sleeper(self) -> None: if ( self.ps.pp_rank == 0@@ -3928,6 +3933,15 @@ class Scheduler( self.maybe_send_health_check_signal() if not self.is_fully_idle():+ # Busy again: re-arm so the next idle transition does a full pass.+ self.idle_housekeeper.notify_busy()+ return++ # This runs in a hot spin loop, so only the first pass of an idle+ # stretch (plus a rate-limited refresh) pays for the checks below.+ if not self.idle_housekeeper.should_run_housekeeping():+ self.new_token_ratio_tracker.reset()+ self.maybe_sleep_on_idle() return if self.enable_unified_memory:diff --git a/python/sglang/srt/managers/scheduler_components/idle_housekeeper.py b/python/sglang/srt/managers/scheduler_components/idle_housekeeper.pynew file mode 100644index 0000000000000000000000000000000000000000..ff2bc8c8d41aae0b6ea77e28ce3a26d62d80458e--- /dev/null+++ b/python/sglang/srt/managers/scheduler_components/idle_housekeeper.py@@ -0,0 +1,36 @@+from __future__ import annotations++import time++# Repeat interval for the redundant idle housekeeping pass. The first pass on an+# idle transition still runs immediately, so /get_loads and DP balancing never+# see a stale *newly* idle snapshot; only the repeats are rate limited.+IDLE_HOUSEKEEP_REPEAT_INTERVAL_S = 0.25+++class IdleHousekeeper:+ """Rate limiter for the scheduler's idle housekeeping.++ ``Scheduler.on_idle`` runs inside a hot spin loop that only blocks on the+ zmq receive poll. Running the full housekeeping pass (pool invariant checks,+ metrics, load-snapshot publish) on every iteration pins a whole CPU core and+ delays picking up the next request, while the idle snapshot it publishes+ does not change as long as the engine stays idle. This collaborator keeps+ the pass on the idle transition and rate limits the repeats.+ """++ def __init__(self, repeat_interval_s: float = IDLE_HOUSEKEEP_REPEAT_INTERVAL_S):+ self.repeat_interval_s = repeat_interval_s+ # Zero deadline: the very first idle pass always runs.+ self._next_deadline = 0.0++ def should_run_housekeeping(self) -> bool:+ now = time.monotonic()+ if now < self._next_deadline:+ return False+ self._next_deadline = now + self.repeat_interval_s+ return True++ def notify_busy(self) -> None:+ """Re-arm so the next idle transition runs a full pass immediately."""+ self._next_deadline = 0.0