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
#![deny(warnings)]

#[derive(Debug, Clone, Copy)]
pub struct CpuInfo {
    pub l1_cache_size: usize,
    pub l2_cache_size: usize,
    pub l3_cache_size: usize,
    pub num_cpus: usize,
}

impl CpuInfo {
    #[cfg(target_os = "linux")]
    pub fn try_new() -> Result<CpuInfo, ()> {
        use libc::sysconf;

        unsafe {
            let l1_cache_size = sysconf(libc::_SC_LEVEL1_DCACHE_SIZE);
            if l1_cache_size <= 0 {
                return Err(());
            }

            let l2_cache_size = sysconf(libc::_SC_LEVEL2_CACHE_SIZE);
            if l2_cache_size <= 0 {
                return Err(());
            }

            let l3_cache_size = sysconf(libc::_SC_LEVEL3_CACHE_SIZE);
            if l3_cache_size <= 0 {
                return Err(());
            }

            let num_cpus = sysconf(libc::_SC_NPROCESSORS_CONF);
            if num_cpus <= 0 {
                return Err(());
            }

            Ok(CpuInfo {
                l1_cache_size: l1_cache_size as usize,
                l2_cache_size: l2_cache_size as usize,
                l3_cache_size: l3_cache_size as usize,
                num_cpus: num_cpus as usize,
            })
        }
    }

    #[cfg(target_os = "macos")]
    pub fn try_new() -> Result<CpuInfo, ()> {
        use std::{mem, ptr};
        use libc::{c_int, c_void, sysconf, sysctl};

        unsafe {
            let mut l1_cache_size: c_int = 0;
            let mut l2_cache_size: c_int = 0;
            let mut l3_cache_size: c_int = 0;
            let mut size = mem::size_of::<c_int>();

            const NAMELEN: libc::c_uint = 2;

            let mut name = [libc::CTL_HW, libc::HW_L1DCACHESIZE];
            if sysctl(
                name.as_mut_ptr(),
                NAMELEN,
                &mut l1_cache_size as *mut c_int as *mut c_void,
                &mut size,
                ptr::null_mut(),
                0,
            ) != 0
            {
                return Err(());
            }

            name[1] = libc::HW_L2CACHESIZE;
            if sysctl(
                name.as_mut_ptr(),
                NAMELEN,
                &mut l2_cache_size as *mut c_int as *mut c_void,
                &mut size,
                ptr::null_mut(),
                0,
            ) != 0
            {
                return Err(());
            }

            name[1] = libc::HW_L3CACHESIZE;
            if sysctl(
                name.as_mut_ptr(),
                NAMELEN,
                &mut l3_cache_size as *mut c_int as *mut c_void,
                &mut size,
                ptr::null_mut(),
                0,
            ) != 0
            {
                return Err(());
            }

            if l1_cache_size <= 0 || l2_cache_size <= 0 || l3_cache_size <= 0 {
                return Err(());
            }

            let num_cpus = sysconf(libc::_SC_NPROCESSORS_CONF);
            if num_cpus <= 0 {
                return Err(());
            }

            Ok(CpuInfo {
                l1_cache_size: l1_cache_size as usize,
                l2_cache_size: l2_cache_size as usize,
                l3_cache_size: l3_cache_size as usize,
                num_cpus: num_cpus as usize,
            })
        }
    }

    #[cfg(target_os = "windows")]
    pub fn try_new() -> Result<CpuInfo, ()> {
        use std::{mem, ptr};
        use winapi::shared::minwindef::{FALSE, TRUE};
        use winapi::shared::winerror::ERROR_INSUFFICIENT_BUFFER;
        use winapi::um::errhandlingapi::GetLastError;
        use winapi::um::sysinfoapi::GetLogicalProcessorInformation;
        use winapi::um::winnt::{
            CacheData, RelationCache, RelationProcessorCore, SYSTEM_LOGICAL_PROCESSOR_INFORMATION,
        };

        unsafe {
            let mut buffer = Vec::new();
            let mut req_bytes = 0;
            let struct_size = mem::size_of::<SYSTEM_LOGICAL_PROCESSOR_INFORMATION>();

            if TRUE == GetLogicalProcessorInformation(ptr::null_mut(), &mut req_bytes) {
                return Err(());
            }

            if GetLastError() == ERROR_INSUFFICIENT_BUFFER {
                buffer.reserve(req_bytes as usize / struct_size);
                buffer.set_len(req_bytes as usize / struct_size);
            } else {
                return Err(());
            }

            if FALSE == GetLogicalProcessorInformation(buffer.as_mut_ptr(), &mut req_bytes) {
                return Err(());
            }

            let mut l1_cache_size = 0;
            let mut l2_cache_size = 0;
            let mut l3_cache_size = 0;
            let mut num_cpus = 0;

            for info in buffer {
                if info.Relationship == RelationCache {
                    let cache = info.u.Cache();
                    match cache.Level {
                        1 => {
                            if cache.Type == CacheData {
                                l1_cache_size = cache.Size
                            }
                        }
                        2 => l2_cache_size = cache.Size,
                        3 => l3_cache_size = cache.Size,
                        _ => {
                            return Err(());
                        }
                    }
                } else if info.Relationship == RelationProcessorCore {
                    num_cpus += 1;
                }
            }

            if l1_cache_size == 0
                || l2_cache_size == 0
                || l3_cache_size == 0
                || num_cpus == 0 {
                    return Err(());
            }

            Ok(CpuInfo {
                l1_cache_size: l1_cache_size as usize,
                l2_cache_size: l2_cache_size as usize,
                l3_cache_size: l3_cache_size as usize,
                num_cpus,
            })
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_create_cpu_info() {
        let _info = CpuInfo::try_new().unwrap();
    }
}