numa_maps/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]

use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::str::FromStr;

/// Properties for memory regions. Consult `man 7 numa` for details.
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum Property {
    /// File backing the memory region.
    File(PathBuf),
    /// Size on each numa node `(numa_node, size)`. Size in pages, or bytes after normalizing.
    N(usize, usize),
    /// Memory range is used for the heap.
    Heap,
    /// Memory range is used for the stack.
    Stack,
    /// Memory range backed by huge pages. Page size differs from other entries.
    Huge,
    /// Number of anonymous pages in range.
    Anon(usize),
    /// Number of dirty pages in range.
    Dirty(usize),
    /// Number of mapped pages in range.
    Mapped(usize),
    /// Number of processes mapping a single page.
    MapMax(usize),
    /// Number of pages that have an associated entry on a swap device.
    SwapCache(usize),
    /// Number of pages on the active list.
    Active(usize),
    /// Number of pages that are currently being written out to disk.
    Writeback(usize),
    /// The size of the pages in this region in bytes.
    Kernelpagesize(usize),
}

impl Property {
    /// Returns the kernel page size if the property matches the page size property.
    #[must_use]
    pub fn page_size(&self) -> Option<usize> {
        match self {
            Self::Kernelpagesize(page_size) => Some(*page_size),
            _ => None,
        }
    }

    /// Normalize the property given the page size. Returns an
    /// optional value, which is set for all but the page size property.
    #[must_use]
    pub fn normalize(self, page_size: usize) -> Option<Self> {
        use Property::*;
        match self {
            File(p) => Some(File(p)),
            N(node, pages) => Some(N(node, pages * page_size)),
            Heap => Some(Heap),
            Stack => Some(Stack),
            Huge => Some(Huge),
            Anon(pages) => Some(Anon(pages * page_size)),
            Dirty(pages) => Some(Dirty(pages * page_size)),
            Mapped(pages) => Some(Mapped(pages * page_size)),
            MapMax(pages) => Some(MapMax(pages)),
            SwapCache(pages) => Some(SwapCache(pages * page_size)),
            Active(pages) => Some(Active(pages * page_size)),
            Writeback(pages) => Some(Writeback(pages * page_size)),
            Kernelpagesize(_) => None,
        }
    }
}

impl FromStr for Property {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (key, val) = if let Some(index) = s.find('=') {
            let (key, val) = s.split_at(index);
            (key, Some(&val[1..]))
        } else {
            (s, None)
        };
        match (key, val) {
            (key, Some(val)) if key.starts_with('N') => {
                let node = key[1..].parse().map_err(|e| format!("{e}"))?;
                let count = val.parse().map_err(|e| format!("{e}"))?;
                Ok(Self::N(node, count))
            }
            ("file", Some(val)) => Ok(Self::File(PathBuf::from(val))),
            ("heap", _) => Ok(Self::Heap),
            ("stack", _) => Ok(Self::Stack),
            ("huge", _) => Ok(Self::Huge),
            ("anon", Some(val)) => val.parse().map(Self::Anon).map_err(|e| format!("{e}")),
            ("dirty", Some(val)) => val.parse().map(Self::Dirty).map_err(|e| format!("{e}")),
            ("mapped", Some(val)) => val.parse().map(Self::Mapped).map_err(|e| format!("{e}")),
            ("mapmax", Some(val)) => val.parse().map(Self::MapMax).map_err(|e| format!("{e}")),
            ("swapcache", Some(val)) => {
                val.parse().map(Self::SwapCache).map_err(|e| format!("{e}"))
            }
            ("active", Some(val)) => val.parse().map(Self::Active).map_err(|e| format!("{e}")),
            ("writeback", Some(val)) => {
                val.parse().map(Self::Writeback).map_err(|e| format!("{e}"))
            }
            ("kernelpagesize_kB", Some(val)) => val
                .parse()
                .map(|sz: usize| Self::Kernelpagesize(sz << 10))
                .map_err(|e| format!("{e}")),
            (key, None) => Err(format!("unknown key: {key}")),
            (key, Some(val)) => Err(format!("unknown key/value: {key}={val}")),
        }
    }
}

