Compare commits

..

4 commits
main ... waybar

Author SHA1 Message Date
Noah Swerhun ba923d5d38 made infinite loop due to waybar's design 2024-05-24 22:07:57 -05:00
Noah Swerhun 449568ee35 updated name 2024-05-24 18:55:09 -05:00
Noah Swerhun 1b7ded5f8a added waybar usage info 2024-05-24 18:53:16 -05:00
Noah Swerhun 8b4721dcf5 waybar version 2024-05-24 18:44:00 -05:00
6 changed files with 142 additions and 37 deletions

View file

@ -1,17 +1,27 @@
# pss-total
# pss-total-waybar
sum proportional set size of every process for accurate memory usage statistics.
In non-pretty mode, the number is given in kibibytes.
Checkout the `waybar` branch for a version of this program which outputs JSON
[for use with waybar](htps://github.com/Alexays/Waybar/wiki/Module:-Custom)
This version is for use with [waybar](https://github.com/Alexays/Waybar). It
outputs JSON that waybar can interpret using a
[custom module](https://github.com/Alexays/Waybar/wiki/Module:-Custom).
Here is an example configuration:
```json
"custom/pss-memory": {
"exec": "pss-total-waybar",
"return-type": "json",
"restart-interval": 3,
"format": "{} {icon}",
"format-icons": ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"]
}
```
Licensed under GPLv3.
### Summary
pss-total [OPTIONS]
pss-total-waybar [OPTIONS]
### Options
- `-p`, `--pretty`: format output using sensible units: KiB, MiB, or GiB.
- `-h`, `--help`: print this help message.

View file

@ -40,8 +40,8 @@ build $builddir/release/dep: mkdir
build $builddir/release/obj/src-main.c.o: cc_release src/main.c
dep = $builddir/release/dep/src-main.c.o.d
build $builddir/release/pss-total: link_release $builddir/release/obj/src-main.c.o | || $builddir/release/obj $builddir/release/dep
build release: phony $builddir/release/pss-total
build $builddir/release/pss-total-waybar: link_release $builddir/release/obj/src-main.c.o | || $builddir/release/obj $builddir/release/dep
build release: phony $builddir/release/pss-total-waybar
# END TARGET release
# BEGIN TARGET main
@ -60,8 +60,8 @@ build $builddir/main/dep: mkdir
build $builddir/main/obj/src-main.c.o: cc_main src/main.c
dep = $builddir/main/dep/src-main.c.o.d
build $builddir/main/pss-total: link_main $builddir/main/obj/src-main.c.o | || $builddir/main/obj $builddir/main/dep
build main: phony $builddir/main/pss-total
build $builddir/main/pss-total-waybar: link_main $builddir/main/obj/src-main.c.o | || $builddir/main/obj $builddir/main/dep
build main: phony $builddir/main/pss-total-waybar
# END TARGET main
# BEGIN TARGET debug
@ -80,6 +80,6 @@ build $builddir/debug/dep: mkdir
build $builddir/debug/obj/src-main.c.o: cc_debug src/main.c
dep = $builddir/debug/dep/src-main.c.o.d
build $builddir/debug/pss-total: link_debug $builddir/debug/obj/src-main.c.o | || $builddir/debug/obj $builddir/debug/dep
build debug: phony $builddir/debug/pss-total
build $builddir/debug/pss-total-waybar: link_debug $builddir/debug/obj/src-main.c.o | || $builddir/debug/obj $builddir/debug/dep
build debug: phony $builddir/debug/pss-total-waybar
# END TARGET debug

View file

@ -1,2 +1,2 @@
mkdir -p /usr/local/bin
cp build/release/pss-total /usr/local/bin
cp build/release/pss-total-waybar /usr/local/bin

View file

@ -2,7 +2,7 @@
compile_commands = true
[targets.main]
outfile = "pss-total"
outfile = "pss-total-waybar"
compiler_flags = ["-I."]
sources = [
"src/main.c",

View file

@ -2,9 +2,11 @@
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "lib/include/ns_fget_line.h"
#include "lib/include/ns_str.h"
@ -33,7 +35,7 @@ double read_file(const char *filename) {
if (status == EOF) {
if (errno != 0) {
perror("error reading file");
perror("error reading smaps file");
ns_str_destroy(&line);
return 0;
} else {
@ -59,7 +61,7 @@ double read_file(const char *filename) {
return sum;
}
int main(int argc, char **argv) {
double get_total_KiB(void) {
DIR *proc_dp;
struct dirent *proc_ent;
Str smaps_path = {0};
@ -100,40 +102,133 @@ int main(int argc, char **argv) {
closedir(proc_dp);
if (argc == 2) {
if ((strcmp(argv[1], "--pretty") == 0) || (strcmp(argv[1], "-p") == 0)) {
const char *prefixes[] = {"KiB", "MiB", "GiB"};
size_t i = 0;
while (total_KiB >= 1024 && i < 2) {
total_KiB /= 1024;
i++;
return total_KiB;
}
size_t get_memtotal(void) {
FILE *meminfo_fp;
Str line = {0};
int status;
size_t memtotal;
errno = 0;
meminfo_fp = fopen("/proc/meminfo", "r");
if (meminfo_fp == NULL) {
perror("cannot open file pointer to /proc/meminfo");
assert(meminfo_fp != NULL);
}
printf("%0.2f %s\n", total_KiB, prefixes[i]);
exit(0);
} else if ((strcmp(argv[1], "--help") == 0) ||
(strcmp(argv[1], "-h") == 0)) {
ns_str_init_empty(&line, 256);
errno = 0;
status = ns_fget_line(meminfo_fp, &line);
if (status == EOF) {
if (errno != 0) {
perror("error reading meminfo");
fclose(meminfo_fp);
ns_str_destroy(&line);
assert(errno == 0);
} else {
fprintf(stderr, "/proc/meminfo is empty... something is wrong.\n");
fclose(meminfo_fp);
ns_str_destroy(&line);
exit(1);
}
}
status = sscanf(line.arr, "MemTotal: %lu", &memtotal);
if (status != 1) {
fprintf(stderr, "MemTotal is not on the first line of /proc/meminfo??? \n");
fclose(meminfo_fp);
ns_str_destroy(&line);
exit(1);
}
fclose(meminfo_fp);
ns_str_destroy(&line);
return memtotal;
}
void get_json(char *json, size_t json_len) {
const char *prefixes[] = {"KiB", "MiB", "GiB"};
const char *classes[] = {"low", "quarter", "half", "three_quarters",
"nine_tenths"};
char mem_text[16] = {0};
size_t prefix_index = 0, classes_index = 0;
double total_KiB = 0, pretty_total = 0, percentage_use = 0;
total_KiB = get_total_KiB();
pretty_total = total_KiB;
while (pretty_total >= 1024 && prefix_index < 2) {
pretty_total /= 1024;
prefix_index++;
}
snprintf((char *restrict)&mem_text, 16, "%0.2f %s", pretty_total,
prefixes[prefix_index]);
percentage_use = total_KiB / get_memtotal();
classes_index = 0;
if (percentage_use >= 0.9) {
classes_index = 4;
} else if (percentage_use >= 0.75) {
classes_index = 3;
} else if (percentage_use >= 0.50) {
classes_index = 2;
} else if (percentage_use >= 0.25) {
classes_index = 1;
}
snprintf(json, json_len,
"{\"text\":\"%s\",\"class\":\"%s\",\"percentage\":%0.2f}", mem_text,
classes[classes_index], percentage_use * 100);
}
// signal stuff
static volatile sig_atomic_t run = 1;
static void stop(int _) { run = 0; }
#define JSON_LEN 64
int main(int argc, char **argv) {
char json[JSON_LEN] = {0};
if (argc == 2) {
if ((strcmp(argv[1], "--help") == 0) || (strcmp(argv[1], "-h") == 0)) {
printf("DESCRIPTION\n");
printf(
" pss-total: sum proportional set size of every process for "
" pss-total-waybar: sum proportional set size of every process for "
"accurate memory\n");
printf(
" usage statistics. In non-pretty mode, the number is given in "
"kibibytes.\n");
" usage statistics. This branch outputs json for use in waybar.\n");
printf("\n");
printf("SUMMARY\n");
printf(" pss-total [OPTIONS]\n");
printf(" pss-total-waybar [OPTIONS]\n");
printf("\n");
printf("OPTIONS\n");
printf(" -p, --pretty\n");
printf(" format output using sensible units: KiB, MiB, or GiB.\n");
printf("\n");
printf(" -h, --help\n");
printf(" print this help message.\n");
exit(0);
}
}
printf("%.0f\n", total_KiB);
signal(SIGINT, stop);
signal(SIGTERM, stop);
while (run) {
memset(json, 0, JSON_LEN);
get_json((char *)&json, JSON_LEN);
printf("%s\n", json);
fflush(stdout);
if (run) {
sleep(3);
}
}
return 0;
}

View file

@ -1 +1 @@
rm /usr/local/bin/pss-total
rm /usr/local/bin/pss-total-waybar