Skip to content

metrics: count leaf cache use and disk reads per file - #1007

Open
citizen-stig wants to merge 3 commits into
thrumdev:masterfrom
citizen-stig:nomt-io-metrics
Open

citizen-stig wants to merge 3 commits into
thrumdev:masterfrom
citizen-stig:nomt-io-metrics

Conversation

@citizen-stig

Copy link
Copy Markdown
Contributor

Problem

NOMT's metrics only describe the merkle page cache, and miss most of the disk traffic of a running node.

In a Sovereign SDK soak test, bpftrace showed ~21k 4 KiB reads/s from NOMT's io-worker threads, while Nomt::metrics() reported ~429M page requests, 0 page cache misses, and no page or value fetch times. All of these figures are consistent with the code:

  • Leaf reads are not counted anywhere. Merkle pages below an elided child are never requested from the page cache: the seek rebuilds them from beatree leaves. The seek also fetches a leaf whenever it ends at a leaf node. The beatree update of a commit, rollback, and value reads go through the leaf cache too. None of this touches a metric, and the leaf cache has no hit/miss counter.
  • PageFetchTime is never recorded. get_page_fetch_time() always returns None.
  • ValueFetchTime is only recorded in Session::read. Users that read values from elsewhere never see it.
  • PageCacheMisses is not a count of reads. A lookup that misses on a page whose load is in flight joins that load. In the new test, 2000 misses led to 65 hash-table reads.
  • Zero misses are plausible. Pages written by a commit go into the page cache, so a node started at genesis whose page cache never evicts can legitimately report zero misses. Meanwhile all of its reads land in the ln file.

Change

New counters, with getters on Metrics and lines in Metrics::print:

metric getter counted at covers
LeafCacheRequests get_leaf_cache_requests LeafCache::get every leaf lookup: seek, elided page rebuild, beatree update, rollback, value reads
LeafCacheMisses get_leaf_cache_misses LeafCache::get the misses among those lookups
HtReads get_ht_reads bitbox::PageLoader::probe every hash-table read, including probes and the reads made at open (root page, page cache prepopulation)
LnReads get_ln_reads beatree::allocator::StoreReader every leaf store read: leaves and overflow pages. StoreReader only wraps the leaf store; bbn reads at open do not go through it

PageFetchTime is now recorded. It measures the time from the first hash-table read of a page load until the page is found. Only loads started by the merkle seek after a page cache miss are timed. Loads at open are not timed: prepopulation queues tens of thousands of loads at once, and on benchtop's default 4M-account DB the mean became 90 ms.

HtReads and LnReads count reads where they are issued, so they line up with a per-file block:block_rq_issue count.

The Metrics handle is passed from Nomt::open through Store::open to the beatree (Tree::open, LeafCache, StoreReader, ops::update) and to bitbox (DB::open). With metrics disabled, each counting site is a branch on None.

Counters and timers are now striped per thread. Each thread adds to its own cache-line-aligned stripe, and the getters sum the stripes. Before, every counter shared a few cache lines that bounced between cores on every update. The public API is unchanged. The module docs now describe what collection costs.

Testing

  • nomt/tests/metrics.rs:
    1. Writes 20k accounts plus one 64 KiB value.
    2. Checks that a prepopulated open counts ht reads but records no fetch time.
    3. Reopens with 1 MiB caches and updates every tenth account without reading.
    4. Checks the page, leaf and disk-read counters. In particular, ln_reads >= leaf_cache_misses > 0 and the fetch time is recorded.
    5. Reads the 64 KiB value and checks that its overflow pages count towards LnReads.
  • A unit test counts from more threads than there are stripes and checks the sums.
  • cargo test -p nomt passes.

Performance

Setup: benchtop, transfer workload on a 4M-account DB, a fresh copy of the DB for every run. The runs were pinned to 6 cores (plus their SMT siblings) of a Ryzen 9 3950X, a machine shared with other work. There were 10 runs per variant, in shuffled order. t is Welch's t. Configs:

  • std: 20 sessions of 12k transfers after warm-up.
  • cold256: the first 5 sessions after open.
  • big: 10 sessions of 120k transfers.
comparison result
this PR vs master, both with metrics on No measurable change in Session::finish, wall time minus fsync, or read latency in any config (|t| < 2).
this PR vs master, both with metrics off No change.
metrics on vs off Only Session::read latency moves, on master as well. In big it rose 5–7% in one round (t ≈ 3.2) and 2–3% in another (t ≈ 1.5), on ~2 µs in-memory reads.
striped vs shared counters, metrics on Within noise end-to-end (read latency −0.8% in big, −2.8% in std, |t| ≤ 1.2). In a microbenchmark of 6 threads that each count a leaf lookup and time a read in a loop, an operation drops
from 254 ns to 59 ns.

The remaining cost of metrics on in-memory reads is mostly the two clock reads of the ValueFetchTime timer, about 40 ns per read.

citizen-stig and others added 3 commits November 26, 2026 10:36
The metrics only covered the merkle page cache. Merkle pages below an
elided child never reach the page cache: they are rebuilt from beatree
leaves. Leaf reads made by the seek, by the beatree update of a commit,
by rollback and by value reads were not counted anywhere. A node could
report zero page cache misses while issuing thousands of reads of the ln
file per second. PageFetchTime was defined but never recorded.

Add four counters:

- LeafCacheRequests and LeafCacheMisses, counted in LeafCache::get, which
  every leaf lookup goes through.
- HtReads, counted when PageLoader::probe submits a read of the hash-table.
- LnReads, counted in StoreReader, which only wraps the leaf store and is
  used for both leaves and overflow pages.

Record PageFetchTime in PageLoad, from the first hash-table read until
the page is found.

PageCacheMisses is not a count of reads: a lookup that misses on a page
whose load is in flight joins that load. On a cold reopen in the new
test, 2000 misses led to 65 ht reads. Say so in its docs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Page cache prepopulation queues a load for every page in the upper
levels at once. Timing those loads as page fetches recorded queueing
behind the whole batch: benchtop's default open of a 4M account DB read
53k pages and reported a 90 ms mean page fetch time. Since the timer is
a mean over the lifetime of the database, that skews it long after
startup.

Only time loads started by the merkle seek after a page cache miss. The
reads at open still count towards HtReads.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
All counters lived in one small struct, so every counter update from any
thread made the same cache lines bounce between cores. In a
microbenchmark of six threads that each count a leaf lookup and time a
read in a loop, an operation took 254 ns with shared atomics and 59 ns
with per-thread stripes; 40-50 ns of that is the two clock reads of the
timer.

Give every thread a cache-line-aligned stripe of all the counters, and
sum the stripes when reading.

In benchtop, in-memory value reads are 2-7% slower with metrics enabled
than without, on master as well, depending on the run. Striping does not
change that measurably: at ~2 us per read the clock reads dominate.
Document that cost.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@citizen-stig
citizen-stig marked this pull request as ready for review November 26, 2026 10:53
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.

1 participant