Skip to content

hugetop: add hugetop command#657

Open
Franklin-Qi wants to merge 2 commits intouutils:mainfrom
Franklin-Qi:feature-#392-add-hugetop-command
Open

hugetop: add hugetop command#657
Franklin-Qi wants to merge 2 commits intouutils:mainfrom
Franklin-Qi:feature-#392-add-hugetop-command

Conversation

@Franklin-Qi
Copy link
Copy Markdown
Contributor

@Franklin-Qi Franklin-Qi commented Mar 26, 2026

The hugetop command displays system-wide hugepage informantion as well as per-process hugepage information.

$ cargo run -q hugetop --help
Report hugepage usage of processes and the system as a whole

Usage: hugetop [OPTIONS]

Options:
  -d, --delay <SECONDS>  Delay between updates (0 = run once)
  -n, --numa             Display per NUMA node huge page information
  -o, --once             Only display once, then exit
  -H, --human            Display human-readable output
  -l, --lines <N>        Show the top N processes
  -h, --help             Print help

Add feature: #244

@Franklin-Qi Franklin-Qi changed the title add hugetop command hugetop: add hugetop command Mar 26, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 26, 2026

Codecov Report

❌ Patch coverage is 3.30882% with 263 lines in your changes missing coverage. Please review.
✅ Project coverage is 18.36%. Comparing base (6ac0e01) to head (24384de).
⚠️ Report is 23 commits behind head on main.

Files with missing lines Patch % Lines
src/uu/hugetop/src/hugetop.rs 0.00% 263 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #657      +/-   ##
==========================================
- Coverage   18.77%   18.36%   -0.41%     
==========================================
  Files          66       68       +2     
  Lines        9752    10017     +265     
  Branches      516      544      +28     
==========================================
+ Hits         1831     1840       +9     
- Misses       7921     8177     +256     
Flag Coverage Δ
macos_latest 6.78% <3.30%> (-0.14%) ⬇️
ubuntu_latest 18.71% <3.30%> (-0.43%) ⬇️
windows_latest 0.02% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

.ok()
}

fn parse_smaps_rollup(path: &Path) -> Option<(u64, u64, u64)> {
Copy link
Copy Markdown
Collaborator

@Krysztal112233 Krysztal112233 Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think take a name for (u64,u64,u64) of Option<(u64,u64,u64)> would be better

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if possible please add document (I mean kernel doc) reference to here would be better

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map.into_values().collect()
}

fn read_hugepage_pools(root: &Path) -> UResult<Vec<HugePagePool>> {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For use case such as root: &Path, you can rewrite it into root: impl AsRef<Path>

Ok(pools)
}

fn read_process_hugepage_usage(root: &Path) -> UResult<Vec<ProcessHugeUsage>> {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

number.parse::<u64>().ok()
}

fn read_u64(path: impl AsRef<Path>) -> u64 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +413 to +456
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;

#[test]
fn parse_hugepage_name_works() {
assert_eq!(parse_hugepage_dir_name("hugepages-2048kB"), Some(2048));
assert_eq!(
parse_hugepage_dir_name("hugepages-1048576kB"),
Some(1_048_576)
);
assert_eq!(parse_hugepage_dir_name("hugepages-foo"), None);
}

#[test]
fn parse_smaps_rollup_works() {
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("smaps_rollup");
let mut f = fs::File::create(&file).unwrap();
writeln!(f, "AnonHugePages: 512 kB").unwrap();
writeln!(f, "Shared_Hugetlb: 64 kB").unwrap();
writeln!(f, "Private_Hugetlb: 32 kB").unwrap();

assert_eq!(parse_smaps_rollup(&file), Some((512, 64, 32)));
}

#[test]
fn reads_pools_from_tree() {
let dir = tempfile::tempdir().unwrap();
let pool = dir.path().join("hugepages-2048kB");
fs::create_dir(&pool).unwrap();
fs::write(pool.join("nr_hugepages"), "10\n").unwrap();
fs::write(pool.join("free_hugepages"), "3\n").unwrap();
fs::write(pool.join("resv_hugepages"), "2\n").unwrap();
fs::write(pool.join("surplus_hugepages"), "1\n").unwrap();

let pools = read_hugepage_pools(dir.path()).unwrap();
assert_eq!(pools.len(), 1);
assert_eq!(pools[0].size_kb, 2048);
assert_eq!(pools[0].total_pages, 10);
assert_eq!(pools[0].free_pages, 3);
}
}
Copy link
Copy Markdown
Collaborator

@Krysztal112233 Krysztal112233 Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those tests only works on Linux I think, you can add #[cfg(target_os = "linux")]

Comment on lines +14 to +21
fn supports_lines_option() {
new_ucmd!().arg("-l").arg("1").succeeds();
}

#[test]
fn supports_numa_option() {
new_ucmd!().arg("-n").succeeds();
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants