File: intmeter.cc

package info (click to toggle)
xosview 1.24-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,184 kB
  • sloc: cpp: 11,975; makefile: 154; ansic: 32; awk: 13; sh: 8
file content (189 lines) | stat: -rw-r--r-- 4,814 bytes parent folder | download | duplicates (5)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
//  Copyright (c) 1994, 1995, 2006 by Mike Romberg ( mike.romberg@noaa.gov )
//
//  This file may be distributed under terms of the GPL
//

#include "intmeter.h"
#include <stdlib.h>
#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include <map>

static const char *INTFILE     = "/proc/interrupts";
static std::map<const int,int> realintnum;
static const int max = 1024;


IntMeter::IntMeter( XOSView *parent, int cpu )
  : BitMeter( parent, "INTS", "", 1, 0, 0 ), _cpu(cpu) {
  _irqs = _lastirqs = NULL;
  initirqcount();
}

IntMeter::~IntMeter( void ){
  if (_irqs)
    delete[] _irqs;
  if (_lastirqs)
    delete[] _lastirqs;
}

void IntMeter::checkevent( void ){
  getirqs();

  for ( int i = 0 ; i < numBits() ; i++ ){
    bits_[i] = ((_irqs[i] - _lastirqs[i]) != 0);
    _lastirqs[i] = _irqs[i];
  }

  BitMeter::checkevent();
}

void IntMeter::checkResources( void ){
  BitMeter::checkResources();
  onColor_  = parent_->allocColor( parent_->getResource( "intOnColor" ) );
  offColor_ = parent_->allocColor( parent_->getResource( "intOffColor" ) );
  priority_ = atoi( parent_->getResource( "intPriority" ) );
  _separate = parent_->isResourceTrue( "intSeparate" );
}

void IntMeter::getirqs( void ){
  std::ifstream intfile( INTFILE );
  std::string line;
  int intno, idx, i;
  unsigned long count, tmp;
  char *end = NULL;

  if ( !intfile ){
    std::cerr <<"Can not open file : " <<INTFILE << std::endl;
    exit( 1 );
  }

  intfile.ignore(max, '\n');

  while ( !intfile.eof() ){
    std::getline(intfile, line);
    if ( line.find_first_of("0123456789") > line.find_first_of(':') )
      break;  // reached non-numeric interrupts
    idx = strtoul(line.c_str(), &end, 10);
    if (idx >= max)
      break;
    intno = realintnum[idx];
    if ( intno >= numBits() )
      updateirqcount(intno, false);
    const char *cur = end + 1;
    count = tmp = i = 0;
    while (*cur && i++ <= _cpu) {
      tmp = strtoul(cur, &end, 10);
      count += tmp;
      cur = end;
    }
    _irqs[intno] = ( _separate ? tmp : count );
  }
}

/* The highest numbered interrupts, the number of interrupts
 * is going to be at least +1 (for int 0) and probably higher
 * if interrupts numbered more than this one just aren't active.
 * Must call with init = true the first time.
 */
void IntMeter::updateirqcount( int n, bool init ){
  int old_bits = numBits();
  setNumBits(n + 1);
  std::ostringstream os;

  os << "0";
  if (realintnum.upper_bound(15) == realintnum.end()) // only 16 ints
    os << "-15";
  else {
    int prev = 15, prev2 = 14;
    for (std::map<int,int>::const_iterator it = realintnum.upper_bound(15),
                                           end = realintnum.end();
                                           it != end; ++it) {
      if ( &*it == &*realintnum.rbegin() ) { // last element
        if ( it->first == prev + 1 )
          os << "-" ;
        else {
          if ( prev == prev2 + 1 )
            os << "-" << prev;
          os << "," ;
        }
        os << it->first;
      }
      else {
        if ( it->first != prev + 1 ) {
          if ( prev == prev2 + 1 )
            os << "-" << prev;
          os << "," << it->first ;
        }
      }
      prev2 = prev;
      prev = it->first;
    }
    os << std::ends;
  }

  legend(os.str().c_str());
  unsigned long *old_irqs = _irqs, *old_lastirqs = _lastirqs;
  _irqs = new unsigned long[n+1];
  _lastirqs = new unsigned long[n+1];
  /* If we are in init, set it to zero,
   * otherwise copy over the old set */
  if (init) {
    for (int i = 0; i < numBits(); i++)
      _irqs[i] = _lastirqs[i] = 0;
  }
  else {
    for (int i = 0; i < old_bits; i++) {
      _irqs[i] = old_irqs[i];
      _lastirqs[i] = old_lastirqs[i];
    }
    // zero to the end the irq's that haven't been seen before
    for (int i = old_bits; i < numBits(); i++)
      _irqs[i] = _lastirqs[i] = 0;
  }
  if (old_irqs)
    delete[] old_irqs;
  if (old_lastirqs)
    delete[] old_lastirqs;
}

/* Find the highest number of interrupts and call updateirqcount to
 * update the number of interrupts listed
 */
void IntMeter::initirqcount( void ){
  std::ifstream intfile( INTFILE );
  int intno = 0;
  int i, idx;

  if ( !intfile ){
    std::cerr <<"Can not open file : " <<INTFILE << std::endl;
    exit( 1 );
  }

  for (i = 0; i < 16; i++)
    realintnum[i] = i;

  intfile.ignore(max, '\n');

  /* just looking for the highest number interrupt that
   * is in use, ignore the rest of the data
   */
  idx = 16;
  while ( !intfile.eof() ){
    intfile >> i;
    /* break when reaching non-numeric special interrupts */
    if (!intfile)
      break;
    if (i < 16)
      intno = i;
    else {
      intno = idx;
      realintnum[i] = idx++;
    }
    intfile.ignore(max, '\n');
  }
  updateirqcount(intno, true);
}