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
|
#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<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 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);
}
void repos() {
printf("\033[H");
}
void die(const char* reason, int 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++) {
;
}
return 0;
}
int printmemory() {
return 0;
}
int printcpu() {
return 0;
}
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));
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 ((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")) {
memory = 1;
}
else if (strstr(argv[i], "--cpu")) {
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(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(proc_data->mem);
free(initial_readings);
free(proc_data->mem);
free(proc_data);
return 0;
}
|