Skip to content

Bump medida and batch - #5338

Merged
graydon merged 6 commits into
stellar:masterfrom
graydon:bump-medida-and-batch
Jul 16, 2026
Merged

graydon merged 6 commits into
stellar:masterfrom
graydon:bump-medida-and-batch

Conversation

@graydon

@graydon graydon commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Bump medida to take stellar/medida#38 (speeding up metrics and adding batch interfaces) and then cherry-picking in a couple commits @dmkozh did to further optimize metrics usage on top.

Copilot AI review requested due to automatic review settings July 3, 2026 04:43
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates Stellar Core’s metrics collection to take advantage of newer medida batching APIs, reducing contention and per-op overhead in Soroban/parallel-apply hot paths. It also modernizes SimpleTimer max tracking to be lock-free.

Changes:

  • Replace SimpleTimer’s mutex-protected max tracking with an atomic, lock-free max and atomic reset on syncMax().
  • Add per-thread SorobanMetrics::ApplyMetricsBatch buffers plus RAII timers (BatchedTimerScope, ScopedNsecsTimer) and publish batched apply-path metrics once per ledger close.
  • Update parallel/sequential Soroban apply paths to record into per-thread batches and add a test ensuring batched metrics are visible after ledger close.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/util/SimpleTimer.h Switch max tracking to atomic; header include changes.
src/util/SimpleTimer.cpp Implement lock-free max update + atomic exchange on sync.
src/transactions/TransactionFrame.cpp Batch per-tx Soroban metrics and use BatchedTimerScope for op apply timing in parallel apply.
src/ledger/LedgerManagerImpl.cpp Record per-tx apply timing into Soroban per-thread batches in apply threads.
src/ledger/SorobanMetrics.h Introduce per-thread apply metrics batching + new RAII timer helpers.
src/ledger/SorobanMetrics.cpp Implement thread-local batch acquisition, draining, and bulk publishing via UpdateMany.
src/transactions/InvokeHostFunctionOpFrame.cpp Record host-fn op metrics into per-thread batch and batch exec-time samples.
src/transactions/ExtendFootprintTTLOpFrame.cpp Record op metrics into per-thread batch and batch exec-time samples.
src/transactions/RestoreFootprintOpFrame.cpp Record op metrics into per-thread batch and batch exec-time samples.
src/transactions/test/InvokeHostFunctionTests.cpp Add coverage verifying batched Soroban metrics are published at ledger close.

Comment thread src/util/SimpleTimer.h Resolved
Comment thread src/ledger/SorobanMetrics.cpp Resolved
Comment thread src/ledger/SorobanMetrics.h Resolved
Comment thread src/ledger/SorobanMetrics.cpp Resolved
graydon and others added 4 commits July 15, 2026 13:50
SimpleTimer::Update is called from the parallel apply threads (notably the
bucket point-load timers), where the per-update mutex around max tracking
was a cross-thread contention point. The sum/count counters were already
atomic; with a compare-exchange max the whole timer is now lock-free.

Co-Authored-By: Claude Fable 5 <[email protected]>
With metrics enabled, every InvokeHostFunction op marked ~20 process-wide
meters and updated ~7 histograms/timers from the apply threads (~25
contended lock/cache-line operations per op), and the parallel path also
performed a registry lookup of the ledger.operation.apply timer per
transaction. On the apply-load benchmark (sac, TX=6000, T=8) this made
close time 282ms with metrics vs 107ms without, with p99 spikes from CKMS
compaction; DISABLE_SOROBAN_METRICS_FOR_TESTING existed to hide this.

Instead, record all per-op/per-tx metric updates into a per-thread
SorobanMetrics::ApplyMetricsBatch (meter increments as plain sums, sample
streams for percentile-bearing histograms/timers as raw value vectors,
one brief uncontended lock per record) and drain all batches into the
underlying medida metrics once per ledger in
publishAndResetLedgerWideMetrics(), using the new UpdateMany bulk APIs.
Observable metric values are preserved: meter counts are identical sums,
EWMA rates tick at 5s granularity either way, and histograms receive the
exact same sample stream, just at close time.

The sequential apply path still updates medida directly (main thread,
cheap after the medida fixes). Combined with those fixes, metrics-on
close time drops to ~106.5ms vs ~104.1ms metrics-off (200-ledger run),
i.e. metric overhead goes from ~164% to ~2%.

Also adds a regression test asserting the batched metrics are visible
after the ledger close that applied the transaction.

Co-Authored-By: Claude Fable 5 <[email protected]>
@graydon
graydon force-pushed the bump-medida-and-batch branch from 83885c2 to 6b02e0d Compare July 15, 2026 21:40
@graydon
graydon added this pull request to the merge queue Jul 16, 2026
Merged via the queue into stellar:master with commit 350acec Jul 16, 2026
54 checks passed
@graydon
graydon deleted the bump-medida-and-batch branch July 16, 2026 18:13
@graydon graydon mentioned this pull request Sep 18, 2026
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.

3 participants