"Fossies" - the Fresh Open Source Software Archive

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

    1 //  
    2 //  Initial port performed by Greg Onufer (exodus@cheers.bungi.com)
    3 //
    4 #include "swapmeter.h"
    5 #include <stdlib.h>
    6 #include <unistd.h>
    7 #include <limits.h>
    8 #include <sys/stat.h>
    9 #include <sys/swap.h>
   10 #include <iostream>
   11 
   12 
   13 SwapMeter::SwapMeter(XOSView *parent, kstat_ctl_t *_kc)
   14     : FieldMeterGraph(parent, 2, "SWAP", "USED/FREE")
   15 {
   16     pagesize = sysconf(_SC_PAGESIZE);
   17 }
   18 
   19 SwapMeter::~SwapMeter(void)
   20 {
   21 }
   22 
   23 void SwapMeter::checkResources(void)
   24 {
   25     FieldMeterGraph::checkResources();
   26 
   27     setfieldcolor(0, parent_->getResource("swapUsedColor"));
   28     setfieldcolor(1, parent_->getResource("swapFreeColor"));
   29     priority_ = atoi(parent_->getResource("swapPriority"));
   30     dodecay_ = parent_->isResourceTrue("swapDecay");
   31     useGraph_ = parent_->isResourceTrue("swapGraph");
   32     SetUsedFormat(parent_->getResource("swapUsedFormat"));
   33 }
   34 
   35 void SwapMeter::checkevent(void)
   36 {
   37     getswapinfo();
   38     drawfields();
   39 }
   40 
   41 void SwapMeter::getswapinfo(void)
   42 {
   43     swaptbl_t *swaps;
   44     char *names;
   45 
   46     total_ = fields_[0] = fields_[1] = 0;
   47     int numswap = swapctl(SC_GETNSWP, NULL);
   48     if (numswap < 0) {
   49         std::cerr << "Can not determine number of swap spaces." << std::endl;
   50         parent_->done(1);
   51         return;
   52     }
   53     if (numswap > 0) {
   54         swaps = (swaptbl_t *)malloc(sizeof(swaptbl_t) + numswap * sizeof(swapent_t));
   55         names = (char *)calloc(numswap + 1, PATH_MAX);
   56         if (!swaps || !names) {
   57             std::cerr << "malloc failed." << std::endl;
   58             parent_->done(1);
   59             return;
   60         }
   61         swaps->swt_n = numswap;
   62         for (int i = 0; i <= numswap; i++)
   63             swaps->swt_ent[i].ste_path = names + (i * PATH_MAX);
   64 
   65         if (swapctl(SC_LIST, swaps) < 0) {
   66             std::cerr << "Can not get list of swap spaces." << std::endl;
   67             parent_->done(1);
   68             return;
   69         }
   70         for (int i = 0; i < numswap; i++) {
   71             total_ += swaps->swt_ent[i].ste_pages;
   72             fields_[1] += swaps->swt_ent[i].ste_free;
   73             XOSDEBUG("%s: %ld kB (%ld kB free)\n",
   74                      swaps->swt_ent[i].ste_path,
   75                      swaps->swt_ent[i].ste_pages * (pagesize / 1024),
   76                      swaps->swt_ent[i].ste_free * (pagesize / 1024));
   77         }
   78         fields_[0] = total_ - fields_[1];
   79         free(swaps);
   80         free(names);
   81     }
   82 
   83     setUsed(fields_[0] * pagesize, total_ * pagesize);
   84 }