"Fossies" - the Fresh Open Source Software Archive

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

    1 //
    2 //  Copyright (c) 1994, 1995 by Mike Romberg ( romberg@fsl.noaa.gov )
    3 //  Copyright (c) 1995, 1996, 1997-2002 by Brian Grayson (bgrayson@netbsd.org)
    4 //
    5 //  Most of this code was written by Werner Fink <werner@suse.de>.
    6 //  Only small changes were made on my part (M.R.)
    7 //  And the near-trivial port to NetBSD was done by Brian Grayson
    8 //
    9 //  This file may be distributed under terms of the GPL or of the BSD
   10 //    license, whichever you choose.  The full license notices are
   11 //    contained in the files COPYING.GPL and COPYING.BSD, which you
   12 //    should have received.  If not, contact one of the xosview
   13 //    authors for a copy.
   14 //
   15 
   16 #include "loadmeter.h"
   17 #include "kernel.h"
   18 #include <stdlib.h>
   19 #include <string.h>
   20 #include <stdio.h>
   21 #include <iostream>
   22 
   23 
   24 LoadMeter::LoadMeter( XOSView *parent )
   25     : FieldMeterGraph( parent, 2, "LOAD", "PROCS/MIN", 1, 1, 0 ) {
   26     total_ = -1.0;
   27 }
   28 
   29 LoadMeter::~LoadMeter( void ) {
   30 }
   31 
   32 void LoadMeter::checkResources( void ) {
   33     FieldMeterGraph::checkResources();
   34 
   35     procloadcol_ = parent_->allocColor( parent_->getResource("loadProcColor") );
   36     warnloadcol_ = parent_->allocColor( parent_->getResource("loadWarnColor") );
   37     critloadcol_ = parent_->allocColor( parent_->getResource("loadCritColor") );
   38 
   39     setfieldcolor( 0, procloadcol_ );
   40     setfieldcolor( 1, parent_->getResource("loadIdleColor") );
   41     priority_ = atoi( parent_->getResource("loadPriority") );
   42     dodecay_ = parent_->isResourceTrue("loadDecay");
   43     useGraph_ = parent_->isResourceTrue("loadGraph");
   44     SetUsedFormat( parent_->getResource("loadUsedFormat") );
   45     do_cpu_speed_ = parent_->isResourceTrue("loadCpuSpeed");
   46 
   47     const char *warn = parent_->getResource("loadWarnThreshold");
   48     if (strncmp(warn, "auto", 2) == 0)
   49         warnThreshold_ = BSDCountCpus();
   50     else
   51         warnThreshold_ = atoi(warn);
   52 
   53     const char *crit = parent_->getResource("loadCritThreshold");
   54     if (strncmp(crit, "auto", 2) == 0)
   55         critThreshold_ = warnThreshold_ * 4;
   56     else
   57         critThreshold_ = atoi(crit);
   58 
   59     alarmstate_ = lastalarmstate_ = 0;
   60 
   61     if (dodecay_) {
   62         //  Warning:  Since the loadmeter changes scale occasionally, old
   63         //  decay values need to be rescaled.  However, if they are rescaled,
   64         //  they could go off the edge of the screen.  Thus, for now, to
   65         //  prevent this whole problem, the load meter can not be a decay
   66         //  meter.  The load is a decaying average kind of thing anyway,
   67         //  so having a decaying load average is redundant.
   68         std::cerr << "Warning:  The loadmeter can not be configured as a decay\n"
   69                   << "  meter.  See the source code (" << __FILE__ << ") for further\n"
   70                   << "  details.\n";
   71         dodecay_ = 0;
   72     }
   73 }
   74 
   75 void LoadMeter::checkevent( void ) {
   76     getloadinfo();
   77 
   78     if (do_cpu_speed_) {
   79         old_cpu_speed_ = cur_cpu_speed_;
   80         cur_cpu_speed_ = BSDGetCPUSpeed();
   81 
   82         if (old_cpu_speed_ != cur_cpu_speed_) {
   83             char l[25];
   84             snprintf(l, 25, "PROCS/MIN %d MHz", cur_cpu_speed_);
   85             legend(l);
   86             drawlegend();
   87         }
   88     }
   89     drawfields();
   90 }
   91 
   92 void LoadMeter::getloadinfo( void ) {
   93     double oneMinLoad;
   94 
   95     getloadavg(&oneMinLoad, 1);  //  Only get the 1-minute-average sample.
   96     fields_[0] = oneMinLoad;
   97 
   98     if (fields_[0] <  warnThreshold_)
   99         alarmstate_ = 0;
  100     else if (fields_[0] >= critThreshold_)
  101         alarmstate_ = 2;
  102     else
  103         alarmstate_ = 1;
  104 
  105     if (alarmstate_ != lastalarmstate_) {
  106         if (alarmstate_ == 0)
  107             setfieldcolor(0, procloadcol_);
  108         else if (alarmstate_ == 1)
  109             setfieldcolor(0, warnloadcol_);
  110         else
  111             setfieldcolor(0, critloadcol_);
  112         drawlegend();
  113         lastalarmstate_ = alarmstate_;
  114     }
  115 
  116     // Adjust total to next power-of-two of the current load.
  117     if ( (fields_[0]*5.0 < total_ && total_ > 1.0) || fields_[0] > total_ ) {
  118         unsigned int i = fields_[0];
  119         i |= i >> 1; i |= i >> 2; i |= i >> 4; i |= i >> 8; i |= i >> 16;  // i = 2^n - 1
  120         total_ = i + 1;
  121     }
  122 
  123     fields_[1] = total_ - fields_[0];
  124     setUsed(fields_[0], total_);
  125 }