File: memmeter.cc

package info (click to toggle)
xosview 1.24-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,184 kB
  • sloc: cpp: 11,975; makefile: 154; ansic: 32; awk: 13; sh: 8
file content (123 lines) | stat: -rw-r--r-- 3,108 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
//  Copyright (c) 1994, 1995, 2006 by Mike Romberg ( mike.romberg@noaa.gov )
//  Copyright (c) 2015 Framestore
//
//  This file may be distributed under terms of the GPL
//

#include "memmeter.h"
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MEMFILENAME "/proc/meminfo"

#define KB(x) ((double)((x) * 1024))


MemMeter::MemMeter( XOSView *parent )
: FieldMeterGraph( parent, 5, "MEM", "USED/BUFF/SLAB/CACHE/FREE" ){
}

void MemMeter::checkResources( void ){
  FieldMeterGraph::checkResources();

  setfieldcolor( 0, parent_->getResource( "memUsedColor" ) );
  setfieldcolor( 1, parent_->getResource( "memBufferColor" ) );
  setfieldcolor( 2, parent_->getResource( "memSlabColor" ) );
  setfieldcolor( 3, parent_->getResource( "memCacheColor" ) );
  setfieldcolor( 4, parent_->getResource( "memFreeColor" ) );
  priority_ = atoi (parent_->getResource( "memPriority" ) );
  dodecay_ = parent_->isResourceTrue( "memDecay" );
  useGraph_ = parent_->isResourceTrue( "memGraph" );
  SetUsedFormat (parent_->getResource("memUsedFormat"));
}

void MemMeter::checkevent( void ){
  getstats();
  drawfields();
}

void MemMeter::getstats() {
  FILE *f;

  f = fopen(MEMFILENAME, "r");
  if (!f) {
    perror(MEMFILENAME);
    exit(1);
  }

  /*
   * The kernel's "unsigned long" values vary in size on 64-bit and
   * 32-bit implementations.
   *
   * But there's nothing saying userland will match the kernel; we
   * must pretty much accomodate anything in ASCII that /proc gives
   * us, so 64-bit integers are always used.
   */

  unsigned long long mem_total = 0,
    mem_free = 0,
    buffers = 0,
    slab = 0,
    cached = 0;

  for (;;) {
    char line[128];
    char *c, *endptr;
    unsigned long long kb = 0;

    /*
     * Parse lines in the format: "FieldName:      12345678 kB"
     *
     * We prefer to not use scanf because it's harder with variable
     * number of fields; the 'kB' is not present if value is 0
     */

    if (!fgets(line, sizeof line, f))
      break;

    c = strchr(line, ':');
    if (!c) {
      fprintf(stderr, MEMFILENAME ": parse error, ':' expected at '%s'\n", line);
      exit(1);
    }

    *c = '\0';
    c++;

    kb = strtoull(c, &endptr, 10);
    if (kb == ULLONG_MAX) {
      fprintf(stderr, MEMFILENAME ": parse error, '%s' is out of range\n", c);
      exit(1);
    }

    if (strcmp(line, "MemTotal") == 0)
      mem_total = kb;
    else if (strcmp(line, "MemFree") == 0)
      mem_free = kb;
    else if (strcmp(line, "Buffers") == 0)
      buffers = kb;
    else if (strcmp(line, "Cached") == 0)
      cached = kb;
    else if (strcmp(line, "Slab") == 0)
      slab = kb;
  }

  if (fclose(f) != 0)
    abort();

  /* Don't do arithmetic on the fields_ themselves; these are floating
   * point and when memory is large are affected by inaccuracy */

  fields_[1] = KB(buffers);
  fields_[2] = KB(slab);
  fields_[3] = KB(cached);
  fields_[4] = KB(mem_free);

  fields_[0] = KB(mem_total - mem_free - buffers - cached - slab);
  total_ =     KB(mem_total);

  setUsed(KB(mem_total - mem_free), KB(mem_total));
}