File: cpuinfometer.cc

package info (click to toggle)
sinfo 0.0.48-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,332 kB
  • sloc: sh: 11,213; cpp: 6,722; makefile: 271; xml: 151; perl: 149
file content (185 lines) | stat: -rw-r--r-- 3,841 bytes parent folder | download | duplicates (3)
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
#include <stdio.h>


#ifdef __sun__
#include <errno.h>
#include <kstat.h>
#include <string.h>
#include <sys/sysinfo.h>
#include <sys/stat.h>
#include <sys/swap.h>
#endif

#ifdef __FreeBSD__
#include <fcntl.h>
#include <kvm.h>
#endif

#include <iostream>
#include <string.h>

#include "cpuinfometer.h"


bool getCpuinfo(Cpuinfo & cpuinfo)
{
  cpuinfo.cpus = 0;
  cpuinfo.speedmhz = 0;


#ifdef __linux__
  FILE *fcinfo;
  if ( (fcinfo = fopen("/proc/cpuinfo", "r") ) != NULL)
  {
#ifdef CPUNO_ADJUST
    int siblings = 1;
    int cores = 1;
#endif
    while (1)
    {
      char line[401];
      int fsret = fscanf(fcinfo, "%400[^\n]\n",line);
      if (fsret == EOF)
        break;

      char entry[201];
      char value[201];
      fsret = sscanf(line, "%200[^\t:]%*[\t: ]%200[^\n]", entry, value);

//        cout << "entry \"" << entry << "\"" << endl
//             << " value \"" << value << "\"" << endl;

      if (fsret == 2)
      {
        // interpret the fscanned data
        if (strcmp(entry, "processor") == 0)
        {
          cpuinfo.cpus++;
        }

        if (strcmp(entry, "cpu MHz") == 0)
        {
          sscanf(value, "%f", &cpuinfo.speedmhz);
        }

#ifdef CPUNO_ADJUST
        if (strcmp(entry, "siblings") == 0)
        {
          sscanf(value, "%d", &siblings);
        }
        if (strcmp(entry, "cpu cores") == 0)
        {
          sscanf(value, "%d", &cores);
        }
#endif

        // for  PowerPC970, contributed by Francois Thomas <FT@fr.ibm.com>
        if (strcmp(entry, "clock") == 0)
        {
          sscanf(value, "%fMHz", &cpuinfo.speedmhz);
        }
      }
    }

#ifdef CPUNO_ADJUST
    cpuinfo.cpus = cpuinfo.cpus * cores / siblings;
#endif
    fclose(fcinfo);
    return true;
  }
#endif



#ifdef __FreeBSD__

  // found hint to the kernal-variabel "tsc_freq" in
  //   /usr/src/sys/i386/i386/identcpu.c
  // thats part of the kernel startup function

  kvm_t *kd;

  if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL)
  {
    fprintf(stderr, "kvm_open failed\n");
    return false;
  }

  struct nlist nlst[] =
  {
#define X_TSC_FREQ  0
    { "_tsc_freq" },
    { 0 }
  };

  kvm_nlist(kd, nlst);
  if (nlst[0].n_type == 0)
  {
    std::cerr << "kvm_nlist failed: " << std::endl;
    return false;
  }

  unsigned int mytsc_freq;
  if (kvm_read(kd, nlst[X_TSC_FREQ].n_value,
               &mytsc_freq, sizeof(mytsc_freq)) != sizeof(mytsc_freq))
  {
    std::cerr << "kvm_read failed: " << kvm_geterr(kd) << std::endl;
    return false;
  }

  kvm_close(kd);


  cpuinfo.cpus = 1;
  // FIXME: I dont have acces to a multi processor FreeBSD system
  // so Ill assume, that there is at least one CPU ....

  std::cout << "mytsc_freq= " << (mytsc_freq + 4999.) / 1e6 << std::endl;
  cpuinfo.speedmhz = (mytsc_freq + 4999.) / 1e6;

  return true;
#endif


#ifdef __sun__
  struct kstat_ctl *kc;
  if ((kc = kstat_open()) == 0)
  {
    fprintf(stderr, "Sorry, I cannot initialise the `kstat' library.\n"
            "This library is used for accessing kernel information.\n"
            "The diagnostics are: `%s'.\n\n", strerror(errno));
  }
  else
  {

    // CPUS zhlen
    for (kstat_t * ksp = kc->kc_chain; ksp != 0; ksp = ksp->ks_next)
    {
      if (strncmp(ksp->ks_name, "cpu_info", 8) == 0)
      {
        cpuinfo.cpus++;

        if (kstat_read(kc, ksp, 0) != -1)
        {
          kstat_named_t *kn = (kstat_named_t *)ksp->ks_data;
          for (int i = 0; i < (int) ksp->ks_ndata; i++)
          {
            std::cout << kn->name << std::endl;
            if (strcmp(kn->name, "clock_MHz") == 0)
            {
              cpuinfo.speedmhz = kn->value.ul;
              break;
            }
            kn++;
          }
        }
      }
    }
    if (kc != 0)
      kstat_close(kc);
    return true;
  }
#endif

  return false;
}