"Fossies" - the Fresh Open Source Software Archive

Member "xosview-1.23/linux/intmeter.cc" (11 Jul 2020, 4814 Bytes) of package /linux/misc/xosview-1.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "intmeter.cc" see the Fossies "Dox" file reference documentation.

    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 
   41   BitMeter::checkevent();
   42 }
   43 
   44 void IntMeter::checkResources( void ){
   45   BitMeter::checkResources();
   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  */
  156 void IntMeter::initirqcount( void ){
  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 }