1use rustc_abi::Endian;
2use rustc_middle::ty::layout::LayoutOf;
34use crate::*;
56/// The maximum number of CPUs supported by miri.
7///
8/// This value is compatible with the libc `CPU_SETSIZE` constant and corresponds to the number
9/// of CPUs that a `cpu_set_t` can contain.
10///
11/// Real machines can have more CPUs than this number, and there exist APIs to set their affinity,
12/// but this is not currently supported by miri.
13pub const MAX_CPUS: usize = 1024;
1415/// A thread's CPU affinity mask determines the set of CPUs on which it is eligible to run.
16// the actual representation depends on the target's endianness and pointer width.
17// See CpuAffinityMask::set for details
18#[derive(Clone)]
19pub(crate) struct CpuAffinityMask([u8; Self::CPU_MASK_BYTES]);
2021impl CpuAffinityMask {
22pub(crate) const CPU_MASK_BYTES: usize = MAX_CPUS / 8;
2324pub fn new<'tcx>(cx: &impl LayoutOf<'tcx>, cpu_count: u32) -> Self {
25let mut this = Self([0; Self::CPU_MASK_BYTES]);
2627// the default affinity mask includes only the available CPUs
28for i in 0..cpu_count as usize {
29 this.set(cx, i);
30 }
3132 this
33 }
3435pub fn chunk_size<'tcx>(cx: &impl LayoutOf<'tcx>) -> u64 {
36// The actual representation of the CpuAffinityMask is [c_ulong; _].
37let ulong = helpers::path_ty_layout(cx, &["core", "ffi", "c_ulong"]);
38 ulong.size.bytes()
39 }
4041fn set<'tcx>(&mut self, cx: &impl LayoutOf<'tcx>, cpu: usize) {
42// we silently ignore CPUs that are out of bounds. This matches the behavior of
43 // `sched_setaffinity` with a mask that specifies more than `CPU_SETSIZE` CPUs.
44if cpu >= MAX_CPUS {
45return;
46 }
4748// The actual representation of the CpuAffinityMask is [c_ulong; _].
49 // Within the array elements, we need to use the endianness of the target.
50let target = &cx.tcx().sess.target;
51match Self::chunk_size(cx) {
524 => {
53let start = cpu / 32 * 4; // first byte of the correct u32
54let chunk = self.0[start..].first_chunk_mut::<4>().unwrap();
55let offset = cpu % 32;
56*chunk = match target.options.endian {
57 Endian::Little => (u32::from_le_bytes(*chunk) | (1 << offset)).to_le_bytes(),
58 Endian::Big => (u32::from_be_bytes(*chunk) | (1 << offset)).to_be_bytes(),
59 };
60 }
618 => {
62let start = cpu / 64 * 8; // first byte of the correct u64
63let chunk = self.0[start..].first_chunk_mut::<8>().unwrap();
64let offset = cpu % 64;
65*chunk = match target.options.endian {
66 Endian::Little => (u64::from_le_bytes(*chunk) | (1 << offset)).to_le_bytes(),
67 Endian::Big => (u64::from_be_bytes(*chunk) | (1 << offset)).to_be_bytes(),
68 };
69 }
70 other => bug!("chunk size not supported: {other}"),
71 };
72 }
7374pub fn as_slice(&self) -> &[u8] {
75self.0.as_slice()
76 }
7778pub fn from_array<'tcx>(
79 cx: &impl LayoutOf<'tcx>,
80 cpu_count: u32,
81 bytes: [u8; Self::CPU_MASK_BYTES],
82 ) -> Option<Self> {
83// mask by what CPUs are actually available
84let default = Self::new(cx, cpu_count);
85let masked = std::array::from_fn(|i| bytes[i] & default.0[i]);
8687// at least one thread must be set for the input to be valid
88masked.iter().any(|b| *b != 0).then_some(Self(masked))
89 }
90}