From c3917e34b1c8b761eb8515b3992400643bc08212 Mon Sep 17 00:00:00 2001 From: shockrahwow Date: Tue, 3 Sep 2019 11:39:59 -0700 Subject: [PATCH] added cpu usage to statline makefile now has configurable options --- dwm-stat/Makefile | 18 +++++++++++++----- dwm-stat/status.c | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/dwm-stat/Makefile b/dwm-stat/Makefile index c6e0efe..a5108d9 100644 --- a/dwm-stat/Makefile +++ b/dwm-stat/Makefile @@ -1,11 +1,19 @@ +# Here are the configuraable options +# LAPTOP : enable batter level +# CPU_USAGE : CPU usage in percentage +# DEBUG : for fixing issues + +# NOTE: the -D is needed for gcc +# MFLAGS= -D MY_OPTION -D ANOTHER_OPT ... +MFLAGS=-D LAPTOP -D CPU_USAGE + + flags=-O2 -s -lX11 output=statline -stat: - gcc -o $(output) status.c $(flags) -laptop: - # compiling with LAPTOP=1 - gcc -D LAPTOP=1 status.c -o $(output) $(flags) + +stat: + gcc $(MFLAGS) -o $(output) status.c $(flags) run: ./statline diff --git a/dwm-stat/status.c b/dwm-stat/status.c index 1955384..fff87dc 100644 --- a/dwm-stat/status.c +++ b/dwm-stat/status.c @@ -20,6 +20,7 @@ // Time in seconds #define TIME_DELAY 5 #define MAXSTR 1024 +#define SMALL_BUF 32 #define MEM_BREAD_SIZE 32 #define STD_IO @@ -91,11 +92,39 @@ battery_percentage(void) #endif +#ifdef CPU_USAGE +// stat(2) -> +#define CPU_PROC_BASE +#define TOTAL "head /proc/stat -n 1 | cut -c 6- | sed 's/ /\\+/g' | bc" +#define IDLE "head /proc/stat -n 1 | cut -c 6- | awk '{print $4}'" static char* cpu_usage(void) { - return "CPU Usage Placeholder"; + // NOTE: not accounting for the query so this may be somewhat off by some metric + // Grabbing the total usage + FILE* query_p; + static char buf[SMALL_BUF]; + unsigned total, idle; + // get the total time + query_p = popen(TOTAL, "r"); + fgets(buf, sizeof(buf)-1, query_p); + total = atoi(buf); + pclose(query_p); + + // get the idle time + query_p = popen(IDLE, "r"); + fgets(buf, sizeof(buf)-1, query_p); + idle = atoi(buf); + pclose(query_p); + + double usage = 1.00 - ((double)idle / (double)total); +#ifdef DEBUG + printf("IDLE/TOTAL = %.02f\n", usage); +#endif + sprintf(buf, "CPU: %.02f % ", usage); + return buf; } +#endif static void XSetRoot(char* name) @@ -122,6 +151,9 @@ main(void) date_time, #ifdef LAPTOP battery_percentage, +#endif +#ifdef CPU_USAGE + cpu_usage, #endif };