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
use std::fmt::{Debug, Display};
use std::hash::{Hash, Hasher};
use derive_more::derive::AsRef;
use crate::pal::{AbstractProcessor, ProcessorFacade};
use crate::{EfficiencyClass, MemoryRegionId, ProcessorId};
/// A processor present on the system and available to the current process.
#[derive(AsRef, Clone)]
pub struct Processor {
#[as_ref]
inner: ProcessorFacade,
}
impl Processor {
#[must_use]
pub(crate) fn new(inner: ProcessorFacade) -> Self {
Self { inner }
}
/// The unique numeric ID of the processor, matching the ID used by operating system tools.
///
/// You can obtain the upper bound via [`SystemHardware::max_processor_id()`][1].
///
/// # Example
///
/// ```
/// use many_cpus::SystemHardware;
///
/// let processors = SystemHardware::current().processors();
///
/// for processor in processors {
/// let id = processor.id();
/// println!("Default processor set includes processor {id}");
/// }
/// ```
///
/// [1]: crate::SystemHardware::max_processor_id
#[cfg_attr(test, mutants::skip)] // Trivial delegation, do not waste time on mutation.
#[inline]
#[must_use]
pub fn id(&self) -> ProcessorId {
self.inner.id()
}
/// The unique numeric ID of the memory region, matching the ID used by operating system tools.
///
/// You can obtain the upper bound via [`SystemHardware::max_memory_region_id()`][1].
///
/// [1]: crate::SystemHardware::max_memory_region_id
#[cfg_attr(test, mutants::skip)] // Trivial delegation, do not waste time on mutation.
#[inline]
#[must_use]
pub fn memory_region_id(&self) -> MemoryRegionId {
self.inner.memory_region_id()
}
/// The [efficiency class][EfficiencyClass] of the processor.
///
/// This is a relative measure - the fastest processors on any given system are always
/// considered performance processors, while any that are slower are considered efficiency
/// processors.
///
/// # Example
///
/// ```
/// use many_cpus::{EfficiencyClass, SystemHardware};
///
/// let processors = SystemHardware::current().processors();
/// let mut performance_count = 0;
/// let mut efficiency_count = 0;
///
/// for processor in processors {
/// match processor.efficiency_class() {
/// EfficiencyClass::Performance => {
/// performance_count += 1;
/// println!("Processor {} is a performance processor", processor.id());
/// }
/// EfficiencyClass::Efficiency => {
/// efficiency_count += 1;
/// println!("Processor {} is an efficiency processor", processor.id());
/// }
/// }
/// }
///
/// println!(
/// "System has {performance_count} performance and {efficiency_count} efficiency processors",
/// );
/// ```
#[cfg_attr(test, mutants::skip)] // Trivial delegation, do not waste time on mutation.
#[inline]
#[must_use]
pub fn efficiency_class(&self) -> EfficiencyClass {
self.inner.efficiency_class()
}
}
impl PartialEq for Processor {
#[cfg_attr(test, mutants::skip)] // Trivial delegation, do not waste time on mutation.
#[inline]
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}
impl Eq for Processor {}
impl Hash for Processor {
#[cfg_attr(test, mutants::skip)] // Trivial delegation, do not waste time on mutation.
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.inner.hash(state);
}
}
impl Display for Processor {
#[cfg_attr(test, mutants::skip)] // Trivial delegation, do not waste time on mutation.
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.inner, f)
}
}
#[cfg_attr(coverage_nightly, coverage(off))] // No API contract to test.
impl Debug for Processor {
#[cfg_attr(test, mutants::skip)] // Trivial delegation, do not waste time on mutation.
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.inner, f)
}
}
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
use std::hash::DefaultHasher;
use std::panic::{RefUnwindSafe, UnwindSafe};
use static_assertions::assert_impl_all;
use super::*;
use crate::fake::platform::FakeProcessor;
assert_impl_all!(Processor: UnwindSafe, RefUnwindSafe);
#[test]
fn smoke_test() {
let pal_processor = FakeProcessor::new(42, 13, EfficiencyClass::Efficiency);
let processor = Processor::new(pal_processor.into());
// Getters appear to get the expected values.
assert_eq!(processor.id(), 42);
assert_eq!(processor.memory_region_id(), 13);
assert_eq!(processor.efficiency_class(), EfficiencyClass::Efficiency);
// A clone is a legit clone.
let processor_clone = processor.clone();
assert_eq!(processor, processor_clone);
// Clones have the same hash.
let mut hasher1 = DefaultHasher::new();
processor.hash(&mut hasher1);
let hash1 = hasher1.finish();
let mut hasher2 = DefaultHasher::new();
processor_clone.hash(&mut hasher2);
let hash2 = hasher2.finish();
assert_eq!(hash1, hash2);
// Display writes something (anything - as long as it writes something and does not panic).
let displayed = format!("{processor}");
assert!(!displayed.is_empty());
// Debug writes something (anything - as long as it writes something and does not panic).
let debugged = format!("{processor:?}");
assert!(!debugged.is_empty());
}
}