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
// This file is part of dpdk. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/dpdk/master/COPYRIGHT. No part of dpdk, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2017 The developers of dpdk. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/dpdk/master/COPYRIGHT.


/// Represents a logical hyper thread, which in Operating System terms is usually a logical CPU (core).
///
/// These usually map 1:1 with `LogicalCore`s
#[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
	}
	
	/// Sets workqueue hyper thread affinity.
	#[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)
	}
	
	/// We ignore failures as the `/proc` for this is brittle.
	///
	/// Should not be needed if `nohz_full` was specified on the Linux command line.
	#[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)
	}
	
	/// Last hyper thread.
	#[inline(always)]
	pub fn last(hyper_threads: &BTreeSet<HyperThread>) -> Option<&Self>
	{
		hyper_threads.iter().last()
	}
	
	/// The complement of `hyper_threads`.
	#[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()
	}
	
	/// Remove as offline `hyper_threads`.
	#[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()
	}
	
	/// CPUs (hyper threaded logical cores) that are present and that could become online.
	///
	/// Consider using libnuma instead of this call.
	///
	/// See <https://www.kernel.org/doc/Documentation/cputopology.txt>.
	#[inline(always)]
	pub fn present(sys_path: &SysPath) -> BTreeSet<Self>
	{
		Self::parse_list_mask(sys_path, "present")
	}
	
	/// Hyper threaded logical cores that are online at some point.
	///
	/// Consider using libnuma instead of this call.
	///
	/// See <https://www.kernel.org/doc/Documentation/cputopology.txt>.
	#[inline(always)]
	pub fn online(sys_path: &SysPath) -> BTreeSet<Self>
	{
		Self::parse_list_mask(sys_path, "online")
	}
	
	/// Hyper threaded logical cores that are offline.
	///
	/// The maximum CPU index in this list ***can exceed the kernel's maximum in `self.kernel_maximum_index`***.
	///
	/// Close to useless.
	///
	/// See <https://www.kernel.org/doc/Documentation/cputopology.txt>.
	#[inline(always)]
	pub fn offline(sys_path: &SysPath) -> BTreeSet<Self>
	{
		Self::parse_list_mask(sys_path, "offline")
	}
	
	/// Hyper threaded logical cores that could possibly be online at some point.
	///
	/// Close to very useless.
	///
	/// See <https://www.kernel.org/doc/Documentation/cputopology.txt>.
	#[inline(always)]
	pub fn possible(sys_path: &SysPath) -> BTreeSet<Self>
	{
		Self::parse_list_mask(sys_path, "possible")
	}
	
	/// Is this hyper thread online?
	///
	/// See <https://www.kernel.org/doc/Documentation/core-api/cpu_hotplug.rst>.
	#[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),
		}
	}
	
	/// Is this hyper thread offline?
	///
	/// See <https://www.kernel.org/doc/Documentation/core-api/cpu_hotplug.rst>.
	#[inline(always)]
	pub fn is_offline(self, sys_path: &SysPath) -> bool
	{
		!self.is_online(sys_path)
	}
	
	/// Disable (offline) this hyper thread.
	///
	/// Requires root.
	///
	/// Hyper thread (CPU) zero (0) is special on x86 / x86-64 and can not ordinarily be offlined.
	///
	/// See <https://www.kernel.org/doc/Documentation/core-api/cpu_hotplug.rst>.
	#[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)
	}
	
	/// Enable (online) this hyper thread.
	///
	/// Requires root.
	///
	/// See <https://www.kernel.org/doc/Documentation/core-api/cpu_hotplug.rst>.
	#[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")
	}
	
	/// Hyper threaded logical cores that are siblings of this one.
	///
	/// Will include `self`.
	///
	/// See <https://www.kernel.org/doc/Documentation/cputopology.txt>.
	#[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()
	}
	
	/// Hyper threaded logical cores that are hyper-thread-siblings of this one.
	///
	/// Will include `self`.
	///
	/// Usually wrong on virtual machines (eg Parallels Desktop).
	///
	/// See <https://www.kernel.org/doc/Documentation/cputopology.txt>.
	#[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()
	}
	
	/// Hyper threaded logical cores grouped as hyper thread groups (eg HT 0 and 1, 2 and 3, etc).
	#[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
	}
	
	/// Tries to find this hyper thread's NUMA node, if this is a NUMA machine.
	#[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
					}
				},
			},
		}
	}
	
	// there is a /node file that symlinks to a NUMA node location.
	
	/// Hyper threaded logical cores that are thread-siblings of this one according to the level 1 cache.
	///
	/// Will include `self`.
	///
	/// Usually reliable.
	#[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()
	}
	
	/// Hyper threaded logical cores that are thread-siblings of this one according to the level 1 cache.
	///
	/// Will exclude `self`.
	///
	/// Usually reliable.
	#[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
	}
	
	/// Underlying hardware, not Linux, core identifier.
	///
	/// See <https://www.kernel.org/doc/Documentation/cputopology.txt>.
	#[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()
	}
	
	/// Underlying hardware, not Linux, socket identifier.
	///
	/// See <https://www.kernel.org/doc/Documentation/cputopology.txt>.
	#[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()
	}
	
	/// Simply reports the maximum *identifier* that could be used by the Linux kernel upto the `CONFIG_` number of CPUs.
	///
	/// Add one to this to get the exclusive maximum.
	///
	/// Consider using libnuma instead of this call.
	#[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()
	}
	
	/// Current hyper thread index that this thread is running on.
	///
	/// Unless this thread has been scheduled to only run on this hyper thread, then the result is close to useless.
	///
	/// Topology is not available on FreeBSD; value will always be zero.
	#[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)
	}
	
	/// Current hyper thread index that this thread is running on.
	///
	/// Unless this thread has been scheduled to only run on this hyper thread, then the result is close to useless.
	///
	/// Topology is not available on FreeBSD; value will always be zero.
	#[cfg(target_os = "freebsd")]
	pub(crate) fn current_hyper_thread() -> Self
	{
		HyperThread(0)
	}
}