|
| 1 | +use nix::sched::{sched_getaffinity, sched_setaffinity, CpuSet}; |
| 2 | +use nix::unistd::Pid; |
| 3 | + |
| 4 | +#[derive(Debug, thiserror::Error)] |
| 5 | +pub enum CPUAffinityError { |
| 6 | + #[error("invalid CPU string: {0}")] |
| 7 | + ParseError(String), |
| 8 | + #[error("values larger than {max} are not supported")] |
| 9 | + CpuOutOfRange { cpu: usize, max: usize }, |
| 10 | + #[error("failed to set CPU for CPU {cpu}: {source}")] |
| 11 | + CpuSet { |
| 12 | + cpu: usize, |
| 13 | + #[source] |
| 14 | + source: nix::Error, |
| 15 | + }, |
| 16 | + #[error("failed to setaffinity")] |
| 17 | + SetAffinity(#[source] nix::Error), |
| 18 | + #[error("failed to getaffinity")] |
| 19 | + GetAffinity(#[source] nix::Error), |
| 20 | +} |
| 21 | + |
| 22 | +type Result<T> = std::result::Result<T, CPUAffinityError>; |
| 23 | + |
| 24 | +pub fn to_cpuset(cpuset_str: &str) -> Result<CpuSet> { |
| 25 | + let mut cpuset = CpuSet::new(); |
| 26 | + let max_cpu = CpuSet::count(); |
| 27 | + |
| 28 | + for part in cpuset_str |
| 29 | + .trim() |
| 30 | + .split(',') |
| 31 | + .map(str::trim) |
| 32 | + .filter(|s| !s.is_empty()) |
| 33 | + { |
| 34 | + match part.split_once('-') { |
| 35 | + Some((start_str, end_str)) => { |
| 36 | + let start = parse_cpu_index(start_str, max_cpu)?; |
| 37 | + let end = parse_cpu_index(end_str, max_cpu)?; |
| 38 | + if start > end { |
| 39 | + return Err(CPUAffinityError::ParseError(format!( |
| 40 | + "invalid range: {}-{}", |
| 41 | + start, end |
| 42 | + ))); |
| 43 | + } |
| 44 | + for cpu in start..=end { |
| 45 | + cpuset |
| 46 | + .set(cpu) |
| 47 | + .map_err(|e| CPUAffinityError::CpuSet { cpu, source: e })?; |
| 48 | + } |
| 49 | + } |
| 50 | + None => { |
| 51 | + let cpu = parse_cpu_index(part, max_cpu)?; |
| 52 | + cpuset |
| 53 | + .set(cpu) |
| 54 | + .map_err(|e| CPUAffinityError::CpuSet { cpu, source: e })?; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + Ok(cpuset) |
| 59 | +} |
| 60 | + |
| 61 | +fn parse_cpu_index(s: &str, max_cpu: usize) -> Result<usize> { |
| 62 | + let cpu: usize = s |
| 63 | + .parse() |
| 64 | + .map_err(|_| CPUAffinityError::ParseError(s.to_string()))?; |
| 65 | + if cpu >= max_cpu { |
| 66 | + return Err(CPUAffinityError::CpuOutOfRange { |
| 67 | + cpu, |
| 68 | + max: max_cpu - 1, |
| 69 | + }); |
| 70 | + } |
| 71 | + Ok(cpu) |
| 72 | +} |
| 73 | + |
| 74 | +pub fn set_cpuset_affinity_from_string(pid: Pid, cpuset_str: &str) -> Result<()> { |
| 75 | + tracing::debug!(?cpuset_str, "setting CPU affinity for tenant container"); |
| 76 | + sched_setaffinity(pid, &to_cpuset(cpuset_str)?).map_err(CPUAffinityError::SetAffinity) |
| 77 | +} |
| 78 | + |
| 79 | +pub fn log_cpu_affinity() -> Result<()> { |
| 80 | + let cpuset = sched_getaffinity(Pid::this()).map_err(CPUAffinityError::GetAffinity)?; |
| 81 | + let mask = (0..usize::BITS as usize) |
| 82 | + .filter(|&i| cpuset.is_set(i).unwrap_or(false)) |
| 83 | + .fold(0, |mask, i| mask | (1 << i)); |
| 84 | + tracing::debug!("affinity: 0x{:x}", mask); |
| 85 | + Ok(()) |
| 86 | +} |
| 87 | + |
| 88 | +#[cfg(test)] |
| 89 | +mod tests { |
| 90 | + use super::*; |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn test_to_cpuset_single_values() { |
| 94 | + let cpuset = to_cpuset("0,1,2").unwrap(); |
| 95 | + for cpu in [0, 1, 2] { |
| 96 | + assert!(cpuset.is_set(cpu).unwrap()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn test_to_cpuset_range() { |
| 102 | + let cpuset = to_cpuset("3-5").unwrap(); |
| 103 | + for cpu in [3, 4, 5] { |
| 104 | + assert!(cpuset.is_set(cpu).unwrap()); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn test_to_cpuset_mixed() { |
| 110 | + let cpuset = to_cpuset("0, 2-4, 6").unwrap(); |
| 111 | + for cpu in [0, 2, 3, 4, 6] { |
| 112 | + assert!(cpuset.is_set(cpu).unwrap()); |
| 113 | + } |
| 114 | + for cpu in [1, 5, 7] { |
| 115 | + assert!(!cpuset.is_set(cpu).unwrap_or(false)); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + #[test] |
| 120 | + fn test_to_cpuset_spaces_and_empty() { |
| 121 | + let cpuset = to_cpuset(" , 1 , 3 , 5-7 , ").unwrap(); |
| 122 | + for cpu in [1, 3, 5, 6, 7] { |
| 123 | + assert!(cpuset.is_set(cpu).unwrap()); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn test_to_cpuset_invalid_range() { |
| 129 | + let err = to_cpuset("5-3").unwrap_err(); |
| 130 | + matches!(err, CPUAffinityError::ParseError(_)); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn test_to_cpuset_invalid_value() { |
| 135 | + let err = to_cpuset("a,b,c").unwrap_err(); |
| 136 | + matches!(err, CPUAffinityError::ParseError(_)); |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn test_to_cpuset_max_allowed_cpu() { |
| 141 | + let max = CpuSet::count(); |
| 142 | + let highest = max - 1; |
| 143 | + let cpuset = to_cpuset(&highest.to_string()).unwrap(); |
| 144 | + assert!(cpuset.is_set(highest).unwrap()); |
| 145 | + } |
| 146 | + |
| 147 | + #[test] |
| 148 | + fn test_to_cpuset_exceeds_max_cpu() { |
| 149 | + let max = CpuSet::count(); |
| 150 | + let result = to_cpuset(&max.to_string()); |
| 151 | + assert!(matches!( |
| 152 | + result, |
| 153 | + Err(CPUAffinityError::CpuOutOfRange { .. }) |
| 154 | + )); |
| 155 | + } |
| 156 | +} |
0 commit comments