app_memory_usage_fetcher/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2
3#![doc = include_str!("../readme.md")]
4#![deny(missing_docs)]
5
6use std::num::NonZeroU64;
7
8/// Library version
9pub const APP_MEMORY_USAGE_FETCHER_VERSION: &str = env!("CARGO_PKG_VERSION");
10
11extern "C" {
12    fn getMemoryUsage() -> i64;
13}
14
15/// Application memory usage in bytes
16/// ```
17///  assert!(app_memory_usage_fetcher::get_memory_usage_bytes().is_some());
18/// ```
19#[inline]
20pub fn get_memory_usage_bytes() -> Option<NonZeroU64> {
21    let bytes = unsafe { getMemoryUsage() };
22    if bytes <= 0 {
23        None
24    } else {
25        NonZeroU64::new(bytes as u64)
26    }
27}
28
29/// Application memory usage in kilobytes
30#[inline]
31pub fn get_memory_usage_kbytes() -> Option<f64> {
32    get_memory_usage_bytes().map(|m| m.get() as f64 / 1024.0f64)
33}
34
35/// Application memory usage in megabytes
36#[inline]
37pub fn get_memory_usage_mbytes() -> Option<f64> {
38    const MEGABYTE: f64 = 1024.0f64 * 1024.0f64;
39    get_memory_usage_bytes().map(|m| m.get() as f64 / MEGABYTE)
40}
41
42/// Application memory usage in gigabytes
43#[inline]
44pub fn get_memory_usage_gbytes() -> Option<f64> {
45    const GIGABYTE: f64 = 1024.0f64 * 1024.0f64 * 1024.0f64;
46    get_memory_usage_bytes().map(|m| m.get() as f64 / GIGABYTE)
47}
48
49/// Application memory usage in terabytes
50#[inline]
51pub fn get_memory_usage_tbytes() -> Option<f64> {
52    const TERABYTE: f64 = 1024.0f64 * 1024.0f64 * 1024.0f64 * 1024.0f64;
53    get_memory_usage_bytes().map(|m| m.get() as f64 / TERABYTE)
54}