summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2021-06-01 10:23:31 -0600
committerAaron Ball <nullspoon@oper.io>2021-06-01 10:23:31 -0600
commitff3f57d3eb6c632ef1ccb6e6870d9f69af0c9943 (patch)
tree86e1f6cf92919d9552a1a97542ff761affea9b91 /src
parente8132f545d45e974523ac01bc1dfde854d7f4965 (diff)
downloadi3cstat-ff3f57d3eb6c632ef1ccb6e6870d9f69af0c9943.tar.gz
i3cstat-ff3f57d3eb6c632ef1ccb6e6870d9f69af0c9943.tar.xz
Move swap and mem modules to use sysinfo over meminfo
The meminfo is read and parsed as a file, which is complex and prone to breakage. The sysinfo library is part of the system and requires no parsing, so is much more reliable (and gives more granular numbers as well).
Diffstat (limited to 'src')
-rw-r--r--src/config_mem.c23
-rw-r--r--src/config_mem.h1
-rw-r--r--src/config_swap.c15
-rw-r--r--src/config_swap.h1
4 files changed, 14 insertions, 26 deletions
diff --git a/src/config_mem.c b/src/config_mem.c
index c85e86f..d579eac 100644
--- a/src/config_mem.c
+++ b/src/config_mem.c
@@ -28,22 +28,13 @@ void load_mem_key(struct node* n, char* key, char* val) {
}
int config_mem_load(struct node* n) {
- long total;
- long free;
- long available;
-
- int percent;
+ struct sysinfo si;
long used;
+ int percent;
- // See https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
- FILE* fd = fopen("/proc/meminfo", "r");
- fscanf(fd, "MemTotal: %ld kB ", &total);
- fscanf(fd, "MemFree: %ld kB ", &free);
- fscanf(fd, "MemAvailable: %ld kB ", &available);
- fclose(fd);
-
- used = total - available;
- percent = used * 100 / total;
+ sysinfo(&si);
+ used = si.totalram - si.freeram;
+ percent = used * 100 / si.totalram;
if(percent < 20) {
strcpy(n->color, C_GREEN);
@@ -57,12 +48,10 @@ int config_mem_load(struct node* n) {
strcpy(n->label_color, C_LGREY);
if(strcmp(n->display, "bar") == 0) {
- print_bar(n->width, (double)used / total, n->text);
+ print_bar(n->width, (double)used / si.totalram, n->text);
} else {
sprintf(n->text, "%02d%%", percent);
}
return 0;
}
-
-
diff --git a/src/config_mem.h b/src/config_mem.h
index 5e947d9..0451160 100644
--- a/src/config_mem.h
+++ b/src/config_mem.h
@@ -18,6 +18,7 @@
#ifndef CONFIG_MEM
#define CONFIG_MEM
+#include <sys/sysinfo.h>
#include "common.h"
#include "config_node.h"
diff --git a/src/config_swap.c b/src/config_swap.c
index c8469d5..090117f 100644
--- a/src/config_swap.c
+++ b/src/config_swap.c
@@ -29,16 +29,11 @@ void load_swap_key(struct node* n, char* key, char* val) {
}
int config_swap_load(struct node* n) {
- long total;
- long free;
+ struct sysinfo si;
int percent;
- FILE* fd = fopen("/proc/meminfo", "r");
- fscanf(fd, "SwapTotal: %ld kB ", &total);
- fscanf(fd, "SwapFree: %ld kB ", &free);
- fclose(fd);
-
- percent = (total - free) * 100 / total;
+ sysinfo(&si);
+ percent = (si.totalswap - si.freeswap) * 100 / si.totalswap;
if(percent < 20) {
strcpy(n->color, C_GREEN);
@@ -52,7 +47,9 @@ int config_swap_load(struct node* n) {
strcpy(n->label_color, C_LGREY);
if(strcmp(n->display, "bar") == 0) {
- print_bar(n->width, (double)(total - free) / total, n->text);
+ print_bar(n->width,
+ (double)(si.totalswap - si.freeswap) / si.totalswap,
+ n->text);
} else {
sprintf(n->text, "%02d%%", percent);
}
diff --git a/src/config_swap.h b/src/config_swap.h
index 52bb931..8cd9443 100644
--- a/src/config_swap.h
+++ b/src/config_swap.h
@@ -18,6 +18,7 @@
#ifndef CONFIG_SWAP
#define CONFIG_SWAP
+#include <sys/sysinfo.h>
#include "common.h"
#include "config_node.h"

Generated by cgit