-
Notifications
You must be signed in to change notification settings - Fork 0
/
a3SystemUtil.c
273 lines (241 loc) · 9.83 KB
/
a3SystemUtil.c
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <utmp.h>
#include <unistd.h>
#include <sys/sysinfo.h>
#include <string.h>
#include <sys/utsname.h>
#include "systemUtil.h"
#include <sys/wait.h>
#include <errno.h>
#include <signal.h>
// Signal handler function for SIGINT
void signal_handler(int sig) {
char ans;
if (sig == SIGTSTP) {
printf("\nCtrl-Z detected: ");
} else if (sig == SIGINT) {
printf("\nCtrl-C detected: ");
}
printf("Do you want to quit? (press 'y' if yes) ");
// Wait for user input
int ret = scanf(" %c", &ans);
if (ret == EOF) {
if (errno == EINTR) {
printf("\nSignal detected during scanf, resuming...\n");
return;
} else {
perror("scanf error");
exit(EXIT_FAILURE);
}
}
if (ans == 'y' || ans == 'Y') {
exit(EXIT_SUCCESS);
} else {
printf("Resuming...\n");
}
}
// Signal handler function for SIGTSTP
void handle_sigtstp(int sig)
{
printf("\nReceived SIGTSTP signal. Cannot run in background while running interactively.\n");
}
int main(int argc, char *argv[])
{
int graphics_check = 0, sequence_check = 0, user_check = 0, system_check = 0;
int samples = 10, seconds = 1, samples_check = 0;
// Reads all the flags
for (int i = 0; i < argc; i++)
{
if (strstr(argv[i], "--samples=") != NULL)
{
char *str = strstr(argv[i], "=");
samples = strtol(str + 1, NULL, 10);
}
if (strstr(argv[i], "--tdelay=") != NULL)
{
char *str = strstr(argv[i], "=");
seconds = strtol(str + 1, NULL, 10);
}
if (strstr(argv[i], "--sequential") != NULL)
sequence_check = 1;
if (strstr(argv[i], "--graphics") != NULL || strstr(argv[i], "-g") != NULL)
graphics_check = 1;
if (strstr(argv[i], "--user") != NULL)
user_check = 1;
if (strstr(argv[i], "--system") != NULL)
system_check = 1;
for (int q = 0; argv[i][q]; q++)
{
if (argv[i][q] >= '0' && argv[i][q] <= '9' && samples_check == 0)
{
samples = strtol(argv[i], NULL, 10);
samples_check = 1;
break;
}
else if (argv[i][q] >= '0' && argv[i][q] <= '9' && samples_check == 1)
{
seconds = strtol(argv[i], NULL, 10);
break;
}
else
break;
}
}
if (samples <= 0 || seconds <= 0)
{
printf("Number of samples or number of seconds cannot be negative\n");
exit(EXIT_FAILURE);
}
// Calculates the cores of the machine in the beginning
int cores = (int)sysconf(_SC_NPROCESSORS_ONLN);
// The three arrays that are needed to store the values
float virtual_memory[samples];
float phyical_memory[samples];
float cpu_utilization[samples][3];
int memory_pipe[2], users_pipe[2], cpu_pipe[2];
pid_t pid_memory, pid_users, pid_cpu;
for (int i = 0; i < samples; i++)
{ // iterate through the number of samples
struct sigaction act;
act.sa_handler = signal_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
signal(SIGINT, signal_handler); // Register signal handler for SIGINT
signal(SIGTSTP,handle_sigtstp); // Register signal handler for SIGTSTP
if (pipe(memory_pipe) == -1 || pipe(users_pipe) == -1 || pipe(cpu_pipe) == -1)
{ // if pipe fails, an error message is printed and the program exits
fprintf(stderr, "Pipe Failed");
return 1;
}
// creates a child process for the memory information
pid_memory = fork(); // forks a child process for memory information
if (pid_memory < 0)
{
fprintf(stderr, "Fork Failed");
return 2;
}
else if (pid_memory == 0)
{
signal(SIGINT, SIG_DFL);
close(memory_pipe[0]);
memory_util(samples, seconds, phyical_memory, virtual_memory, sequence_check, graphics_check, i, memory_pipe[1]);
close(memory_pipe[1]);
exit(0);
}
else
{
pid_users = fork();
if (pid_users < 0)
{
fprintf(stderr, "Fork Failed");
return 2;
}
else if (pid_users == 0)
{
signal(SIGINT, SIG_DFL);
close(users_pipe[0]);
session_users(users_pipe[1]);
close(users_pipe[1]);
exit(0); // exit the child process
}
else
{
pid_cpu = fork();
if (pid_cpu < 0)
{
fprintf(stderr, "Fork Failed");
return 2;
}
else if (pid_cpu == 0)
{
signal(SIGINT, SIG_DFL);
close(cpu_pipe[0]);
float old_total =0, old_idle = 0;
if(i > 0) old_total = cpu_utilization[i-1][0], old_idle = cpu_utilization[i-1][1];
core_usage(old_total, old_idle, i, graphics_check, sequence_check, cpu_pipe[1]);
close(cpu_pipe[1]);
exit(0);
}
else
{
// main (parent) process
close(memory_pipe[1]); // close the write end of the pipe
close(users_pipe[1]); // close the write end of the pipe
close(cpu_pipe[1]); // close the write end of the pipe
mem memory; // struct to store memory information
read(memory_pipe[0], &memory, sizeof(memory)); // read from the read end of the pipe
virtual_memory[i] = memory.virtual_memory;
phyical_memory[i] = memory.physical_memory;
close(memory_pipe[0]); // close the read end of the pipe
cpu_stat cpu_info;
read(cpu_pipe[0], &cpu_info, sizeof(cpu_info)); // reads the current cpu usage from the pipe
cpu_utilization[i][0] = cpu_info.total_time;
cpu_utilization[i][1] = cpu_info.total_idle;
cpu_utilization[i][2] = cpu_info.total_cpu_usage;
close(cpu_pipe[0]); // close the read end of the pipe
waitpid(pid_memory, NULL, 0);
waitpid(pid_users, NULL, 0);
waitpid(pid_cpu, NULL, 0);
if (sequence_check == 1)
{
printf(">>>>>Iteration %d\n", i + 1);
}
else
printf("\033c");
// prints the number of samples, seconds and the memory usage
first_part(samples, seconds);
// The different posibilities
if (argc == 1)
{
//print_session_users(users_pipe[0]);
print_memory_util(samples, seconds, phyical_memory, virtual_memory, sequence_check, graphics_check, i, memory);
print_session_users(users_pipe[0]);
printf("Number of cores: %d\n", cores);
print_core_usage(cpu_utilization, i, graphics_check, sequence_check);
}
else if ((seconds != 1 || samples != 10) && sequence_check == 0 && graphics_check == 0 && user_check == 0 && system_check == 0)
{
print_memory_util(samples, seconds, phyical_memory, virtual_memory, sequence_check, graphics_check, i, memory);
print_session_users(users_pipe[0]);
printf("Number of cores: %d\n", cores);
print_core_usage(cpu_utilization, i, graphics_check, sequence_check);
}
else if (user_check == 1 || system_check == 1)
{
if (system_check == 1)
{
print_memory_util(samples, seconds, phyical_memory, virtual_memory, sequence_check, graphics_check, i, memory);
}
if (user_check == 1)
{
print_session_users(users_pipe[0]);
}
if (system_check == 1)
{
printf("Number of cores: %d\n", cores);
print_core_usage(cpu_utilization, i, graphics_check, sequence_check);
}
}
else if ((sequence_check == 1 || graphics_check == 1) && user_check == 0 && system_check == 0)
{
print_memory_util(samples, seconds, phyical_memory, virtual_memory, sequence_check, graphics_check, i, memory);
print_session_users(users_pipe[0]);
printf("Number of cores: %d\n", cores);
print_core_usage(cpu_utilization, i, graphics_check, sequence_check);
}
close(users_pipe[0]); // close the read end of the pipe
if (i + 1 != samples)
sleep(seconds); // this is to make sure the samples are seperated
}
}
}
}
close(memory_pipe[0]); // close the read end of the pipe
close(users_pipe[0]); // close the read end of the pipe
close(cpu_pipe[0]); // close the read end of the pipe
printf("### System Information ###\n");
system_info();
return 0;
}