xosview  1.23
About: xosview is an X Windows based system monitor (cpu, memory, swap and network usage; interrupt and serial activities; load average).
  Fossies Dox: xosview-1.23.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

loadmeter.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 1994, 1995, 2006, 2008 by Mike Romberg ( mike.romberg@noaa.gov )
3 //
4 // This file may be distributed under terms of the GPL
5 //
6 // Most of this code was written by Werner Fink <werner@suse.de>.
7 // Only small changes were made on my part (M.R.)
8 //
9 
10 #include "loadmeter.h"
11 #include "cpumeter.h"
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <fstream>
16 #include <iostream>
17 #include <string>
18 
19 static const char LOADFILENAME[] = "/proc/loadavg";
20 static const char SPEEDFILENAME[] = "/proc/cpuinfo";
21 
22 
24  : FieldMeterGraph( parent, 2, "LOAD", "PROCS/MIN", 1, 1, 0 ){
25  lastalarmstate = -1;
26  total_ = 2.0;
27  old_cpu_speed_= cur_cpu_speed_=0;
28  do_cpu_speed = 0;
29 
30 }
31 
32 LoadMeter::~LoadMeter( void ){
33 }
34 
35 void LoadMeter::checkResources( void ){
37 
38  procloadcol_ = parent_->allocColor(parent_->getResource( "loadProcColor" ));
39  warnloadcol_ = parent_->allocColor(parent_->getResource( "loadWarnColor" ));
40  critloadcol_ = parent_->allocColor(parent_->getResource( "loadCritColor" ));
41 
43  setfieldcolor( 1, parent_->getResource( "loadIdleColor" ) );
44  priority_ = atoi (parent_->getResource( "loadPriority" ) );
45  useGraph_ = parent_->isResourceTrue( "loadGraph" );
46  dodecay_ = parent_->isResourceTrue( "loadDecay" );
47  SetUsedFormat (parent_->getResource("loadUsedFormat"));
48 
49  const char *warn = parent_->getResource("loadWarnThreshold");
50  if (strncmp(warn, "auto", 2) == 0) {
52  } else {
53  warnThreshold = atoi(warn);
54  }
55 
56  const char *crit = parent_->getResource("loadCritThreshold");
57  if (strncmp(crit, "auto", 2) == 0) {
59  } else {
60  critThreshold = atoi(crit);
61  }
62 
63  do_cpu_speed = parent_->isResourceTrue( "loadCpuSpeed" );
64 
65  if (dodecay_){
66  // Warning: Since the loadmeter changes scale occasionally, old
67  // decay values need to be rescaled. However, if they are rescaled,
68  // they could go off the edge of the screen. Thus, for now, to
69  // prevent this whole problem, the load meter can not be a decay
70  // meter. The load is a decaying average kind of thing anyway,
71  // so having a decaying load average is redundant.
72  std::cerr << "Warning: The loadmeter can not be configured as a decay\n"
73  << " meter. See the source code (" << __FILE__ << ") for further\n"
74  << " details.\n";
75  dodecay_ = 0;
76  }
77 }
78 
79 void LoadMeter::checkevent( void ){
80  getloadinfo();
81  if ( do_cpu_speed ) {
82  getspeedinfo();
83  if ( old_cpu_speed_ != cur_cpu_speed_ ) {
84  // update the legend:
85  char l[32];
86  snprintf(l, 32, "PROCS/MIN %d MHz", cur_cpu_speed_);
87  legend(l);
88  drawlegend();
89  }
90  }
91 
92  drawfields();
93 }
94 
95 
96 void LoadMeter::getloadinfo( void ){
97  std::ifstream loadinfo( LOADFILENAME );
98 
99  if ( !loadinfo ){
100  std::cerr <<"Can not open file : " <<LOADFILENAME << std::endl;
101  parent_->done(1);
102  return;
103  }
104 
105  loadinfo >> fields_[0];
106 
107  if ( fields_[0] < warnThreshold ) alarmstate = 0;
108  else
109  if ( fields_[0] >= critThreshold ) alarmstate = 2;
110  else
111  /* if fields_[0] >= warnThreshold */ alarmstate = 1;
112 
113  if ( alarmstate != lastalarmstate ){
114  if ( alarmstate == 0 ) setfieldcolor( 0, procloadcol_ );
115  else
116  if ( alarmstate == 1 ) setfieldcolor( 0, warnloadcol_ );
117  else
118  /* if alarmstate == 2 */ setfieldcolor( 0, critloadcol_ );
119  drawlegend();
121  }
122 
123  // Adjust total to next power-of-two of the current load.
124  if ( (fields_[0]*5.0 < total_ && total_ > 1.0) || fields_[0] > total_ ) {
125  unsigned int i = fields_[0];
126  i |= i >> 1; i |= i >> 2; i |= i >> 4; i |= i >> 8; i |= i >> 16; // i = 2^n - 1
127  total_ = i + 1;
128  }
129 
130  fields_[1] = (float) (total_ - fields_[0]);
131 
132  setUsed(fields_[0], (float) 1.0);
133 }
134 
135 // just check /proc/cpuinfo for the speed of cpu
136 // (average multi-cpus on different speeds)
137 // (yes - i know about devices/system/cpu/cpu*/cpufreq )
139  std::ifstream speedinfo(SPEEDFILENAME);
140  std::string line, val;
141  unsigned int total_cpu = 0, ncpus = 0;
142 
143  while ( speedinfo.good() ) {
144  std::getline(speedinfo, line);
145  if ( strncmp(line.c_str(), "cpu MHz", 7) == 0 ) {
146  val = line.substr(line.find_last_of(':') + 1);
147  XOSDEBUG("SPEED: %s\n", val.c_str());
148  total_cpu += atoi(val.c_str());
149  ncpus++;
150  }
151  }
152 
154  if (ncpus > 0)
155  cur_cpu_speed_ = total_cpu / ncpus;
156  else
157  cur_cpu_speed_ = 0;
158 }
static int countCPUs(void)
Definition: cpumeter.cc:300
virtual void drawfields(int mandatory=0)
virtual void checkResources(void)
double total_
Definition: fieldmeter.h:41
void setfieldcolor(int field, const char *color)
Definition: fieldmeter.cc:108
void drawlegend(void)
Definition: fieldmeter.cc:136
void setUsed(double val, double total)
Definition: fieldmeter.cc:76
double * fields_
Definition: fieldmeter.h:40
void SetUsedFormat(const char *const str)
Definition: fieldmeter.cc:60
unsigned long warnloadcol_
Definition: loadmeter.h:36
int warnThreshold
Definition: loadmeter.h:32
LoadMeter(XOSView *parent)
Definition: loadmeter.cc:24
void getloadinfo(void)
Definition: loadmeter.cc:92
void getspeedinfo(void)
Definition: loadmeter.cc:138
int alarmstate
Definition: loadmeter.h:32
void checkevent(void)
Definition: loadmeter.cc:75
unsigned long critloadcol_
Definition: loadmeter.h:36
void checkResources(void)
Definition: loadmeter.cc:32
int old_cpu_speed_
Definition: loadmeter.h:38
int do_cpu_speed
Definition: loadmeter.h:35
int lastalarmstate
Definition: loadmeter.h:32
int critThreshold
Definition: loadmeter.h:32
unsigned long procloadcol_
Definition: loadmeter.h:36
~LoadMeter(void)
Definition: loadmeter.cc:29
int cur_cpu_speed_
Definition: loadmeter.h:38
int priority_
Definition: meter.h:54
const char * legend(void)
Definition: meter.h:27
XOSView * parent_
Definition: meter.h:52
unsigned long allocColor(const char *name)
Definition: xwin.cc:383
int done(void)
Definition: xwin.h:29
int isResourceTrue(const char *name)
Definition: xwin.h:89
const char * getResource(const char *name)
Definition: xwin.cc:362
static const char SPEEDFILENAME[]
Definition: loadmeter.cc:20
static const char LOADFILENAME[]
Definition: loadmeter.cc:19
#define XOSDEBUG(...)
Definition: xosview.h:84