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 by Mike Romberg ( romberg@fsl.noaa.gov )
3 // Copyright (c) 1995, 1996, 1997-2002 by Brian Grayson (bgrayson@netbsd.org)
4 //
5 // Most of this code was written by Werner Fink <werner@suse.de>.
6 // Only small changes were made on my part (M.R.)
7 // And the near-trivial port to NetBSD was done by Brian Grayson
8 //
9 // This file may be distributed under terms of the GPL or of the BSD
10 // license, whichever you choose. The full license notices are
11 // contained in the files COPYING.GPL and COPYING.BSD, which you
12 // should have received. If not, contact one of the xosview
13 // authors for a copy.
14 //
15 
16 #include "loadmeter.h"
17 #include "kernel.h"
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <iostream>
22 
23 
25  : FieldMeterGraph( parent, 2, "LOAD", "PROCS/MIN", 1, 1, 0 ) {
26  total_ = -1.0;
27 }
28 
30 }
31 
34 
35  procloadcol_ = parent_->allocColor( parent_->getResource("loadProcColor") );
36  warnloadcol_ = parent_->allocColor( parent_->getResource("loadWarnColor") );
37  critloadcol_ = parent_->allocColor( parent_->getResource("loadCritColor") );
38 
40  setfieldcolor( 1, parent_->getResource("loadIdleColor") );
41  priority_ = atoi( parent_->getResource("loadPriority") );
42  dodecay_ = parent_->isResourceTrue("loadDecay");
43  useGraph_ = parent_->isResourceTrue("loadGraph");
44  SetUsedFormat( parent_->getResource("loadUsedFormat") );
45  do_cpu_speed_ = parent_->isResourceTrue("loadCpuSpeed");
46 
47  const char *warn = parent_->getResource("loadWarnThreshold");
48  if (strncmp(warn, "auto", 2) == 0)
50  else
51  warnThreshold_ = atoi(warn);
52 
53  const char *crit = parent_->getResource("loadCritThreshold");
54  if (strncmp(crit, "auto", 2) == 0)
56  else
57  critThreshold_ = atoi(crit);
58 
60 
61  if (dodecay_) {
62  // Warning: Since the loadmeter changes scale occasionally, old
63  // decay values need to be rescaled. However, if they are rescaled,
64  // they could go off the edge of the screen. Thus, for now, to
65  // prevent this whole problem, the load meter can not be a decay
66  // meter. The load is a decaying average kind of thing anyway,
67  // so having a decaying load average is redundant.
68  std::cerr << "Warning: The loadmeter can not be configured as a decay\n"
69  << " meter. See the source code (" << __FILE__ << ") for further\n"
70  << " details.\n";
71  dodecay_ = 0;
72  }
73 }
74 
75 void LoadMeter::checkevent( void ) {
76  getloadinfo();
77 
78  if (do_cpu_speed_) {
81 
83  char l[25];
84  snprintf(l, 25, "PROCS/MIN %d MHz", cur_cpu_speed_);
85  legend(l);
86  drawlegend();
87  }
88  }
89  drawfields();
90 }
91 
92 void LoadMeter::getloadinfo( void ) {
93  double oneMinLoad;
94 
95  getloadavg(&oneMinLoad, 1); // Only get the 1-minute-average sample.
96  fields_[0] = oneMinLoad;
97 
98  if (fields_[0] < warnThreshold_)
99  alarmstate_ = 0;
100  else if (fields_[0] >= critThreshold_)
101  alarmstate_ = 2;
102  else
103  alarmstate_ = 1;
104 
105  if (alarmstate_ != lastalarmstate_) {
106  if (alarmstate_ == 0)
108  else if (alarmstate_ == 1)
110  else
112  drawlegend();
114  }
115 
116  // Adjust total to next power-of-two of the current load.
117  if ( (fields_[0]*5.0 < total_ && total_ > 1.0) || fields_[0] > total_ ) {
118  unsigned int i = fields_[0];
119  i |= i >> 1; i |= i >> 2; i |= i >> 4; i |= i >> 8; i |= i >> 16; // i = 2^n - 1
120  total_ = i + 1;
121  }
122 
123  fields_[1] = total_ - fields_[0];
124  setUsed(fields_[0], total_);
125 }
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 critThreshold_
Definition: loadmeter.h:37
int alarmstate_
Definition: loadmeter.h:37
int warnThreshold_
Definition: loadmeter.h:37
LoadMeter(XOSView *parent)
Definition: loadmeter.cc:24
void getloadinfo(void)
Definition: loadmeter.cc:92
int lastalarmstate_
Definition: loadmeter.h:37
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
bool do_cpu_speed_
Definition: loadmeter.h:39
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 isResourceTrue(const char *name)
Definition: xwin.h:89
const char * getResource(const char *name)
Definition: xwin.cc:362
int BSDCountCpus(void)
Definition: kernel.cc:1127
int BSDGetCPUSpeed()
Definition: kernel.cc:254