diff options
author | Josh <joshua.liu@sourceobby.com> | 2025-01-21 10:35:11 -0500 |
---|---|---|
committer | Josh <joshua.liu@sourceobby.com> | 2025-01-21 10:35:11 -0500 |
commit | 1506c5505fe40e972f1eace7ba1755e3ee0264a2 (patch) | |
tree | a04bc32d88f28fdfd220213a8974ece7a90cd5bf | |
parent | d03cb95135eae338d8fa864f05325cd5cd5eb7e6 (diff) |
feat: finished most of the program, wrote code for getting cpu load, split up graphing functionality, adjusted repositioning
-rw-r--r-- | main.c | 118 |
1 files changed, 102 insertions, 16 deletions
@@ -6,12 +6,15 @@ #include<sys/sysinfo.h> #include<sys/types.h> #include<unistd.h> -#include<time.h> +#include<math.h> +#include<sys/time.h> #define CPUINFO "/proc/cpuinfo" -#define CPUFREQ "/sys/devices/system/cpu/cpu0/cpufreq/" +/*#define CPUFREQ "/sys/devices/system/cpu/cpu0/cpufreq/"*/ +#define LOADAVG "/proc/loadavg" #define REPOS "\033[H" // Re-positions the cursor to top left of screen #define CLEAR "\033[2J" +#define SPACE " " typedef struct Chunk chunk; @@ -58,6 +61,8 @@ void repos() { } void die(const char* reason, int ret) { + printf("%s\n", reason); + exit(ret); } int printcores(char* proc_data) { @@ -66,30 +71,91 @@ int printcores(char* proc_data) { int core_num = atoi(core_loc); float core_freq = atof(freq_loc); for (int i = 0; i < core_num; i++) { - ; + printf("+---+\n| |\n+---+\n"); } return 0; } -int printmemory() { +char* rightshift(char* data, size_t size) { + char* res = malloc(size * sizeof(char)); + *res = ' '; + memcpy(res + 1, data, size - 1); + free(data); + return res; +} + +// Graph with have a height of 10 characters and width of 12 +int printmemgraph(int max, int inuse, char** graphdata, char prog) { + float percentage = (float)inuse/(float)max; + int percent = floorf(percentage); + for (int i = 0; i < 10; i++) { + graphdata[i] = rightshift(graphdata[i], 12); + if ((percentage >= i) && (percentage <= i + 10)) { + graphdata[i][0] = prog; + } + printf("%s\n", graphdata[i]); + } return 0; } -int printcpu() { +int printcpugraph (float max, float inuse, char** graphdata, char prog) { + float percentage = inuse/max; + int percent = floorf(percentage); + if (inuse > max) { + graphdata[9][0] = prog; + } + else { + for (int i = 0; i < 10; i++) { + graphdata[i] = rightshift(graphdata[i], 12); + if ((percentage >= i) && (percentage <= i + 1)) { + graphdata[i][0] = prog; + } + printf("%s\n", graphdata[i]); + } + } return 0; } +float getload() { + chunk* data = malloc(sizeof(chunk)); + data->mem = malloc(1); + data->size = 0; + if (read_file(fopen(LOADAVG, "r"), data) == -1) { + die("Reading file error!", 4); + } + char* load = malloc(5 * sizeof(char)); + strncpy(load, data->mem, 4); + float res = atof(load); + free(load); + free(data->mem); + free(data); + return res; +} + +void wipe(char** arr, int samples) { + for (int i = 0; i < 10; i++) { + for (int r = 0; i < samples; r++) { + *(*(arr + i)+r) = ' '; + } + } +} + 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)); + long max_ram = 0; + int core_num = 0; + char** memgraph; + char** cpugraph; + + struct sysinfo system_read; chunk* proc_data = malloc(sizeof(chunk)); - if (!initial_readings || !proc_data) { + if (!proc_data) { die("Memory error!", 3); } proc_data->mem = malloc(1); @@ -97,14 +163,14 @@ int main(int argc, char** argv) { die("Memory error!", 3); } proc_data->size = 0; - if (!sysinfo(initial_readings)) { - free(initial_readings); + if (!sysinfo(&system_read)) { die("Unable to get system info!", 2); } if (read_file(fopen(CPUINFO, "r"), proc_data) == -1) { die("Reading file error!", 4); } int start = 1; + max_ram = system_read.totalram; if ((atoi(argv[1]) != 0) || (strlen(argv[1]) == 1)) { samples = atoi(argv[1]); start++; @@ -116,9 +182,19 @@ int main(int argc, char** argv) { for (int i = start; i < argc ; i++) { if (strstr(argv[i], "--memory")) { + memgraph = malloc(sizeof(char*) * 10); + for (int i = 0; i < 10; i++) { + *(memgraph+i) = malloc(sizeof(char) * samples); + } + wipe(memgraph, samples); memory = 1; } else if (strstr(argv[i], "--cpu")) { + cpugraph = malloc(sizeof(char*) * 10); + for (int i = 0; i < 10; i++) { + *(cpugraph+i) = malloc(sizeof(char) * samples); + } + wipe(cpugraph, samples); cpu = 1; } else if (strstr(argv[i], "--cores")) { @@ -131,7 +207,6 @@ int main(int argc, char** argv) { tdelay = atoi(strstr(argv[i], "--tdelay=") + 9); } else { - free(initial_readings); free(proc_data->mem); free(proc_data); printf("Invalid argument!"); @@ -139,25 +214,36 @@ int main(int argc, char** argv) { return 1; } } + + /* Main Logic Here*/ + struct timeval tv; if (samples < 0 || tdelay < 0) die("Invalid argument!", 1); for (int i = 0; i < samples; i++) { + gettimeofday(&tv, NULL); + prevtime = tv.tv_usec; + printf("%s", CLEAR); - prevtime = time(&sec); + printf("%s", REPOS); if (memory) { - printmemory(); + if (!sysinfo(&system_read)) { + die("Unable to get system info!", 2); + } + printmemgraph(max_ram, system_read.bufferram, memgraph, '#'); } + printf("\n"); if (cpu) { - printcpu(); + printcpugraph(1.0, getload()/(float)core_num, cpugraph,':'); } while (1) { - if (prevtime - time(&sec) >= tdelay) + gettimeofday(&tv, NULL); + if (prevtime - tv.tv_usec >= tdelay) break; } } if (cores) printcores(proc_data->mem); - free(initial_readings); + free(proc_data->mem); free(proc_data); return 0; |