Repository navigation
metrics: count leaf cache use and disk reads per file - #1007
Open
citizen-stig wants to merge 3 commits into
Open
citizen-stig wants to merge 3 commits into
citizen-stig wants to merge 3 commits into
Conversation
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
marked this pull request as ready for review
November 26, 2026 10:53
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-workerthreads, whileNomt::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:PageFetchTimeis never recorded.get_page_fetch_time()always returnsNone.ValueFetchTimeis only recorded inSession::read. Users that read values from elsewhere never see it.PageCacheMissesis 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.lnfile.Change
New counters, with getters on
Metricsand lines inMetrics::print:LeafCacheRequestsget_leaf_cache_requestsLeafCache::getLeafCacheMissesget_leaf_cache_missesLeafCache::getHtReadsget_ht_readsbitbox::PageLoader::probeLnReadsget_ln_readsbeatree::allocator::StoreReaderStoreReaderonly wraps the leaf store; bbn reads at open do not go through itPageFetchTimeis 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.HtReadsandLnReadscount reads where they are issued, so they line up with a per-fileblock:block_rq_issuecount.The
Metricshandle is passed fromNomt::openthroughStore::opento the beatree (Tree::open,LeafCache,StoreReader,ops::update) and to bitbox (DB::open). With metrics disabled, each counting site is a branch onNone.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:htreads but records no fetch time.ln_reads >= leaf_cache_misses > 0and the fetch time is recorded.LnReads.cargo test -p nomtpasses.Performance
Setup: benchtop,
transferworkload 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.Session::finish, wall time minus fsync, or read latency in any config (|t| < 2).Session::readlatency moves, on master as well. Inbigit rose 5–7% in one round (t ≈ 3.2) and 2–3% in another (t ≈ 1.5), on ~2 µs in-memory reads.big, −2.8% instd, |t| ≤ 1.2). In a microbenchmark of 6 threads that each count a leaf lookup and time a read in a loop, an operation dropsThe remaining cost of metrics on in-memory reads is mostly the two clock reads of the
ValueFetchTimetimer, about 40 ns per read.