11use std:: { sync, thread, time} ;
22
3- use ckb_logger:: { debug, error, info} ;
3+ use ckb_logger:: { error, info} ;
4+ use ckb_metrics:: metrics;
45use futures:: executor:: block_on;
56use heim:: units:: information:: byte;
67use jemalloc_ctl:: { epoch, stats} ;
78
8- use crate :: { rocksdb:: TrackRocksDBMemory , utils :: HumanReadableSize } ;
9+ use crate :: rocksdb:: TrackRocksDBMemory ;
910
1011macro_rules! je_mib {
1112 ( $key: ty) => {
@@ -21,7 +22,7 @@ macro_rules! je_mib {
2122macro_rules! mib_read {
2223 ( $mib: ident) => {
2324 if let Ok ( value) = $mib. read( ) {
24- HumanReadableSize :: from ( value as u64 )
25+ value as i64
2526 } else {
2627 error!( "failed to read jemalloc stats for {}" , stringify!( $mib) ) ;
2728 return ;
@@ -58,17 +59,19 @@ pub fn track_current_process<Tracker: 'static + TrackRocksDBMemory + Sync + Send
5859 . name ( "MemoryTracker" . to_string ( ) )
5960 . spawn ( move || {
6061 if let Ok ( process) = block_on ( heim:: process:: current ( ) ) {
61- let pid = process. pid ( ) ;
6262 loop {
6363 if je_epoch. advance ( ) . is_err ( ) {
6464 error ! ( "failed to refresh the jemalloc stats" ) ;
6565 return ;
6666 }
6767 if let Ok ( memory) = block_on ( process. memory ( ) ) {
6868 // Resident set size, amount of non-swapped physical memory.
69- let rss: HumanReadableSize = memory. rss ( ) . get :: < byte > ( ) . into ( ) ;
69+ let rss = memory. rss ( ) . get :: < byte > ( ) as i64 ;
7070 // Virtual memory size, total amount of memory.
71- let virt: HumanReadableSize = memory. vms ( ) . get :: < byte > ( ) . into ( ) ;
71+ let vms = memory. vms ( ) . get :: < byte > ( ) as i64 ;
72+
73+ metrics ! ( gauge, "ckb-sys.mem.process" , rss, "type" => "rss" ) ;
74+ metrics ! ( gauge, "ckb-sys.mem.process" , vms, "type" => "vms" ) ;
7275
7376 let allocated = mib_read ! ( allocated) ;
7477 let resident = mib_read ! ( resident) ;
@@ -77,56 +80,15 @@ pub fn track_current_process<Tracker: 'static + TrackRocksDBMemory + Sync + Send
7780 let retained = mib_read ! ( retained) ;
7881 let metadata = mib_read ! ( metadata) ;
7982
83+ metrics ! ( gauge, "ckb-sys.mem.jemalloc" , allocated, "type" => "allocated" ) ;
84+ metrics ! ( gauge, "ckb-sys.mem.jemalloc" , resident, "type" => "resident" ) ;
85+ metrics ! ( gauge, "ckb-sys.mem.jemalloc" , active, "type" => "active" ) ;
86+ metrics ! ( gauge, "ckb-sys.mem.jemalloc" , mapped, "type" => "mapped" ) ;
87+ metrics ! ( gauge, "ckb-sys.mem.jemalloc" , retained, "type" => "retained" ) ;
88+ metrics ! ( gauge, "ckb-sys.mem.jemalloc" , metadata, "type" => "metadata" ) ;
89+
8090 if let Some ( tracker) = tracker_opt. clone ( ) {
81- let stats = tracker. gather_memory_stats ( ) ;
82- debug ! (
83- "CurrentProcess {{ \
84- pid: {}, rss: {}, virt: {}, \
85- Jemalloc: {{ \
86- allocated: {}, resident: {}, \
87- active: {}, mapped: {}, retained: {}, \
88- metadata: {} }}, \
89- RocksDB: {{ \
90- total: {}, cache: {}, readers: {}, \
91- memtables: {}, pinned: {}, \
92- cache-capacity: {} \
93- }} \
94- }}",
95- pid,
96- rss,
97- virt,
98- allocated,
99- resident,
100- active,
101- mapped,
102- retained,
103- metadata,
104- stats. total_memory,
105- stats. block_cache_usage,
106- stats. estimate_table_readers_mem,
107- stats. cur_size_all_mem_tables,
108- stats. block_cache_pinned_usage,
109- stats. block_cache_capacity,
110- ) ;
111- } else {
112- debug ! (
113- "CurrentProcess {{ \
114- pid: {}, rss: {}, virt: {}, \
115- Jemalloc: {{ \
116- allocated: {}, resident: {}, \
117- active: {}, mapped: {}, retained: {}, \
118- metadata: {} }} \
119- }}",
120- pid,
121- rss,
122- virt,
123- allocated,
124- resident,
125- active,
126- mapped,
127- retained,
128- metadata,
129- ) ;
91+ let _ignored = tracker. gather_memory_stats ( ) ;
13092 }
13193 } else {
13294 error ! ( "failed to fetch the memory information about current process" ) ;
0 commit comments