forked from LPD-EPFL/ASCYLIB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
413 lines (363 loc) · 13.2 KB
/
test.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* File: test.c
* Author: Vincent Gramoli <[email protected]>,
* Tudor David <[email protected]>
* Description:
* test.c is part of ASCYLIB
*
* Copyright (c) 2014 Vasileios Trigonakis <[email protected]>,
* Tudor David <[email protected]>
* Distributed Programming Lab (LPD), EPFL
*
* ASCYLIB 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, version 2
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <assert.h>
#include <getopt.h>
#include <limits.h>
#include <signal.h>
#include <sys/time.h>
#include <time.h>
#include "bst_ellen.h"
#include "measurements.h"
#include "utils.h"
#include "ssalloc.h"
#define XSTR(s) STR(s)
#define STR(s) #s
//not used any more; TODO remove this parameter
#define DEFAULT_SEED 0
//default percentage of reads
#define DEFAULT_READS 80
#define DEFAULT_UPDATES 20
//default number of threads
#define DEFAULT_NUM_THREADS 1
//default experiment duration in miliseconds
#define DEFAULT_DURATION 1000
//the maximum value the key stored in the bst can take; defines the key range
#define DEFAULT_RANGE 2048
//#define DEBUG 1
int duration;
int num_threads;
uint32_t finds;
uint32_t updates;
uint32_t max_key;
int seed;
//static volatile int stop;
//used to signal the threads when to stop
ALIGNED(64) uint8_t running[64];
//per-thread seeds for the custom random function
__thread unsigned long * seeds;
//the root of the binary search tree
node_t * root;
//data structure through which we send parameters to and get results from the worker threads
typedef ALIGNED(128) struct thread_data {
//pointer to the global barrier
barrier_t *barrier;
//counts the number of operations each thread performs
unsigned long num_operations;
//total operation time (not used here)
ticks total_time;
//seed (not used here)
unsigned int seed;
//the number of elements each thread should add at the beginning of its execution
uint64_t num_add;
//number of inserts a thread performs
unsigned long num_insert;
//number of removes a thread performs
unsigned long num_remove;
//number of searches a thread performs
unsigned long num_search;
//the id of the thread (used for thread placement on cores)
int id;
} thread_data_t;
void *test(void *data)
{
DDPRINT("starting test\n",NULL);
//get the per-thread data
thread_data_t *d = (thread_data_t *)data;
//scale percentages of the various operations to the range 0..255
//this saves us a floating point operation during the benchmark
//e.g instead of random()%100 to determine the next operation we will do, we can simply do random()&256
//this saves time on some platfroms
uint32_t read_thresh = 256 * finds / 100;
//uint32_t write_thresh = 256 * (finds + inserts) / 100;
//place the thread on the apropriate cpu
set_cpu(d->id);
//initialize the custom memeory allocator for this thread (we do not use malloc due to concurrency bottleneck issues)
ssalloc_init();
bst_init_local();
//for fine-grain latency measurements, we need to get the lenght of a getticks() function call, which is also counted
//by default when we do getticks(); //code... getticks(); PF_START and PF_STOP use this when fine grain measurements are enabled
PF_CORRECTION;
uint32_t rand_max;
//seed the custom random number generator
seeds = seed_rand();
rand_max = max_key;
uint32_t op;
skey_t key;
int i;
int last = -1;
DDPRINT("staring initial insert\n",NULL);
DDPRINT("number of inserts: %u up to %u\n",d->num_add,rand_max);
//before starting the test, we insert a number of elements in the data structure
//we do this at each thread to avoid the situation where the entire data structure
//resides in the same memory node
for (i=0;i<d->num_add;++i) {
key = my_random(&seeds[0],&seeds[1],&seeds[2]) & rand_max;
DDPRINT("key is %u\n",key);
//we make sure the insert was effective (as opposed to just updating an existing entry)
if (bst_insert(key,root)!=TRUE) {
i--;
}
}
DDPRINT("added initial data\n",NULL);
bool_t res;
/* Init of local data if necessary */
ticks t1,t2;
/* Wait on barrier */
barrier_cross(d->barrier);
//start the test
while (*running) {
//generate a key (node that rand_max is expected to be a power of 2)
key = my_random(&seeds[0],&seeds[1],&seeds[2]) & rand_max;
//generate the operation
op = my_random(&seeds[0],&seeds[1],&seeds[2]) & 0xff;
if (op < read_thresh) {
//do a find operation
//PF_START and PF_STOP can be used to do latency measurements of the operation
//to enable them, DO_TIMINGS must be defined at compile time, otherwise they do nothing
//PF_START(2);
bst_find(key,root);
//PF_STOP(2);
} else if (last == -1) {
//do a write operation
if (bst_insert(key,root)) {
d->num_insert++;
last=1;
}
} else {
//do a delete operation
if (bst_delete(key,root)) {
d->num_remove++;
last=-1;
}
}
d->num_operations++;
//memory barrier to ensure no unwanted reporderings are happening
//MEM_BARRIER;
}
//summary of the fine grain measurements if enabled
PF_PRINT;
return NULL;
}
void catcher(int sig)
{
static int nb = 0;
printf("CAUGHT SIGNAL %d\n", sig);
if (++nb >= 3)
exit(1);
}
int main(int argc, char* const argv[]) {
//place thread on the first cpu
set_cpu(0);
//initialize the custom memory allocator
ssalloc_init();
pthread_t *threads;
pthread_attr_t attr;
barrier_t barrier;
struct timeval start, end;
struct timespec timeout;
thread_data_t *data;
sigset_t block_set;
//initially, set parameters to their default values
num_threads = DEFAULT_NUM_THREADS;
seed=DEFAULT_SEED;
max_key=DEFAULT_RANGE;
updates=DEFAULT_UPDATES;
finds=DEFAULT_READS;
//inserts=DEFAULT_INSERTS;
//removes=DEFAULT_REMOVES;
duration=DEFAULT_DURATION;
//now read the parameters in case the user provided values for them
//we use getopt, the same skeleton may be used for other bechmarks,
//though the particular parameters may be different
struct option long_options[] = {
// These options don't set a flag
{"help", no_argument, NULL, 'h'},
{"duration", required_argument, NULL, 'd'},
{"range", required_argument, NULL, 'r'},
{"initial", required_argument, NULL, 'i'},
{"num-threads", required_argument, NULL, 'n'},
{"updates", required_argument, NULL, 'u'},
{"seed", required_argument, NULL, 's'},
{NULL, 0, NULL, 0}
};
int i,c;
//actually get the parameters form the command-line
while(1) {
i = 0;
c = getopt_long(argc, argv, "hd:n:l:u:i:r:s", long_options, &i);
if(c == -1)
break;
if(c == 0 && long_options[i].flag == 0)
c = long_options[i].val;
switch(c) {
case 0:
/* Flag is automatically set */
break;
case 'h':
printf("lock stress test\n"
"\n"
"Usage:\n"
" stress_test [options...]\n"
"\n"
"Options:\n"
" -h, --help\n"
" Print this message\n"
" -d, --duration <int>\n"
" Test duration in milliseconds (0=infinite, default=" XSTR(DEFAULT_DURATION) ")\n"
" -u, --updates <int>\n"
" Percentage of update operations (default=" XSTR(DEFAULT_UPDATES) ")\n"
" -r, --range <int>\n"
" Key range (default=" XSTR(DEFAULT_RANGE) ")\n"
" -n, --num-threads <int>\n"
" Number of threads (default=" XSTR(DEFAULT_NUM_THREADS) ")\n"
" -s, --seed <int>\n"
" RNG seed (0=time-based, default=" XSTR(DEFAULT_SEED) ")\n"
);
exit(0);
case 'd':
duration = atoi(optarg);
break;
case 'u':
updates = atoi(optarg);
finds = 100 - updates;
break;
case 'r':
max_key = atoi(optarg);
break;
case 'i':
break;
case 'l':
break;
case 'n':
num_threads = atoi(optarg);
break;
case 's':
seed = atoi(optarg);
break;
case '?':
printf("Use -h or --help for help\n");
exit(0);
default:
exit(1);
}
}
max_key--;
//we round the max key up to the nearest power of 2, which makes our random key generation more efficient
max_key = pow2roundup(max_key)-1;
//initialization of the tree
root = bst_initialize();
//initialize the data which will be passed to the threads
if ((data = (thread_data_t *)malloc(num_threads * sizeof(thread_data_t))) == NULL) {
perror("malloc");
exit(1);
}
if ((threads = (pthread_t *)malloc(num_threads * sizeof(pthread_t))) == NULL) {
perror("malloc");
exit(1);
}
if (seed == 0)
srand((int)time(NULL));
else
srand(seed);
//flag signaling the threads until when to run
*running = 1;
//global barrier initialization (used to start the threads at the same time)
barrier_init(&barrier, num_threads + 1);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
timeout.tv_sec = duration / 1000;
timeout.tv_nsec = (duration % 1000) * 1000000;
//set the data for each thread and create the threads
for (i = 0; i < num_threads; i++) {
data[i].id = i;
data[i].num_operations = 0;
data[i].total_time=0;
data[i].num_insert=0;
data[i].num_remove=0;
data[i].num_search=0;
data[i].num_add = max_key/(2 * num_threads);
if (i< ((max_key/2)%num_threads)) data[i].num_add++;
data[i].seed = rand();
data[i].barrier = &barrier;
if (pthread_create(&threads[i], &attr, test, (void *)(&data[i])) != 0) {
fprintf(stderr, "Error creating thread\n");
exit(1);
}
}
pthread_attr_destroy(&attr);
/* Catch some signals */
if (signal(SIGHUP, catcher) == SIG_ERR ||
signal(SIGINT, catcher) == SIG_ERR ||
signal(SIGTERM, catcher) == SIG_ERR) {
perror("signal");
exit(1);
}
/* Start threads */
barrier_cross(&barrier);
gettimeofday(&start, NULL);
if (duration > 0) {
//sleep for the duration of the experiment
nanosleep(&timeout, NULL);
} else {
sigemptyset(&block_set);
sigsuspend(&block_set);
}
//signal the threads to stop
*running = 0;
gettimeofday(&end, NULL);
/* Wait for thread completion */
for (i = 0; i < num_threads; i++) {
if (pthread_join(threads[i], NULL) != 0) {
fprintf(stderr, "Error waiting for thread completion\n");
exit(1);
}
}
DDPRINT("threads finshed\n",NULL);
//compute the exact duration of the experiment
duration = (end.tv_sec * 1000 + end.tv_usec / 1000) - (start.tv_sec * 1000 + start.tv_usec / 1000);
//bst_print(root);
unsigned long operations = 0;
ticks total_ticks = 0;
long reported_total = 0;
//report some experiment statistics
for (i = 0; i < num_threads; i++) {
printf("Thread %d\n", i);
printf(" #operations : %lu\n", data[i].num_operations);
printf(" #inserts : %lu\n", data[i].num_insert);
printf(" #removes : %lu\n", data[i].num_remove);
operations += data[i].num_operations;
total_ticks += data[i].total_time;
reported_total = reported_total + data[i].num_add + data[i].num_insert - data[i].num_remove;
}
printf("Duration : %d (ms)\n", duration);
printf("#txs : %lu (%f / s)\n", operations, operations * 1000.0 / duration);
//printf("Operation latency %lu\n", total_ticks / operations);
//make sure the tree is correct
printf("Expected size: %ld Actual size: %lu\n",reported_total,bst_size(root));
free(threads);
free(data);
return 0;
}