summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh <eeei3.joshua0307lol@gmail.com>2025-01-18 17:23:30 -0500
committerJosh <eeei3.joshua0307lol@gmail.com>2025-01-18 17:23:30 -0500
commit89810561a269ad6d11ecae58c49e209314e14c1a (patch)
tree0c71d3a81b6fbfe932657cb3388bd7af63ced89a
parent40ba4fb38336b7ebc26729f69c550cc50b404e7d (diff)
bugfix: Fixed my CLA
-rw-r--r--main.c120
1 files changed, 100 insertions, 20 deletions
diff --git a/main.c b/main.c
index 3d8223d..0c80d61 100644
--- a/main.c
+++ b/main.c
@@ -8,9 +8,46 @@
#include<unistd.h>
#include<time.h>
-#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 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);
@@ -20,14 +57,17 @@ void repos() {
printf("\033[H");
}
-void clear() {
- printf("\033[2J");
-}
-
void die(const char* reason, int ret) {
}
-int printcores() {
+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;
}
@@ -44,41 +84,81 @@ int main(int argc, char** argv) {
short cpu = 0;
short cores = 0;
int samples = 20;
- unsigned long tdelay = 500000;
-
+ 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 ((atoi(argv[2]) != 0) || (strlen(argv[2]) == 1)) {
+ if ((argc >= 3) && ((atoi(argv[2]) != 0) || (strlen(argv[2]) == 1))) {
tdelay = atoi(argv[2]);
+ start++;
}
- for (int i = 1; i < argc ; i++) {
- if (strstr(*argv, "--memory"))
+ for (int i = start; i < argc ; i++) {
+ if (strstr(argv[i], "--memory")) {
memory = 1;
- else if (strstr(*argv, "--cpu"))
+ }
+ else if (strstr(argv[i], "--cpu")) {
cpu = 1;
- else if (strstr(*argv, "--cores"))
+ }
+ else if (strstr(argv[i], "--cores")) {
cores = 1;
- else if (strstr(argv[i], "--samples="))
- samples = atoi(strstr(argv[i], "--samples="));
- else if (strstr(argv[i], "--tdelay="))
- tdelay = atoi(strstr(argv[i], "--tdelay="));
+ }
+ 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();
+ printcores(proc_data->mem);
+ free(initial_readings);
+ free(proc_data->mem);
+ free(proc_data);
return 0;
}