"Fossies" - the Fresh Open Source Software Archive

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

    1 //
    2 //  Copyright (c) 1994, 1995 by Mike Romberg ( romberg@fsl.noaa.gov )
    3 //
    4 //  NetBSD port:
    5 //  Copyright (c) 1995, 1996, 1997-2002 by Brian Grayson (bgrayson@netbsd.org)
    6 //
    7 //  This file was written by Brian Grayson for the NetBSD and xosview
    8 //    projects.
    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 "netmeter.h"
   17 #include "kernel.h"
   18 #include <stdlib.h>
   19 #include <err.h>
   20 
   21 
   22 NetMeter::NetMeter( XOSView *parent, double max )
   23     : FieldMeterGraph( parent, 3, "NET", "IN/OUT/IDLE" ) {
   24     if (!BSDNetInit()) {
   25         warnx("The kernel does not seem to have the symbols needed for the NetMeter.");
   26         warnx("The NetMeter has been disabled.");
   27         disableMeter ();
   28     }
   29     else {
   30         total_ = netBandwidth_ = max;
   31         lastBytesIn_ = lastBytesOut_ = 0;
   32         netIface_ = "False";
   33         ignored_ = false;
   34         BSDGetNetInOut(&lastBytesIn_, &lastBytesOut_, netIface_.c_str(), ignored_);
   35         IntervalTimerStart();
   36     }
   37 }
   38 
   39 NetMeter::~NetMeter( void ) {
   40 }
   41 
   42 void NetMeter::checkResources( void ) {
   43     FieldMeterGraph::checkResources();
   44 
   45     setfieldcolor( 0, parent_->getResource("netInColor") );
   46     setfieldcolor( 1, parent_->getResource("netOutColor") );
   47     setfieldcolor( 2, parent_->getResource("netBackground") );
   48     priority_ = atoi( parent_->getResource("netPriority") );
   49     dodecay_ = parent_->isResourceTrue("netDecay");
   50     useGraph_ = parent_->isResourceTrue("netGraph");
   51     SetUsedFormat( parent_->getResource("netUsedFormat") );
   52     netIface_ = parent_->getResource("netIface");
   53     if (netIface_[0] == '-') {
   54         ignored_ = true;
   55         netIface_.erase(0, netIface_.find_first_not_of("- "));
   56     }
   57 }
   58 
   59 void NetMeter::checkevent( void ) {
   60     getstats();
   61     drawfields();
   62 }
   63 
   64 void NetMeter::getstats(void) {
   65     //  Reset total_ to expected maximum.  If it is too low, it
   66     //  will be adjusted in adjust().  bgrayson
   67     total_ = netBandwidth_;
   68     fields_[0] = fields_[1] = 0;
   69     uint64_t nowBytesIn, nowBytesOut;
   70 
   71     IntervalTimerStop();
   72     BSDGetNetInOut(&nowBytesIn, &nowBytesOut, netIface_.c_str(), ignored_);
   73     double t = (1.0) / IntervalTimeInSecs();
   74     IntervalTimerStart();
   75 
   76     fields_[0] = (double)(nowBytesIn - lastBytesIn_) * t;
   77     lastBytesIn_ = nowBytesIn;
   78     fields_[1] = (double)(nowBytesOut - lastBytesOut_) * t;
   79     lastBytesOut_ = nowBytesOut;
   80 
   81     if (total_ < (fields_[0] + fields_[1]))
   82         total_ = fields_[0] + fields_[1];
   83     fields_[2] = total_ - fields_[0] - fields_[1];
   84     /*  The fields_ values have already been scaled into bytes/sec by
   85      *  the manipulations (* t) above.  */
   86     setUsed(fields_[0] + fields_[1], total_);
   87 }