1 //! Example that displays information about the caches.
2 extern crate raw_cpuid;
3 use raw_cpuid::{CacheType, CpuId};
4 
main()5 fn main() {
6     let cpuid = CpuId::new();
7     cpuid.get_cache_parameters().map_or_else(
8         || println!("No cache parameter information available"),
9         |cparams| {
10             for cache in cparams {
11                 let size = cache.associativity()
12                     * cache.physical_line_partitions()
13                     * cache.coherency_line_size()
14                     * cache.sets();
15 
16                 let typ = match cache.cache_type() {
17                     CacheType::Data => "Instruction-Cache",
18                     CacheType::Instruction => "Data-Cache",
19                     CacheType::Unified => "Unified-Cache",
20                     _ => "Unknown cache type",
21                 };
22 
23                 let associativity = if cache.is_fully_associative() {
24                     format!("fully associative")
25                 } else {
26                     format!("{}-way associativity", cache.associativity())
27                 };
28 
29                 let size_repr = if size > 1024 * 1024 {
30                     format!("{} MiB", size / (1024 * 1024))
31                 } else {
32                     format!("{} KiB", size / 1024)
33                 };
34 
35                 let mapping = if cache.has_complex_indexing() {
36                     "hash-based-mapping"
37                 } else {
38                     "direct-mapped"
39                 };
40 
41                 println!(
42                     "L{} {}: ({}, {}, {})",
43                     cache.level(),
44                     typ,
45                     size_repr,
46                     associativity,
47                     mapping
48                 );
49             }
50         },
51     );
52 }
53