commit 202e16587584cf0d57b99f986627286c19fad5dd
parent 89f0b70ddf7baf7c5079ccbeb5f313030937665d
Author: tongong <tongong@gmx.net>
Date: Tue, 13 Jul 2021 17:32:16 +0200
[dsblocks] made cpu block core number agnostic
Diffstat:
1 file changed, 21 insertions(+), 2 deletions(-)
diff --git a/suckless/dsblocks/blocks/cpu.c b/suckless/dsblocks/blocks/cpu.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <string.h>
#include "../util.h"
#include "cpu.h"
@@ -8,8 +9,26 @@
/* with /proc/stat one could watch every single core */
#define STATFILE "/proc/uptime"
#define MAX_CHARS_PER_LINE 40
+#define CPUINFOFILE "/proc/cpuinfo"
/* IMPORTANT: idle is the sum of the cores, but uptime real time */
-#define CORE_NUM 8
+
+int get_core_number() {
+ static int corenum = 0;
+
+ // core number only has to be read once
+ if (!corenum) {
+ FILE *cpufile;
+ char tmpstring[MAX_CHARS_PER_LINE];
+
+ cpufile = fopen(CPUINFOFILE, "r");
+ while (fgets(tmpstring, MAX_CHARS_PER_LINE, cpufile)) {
+ if (!strncmp(tmpstring, "processor", 9)) corenum++;
+ }
+ fclose(cpufile);
+ }
+
+ return corenum;
+}
size_t cpuu(char *str, int sigval)
{
@@ -32,7 +51,7 @@ size_t cpuu(char *str, int sigval)
sscanf(tmpstring, "%f %f", &uptime, &idle);
- usage = 100 * (1 - ((idle - last_idle) / CORE_NUM / (uptime - last_uptime)));
+ usage = 100 * (1 - ((idle - last_idle) / get_core_number() / (uptime - last_uptime)));
last_uptime = uptime;
last_idle = idle;