-
Notifications
You must be signed in to change notification settings - Fork 346
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
utam0k
reviewed
Aug 6, 2021
src/cgroups/stats.rs
Outdated
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()?)) | ||
} |
There was a problem hiding this comment.
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.
@utam0k PTAL |
utam0k
approved these changes
Aug 7, 2021
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is the followup to #171, this time for cgroup v2 stats.