File: cpumeter.cc

package info (click to toggle)
xosview 1.24-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,184 kB
  • sloc: cpp: 11,975; makefile: 154; ansic: 32; awk: 13; sh: 8
file content (346 lines) | stat: -rw-r--r-- 11,623 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//
//  Copyright (c) 1994, 1995, 2002, 2006 by Mike Romberg ( mike.romberg@noaa.gov )
//
//  This file may be distributed under terms of the GPL
//

#include "cpumeter.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/utsname.h>
#include <string>
#include <iostream>
#include <fstream>

static const char STATFILENAME[] = "/proc/stat";
static int cputime_to_field[10] = { 0, 1, 2, 9, 5, 4, 3, 8, 6, 7 };

#define MAX_PROCSTAT_LENGTH 4096

CPUMeter::CPUMeter(XOSView *parent, const char *cpuID)
: FieldMeterGraph( parent, 10, toUpper(cpuID), "USR/NIC/SYS/SI/HI/WIO/GST/NGS/STL/IDLE" ) {
  _lineNum = findLine(cpuID);
  for ( int i = 0 ; i < 2 ; i++ )
    for ( int j = 0 ; j < 10 ; j++ )
      cputime_[i][j] = 0;
  cpuindex_ = 0;
  kernel_ = getkernelversion();
  if (kernel_ < 2006000)
    statfields_ = 4;
  else if (kernel_ < 2006011)
    statfields_ = 7;
  else if (kernel_ < 2006024)
    statfields_ = 8;
  else if (kernel_ < 2006032)
    statfields_ = 9;
  else
    statfields_ = 10;
}

CPUMeter::~CPUMeter( void ){
}

