-
Notifications
You must be signed in to change notification settings - Fork 0
Beefing up the proc stats #5
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
jkbecker
wants to merge
4
commits into
main
Choose a base branch
from
proc-stats-galore
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| use anyhow::Result; | ||
| use regex::Regex; | ||
| use serde::{Deserialize, Serialize}; | ||
| use std::{collections::HashMap, fmt, fs, path::Path, str::FromStr}; | ||
|
|
||
| /// A system resource as identified in <https://docs.kernel.org/accounting/psi.html> for use | ||
| /// in pressure statistics collection. | ||
| #[derive(Serialize, Deserialize)] | ||
| pub enum SysResource { | ||
| Cpu, | ||
| Memory, | ||
| Io, | ||
| } | ||
| impl fmt::Display for SysResource { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| match self { | ||
| SysResource::Cpu => write!(f, "cpu"), | ||
| SysResource::Memory => write!(f, "memory"), | ||
| SysResource::Io => write!(f, "io"), | ||
| } | ||
| } | ||
| } | ||
| impl SysResource { | ||
| pub fn pressure_file(&self) -> &Path { | ||
| match self { | ||
| SysResource::Cpu => Path::new("/proc/pressure/cpu"), | ||
| SysResource::Memory => Path::new("/proc/pressure/memory"), | ||
| SysResource::Io => Path::new("/proc/pressure/io"), | ||
| } | ||
| } | ||
| } | ||
| #[derive(Serialize, Deserialize, Eq, Hash, PartialEq)] | ||
| pub enum SysPressureCategory { | ||
| Some, | ||
| Full, | ||
| } | ||
| #[derive(Debug)] | ||
| pub struct SysPressureParseError; | ||
|
|
||
| impl FromStr for SysPressureCategory { | ||
| type Err = SysPressureParseError; | ||
| fn from_str(input: &str) -> Result<SysPressureCategory, SysPressureParseError> { | ||
| match input { | ||
| "some" => Ok(SysPressureCategory::Some), | ||
| "full" => Ok(SysPressureCategory::Full), | ||
| _ => Err(SysPressureParseError), | ||
| } | ||
| } | ||
| } | ||
| impl fmt::Display for SysPressureCategory { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| match self { | ||
| SysPressureCategory::Full => write!(f, "full"), | ||
| SysPressureCategory::Some => write!(f, "some"), | ||
| } | ||
| } | ||
| } | ||
| #[derive(Serialize, Deserialize)] | ||
| pub struct SysPressureData { | ||
| avg10: f64, | ||
| avg60: f64, | ||
| avg300: f64, | ||
| total: u64, | ||
| } | ||
| /// This structure represents a ["Pressure Stall Information"](kernel-psi) reading against one | ||
| /// of the resources specified in [`crate::common::SysResource`](enum.SysResource.html). | ||
| /// | ||
| /// [kernel-psi]: https://docs.kernel.org/accounting/psi.html | ||
| #[derive(Serialize, Deserialize)] | ||
| pub struct SysPressure { | ||
| resource: SysResource, | ||
| pressure: HashMap<SysPressureCategory, SysPressureData>, | ||
| } | ||
|
|
||
| impl SysPressure { | ||
| pub fn try_from(resource: SysResource) -> Result<Self> { | ||
| //let mut path = PathBuf::from("/proc/pressure/"); | ||
| //path.push(resource.to_string()); | ||
| let pressure: HashMap<SysPressureCategory, SysPressureData> = | ||
| fs::read_to_string(resource.pressure_file()) | ||
| .expect("Failed to read {path}") | ||
| .trim() | ||
| .split('\n') | ||
| .filter_map(|line| { | ||
| // Example line: | ||
| // some avg10=0.04 avg60=0.08 avg300=0.12 total=10739245730 | ||
| let re = Regex::new( | ||
| r"(?x) | ||
| (some|full)+\s # $1 Should map to a SysPressureCategory | ||
| avg10=([0-9]*\.[0-9]+|[0-9]+)\s # $2 a f64 for the 10 second average | ||
| avg60=([0-9]*\.[0-9]+|[0-9]+)\s # $3 a f64 for the 60 second average | ||
| avg300=([0-9]*\.[0-9]+|[0-9]+)\s # $4 a f64 for the 300 second average | ||
| total=([0-9]+) # $5 a u64 for the total | ||
| ", | ||
| ) | ||
| .unwrap(); | ||
| let cap = re.captures(line); | ||
| match cap { | ||
| Some(c) => Some({ | ||
| let cat = | ||
| SysPressureCategory::from_str(c.get(1).map_or("", |m| m.as_str())) | ||
| .expect("Failed to read pressure category"); | ||
| let data = SysPressureData { | ||
| avg10: c | ||
| .get(2) | ||
| .map_or(0.00, |m| m.as_str().parse::<f64>().unwrap()), | ||
| avg60: c | ||
| .get(3) | ||
| .map_or(0.00, |m| m.as_str().parse::<f64>().unwrap()), | ||
| avg300: c | ||
| .get(4) | ||
| .map_or(0.00, |m| m.as_str().parse::<f64>().unwrap()), | ||
| total: c | ||
| .get(5) | ||
| .map_or(0_u64, |m| m.as_str().parse::<u64>().unwrap()), | ||
| }; | ||
| (cat, data) | ||
| }), | ||
| None => todo!(), | ||
| } | ||
| }) | ||
| .collect(); | ||
| assert_eq!(pressure.len(), 2); | ||
| Ok(SysPressure { resource, pressure }) | ||
| } | ||
| } | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| use std::{collections::HashMap, fs}; | ||
|
|
||
| use crate::common::{SysPressure, SysResource}; | ||
|
|
||
| pub fn cpu_info() -> HashMap<String, String> { | ||
| fs::read_to_string("/proc/cpuinfo") | ||
| .expect("Failed to read /proc/cpuinfo") | ||
| .trim() | ||
| .split('\n') | ||
| .filter_map(|line| { | ||
| // Example line: | ||
| // vendor_id : GenuineIntel | ||
| line.split_once(':') | ||
| .map(|(name, value)| (name.trim().to_string(), value.trim().to_string())) | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| pub fn cpu_pressure() -> SysPressure { | ||
| SysPressure::try_from(SysResource::Cpu).unwrap() | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| use crate::common::{SysPressure, SysResource}; | ||
|
|
||
| pub fn io_pressure() -> SysPressure { | ||
| SysPressure::try_from(SysResource::Io).unwrap() | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
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.
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.
All of these impls seem really verbose to me... is there a more elegant way to do this?
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 think there are some crates that could help (e.g. strum), but it seems ok as-is to me for now. That being said, I am not sure you need either of the
impl fmt::Displayblocks; nothing broke when I commented them out. 🤔