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 // Initial port performed by Greg Onufer (exodus@cheers.bungi.com)
3 //
4 
5 #include "loadmeter.h"
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <iostream>
11 #ifdef NO_GETLOADAVG
12 #ifndef FSCALE
13 #define FSCALE (1<<8)
14 #endif
15 #else
16 #include <sys/loadavg.h>
17 #endif
18 
19 
20 LoadMeter::LoadMeter(XOSView *parent, kstat_ctl_t *_kc)
21  : FieldMeterGraph(parent, 2, "LOAD", "PROCS/MIN", 1, 1, 0)
22 {
23  kc = _kc;
25 #ifdef NO_GETLOADAVG
26  ksp = kstat_lookup(kc, "unix", 0, "system_misc");
27  if (ksp == NULL) {
28  parent_->done(1);
29  return;
30  }
31 #endif
32  total_ = -1;
33  lastalarmstate = -1;
35 }
36 
38 {
39 }
40 
42 {
44 
45  warnloadcol = parent_->allocColor(parent_->getResource("loadWarnColor"));
46  procloadcol = parent_->allocColor(parent_->getResource("loadProcColor"));
47  critloadcol = parent_->allocColor(parent_->getResource("loadCritColor"));
48 
50  setfieldcolor(1, parent_->getResource("loadIdleColor"));
51  priority_ = atoi (parent_->getResource("loadPriority"));
52  dodecay_ = parent_->isResourceTrue("loadDecay");
53  useGraph_ = parent_->isResourceTrue("loadGraph");
54  SetUsedFormat(parent_->getResource("loadUsedFormat"));
55  do_cpu_speed = parent_->isResourceTrue("loadCpuSpeed");
56 
57  const char *warn = parent_->getResource("loadWarnThreshold");
58  if (strncmp(warn, "auto", 2) == 0)
59  warnThreshold = sysconf(_SC_NPROCESSORS_ONLN);
60  else
61  warnThreshold = atoi(warn);
62 
63  const char *crit = parent_->getResource("loadCritThreshold");
64  if (strncmp(crit, "auto", 2) == 0)
66  else
67  critThreshold = atoi(crit);
68 
69  if (dodecay_){
70  /*
71  * Warning: Since the loadmeter changes scale
72  * occasionally, old decay values need to be rescaled.
73  * However, if they are rescaled, they could go off the
74  * edge of the screen. Thus, for now, to prevent this
75  * whole problem, the load meter can not be a decay
76  * meter. The load is a decaying average kind of thing
77  * anyway, so having a decaying load average is
78  * redundant.
79  */
80  std::cerr << "Warning: The loadmeter can not be configured as a decay\n"
81  << " meter. See the source code (" << __FILE__ << ") for further\n"
82  << " details.\n";
83  dodecay_ = 0;
84  }
85 }
86 
87 void LoadMeter::checkevent(void)
88 {
89  getloadinfo();
90  if (do_cpu_speed) {
91  getspeedinfo();
93  // update the legend:
94  char l[32];
95  snprintf(l, 32, "PROCS/MIN %d MHz", cur_cpu_speed);
96  legend(l);
97  drawlegend();
98  }
99  }
100  drawfields();
101 }
102 
103 void LoadMeter::getloadinfo(void)
104 {
105  int alarmstate;
106 #ifdef NO_GETLOADAVG
107  // This code is mainly for Solaris 6 and earlier, but should work on
108  // any version.
109  kstat_named_t *k;
110 
111  if (kstat_read(kc, ksp, NULL) == -1) {
112  parent_->done(1);
113  return;
114  }
115  k = (kstat_named_t *)kstat_data_lookup(ksp, "avenrun_1min");
116  if (k == NULL) {
117  parent_->done(1);
118  return;
119  }
120  fields_[0] = kstat_to_double(k) / FSCALE;
121 #else
122  // getloadavg() if found on Solaris 7 and newer.
123  getloadavg(&fields_[0], 1);
124 #endif
125 
126  if (fields_[0] < warnThreshold)
127  alarmstate = 0;
128  else if (fields_[0] >= critThreshold)
129  alarmstate = 2;
130  else /* if fields_[0] >= warnThreshold */
131  alarmstate = 1;
132 
133  if (alarmstate != lastalarmstate) {
134  if (alarmstate == 0)
136  else if (alarmstate == 1)
138  else /* if alarmstate == 2 */
140  drawlegend();
142  }
143 
144  // Adjust total to next power-of-two of the current load.
145  if ( (fields_[0]*5.0 < total_ && total_ > 1.0) || fields_[0] > total_ ) {
146  unsigned int i = fields_[0];
147  i |= i >> 1; i |= i >> 2; i |= i >> 4; i |= i >> 8; i |= i >> 16; // i = 2^n - 1
148  total_ = i + 1;
149  }
150 
151  fields_[1] = total_ - fields_[0];
152  setUsed(fields_[0], total_);
153 }
154 
155 void LoadMeter::getspeedinfo(void)
156 {
157  unsigned int total_mhz = 0, i = 0;
158  kstat_named_t *k;
159  kstat_t *cpu;
160  cpulist->update(kc);
161 
162  for (i = 0; i < cpulist->count(); i++) {
163  cpu = (*cpulist)[i];
164  if (kstat_read(kc, cpu, NULL) == -1) {
165  parent_->done(1);
166  return;
167  }
168  // Try current_clock_Hz first (needs frequency scaling support),
169  // then clock_MHz.
170  k = (kstat_named_t *)kstat_data_lookup(cpu, "current_clock_Hz");
171  if (k == NULL) {
172  k = (kstat_named_t *)kstat_data_lookup(cpu, "clock_MHz");
173  if (k == NULL) {
174  std::cerr << "CPU speed is not available." << std::endl;
175  parent_->done(1);
176  return;
177  }
178  XOSDEBUG("Speed of cpu %d is %lld MHz\n", i, kstat_to_ui64(k));
179  total_mhz += kstat_to_ui64(k);
180  }
181  else {
182  XOSDEBUG("Speed of cpu %d is %lld Hz\n", i, kstat_to_ui64(k));
183  total_mhz += ( kstat_to_ui64(k) / 1000000 );
184  }
185  }
187  cur_cpu_speed = ( i > 0 ? total_mhz / i : 0 );
188 }
189 
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
void update(kstat_ctl_t *kcp)
Definition: kstats.h:48
@ CPU_INFO
Definition: kstats.h:18
size_t count(void)
Definition: kstats.h:47
static KStatList * getList(kstat_ctl_t *kcp, module m)
Definition: kstats.h:23
unsigned long procloadcol
Definition: loadmeter.h:27
unsigned int old_cpu_speed
Definition: loadmeter.h:29
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
void checkResources(void)
Definition: loadmeter.cc:32
kstat_ctl_t * kc
Definition: loadmeter.h:33
int do_cpu_speed
Definition: loadmeter.h:35
unsigned int cur_cpu_speed
Definition: loadmeter.h:29
int lastalarmstate
Definition: loadmeter.h:32
unsigned long warnloadcol
Definition: loadmeter.h:27
KStatList * cpulist
Definition: loadmeter.h:32
unsigned long critloadcol
Definition: loadmeter.h:27
int critThreshold
Definition: loadmeter.h:32
~LoadMeter(void)
Definition: loadmeter.cc:29
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
#define FSCALE
Definition: loadmeter.cc:18
double kstat_to_double(kstat_named_t *k)
Definition: kstats.h:92
unsigned long long kstat_to_ui64(kstat_named_t *k)
Definition: kstats.h:118
#define XOSDEBUG(...)
Definition: xosview.h:84