themis/topology/cpu/
efficiency_view.rs1use super::CpuTopology;
4#[cfg(windows)]
5use super::ProcessorAffinityGroups;
6use crate::topology::types::EfficiencyClass;
7
8#[derive(Clone, Copy, Debug)]
15pub struct CpuEfficiencyView<'topology> {
16 classes: &'topology [EfficiencyClass],
17 highest: EfficiencyClass,
18}
19
20impl<'topology> CpuEfficiencyView<'topology> {
21 pub(super) fn new(classes: &'topology [EfficiencyClass]) -> Option<Self> {
22 let highest = classes.iter().max().copied()?;
23 Some(Self { classes, highest })
24 }
25
26 #[must_use]
28 pub const fn classes(self) -> &'topology [EfficiencyClass] {
29 self.classes
30 }
31
32 #[must_use]
34 pub fn class_count(self) -> usize {
35 usize::from(self.highest.rank()) + 1
36 }
37
38 #[must_use]
40 pub fn is_hybrid(self) -> bool {
41 self.class_count() > 1
42 }
43
44 #[must_use]
46 pub const fn highest_class(self) -> EfficiencyClass {
47 self.highest
48 }
49
50 #[must_use]
55 pub fn processor_class(self, processor: u32) -> Option<EfficiencyClass> {
56 self.classes.get(usize::try_from(processor).ok()?).copied()
57 }
58
59 #[must_use]
63 pub fn is_in_highest_class(self, processor: u32) -> Option<bool> {
64 Some(self.processor_class(processor)? == self.highest)
65 }
66
67 #[must_use = "iterators are lazy; consume the returned processor iterator"]
69 pub fn processors_in_class(
70 self,
71 class: EfficiencyClass,
72 ) -> impl Iterator<Item = u32> + 'topology {
73 self.classes
74 .iter()
75 .enumerate()
76 .filter(move |(_, candidate)| **candidate == class)
77 .filter_map(|(processor, _)| u32::try_from(processor).ok())
79 }
80
81 #[must_use = "iterators are lazy; consume the returned processor iterator"]
83 pub fn highest_class_processors(self) -> impl Iterator<Item = u32> + 'topology {
84 self.processors_in_class(self.highest)
85 }
86
87 #[cfg(windows)]
89 #[must_use]
90 pub fn processor_affinity_groups(self, class: EfficiencyClass) -> ProcessorAffinityGroups {
91 ProcessorAffinityGroups::from_processors(self.processors_in_class(class))
92 }
93
94 #[cfg(windows)]
96 #[must_use]
97 pub fn highest_class_affinity_groups(self) -> ProcessorAffinityGroups {
98 self.processor_affinity_groups(self.highest)
99 }
100}
101
102impl CpuTopology {
103 #[must_use]
110 pub fn efficiency(&self) -> Option<CpuEfficiencyView<'_>> {
111 CpuEfficiencyView::new(self.efficiency_classes.as_deref()?)
112 }
113}