8000 vmstat: implement `--slabs` by Bluemangoo · Pull Request #460 · uutils/procps · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

vmstat: implement --slabs #460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/uu/slabtop/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use std::{
};

#[derive(Debug, Default)]
pub(crate) struct SlabInfo {
pub(crate) meta: Vec<String>,
pub(crate) data: Vec<(String, Vec<u64>)>,
pub struct SlabInfo {
pub meta: Vec<String>,
pub data: Vec<(String, Vec<u64>)>,
}

impl SlabInfo {
Expand Down
4 changes: 3 additions & 1 deletion src/uu/slabtop/src/slabtop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use crate::parse::SlabInfo;
pub use crate::parse::SlabInfo;
use clap::{arg, crate_version, ArgAction, Command};
use uucore::{error::UResult, format_usage, help_about, help_section, help_usage};

Expand All @@ -25,6 +25,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

let slabinfo = SlabInfo::new()?.sort(*sort_flag, false);

println!("{slabinfo:?}");

if matches.get_flag("once") {
output_header(&slabinfo);
println!();
Expand Down
2 changes: 2 additions & 0 deletions src/uu/vmstat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ clap = { workspace = true }
terminal_size = { workspace = true }
uucore = { workspace = true, features = ["custom-tz-fmt"] }

uu_slabtop = {path = "../slabtop"}

[lib]
path = "src/vmstat.rs"

Expand Down
50 changes: 44 additions & 6 deletions src/uu/vmstat/src/vmstat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

let one_header = matches.get_flag("one-header");
let no_first = matches.get_flag("no-first");
let term_height = terminal_size::terminal_size()
.map(|size| size.1 .0)
.unwrap_or(0);

if matches.get_flag("slabs") {
return print_slabs(one_header, term_height);
}

let delay = matches.get_one::<u64>("delay");
let count = matches.get_one::<u64>("count");
Expand All @@ -57,14 +64,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
line_count += 1;
}

let term_height = terminal_size::terminal_size()
.map(|size| size.1 .0)
.unwrap_or(0);

while count.is_none() || line_count < count.unwrap() {
std::thread::sleep(std::time::Duration::from_secs(delay));
let proc_data_now = ProcData::new();
if !one_header && term_height > 0 && ((line_count + 3) % term_height as u64 == 0) {
if needs_header(one_header, term_height, line_count) {
print_header(&pickers);
}
print_data(&pickers, &proc_data_now, Some(&proc_data), &matches);
Expand All @@ -76,6 +79,41 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Ok(())
}

#[cfg(target_os = "linux")]
fn print_slabs(one_header: bool, term_height: u16) -> UResult<()> {
let mut slab_data = uu_slabtop::SlabInfo::new()?.data;

slab_data.sort_by_key(|k| k.0.to_lowercase());

print_slab_header();

for (line_count, slab_item) in slab_data.into_iter().enumerate() {
if needs_header(one_header, term_height, line_count as u64) {
print_slab_header();
}

println!(
"{:<24} {:>6} {:>6} {:>6} {:>6}",
slab_item.0, slab_item.1[0], slab_item.1[1], slab_item.1[2], slab_item.1[3]
);
}

Ok(())
}

#[cfg(target_os = "linux")]
fn needs_header(one_header: bool, term_height: u16, line_count: u64) -> bool {
!one_header && term_height > 0 && ((line_count + 3) % term_height as u64 == 0)
}

#[cfg(target_os = "linux")]
fn print_slab_header() {
println!(
"{:<24} {:>6} {:>6} {:>6} {:>6}",
"Cache", "Num", "Total", "Size", "Pages"
);
}

#[cfg(target_os = "linux")]
fn print_header(pickers: &[Picker]) {
let mut section: Vec<&str> = vec![];
Expand Down Expand Up @@ -126,7 +164,7 @@ pub fn uu_app() -> Command {
.value_parser(value_parser!(u64)),
arg!(-a --active "Display active and inactive memory"),
// arg!(-f --forks "switch displays the number of forks since boot"),
// arg!(-m --slabs "Display slabinfo"),
arg!(-m --slabs "Display slabinfo"),
arg!(-n --"one-header" "Display the header only once rather than periodically"),
// arg!(-s --stats "Displays a table of various event counters and memory statistics"),
// arg!(-d --disk "Report disk statistics"),
Expand Down
Loading
0