-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcpu_mem_simulator.c
More file actions
93 lines (77 loc) · 3.1 KB
/
cpu_mem_simulator.c
File metadata and controls
93 lines (77 loc) · 3.1 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#define MILLION 1000000
volatile sig_atomic_t keep_running = 1;
// Signal handler for SIGINT and SIGTERM
void handle_signal(int sig) {
keep_running = 0;
}
// Function to simulate CPU load
void simulate_cpu_load(int millicores) {
long long start_time, current_time;
struct timeval time_check;
int active_time = millicores * 1000; // Microseconds active
int sleep_time = MILLION - active_time; // Microseconds sleeping
while (keep_running) {
gettimeofday(&time_check, NULL);
start_time = time_check.tv_sec * MILLION + time_check.tv_usec;
// Busy-waiting for the duration of active_time
do {
gettimeofday(&time_check, NULL);
current_time = time_check.tv_sec * MILLION + time_check.tv_usec;
} while ((current_time - start_time) < active_time && keep_running);
// Sleep for the remainder of the second
if (keep_running) usleep(sleep_time);
}
}
int main(int argc, char *argv[]) {
int millicores = 0; // Default to 0, meaning not set
int memory_in_MiB = 0; // Default to 0, meaning not set
// Register signal handlers
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-millicores") == 0 && i + 1 < argc) {
millicores = atoi(argv[++i]);
} else if (strcmp(argv[i], "-MiB") == 0 && i + 1 < argc) {
memory_in_MiB = atoi(argv[++i]);
} else {
fprintf(stderr, "Unknown or incomplete argument: %s\n\n", argv[i]);
fprintf(stderr, "Created by James Spurin @ DiveInto.\n\n");
fprintf(stderr, "Usage: %s -millicores [value] -MiB [value]\n", argv[0]);
fprintf(stderr, "Both arguments are optional but at least one must be provided.\n");
return 1;
}
}
if (millicores <= 0 && memory_in_MiB <= 0) {
fprintf(stderr, "Created by James Spurin @ DiveInto.\n\n");
fprintf(stderr, "No valid arguments provided. At least one parameter must be set.\n");
fprintf(stderr, "Usage: %s -millicores [value] -MiB [value]\n", argv[0]);
return 1;
}
if (millicores <= 0) {
millicores = 100; // Default millicore value if not specified
printf("Defaulting millicores to 100 as none specified.\n");
}
if (memory_in_MiB <= 0) {
memory_in_MiB = 10; // Default memory value if not specified
printf("Defaulting memory to 10 MiB as none specified.\n");
}
// Memory allocation
char *memory = malloc(memory_in_MiB * 1024 * 1024);
if (memory == NULL) {
fprintf(stderr, "Failed to allocate memory.\n");
return 1;
}
memset(memory, 0, memory_in_MiB * 1024 * 1024);
printf("Simulating %d millicores and allocating %d MiB of memory indefinitely...\n", millicores, memory_in_MiB);
simulate_cpu_load(millicores);
// Free memory and exit cleanly
free(memory);
printf("Cleanly exiting after signal interruption.\n");
return 0;
}