Skip to content

[mpt] expose triedb counters via FFI - #2552

Merged
maxkozlovsky merged 3 commits into
mainfrom
max/triedb-stats-export
Sep 16, 2026
Merged

maxkozlovsky merged 3 commits into
mainfrom
max/triedb-stats-export

Conversation

@maxkozlovsky

Copy link
Copy Markdown
Contributor

Currently the only way to get the stats from execution is reading and parsing the logs, which is cumbersome to automate.

Add an optional small shared memory file that execution publishes the stats to on every upsert and monad-node process reads and exports them as prometheus metrics.

Add FFI interface to read the shared memory from Rust code.

The shared memory file name is passed as command line argument and needs to be agreed on between the two processes.

Copilot AI lite review requested due to automatic review settings September 10, 2026 17:24

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.

🟡 Changes recommended

The new C ABI entrypoint can unwind exceptions across extern "C" and the sidecar creation path can overwrite an existing empty file, both of which should be fixed before merging.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR adds a cross-process “stats sidecar” file for TrieDB lifetime update counters and exposes a reader via the existing C FFI so Rust (monad-node) can scrape and export metrics without parsing logs.

Changes:

  • Add DbStatsPublisher/DbStatsReader implementation backed by an mmap’d sidecar file with a seqlock snapshot protocol.
  • Wire publishing into trie upsert/update paths and plumb a new --db-stats-file CLI option into the on-disk DB config.
  • Add Rust-side TriedbStatsReader wrapper + tests and extend the Rust/C++ FFI surface for reading update counters.

Verdict: NEEDS CHANGES

File summaries
File Description
rust/crates/monad-triedb/src/lib.rs Add Rust TriedbStatsReader + UpdateStats struct and tests for sidecar reading semantics.
rust/crates/monad-triedb/src/ffi.rs Re-export new FFI bindings for stats reader and update counters.
rust/crates/monad-triedb/src/ffi.cpp Implement FFI open/close/read functions backed by monad::mpt::DbStatsReader.
rust/crates/monad-triedb/include/ffi.h Extend C ABI with stats reader handle + triedb_update_stats.
cmd/monad/main.cpp Add --db-stats-file option and pass it into OnDiskDbConfig.
category/mpt/update_aux.cpp Publish lifetime update counters to the sidecar after each upsert.
category/mpt/trie.hpp Add DbStatsPublisher* hook into UpdateAux to enable publishing.
category/mpt/test/db_test.cpp Add on-disk DB tests asserting sidecar publishing and non-fatal sidecar failures.
category/mpt/test/db_stats_shm_test.cpp New unit tests for the sidecar seqlock layout and takeover semantics.
category/mpt/test/compaction_test.cpp Add compaction fixture coverage for publishing on non-compaction upserts.
category/mpt/test/CMakeLists.txt Register new db_stats_shm_test target.
category/mpt/ondisk_db_config.hpp Add stats_file_path option to configure sidecar publishing.
category/mpt/db.cpp Create publisher from config and attach it to UpdateAux lifetime-safely.
category/mpt/db_stats_shm.hpp Define sidecar layout and publisher/reader APIs.
category/mpt/db_stats_shm.cpp Implement mmap + seqlock read/write and exclusive-writer locking.
category/mpt/CMakeLists.txt Add db_stats_shm.{hpp,cpp} to the mpt library build.

🤖 Generated with Claude Code

Review details
  • Files reviewed: 16/16 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread category/mpt/db_stats_shm.cpp
Comment thread rust/crates/monad-triedb/src/ffi.cpp
Comment thread rust/crates/monad-triedb/src/ffi.cpp Outdated
@maxkozlovsky
maxkozlovsky force-pushed the max/triedb-stats-export branch from 46e6bf0 to d3bbb3e Compare September 10, 2026 18:46
maxkozlovsky and others added 2 commits September 15, 2026 12:15
UpdateAux keeps lifetime totals for the trie update and compaction counters,
but they live in the writing process's memory. A metrics scraper in another
process -- monad-node, which owns the only Prometheus endpoint -- cannot see
them at all: its own read-only handle never upserts, so every counter it can
reach reads zero.

Add an optional sidecar, a small file the db maps shared and writes after
every upsert, guarded by a seqlock so a reader never observes a half-written
snapshot. --db-stats-file turns it on; without it nothing is mapped and
nothing is published. Sections after the header are append-only and
payload_size says how many the writer wrote, so a section a writer predates
reads back as absent rather than as a version mismatch.

What is published is the writer's lifetime totals, which restart at zero when
the process does. Consumers that compute rates already treat a decrease as a
counter reset.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Add triedb_stats_open / triedb_update_stats_read -> TriedbStatsReader, so
monad-node can publish the counters an upserting db writes into its statistics
sidecar as Prometheus metrics.

The reader is its own handle rather than a method on TriedbRoInner, mirroring
what it reads: the sidecar is a separate path with its own lifetime, and it is
absent unless the writer was started with --db-stats-file. Reading returns a
bool instead of using a zero sentinel the way triedb_storage_stats_read can,
because a writer that has not upserted yet legitimately reports all zeros.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@maxkozlovsky
maxkozlovsky force-pushed the max/triedb-stats-export branch from d3bbb3e to b427ef2 Compare September 15, 2026 16:20
@maxkozlovsky
maxkozlovsky merged commit a1e7a39 into main Sep 16, 2026
15 checks passed
@maxkozlovsky
maxkozlovsky deleted the max/triedb-stats-export branch September 16, 2026 22:32
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.

4 participants