void CPUMeter::checkResources( void ){
  FieldMeterGraph::checkResources();

  unsigned long usercolor = parent_->allocColor(parent_->getResource( "cpuUserColor" ) );
  unsigned long nicecolor = parent_->allocColor(parent_->getResource( "cpuNiceColor" ) );
  unsigned long syscolor  = parent_->allocColor(parent_->getResource( "cpuSystemColor" ) );
  unsigned long sintcolor = parent_->allocColor(parent_->getResource( "cpuSInterruptColor" ) );
  unsigned long intcolor  = parent_->allocColor(parent_->getResource( "cpuInterruptColor" ) );
  unsigned long waitcolor = parent_->allocColor(parent_->getResource( "cpuWaitColor" ) );
  unsigned long gstcolor  = parent_->allocColor(parent_->getResource( "cpuGuestColor" ) );
  unsigned long ngstcolor = parent_->allocColor(parent_->getResource( "cpuNiceGuestColor" ) );
  unsigned long stealcolor= parent_->allocColor(parent_->getResource( "cpuStolenColor" ) );
  unsigned long idlecolor = parent_->allocColor(parent_->getResource( "cpuFreeColor" ) );

  priority_ = atoi(parent_->getResource( "cpuPriority" ) );
  dodecay_ = parent_->isResourceTrue( "cpuDecay" );
  useGraph_ = parent_->isResourceTrue( "cpuGraph" );
  SetUsedFormat(parent_->getResource("cpuUsedFormat") );

  /* Use user-defined fields.
   * Fields         Including if not its own field
   * --------------|------------------------------
   *   USED         all used time, including user and system times
   *     USR        user time, including nice and guest times
   *       NIC      niced time, including niced guest unless guest is present
   *       GST      guest time, including niced guest time
   *         NGS    niced guest time
   *     SYS        system time, including interrupt and stolen times
   *       INT      interrupt time, including soft and hard interrupt times
   *         HI     hard interrupt time
   *         SI     soft interrupt time
   *       STL      stolen time
   *   IDLE         idle time, including io wait time
   *     WIO        io wait time
   *
   * Stolen time is a class of its own in kernel scheduler, in cpufreq it is
   * considered used time. Here it is part of used and system time, but can be
   * separate field as well.
   * Idle field is always present.
   * Either USED or at least USR+SYS must be included.
   */

  const char *f = parent_->getResource( "cpuFields" );
  std::string lgnd, fields(f);
  int field = 0;

  /* Check for possible fields and define field mapping. Assign colors and
   * build legend on the way.
   */
  if (fields.find("USED") != fields.npos) { // USED = USR+NIC+SYS+SI+HI+GST+NGS(+STL)
    if (fields.find("USR") != fields.npos || fields.find("NIC") != fields.npos ||
        fields.find("SYS") != fields.npos || fields.find("INT") != fields.npos ||
        fields.find("HI")  != fields.npos || fields.find("SI")  != fields.npos ||
        fields.find("GST") != fields.npos || fields.find("NGS") != fields.npos) {
      std::cerr << "'USED' cannot be in cpuFields together with either 'USR', "
                << "'NIC', 'SYS', 'INT', 'HI', 'SI', 'GST' or 'NGS'." << std::endl;
      exit(1);
    }
    setfieldcolor(field, usercolor);
    if (kernel_ >= 2006000) // SI and HI
      cputime_to_field[5] = cputime_to_field[6] = field;
    if (kernel_ >= 2006024) // GST
      cputime_to_field[8] = field;
    if (kernel_ >= 2006032) // NGS
      cputime_to_field[9] = field;
    if (kernel_ >= 2006011 && fields.find("STL") == fields.npos)
      cputime_to_field[7] = field; // STL can be separate as well
    // USR, NIC and SYS
    cputime_to_field[0] = cputime_to_field[1] = cputime_to_field[2] = field++;
    lgnd = "USED";
  }
  if (fields.find("USR") != fields.npos) {
    setfieldcolor(field, usercolor);
    // add NIC if not on its own
    if (fields.find("NIC") == fields.npos)
      cputime_to_field[1] = field;
    // add GST if not on its own
    if (kernel_ >= 2006024 && fields.find("GST") == fields.npos)
      cputime_to_field[8] = field;
    // add NGS if not on its own and neither NIC or GST is present
    if (kernel_ >= 2006032 && fields.find("NGS") == fields.npos &&
        fields.find("NIC") == fields.npos && fields.find("GST") == fields.npos)
      cputime_to_field[9] = field;
    cputime_to_field[0] = field++;
    lgnd = "USR";
  }
  else {
    if (fields.find("USED") == fields.npos) {
      std::cerr << "Either 'USED' or 'USR' is mandatory in cpuFields." << std::endl;
      exit(1);
    }
  }
  if (fields.find("NIC") != fields.npos) {
    setfieldcolor(field, nicecolor);
    // add NGS if not on its own and GST is not present
    if (kernel_ >= 2006032 && fields.find("NGS") == fields.npos &&
        fields.find("GST") == fields.npos)
      cputime_to_field[9] = field;
    cputime_to_field[1] = field++;
    lgnd += "/NIC";
  }
  if (fields.find("SYS") != fields.npos) {
    setfieldcolor(field, syscolor);
    // add SI if not on its own and INT is not present
    if (kernel_ >= 2006000 && fields.find("SI") == fields.npos &&
        fields.find("INT") == fields.npos)
      cputime_to_field[6] = field;
    // add HI if not on its own and INT is not present
    if (kernel_ >= 2006000 && fields.find("HI") == fields.npos &&
        fields.find("INT") == fields.npos)
      cputime_to_field[5] = field;
    // add STL if not on its own
    if (kernel_ >= 2006011 && fields.find("STL") == fields.npos)
      cputime_to_field[7] = field;
    cputime_to_field[2] = field++;
    lgnd += "/SYS";
  }
  else {
    if (fields.find("USED") == fields.npos) {
      std::cerr << "Either 'USED' or 'SYS' is mandatory in cpuFields." << std::endl;
      exit(1);
    }
  }
  if (kernel_ >= 2006000) {
    if (fields.find("INT") != fields.npos) { // combine soft and hard interrupt times
      setfieldcolor(field, intcolor);
      cputime_to_field[5] = cputime_to_field[6] = field++;
      lgnd += "/INT";
    } // Maybe should warn if both INT and HI/SI are requested ???
    else { // separate soft and hard interrupt times
      if (fields.find("SI") != fields.npos) {
        setfieldcolor(field, sintcolor);
        cputime_to_field[5] = field++;
        lgnd += "/SI";
      }
      if (fields.find("HI") != fields.npos) {
        setfieldcolor(field, intcolor);
        cputime_to_field[6] = field++;
        lgnd += "/HI";
      }
    }
    if (fields.find("WIO") != fields.npos) {
      setfieldcolor(field, waitcolor);
      cputime_to_field[4] = field++;
      lgnd += "/WIO";
    }
    if (kernel_ >= 2006024 && fields.find("GST") != fields.npos) {
      setfieldcolor(field, gstcolor);
      // add NGS if not on its own
      if (kernel_ >= 2006032 && fields.find("NGS") == fields.npos)
        cputime_to_field[9] = field;
      cputime_to_field[8] = field++;
      lgnd += "/GST";
    }
    if (kernel_ >= 2006032 && fields.find("NGS") != fields.npos) {
      setfieldcolor(field, ngstcolor);
      cputime_to_field[9] = field++;
      lgnd += "/NGS";
    }
    if (kernel_ >= 2006011 && fields.find("STL") != fields.npos) {
      setfieldcolor(field, stealcolor);
      cputime_to_field[7] = field++;
      lgnd += "/STL";
    }
  }
  // always add IDLE field
  setfieldcolor(field, idlecolor);
  // add WIO if not on its own
  if (kernel_ >= 2006000 && fields.find("WIO") == fields.npos)
    cputime_to_field[4] = field;
  cputime_to_field[3] = field++;
  lgnd += "/IDLE";

  legend(lgnd.c_str());
  numfields_ = field; // can't use setNumFields as it destroys the color mapping
}

