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
#[derive(Default, Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
#[derive(Deserialize, Serialize)]
pub struct HyperThread(u16);
impl From<u16> for HyperThread
{
#[inline(always)]
fn from(value: u16) -> Self
{
HyperThread(value)
}
}
impl Into<u16> for HyperThread
{
#[inline(always)]
fn into(self) -> u16
{
self.0
}
}
impl HyperThread
{
#[inline(always)]
pub(crate) fn hyper_threads_to_mask(hyper_threads: &BTreeSet<Self>) -> String
{
let mut mask: u32 = 0;
for hyper_thread in hyper_threads.iter()
{
let bit = (1 << hyper_thread.0) as u32;
mask |= bit;
}
format!("{:08x}", mask)
}
#[inline(always)]
#[cfg(any(target_os = "android", target_os = "linux"))]
pub(crate) fn hyper_threads_to_list(hyper_threads: &BTreeSet<Self>) -> String
{
let mut list = String::with_capacity(hyper_threads.len() * 4);
for hyper_thread in hyper_threads.iter()
{
if !list.is_empty()
{
list.push(',');
}
list.push_str(&format!("{}", hyper_thread.0))
}
list
}
#[inline(always)]
pub fn set_work_queue_hyper_thread_affinity(hyper_threads: &BTreeSet<HyperThread>, sys_path: &SysPath) -> io::Result<()>
{
let mask = Self::hyper_threads_to_mask(hyper_threads);
sys_path.workqueue_file_path("cpumask").write_value(&mask)?;
sys_path.workqueue_file_path("writeback/cpumask").write_value(&mask)
}
#[inline(always)]
#[cfg(any(target_os = "android", target_os = "linux"))]
pub fn force_watchdog_to_just_these_hyper_threads(hyper_threads: &BTreeSet<HyperThread>, proc_path: &ProcPath) -> io::Result<()>
{
let yes_a_list_even_though_file_is_named_a_cpumask = HyperThread::hyper_threads_to_list(hyper_threads);
proc_path.file_path("sys/kernel/watchdog_cpumask").write_value(&yes_a_list_even_though_file_is_named_a_cpumask)
}
#[inline(always)]
pub fn last(hyper_threads: &BTreeSet<HyperThread>) -> Option<&Self>
{
hyper_threads.iter().last()
}
#[inline(always)]
pub fn complement(hyper_threads: &BTreeSet<Self>, sys_path: &SysPath) -> BTreeSet<Self>
{
let present = Self::present(sys_path);
present.difference(hyper_threads).cloned().collect()
}
#[inline(always)]
pub fn remove_those_offline(hyper_threads: &BTreeSet<Self>, sys_path: &SysPath) -> BTreeSet<Self>
{
let online = Self::online(sys_path);
online.intersection(hyper_threads).cloned().collect()
}
#[inline(always)]
pub fn present(sys_path: &SysPath) -> BTreeSet<Self>
{
Self::parse_list_mask(sys_path, "present")
}
#[inline(always)]
pub fn online(sys_path: &SysPath) -> BTreeSet<Self>
{
Self::parse_list_mask(sys_path, "online")
}
#[inline(always)]
pub fn offline(sys_path: &SysPath) -> BTreeSet<Self>
{
Self::parse_list_mask(sys_path, "offline")
}
#[inline(always)]
pub fn possible(sys_path: &SysPath) -> BTreeSet<Self>
{
Self::parse_list_mask(sys_path, "possible")
}
#[inline(always)]
pub fn is_online(self, sys_path: &SysPath) -> bool
{
match &self.online_file_path(sys_path).read_string_without_line_feed().unwrap()[..]
{
"0" => false,
"1" => true,
invalid @ _ => panic!("Invalid value for CPU online '{}'", invalid),
}
}
#[inline(always)]
pub fn is_offline(self, sys_path: &SysPath) -> bool
{
!self.is_online(sys_path)
}
#[inline(always)]
pub fn set_offline(self, sys_path: &SysPath) -> io::Result<()>
{
assert_effective_user_id_is_root(&format!("Offline CPU '{}'", self.0));
self.online_file_path(sys_path).write_value(0)
}
#[inline(always)]
pub fn set_online(self, sys_path: &SysPath) -> io::Result<()>
{
assert_effective_user_id_is_root(&format!("Online CPU '{}'", self.0));
self.online_file_path(sys_path).write_value(1)
}
#[inline(always)]
fn online_file_path(self, sys_path: &SysPath) -> PathBuf
{
sys_path.hyper_thread_path(self, "online")
}
#[inline(always)]
pub fn siblings(self, sys_path: &SysPath) -> BTreeSet<Self>
{
sys_path.hyper_thread_path(self, "topology/core_siblings_list").read_linux_core_or_numa_list(HyperThread::from).unwrap()
}
#[inline(always)]
pub fn thread_siblings(self, sys_path: &SysPath) -> BTreeSet<Self>
{
sys_path.hyper_thread_path(self, "topology/thread_siblings_list").read_linux_core_or_numa_list(HyperThread::from).unwrap()
}
#[inline(always)]
pub fn hyper_thread_groups(hyper_threads: &BTreeSet<Self>, sys_path: &SysPath) -> BTreeSet<BTreeSet<Self>>
{
let mut hyper_thread_groups = BTreeSet::new();
for hyper_thread in hyper_threads.iter()
{
let hyper_thread_group = (*hyper_thread).level1_cache_hyper_thread_siblings_including_self(sys_path);
hyper_thread_groups.insert(hyper_thread_group);
}
hyper_thread_groups
}
#[inline(always)]
pub fn numa_node(self, sys_path: &SysPath) -> Option<u8>
{
match sys_path.hyper_thread_path(self, "node").canonicalize()
{
Err(_) => None,
Ok(canonical) => match canonical.file_name()
{
None => None,
Some(file_name) => match file_name.to_str()
{
None => None,
Some(file_name) => if file_name.starts_with("node")
{
u8::from_str(&file_name[ ("node".len()) .. ]).ok()
}
else
{
None
}
},
},
}
}
#[inline(always)]
pub fn level1_cache_hyper_thread_siblings_including_self(self, sys_path: &SysPath) -> BTreeSet<Self>
{
sys_path.hyper_thread_path(self, "cache/index0/shared_cpu_list").read_linux_core_or_numa_list(HyperThread::from).unwrap()
}
#[inline(always)]
pub fn level1_cache_hyper_thread_siblings_excluding_self(self, sys_path: &SysPath) -> BTreeSet<Self>
{
let mut hyper_threads = self.level1_cache_hyper_thread_siblings_including_self(sys_path);
hyper_threads.remove(&self);
hyper_threads
}
#[inline(always)]
pub fn underlying_hardware_physical_core_identifier(self, sys_path: &SysPath) -> io::Result<u16>
{
sys_path.hyper_thread_path(self, "topology/core_id").read_value()
}
#[inline(always)]
pub fn underlying_hardware_physical_socket_identifier(self, sys_path: &SysPath) -> io::Result<u16>
{
sys_path.hyper_thread_path(self, "topology/physical_package_id").read_value()
}
#[inline(always)]
pub fn kernel_maximum_index(sys_path: &SysPath) -> io::Result<Self>
{
sys_path.hyper_threads_path("kernel_max").read_value().map(|value| HyperThread(value))
}
#[inline(always)]
fn parse_list_mask(sys_path: &SysPath, file_name: &str) -> BTreeSet<Self>
{
sys_path.hyper_threads_path(file_name).read_linux_core_or_numa_list(HyperThread::from).unwrap()
}
#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "linux"))]
pub fn current_hyper_thread() -> Self
{
extern "C"
{
fn sched_getcpu() -> c_int;
}
let result = unsafe { sched_getcpu() };
debug_assert!(result >= 0, "sched_getcpu() was negative");
debug_assert!(result <= ::std::u16::MAX as i32, "sched_getcpu() was too large");
HyperThread(result as u16)
}
#[cfg(target_os = "freebsd")]
pub(crate) fn current_hyper_thread() -> Self
{
HyperThread(0)
}
}