summaryrefslogtreecommitdiff
path: root/src/status/memusage.c
blob: 15523de20008e1e42c3d93b9a682ef355457ac25 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common.h"
#include "tools.h"


void status_memusage(GlobalData *g)
{
  StatusItem s;
  char text[16] = { 0 };

  char *stline = NULL;
  size_t stlen;
  FILE *stfile;

  int memtotal = 0;
  int memfree = 0;
  int memused;
  int membuffers = 0;
  int memcached = 0;


  statusitem_init(&s);
  s.text = text;

  stfile = fopen("/proc/meminfo", "r");
  if (stfile != NULL) {
    stlen = getline(&stline, &stlen, stfile);
    memtotal = atoi(&stline[17]);

    stlen = getline(&stline, &stlen, stfile);
    memfree = atoi(&stline[17]);

    stlen = getline(&stline, &stlen, stfile);
    if (stlen > 13 && !memcmp(stline, "MemAvailable:", 13)) {
      stlen = getline(&stline, &stlen, stfile);
    }
    membuffers = atoi(&stline[17]);

    stlen = getline(&stline, &stlen, stfile);
    memcached = atoi(&stline[17]);
    free(stline);

    fclose(stfile);

    memused = memtotal - memfree - memcached - membuffers;

    memused /= 1024;	// Just show MBs used

    /* Change color based on % of RAM used */
    if ((float)memused / (float)memtotal < 0.85) {
      s.color = "#22FF22";  // green
    } else {
      s.color = "#FF0000";  // red
    }

    snprintf(text, sizeof(text), "Mem: %d M", memused);

    line_append_item(g, &s);
  }
}