"Fossies" - the Fresh Open Source Software Archive

Member "xosview-1.23/irix65/cpumeter.cc" (11 Jul 2020, 3132 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 "cpumeter.cc" see the Fossies "Dox" file reference documentation.

    1 //
    2 //  Initial port performed by Stefan Eilemann (eilemann@gmail.com)
    3 //
    4 #include "cpumeter.h"
    5 #include "xosview.h"
    6 #include <stdlib.h>
    7 #include <unistd.h>
    8 #include <ctype.h>
    9 #include <string.h>
   10 #include <sstream>
   11 
   12 CPUMeter::CPUMeter(XOSView *parent, int cpuid)
   13     : FieldMeterGraph(parent, USED_CPU_STATES, toUpper(cpuStr(cpuid)),
   14         "USER/SYS/INTR/WAIT/IDLE")
   15 {
   16     for (int i = 0 ; i < 2 ; i++)
   17         for (int j = 0 ; j < USED_CPU_STATES ; j++)
   18             cputime_[i][j] = 0;
   19 
   20     if ((sinfosz = sysmp(MP_SASZ, MPSA_SINFO)) < 0) {
   21            std::cerr << "sysinfo scall interface not supported" << endl;
   22         parent_->done(1);
   23         return;
   24     }
   25     cpuid_ = cpuid;
   26     cpuindex_ = 0;
   27 }
   28 
   29 CPUMeter::~CPUMeter(void)
   30 {
   31 }
   32 
   33 void CPUMeter::checkResources(void)
   34 {
   35     FieldMeterGraph::checkResources();
   36 
   37     setfieldcolor(0, parent_->getResource("cpuUserColor"));
   38     setfieldcolor(1, parent_->getResource("cpuSystemColor"));
   39     setfieldcolor(2, parent_->getResource("cpuInterruptColor"));
   40     setfieldcolor(3, parent_->getResource("cpuWaitColor"));
   41     setfieldcolor(4, parent_->getResource("cpuFreeColor"));
   42     priority_ = atoi(parent_->getResource("cpuPriority"));
   43     dodecay_ = parent_->isResourceTrue("cpuDecay");
   44     useGraph_ = parent_->isResourceTrue("cpuGraph");
   45     SetUsedFormat(parent_->getResource("cpuUsedFormat"));
   46 }
   47 
   48 void CPUMeter::checkevent(void)
   49 {
   50     getcputime();
   51     drawfields();
   52 }
   53 
   54 void CPUMeter::getcputime(void)
   55 {
   56     total_ = 0;
   57 
   58     if(cpuid_==-1)
   59         sysmp(MP_SAGET, MPSA_SINFO, (char *) &tsp, sinfosz);
   60     else
   61         sysmp(MP_SAGET1, MPSA_SINFO, (char *) &tsp, sinfosz, cpuid_);
   62 
   63     tsp.cpu[CPU_WAIT] -= (tsp.wait[W_GFXF] + tsp.wait[W_GFXC]);
   64 
   65     cputime_[cpuindex_][0] = tsp.cpu[CPU_USER];
   66     cputime_[cpuindex_][1] = tsp.cpu[CPU_KERNEL];
   67     cputime_[cpuindex_][2] = tsp.cpu[CPU_INTR];
   68     cputime_[cpuindex_][3] = tsp.cpu[CPU_WAIT];
   69     cputime_[cpuindex_][4] = tsp.cpu[CPU_IDLE] + tsp.cpu[CPU_SXBRK];
   70 
   71     int oldindex = (cpuindex_ + 1) % 2;
   72     for (int i = 0 ; i < USED_CPU_STATES ; i++) {
   73         fields_[i] = cputime_[cpuindex_][i] - cputime_[oldindex][i];
   74         total_ += fields_[i];
   75     }
   76     cpuindex_ = (cpuindex_ + 1) % 2;
   77 
   78     if (total_)
   79         setUsed(total_ - fields_[3] - fields_[4], total_); // wait doesn't count
   80     // as used
   81 }
   82 
   83 const char *CPUMeter::toUpper(const char *str)
   84 {
   85     static char buffer[256];
   86     strncpy(buffer, str, 256);
   87     for (char *tmp = buffer ; *tmp != '\0' ; tmp++)
   88         *tmp = toupper(*tmp);
   89 
   90     return buffer;
   91 }
   92 
   93 const char *CPUMeter::cpuStr(int num)
   94 {
   95     static char buffer[32];
   96     const int nCPU = nCPUs();
   97 
   98     if( nCPU==1 || num==-1 )
   99     {
  100         snprintf(buffer, 32, "CPU");
  101     }
  102     else if( nCPU<=10 )
  103     {
  104         snprintf(buffer, 32, "#%d", num);
  105     }
  106     else if ( nCPU<=100 )
  107     {
  108         snprintf(buffer, 32, "#%02d", num);
  109     }
  110     else if ( nCPU<=1000 )
  111     {
  112         snprintf(buffer, 32, "#%03d", num);
  113     }
  114     else
  115     {
  116         snprintf(buffer, 32, "%4.1d", num);
  117     }
  118     return buffer;
  119 }
  120 
  121 int CPUMeter::nCPUs()
  122 {
  123     return sysmp(MP_NPROCS); // FIXME: use NAPROCS + identify 'unused procs'
  124 }