diff options
author | Josh <joshua.liu@sourceobby.com> | 2025-01-22 11:26:26 -0500 |
---|---|---|
committer | Josh <joshua.liu@sourceobby.com> | 2025-01-22 11:26:26 -0500 |
commit | 429b9aff5f1347be5ebbe118680ee699c3b5e908 (patch) | |
tree | b4f619e396c2faf802667d1bd98b05c0268cfc3e | |
parent | 1506c5505fe40e972f1eace7ba1755e3ee0264a2 (diff) |
feat: rewrote reading cpu utilization, updated printing cores
-rw-r--r-- | main.c | 50 |
1 files changed, 41 insertions, 9 deletions
@@ -12,9 +12,11 @@ #define CPUINFO "/proc/cpuinfo" /*#define CPUFREQ "/sys/devices/system/cpu/cpu0/cpufreq/"*/ #define LOADAVG "/proc/loadavg" +#define CPUTIME "/proc/sys" #define REPOS "\033[H" // Re-positions the cursor to top left of screen #define CLEAR "\033[2J" #define SPACE " " +#define PROCSPACE " " typedef struct Chunk chunk; @@ -70,6 +72,15 @@ int printcores(char* proc_data) { char* freq_loc = strstr(proc_data, "cpu MHz"); int core_num = atoi(core_loc); float core_freq = atof(freq_loc); + int fullcpurows = floorf((float) atoi(core_loc)/4.0); + int remainder = atoi(core_loc) - fullcpurows * 4; + for (int i = 0; i < fullcpurows; i++) { + printf("+---+%s+---+%s+---+%s+---+\n", SPACE, SPACE, SPACE); + printf("|%s|%s|%s|%s|%s|%s|%s|\n", PROCSPACE, SPACE, PROCSPACE, SPACE, PROCSPACE, SPACE, PROCSPACE); + printf("+---+%s+---+%s+---+%s+---+\n", SPACE, SPACE, SPACE); + } + for (int i = 0; i < remainder; i++) { + } for (int i = 0; i < core_num; i++) { printf("+---+\n| |\n+---+\n"); } @@ -84,7 +95,6 @@ char* rightshift(char* data, size_t size) { 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); @@ -120,16 +130,34 @@ float getload() { chunk* data = malloc(sizeof(chunk)); data->mem = malloc(1); data->size = 0; - if (read_file(fopen(LOADAVG, "r"), data) == -1) { + if (read_file(fopen(CPUTIME, "r"), data) == -1) { die("Reading file error!", 4); } - char* load = malloc(5 * sizeof(char)); - strncpy(load, data->mem, 4); - float res = atof(load); + int inuse_time = 0; + int total_time = 0; + int is_idle = 0; + char* cpu_line = strstr(data->mem, "cpu0"); + long diff =(long) strstr(cpu_line, "\n") - (long) cpu_line; + char* load = malloc((diff * sizeof(char))); + strncpy(load, cpu_line, diff); + char* read_line = strtok(cpu_line, " "); + while (read_line != NULL) { + if (is_idle != 3) { + int num = atoi(read_line); + inuse_time+=num; + total_time+=num; + } + else { + int num = atoi(read_line); + total_time+=num; + } + read_line = strtok(NULL, " "); + is_idle++; + } free(load); free(data->mem); free(data); - return res; + return (float)inuse_time/(float)total_time; } void wipe(char** arr, int samples) { @@ -141,17 +169,21 @@ void wipe(char** arr, int samples) { } int main(int argc, char** argv) { - + /*Variables pertaining to program CLA*/ short memory = 0; short cpu = 0; short cores = 0; int samples = 20; unsigned int tdelay = 500000; + + /*Variables pertaining to the running of the program*/ unsigned long prevtime; long max_ram = 0; int core_num = 0; char** memgraph; char** cpugraph; + int cpu_rows = 0; + int cpu_remainder = 0; struct sysinfo system_read; chunk* proc_data = malloc(sizeof(chunk)); @@ -233,11 +265,11 @@ int main(int argc, char** argv) { } printf("\n"); if (cpu) { - printcpugraph(1.0, getload()/(float)core_num, cpugraph,':'); + printcpugraph(1.0, getload(), cpugraph,':'); } while (1) { gettimeofday(&tv, NULL); - if (prevtime - tv.tv_usec >= tdelay) + if (prevtime - tv.tv_usec >= tdelay) break; } } |