/// A memory range, with a base address and a list of properties.
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
pub struct Range {
    /// The base address of this memory range.
    pub address: usize,
    /// The range's memory policy.
    pub policy: String,
    /// Properties associated with the memory range.
    pub properties: Vec<Property>,
}

impl Range {
    /// Parse a numa map line. Prints errors to stderr.
    ///
    /// Returns no value if the line does not contain an address, or the address is
    /// malformed.
    fn parse(line: &str) -> Option<Self> {
        let mut parts = line.split_whitespace();
        let address = <usize>::from_str_radix(parts.next()?, 16).ok()?;
        let policy = parts.next()?.to_string();
        let mut properties = Vec::new();
        for part in parts {
            match part.parse::<Property>() {
                Ok(property) => properties.push(property),
                Err(err) => eprintln!("Failed to parse numa_map entry \"{part}\": {err}"),
            }
        }
        Some(Self {
            address,
            policy,
            properties,
        })
    }

    /// Normalize the range using the page size property, if it exists.
    pub fn normalize(&mut self) {
        let page_size = self.properties.iter().find_map(Property::page_size);
        if let Some(page_size) = page_size {
            let mut properties: Vec<_> = self
                .properties
                .drain(..)
                .filter_map(|p| Property::normalize(p, page_size))
                .collect();
            properties.sort();
            self.properties = properties;
        }
    }
}

/// A whole `numu_maps` file.
#[derive(Default)]
pub struct NumaMap {
    /// Individual memory ranges.
    pub ranges: Vec<Range>,
}

