Skip to content

Export every operation in memtier_latency_seconds (fixes #637) - #665

Merged
fcostaoliveira merged 8 commits into
masterfrom
fix-637-prom-latency-cumulative
Oct 16, 2026
Merged

fcostaoliveira merged 8 commits into
masterfrom
fix-637-prom-latency-cumulative

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Decision: memtier_latency_seconds now counts every completed operation exactly once. It is no longer a sample, and the docs and HELP text no longer promise "about half". While a run is in progress, _count trails memtier_ops_total by up to about one second. At run end the two are equal, including after Ctrl+C.

Release note: memtier_latency_seconds now records every completed operation, so memtier_latency_seconds_count is comparable to memtier_ops_total (it trails it by about one second during a run and equals it at run end); before, it was a small, phase-dependent sample and its HELP text said it was not comparable. Dashboards that derived a rate from _count will now show the real operation rate.

Problem

memtier_latency_seconds_count stops growing after the first second. It gains a few dozen samples per second while memtier_ops_total gains tens of thousands. On master with -c 5 -t 2, scraped once a second:

ops_total  latency_seconds_count
34434      31030
82763      37060
151017     37135
251623     37203

Root cause

I confirmed this by logging each exporter tick in run_stats::copy_inst_histogram_if_changed() with -c 1 -t 1:

wsec=1 usec_into_sec=272 cur=6     last=0     added=6
wsec=2 usec_into_sec=380 cur=8     last=6     added=5
wsec=2 usec_into_sec=452 cur=26730 last=8     added=26722
wsec=4 usec_into_sec=579 cur=10    last=26730 added=10

The exporter read each client's per-second histogram, inst_m_totals_latency_histogram. The worker resets that histogram at every second boundary in summarize_current_second(). The main loop's 1 Hz tick lands at an almost fixed phase, 0.3 to 0.6 ms after that reset, so it sees only a handful of samples. prom::hdr_add_positive_delta() then drops part of those too: 8 samples seen, 5 added. Now and then a tick lands just before the reset and catches one full second. That is why the capture rate depends on phase and is never close to the 50% the docs claim.

Fix

  • run_stats gets a per-client m_prom_pending_latency_histogram. Right before the per-second reset, the worker adds the second's histogram into it. This only happens when the exporter is enabled (m_config->prometheus != NULL), so the cost is one hdr_add per client per second and nothing per operation.
  • The exporter tick drains the pending histogram (add, then reset) through drain_prom_latency_histogram(). Both sides hold m_inst_histogram_mutex, and only those two code paths touch the pending histogram, so every sample is exported exactly once with no race.
  • set_end_time() already summarizes the last partial second. Every tick drains every thread, finished ones included, so threads that finish early under --requests don't lag. That is safe because the drain holds the mutex and every m_cg lives until after the run-end publish. The run-end publish drains once more and passes the result to publish_run_end(), which used to publish a NULL histogram. In the normal flow the loop's last tick has already drained everything. The run-end drain covers the exits that skip that tick: Ctrl+C and the connection-stage abort.
  • Ctrl+C and the connection-stage abort need two more steps (Bugbot finding). There, client_group::interrupt() calls set_end_time() from the main thread while a worker may still be finishing its current read callback. When the run is interrupted and the exporter is enabled, set_end_time() now leaves the live inst_m_totals_latency_histogram in place instead of folding and resetting it from the main thread, and the post-join run-end drain (drain_prom_latency_histogram(target, run_end=true)) also moves whatever is left in it. A main-thread fold+reset races the worker's lock-free recording and drops the samples recorded between the add and the reset; ops completed after it were never folded at all. Both changes are no-ops on the --test-time / --requests paths, where set_end_time() runs on the worker after all connections are idle.
  • Removed copy_inst_histogram_if_changed(), prom::hdr_add_positive_delta() with its U9 unit test, and the matching TSan suppression line. None of them is used any more.
  • A stalled server adds nothing, because nothing completes. The stall-dedup test F19 still passes.
  • Docs: README, docs/REALTIME_METRICS.md (error model) and the HELP text now describe the actual behaviour. That includes a longer lag while the server is stalled, and HDR quantization that is 1% only above ~1 ms: the slots are 8 µs wide below ~2 ms, so up to 8% at 100 µs.

Tests

New tests/test_prometheus.py::test_F25_latency_count_tracks_ops (10 s run, -t 2 -c 2). It scrapes every 0.5 s and asserts two things:

  • _count <= ops_total on every scrape.
  • From the 5th scrape to the last, _count grows by at least 0.7 times what ops_total grows.

Measured locally (standalone, --env existing-env). The ratio is Δcount/Δops:

binary runs result Δcount/Δops
origin/master 0298524 6 6/6 FAIL 0.0006, 0.0006, 0.002, 0.005, 0.022, 0.34
this PR 9 9/9 PASS 0.999 to 1.29 (the first scrape can be one second behind)

Run-end exactness with --run-count=2: at the run-1 boundary, ops_total and _count were both 254998.

New test_F26_run_end_drains_latency (--requests=50000 -t 2 -c 2 --run-count=2) asserts that every scrape taken during run 2 already counts all 200000 run-1 operations in _count, and that _count <= ops_total.

binary F26 runs result
this PR 10 10/10 PASS
mutant: drain skips finished threads (the old tick guard, now also at run end) 5 5/5 FAIL (_count 50789 to 181412 of 200000)
mutant: publish_run_end publishes NULL 5 5/5 PASS. This mutant is now equivalent in the normal flow, because the loop's last tick drains the finished threads. It only matters on the Ctrl+C and connection-stage-abort exits; F27 below covers Ctrl+C.

Ctrl+C at run end (-t 4 -c 2 --pipeline 1024 --test-time 30, SIGINT after a random 1.2-2.0 s, run-end _count vs memtier_ops_total read from a temporary debug print in publish_run_end, not committed).

binary runs _count < ops_total at run end
e31b472 (before) 20 7/20, short by 28 to 988 ops
run-end drain of the live histogram only 20 4/20, short by 12 to 15 ops (the main-thread fold/reset window)
this PR (07e4370, same code as 43ee3f8) 44 0/44

New test_F27_interrupt_run_end_counts_every_op (--test-time=30 -t 4 -c 10 --pipeline=16 --ratio=1:1 --run-count=2). Ctrl+C does not stop later --run-count runs: run 1 publishes its run-end snapshot right away and run 2 starts about a second later, so a scrape in that gap sees run 1's final counters. F27 stops scraping for 1.2 s (so the 1 s render cache expires), sends SIGINT with traffic in flight, then scrapes until exit. On the last scrape that shows run 1 with 0 threads, it asserts _count == memtier_ops_total. It skips if no scrape lands between the runs.

binary F27 runs result
this PR (43ee3f8) 15 standalone + 3 oss-cluster all PASS
mutant: run-end drain without the live histogram (prom_drain_latency(cfg, threads, false)) 5 5/5 FAIL, _count short by 224096 to 318240 ops
358db50 (before the Ctrl+C fix) 20 20/20 PASS. This race is narrow at --pipeline=16; a reviewer's probe saw 2/35 short by 16

So F27 guards the run-end drain of the live histogram, the part a revert would break wholesale. The narrow loss it was written for, between the main thread's add and reset, can't be caught reliably from outside the process. That part is covered by code review and the reviewers' instrumented SIGINT probes: 2/25 runs short on the parent commit, 0/40 at this head. A deterministic run_stats unit test is a follow-up.

With --test-time 3, --requests 20000 and --test-time 2 --run-count 2, the run-end _count equals ops_total exactly (2198912, 160000, 1579712 / 3976064).

F25's 0.7 bound: the window is about 7-8 s and _count lags by at most about 1 s of ops at either end, so the ratio cannot drop much below 0.86. It also holds in the sanitizer builds: at e31b472, F25 and F26 passed in the ASAN and TSAN Single Endpoint: TCP TLS (misc) cells (which run test_prometheus.py), and every ASAN/TSAN/UBSan cell was green.

The rest of test_prometheus.py passes with this PR. F18 and F23 are skipped because they need a cluster. F15 needs ROOT_FOLDER set for its fixture, and passes with it. make check (unit tests) passes.

Affected versions

2.5.0 through 2.5.2 (latest) and master.

Fixes #637

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

🤖 Automated first-pass review — a human maintainer's review is still required before merge.

Thanks for this, and for the instrumentation in the description. It makes the root cause easy to follow. I went through the C++ side and the new tests, and a few comments are below.

The design looks right to me. The worker folds into m_prom_pending_latency_histogram under m_inst_histogram_mutex right before its own reset, and the main thread drains under the same mutex, so each sample gets exported once by construction. That's simpler than the old positive-delta snapshotting, and removing hdr_add_positive_delta/U9 and the TSan suppression line follows from it. When the exporter is off, the only extra work is one branch per client per second, so I don't see a hot-path concern. Draining before prom_observe_and_fill() so _count can't get ahead of ops_total is a good touch.

The metric's meaning does change for anyone scraping it. I don't think that's a compat problem in the usual sense, though. The old HELP text explicitly said _count was not comparable to ops_total, and the real capture rate was far below the documented one. This brings the series in line with what people would expect, and the release note covers it. F25 failing 6/6 on master and passing on this branch is the kind of fails-then-passes regression test we want for an output fix.

A couple of questions:

  • F27 calls env.skip() when no scrape lands between the two runs. That's reasonable on a slow runner, but if it skips most of the time in CI, the Ctrl+C path ends up with no coverage at all. Have you checked how often it actually skips in the CI cells? If it's often, maybe widen the window, or record the skip count somewhere so a silent no-op is visible.
  • Just checking: on the interrupt path, summarize_current_second(true) still runs summarize_quantiles on the live totals histogram from the main thread while the worker may be recording. I think that race was already there before this PR and only the reset was dropped, so the per-second output shouldn't be any worse. Do you confirm?

Nitpicking: prom_publish_tick is now a one-line forward to publish(). It could be inlined, but I'm fine keeping it for symmetry with the other prom_* stubs.

Overall the fix looks solid to me. The threading changes in set_end_time()/interrupt() may still be worth a second look from whoever owns the exporter before merge.

(would file this as a comment, not a formal block)

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 48df4ac. Configure here.

Comment thread run_stats.cpp Resolved
@fcostaoliveira
fcostaoliveira force-pushed the fix-637-prom-latency-cumulative branch from e31b472 to 07e4370 Compare October 16, 2026 07:07
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Replies to your points:

  • Release note. Yes, it should be called out. I added a release-note sentence near the top of the PR body: memtier_latency_seconds now records every completed operation, so memtier_latency_seconds_count is comparable to memtier_ops_total. It trails it by about one second during a run and equals it at run end. Before this change it was a small, phase-dependent sample, and its HELP text said it was not comparable. Dashboards that derived a rate from _count will now show the real operation rate.
  • The 0.7 bound under sanitizers. It holds. test_prometheus.py runs in the sanitizer (misc) cells. At e31b472, F25 and F26 passed in ASAN Single Endpoint: TCP TLS (misc) and TSAN Single Endpoint: TCP TLS (misc) (I checked the job logs), and every ASAN, TSAN and UBSan cell was green. The bound has a lot of margin: the ratio cannot drop much below about 0.86, and it measured 0.999 to 1.29 locally.
  • tsan_suppressions.txt. Re-wrapped in 07e4370 so the line matches the rest of the block.

07e4370 also fixes the Bugbot finding about Ctrl+C: on that path the run-end _count now equals ops_total (0/44 runs short, was 7/20). Details are in the inline reply.

The Test OSS TCP TLS v1.2 failure at e31b472 was the 25-minute step timeout (#679, which #683 fixes), not a test failure.

@fcostaoliveira
fcostaoliveira force-pushed the fix-637-prom-latency-cumulative branch from 07e4370 to 0090da4 Compare October 16, 2026 07:26
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Follow-up on the updated review, now at 0090da4 (rebased on master 57ad921):

  • m_interrupted as a stand-in for "called on the main thread". interrupt() sets it before finalize_all_clients(). The worker's own paths, both its stop condition and the worker-failure finalize_all_clients(), leave it false, so they fold on the only thread that writes the histogram. A worker that races interrupt() can read either value, and both are safe: false folds on the only writer, and true leaves the samples for the drain after the join. A comment at the call site now says this, and that any new main-thread caller of set_end_time() must set the flag first. Three reviewers checked this under TSan with SIGINT runs and found no new reports. The only reports that touch set_end_time are the existing one_second_stats push_back race, which race:run_stats::set_end_time already covers.
  • No automated Ctrl+C test. A scrape test can't see the run-end snapshot, because the exporter goes away about 30 ms after SIGINT. The cheap option is a unit test of run_stats (interrupt, record, set_end_time, record more, drain with run_end=true, then compare counts), but run_stats.cpp isn't linked into any unit test yet. I'm leaving that for a follow-up. Reviewers measured the SIGINT scenario with a temporary debug print: 2 of 25 runs were short on the parent commit and 0 of 40 at this head. In cluster mode it was 0 of 15.
  • prom_publish_tick pass-through. It stays, so the !HAVE_EVHTTP stubs keep the same shape as the real functions.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Correction to my earlier note: I said a scrape test can't see the run-end snapshot after Ctrl+C. That was wrong. Ctrl+C does not stop later --run-count runs. Run 1 publishes its run-end snapshot right away, and run 2 starts about a second later, so the exporter keeps serving run 1's final counters through that gap.

43ee3f8 adds test_F27_interrupt_run_end_counts_every_op, which uses that gap. It runs --run-count=2 and stops scraping for 1.2 s so the render cache expires. Then it sends SIGINT with traffic in flight and asserts _count == memtier_ops_total on the last scrape that shows run 1 with 0 threads. It skips if no scrape lands between the runs.

  • This PR: 15/15 PASS standalone, 3/3 PASS oss-cluster.
  • Mutant where the run-end drain skips the live histogram: 5/5 FAIL, short by 224k to 318k ops.
  • 358db50, before the Ctrl+C fix: 20/20 PASS locally. That race is narrow at --pipeline=16.

fcostaoliveira and others added 8 commits October 16, 2026 08:52
The Prometheus exporter read each client's per-second latency histogram at
its 1 Hz tick. That histogram is reset at each worker-second boundary, and
the main loop's tick lands at an almost constant phase just after the
reset (instrumented: 0.3-0.6 ms into the worker second, 6-10 samples
visible), so each tick saw only a handful of samples. The positive-delta
copy then dropped part of those too. memtier_latency_seconds_count grew by
tens of samples per second while memtier_ops_total grew by tens of
thousands, contrary to the "about half" the docs promised.

Instead, the worker now folds each completed second into a per-client
pending histogram right before the per-second reset (only when the
exporter is enabled), and the exporter tick drains it (add + reset). Both
sides hold m_inst_histogram_mutex, so every sample is exported exactly
once. The last partial second is folded by set_end_time(), and the
run-end publish drains every thread, so _count matches the ops completed
in the run. While a run is in progress _count trails ops_total by up to
about one second.

This removes copy_inst_histogram_if_changed and the now-unused
prom::hdr_add_positive_delta (and its U9 unit test). The README,
docs/REALTIME_METRICS.md and the HELP text no longer claim sampling.

Fixes #637

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The 1 Hz tick skipped threads that had already finished, so with
--requests a thread that finished early kept its last partial second out of
memtier_latency_seconds until the run-end publish. The drain is protected by
m_inst_histogram_mutex and every m_cg lives until after the run-end
publish, so finished threads are now drained too. The run-end drain stays
for the exits that skip the loop's last tick (Ctrl+C, connection-stage
abort).

Docs and HELP text: the histogram also lags while the server is stalled,
and the HDR quantization is 1% only above ~1 ms (8 us slots below ~2 ms).

Refs #637

Co-Authored-By: Claude Opus 5.5 <[email protected]>
test_F26 runs --requests with --run-count=2 and asserts every scrape taken
during run 2 already counts all of run 1's operations in
memtier_latency_seconds_count. Also document how F25's 0.7 bound follows
from the at-most-one-second lag.

Refs #637

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The 1 Hz tick read every thread's ops counter, then drained the latency
histograms. If a thread finished, or a connection rolled its second, in
between, the drain picked up samples whose ops were not yet counted, and
the scrape showed memtier_latency_seconds_count > memtier_ops_total until
the next tick. Draining first makes that impossible: a second is folded
under the drain's mutex only after its ops were counted.

Docs: the lag is about one second, longer when a connection's next op is
slow (a stalled server, a low --rate-limiting rate), and _count never
exceeds ops_total. Re-wrap the tsan_suppressions.txt comment.

Refs #637

Co-Authored-By: Claude Opus 5.5 <[email protected]>
On Ctrl+C and the connection-stage abort, client_group::interrupt() calls
set_end_time() from the main thread while a worker may still be finishing
its current read callback. Two things lost latency samples there:

- set_end_time() folded the live inst-totals histogram into the pending
  histogram and reset it on the main thread, while the worker recorded
  into it lock-free. Samples recorded between the add and the reset were
  dropped.
- Replies the worker completed after that fold were counted in
  memtier_ops_total but never reached the pending histogram, because
  m_end_set stops a second set_end_time().

So the run-end memtier_latency_seconds_count could trail memtier_ops_total
(7 of 20 SIGINT runs, by up to 988 ops). When interrupted with the
exporter enabled, set_end_time() now leaves the live totals in place, and
the post-join run-end drain moves what is left in them as well. Both are
no-ops on the --test-time / --requests paths.

Also re-wrap the over-long tsan_suppressions.txt comment line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Ctrl+C does not stop later --run-count runs: run 1 publishes its run-end
snapshot right away and run 2 starts a second later, so a scrape in that
gap sees run 1's final memtier_ops_total and memtier_latency_seconds_count.
F27 interrupts run 1 mid-traffic, lets the 1 s render cache expire first so
its next scrape renders inside the gap, and asserts the two are equal. It
skips if no scrape lands between the runs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
set_end_time() inferred "called from interrupt() on the main thread" from
m_interrupted, a plain bool the main thread writes while a worker may read
it. interrupt() now passes the intent through
finalize_all_clients(true) -> client::set_end_time_keep_live_totals() ->
run_stats::set_end_time(NULL, true); every other caller keeps the default
(fold on the worker), so no flag is read across threads and a new
main-thread caller has to opt in explicitly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix-637-prom-latency-cumulative branch from 92dedfe to 082233d Compare October 16, 2026 07:53
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

On the updated review, now at 082233d:

  • Commit identity: fixed. A placeholder user.name/user.email had ended up in the local git config of the repo used to build this branch. It's removed, and every commit on the branch is now re-authored as fcostaoliveira, author and committer.
  • m_interrupted read across threads / passing the intent explicitly: done the way you suggested. interrupt() now calls finalize_all_clients(true), which calls client::set_end_time_keep_live_totals(true), which calls run_stats::set_end_time(NULL, true). Every other caller keeps the default and folds on the worker. set_end_time() no longer reads m_interrupted, so nothing crosses threads, and any new main-thread caller has to opt in. test_prometheus.py passes 28/28 locally, including F25-F27.
  • F27 skipping: a reviewer ran it 27 times (release build, single core, TSan with and without pinning) and it never skipped. With the run-end drain mutated it failed 5/5, short by 120k-330k ops. It pauses scraping for more than 1 s before SIGINT, so a fresh render lands in the gap before run 2.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

On the latest review:

  • How often F27 skips in CI: it didn't. At 082233d, F27 ran in 10 CI cells (the plain Test cells plus the ASAN/TSAN/UBSan "misc" cells that run test_prometheus.py) and passed in all 10, with 0 skips. A reviewer also measured 0 skips in 27 local runs, including single-core pinning and TSan. The 1.2 s scrape pause before SIGINT forces a fresh render inside the gap between runs.
  • summarize_quantiles on the interrupt path: confirmed, that race predates this PR. On master, set_end_time() from interrupt() already ran summarize_quantiles and then reset the live histograms from the main thread while the worker recorded. race:run_stats::set_end_time in tsan_suppressions.txt covers it. This PR only stops the reset of the totals histogram on that path, which was the source of the lost samples, so the per-second output is no worse. The push_back race on m_stats in the same function also predates this PR.
  • prom_publish_tick: kept, so the !HAVE_EVHTTP stubs keep the same shape.

@fcostaoliveira
fcostaoliveira merged commit 6eedafb into master Oct 16, 2026
129 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prometheus memtier_latency_seconds stops growing after the first second (samples ~0.1% of ops, not ~50% as documented)

1 participant