-
Notifications
You must be signed in to change notification settings - Fork 0
/
noise.c
251 lines (234 loc) · 6.19 KB
/
noise.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
#include <string.h>
#include <numaif.h>
#include <numa.h>
#include <errno.h>
#include <time.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
struct bitmask *noiseCpu = NULL, *noiseMem = NULL, *benchCpu = NULL, *benchMem = NULL;
long noiseChunks = -1, noiseChunkOrder = -1;
struct timespec noiseInterval = {-1, -1};
long threads = -1;
char **benchBin;
size_t chunkSize, totalSize, chunkMask;
sem_t barrier;
void printUsage(char *name){
fprintf(stderr, "Usage: %s [options] benchmark [benchmark arguments]\n\noptions:\n", name);
fprintf(stderr, "-c <nodes> : the cpu numa nodes where the benchmark will be run, similar to numactl -N (defult current behaviour)\n");
fprintf(stderr, "-C <nodes> : the cpu numa nodes where the noise will be run (defult same as -c)\n");
fprintf(stderr, "-m <nodes> : the memory numa nodes where the benchmark will be run, similar to numactl -m (defult current behaviour)\n");
fprintf(stderr, "-M <nodes> : the memory numa nodes where the noise will be run (defult same as -m)\n");
fprintf(stderr, "-t <nsecs> : the interval between each noise memcpy (default 0)\n");
fprintf(stderr, "-T <threads> : the number of noise threads (default 0)\n");
fprintf(stderr, "-s <shift> : noise chunk size will be 1 << shift (default 12 => 4KB)\n");
fprintf(stderr, "-n <shift> : noise chunks number will be 1 << shift (default 1 => 2 chunks)\n");
}
int parseOpts(int argc, char **argv){
int i;
for(i = 1; (i < argc - 1) && (argv[i][0] == '-'); i += 2){
if(!strcmp(argv[i], "-c")){
if(benchCpu == NULL){
benchCpu = numa_parse_nodestring(argv[i+1]);
if(benchCpu == NULL || benchCpu == numa_no_nodes_ptr){
goto inval;
}
}else{
goto redef;
}
}else if(!strcmp(argv[i], "-m")){
if(benchMem == NULL){
benchMem = numa_parse_nodestring(argv[i+1]);
if(benchMem == NULL || benchMem == numa_no_nodes_ptr){
goto inval;
}
}else{
goto redef;
}
}else if(!strcmp(argv[i], "-C")){
if(noiseCpu == NULL){
noiseCpu = numa_parse_nodestring(argv[i+1]);
if(noiseCpu == NULL || noiseCpu == numa_no_nodes_ptr){
goto inval;
}
}else{
goto redef;
}
}
else if(!strcmp(argv[i], "-M")){
if(noiseMem == NULL){
noiseMem = numa_parse_nodestring(argv[i+1]);
if(noiseMem == NULL || noiseMem == numa_no_nodes_ptr){
goto inval;
}
}else{
goto redef;
}
}else if(!strcmp(argv[i], "-t")){
if(noiseInterval.tv_sec == -1){
noiseInterval.tv_nsec = strtol(argv[i+1], NULL, 0);
if(noiseInterval.tv_nsec < 0){
goto inval;
}
/* WONTFIX support interval >= (1 << 64) nsec*/
noiseInterval.tv_sec = noiseInterval.tv_nsec/1000000000;
noiseInterval.tv_nsec %= 1000000000;
}else{
goto redef;
}
}else if(!strcmp(argv[i], "-T")){
if(threads == -1){
threads = strtol(argv[i+1], NULL, 0);
if(threads < 0){
goto inval;
}
}else{
goto redef;
}
}else if(!strcmp(argv[i], "-s")){
if(noiseChunkOrder == -1){
noiseChunkOrder = strtol(argv[i+1], NULL, 0);
if(noiseChunkOrder < 0){
goto inval;
}
}else{
goto redef;
}
}else if(!strcmp(argv[i], "-n")){
if(noiseChunks == -1){
noiseChunks = strtol(argv[i+1], NULL, 0);
if(noiseChunks < 1){
goto inval;
}
}else{
goto redef;
}
}else{
fprintf(stderr, "Invalid option: %s\n", argv[i]);
printUsage(argv[0]);
return -EINVAL;
}
}
if(argv[i][0] == '-'){
fprintf(stderr, "%s option has no argument\n\n", argv[i]);
printUsage(argv[0]);
return -EINVAL;
}
if(noiseCpu == NULL){
noiseCpu = benchCpu;
}
if(noiseMem == NULL){
benchMem = benchMem;
}
if(noiseInterval.tv_sec == -1){
noiseInterval.tv_sec = 0;
noiseInterval.tv_nsec = 0;
}
if(threads == -1){
threads = 0;
}
if(noiseChunkOrder == -1){
noiseChunkOrder = 12;
}
if(noiseChunks == -1){
noiseChunks = 1;
}
chunkSize = 1 << noiseChunkOrder;
totalSize = chunkSize << noiseChunks;
chunkMask = totalSize - 1;
benchBin = argv + i;
return 0;
redef:
fprintf(stderr, "%s option defined mere than once\n\n", argv[i]);
printUsage(argv[0]);
return -EINVAL;
inval:
fprintf(stderr, "%s option has an invalid argument\n\n", argv[i]);
printUsage(argv[0]);
return -EINVAL;
}
void *noise(void * unused){
void *base;
size_t fromOfft = 0, toOfft;
base = aligned_alloc((chunkSize>getpagesize())?chunkSize:getpagesize(), (totalSize>getpagesize())?totalSize:getpagesize());
if(base == NULL){
fprintf(stderr, "noise memory allocation failed\n");
return NULL;
}
if(noiseMem != NULL && mbind(base, (totalSize>getpagesize())?totalSize:getpagesize(), MPOL_BIND, noiseMem->maskp, noiseMem->size, MPOL_MF_MOVE)){
perror("mbind()");
return NULL;
}
if(noiseCpu != NULL && numa_run_on_node_mask_all(noiseCpu)){
perror("numa_run_on_node_mask_all()");
return NULL;
}
toOfft = totalSize >> 1;
memset(base, 0xff, totalSize);
sem_post(&barrier);
while(1){
memcpy(base + toOfft, base + fromOfft, chunkSize);
toOfft = ((toOfft + chunkSize) & chunkMask);
fromOfft = ((fromOfft + chunkSize) & chunkMask);
if(noiseInterval.tv_sec || noiseInterval.tv_nsec){
nanosleep(&noiseInterval, NULL);
}
}
}
int runBench(char **argv){
pid_t pid;
pid = fork();
if(pid < 0){
perror("fork()");
return -errno;
}else if(pid == 0){
if(benchCpu != NULL && numa_run_on_node_mask_all(benchCpu)){
perror("numa_run_on_node_mask_all()");
return -errno;
}
if(benchMem != NULL){
numa_set_membind(benchMem);
}
execv(argv[0], argv);
perror("execv()");
return -errno;
}else{
/*parent*/
int status;
if(waitpid(pid, &status, 0) < 0){
perror("waitpid()");
return -errno;
}
if(WIFEXITED(status)){
return -WEXITSTATUS(status);
}else{
return -EINTR;
}
}
}
int main(int argc, char **argv){
int err, i;
pthread_t unused;
if(argc == 2 && !strcmp(argv[1], "-h")){
printUsage(argv[0]);
return 0;
}
if((err = parseOpts(argc, argv))){
return -err;
}
sem_init(&barrier, 0, 0);
for(i = 0; i < threads; i++){
if(pthread_create(&unused, NULL, noise, NULL)){
perror("pthread_create()");
return errno;
}
}
for(i = 0; i < threads; i++){
while(sem_wait(&barrier));
}
sem_destroy(&barrier);
return -runBench(benchBin);
}