"Fossies" - the Fresh Open Source Software Archive

Member "xosview-1.23/linux/memmeter.cc" (11 Jul 2020, 3108 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 //  Copyright (c) 1994, 1995, 2006 by Mike Romberg ( mike.romberg@noaa.gov )
    3 //  Copyright (c) 2015 Framestore
    4 //
    5 //  This file may be distributed under terms of the GPL
    6 //
    7 
    8 #include "memmeter.h"
    9 #include <limits.h>
   10 #include <stdlib.h>
   11 #include <stdio.h>
   12 #include <string.h>
   13 
   14 #define MEMFILENAME "/proc/meminfo"
   15 
   16 #define KB(x) ((double)((x) * 1024))
   17 
   18 
   19 MemMeter::MemMeter( XOSView *parent )
   20 : FieldMeterGraph( parent, 5, "MEM", "USED/BUFF/SLAB/CACHE/FREE" ){
   21 }
   22 
   23 void MemMeter::checkResources( void ){
   24   FieldMeterGraph::checkResources();
   25 
   26   setfieldcolor( 0, parent_->getResource( "memUsedColor" ) );
   27   setfieldcolor( 1, parent_->getResource( "memBufferColor" ) );
   28   setfieldcolor( 2, parent_->getResource( "memSlabColor" ) );
   29   setfieldcolor( 3, parent_->getResource( "memCacheColor" ) );
   30   setfieldcolor( 4, parent_->getResource( "memFreeColor" ) );
   31   priority_ = atoi (parent_->getResource( "memPriority" ) );
   32   dodecay_ = parent_->isResourceTrue( "memDecay" );
   33   useGraph_ = parent_->isResourceTrue( "memGraph" );
   34   SetUsedFormat (parent_->getResource("memUsedFormat"));
   35 }
   36 
   37 void MemMeter::checkevent( void ){
   38   getstats();
   39   drawfields();
   40 }
   41 
   42 void MemMeter::getstats() {
   43   FILE *f;
   44 
   45   f = fopen(MEMFILENAME, "r");
   46   if (!f) {
   47     perror(MEMFILENAME);
   48     exit(1);
   49   }
   50 
   51   /*
   52    * The kernel's "unsigned long" values vary in size on 64-bit and
   53    * 32-bit implementations.
   54    *
   55    * But there's nothing saying userland will match the kernel; we
   56    * must pretty much accomodate anything in ASCII that /proc gives
   57    * us, so 64-bit integers are always used.
   58    */
   59 
   60   unsigned long long mem_total = 0,
   61     mem_free = 0,
   62     buffers = 0,
   63     slab = 0,
   64     cached = 0;
   65 
   66   for (;;) {
   67     char line[128];
   68     char *c, *endptr;
   69     unsigned long long kb = 0;
   70 
   71     /*
   72      * Parse lines in the format: "FieldName:      12345678 kB"
   73      *
   74      * We prefer to not use scanf because it's harder with variable
   75      * number of fields; the 'kB' is not present if value is 0
   76      */
   77 
   78     if (!fgets(line, sizeof line, f))
   79       break;
   80 
   81     c = strchr(line, ':');
   82     if (!c) {
   83       fprintf(stderr, MEMFILENAME ": parse error, ':' expected at '%s'\n", line);
   84       exit(1);
   85     }
   86 
   87     *c = '\0';
   88     c++;
   89 
   90     kb = strtoull(c, &endptr, 10);
   91     if (kb == ULLONG_MAX) {
   92       fprintf(stderr, MEMFILENAME ": parse error, '%s' is out of range\n", c);
   93       exit(1);
   94     }
   95 
   96     if (strcmp(line, "MemTotal") == 0)
   97       mem_total = kb;
   98     else if (strcmp(line, "MemFree") == 0)
   99       mem_free = kb;
  100     else if (strcmp(line, "Buffers") == 0)
  101       buffers = kb;
  102     else if (strcmp(line, "Cached") == 0)
  103       cached = kb;
  104     else if (strcmp(line, "Slab") == 0)
  105       slab = kb;
  106   }
  107 
  108   if (fclose(f) != 0)
  109     abort();
  110 
  111   /* Don't do arithmetic on the fields_ themselves; these are floating
  112    * point and when memory is large are affected by inaccuracy */
  113 
  114   fields_[1] = KB(buffers);
  115   fields_[2] = KB(slab);
  116   fields_[3] = KB(cached);
  117   fields_[4] = KB(mem_free);
  118 
  119   fields_[0] = KB(mem_total - mem_free - buffers - cached - slab);
  120   total_ =     KB(mem_total);
  121 
  122   setUsed(KB(mem_total - mem_free), KB(mem_total));
  123 }