Skip to content
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

Implement events command for cgroup v2 stats #191

Merged
merged 14 commits into from
Aug 7, 2021
Merged

Conversation

Furisto
Copy link
Collaborator

@Furisto Furisto commented Aug 6, 2021

This is the followup to #171, this time for cgroup v2 stats.

Comment on lines 268 to 373
}

let file_name = hugetlb_entry.file_name();
let file_name = file_name.to_str().unwrap();
if let Some(name_stripped) = file_name.strip_prefix("hugepages-") {
if let Some(size) = name_stripped.strip_suffix("kB") {
let size: u64 = size.parse()?;

let size_moniker = if size >= (1 << 20) {
(size >> 20).to_string() + "GB"
} else if size >= (1 << 10) {
(size >> 10).to_string() + "MB"
} else {
size.to_string() + "KB"
};

sizes.push(size_moniker);
}
}
}

Ok(sizes)
}

pub fn parse_value(value: &str) -> Result<u64> {
value
.parse()
.with_context(|| format!("failed to parse {}", value))
}

pub fn parse_single_value(file_path: &Path) -> Result<u64> {
let value = common::read_cgroup_file(file_path)?;
let value = value.trim();
if value == "max" {
return Ok(u64::MAX);
}

value.parse().with_context(|| {
format!(
"failed to parse value {} from {}",
value,
file_path.display()
)
})
}

pub fn parse_flat_keyed_data(file_path: &Path) -> Result<HashMap<String, u64>> {
let mut stats = HashMap::new();
let keyed_data = common::read_cgroup_file(file_path)?;
for entry in keyed_data.lines() {
let entry_fields: Vec<&str> = entry.split_ascii_whitespace().collect();
if entry_fields.len() != 2 {
continue;
}

stats.insert(
entry_fields[0].to_owned(),
entry_fields[1].parse().with_context(|| {
format!(
"failed to parse value {} from {}",
entry_fields[0],
file_path.display()
)
})?,
);
}

Ok(stats)
}

pub fn parse_nested_keyed_data(file_path: &Path) -> Result<HashMap<String, Vec<String>>> {
let mut stats: HashMap<String, Vec<String>> = HashMap::new();
let keyed_data = common::read_cgroup_file(file_path)?;
for entry in keyed_data.lines() {
let entry_fields: Vec<&str> = entry.split_ascii_whitespace().collect();
if entry_fields.len() < 2 {
continue;
}

stats.insert(
entry_fields[0].to_owned(),
entry_fields[1..]
.iter()
.copied()
.map(|p| p.to_owned())
.collect(),
);
}

Ok(stats)
}

pub fn parse_device_number(device: &str) -> Result<(u64, u64)> {
let numbers: Vec<&str> = device.split_terminator(':').collect();
if numbers.len() != 2 {
bail!("failed to parse device number {}", device);
}

Ok((numbers[0].parse()?, numbers[1].parse()?))
}
Copy link
Member

@utam0k utam0k Aug 6, 2021

Choose a reason for hiding this comment

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

I want to ensure that the functions in this area that process strings are safe enough to include cases of failure by testing.

@Furisto
Copy link
Collaborator Author

Furisto commented Aug 6, 2021

@utam0k PTAL

Copy link
Member

@utam0k utam0k left a comment

Choose a reason for hiding this comment

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

great!

@utam0k utam0k merged commit 21a7453 into youki-dev:main Aug 7, 2021
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