WaybarConfig/gpu_monitor.c

44 lines
1.2 KiB
C
Raw Normal View History

#include <stdio.h>
#include <dirent.h>
#define INVALID -128
int read_int(char* filename) {
printf("Reading filename %s\n", filename);
int output = INVALID;
FILE* pInput = fopen(filename, "r");
if (pInput != NULL) {
fscanf(pInput, "%d", &output);
fclose(pInput);
} else printf("No such file\n");
return output;
}
int main() {
FILE* pOutput;
FILE* pInput;
char filename[] = "/sys/class/drm/card?/device/gpu_busy_percent"; // ? is filename[19], overwrite each loop
char hwmon_dirname[] = "/sys/class/drm/card?/device/hwmon"; // ? is hwmon_dirname[19], overwrite each loop
for (int i = 0; i < 8; i++) {
int usage = 0;
int temperature = 0;
char sOutput[16];
filename[19] = i + 48;
hwmon_dirname[19] = i + 48;
usage = read_int(filename);
if (usage == INVALID) break;
DIR* folder = opendir(hwmon_dirname);
if (folder == NULL) break;
closedir(folder);
char filename2[] = "/sys/class/drm/card0/device/hwmon/hwmon*/temp1_input";
filename2[19] = i + 48;
temperature = read_int(filename2);
if (temperature == INVALID) break;
// sprintf(sOutput, "%3d% %3d℃", usage, temperature);
printf("%3d% %3d℃", usage, temperature);
}
return 0;
}