Fork-based Per-type Object Memory Profiling#9
Closed
artikell wants to merge 1 commit into
Closed
Conversation
dae4e3f to
3e59fc0
Compare
b9bd312 to
d5f549d
Compare
Introduce a new BGSAVE subcommand `STAT_MEMORY` that collects per-type object memory statistics and a category breakdown during RDB save. The implementation uses a file-static function pointer callback in rdb.c, gated by the new RDBFLAGS_STAT_MEMORY bit passed through the rdbflags chain. Statistics collected in the forked child process: - Per-type (string/list/set/zset/hash/stream): key count and exact bytes - KV data: user data, expiration kvstore, hash field-level TTL metadata - User metadata: kvstore overhead, client I/O buffers - System: AOF buffer, replication buffer/backlog Results are reported via childinfo pipe and logged at LL_NOTICE level after BGSAVE completes. All measurement runs on the COW snapshot with zero impact on main-thread throughput.
d5f549d to
8ae546b
Compare
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.
The problem/use-case that the feature addresses
Production operators currently lack a precise, low-overhead mechanism to understand the memory composition of a Valkey instance at the object level. The existing toolset falls short:
INFO memoryonly exposes aggregate metrics (used_memory,used_memory_rss,mem_fragmentation_ratio) with no breakdown by data type (string/list/set/zset/hash/stream).MEMORY USAGE <key>operates per-key. For instances with hundreds of millions of keys, scanning them all is prohibitively expensive — it blocks the main thread and has O(N) complexity.INFOsections. There is no unified "memory profile" captured atomically in a single snapshot.MEMORY STATSprovides only coarse-grained overhead categories (dataset.percentage,overhead.total) without per-type exact allocation sizes.Typical operational scenarios that remain difficult:
Description of the feature
Leverage the COW snapshot already created by
fork()during BGSAVE to collect exact per-key memory statistics alongside RDB serialization at near-zero additional cost, then report a structured summary back to the parent process.New subcommand:
This is mutually exclusive with
SCHEDULEandCANCEL. When issued, the forked child process collects memory statistics during the normal RDB serialization pass and transmits them back via the existingchildinfopipe.Statistics collected:
count[type]/bytes[type]mem_kv_user_datamem_kv_expirationmem_kv_hash_metadatamem_um_kvstoremem_um_clients_iomem_sys_aof_buffermem_sys_repl_buffermem_sys_repl_backlogOutput format (LL_NOTICE log lines after BGSAVE completes):
Implementation approach:
rdb_key_mem_stat_fn) inrdb.cserves as the statistics callback, registered only whenRDBFLAGS_STAT_MEMORYis set in the rdbflags bitmask.rdbSaveObject()invokes the callback with(obj_type, mem_used, count)per key usingzmalloc_size()for exact allocator-level measurements.CHILD_INFO_TYPE_RDB_OBJECT_STATSover the childinfo pipe.Alternatives you've considered
SCAN+MEMORY USAGEloopDEBUG OBJECT+ samplingDEBUGcommands are risky in productionMEMORY STATS/MEMORY DOCTORThe proposed approach reuses the existing BGSAVE fork + full-scan path, achieving exact statistics at near-zero additional cost (~10% child process overhead measured on 10M+ simple string keys; lower with complex types due to I/O dominance).
Additional information
BGSAVE STAT_MEMORY). NormalBGSAVE, scheduled saves, and replication-triggered saves have zero overhead.zmalloc_size()(exact allocator-level size), reflecting true RSS contribution.INFO rdb_statssection for monitoring systems to scrape (current implementation is log-only).