mac_sys_info/structs/
cpu_info.rs

1/*
2MIT License
3
4Copyright (c) 2020 Philipp Schuster
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25//! Basic info about CPU.
26
27use derive_more::Display as DeriveMoreDisplay;
28use serde::{Serialize};
29use std::collections::BTreeMap;
30use crate::error::MacSysInfoError;
31use crate::generated_sysctl_keys::SysctlKey;
32use crate::structs::CacheInfo;
33use crate::structs::CpuArchitectureInfo;
34use crate::parse::{parse_sysctl_value, ParseAsType};
35
36
37/// General CPU info.
38#[derive(Debug, Serialize, DeriveMoreDisplay)]
39#[display(fmt = "CpuInfo (\n\
40\x20    phys_cores: {},\n\
41\x20    logic_cores: {},\n\
42\x20    num_cores: {},\n\
43\x20    cache_info: <CacheInfo>,\n\
44\x20    brand_string: {},\n\
45\x20    frequency: {},\n\
46\x20    min_frequency: {},\n\
47\x20    max_frequency: {},\n\
48\x20    architecture: {},\n\
49)", phys_cores, logic_cores, num_cores, brand_string, frequency, min_frequency, max_frequency, architecture)]
50pub struct CpuInfo {
51    /// Physical cores on the CPU.
52    phys_cores: usize,
53    /// Logical cores on the CPU. Due to hardware multithreading (Like Intel Hyperthreading)
54    /// this can be more than the physical cores.
55    logic_cores: usize,
56    /// Actual available cores on the system.
57    num_cores: usize,
58    /// Information about the CPU cache.
59    cache_info: CacheInfo,
60    /// For example "Intel(R) Core(TM) i5-5257U CPU @ 2.70GH"
61    brand_string: String,
62    /// Base frequency in Hertz.
63    frequency: usize,
64    /// Min frequency in Hertz.
65    min_frequency: usize,
66    /// Max frequency in Hertz.
67    max_frequency: usize,
68    /// Architecture. x86_64 or AppleSi
69    architecture: CpuArchitectureInfo,
70}
71
72impl CpuInfo {
73
74    /// Constructor.
75    pub(crate) fn new(sysinfo: &BTreeMap<String, String>) -> Result<Self, MacSysInfoError> {
76        let x = CpuInfo {
77            phys_cores: parse_sysctl_value(
78                "phys_cores",
79                SysctlKey::HwPhysicalcpu,
80                sysinfo,
81                ParseAsType::Usize)?
82                .get_usize(),
83            logic_cores: parse_sysctl_value(
84                "logic_cores",
85                SysctlKey::HwLogicalcpu,
86                sysinfo,
87                ParseAsType::Usize)?
88                .get_usize(),
89            num_cores: parse_sysctl_value(
90                "num_cores",
91                SysctlKey::HwActivecpu,
92                sysinfo,
93                ParseAsType::Usize)?
94                .get_usize(),
95            brand_string: parse_sysctl_value(
96                "brand_string",
97                SysctlKey::MachdepCpuBrand_string,
98                sysinfo,
99                ParseAsType::String)?
100                .get_string(),
101            frequency: parse_sysctl_value(
102                "frequency",
103                SysctlKey::HwCpufrequency,
104                sysinfo,
105                ParseAsType::Usize)?
106                .get_usize(),
107            min_frequency: parse_sysctl_value(
108                "min_frequency",
109                SysctlKey::HwCpufrequency_min,
110                sysinfo,
111                ParseAsType::Usize)?
112                .get_usize(),
113            max_frequency: parse_sysctl_value(
114                "max_frequency",
115                SysctlKey::HwCpufrequency_max,
116                sysinfo,
117                ParseAsType::Usize)?
118                .get_usize(),
119            cache_info: CacheInfo::new(sysinfo)?,
120            architecture: CpuArchitectureInfo::new(sysinfo)?,
121        };
122        Ok(x)
123    }
124
125    /// Getter for the field `phys_cores`.
126    pub fn phys_cores(&self) -> usize {
127        self.phys_cores
128    }
129
130    /// Getter for the field `logic_cores`.
131    pub fn logic_cores(&self) -> usize {
132        self.logic_cores
133    }
134
135    /// Getter for the field `num_cores`.
136    pub fn num_cores(&self) -> usize {
137        self.num_cores
138    }
139
140    /// Getter for [`crate::structs::CacheInfo`].
141    pub fn cache_info(&self) -> &CacheInfo {
142        &self.cache_info
143    }
144    /// Getter for the field `brand_string`.
145    pub fn brand_string(&self) -> &str {
146        &self.brand_string
147    }
148    /// Getter for the field `frequency`.
149    pub fn frequency(&self) -> usize {
150        self.frequency
151    }
152    /// Getter for the field `min_frequency`.
153    pub fn min_frequency(&self) -> usize {
154        self.min_frequency
155    }
156    /// Getter for the field `max_frequency`.
157    pub fn max_frequency(&self) -> usize {
158        self.max_frequency
159    }
160    /// Getter for the field `frequency` in Ghz.
161    pub fn frequency_ghz(&self) -> f64 {
162        self.frequency as f64 / 1E9_f64
163    }
164    /// Getter for the field `min_frequency` in Ghz.
165    pub fn min_frequency_ghz(&self) -> f64 {
166        self.min_frequency as f64 / 1E9_f64
167    }
168    /// Getter for the field `max_frequency` in Ghz.
169    pub fn max_frequency_ghz(&self) -> f64 {
170        self.max_frequency as f64 / 1E9_f64
171    }
172
173    /// Getter for the field `architecture`.
174    pub fn architecture(&self) -> &CpuArchitectureInfo {
175        &self.architecture
176    }
177}