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)  

intmeter.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 1994, 1995, 2006 by Mike Romberg ( mike.romberg@noaa.gov )
3 //
4 // This file may be distributed under terms of the GPL
5 //
6 
7 #include "intmeter.h"
8 #include <stdlib.h>
9 #include <fstream>
10 #include <string>
11 #include <iostream>
12 #include <sstream>
13 #include <map>
14 
15 static const char *INTFILE = "/proc/interrupts";
16 static std::map<const int,int> realintnum;
17 static const int max = 1024;
18 
19 
20 IntMeter::IntMeter( XOSView *parent, int cpu )
21  : BitMeter( parent, "INTS", "", 1, 0, 0 ), _cpu(cpu) {
22  _irqs = _lastirqs = NULL;
23  initirqcount();
24 }
25 
26 IntMeter::~IntMeter( void ){
27  if (_irqs)
28  delete[] _irqs;
29  if (_lastirqs)
30  delete[] _lastirqs;
31 }
32 
33 void IntMeter::checkevent( void ){
34  getirqs();
35 
36  for ( int i = 0 ; i < numBits() ; i++ ){
37  bits_[i] = ((_irqs[i] - _lastirqs[i]) != 0);
38  _lastirqs[i] = _irqs[i];
39  }
40 
42 }
43 
44 void IntMeter::checkResources( void ){
46  onColor_ = parent_->allocColor( parent_->getResource( "intOnColor" ) );
47  offColor_ = parent_->allocColor( parent_->getResource( "intOffColor" ) );
48  priority_ = atoi( parent_->getResource( "intPriority" ) );
49  _separate = parent_->isResourceTrue( "intSeparate" );
50 }
51 
52 void IntMeter::getirqs( void ){
53  std::ifstream intfile( INTFILE );
54  std::string line;
55  int intno, idx, i;
56  unsigned long count, tmp;
57  char *end = NULL;
58 
59  if ( !intfile ){
60  std::cerr <<"Can not open file : " <<INTFILE << std::endl;
61  exit( 1 );
62  }
63 
64  intfile.ignore(max, '\n');
65 
66  while ( !intfile.eof() ){
67  std::getline(intfile, line);
68  if ( line.find_first_of("0123456789") > line.find_first_of(':') )
69  break; // reached non-numeric interrupts
70  idx = strtoul(line.c_str(), &end, 10);
71  if (idx >= max)
72  break;
73  intno = realintnum[idx];
74  if ( intno >= numBits() )
75  updateirqcount(intno, false);
76  const char *cur = end + 1;
77  count = tmp = i = 0;
78  while (*cur && i++ <= _cpu) {
79  tmp = strtoul(cur, &end, 10);
80  count += tmp;
81  cur = end;
82  }
83  _irqs[intno] = ( _separate ? tmp : count );
84  }
85 }
86 
87 /* The highest numbered interrupts, the number of interrupts
88  * is going to be at least +1 (for int 0) and probably higher
89  * if interrupts numbered more than this one just aren't active.
90  * Must call with init = true the first time.
91  */
92 void IntMeter::updateirqcount( int n, bool init ){
93  int old_bits = numBits();
94  setNumBits(n + 1);
95  std::ostringstream os;
96 
97  os << "0";
98  if (realintnum.upper_bound(15) == realintnum.end()) // only 16 ints
99  os << "-15";
100  else {
101  int prev = 15, prev2 = 14;
102  for (std::map<int,int>::const_iterator it = realintnum.upper_bound(15),
103  end = realintnum.end();
104  it != end; ++it) {
105  if ( &*it == &*realintnum.rbegin() ) { // last element
106  if ( it->first == prev + 1 )
107  os << "-" ;
108  else {
109  if ( prev == prev2 + 1 )
110  os << "-" << prev;
111  os << "," ;
112  }
113  os << it->first;
114  }
115  else {
116  if ( it->first != prev + 1 ) {
117  if ( prev == prev2 + 1 )
118  os << "-" << prev;
119  os << "," << it->first ;
120  }
121  }
122  prev2 = prev;
123  prev = it->first;
124  }
125  os << std::ends;
126  }
127 
128  legend(os.str().c_str());
129  unsigned long *old_irqs = _irqs, *old_lastirqs = _lastirqs;
130  _irqs = new unsigned long[n+1];
131  _lastirqs = new unsigned long[n+1];
132  /* If we are in init, set it to zero,
133  * otherwise copy over the old set */
134  if (init) {
135  for (int i = 0; i < numBits(); i++)
136  _irqs[i] = _lastirqs[i] = 0;
137  }
138  else {
139  for (int i = 0; i < old_bits; i++) {
140  _irqs[i] = old_irqs[i];
141  _lastirqs[i] = old_lastirqs[i];
142  }
143  // zero to the end the irq's that haven't been seen before
144  for (int i = old_bits; i < numBits(); i++)
145  _irqs[i] = _lastirqs[i] = 0;
146  }
147  if (old_irqs)
148  delete[] old_irqs;
149  if (old_lastirqs)
150  delete[] old_lastirqs;
151 }
152 
153 /* Find the highest number of interrupts and call updateirqcount to
154  * update the number of interrupts listed
155  */
157  std::ifstream intfile( INTFILE );
158  int intno = 0;
159  int i, idx;
160 
161  if ( !intfile ){
162  std::cerr <<"Can not open file : " <<INTFILE << std::endl;
163  exit( 1 );
164  }
165 
166  for (i = 0; i < 16; i++)
167  realintnum[i] = i;
168 
169  intfile.ignore(max, '\n');
170 
171  /* just looking for the highest number interrupt that
172  * is in use, ignore the rest of the data
173  */
174  idx = 16;
175  while ( !intfile.eof() ){
176  intfile >> i;
177  /* break when reaching non-numeric special interrupts */
178  if (!intfile)
179  break;
180  if (i < 16)
181  intno = i;
182  else {
183  intno = idx;
184  realintnum[i] = idx++;
185  }
186  intfile.ignore(max, '\n');
187  }
188  updateirqcount(intno, true);
189 }
int numBits(void) const
Definition: bitmeter.h:25
unsigned long onColor_
Definition: bitmeter.h:30
void setNumBits(int n)
Definition: bitmeter.cc:23
unsigned long offColor_
Definition: bitmeter.h:30
char * bits_
Definition: bitmeter.h:31
void checkResources(void)
Definition: bitmeter.cc:43
void checkevent(void)
Definition: bitmeter.cc:47
bool _separate
Definition: intmeter.h:27
int _cpu
Definition: intmeter.h:26
void getirqs(void)
Definition: intmeter.cc:57
IntMeter(XOSView *parent, const char *title="", const char *legend="", int dolegends=0, int dousedlegends=0)
Definition: intmeter.cc:14
unsigned long * _lastirqs
Definition: intmeter.h:25
void checkResources(void)
Definition: intmeter.cc:50
void initirqcount(void)
Definition: intmeter.cc:156
void checkevent(void)
Definition: intmeter.cc:31
void updateirqcount(bool init=false)
Definition: intmeter.cc:61
unsigned long * _irqs
Definition: intmeter.h:25
~IntMeter(void)
Definition: intmeter.cc:25
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
static const int max
Definition: intmeter.cc:17
static std::map< const int, int > realintnum
Definition: intmeter.cc:16
static const char * INTFILE
Definition: intmeter.cc:15