-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
330 lines (256 loc) · 6.85 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
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
/* Multi-threaded DNS-like Simulation.
*
* Professor Don Porter - porter@cs.stonybrook.edu
*
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <ctype.h>
#include "trie.h"
int allow_squatting = 0;
int simulation_length = 30; // default to 30 seconds
volatile int finished = 0;
// Uncomment this line for debug printing
//#define DEBUG 1
#ifdef DEBUG
#define DEBUG_PRINT(...) printf(__VA_ARGS__)
#else
#define DEBUG_PRINT(...)
#endif
static void *
client(void *arg)
{
struct random_data rd;
char rand_state[256];
int32_t salt = time(0);
// See http://lists.debian.org/debian-glibc/2006/01/msg00037.html
rd.state = (int32_t*)rand_state;
// Initialize the prng. For testing, it may be helpful to reduce noise by
// temporarily setting this to a fixed value.
initstate_r(salt, rand_state, sizeof(rand_state), &rd);
while (!finished) {
/* Pick a random operation, string, and ip */
int32_t code;
int rv = random_r(&rd, &code);
int length = (code >> 2) & (64-1);
char buf[64];
int j;
int32_t ip4_addr;
if (rv) {
printf("Failed to get random number - %d\n", rv);
return NULL;
}
DEBUG_PRINT("Length is %d\n", length);
memset(buf, 0, 64);
/* Generate a random string in lowercase */
for (j = 0; j < length; j+= 6) {
int i;
int32_t chars;
rv = random_r(&rd, &chars);
if (rv) {
printf("Failed to get random number - %d\n", rv);
return NULL;
}
for (i = 0; i < 6 && (i+j) < length; i++) {
char val = ( (chars >> (5 * i)) & 31);
if (val > 25)
val = 25;
buf[j+i] = 'a' + val;
}
}
DEBUG_PRINT ("Random string is %s\n", buf);
switch (code % 3) {
case 0: // Search
DEBUG_PRINT ("Search\n");
search (buf, length, NULL);
break;
case 1: // insert
DEBUG_PRINT ("insert\n");
rv = random_r(&rd, &ip4_addr);
if (rv) {
printf("Failed to get random number - %d\n", rv);
return NULL;
}
insert (buf, length, ip4_addr);
break;
case 2: // delete
DEBUG_PRINT ("delete\n");
//delete (buf, length);
break;
default:
assert(0);
}
}
return NULL;
}
static void *
squatter_stress(void *arg)
{
/* Just loop over 4 names, trying to create them
* and then free them. If all threads
* try to create the same name, all but one should block
* until a given name is available again.
*/
int32_t ip = random();
while (!finished) {
insert ("abc", 3, ip);
insert ("abe", 3, ip);
insert ("bce", 3, ip);
insert ("bcc", 3, ip);
delete ("abc", 3);
delete ("abe", 3);
delete ("bce", 3);
delete ("bcc", 3);
}
return NULL;
}
#define die(msg) do { \
print(); \
printf(msg); \
exit(1); \
} while (0)
int self_tests() {
int rv;
int32_t ip = 0;
rv = insert ("abc", 3, 4);
if (!rv) die ("Failed to insert key abc\n");
rv = delete("abc", 3);
if (!rv) die ("Failed to delete key abc\n");
rv = insert ("google", 6, 5);
if (!rv) die ("Failed to insert key google\n");
rv = insert ("goggle", 6, 4);
if (!rv) die ("Failed to insert key goggle\n");
rv = delete("goggle", 6);
if (!rv) die ("Failed to delete key goggle\n");
rv = delete("google", 6);
if (!rv) die ("Failed to delete key google\n");
rv = insert ("ab", 2, 2);
if (!rv) die ("Failed to insert key ab\n");
rv = insert("bb", 2, 2);
if (!rv) die ("Failed to insert key bb\n");
print();
printf("So far so good\n\n");
rv = search("ab", 2, &ip);
printf("Rv is %d\n", rv);
if (!rv) die ("Failed to find key ab\n");
if (ip != 2) die ("Found bad IP for key ab\n");
rv = search("aa", 2, NULL);
if (rv) die ("Found bogus key aa\n");
ip = 0;
rv = search("bb", 2, &ip);
if (!rv) die ("Failed to find key bb\n");
if (ip != 2) die ("Found bad IP for key bb\n");
ip = 0;
rv = delete("cb", 2);
if (rv) die ("deleted bogus key cb\n");
rv = delete("bb", 2);
if (!rv) die ("Failed to delete real key bb\n");
rv = search("ab", 2, &ip);
if (!rv) die ("Failed to find key ab\n");
if (ip != 2) die ("Found bad IP for key ab\n");
ip = 0;
rv = delete("ab", 2);
if (!rv) die ("Failed to delete real key ab\n");
printf("End of self-tests, tree is:\n");
print();
printf("End of self-tests\n");
return 0;
}
void help() {
printf ("DNS Simulator. Usage: ./dns-[variant] [options]\n\n");
printf ("Options:\n");
printf ("\t-c numclients - Use numclients threads.\n");
printf ("\t-h - Print this help.\n");
printf ("\t-l length - Run clients for length seconds.\n");
printf ("\t-q - Allow a client to block (squat) if a requested name is taken.\n");
printf ("\t-t - Stress test name squatting.\n");
printf ("\n\n");
}
int main(int argc, char ** argv) {
int numthreads = 1; // default to 1
int c, i, rv;
pthread_t *tinfo;
int stress_squatting = 0;
// Read options from command line:
// # clients from command line, as well as seed file
// Simulation length
// Block if a name is already taken ("Squat")
// Stress test "squatting"
while ((c = getopt (argc, argv, "c:hl:qt")) != -1) {
switch (c) {
case 'c':
numthreads = atoi(optarg);
break;
case 'h':
help();
return 0;
case 'l':
simulation_length = atoi(optarg);
break;
case 'q':
allow_squatting = 1;
break;
case 't':
stress_squatting = 1;
break;
default:
printf ("Unknown option\n");
help();
return 1;
}
}
// Create initial data structure, populate with initial entries
// Note: Each variant of the tree has a different init function, statically compiled in
init(numthreads);
srandom(time(0));
// Run the self-tests if we are in debug mode
#ifdef DEBUG
self_tests();
#endif
// Launch client threads
tinfo = calloc(numthreads, sizeof(pthread_t));
for (i = 0; i < numthreads; i++) {
if (stress_squatting) {
rv = pthread_create(&tinfo[i], NULL,
&squatter_stress, &i);
} else {
rv = pthread_create(&tinfo[i], NULL,
&client, NULL);
}
if (rv != 0) {
printf ("Thread creation failed %d\n", rv);
return rv;
}
}
// After the simulation is done, shut it down
sleep (simulation_length);
finished = 1;
// Wait for all clients to exit. If we are allowing blocking,
// cancel the threads, since they may hang forever
if (allow_squatting)
{
printf("Sim done\n");
for (i = 0; i < numthreads; i++)
{
int rv = pthread_cancel(tinfo[i]);
if (rv != 0)
{
printf ("Uh oh. pthread_cancel failed %d\n", rv);
}
}
}
for (i = 0; i < numthreads; i++) {
int rv = pthread_join(tinfo[i], NULL);
if (rv != 0)
printf ("Uh oh. pthread_join failed %d\n", rv);
}
#ifdef DEBUG
/* Print the final tree for fun */
print();
#endif
return 0;
}