alternative impl

This commit is contained in:
haoran 2022-11-30 16:29:22 +00:00 committed by HaoranYi
parent fc97d818b6
commit 2e078bb42c
2 changed files with 34 additions and 32 deletions

View File

@ -184,25 +184,34 @@ fn read_be_u64(input: &[u8]) -> u64 {
/// Utility function to get number of Mmap files for current process
pub fn get_mmap_count() -> Option<usize> {
use std::io::BufRead;
let pid = std::process::id();
let map_path = format!("/proc/{}/maps", pid);
let output = std::process::Command::new("wc")
.args(["-l", &map_path])
.output()
.unwrap();
if output.status.success() {
let n: usize = std::str::from_utf8(&output.stdout)
.unwrap()
.split_whitespace()
.next()
.unwrap()
.parse()
.unwrap();
let tmp_dir = tempfile::TempDir::new().ok()?;
let copy_path = tmp_dir.path().join("maps");
std::fs::copy(map_path, copy_path.as_os_str()).unwrap();
Some(n)
} else {
None
}
let file = std::fs::File::open(copy_path).ok()?;
Some(std::io::BufReader::new(file).lines().count())
// let output = std::process::Command::new("wc")
// .args(["-l", &map_path])
// .output()
// .unwrap();
// if output.status.success() {
// let n: usize = std::str::from_utf8(&output.stdout)
// .unwrap()
// .split_whitespace()
// .next()
// .unwrap()
// .parse()
// .unwrap();
//
// Some(n)
// } else {
// None
// }
}
/// Utility function to get open_fd stats

View File

@ -391,7 +391,7 @@ impl BucketStorage {
#[cfg(test)]
mod test {
use {super::*, procfs::process::LimitValue, tempfile::tempdir};
use {super::*, tempfile::tempdir};
#[test]
fn test_bucket_storage() {
@ -424,27 +424,20 @@ mod test {
assert_eq!(storage.uid(ix), None);
// test get_mmap_fd_stats
let (mmap_count, num_open_files, max_open_files_limit) =
BucketStorage::get_mmap_fd_stats().unwrap();
let (mmap_count, num_open_files, soft_limit, hard_limit) =
get_open_fd_stats().unwrap();
assert!(mmap_count > 0);
assert!(num_open_files > 0);
match max_open_files_limit.soft_limit {
LimitValue::Unlimited => {}
LimitValue::Value(x) => assert!(x > 0),
}
match max_open_files_limit.hard_limit {
LimitValue::Unlimited => {}
LimitValue::Value(x) => assert!(x > 0),
}
assert!(soft_limit > 0);
assert!(hard_limit > 0);
}
#[test]
fn test_time_mmap() {
use std::time::Instant;
let v = vec![];
let mut v = vec![];
for i in 1..1900000 {
if i % 100 == 0 {
println!("{}", i);
@ -453,7 +446,7 @@ mod test {
let tmpdir = tempdir().unwrap();
let paths: Vec<PathBuf> = vec![tmpdir.path().to_path_buf()];
assert!(!paths.is_empty());
let mut s =
let s =
BucketStorage::new(Arc::new(paths), 1, 1, 1, Arc::default(), Arc::default());
v.push(s);
}
@ -464,8 +457,8 @@ mod test {
let duration = start.elapsed();
println!(
"{} {} {:?}",
mmap_count, num_open_files, max_open_files_limit
"{} {} {} {}",
mmap_count, num_open_files, soft_limit, hard_limit
);
println!("Time elapsed is: {:?}", duration);