diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 175 |
1 files changed, 114 insertions, 61 deletions
@@ -11,12 +11,12 @@ #define CPUINFO "/proc/cpuinfo" /*#define CPUFREQ "/sys/devices/system/cpu/cpu0/cpufreq/"*/ -#define LOADAVG "/proc/loadavg" -#define CPUTIME "/proc/sys" +#define CPUTIME "/proc/stat" #define REPOS "\033[H" // Re-positions the cursor to top left of screen #define CLEAR "\033[2J" #define SPACE " " #define PROCSPACE " " +#define GRAPHSTART 9 typedef struct Chunk chunk; @@ -42,12 +42,13 @@ size_t read_file(FILE* file, chunk* out) { old = out->size; out->size = out->size + fileout; out->mem = realloc(out->mem, out->size); - if (out->mem) { + if (!out->mem) { fclose(file); free(block); return -1; } memcpy(&(out->mem[old]), block, fileout); + fileout = fread(block, sizeof(char), 500, file); } free(block); fclose(file); @@ -67,62 +68,88 @@ void die(const char* reason, int ret) { exit(ret); } -int printcores(char* proc_data) { +int printcores(char* proc_data, int startrow) { 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); int fullcpurows = floorf((float) atoi(core_loc)/4.0); int remainder = atoi(core_loc) - fullcpurows * 4; + /*printf("%d\n", fullcpurows);*/ + move(startrow, 1); 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); } + move(startrow + (fullcpurows * 3) , 1); for (int i = 0; i < remainder; i++) { - } - for (int i = 0; i < core_num; i++) { - printf("+---+\n| |\n+---+\n"); + move(startrow + (fullcpurows * 3), (i * 5) + (i * 2) + 1); + printf("+---+\n"); + move(startrow + (fullcpurows * 3) + 1, (i * 5) + (i * 2) + 1); + printf("|%s|\n", PROCSPACE); + move(startrow + (fullcpurows * 3) + 2, (i * 5) + (i * 2) + 1); + printf("+---+\n"); + /*repos();*/ } return 0; } -char* rightshift(char* data, size_t size) { - char* res = malloc(size * sizeof(char)); - *res = ' '; - memcpy(res + 1, data, size - 1); - free(data); - return res; +void creategraph() { } -int printmemgraph(int max, int inuse, char** graphdata, char prog) { - float percentage = (float)inuse/(float)max; + +/*char* rightshift(char* data, size_t size) {*/ +/* char* res = malloc(size * sizeof(char));*/ +/* *res = ' ';*/ +/* memcpy(res + 1, data, size - 1);*/ +/* free(data);*/ +/* return res;*/ +/*}*/ + +/*int printmemgraph(int max, int inuse, char** graphdata, char prog) {*/ +int printmemgraph(float max, float inuse, char prog, int startrow) { + float percentage = inuse/max; int percent = floorf(percentage); + move(startrow, GRAPHSTART); for (int i = 0; i < 10; i++) { - graphdata[i] = rightshift(graphdata[i], 12); - if ((percentage >= i) && (percentage <= i + 10)) { - graphdata[i][0] = prog; + /*graphdata[i] = rightshift(graphdata[i], 12);*/ + if ((percent >= i) && (percent <= i + 1)) { + printf("%c", prog); + /*graphdata[i][0] = prog; */ + } + else { + printf(" "); } - printf("%s\n", graphdata[i]); + move(startrow + i, GRAPHSTART); + printf("\n"); } return 0; } -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]); +/*int printcpugraph (float max, float inuse, char** graphdata, char prog) {*/ +int printcpugraph (float inuse, char prog, int startrow) { + /*float percentage = inuse/max;*/ + /*int percent = floorf(percentage);*/ + /* if (inuse > max) {*/ + /*move(MEMGRAPHLOCROW, MEMGRAPHLOCCOL);*/ + /*printf("%c", prog);*/ + /* }*/ + /*else {*/ + move(startrow, GRAPHSTART); + for (int i = 0; i < 10; i++) { + /*graphdata[i] = rightshift(graphdata[i], 12);*/ + if ((inuse >= i) && (inuse <= i + 1)) { + printf("%c", prog); + /*graphdata[i][0] = prog; */ + } + else { + printf(" "); } + move(startrow + i, GRAPHSTART); + printf("\n"); } + /*}*/ return 0; } @@ -135,15 +162,24 @@ float getload() { } int inuse_time = 0; int total_time = 0; - int is_idle = 0; + int count = 0; + int num = 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); + while (count < 11) { + if (count != 4) { + if (strstr(read_line, "\n") != NULL) { + num = atoi(strtok(read_line, "\n")); + } + else if (strstr(read_line, "cpu") != NULL) { + num = 0; + } + else { + num = atoi(read_line); + } inuse_time+=num; total_time+=num; } @@ -152,12 +188,12 @@ float getload() { total_time+=num; } read_line = strtok(NULL, " "); - is_idle++; + count++; } free(load); free(data->mem); free(data); - return (float)inuse_time/(float)total_time; + return (float)inuse_time/(float)total_time; } void wipe(char** arr, int samples) { @@ -175,16 +211,21 @@ int main(int argc, char** argv) { short cores = 0; int samples = 20; unsigned int tdelay = 500000; + int memgraphstart = 3; + int cpugraphstart = 3; + int coregraphstart = 3; /*Variables pertaining to the running of the program*/ unsigned long prevtime; - long max_ram = 0; + float max_ram = 0.0; int core_num = 0; - char** memgraph; - char** cpugraph; + /*char** memgraph;*/ + /*char** cpugraph;*/ int cpu_rows = 0; int cpu_remainder = 0; + struct timeval tv; + /*Set up*/ struct sysinfo system_read; chunk* proc_data = malloc(sizeof(chunk)); if (!proc_data) { @@ -195,15 +236,18 @@ int main(int argc, char** argv) { die("Memory error!", 3); } proc_data->size = 0; - if (!sysinfo(&system_read)) { + if (sysinfo(&system_read) != 0) { die("Unable to get system info!", 2); } if (read_file(fopen(CPUINFO, "r"), proc_data) == -1) { die("Reading file error!", 4); } + + + /*Begin reading CLAs*/ int start = 1; - max_ram = system_read.totalram; - if ((atoi(argv[1]) != 0) || (strlen(argv[1]) == 1)) { + max_ram = (float) system_read.totalram / 1000000000; + if ((argc > 1) && ((atoi(argv[1]) != 0) || (strlen(argv[1]) == 1))) { samples = atoi(argv[1]); start++; } @@ -214,19 +258,20 @@ 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); + /* memgraph = malloc(sizeof(char*) * 10);*/ + /* for (int i = 0; i < 10; i++) {*/ + /**(memgraph+i) = malloc(sizeof(char) * samples);*/ + /* }*/ + /* wipe(memgraph, samples);*/ memory = 1; + coregraphstart = 16; } 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); + /* 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")) { @@ -248,33 +293,41 @@ int main(int argc, char** argv) { } /* Main Logic Here*/ - struct timeval tv; + float gigs = 0.0; 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); printf("%s", REPOS); + printf("Nbr of samples: %d -- every %d microseconds (%f secs)\n\n", samples, tdelay, (float)tdelay/(float)1000000); if (memory) { - if (!sysinfo(&system_read)) { + if (sysinfo(&system_read) != 0) { die("Unable to get system info!", 2); } - printmemgraph(max_ram, system_read.bufferram, memgraph, '#'); + gigs = (float) system_read.bufferram / 1000000000; + printf("v Memory %s%f GB", SPACE, gigs); + /*printmemgraph(max_ram, system_read.bufferram, memgraph, '#');*/ + printmemgraph(max_ram, gigs, '#', memgraphstart); } printf("\n"); if (cpu) { - printcpugraph(1.0, getload(), cpugraph,':'); + float load = getload(); + printf("v CPU %s%f %%", SPACE, load); + /*printcpugraph(1.0, getload(), cpugraph,':');*/ + printcpugraph(load, ':', coregraphstart); } while (1) { gettimeofday(&tv, NULL); - if (prevtime - tv.tv_usec >= tdelay) + if (prevtime - tv.tv_usec >= tdelay) { break; + } } } + if (cores) - printcores(proc_data->mem); + printcores(proc_data->mem, coregraphstart); free(proc_data->mem); free(proc_data); |