void CPUMeter::checkevent( void ){
  getcputime();
  drawfields();
}

void CPUMeter::getcputime( void ){
  total_ = 0;
  std::string tmp;
  std::ifstream stats( STATFILENAME );
  char *end = NULL;

  if ( !stats ){
    std::cerr <<"Can not open file : " <<STATFILENAME << std::endl;
    exit( 1 );
  }

  // read until we are at the right line.
  for (int i = 0 ; i < _lineNum ; i++) {
    if (stats.eof())
      return;
    stats.ignore(1024, '\n');
  }
  std::getline(stats, tmp);

  int col = 0;
  std::string l = tmp.substr(tmp.find_first_of(' ') + 1);
  const char *line = l.c_str();
  while (*line) {
    cputime_[cpuindex_][col++] = strtoull(line, &end, 10);
    line = end;
  }

  // Guest time already included in user time.
  cputime_[cpuindex_][0] -= cputime_[cpuindex_][8];
  // Same applies to niced guest time.
  cputime_[cpuindex_][1] -= cputime_[cpuindex_][9];

  int oldindex = (cpuindex_+1)%2;
  // zero all the fields
  memset(fields_, 0, numfields_*sizeof(fields_[0]));
  for ( int i = 0 ; i < statfields_ ; i++ ){
    int time = cputime_[cpuindex_][i] - cputime_[oldindex][i];
    if (time < 0)    // counters in /proc/stat do sometimes go backwards
      time = 0;
    fields_[cputime_to_field[i]] += time;
    total_ += time;
//     XOSDEBUG("cputime_[%d] = %2d  fields_[%d] = %d\n", i, time, cputime_to_field[i], (int)fields_[cputime_to_field[i]]);
  }

  if (total_){
    setUsed (total_ - fields_[numfields_ - 1], total_); // any non-idle time
    cpuindex_ = (cpuindex_ + 1) % 2;
  }
}

int CPUMeter::findLine(const char *cpuID){
  std::ifstream stats( STATFILENAME );

  if ( !stats ){
    std::cerr <<"Can not open file : " <<STATFILENAME << std::endl;
    exit( 1 );
  }

  int line = -1;
  std::string buf;
  while (!stats.eof()){
    getline(stats, buf);
    if (!stats.eof()){
      line++;
      if (!strncmp(cpuID, buf.data(), strlen(cpuID))
        && buf[strlen(cpuID)] == ' ')
        return line;
    }
  }
  return -1;
}

// Returns the number of cpus that are on this machine.
int CPUMeter::countCPUs(void){
  std::ifstream stats( STATFILENAME );

  if ( !stats ){
    std::cerr <<"Can not open file : " <<STATFILENAME << std::endl;
    exit( 1 );
  }

  int cpuCount = 0;
  std::string buf;
  while (getline(stats, buf))
      if (!strncmp(buf.data(), "cpu", 3) && buf[3] != ' ')
          cpuCount++;

  return cpuCount;
}

const char *CPUMeter::cpuStr(int num){
  static char buffer[32];

  if (num != 0)
    snprintf(buffer, sizeof(buffer), "cpu%d", num - 1);
  else
    strcpy(buffer, "cpu");

  return buffer;
}

const char *CPUMeter::toUpper(const char *str){
  static char buffer[MAX_PROCSTAT_LENGTH + 1];
  snprintf(buffer, MAX_PROCSTAT_LENGTH + 1, "%s", str);
  buffer[MAX_PROCSTAT_LENGTH] = 0;
  for (char *tmp = buffer ; *tmp != '\0' ; tmp++)
    *tmp = toupper(*tmp);

  return buffer;
}

int CPUMeter::getkernelversion(void){
  static int major = 0, minor = 0, micro = 0;
  if (!major) {
    struct utsname myosrelease;
    uname(&myosrelease);
    sscanf(myosrelease.release, "%d.%d.%d", &major, &minor, &micro);
  }
  return (major*1000000 + minor*1000 + micro);
}