-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathtest-hashmap.c
209 lines (182 loc) · 5.39 KB
/
test-hashmap.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
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "free_later.h"
#include "hashmap.h"
/* global hash map */
static hashmap_t *map = NULL;
/* how many threads should run in parallel */
#define N_THREADS 32
/* how many times the work loop should repeat */
#define N_LOOPS 100
/* state for the threads */
static pthread_t threads[N_THREADS];
/* state for the threads that test deletes */
static pthread_t threads_del[N_THREADS * 2];
static uint32_t MAX_VAL_PLUS_ONE = N_THREADS * N_LOOPS + 1;
extern volatile uint32_t hashmap_del_fail, hashmap_del_fail_new_head;
extern volatile uint32_t hashmap_put_retries, hashmap_put_replace_fail;
extern volatile uint32_t hashmap_put_head_fail;
static uint8_t cmp_uint32(const void *x, const void *y)
{
uint32_t xi = *(uint32_t *) x, yi = *(uint32_t *) y;
if (xi > yi)
return -1;
if (xi < yi)
return 1;
return 0;
}
static uint64_t hash_uint32(const void *key)
{
return *(uint32_t *) key;
}
/* Simulates work that is quick and uses the hashtable once per loop */
static void *add_vals(void *args)
{
int *offset = args;
for (int j = 0; j < N_LOOPS; j++) {
int *val = malloc(sizeof(int));
*val = (*offset * N_LOOPS) + j;
hashmap_put(map, val, val);
}
return NULL;
}
bool mt_add_vals(void)
{
for (int i = 0; i < N_THREADS; i++) {
int *offset = malloc(sizeof(int));
*offset = i;
if (pthread_create(&threads[i], NULL, add_vals, offset) != 0) {
printf("Failed to create thread %d\n", i);
exit(1);
}
}
// wait for work to finish
for (int i = 0; i < N_THREADS; i++) {
if (pthread_join(threads[i], NULL) != 0) {
printf("Failed to join thread %d\n", i);
exit(1);
}
}
return true;
}
/* add a value over and over to test the del functionality */
void *add_val(void *args)
{
for (int j = 0; j < N_LOOPS; j++)
hashmap_put(map, &MAX_VAL_PLUS_ONE, &MAX_VAL_PLUS_ONE);
return NULL;
}
static void *del_val(void *args)
{
for (int j = 0; j < N_LOOPS; j++)
hashmap_del(map, &MAX_VAL_PLUS_ONE);
return NULL;
}
bool mt_del_vals(void)
{
for (int i = 0; i < N_THREADS; i++) {
if (pthread_create(&threads_del[i], NULL, add_val, NULL) != 0) {
printf("Failed to create thread %d\n", i);
exit(1);
}
if (pthread_create(&threads_del[N_THREADS + i], NULL, del_val, NULL)) {
printf("Failed to create thread %d\n", i);
exit(1);
}
}
// also add normal numbers to ensure they aren't clobbered
mt_add_vals();
// wait for work to finish
for (int i = 0; i < N_THREADS * 2; i++) {
if (pthread_join(threads_del[i], NULL) != 0) {
printf("Failed to join thread %d\n", i);
exit(1);
}
}
return true;
}
bool test_add()
{
map = hashmap_new(10, cmp_uint32, hash_uint32);
int loops = 0;
while (hashmap_put_retries == 0) {
loops += 1;
if (!mt_add_vals()) {
printf("Error. Failed to add values!\n");
return false;
}
/* check all the list entries */
uint32_t TOTAL = N_THREADS * N_LOOPS;
uint32_t found = 0;
for (uint32_t i = 0; i < TOTAL; i++) {
uint32_t *v = (uint32_t *) hashmap_get(map, &i);
if (v && *v == i) {
found++;
} else {
printf("Cound not find %u in the map\n", i);
}
}
if (found == TOTAL) {
printf(
"Loop %d. All values found. hashmap_put_retries=%u, "
"hashmap_put_head_fail=%u, hashmap_put_replace_fail=%u\n",
loops, hashmap_put_retries, hashmap_put_head_fail,
hashmap_put_replace_fail);
} else {
printf("Found %u of %u values. Where are the missing?", found,
TOTAL);
}
}
printf("Done. Loops=%d\n", loops);
return true;
}
bool test_del()
{
/* keep looping until a CAS retry was needed by hashmap_del */
uint32_t loops = 0;
/* make sure test counters are zeroed */
hashmap_del_fail = hashmap_del_fail_new_head = 0;
while (hashmap_del_fail == 0 || hashmap_del_fail_new_head == 0) {
map = hashmap_new(10, cmp_uint32, hash_uint32);
/* multi-threaded add values */
if (!mt_del_vals()) {
printf("test_del() is failing. Can't complete mt_del_vals()");
return false;
}
loops++;
// check all the list entries
uint32_t TOTAL = N_THREADS * N_LOOPS, found = 0;
for (uint32_t i = 0; i < TOTAL; i++) {
uint32_t *v = hashmap_get(map, &i);
if (v && *v == i) {
found++;
} else {
printf("Cound not find %u in the hashmap\n", i);
}
}
if (found != TOTAL) {
printf("test_del() is failing. Not all values found!?");
return false;
}
}
printf("Done. Needed %u loops\n", loops);
return true;
}
int main()
{
free_later_init();
if (!test_add()) {
printf("Failed to run multi-threaded addition test.");
return 1;
}
if (!test_del()) {
printf("Failed to run multi-threaded deletion test.");
return 2;
}
free_later_exit();
return 0;
}