Skip to content

Commit 3b2d527

Browse files
heymindzhangli-pearheymindldm0
authored
feat(memory): android & linux support (#4)
* memory: andorid & linux support * Fix memory info on android&linux * Fix ios compilation --------- Co-authored-by: zhangli.pear <[email protected]> Co-authored-by: heymind <[email protected]> Co-authored-by: Liu Dingming <[email protected]>
1 parent 02b62dd commit 3b2d527

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ keywords = ["perf", "statistics", "monitor", "performance"]
1414

1515

1616
[features]
17-
alloc_hook_prof = ["alloc_hook"]
18-
alloc_hook_switchable = ["alloc_hook"]
19-
alloc_hook = []
17+
allocation_counter = []
2018
darwin_private = []
2119

2220
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

src/fd/android_linux.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,30 @@ mod test {
1414

1515
#[test]
1616
fn test_fd_count() {
17-
let mut buf = vec![];
17+
#[cfg(target_os = "linux")]
18+
const TEMP_DIR: &str = "/tmp";
19+
#[cfg(target_os = "android")]
20+
const TEMP_DIR: &str = "/data/local/tmp";
21+
1822
const NUM: usize = 100;
1923

2024
// open some files and do not close them.
21-
for i in 0..NUM {
22-
let fname = format!("/tmp/tmpfile{}", i);
23-
let file = std::fs::OpenOptions::new()
24-
.write(true)
25-
.create(true)
26-
.open(fname);
27-
buf.push(file);
28-
}
25+
let fds: Vec<_> = (0..NUM)
26+
.map(|i| {
27+
let fname = format!("{}/tmpfile{}", TEMP_DIR, i);
28+
std::fs::File::create(fname).unwrap()
29+
})
30+
.collect();
2931
let count = fd_count_cur().unwrap();
3032

31-
assert!(NUM + 3 <= count);
33+
dbg!(count);
34+
assert!(count >= NUM);
35+
let old_count = count;
3236

33-
// close files
34-
while let Some(_) = buf.pop() {}
37+
drop(fds);
3538
let count = fd_count_cur().unwrap();
36-
assert!(3 <= count);
37-
assert!(NUM > count);
39+
// Though tests are run in multi-thread mode without using nextest, we
40+
// assume NUM is big enough to make fd count lower in a short period.
41+
assert!(count < old_count);
3842
}
3943
}

src/mem/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ mod allocation_counter;
1313

1414
pub use allocation_counter::CountingAllocator;
1515

16-
#[cfg(any(target_os = "macos", target_os = "windows"))]
1716
mod process_memory_info;
18-
#[cfg(any(target_os = "macos", target_os = "windows"))]
1917
pub use process_memory_info::{get_process_memory_info, ProcessMemoryInfo};
2018

2119
#[cfg(target_os = "macos")]

src/mem/process_memory_info.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub struct ProcessMemoryInfo {
99
/// On Windows this is an alias for wset field and it matches "Mem Usage"
1010
/// column of taskmgr.exe.
1111
pub resident_set_size: u64,
12+
#[cfg(not(any(target_os = "android", target_os = "linux")))]
13+
#[cfg_attr(doc, doc(cfg(not(linux))))]
1214
pub resident_set_size_peak: u64,
1315

1416
/// this is the total amount of virtual memory used by the process.
@@ -33,10 +35,12 @@ pub struct ProcessMemoryInfo {
3335
/// + page_table
3436
///
3537
/// details: <https://github.com/apple/darwin-xnu/blob/master/osfmk/kern/task.c>
36-
#[cfg(target_os = "macos")]
38+
#[cfg(any(target_os = "macos", target_os = "ios"))]
39+
#[cfg_attr(doc, doc(macos))]
3740
pub phys_footprint: u64,
3841

39-
#[cfg(target_os = "macos")]
42+
#[cfg(any(target_os = "macos", target_os = "ios"))]
43+
#[cfg_attr(doc, doc(macos))]
4044
pub compressed: u64,
4145
}
4246

@@ -68,7 +72,24 @@ fn get_process_memory_info_impl() -> Result<ProcessMemoryInfo> {
6872
})
6973
}
7074

71-
#[cfg(target_os = "macos")]
75+
#[cfg(any(target_os = "linux", target_os = "android"))]
76+
fn get_process_memory_info_impl() -> Result<ProcessMemoryInfo> {
77+
// https://www.kernel.org/doc/Documentation/filesystems/proc.txt
78+
let statm = std::fs::read_to_string("/proc/self/statm")?;
79+
let mut parts = statm.split(' ');
80+
let Some(virtual_memory_size) = parts.next().and_then(|s| s.parse().ok()) else {
81+
return Err(Error::new(std::io::ErrorKind::Other, "Invalid VmSize in /proc/self/statm"));
82+
};
83+
let Some(resident_set_size) = parts.next().and_then(|s| s.parse().ok()) else {
84+
return Err(Error::new(std::io::ErrorKind::Other, "Invalid VmRSS in /proc/self/statm"));
85+
};
86+
Ok(ProcessMemoryInfo {
87+
virtual_memory_size,
88+
resident_set_size,
89+
})
90+
}
91+
92+
#[cfg(any(target_os = "macos", target_os = "ios"))]
7293
fn get_process_memory_info_impl() -> Result<ProcessMemoryInfo> {
7394
use crate::bindings::task_vm_info;
7495
use mach::{

0 commit comments

Comments
 (0)