diff options
author | Aaron Ball <nullspoon@oper.io> | 2022-03-21 09:34:17 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2022-03-21 09:34:17 -0600 |
commit | e22ce3145599dceb86973ec021ad3870b83e1c86 (patch) | |
tree | 62d20ee6b65182438c4b0f0e98baeea11279b879 | |
parent | 590766e394172f1ff1e054ff30063bdb482e1a7f (diff) | |
parent | 7efcb484614f1113545081fc9f3d74cbfb26bcc7 (diff) | |
download | i3cstat-e22ce3145599dceb86973ec021ad3870b83e1c86.tar.gz i3cstat-e22ce3145599dceb86973ec021ad3870b83e1c86.tar.xz |
Merge branch 'config_mem_meminfo'
-rw-r--r-- | src/config_mem.c | 34 | ||||
-rw-r--r-- | src/config_mem.h | 9 |
2 files changed, 38 insertions, 5 deletions
diff --git a/src/config_mem.c b/src/config_mem.c index 9028bc7..4227be1 100644 --- a/src/config_mem.c +++ b/src/config_mem.c @@ -24,17 +24,41 @@ void config_mem_init(struct node* n) { n->loadkey = &load_mem_key; } +int _meminfo_read(struct _meminfo* minfo) { + FILE* fd; + char buf[64]; + fd = fopen("/proc/meminfo", "r"); + if(!fd) + return -1; + while(fgets(buf, 64, fd) != NULL) { + if(strncmp(buf, "MemTotal: ", 10) == 0) + sscanf(buf, "MemTotal: %ld kB", &minfo->total); + else if(strncmp(buf, "MemFree: ", 9) == 0) + sscanf(buf, "MemFree: %ld", &minfo->free); + else if(strncmp(buf, "MemAvailable: ", 14) == 0) + sscanf(buf, "MemAvailable: %ld ", &minfo->available); + else if(strncmp(buf, "Buffers: ", 9) == 0) + sscanf(buf, "Buffers: %ld ", &minfo->buffers); + else if(strncmp(buf, "Cached: ", 8) == 0) + sscanf(buf, "Cached: %ld ", &minfo->cached); + else if(strncmp(buf, "SwapCached: ", 12) == 0) + sscanf(buf, "SwapCached: %ld ", &minfo->swapcached); + } + fclose(fd); + return 0; +} + void load_mem_key(struct node* n, char* key, char* val) { } int config_mem_load(struct node* n) { - struct sysinfo si; + struct _meminfo minfo; long used; int percent; - sysinfo(&si); - used = si.totalram - si.freeram - si.bufferram; - percent = used * 100 / si.totalram; + _meminfo_read(&minfo); + used = minfo.total - minfo.free - minfo.buffers - minfo.cached; + percent = used * 100 / minfo.total; if(percent < 20) { strcpy(n->color, C_GREEN); @@ -48,7 +72,7 @@ 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 / si.totalram, n->text); + print_bar(n->width, (double)used / minfo.total, n->text); } else { sprintf(n->text, "%02d%%", percent); } diff --git a/src/config_mem.h b/src/config_mem.h index 9298d7a..f330c1d 100644 --- a/src/config_mem.h +++ b/src/config_mem.h @@ -24,6 +24,15 @@ #define CTYPE_MEM 60 +struct _meminfo { + long total; + long free; + long available; + long buffers; + long cached; + long swapcached; +}; + void config_mem_init(struct node*); int config_mem_load(struct node*); void load_mem_key(struct node*, char*, char*); |