Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.70 KB | None | 0 0
  1. //compile hint: gcc statustest.c -I/opt/cuda/include -lnvidia-ml
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdbool.h>
  7. #include <unistd.h>
  8. #include <sys/uio.h>
  9. #include <dirent.h>
  10. #include <sys/sysinfo.h>
  11. #include <sys/statvfs.h>
  12. #include <nvml.h>
  13. #include <time.h>
  14.  
  15. int main() {
  16.   FILE *fp;
  17.   char *line = NULL;
  18.   size_t len = 0;
  19.   ssize_t nread;
  20.   fp = fopen("/proc/stat", "r");
  21.   int cores = -2;
  22.   do {
  23.     ++cores;
  24.     nread = getline(&line, &len, fp);
  25.   } while (*line == 'c');
  26.   free(line);
  27.   fclose(fp);
  28.   int user, nice, sys, idle, coretemp;
  29.   int *user_old, *nice_old, *sys_old, *idle_old;
  30.   user_old = malloc(cores*sizeof(int));
  31.   nice_old = malloc(cores*sizeof(int));
  32.   sys_old = malloc(cores*sizeof(int));
  33.   idle_old = malloc(cores*sizeof(int));
  34.   DIR *d;
  35.   struct dirent *dir;
  36.   char hwmonPath[128], name[32], link[128], tstr[32];
  37.   d = opendir("/sys/class/hwmon/");
  38.   if (!d) {
  39.     perror("Failed to open dir");
  40.     return -1;
  41.   }
  42.   while((dir = readdir(d)) != NULL) {
  43.     if (dir->d_type == DT_LNK) {
  44.       snprintf(hwmonPath, sizeof(hwmonPath), "/sys/class/hwmon/%s", dir->d_name);
  45.       realpath(hwmonPath, link);
  46.       snprintf(hwmonPath, sizeof(hwmonPath), "%s/name", link);
  47.       fp = fopen(hwmonPath, "r");
  48.       if (!fp) {
  49.         perror("File opening failed");
  50.         return -1;
  51.       }
  52.       fscanf(fp, "%s", name);
  53.       fclose(fp);
  54.       if (strcmp(name, "coretemp") == 0) {
  55.         snprintf(hwmonPath, sizeof(hwmonPath), "%s/temp1_input", link);
  56.         break;
  57.       }
  58.     }
  59.   }
  60.  
  61.   fp = fopen("/proc/stat", "r");
  62.   if (!fp) {
  63.     perror("File opening failed");
  64.     return -1;
  65.   }
  66.   while(fgetc(fp) != '\n');
  67.   for (unsigned i = 0; i < cores; ++i) {
  68.     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]);
  69.   }
  70.   fclose(fp);
  71.   char linefgets[256];
  72.   int ramTotal = 0, ramAvailable, swapTotal, swapFree;
  73.   fp = fopen("/proc/meminfo", "r");
  74.   if (!fp) {
  75.     perror("File opening failed");
  76.     return -1;
  77.   }
  78.   while(fgets(linefgets, sizeof(linefgets), fp)) {
  79.     if (!ramTotal)
  80.       sscanf(linefgets, "MemTotal: %d kB", &ramTotal);
  81.     if (sscanf(linefgets, "SwapTotal: %d kB", &swapTotal) == 1)
  82.       break;
  83.   }
  84.   fclose(fp);
  85.  
  86.   nvmlReturn_t result;
  87.   nvmlDevice_t* devices;
  88.   nvmlUtilization_t util;
  89.   unsigned temp;
  90.   unsigned device_count;
  91.   result = nvmlInit();
  92.   if (NVML_SUCCESS != result) {
  93.       printf("Failed to initialize NVML: %s\n", nvmlErrorString(result));
  94.       return -1;
  95.   }
  96.   result = nvmlDeviceGetCount(&device_count);
  97.   if (NVML_SUCCESS != result) {
  98.       printf("Failed to query device count: %s\n", nvmlErrorString(result));
  99.       return -1;
  100.   }
  101.   devices = malloc(sizeof(nvmlDevice_t)*device_count);
  102.   for (unsigned i = 0; i < device_count; ++i) {
  103.     result = nvmlDeviceGetHandleByIndex(i, &devices[i]);
  104.     if (NVML_SUCCESS != result) {
  105.         printf("Failed to get handle for device %i: %s\n", i, nvmlErrorString(result));
  106.         return -1;
  107.     }
  108.   }
  109.  
  110.   struct statvfs fileSysStat;
  111.  
  112.   time_t t;
  113.   struct tm *tmp;
  114.  
  115.   while (true) {
  116.     sleep(1);
  117.  
  118.     printf("CPU Usage:");
  119.     fp = fopen("/proc/stat", "r");
  120.     if (!fp) {
  121.       perror("File opening failed");
  122.       return -1;
  123.     }
  124.     while(fgetc(fp) != '\n');
  125.     for (unsigned i = 0; i < cores; ++i) {
  126.       fscanf(fp, "%*s %d %d %d %d %*d %*d %*d %*d %*d %*d\n", &user, &nice, &sys, &idle);
  127.       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);
  128.       if (i != cores-1)
  129.         printf(";");
  130.       user_old[i] = user;
  131.       nice_old[i] = nice;
  132.       sys_old[i] = sys;
  133.       idle_old[i] = idle;
  134.     }
  135.     fclose(fp);
  136.  
  137.     fp = fopen(hwmonPath, "r");
  138.     if (!fp) {
  139.       perror("File opening failed");
  140.       return -1;
  141.     }
  142.     fscanf(fp, "%d", &coretemp);
  143.     fclose(fp);
  144.  
  145.     fp = fopen("/proc/meminfo", "r");
  146.     if (!fp) {
  147.       perror("File opening failed");
  148.       return -1;
  149.     }
  150.     ramAvailable = 0;
  151.     while(fgets(linefgets, sizeof(linefgets), fp)) {
  152.       if (!ramAvailable)
  153.         sscanf(linefgets, "MemAvailable: %d kB", &ramAvailable);
  154.       if (sscanf(linefgets, "SwapFree: %d kB", &swapFree) == 1)
  155.         break;
  156.     }
  157.     fclose(fp);
  158.     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);
  159.     for (unsigned i = 0; i < device_count; ++i) {
  160.       result = nvmlDeviceGetUtilizationRates(devices[i], &util);
  161.       if (NVML_SUCCESS != result) {
  162.           printf("Failed utilization for device %i: %s\n", i, nvmlErrorString(result));
  163.           return -1;
  164.       }
  165.       result = nvmlDeviceGetTemperature(devices[i], NVML_TEMPERATURE_GPU, &temp);
  166.       if (NVML_SUCCESS != result) {
  167.           printf("Failed to get temperature for device %i: %s\n", i, nvmlErrorString(result));
  168.           return -1;
  169.       }
  170.       printf("%*d%%(%*d°C)", 3, util.gpu, 2, temp);
  171.       if (i != device_count-1)
  172.         printf(";");
  173.     }
  174.  
  175.     if (statvfs("/home", &fileSysStat) != 0) {
  176.       perror("Getting the available disk space failed");
  177.       return -1;
  178.     }
  179.     t = time(NULL);
  180.     tmp = localtime(&t);
  181.     strftime(tstr, sizeof(tstr), "%d-%m-%g %T", tmp);
  182.     printf(" | Disk Space: %*dG | %s", 3, fileSysStat.f_bsize*fileSysStat.f_bfree/1073741824, tstr);
  183.     fflush(stdout);
  184.   }
  185.  
  186.   result = nvmlShutdown();
  187.   if (NVML_SUCCESS != result)
  188.       printf("Failed to shutdown NVML: %s\n", nvmlErrorString(result));
  189.   free(user_old);
  190.   free(nice_old);
  191.   free(sys_old);
  192.   free(idle_old);
  193.   free(devices);
  194.   return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement