procfs_core/
iomem.rs

1#[cfg(feature = "serde1")]
2use serde::{Deserialize, Serialize};
3use std::io::BufRead;
4
5use super::ProcResult;
6use crate::{process::Pfn, split_into_num};
7
8#[derive(Debug, PartialEq, Eq, Clone, Hash)]
9#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
10pub struct Iomem(pub Vec<(usize, PhysicalMemoryMap)>);
11
12impl crate::FromBufRead for Iomem {
13    fn from_buf_read<R: BufRead>(r: R) -> ProcResult<Self> {
14        let mut vec = Vec::new();
15
16        for line in r.lines() {
17            let line = expect!(line);
18
19            let (indent, map) = PhysicalMemoryMap::from_line(&line)?;
20
21            vec.push((indent, map));
22        }
23
24        Ok(Iomem(vec))
25    }
26}
27
28#[derive(Debug, PartialEq, Eq, Clone, Hash)]
29#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
30pub struct PhysicalMemoryMap {
31    /// The address space in the process that the mapping occupies.
32    pub address: (u64, u64),
33    pub name: String,
34}
35
36impl PhysicalMemoryMap {
37    fn from_line(line: &str) -> ProcResult<(usize, PhysicalMemoryMap)> {
38        let indent = line.chars().take_while(|c| *c == ' ').count() / 2;
39        let line = line.trim();
40        let mut s = line.split(" : ");
41        let address = expect!(s.next());
42        let name = expect!(s.next());
43
44        Ok((
45            indent,
46            PhysicalMemoryMap {
47                address: split_into_num(address, '-', 16)?,
48                name: String::from(name),
49            },
50        ))
51    }
52
53    /// Get the PFN range for the mapping
54    ///
55    /// First element of the tuple (start) is included.
56    /// Second element (end) is excluded
57    pub fn get_range(&self) -> impl crate::WithSystemInfo<Output = (Pfn, Pfn)> {
58        move |si: &crate::SystemInfo| {
59            let start = self.address.0 / si.page_size();
60            let end = (self.address.1 + 1) / si.page_size();
61
62            (Pfn(start), Pfn(end))
63        }
64    }
65}