"Fossies" - the Fresh Open Source Software Archive

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

    1 //  
    2 //  Initial port performed by Greg Onufer (exodus@cheers.bungi.com)
    3 //
    4 
    5 #include "memmeter.h"
    6 #include "kstats.h"
    7 #include <unistd.h>
    8 #include <stdlib.h>
    9 #include <iostream>
   10 
   11 
   12 MemMeter::MemMeter(XOSView *parent, kstat_ctl_t *_kc)
   13     : FieldMeterGraph(parent, 4, "MEM", "SYS/ZFS/OTHER/FREE")
   14 {
   15     kc = _kc;
   16     pageSize = sysconf(_SC_PAGESIZE);
   17     total_ = sysconf(_SC_PHYS_PAGES);
   18 
   19     ksp_sp = kstat_lookup(kc, "unix", 0, "system_pages");
   20     ksp_zfs = kstat_lookup(kc, "vmem", -1, "zfs_file_data_buf");
   21     if (ksp_sp == NULL) { // ZFS cache may be missing
   22         std::cerr << "Can not find unix:0:system_pages kstat." << std::endl;
   23         parent_->done(1);
   24         return;
   25     }
   26 }
   27 
   28 void MemMeter::checkResources(void)
   29 {
   30     FieldMeterGraph::checkResources();
   31 
   32     setfieldcolor(0, parent_->getResource("memKernelColor"));
   33     setfieldcolor(1, parent_->getResource("memCacheColor"));
   34     setfieldcolor(2, parent_->getResource("memUsedColor"));
   35     setfieldcolor(3, parent_->getResource("memFreeColor"));
   36     priority_ = atoi(parent_->getResource("memPriority"));
   37     dodecay_ = parent_->isResourceTrue("memDecay");
   38     useGraph_ = parent_->isResourceTrue("memGraph");
   39     SetUsedFormat(parent_->getResource("memUsedFormat"));
   40 }
   41 
   42 MemMeter::~MemMeter(void)
   43 {
   44 }
   45 
   46 void MemMeter::checkevent(void)
   47 {
   48     getmeminfo();
   49     drawfields();
   50 }
   51 
   52 void MemMeter::getmeminfo(void)
   53 {
   54     kstat_named_t *k;
   55 
   56     fields_[1] = 0;
   57     if (ksp_zfs) {
   58         if (kstat_read(kc, ksp_zfs, NULL) == -1) {
   59             std::cerr << "Can not read vmem::zfs_file_data_buf kstat." << std::endl;
   60             parent_->done(1);
   61             return;
   62         }
   63         k = (kstat_named_t *)kstat_data_lookup(ksp_zfs, "mem_inuse");
   64         if (k == NULL) {
   65             std::cerr << "Can not read vmem::zfs_file_data_buf:mem_inuse kstat." << std::endl;
   66             parent_->done(1);
   67             return;
   68         }
   69         fields_[1] = kstat_to_double(k) / pageSize;
   70     }
   71 
   72     if (kstat_read(kc, ksp_sp, NULL) == -1) {
   73         std::cerr << "Can not read unix:0:system_pages kstat." << std::endl;
   74         parent_->done(1);
   75         return;
   76     }
   77     k = (kstat_named_t *)kstat_data_lookup(ksp_sp, "pp_kernel");
   78     if (k == NULL) {
   79         std::cerr << "Can not read unix:0:system_pages:pp_kernel kstat." << std::endl;
   80         parent_->done(1);
   81         return;
   82     }
   83     fields_[0] = kstat_to_double(k) - fields_[1];
   84     k = (kstat_named_t *)kstat_data_lookup(ksp_sp, "freemem");
   85     if (k == NULL) {
   86         std::cerr << "Can not read unix:0:system_pages:freemem kstat." << std::endl;
   87         parent_->done(1);
   88         return;
   89     }
   90     fields_[3] = kstat_to_double(k);
   91     fields_[2] = total_ - (fields_[0] + fields_[1] + fields_[3]);
   92 
   93     XOSDEBUG("kernel: %llu kB zfs: %llu kB other: %llu kB free: %llu kB\n",
   94              (unsigned long long)(fields_[0] * pageSize / 1024),
   95              (unsigned long long)(fields_[1] * pageSize / 1024),
   96              (unsigned long long)(fields_[2] * pageSize / 1024),
   97              (unsigned long long)(fields_[3] * pageSize / 1024));
   98 
   99     setUsed((fields_[0] + fields_[1] + fields_[2]) * pageSize, total_ * pageSize);
  100 }
  101