"Fossies" - the Fresh Open Source Software Archive

Member "xosview-1.23/sunos5/cpumeter.cc" (11 Jul 2020, 2616 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 Greg Onufer (exodus@cheers.bungi.com)
    3 //
    4 
    5 #include "cpumeter.h"
    6 #include <stdlib.h>
    7 #include <stdio.h>
    8 #include <strings.h>
    9 #include <sys/sysinfo.h>
   10 
   11 
   12 CPUMeter::CPUMeter(XOSView *parent, kstat_ctl_t *_kc, int cpuid)
   13     : FieldMeterGraph(parent, CPU_STATES, cpuStr(cpuid), "USER/SYS/WAIT/IDLE")
   14 {
   15     kc = _kc;
   16     aggregate = ( cpuid < 0 );
   17     for (int i = 0 ; i < 2 ; i++)
   18         for (int j = 0 ; j < CPU_STATES ; j++)
   19             cputime_[i][j] = 0;
   20     cpuindex_ = 0;
   21     cpustats = KStatList::getList(kc, KStatList::CPU_STAT);
   22 
   23     if (!aggregate)
   24         for (unsigned int i = 0; i < cpustats->count(); i++)
   25             if ((*cpustats)[i]->ks_instance == cpuid)
   26                 ksp = (*cpustats)[i];
   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("cpuFreeColor"));
   41     priority_ = atoi(parent_->getResource("cpuPriority"));
   42     dodecay_ = parent_->isResourceTrue("cpuDecay");
   43     useGraph_ = parent_->isResourceTrue("cpuGraph");
   44     SetUsedFormat(parent_->getResource("cpuUsedFormat"));
   45 }
   46 
   47 void CPUMeter::checkevent(void)
   48 {
   49     getcputime();
   50     drawfields();
   51 }
   52 
   53 void CPUMeter::getcputime(void)
   54 {
   55     total_ = 0;
   56     cpu_stat_t cs;
   57 
   58     if (aggregate) {
   59         cpustats->update(kc);
   60         bzero(cputime_[cpuindex_], CPU_STATES * sizeof(cputime_[cpuindex_][0]));
   61         for (unsigned int i = 0; i < cpustats->count(); i++) {
   62             if (kstat_read(kc, (*cpustats)[i], &cs) == -1) {
   63                 parent_->done(1);
   64                 return;
   65             }
   66             cputime_[cpuindex_][0] += cs.cpu_sysinfo.cpu[CPU_USER];
   67             cputime_[cpuindex_][1] += cs.cpu_sysinfo.cpu[CPU_KERNEL];
   68             cputime_[cpuindex_][2] += cs.cpu_sysinfo.cpu[CPU_WAIT];
   69             cputime_[cpuindex_][3] += cs.cpu_sysinfo.cpu[CPU_IDLE];
   70         }
   71     }
   72     else {
   73         if (kstat_read(kc, ksp, &cs) == -1) {
   74             parent_->done(1);
   75             return;
   76         }
   77         cputime_[cpuindex_][0] = cs.cpu_sysinfo.cpu[CPU_USER];
   78         cputime_[cpuindex_][1] = cs.cpu_sysinfo.cpu[CPU_KERNEL];
   79         cputime_[cpuindex_][2] = cs.cpu_sysinfo.cpu[CPU_WAIT];
   80         cputime_[cpuindex_][3] = cs.cpu_sysinfo.cpu[CPU_IDLE];
   81     }
   82 
   83     int oldindex = (cpuindex_ + 1) % 2;
   84     for (int i = 0 ; i < CPU_STATES ; i++) {
   85         fields_[i] = cputime_[cpuindex_][i] - cputime_[oldindex][i];
   86         total_ += fields_[i];
   87     }
   88     cpuindex_ = (cpuindex_ + 1) % 2;
   89 
   90     if (total_)
   91         setUsed(total_ - fields_[3], total_);
   92 }
   93 
   94 const char *CPUMeter::cpuStr(int num)
   95 {
   96     static char buffer[8] = "CPU";
   97     if (num >= 0)
   98         snprintf(buffer + 3, 4, "%d", num);
   99     buffer[7] = '\0';
  100     return buffer;
  101 }