dbs_address_space/
numa.rs

1// Copyright (C) 2021 Alibaba Cloud. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Types for NUMA information.
5
6use vm_memory::{GuestAddress, GuestUsize};
7
8/// Strategy of mbind() and don't lead to OOM.
9pub const MPOL_PREFERRED: u32 = 1;
10
11/// Strategy of mbind()
12pub const MPOL_MF_MOVE: u32 = 2;
13
14/// Type for recording numa ids of different devices
15pub struct NumaIdTable {
16    /// vectors of numa id for each memory region
17    pub memory: Vec<u32>,
18    /// vectors of numa id for each cpu
19    pub cpu: Vec<u32>,
20}
21
22/// Record numa node memory information.
23#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
24pub struct NumaNodeInfo {
25    /// Base address of the region in guest physical address space.
26    pub base: GuestAddress,
27    /// Size of the address region.
28    pub size: GuestUsize,
29}
30
31/// Record all region's info of a numa node.
32#[derive(Debug, Default, Clone, PartialEq, Eq)]
33pub struct NumaNode {
34    region_infos: Vec<NumaNodeInfo>,
35    vcpu_ids: Vec<u32>,
36}
37
38impl NumaNode {
39    /// get reference of region_infos in numa node.
40    pub fn region_infos(&self) -> &Vec<NumaNodeInfo> {
41        &self.region_infos
42    }
43
44    /// get vcpu ids belonging to a numa node.
45    pub fn vcpu_ids(&self) -> &Vec<u32> {
46        &self.vcpu_ids
47    }
48
49    /// add a new numa region info into this numa node.
50    pub fn add_info(&mut self, info: &NumaNodeInfo) {
51        self.region_infos.push(*info);
52    }
53
54    /// add a group of vcpu ids belong to this numa node
55    pub fn add_vcpu_ids(&mut self, vcpu_ids: &[u32]) {
56        self.vcpu_ids.extend(vcpu_ids)
57    }
58
59    /// create a new numa node struct
60    pub fn new() -> NumaNode {
61        NumaNode {
62            region_infos: Vec::new(),
63            vcpu_ids: Vec::new(),
64        }
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn test_create_numa_node() {
74        let mut numa_node = NumaNode::new();
75        let info = NumaNodeInfo {
76            base: GuestAddress(0),
77            size: 1024,
78        };
79        numa_node.add_info(&info);
80        assert_eq!(*numa_node.region_infos(), vec![info]);
81        let vcpu_ids = vec![0, 1, 2, 3];
82        numa_node.add_vcpu_ids(&vcpu_ids);
83        assert_eq!(*numa_node.vcpu_ids(), vcpu_ids);
84    }
85}