Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //compile hint: gcc statustest.c -I/opt/cuda/include -lnvidia-ml
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include <unistd.h>
- #include <sys/uio.h>
- #include <dirent.h>
- #include <sys/sysinfo.h>
- #include <sys/statvfs.h>
- #include <nvml.h>
- #include <time.h>
- int main() {
- FILE *fp;
- char *line = NULL;
- size_t len = 0;
- ssize_t nread;
- fp = fopen("/proc/stat", "r");
- int cores = -2;
- do {
- ++cores;
- nread = getline(&line, &len, fp);
- } while (*line == 'c');
- free(line);
- fclose(fp);
- int user, nice, sys, idle, coretemp;
- int *user_old, *nice_old, *sys_old, *idle_old;
- user_old = malloc(cores*sizeof(int));
- nice_old = malloc(cores*sizeof(int));
- sys_old = malloc(cores*sizeof(int));
- idle_old = malloc(cores*sizeof(int));
- DIR *d;
- struct dirent *dir;
- char hwmonPath[128], name[32], link[128], tstr[32];
- d = opendir("/sys/class/hwmon/");
- if (!d) {
- perror("Failed to open dir");
- return -1;
- }
- while((dir = readdir(d)) != NULL) {
- if (dir->d_type == DT_LNK) {
- snprintf(hwmonPath, sizeof(hwmonPath), "/sys/class/hwmon/%s", dir->d_name);
- realpath(hwmonPath, link);
- snprintf(hwmonPath, sizeof(hwmonPath), "%s/name", link);
- fp = fopen(hwmonPath, "r");
- if (!fp) {
- perror("File opening failed");
- return -1;
- }
- fscanf(fp, "%s", name);
- fclose(fp);
- if (strcmp(name, "coretemp") == 0) {
- snprintf(hwmonPath, sizeof(hwmonPath), "%s/temp1_input", link);
- break;
- }
- }
- }
- fp = fopen("/proc/stat", "r");
- if (!fp) {
- perror("File opening failed");
- return -1;
- }
- while(fgetc(fp) != '\n');
- for (unsigned i = 0; i < cores; ++i) {
- fscanf(fp, "%*s %d %d %d %d %*d %*d %*d %*d %*d %*d\n", &user_old[i], &nice_old[i], &sys_old[i], &idle_old[i]);
- }
- fclose(fp);
- char linefgets[256];
- int ramTotal = 0, ramAvailable, swapTotal, swapFree;
- fp = fopen("/proc/meminfo", "r");
- if (!fp) {
- perror("File opening failed");
- return -1;
- }
- while(fgets(linefgets, sizeof(linefgets), fp)) {
- if (!ramTotal)
- sscanf(linefgets, "MemTotal: %d kB", &ramTotal);
- if (sscanf(linefgets, "SwapTotal: %d kB", &swapTotal) == 1)
- break;
- }
- fclose(fp);
- nvmlReturn_t result;
- nvmlDevice_t* devices;
- nvmlUtilization_t util;
- unsigned temp;
- unsigned device_count;
- result = nvmlInit();
- if (NVML_SUCCESS != result) {
- printf("Failed to initialize NVML: %s\n", nvmlErrorString(result));
- return -1;
- }
- result = nvmlDeviceGetCount(&device_count);
- if (NVML_SUCCESS != result) {
- printf("Failed to query device count: %s\n", nvmlErrorString(result));
- return -1;
- }
- devices = malloc(sizeof(nvmlDevice_t)*device_count);
- for (unsigned i = 0; i < device_count; ++i) {
- result = nvmlDeviceGetHandleByIndex(i, &devices[i]);
- if (NVML_SUCCESS != result) {
- printf("Failed to get handle for device %i: %s\n", i, nvmlErrorString(result));
- return -1;
- }
- }
- struct statvfs fileSysStat;
- time_t t;
- struct tm *tmp;
- while (true) {
- sleep(1);
- printf("CPU Usage:");
- fp = fopen("/proc/stat", "r");
- if (!fp) {
- perror("File opening failed");
- return -1;
- }
- while(fgetc(fp) != '\n');
- for (unsigned i = 0; i < cores; ++i) {
- fscanf(fp, "%*s %d %d %d %d %*d %*d %*d %*d %*d %*d\n", &user, &nice, &sys, &idle);
- printf("%*.1f%%", 5, (float)(user+nice+sys-user_old[i]-nice_old[i]-sys_old[i])/(user+nice+sys+idle-user_old[i]-nice_old[i]-sys_old[i]-idle_old[i])*100);
- if (i != cores-1)
- printf(";");
- user_old[i] = user;
- nice_old[i] = nice;
- sys_old[i] = sys;
- idle_old[i] = idle;
- }
- fclose(fp);
- fp = fopen(hwmonPath, "r");
- if (!fp) {
- perror("File opening failed");
- return -1;
- }
- fscanf(fp, "%d", &coretemp);
- fclose(fp);
- fp = fopen("/proc/meminfo", "r");
- if (!fp) {
- perror("File opening failed");
- return -1;
- }
- ramAvailable = 0;
- while(fgets(linefgets, sizeof(linefgets), fp)) {
- if (!ramAvailable)
- sscanf(linefgets, "MemAvailable: %d kB", &ramAvailable);
- if (sscanf(linefgets, "SwapFree: %d kB", &swapFree) == 1)
- break;
- }
- fclose(fp);
- printf("(%*d°C) | RAM Usage:%*.1f%% | Swap Usage:%*.1f%% | GPU Usage:", 3, coretemp/1000, 5, (float)(ramTotal-ramAvailable)/ramTotal*100, 5, (float)(ramTotal-swapFree)/swapTotal*100);
- for (unsigned i = 0; i < device_count; ++i) {
- result = nvmlDeviceGetUtilizationRates(devices[i], &util);
- if (NVML_SUCCESS != result) {
- printf("Failed utilization for device %i: %s\n", i, nvmlErrorString(result));
- return -1;
- }
- result = nvmlDeviceGetTemperature(devices[i], NVML_TEMPERATURE_GPU, &temp);
- if (NVML_SUCCESS != result) {
- printf("Failed to get temperature for device %i: %s\n", i, nvmlErrorString(result));
- return -1;
- }
- printf("%*d%%(%*d°C)", 3, util.gpu, 2, temp);
- if (i != device_count-1)
- printf(";");
- }
- if (statvfs("/home", &fileSysStat) != 0) {
- perror("Getting the available disk space failed");
- return -1;
- }
- t = time(NULL);
- tmp = localtime(&t);
- strftime(tstr, sizeof(tstr), "%d-%m-%g %T", tmp);
- printf(" | Disk Space: %*dG | %s", 3, fileSysStat.f_bsize*fileSysStat.f_bfree/1073741824, tstr);
- fflush(stdout);
- }
- result = nvmlShutdown();
- if (NVML_SUCCESS != result)
- printf("Failed to shutdown NVML: %s\n", nvmlErrorString(result));
- free(user_old);
- free(nice_old);
- free(sys_old);
- free(idle_old);
- free(devices);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement