Super hacky shell wrapper around lemonbar that aims to poll as little as possible, minimising bar updates to the screen and reduce resource usage.
git clone https://mcol.xyz/code/lemontools
Log | Files | Refs | README | LICENSE

commit 117f52d5d77901f924d6c5c6bfbd364e21932228
parent 1e886c0f7d303f811dc26cefe3ca878c89b5e62b
Author: mcol <mcol@posteo.net>
Date:   Sun, 20 Jan 2019 13:37:55 +0000

added cpu module, and src directory to house other c code snippets

Diffstat:
Amakefile | 2++
Amodules/cpu | 12++++++++++++
Asrc/cpu | 0
Asrc/cpu.c | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 81 insertions(+), 0 deletions(-)

diff --git a/makefile b/makefile @@ -0,0 +1,2 @@ +atm: + @gcc src/cpu.c -o src/cpu diff --git a/modules/cpu b/modules/cpu @@ -0,0 +1,12 @@ +#---------------------------------------------------------------------------------- +# file server connections module +#---------------------------------------------------------------------------------- + +_cpu() { + $dir/src/cpu 3 | while read val + do + echo "cpu='${pad}${val}${pad}'" + done > $fifo 2>> $log & +} + +# vim: ft=sh diff --git a/src/cpu b/src/cpu Binary files differ. diff --git a/src/cpu.c b/src/cpu.c @@ -0,0 +1,67 @@ +// print current CPU usage every arg seconds, defaulting to every second + +#include "stdlib.h" +#include "stdio.h" +#include "string.h" +#include "unistd.h" + +static unsigned long long lastTotalUser, lastTotalUserLow, lastTotalSys, lastTotalIdle; + +void init(){ + FILE* file = fopen("/proc/stat", "r"); + fscanf(file, "cpu %llu %llu %llu %llu", &lastTotalUser, &lastTotalUserLow, + &lastTotalSys, &lastTotalIdle); + fclose(file); +} + +double getCurrentValue(){ + double percent; + FILE* file; + unsigned long long totalUser, totalUserLow, totalSys, totalIdle, total; + + file = fopen("/proc/stat", "r"); + fscanf(file, "cpu %llu %llu %llu %llu", &totalUser, &totalUserLow, + &totalSys, &totalIdle); + fclose(file); + + if (totalUser < lastTotalUser || totalUserLow < lastTotalUserLow || + totalSys < lastTotalSys || totalIdle < lastTotalIdle){ + //Overflow detection. Just skip this value. + percent = -1.0; + } + else{ + total = (totalUser - lastTotalUser) + (totalUserLow - lastTotalUserLow) + + (totalSys - lastTotalSys); + percent = total; + total += (totalIdle - lastTotalIdle); + percent /= total; + percent *= 100; + } + + lastTotalUser = totalUser; + lastTotalUserLow = totalUserLow; + lastTotalSys = totalSys; + lastTotalIdle = totalIdle; + + return percent; +} + + +void main(int argc, char *argv[]){ + + int sec; + if (argc > 1) { + char *a = argv[1]; + sec = atoi(a); + } + else + { + sec = 1; + } + + while(1) { + printf("%2.f\n", getCurrentValue()); + fflush(stdout); + sleep(sec); + } +}