made infinite loop due to waybar's design
This commit is contained in:
parent
449568ee35
commit
ba923d5d38
3 changed files with 71 additions and 53 deletions
|
@ -1,6 +1,5 @@
|
|||
# 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.
|
||||
|
||||
This version is for use with [waybar](https://github.com/Alexays/Waybar). It
|
||||
outputs JSON that waybar can interpret using a
|
||||
|
|
40
build.ninja
40
build.ninja
|
@ -24,6 +24,26 @@ build $builddir/compile_commands.json: regen_compile_commands ngen.toml || $buil
|
|||
build build.ninja: regen_ninjafile ngen.toml || $builddir/compile_commands.json
|
||||
pool = console
|
||||
|
||||
# BEGIN TARGET release
|
||||
rule cc_release
|
||||
deps = gcc
|
||||
depfile = $dep
|
||||
command = cc -I. -flto -O2 -MD -MF $dep -o $out -c $in
|
||||
description = Building $in -> $out
|
||||
rule link_release
|
||||
command = cc -flto -o $out $in
|
||||
description = Linking $out
|
||||
|
||||
build $builddir/release/obj: mkdir
|
||||
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-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
|
||||
rule cc_main
|
||||
deps = gcc
|
||||
|
@ -63,23 +83,3 @@ build $builddir/debug/obj/src-main.c.o: cc_debug src/main.c
|
|||
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
|
||||
|
||||
# BEGIN TARGET release
|
||||
rule cc_release
|
||||
deps = gcc
|
||||
depfile = $dep
|
||||
command = cc -I. -flto -O2 -MD -MF $dep -o $out -c $in
|
||||
description = Building $in -> $out
|
||||
rule link_release
|
||||
command = cc -flto -o $out $in
|
||||
description = Linking $out
|
||||
|
||||
build $builddir/release/obj: mkdir
|
||||
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-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
|
||||
|
|
77
src/main.c
77
src/main.c
|
@ -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"
|
||||
|
@ -22,15 +24,7 @@ double read_file(const char *filename) {
|
|||
fp = fopen(filename, "r");
|
||||
|
||||
if (fp == NULL) {
|
||||
switch (errno) {
|
||||
case EACCES:
|
||||
return 0;
|
||||
break;
|
||||
default:
|
||||
perror("cannot open file pointer to smaps");
|
||||
assert(fp != NULL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (1) {
|
||||
|
@ -133,10 +127,12 @@ size_t get_memtotal(void) {
|
|||
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);
|
||||
}
|
||||
|
@ -146,16 +142,18 @@ size_t get_memtotal(void) {
|
|||
|
||||
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;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
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"};
|
||||
|
@ -163,25 +161,6 @@ int main(int argc, char **argv) {
|
|||
size_t prefix_index = 0, classes_index = 0;
|
||||
double total_KiB = 0, pretty_total = 0, percentage_use = 0;
|
||||
|
||||
if (argc == 2) {
|
||||
if ((strcmp(argv[1], "--help") == 0) || (strcmp(argv[1], "-h") == 0)) {
|
||||
printf("DESCRIPTION\n");
|
||||
printf(
|
||||
" pss-total-waybar: sum proportional set size of every process for "
|
||||
"accurate memory\n");
|
||||
printf(
|
||||
" usage statistics. This branch outputs json for use in waybar.\n");
|
||||
printf("\n");
|
||||
printf("SUMMARY\n");
|
||||
printf(" pss-total-waybar [OPTIONS]\n");
|
||||
printf("\n");
|
||||
printf("OPTIONS\n");
|
||||
printf(" -h, --help\n");
|
||||
printf(" print this help message.\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
total_KiB = get_total_KiB();
|
||||
pretty_total = total_KiB;
|
||||
|
||||
|
@ -206,10 +185,50 @@ int main(int argc, char **argv) {
|
|||
classes_index = 1;
|
||||
}
|
||||
|
||||
printf("{\"text\":\"%s\",\"class\":\"%s\",\"percentage\":%f}\n", mem_text,
|
||||
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-waybar: sum proportional set size of every process for "
|
||||
"accurate memory\n");
|
||||
printf(
|
||||
" usage statistics. This branch outputs json for use in waybar.\n");
|
||||
printf("\n");
|
||||
printf("SUMMARY\n");
|
||||
printf(" pss-total-waybar [OPTIONS]\n");
|
||||
printf("\n");
|
||||
printf("OPTIONS\n");
|
||||
printf(" -h, --help\n");
|
||||
printf(" print this help message.\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue