File: cpumeter.cc

package info (click to toggle)
xosview 1.7.3-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,000 kB
  • ctags: 1,200
  • sloc: cpp: 7,317; sh: 1,760; ansic: 368; makefile: 46; awk: 20
file content (146 lines) | stat: -rw-r--r-- 3,487 bytes parent folder | download
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
//  
//  Copyright (c) 1994, 1995 by Mike Romberg ( romberg@fsl.noaa.gov )
//
//  This file may be distributed under terms of the GPL
//
//
// $Id: cpumeter.cc,v 1.9 1999/02/24 22:16:20 romberg Exp $
//
#include "cpumeter.h"
#include "xosview.h"
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
#include <strstream.h>
#include <ctype.h>

static const char STATFILENAME[] = "/proc/stat";

CPUMeter::CPUMeter(XOSView *parent, const char *cpuID)
: FieldMeterGraph( parent, 4, toUpper(cpuID), "USR/NICE/SYS/FREE" ) {
  _lineNum = findLine(cpuID);
  for ( int i = 0 ; i < 2 ; i++ )
    for ( int j = 0 ; j < 4 ; j++ )
      cputime_[i][j] = 0;
  cpuindex_ = 0;

}

CPUMeter::~CPUMeter( void ){
}

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

  setfieldcolor( 0, parent_->getResource( "cpuUserColor" ) );
  setfieldcolor( 1, parent_->getResource( "cpuNiceColor" ) );
  setfieldcolor( 2, parent_->getResource( "cpuSystemColor" ) );
  setfieldcolor( 3, parent_->getResource( "cpuFreeColor" ) );
  priority_ = atoi (parent_->getResource( "cpuPriority" ) );
  dodecay_ = parent_->isResourceTrue( "cpuDecay" );
  useGraph_ = parent_->isResourceTrue( "cpuGraph" );
  SetUsedFormat (parent_->getResource("cpuUsedFormat"));
}

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

void CPUMeter::getcputime( void ){
  total_ = 0;
  char tmp[1024];
  ifstream stats( STATFILENAME );

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

  // read until we are at the right line.
  for (int i = 0 ; i < _lineNum ; i++)
    stats.getline(tmp, 1024);

  stats >>tmp >>cputime_[cpuindex_][0]  
	      >>cputime_[cpuindex_][1]  
	      >>cputime_[cpuindex_][2]  
	      >>cputime_[cpuindex_][3];

  int oldindex = (cpuindex_+1)%2;
  for ( int i = 0 ; i < 4 ; i++ ){
    fields_[i] = cputime_[cpuindex_][i] - cputime_[oldindex][i];
    total_ += fields_[i];
  }

  if (total_){
    setUsed (total_ - fields_[3], total_);
    cpuindex_ = (cpuindex_ + 1) % 2;
  }
}

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

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

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

// Checks for the SMP kernel patch by forissier@isia.cma.fr.  
// http://www-isia.cma.fr/~forissie/smp_kernel_patch/
// If it finds that this patch has been applied to the current kernel 
// then returns the number of cpus that are on this machine.
int CPUMeter::countCPUs(void){
  ifstream stats( STATFILENAME );

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

  int cpuCount = 0;
  char buf[1024];
  while (!stats.eof()){
    stats.getline(buf, 1024);
    if (!stats.eof()){
      if (!strncmp(buf, "cpu", 3) && buf[3] != ' ')
          cpuCount++;
    }
  }

  return cpuCount;
}

const char *CPUMeter::cpuStr(int num){
  static char buffer[32];
  ostrstream str(buffer, 32);
  
  str << "cpu";
  if (num != 0)
    str << (num - 1);
  str << ends;

  return buffer;
}

const char *CPUMeter::toUpper(const char *str){
  static char buffer[1024];
  strncpy(buffer, str, 1024);
  for (char *tmp = buffer ; *tmp != '\0' ; tmp++)
    *tmp = toupper(*tmp);

  return buffer;
}