Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

serve: average the throughput gauges over the window between scrapes (#2341)

nekomario28a091cdf68141
diff --git a/serve/server.py b/serve/server.py
index 5ca7f4e4d..ef41da5cc 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -3075,6 +3075,9 @@ class Service:
"prompt_ms": 0.0, "decode_ms": 0.0,
"drafts_offered": 0, "drafts_accepted": 0} # #457: the MTP drafts, summed where reported
self.last_timings = None # the last finished request's, llama.cpp's names (/v1/status)
+ # /metrics/prometheus' two throughput gauges need the totals as they stood at the last scrape, because
+ # llama.cpp averages them over the window between two scrapes (#2341).
+ self.scrape_bucket = (0, 0.0, 0, 0.0)
from serve.prometheus import Latencies
self.latencies = Latencies() # GET /metrics in Prometheus text: the latency histograms
self.last_request_at = None # when a request last started or finished
@@ -3703,8 +3706,15 @@ class Service:
with self.status_lock:
s, t = dict(self.status), dict(self.totals)
running = len(self.live_reqs) or int(bool(s.get("busy")))
- prompt_n = t["prompt_tokens"] - t["reused"] # what was read, as llama.cpp counts it
- prompt_s, decode_s = t["prompt_ms"] / 1000, t["decode_ms"] / 1000
+ prompt_n = t["prompt_tokens"] - t["reused"] # what was read, as llama.cpp counts it
+ prompt_s, decode_s = t["prompt_ms"] / 1000, t["decode_ms"] / 1000
+ # llama.cpp reads these two gauges from a bucket it resets on every scrape (server_metrics::bucket,
+ # "the gauges are averaged over the window between two scrapes"), so an idle server reports 0 rather
+ # than its lifetime average. Keep the same window here, or the names lie to llama.cpp dashboards.
+ was_prompt_n, was_prompt_s, was_predicted_n, was_decode_s = self.scrape_bucket
+ self.scrape_bucket = (prompt_n, prompt_s, t["output_tokens"], decode_s)
+ d_prompt_n, d_prompt_s = prompt_n - was_prompt_n, prompt_s - was_prompt_s
+ d_predicted_n, d_decode_s = t["output_tokens"] - was_predicted_n, decode_s - was_decode_s
rows = [
("llamacpp:prompt_tokens_total", "counter", "Prompt tokens read", prompt_n),
("llamacpp:prompt_tokens_cached_total", "counter", "Prompt tokens the conversation cache already held",
@@ -3712,10 +3722,10 @@ class Service:
("llamacpp:prompt_seconds_total", "counter", "Prompt read time", prompt_s),
("llamacpp:tokens_predicted_total", "counter", "Tokens generated", t["output_tokens"]),
("llamacpp:tokens_predicted_seconds_total", "counter", "Generation time", decode_s),
- ("llamacpp:prompt_tokens_seconds", "gauge", "Average prompt read throughput in tokens/s",
- prompt_n / prompt_s if prompt_s else 0),
- ("llamacpp:predicted_tokens_seconds", "gauge", "Average generation throughput in tokens/s",
- t["output_tokens"] / decode_s if decode_s else 0),
+ ("llamacpp:prompt_tokens_seconds", "gauge", "Average prompt read throughput in tokens/s, since the last scrape",
+ d_prompt_n / d_prompt_s if d_prompt_s > 0 else 0),
+ ("llamacpp:predicted_tokens_seconds", "gauge", "Average generation throughput in tokens/s, since the last scrape",
+ d_predicted_n / d_decode_s if d_decode_s > 0 else 0),
("llamacpp:requests_processing", "gauge", "Requests running", running),
("llamacpp:requests_deferred", "gauge", "Requests waiting their turn", s.get("queued") or 0),
("strata:requests_total", "counter", "Requests finished", t["requests"]),
diff --git a/serve/test_server.py b/serve/test_server.py
index 3a49ff255..d12050317 100644
--- a/serve/test_server.py
+++ b/serve/test_server.py
@@ -1989,6 +1989,39 @@ class PrometheusMetrics(unittest.TestCase):
self.assertEqual((m["strata:requests_total"], m["strata:drafts_offered_total"],
m["strata:drafts_accepted_total"]), (2, 17, 10))
+ def test_throughput_gauges_cover_the_window_between_scrapes(self):
+ """#2341: llama.cpp reads these two from a bucket it resets on every scrape, so a scrape that saw no
+ traffic reports 0 - not the lifetime average, which froze at the last request's speed."""
+ tok = ByteTokenizer()
+ engine = DoneLineEngine(tok, "</think>\n\nok", max_context=CTX,
+ done_lines=["DONE 4 20 400.0 200.0 stop 7 12 5"])
+ svc = Service(engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja"))
+ httpd = serve(svc, port=0)
+ base = f"http://127.0.0.1:{httpd.server_address[1]}"
+
+ def scrape():
+ with urllib.request.urlopen(base + "/metrics/prometheus", timeout=10) as r:
+ return {line.split()[0]: float(line.split()[1])
+ for line in r.read().decode().splitlines() if not line.startswith("#")}
+
+ try:
+ body = json.dumps({"model": "m", "max_tokens": 10, "messages": [{"role": "user", "content": "hi"}]})
+ with urllib.request.urlopen(urllib.request.Request(base + "/v1/chat/completions", data=body.encode(),
+ headers={"Content-Type": "application/json"}),
+ timeout=30) as r:
+ self.assertEqual(r.status, 200)
+ first = scrape()
+ self.assertGreater(first["llamacpp:prompt_tokens_seconds"], 0)
+ self.assertGreater(first["llamacpp:predicted_tokens_seconds"], 0)
+ idle = scrape() # nothing ran since the scrape above, so the window is empty
+ self.assertEqual((idle["llamacpp:prompt_tokens_seconds"],
+ idle["llamacpp:predicted_tokens_seconds"]), (0, 0))
+ self.assertEqual(idle["llamacpp:tokens_predicted_total"], # counters stay cumulative
+ first["llamacpp:tokens_predicted_total"])
+ finally:
+ httpd.shutdown()
+ httpd.server_close()
+
class PcieShare(unittest.TestCase):
"""#588: the hit rate stays the VRAM share of the lookups; the routed experts the GPU read over PCIe (the DONE