mac_sys_info/structs/
cache_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//! Cache-Info (L1, L2, L3).
26
27use derive_more::Display as DeriveMoreDisplay;
28use serde::{Serialize};
29use std::collections::BTreeMap;
30use crate::error::MacSysInfoError;
31use crate::parse::{parse_sysctl_value, ParseAsType};
32use crate::generated_sysctl_keys::SysctlKey;
33
34/// Info about L1 (I & D), L2, and L3 cache.
35#[derive(Debug, Serialize, DeriveMoreDisplay)]
36#[display(fmt = "CacheInfo (\n\
37\x20    l1i_cache_size: {},\n\
38\x20    l1d_cache_size: {},\n\
39\x20    l2_cache_size: {},\n\
40\x20    l3_cache_size: {},\n\
41)", l1i_cache_size, l1d_cache_size, l2_cache_size, l3_cache_size)]
42pub struct CacheInfo {
43    /// Size of L1 instruction cache in byte.
44    l1i_cache_size: usize,
45    /// Size of L1 data cache in byte.
46    l1d_cache_size: usize,
47    /// Size of L2 in byte.
48    l2_cache_size: usize,
49    /// Size of L2 in byte.
50    l3_cache_size: usize,
51}
52
53impl CacheInfo {
54
55    pub(crate) fn new(sysinfo: &BTreeMap<String, String>) -> Result<Self, MacSysInfoError> {
56        let x = CacheInfo {
57            l1i_cache_size: parse_sysctl_value(
58                "l1i_cache_size",
59                SysctlKey::HwL1icachesize,
60                sysinfo,
61                ParseAsType::Usize)?
62                .get_usize(),
63            l1d_cache_size: parse_sysctl_value(
64                "l1d_cache_size",
65                SysctlKey::HwL1dcachesize,
66                sysinfo,
67                ParseAsType::Usize)?
68                .get_usize(),
69            l2_cache_size: parse_sysctl_value(
70                "l2_cache_size",
71                SysctlKey::HwL2cachesize,
72                sysinfo,
73                ParseAsType::Usize)?
74                .get_usize(),
75            l3_cache_size: parse_sysctl_value(
76                "l3_cache_size",
77                SysctlKey::HwL3cachesize,
78                sysinfo,
79                ParseAsType::Usize)?
80                .get_usize(),
81        };
82        Ok(x)
83    }
84
85    /// Size of L1 instruction cache in byte.
86    pub fn l1i_cache_size(&self) -> usize {
87        self.l1i_cache_size
88    }
89
90    /// Size of L1 data cache in byte.
91    pub fn l1d_cache_size(&self) -> usize {
92        self.l1d_cache_size
93    }
94
95    /// Size of L2 in byte.
96    pub fn l2_cache_size(&self) -> usize {
97        self.l2_cache_size
98    }
99
100    /// Size of L2 in byte.
101    pub fn l3_cache_size(&self) -> usize {
102        self.l3_cache_size
103    }
104
105    /// Size of L1 instruction cache in kibibyte.
106    pub fn l1i_cache_size_kb(&self) -> f64 {
107        self.l1i_cache_size as f64 / 1024_f64
108    }
109
110    /// Size of L1 data cache in kibibyte.
111    pub fn l1d_cache_size_kb(&self) -> f64 {
112        self.l1d_cache_size as f64 / 1024_f64
113    }
114
115    /// Size of L2 cache in kibibyte.
116    pub fn l2_cache_size_kb(&self) -> f64 {
117        self.l2_cache_size as f64 / 1024_f64
118    }
119
120    /// Size of L3 cache in kibibyte.
121    pub fn l3_cache_size_kb(&self) -> f64 {
122        self.l3_cache_size as f64 / 1024_f64
123    }
124}
125