cpuinfo.rs.html -- source
Description: Source to the Rust file `libstd/../stdsimd/stdsimd/arch/detect/os/linux/cpuinfo.rs`.
Keywords: rust, rustlang, rust-lang

      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
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302

    //! Parses /proc/cpuinfo
    #![cfg_attr(not(target_arch = "arm"), allow(dead_code))]

    use prelude::v1::*;
    use fs::File;
    use io::{self, Read};

    /// cpuinfo
    pub(crate) struct CpuInfo {
        raw: String,
    }

    impl CpuInfo {
        /// Reads /proc/cpuinfo into CpuInfo.
        pub(crate) fn new() -> Result<Self, io::Error> {
            let mut file = File::open("/proc/cpuinfo")?;
            let mut cpui = Self { raw: String::new() };
            file.read_to_string(&mut cpui.raw)?;
            Ok(cpui)
        }
        /// Returns the value of the cpuinfo `field`.
        pub(crate) fn field(&self, field: &str) -> CpuInfoField {
            for l in self.raw.lines() {
                if l.trim().starts_with(field) {
                    return CpuInfoField::new(l.split(": ").nth(1));
                }
            }
            CpuInfoField(None)
        }

        /// Returns the `raw` contents of `/proc/cpuinfo`
        #[cfg(test)]
        fn raw(&self) -> &String {
            &self.raw
        }

        #[cfg(test)]
        fn from_str(other: &str) -> Result<Self, ::std::io::Error> {
            Ok(Self {
                raw: String::from(other),
            })
        }
    }

    /// Field of cpuinfo
    #[derive(Debug)]
    pub(crate) struct CpuInfoField<'a>(Option<&'a str>);

    impl<'a> PartialEq<&'a str> for CpuInfoField<'a> {
        fn eq(&self, other: &&'a str) -> bool {
            match self.0 {
                None => other.is_empty(),
                Some(f) => f == other.trim(),
            }
        }
    }

    impl<'a> CpuInfoField<'a> {
        pub(crate) fn new<'b>(v: Option<&'b str>) -> CpuInfoField<'b> {
            match v {
                None => CpuInfoField::<'b>(None),
                Some(f) => CpuInfoField::<'b>(Some(f.trim())),
            }
        }
        /// Does the field exist?
        #[cfg(test)]
        pub(crate) fn exists(&self) -> bool {
            self.0.is_some()
        }
        /// Does the field contain `other`?
        pub(crate) fn has(&self, other: &str) -> bool {
            match self.0 {
                None => other.is_empty(),
                Some(f) => {
                    let other = other.trim();
                    for v in f.split(' ') {
                        if v == other {
                            return true;
                        }
                    }
                    false
                }
            }
        }
    }

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

        #[test]
        fn raw_dump() {
            let cpuinfo = CpuInfo::new().unwrap();
            if cpuinfo.field("vendor_id") == "GenuineIntel" {
                assert!(cpuinfo.field("flags").exists());
                assert!(!cpuinfo.field("vendor33_id").exists());
                assert!(cpuinfo.field("flags").has("sse"));
                assert!(!cpuinfo.field("flags").has("avx314"));
            }
            println!("{}", cpuinfo.raw());
        }

        const CORE_DUO_T6500: &str = r"processor       : 0
    vendor_id       : GenuineIntel
    cpu family      : 6
    model           : 23
    model name      : Intel(R) Core(TM)2 Duo CPU     T6500  @ 2.10GHz
    stepping        : 10
    microcode       : 0xa0b
    cpu MHz         : 1600.000
    cache size      : 2048 KB
    physical id     : 0
    siblings        : 2
    core id         : 0
    cpu cores       : 2
    apicid          : 0
    initial apicid  : 0
    fdiv_bug        : no
    hlt_bug         : no
    f00f_bug        : no
    coma_bug        : no
    fpu             : yes
    fpu_exception   : yes
    cpuid level     : 13
    wp              : yes
    flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts aperfmperf pni dtes64 monitor ds_cpl est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm dtherm
    bogomips        : 4190.43
    clflush size    : 64
    cache_alignment : 64
    address sizes   : 36 bits physical, 48 bits virtual
    power management:
    ";

        #[test]
        fn core_duo_t6500() {
            let cpuinfo = CpuInfo::from_str(CORE_DUO_T6500).unwrap();
            assert_eq!(cpuinfo.field("vendor_id"), "GenuineIntel");
            assert_eq!(cpuinfo.field("cpu family"), "6");
            assert_eq!(cpuinfo.field("model"), "23");
            assert_eq!(
                cpuinfo.field("model name"),
                "Intel(R) Core(TM)2 Duo CPU     T6500  @ 2.10GHz"
            );
            assert_eq!(
                cpuinfo.field("flags"),
                "fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts aperfmperf pni dtes64 monitor ds_cpl est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm dtherm"
            );
            assert!(cpuinfo.field("flags").has("fpu"));
            assert!(cpuinfo.field("flags").has("dtherm"));
            assert!(cpuinfo.field("flags").has("sse2"));
            assert!(!cpuinfo.field("flags").has("avx"));
        }

        const ARM_CORTEX_A53: &str =
            r"Processor   : AArch64 Processor rev 3 (aarch64)
            processor   : 0
            processor   : 1
            processor   : 2
            processor   : 3
            processor   : 4
            processor   : 5
            processor   : 6
            processor   : 7
            Features    : fp asimd evtstrm aes pmull sha1 sha2 crc32
            CPU implementer : 0x41
            CPU architecture: AArch64
            CPU variant : 0x0
            CPU part    : 0xd03
            CPU revision    : 3

            Hardware    : HiKey Development Board
            ";

        #[test]
        fn arm_cortex_a53() {
            let cpuinfo = CpuInfo::from_str(ARM_CORTEX_A53).unwrap();
            assert_eq!(
                cpuinfo.field("Processor"),
                "AArch64 Processor rev 3 (aarch64)"
            );
            assert_eq!(
                cpuinfo.field("Features"),
                "fp asimd evtstrm aes pmull sha1 sha2 crc32"
            );
            assert!(cpuinfo.field("Features").has("pmull"));
            assert!(!cpuinfo.field("Features").has("neon"));
            assert!(cpuinfo.field("Features").has("asimd"));
        }

        const ARM_CORTEX_A57: &str = r"Processor	: Cortex A57 Processor rev 1 (aarch64)
    processor	: 0
    processor	: 1
    processor	: 2
    processor	: 3
    Features	: fp asimd aes pmull sha1 sha2 crc32 wp half thumb fastmult vfp edsp neon vfpv3 tlsi vfpv4 idiva idivt
    CPU implementer	: 0x41
    CPU architecture: 8
    CPU variant	: 0x1
    CPU part	: 0xd07
    CPU revision	: 1";

        #[test]
        fn arm_cortex_a57() {
            let cpuinfo = CpuInfo::from_str(ARM_CORTEX_A57).unwrap();
            assert_eq!(
                cpuinfo.field("Processor"),
                "Cortex A57 Processor rev 1 (aarch64)"
            );
            assert_eq!(
                cpuinfo.field("Features"),
                "fp asimd aes pmull sha1 sha2 crc32 wp half thumb fastmult vfp edsp neon vfpv3 tlsi vfpv4 idiva idivt"
            );
            assert!(cpuinfo.field("Features").has("pmull"));
            assert!(cpuinfo.field("Features").has("neon"));
            assert!(cpuinfo.field("Features").has("asimd"));
        }

        const POWER8E_POWERKVM: &str = r"processor       : 0
    cpu             : POWER8E (raw), altivec supported
    clock           : 3425.000000MHz
    revision        : 2.1 (pvr 004b 0201)

    processor       : 1
    cpu             : POWER8E (raw), altivec supported
    clock           : 3425.000000MHz
    revision        : 2.1 (pvr 004b 0201)

    processor       : 2
    cpu             : POWER8E (raw), altivec supported
    clock           : 3425.000000MHz
    revision        : 2.1 (pvr 004b 0201)

    processor       : 3
    cpu             : POWER8E (raw), altivec supported
    clock           : 3425.000000MHz
    revision        : 2.1 (pvr 004b 0201)

    timebase        : 512000000
    platform        : pSeries
    model           : IBM pSeries (emulated by qemu)
    machine         : CHRP IBM pSeries (emulated by qemu)";

        #[test]
        fn power8_powerkvm() {
            let cpuinfo = CpuInfo::from_str(POWER8E_POWERKVM).unwrap();
            assert_eq!(cpuinfo.field("cpu"), "POWER8E (raw), altivec supported");

            assert!(cpuinfo.field("cpu").has("altivec"));
        }

        const POWER5P: &str = r"processor       : 0
    cpu             : POWER5+ (gs)
    clock           : 1900.098000MHz
    revision        : 2.1 (pvr 003b 0201)

    processor       : 1
    cpu             : POWER5+ (gs)
    clock           : 1900.098000MHz
    revision        : 2.1 (pvr 003b 0201)

    processor       : 2
    cpu             : POWER5+ (gs)
    clock           : 1900.098000MHz
    revision        : 2.1 (pvr 003b 0201)

    processor       : 3
    cpu             : POWER5+ (gs)
    clock           : 1900.098000MHz
    revision        : 2.1 (pvr 003b 0201)

    processor       : 4
    cpu             : POWER5+ (gs)
    clock           : 1900.098000MHz
    revision        : 2.1 (pvr 003b 0201)

    processor       : 5
    cpu             : POWER5+ (gs)
    clock           : 1900.098000MHz
    revision        : 2.1 (pvr 003b 0201)

    processor       : 6
    cpu             : POWER5+ (gs)
    clock           : 1900.098000MHz
    revision        : 2.1 (pvr 003b 0201)

    processor       : 7
    cpu             : POWER5+ (gs)
    clock           : 1900.098000MHz
    revision        : 2.1 (pvr 003b 0201)

    timebase        : 237331000
    platform        : pSeries
    machine         : CHRP IBM,9133-55A";

        #[test]
        fn power5p() {
            let cpuinfo = CpuInfo::from_str(POWER5P).unwrap();
            assert_eq!(cpuinfo.field("cpu"), "POWER5+ (gs)");

            assert!(!cpuinfo.field("cpu").has("altivec"));
        }
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)