-
Notifications
You must be signed in to change notification settings - Fork 14
/
t_stress.c
288 lines (249 loc) · 6.37 KB
/
t_stress.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
/*
* Copyright (c) 2017-2018 Mindaugas Rasiukevicius <rmind at noxt eu>
* All rights reserved.
*
* Use is subject to license terms, as specified in the LICENSE file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <limits.h>
#include <errno.h>
#include <err.h>
#include "thmap.h"
#include "utils.h"
#define CHECK_TRUE(x) \
if (!(x)) { printf("FAIL: %s line %d\n", __func__, __LINE__); abort(); }
static thmap_t * map;
static pthread_barrier_t barrier;
static unsigned nworkers;
static uint64_t c_keys[4];
static unsigned thmap_alloc_count;
static uintptr_t
alloc_test_wrapper(size_t len)
{
thmap_alloc_count++; // count the allocation
return (uintptr_t)malloc(len);
}
static void
free_test_wrapper(uintptr_t addr, size_t len)
{
free((void *)addr); (void)len;
}
static void
del_collision_keys(thmap_t *m)
{
thmap_del(m, &c_keys[0], sizeof(uint64_t));
thmap_del(m, &c_keys[1], sizeof(uint64_t));
thmap_del(m, &c_keys[2], sizeof(uint64_t));
thmap_del(m, &c_keys[3], sizeof(uint64_t));
}
static void
prepare_collisions(void)
{
static const thmap_ops_t thmap_test_ops = {
.alloc = alloc_test_wrapper,
.free = free_test_wrapper
};
void *val, *keyval = (void *)(uintptr_t)0xdeadbeef;
/*
* Pre-calculated collisions. Note: the brute-force conditions
* on the murmurhash3() values:
*
* ((h0 >> 26) ^ 8) != ((h1 >> 26) ^ 8) || (h0 & 3) == (h1 & 3)
* ((h0 >> 26) ^ 8) != ((h1 >> 26) ^ 8) || (h0 & 3) != (h1 & 3)
* h0 != h3
*/
c_keys[0] = 0x8000100000080001;
c_keys[1] = 0x80001000000800fa;
c_keys[2] = 0x80001000000800df;
c_keys[3] = 0x800010012e04d085;
/*
* Validate check root-level collision.
*/
map = thmap_create(0, &thmap_test_ops, THMAP_NOCOPY);
thmap_alloc_count = 0;
val = thmap_put(map, &c_keys[0], sizeof(uint64_t), keyval);
CHECK_TRUE(val && thmap_alloc_count == 2); // leaf + internode
val = thmap_put(map, &c_keys[1], sizeof(uint64_t), keyval);
CHECK_TRUE(val && thmap_alloc_count == 3); // just leaf
del_collision_keys(map);
thmap_destroy(map);
/*
* Validate check first-level (L0) collision.
*/
map = thmap_create(0, &thmap_test_ops, THMAP_NOCOPY);
(void)thmap_put(map, &c_keys[0], sizeof(uint64_t), keyval);
thmap_alloc_count = 0;
val = thmap_put(map, &c_keys[2], sizeof(uint64_t), keyval);
CHECK_TRUE(val && thmap_alloc_count == 2); // leaf + internode
del_collision_keys(map);
thmap_destroy(map);
/*
* Validate the full 32-bit collision.
*/
map = thmap_create(0, &thmap_test_ops, THMAP_NOCOPY);
(void)thmap_put(map, &c_keys[0], sizeof(uint64_t), keyval);
thmap_alloc_count = 0;
val = thmap_put(map, &c_keys[3], sizeof(uint64_t), keyval);
CHECK_TRUE(val && thmap_alloc_count == 1 + 8); // leaf + 8 levels
del_collision_keys(map);
thmap_destroy(map);
}
/*
* Simple xorshift; random() causes huge lock contention on Linux/glibc,
* which would "hide" the possible race conditions.
*/
static unsigned long
fast_random(void)
{
static __thread uint32_t fast_random_seed = 5381;
uint32_t x = fast_random_seed;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
fast_random_seed = x;
return x;
}
static void *
fuzz_collision(void *arg, unsigned range_mask)
{
const unsigned id = (uintptr_t)arg;
unsigned n = 1 * 1000 * 1000;
pthread_barrier_wait(&barrier);
while (n--) {
uint64_t key = c_keys[fast_random() & range_mask];
void *keyval = (void *)(uintptr_t)key;
void *val;
switch (fast_random() & 3) {
case 0:
case 1: // ~50% lookups
val = thmap_get(map, &key, sizeof(key));
CHECK_TRUE(!val || val == keyval);
break;
case 2:
val = thmap_put(map, &key, sizeof(key), keyval);
CHECK_TRUE(val == keyval);
break;
case 3:
val = thmap_del(map, &key, sizeof(key));
CHECK_TRUE(!val || val == keyval);
break;
}
}
pthread_barrier_wait(&barrier);
/* The primary thread performs the clean-up. */
if (id == 0) {
thmap_del(map, &c_keys[0], sizeof(uint64_t));
thmap_del(map, &c_keys[1], sizeof(uint64_t));
thmap_del(map, &c_keys[2], sizeof(uint64_t));
thmap_del(map, &c_keys[3], sizeof(uint64_t));
}
pthread_exit(NULL);
return NULL;
}
static void *
fuzz_root_collision(void *arg)
{
/* Root-level collision: c_keys[0] vs c_keys[1]. */
return fuzz_collision(arg, 0x1);
}
static void *
fuzz_l0_collision(void *arg)
{
/* First-level collision: c_keys[0] vs c_keys[2]. */
return fuzz_collision(arg, 0x2);
}
static void *
fuzz_multi_collision(void *arg)
{
/* Root-level collision: c_keys vs c_keys. */
return fuzz_collision(arg, 0x3);
}
static void *
fuzz_multi(void *arg, uint64_t range_mask)
{
const unsigned id = (uintptr_t)arg;
unsigned n = 1 * 1000 * 1000;
pthread_barrier_wait(&barrier);
while (n--) {
uint64_t key = fast_random() & range_mask;
void *keyval = (void *)(uintptr_t)key;
void *val;
switch (fast_random() & 3) {
case 0:
case 1: // ~50% lookups
val = thmap_get(map, &key, sizeof(key));
CHECK_TRUE(!val || val == keyval);
break;
case 2:
val = thmap_put(map, &key, sizeof(key), keyval);
CHECK_TRUE(val == keyval);
break;
case 3:
val = thmap_del(map, &key, sizeof(key));
CHECK_TRUE(!val || val == keyval);
break;
}
}
pthread_barrier_wait(&barrier);
if (id == 0) for (uint64_t key = 0; key <= range_mask; key++) {
thmap_del(map, &key, sizeof(key));
}
pthread_exit(NULL);
return NULL;
}
static void *
fuzz_multi_128(void *arg)
{
/*
* Key range of 128 values to trigger contended
* expand/collapse cycles mostly within two levels.
*/
return fuzz_multi(arg, 0x1f);
}
static void *
fuzz_multi_512(void *arg)
{
/*
* Key range of 512 ought to create multiple levels.
*/
return fuzz_multi(arg, 0x1ff);
}
static void
run_test(void *func(void *))
{
pthread_t *thr;
puts(".");
map = thmap_create(0, NULL, 0);
nworkers = sysconf(_SC_NPROCESSORS_CONF) + 1;
thr = malloc(sizeof(pthread_t) * nworkers);
pthread_barrier_init(&barrier, NULL, nworkers);
for (unsigned i = 0; i < nworkers; i++) {
if ((errno = pthread_create(&thr[i], NULL,
func, (void *)(uintptr_t)i)) != 0) {
err(EXIT_FAILURE, "pthread_create");
}
}
for (unsigned i = 0; i < nworkers; i++) {
pthread_join(thr[i], NULL);
}
pthread_barrier_destroy(&barrier);
thmap_destroy(map);
free(thr);
}
int
main(void)
{
prepare_collisions();
run_test(fuzz_root_collision);
run_test(fuzz_l0_collision);
run_test(fuzz_multi_collision);
run_test(fuzz_multi_128);
run_test(fuzz_multi_512);
puts("ok");
return 0;
}