forked from antonblanchard/will-it-scale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
290 lines (222 loc) · 5.34 KB
/
main.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright (C) 2010 Anton Blanchard <anton@au.ibm.com>, IBM
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*/
#define _GNU_SOURCE
#include <sched.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <hwloc.h>
#include <hwloc/glibc-sched.h>
#include <sys/types.h>
#include <signal.h>
#define MAX_TASKS 1024
#define CACHELINE_SIZE 128
#define WARMUP_ITERATIONS 5
extern char *testcase_description;
extern void __attribute__((weak)) testcase_prepare(void) { }
extern void __attribute__((weak)) testcase_cleanup(void) { }
extern void *testcase(void *iterations);
static char *initialise_shared_area(unsigned long size)
{
char template[] = "/tmp/shared_area_XXXXXX";
int fd;
char *m;
int page_size = getpagesize();
/* Align to page boundary */
size = (size + page_size-1) & ~(page_size-1);
fd = mkstemp(template);
if (fd < 0) {
perror("mkstemp");
exit(1);
}
if (ftruncate(fd, size) == -1) {
perror("ftruncate");
unlink(template);
exit(1);
}
m = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (m == MAP_FAILED) {
perror("mmap");
unlink(template);
exit(1);
}
memset(m, 0, size);
unlink(template);
return m;
}
static void usage(char *command)
{
printf("Usage: %s <-t tasks> <-s iterations>\n\n", command);
exit(1);
}
#ifdef THREADS
#include <pthread.h>
void new_task(void *(func)(void *), void *arg)
{
pthread_t tid;
pthread_create(&tid, NULL, func, arg);
}
void new_task_affinity(void *(func)(void *), void *arg,
size_t cpuset_size, cpu_set_t *mask)
{
pthread_attr_t attr;
pthread_t tid;
pthread_attr_init(&attr);
pthread_attr_setaffinity_np(&attr, cpuset_size, mask);
pthread_create(&tid, &attr, func, arg);
pthread_attr_destroy(&attr);
}
/* All threads will die when we exit */
static void kill_tasks(void)
{
}
#else
#include <signal.h>
static int pids[MAX_TASKS];
static int nr_pids;
/* Watchdog used to make sure all children exit when the parent does */
static int parent_pid;
static void watchdog(int junk)
{
if (kill(parent_pid, 0) == -1)
exit(0);
alarm(1);
}
void new_task(void *(func)(void *), void *arg)
{
int pid;
parent_pid = getpid();
pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
if (!pid) {
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = watchdog;
sa.sa_flags = SA_RESTART;
sigaction(SIGALRM, &sa, NULL);
alarm(1);
func(arg);
}
pids[nr_pids++] = pid;
}
void new_task_affinity(void *(func)(void *), void *arg,
size_t cpuset_size, cpu_set_t *mask)
{
cpu_set_t old_mask;
int pid;
sched_getaffinity(0, sizeof(old_mask), &old_mask);
sched_setaffinity(0, cpuset_size, mask);
parent_pid = getpid();
pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
if (!pid) {
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = watchdog;
sa.sa_flags = SA_RESTART;
sigaction(SIGALRM, &sa, NULL);
alarm(1);
func(arg);
}
sched_setaffinity(0, sizeof(old_mask), &old_mask);
pids[nr_pids++] = pid;
}
static void kill_tasks(void)
{
int i;
for (i = 0; i < nr_pids; i++)
kill(pids[i], SIGTERM);
}
#endif
int main(int argc, char *argv[])
{
int opt_tasks = 1;
int opt_iterations = 0;
int iterations = 0;
int i, n;
char *m;
static unsigned long long *results[MAX_TASKS];
hwloc_topology_t topology;
unsigned long long prev[MAX_TASKS] = {0, };
unsigned long long total = 0;
while (1) {
signed char c = getopt(argc, argv, "t:s:h");
if (c < 0)
break;
switch (c) {
case 't':
opt_tasks = atoi(optarg);
if (opt_tasks > MAX_TASKS) {
printf("tasks cannot exceed %d\n",
MAX_TASKS);
exit(1);
}
break;
case 's':
opt_iterations = atoi(optarg);
break;
default:
usage(argv[0]);
}
}
if (optind < argc)
usage(argv[0]);
m = initialise_shared_area(opt_tasks * CACHELINE_SIZE);
for (i = 0; i < opt_tasks; i++)
results[i] = (unsigned long long *)&m[i * CACHELINE_SIZE];
testcase_prepare();
hwloc_topology_init(&topology);
hwloc_topology_load(topology);
n = hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_CORE);
for (i = 0; i < opt_tasks; i++) {
hwloc_obj_t obj;
cpu_set_t mask;
obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_CORE, i % n);
hwloc_cpuset_to_glibc_sched_affinity(topology,
obj->cpuset, &mask, sizeof(mask));
new_task_affinity(testcase, results[i], sizeof(mask), &mask);
}
hwloc_topology_destroy(topology);
printf("testcase:%s\n", testcase_description);
printf("warmup\n");
while (1) {
unsigned long long sum = 0, min = -1ULL, max = 0;
sleep(1);
for (i = 0; i < opt_tasks; i++) {
unsigned long long val = *(results[i]);
unsigned long long diff = val - prev[i];
if (diff < min)
min = diff;
if (diff > max)
max = diff;
sum += diff;
prev[i] = val;
}
printf("min:%llu max:%llu total:%llu\n", min, max, sum);
if (iterations == WARMUP_ITERATIONS)
printf("measurement\n");
if (iterations++ > WARMUP_ITERATIONS)
total += sum;
if (opt_iterations &&
(iterations > (opt_iterations + WARMUP_ITERATIONS))) {
printf("average:%llu\n", total / opt_iterations);
testcase_cleanup();
kill_tasks();
exit(0);
}
}
}