[mpt] expose triedb counters via FFI - #2552
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
🟡 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/DbStatsReaderimplementation 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-fileCLI option into the on-disk DB config. - Add Rust-side
TriedbStatsReaderwrapper + 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.
maxkozlovsky
force-pushed
the
max/triedb-stats-export
branch
from
September 10, 2026 18:46
46e6bf0 to
d3bbb3e
Compare
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
force-pushed
the
max/triedb-stats-export
branch
from
September 15, 2026 16:20
d3bbb3e to
b427ef2
Compare
Chen-Yifan
approved these changes
Sep 15, 2026
Akatsukis
approved these changes
Sep 16, 2026
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.
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.