#include #include #include #include #include #include #include #include #include #define CPUINFO "/proc/cpuinfo" #define CPUFREQ "/sys/devices/system/cpu/cpu0/cpufreq/" #define REPOS "\033[H" // Re-positions the cursor to top left of screen #define CLEAR "\033[2J" typedef struct Chunk chunk; struct Chunk { size_t size; char* mem; }; size_t read_file(FILE* file, chunk* out) { char* block = malloc(500 * sizeof(char)); if (!file) { return -1; } if (!block) { fclose(file); return -1; } size_t fileout = 0; size_t old = 0; fileout = fread(block, sizeof(char), 500, file); while (fileout != 0) { old = out->size; out->size = out->size + fileout; out->mem = realloc(out->mem, out->size); if (out->mem) { fclose(file); free(block); return -1; } memcpy(&(out->mem[old]), block, fileout); } free(block); fclose(file); return fileout; } void move(int row, int col) { printf("\x1b[%d;%df", row, col); } void repos() { printf("\033[H"); } void die(const char* reason, int ret) { } int printcores(char* proc_data) { char* core_loc = strstr(proc_data, "siblings"); char* freq_loc = strstr(proc_data, "cpu MHz"); int core_num = atoi(core_loc); float core_freq = atof(freq_loc); for (int i = 0; i < core_num; i++) { ; } return 0; } int printmemory() { return 0; } int printcpu() { return 0; } int main(int argc, char** argv) { short memory = 0; short cpu = 0; short cores = 0; int samples = 20; unsigned int tdelay = 500000; unsigned long prevtime; time_t sec; struct sysinfo* initial_readings = (struct sysinfo*)malloc(sizeof(struct sysinfo)); chunk* proc_data = malloc(sizeof(chunk)); if (!initial_readings || !proc_data) { die("Memory error!", 3); } proc_data->mem = malloc(1); if (!proc_data) { die("Memory error!", 3); } proc_data->size = 0; if (!sysinfo(initial_readings)) { free(initial_readings); die("Unable to get system info!", 2); } if (read_file(fopen(CPUINFO, "r"), proc_data) == -1) { die("Reading file error!", 4); } int start = 1; if ((atoi(argv[1]) != 0) || (strlen(argv[1]) == 1)) { samples = atoi(argv[1]); start++; } if ((argc >= 3) && ((atoi(argv[2]) != 0) || (strlen(argv[2]) == 1))) { tdelay = atoi(argv[2]); start++; } for (int i = start; i < argc ; i++) { if (strstr(argv[i], "--memory")) { memory = 1; } else if (strstr(argv[i], "--cpu")) { cpu = 1; } else if (strstr(argv[i], "--cores")) { cores = 1; } else if (strstr(argv[i], "--samples=")) { samples = atoi(argv[i] + 10); } else if (strstr(argv[i], "--tdelay=")) { tdelay = atoi(strstr(argv[i], "--tdelay=") + 9); } else { free(initial_readings); free(proc_data->mem); free(proc_data); printf("Invalid argument!"); die("Invalid argument!", 1); return 1; } } if (samples < 0 || tdelay < 0) die("Invalid argument!", 1); for (int i = 0; i < samples; i++) { printf("%s", CLEAR); prevtime = time(&sec); if (memory) { printmemory(); } if (cpu) { printcpu(); } while (1) { if (prevtime - time(&sec) >= tdelay) break; } } if (cores) printcores(proc_data->mem); free(initial_readings); free(proc_data->mem); free(proc_data); return 0; }