File: cpu_info.cpp

package info (click to toggle)
primesieve 7.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,080 kB
  • sloc: cpp: 7,270; ansic: 455; sh: 199; makefile: 86
file content (107 lines) | stat: -rw-r--r-- 2,306 bytes parent folder | download | duplicates (2)
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
///
/// @file   cpu_info.cpp
/// @brief  Detect the CPUs' cache sizes
///
/// Copyright (C) 2018 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <primesieve/CpuInfo.hpp>
#include <iostream>

using namespace std;
using namespace primesieve;

int main()
{
  const CpuInfo cpu;
  string error = cpu.getError();

  if (!error.empty())
  {
    cerr << "Error: " << error << endl;
    return 1;
  }

  if (!cpu.hasCpuCores() &&
      cpu.cpuCores() > 0)
  {
    cerr << "Invalid CPU cores: " << cpu.cpuCores() << endl;
    return 1;
  }

  if (!cpu.hasCpuThreads() &&
      cpu.cpuThreads() > 0)
  {
    cerr << "Invalid CPU threads: " << cpu.cpuThreads() << endl;
    return 1;
  }

  if (!cpu.hasL1Cache() &&
      cpu.l1CacheSize() > 0)
  {
    cerr << "Invalid L1 cache size: " << cpu.l1CacheSize() << endl;
    return 1;
  }

  if (!cpu.hasL2Cache() &&
      cpu.l2CacheSize() > 0)
  {
    cerr << "Invalid L2 cache size: " << cpu.l2CacheSize() << endl;
    return 1;
  }

  if (!cpu.hasL3Cache() &&
      cpu.l3CacheSize() > 0)
  {
    cerr << "Invalid L3 cache size: " << cpu.l3CacheSize() << endl;
    return 1;
  }

  if (!cpu.hasL1Sharing() &&
      cpu.l1Sharing() > 0)
  {
    cerr << "Invalid L1 cache sharing: " << cpu.l1Sharing() << endl;
    return 1;
  }

  if (!cpu.hasL2Sharing() &&
      cpu.l2Sharing() > 0)
  {
    cerr << "Invalid L2 cache sharing: " << cpu.l2Sharing() << endl;
    return 1;
  }

  if (!cpu.hasL3Sharing() &&
      cpu.l3Sharing() > 0)
  {
    cerr << "Invalid L3 cache sharing: " << cpu.l3Sharing() << endl;
    return 1;
  }

  if (!cpu.hasThreadsPerCore() &&
      cpu.threadsPerCore() > 0)
  {
    cerr << "Invalid threads per CPU core: " << cpu.threadsPerCore() << endl;
    return 1;
  }

  if (cpu.hasCpuName())
    cout << cpu.cpuName() << endl;

  cout << "L1 cache size: " << (cpu.l1CacheSize() >> 10) << " KiB" << endl;
  cout << "L2 cache size: " << (cpu.l2CacheSize() >> 10) << " KiB" << endl;
  cout << "L3 cache size: " << (cpu.l3CacheSize() >> 10) << " KiB" << endl;

  if (cpu.hasL2Cache())
  {
    if (cpu.hasPrivateL2Cache())
      cout << "L2 cache: private" << endl;
    else
      cout << "L2 cache: shared"  << endl;
  }

  return 0;
}