1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! Track the memory usage of CKB.

#[cfg(all(
    not(target_env = "msvc"),
    not(target_os = "macos"),
    feature = "profiling"
))]
mod jemalloc;
#[cfg(not(all(
    not(target_env = "msvc"),
    not(target_os = "macos"),
    feature = "profiling"
)))]
mod jemalloc {
    /// A dummy function which is used when the Jemalloc profiling isn't supported.
    ///
    /// Jemalloc profiling is disabled in default, the feature `profiling` is used to enable it.
    pub fn jemalloc_profiling_dump(_: &str) -> Result<(), String> {
        Err("jemalloc profiling dump: unsupported".to_string())
    }
}

#[cfg(all(not(target_env = "msvc"), not(target_os = "macos")))]
mod process;
#[cfg(not(all(not(target_env = "msvc"), not(target_os = "macos"))))]
mod process {
    use std::sync;

    use crate::rocksdb::TrackRocksDBMemory;
    use ckb_logger::info;

    /// A dummy function which is used when tracking memory usage isn't supported.
    pub fn track_current_process<Tracker: 'static + TrackRocksDBMemory + Sync + Send>(
        _: u64,
        _: Option<sync::Arc<Tracker>>,
    ) {
        info!("track current process: unsupported");
    }
}
mod rocksdb;

pub use jemalloc::jemalloc_profiling_dump;
pub use process::track_current_process;
pub use rocksdb::TrackRocksDBMemory;

/// Track the memory usage of the CKB process and Jemalloc.
pub fn track_current_process_simple(interval: u64) {
    track_current_process::<rocksdb::DummyRocksDB>(interval, None);
}