summaryrefslogtreecommitdiff
path: root/main.c
blob: 4d16d81cade0e331864e456c332a9d49bb9353e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/resource.h>
#include<sys/utsname.h>
#include<sys/sysinfo.h>
#include<sys/types.h>
#include<unistd.h>
#include<math.h>
#include<sys/time.h>

#define CPUINFO "/proc/cpuinfo"
/*#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;

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) {
    printf("%s\n", reason);
    exit(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++) {
	printf("+---+\n|   |\n+---+\n");
    }
    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;
}

// 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 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;
    long max_ram = 0;
    int core_num = 0;
    char** memgraph;
    char** cpugraph;

    struct sysinfo system_read;
    chunk* proc_data = malloc(sizeof(chunk));
    if (!proc_data) {
	die("Memory error!", 3);
    }
    proc_data->mem = malloc(1);
    if (!proc_data) {
	die("Memory error!", 3);
    }
    proc_data->size = 0;
    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++;
    }
    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")) {
	    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")) {
	    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(proc_data->mem);
	    free(proc_data);
	    printf("Invalid argument!");
	    die("Invalid argument!", 1);
	    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);
	printf("%s", REPOS);
	if (memory) {
	    if (!sysinfo(&system_read)) {
		die("Unable to get system info!", 2);
	    }
	    printmemgraph(max_ram, system_read.bufferram, memgraph, '#');
	}
	printf("\n");
	if (cpu) {
	    printcpugraph(1.0, getload()/(float)core_num, cpugraph,':');
	}
	while (1) {
	    gettimeofday(&tv, NULL); 
	    if (prevtime -  tv.tv_usec >= tdelay)
		break;
	}
    }
    if (cores)
	printcores(proc_data->mem);

    free(proc_data->mem);
    free(proc_data);
    return 0;
}