Skip to content

Commit

Permalink
Don't use '?' in for loops
Browse files Browse the repository at this point in the history
  • Loading branch information
nokyan committed Dec 15, 2024
1 parent 6935f67 commit 1514eee
Showing 1 changed file with 29 additions and 34 deletions.
63 changes: 29 additions & 34 deletions lib/process_data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,7 @@ impl ProcessData {
let mut seen_fds = HashSet::new();

let mut return_map = BTreeMap::new();
for entry in std::fs::read_dir(fdinfo_dir)? {
let entry = entry?;
for entry in std::fs::read_dir(fdinfo_dir)?.flatten() {
let fdinfo_path = entry.path();

let (plausible, fd_num) = Self::drm_fdinfo_plausible(&fdinfo_path, pid, &seen_fds);
Expand Down Expand Up @@ -526,8 +525,7 @@ impl ProcessData {
let mut seen_fds = HashSet::new();

let mut return_map = BTreeMap::new();
for entry in std::fs::read_dir(fdinfo_dir)? {
let entry = entry?;
for entry in std::fs::read_dir(fdinfo_dir)?.flatten() {
let fdinfo_path = entry.path();

let (plausible, fd_num) = Self::drm_fdinfo_plausible(&fdinfo_path, pid, &seen_fds);
Expand Down Expand Up @@ -558,44 +556,41 @@ impl ProcessData {
fn read_npu_fdinfo<P: AsRef<Path>>(fdinfo_path: P) -> Result<(PciSlot, NpuUsageStats)> {
let content = std::fs::read_to_string(fdinfo_path.as_ref())?;

let driver = RE_DRM_DRIVER
let pci_slot = RE_DRM_PDEV
.captures(&content)
.and_then(|captures| captures.get(1))
.map(|capture| capture.as_str());

if let Some(driver) = driver {
if !NPU_DRIVER_NAMES.contains(&driver) {
bail!("this is not an NPU")
}
.and_then(|capture| PciSlot::from_str(capture.as_str()).ok())
.context("can't parse PCI slot ID")?;

let pci_slot = RE_DRM_PDEV
.captures(&content)
.and_then(|captures| captures.get(1))
.and_then(|capture| PciSlot::from_str(capture.as_str()).ok())
.unwrap_or_default();
let driver = RE_DRM_DRIVER
.captures(&content)
.and_then(|captures| captures.get(1))
.map(|capture| capture.as_str())
.unwrap_or_default();

let usage = RE_DRM_ENGINE_NPU_AMDXDNA
.captures(&content)
.and_then(|captures| captures.get(1))
.and_then(|capture| capture.as_str().parse::<u64>().ok())
.unwrap_or_default();
if !NPU_DRIVER_NAMES.contains(&driver) {
bail!("this is not an NPU")
}

let total_memory = RE_DRM_TOTAL_MEMORY
.captures(&content)
.and_then(|captures| captures.get(1))
.and_then(|capture| capture.as_str().parse::<u64>().ok())
.unwrap_or_default()
.saturating_mul(1024);
let usage = RE_DRM_ENGINE_NPU_AMDXDNA
.captures(&content)
.and_then(|captures| captures.get(1))
.and_then(|capture| capture.as_str().parse::<u64>().ok())
.unwrap_or_default();

let stats = NpuUsageStats {
usage,
mem: total_memory,
};
let total_memory = RE_DRM_TOTAL_MEMORY
.captures(&content)
.and_then(|captures| captures.get(1))
.and_then(|capture| capture.as_str().parse::<u64>().ok())
.unwrap_or_default()
.saturating_mul(1024);

return Ok((pci_slot, stats));
}
let stats = NpuUsageStats {
usage,
mem: total_memory,
};

bail!("unable to find gpu information in this fdinfo");
return Ok((pci_slot, stats));
}

fn read_gpu_fdinfo<P: AsRef<Path>>(fdinfo_path: P) -> Result<(PciSlot, GpuUsageStats)> {
Expand Down

0 comments on commit 1514eee

Please sign in to comment.