-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload.c
More file actions
executable file
·112 lines (101 loc) · 2 KB
/
Copy pathload.c
File metadata and controls
executable file
·112 lines (101 loc) · 2 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/signal.h>
#include "load.h"
/* Max. load processes */
#define PLIMIT 100
static pid_t child_id[PLIMIT];
static int child_count = 0;
static int sink;
/* Run cpu-intensive code */
static void cpu_run()
{
int i = 1;
sink = 1;
while (1)
sink *= i++;
}
/* Run cache intensive code */
/* L1 cache is 16KB = 4K integers, with 32B line size */
#define CSIZE (1<<12)
#define CSTRIDE 8
static void cache_run()
{
int i;
int *data = calloc(CSIZE, sizeof(int));
while (1) {
/* Write array */
for (i = 0; i < CSIZE; i += CSTRIDE)
data[i] = i;
/* Read it back */
sink = 0;
for (i = 0; i < CSIZE; i += CSTRIDE)
sink += data[i];
}
}
/* Run memory intensive code */
/* L2 cache is 512KB = 128K integers, with 32B line size */
#define MSIZE (1<<18)
#define MSTRIDE 8
static void mem_run()
{
int i;
int *data = calloc(MSIZE, sizeof(int));
while (1) {
/* Write array */
for (i = 0; i < MSIZE; i += MSTRIDE)
data[i] = i;
/* Read it back */
sink = 0;
for (i = 0; i < MSIZE; i += MSTRIDE)
sink += data[i];
}
}
void add_load(int count, load_t load_type) {
int i;
if (count == 0)
return;
for (i = 0; i < count; i++) {
pid_t id;
if (i+1 == PLIMIT) {
fprintf(stderr, "Can't create more than %d child processes\n",
PLIMIT);
exit(1);
}
id = fork();
if (id) {
/* Parent */
child_id[child_count++] = id;
} else {
/* Child */
switch(load_type) {
case CPU_LOAD:
cpu_run();
break;
case CACHE_LOAD:
cache_run();
break;
case MEM_LOAD:
mem_run();
break;
default:
fprintf(stderr, "Unknown Load type %d\n", load_type);
exit(1);
}
exit(0);
}
}
sleep(1);
}
/* Kill all existing loads */
/* Kill All Load Processes */
void kill_loads() {
while (child_count > 0) {
int status;
kill(child_id[--child_count], 9);
wait(&status);
}
}