impl NumaMap {
    /// Read a `numa_maps` file from `path`.
    ///
    /// Parses the contents and returns them as [`NumaMap`]. Each line translates
    /// to an entry in [`NumaMap::ranges`], which stores the properties gathered
    /// from the file as [`Property`].
    ///
    /// # Errors
    ///
    /// Returns an error if it fails to read the file.
    pub fn from_file<P: AsRef<Path>>(path: P) -> std::io::Result<Self> {
        let file = File::open(path)?;
        let reader = BufReader::new(file);

        let mut ranges = Vec::new();
        for line in reader.lines() {
            if let Some(range) = Range::parse(&(line?)) {
                ranges.push(range);
            }
        }
        Ok(Self { ranges })
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_read() -> std::io::Result<()> {
        let map = NumaMap::from_file("resources/numa_maps")?;

        assert_eq!(map.ranges.len(), 23);

        use Property::{Active, Anon, Dirty, File, Heap, Kernelpagesize, MapMax, Mapped, Stack, N};
        let expected = [
            Range {
                address: 93893825802240,
                policy: "default".to_string(),
                properties: vec![
                    File("/usr/bin/cat".into()),
                    Mapped(2),
                    MapMax(3),
                    Active(0),
                    N(0, 2),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 93893825810432,
                policy: "default".to_string(),
                properties: vec![
                    File("/usr/bin/cat".into()),
                    Mapped(6),
                    MapMax(3),
                    Active(0),
                    N(0, 6),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 93893825835008,
                policy: "default".to_string(),
                properties: vec![
                    File("/usr/bin/cat".into()),
                    Mapped(3),
                    MapMax(3),
                    Active(0),
                    N(0, 3),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 93893825847296,
                policy: "default".to_string(),
                properties: vec![
                    File("/usr/bin/cat".into()),
                    Anon(1),
                    Dirty(1),
                    Active(0),
                    N(0, 1),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 93893825851392,
                policy: "default".to_string(),
                properties: vec![
                    File("/usr/bin/cat".into()),
                    Anon(1),
                    Dirty(1),
                    Active(0),
                    N(0, 1),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 93893836886016,
                policy: "default".to_string(),
                properties: vec![Heap, Anon(2), Dirty(2), N(0, 2), Kernelpagesize(4096)],
            },
            Range {
                address: 140172716933120,
                policy: "default".to_string(),
                properties: vec![
                    File("/usr/lib/locale/locale-archive".into()),
                    Mapped(94),
                    MapMax(138),
                    Active(0),
                    N(0, 94),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 140172721541120,
                policy: "default".to_string(),
                properties: vec![Anon(3), Dirty(3), Active(1), N(0, 3), Kernelpagesize(4096)],
            },
            Range {
                address: 140172721692672,
                policy: "default".to_string(),
                properties: vec![
                    File("/usr/lib/x86_64-linux-gnu/libc.so.6".into()),
                    Mapped(38),
                    MapMax(174),
                    N(0, 38),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 140172721848320,
                policy: "default".to_string(),
                properties: vec![
                    File("/usr/lib/x86_64-linux-gnu/libc.so.6".into()),
                    Mapped(192),
                    MapMax(185),
                    N(0, 192),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 140172723245056,
                policy: "default".to_string(),
                properties: vec![
                    File("/usr/lib/x86_64-linux-gnu/libc.so.6".into()),
                    Mapped(43),
                    MapMax(181),
                    N(0, 43),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 140172723589120,
                policy: "default".to_string(),
                properties: vec![
                    File("/usr/lib/x86_64-linux-gnu/libc.so.6".into()),
                    Anon(4),
                    Dirty(4),
                    Active(0),
                    N(0, 4),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 140172723605504,
                policy: "default".to_string(),
                properties: vec![
                    File("/usr/lib/x86_64-linux-gnu/libc.so.6".into()),
                    Anon(2),
                    Dirty(2),
                    Active(0),
                    N(0, 2),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 140172723613696,
                policy: "default".to_string(),
                properties: vec![Anon(5), Dirty(5), Active(1), N(0, 5), Kernelpagesize(4096)],
            },
            Range {
                address: 140172723773440,
                policy: "default".to_string(),
                properties: vec![Anon(1), Dirty(1), Active(0), N(0, 1), Kernelpagesize(4096)],
            },
            Range {
                address: 140172723781632,
                policy: "default".to_string(),
                properties: vec![
                    File("/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2".into()),
                    Mapped(1),
                    MapMax(173),
                    N(0, 1),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 140172723785728,
                policy: "default".to_string(),
                properties: vec![
                    File("/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2".into()),
                    Mapped(37),
                    MapMax(181),
                    N(0, 37),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 140172723937280,
                policy: "default".to_string(),
                properties: vec![
                    File("/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2".into()),
                    Mapped(10),
                    MapMax(173),
                    N(0, 10),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 140172723978240,
                policy: "default".to_string(),
                properties: vec![
                    File("/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2".into()),
                    Anon(2),
                    Dirty(2),
                    Active(0),
                    N(0, 2),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 140172723986432,
                policy: "default".to_string(),
                properties: vec![
                    File("/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2".into()),
                    Anon(2),
                    Dirty(2),
                    Active(0),
                    N(0, 2),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 140736328499200,
                policy: "default".to_string(),
                properties: vec![
                    Stack,
                    Anon(3),
                    Dirty(3),
                    Active(0),
                    N(0, 3),
                    Kernelpagesize(4096),
                ],
            },
            Range {
                address: 140736330227712,
                policy: "default".to_string(),
                properties: vec![],
            },
            Range {
                address: 140736330244096,
                policy: "default".to_string(),
                properties: vec![],
            },
        ];

        assert_eq!(&expected[..], &map.ranges[..]);

        Ok(())
    }

    #[test]
    fn test_normalize() {
        use Property::*;

        let line = "7fbd0c10f000 default anon=5 dirty=5 active=1 N0=5 kernelpagesize_kB=4";
        let mut range = Range::parse(line).unwrap();
        range.normalize();
        range.properties.sort();
        let expected = vec![
            N(0, 5 << 12),
            Anon(5 << 12),
            Dirty(5 << 12),
            Active(1 << 12),
        ];
        assert_eq!(&expected, &range.properties);
    }

    #[test]
    #[cfg(target_os = "linux")]
    fn test_read_self() -> std::io::Result<()> {
        let map = NumaMap::from_file("/proc/self/numa_maps")?;
        assert!(map.ranges.len() > 0);
        Ok(())
    }
}