-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock_main.c~
409 lines (347 loc) · 8.53 KB
/
lock_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
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
#include"lock_manager.h"
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<limits.h>
extern list_head_t freeNodes;
extern tree_node_t *rootArray[MAX_NAMESPACE_ID];
extern next_index[MAX_NAMESPACE_ID];
extern tree_array[MAX_NODES+1];
/**
* @brief generate range lba range. Used for unit test.
*
**/
void random_lba_range(int *start_lba, int *end_lba){
*start_lba = rand()%UCHAR_MAX/2;
*end_lba = rand()%UCHAR_MAX;
if(*start_lba > *end_lba){
int temp = *start_lba;
*start_lba = *end_lba;
*end_lba = temp;
}
}
/**
* @brief obtain random node.
*
**/
void obtain_random_node(tree_node_t *node){
int start_lba, end_lba;
random_lba_range(&start_lba, &end_lba);
node->start_lba = start_lba;
node->end_lba = end_lba;
node->type = rand()%2;
if(next_index[0] + 1 > MAX_NODES){
memset(tree_array, 0, sizeof(tree_array));
obtainEventInexMarker(rootArray[0]);
updateIndex(rootArray[0]);
next_index[0] = sum(MAX_NODES);
}
next_index[0]++;
node->eventIndex = next_index[0];
}
/**
* @brief Test AVL balanced tree property is mained or not.
*
* @para[in] node -- root node of AVL tree
*
* @retval 1 -- balanced
* @retval 0 -- unbalanced.
*/
int test_AVL_balanced(tree_node_t *node){
if(node == NULL) return 1;
/*test root*/
if(height(node->child[RIGHT]) - height(node->child[LEFT]) >= 2)
return 0;
/*test left and right sub tree*/
if(!test_AVL_balanced(node->child[LEFT]) || !test_AVL_balanced(node->child[RIGHT])) return 0;
return 1;
}
/**
* @brief test insert to AVL tree
*
**/
void test_insert_AVL(){
int i;
treeInit();
srand(time(NULL));
/*test insert node*/
for(i = 0; i < MAX_NODES + 5; i++){
tree_node_t *node = allocNodes();
if(node == NULL) continue;
obtain_random_node(node);
printf("inserting [%d -- %d] R(%d) eventIndex %d \n",
node->start_lba,
node->end_lba,
node->type,
node->eventIndex);
insertNode(&rootArray[0], node, 1);
printf("AVL tree propety maintained? ");
if(test_AVL_balanced(rootArray[0]) == 1)
printf("Y\n");
else
printf("N\n");
printf("\n");
}
treeDump(rootArray[0]);
}
void test_delete_AVL(){
int i;
for(i = 0; i < MAX_NODES; i++){
printf("delete case %d\n", i);
int index = rand()%MAX_NODES;
if(isInAVL(rootArray[0], &nodes[index])){
/*found nodes and then to delete it*/
printf("delete event index %d\n", nodes[index].eventIndex);
removeNode(&rootArray[0], &nodes[index]);
/*
* check if this node has pending locks associated with it.
* And if so, try to add each one to the tree
*/
tree_node_t *pendingNode = (tree_node_t *) (nodes[index].pendingList.next);
while(pendingNode != &nodes[index]){
printf("insert node with index %2d\n", pendingNode->eventIndex);
tree_node_t *nextNode = (tree_node_t *) (pendingNode->pendingList.next);
pendingNode->child[0] = NULL;
pendingNode->child[1] = NULL;
/*
* Reset removed node pending list pointers
*/
pendingNode->pendingList.next =
pendingNode->pendingList.prev =
&pendingNode->pendingList;
insertNode(&rootArray[0], pendingNode, 1);
pendingNode = nextNode;
}
/*Add the removed node back to the free list*/
freeNode(&nodes[index]);
printf("AVL tree propety maintained? ");
if(test_AVL_balanced(rootArray[0]) == 1)
printf("Y\n");
else
printf("N\n");
printf("\n");
}
}
}
/**
* @brief: Test one example
*/
void test_case1(){
treeInit();
tree_node_t *n1,*n2, *n3, *n4, *n5, *n6;
n1 = allocNodes();
n2 = allocNodes();
n3 = allocNodes();
n4 = allocNodes();
n5 = allocNodes();
n6 = allocNodes();
/**
* Event 1: R [ 1- 40]
*/
n1->eventIndex = 1;
n1->start_lba = 1;
n1->end_lba = 40;
n1->type = 0;
/**
* Event 2: W [ 1- 10]
*/
n2->eventIndex = 2;
n2->start_lba = 1;
n2->end_lba = 10;
n2->type = 1;
/**
* Event 3: W [ 8- 20]
*/
n3->eventIndex = 3;
n3->start_lba = 8;
n3->end_lba = 20;
n3->type = 1;
/**
* Event 4: W [ 21- 30]
*/
n4->eventIndex = 4;
n4->start_lba = 21;
n4->end_lba = 30;
n4->type = 1;
/**
* Event 5: W [ 31- 40]
*/
n5->eventIndex = 5;
n5->start_lba = 31;
n5->end_lba = 40;
n5->type = 1;
/**
* Event 6: R [ 1- 40]
*/
n6->eventIndex = 6;
n6->start_lba = 1;
n6->end_lba = 40;
n6->type = 0;
/*insert all events, it should be a linked list
* event 1 R[1-40] -->event 2 W[1-10] -->event 3 W[8-20] -->event 4 W[21-30] -->event 5 W[31-40] -->event 6 R[1-40]
*/
insertNode(&rootArray[0], n1, 1);
insertNode(&rootArray[0], n2, 1);
insertNode(&rootArray[0], n3, 1);
insertNode(&rootArray[0], n4, 1);
insertNode(&rootArray[0], n5, 1);
insertNode(&rootArray[0], n6, 1);
treeDump(rootArray[0]);
printf("remove n1\n");
lockRelease(n1, 0);
/*
* the avl tree should be:
* event 4 --> event 6
* / \
*event 3 <- event 2 event 5
*/
treeDump(rootArray[0]);
/**
* delete event 6, and it should be rejected.
*/
printf("----\n\n");
printf("remove n6\n");
lockRelease(n6, 0);
treeDump(rootArray[0]);
/*
* delete event 2, and the AVL tree should be like this
*
* event 4 --> event 3 -->event 6
* \
* event 5
*/
printf("---\n\n");
printf("remove n2\n");
lockRelease(n2, 0);
treeDump(rootArray[0]);
/*
* delete event 4, and the AVL tree should be like this
*
* event 5 ->event 6
* /
* event 3
*/
printf("--\n\n");
printf("remove n4\n");
lockRelease(n4, 0);
treeDump(rootArray[0]);
/*
* delete event 5, and the AVL tree should be like this
*
* event 3 -->event 6
*
*/
printf("---\n\n");
printf("remove n5\n");
lockRelease(n5, 0);
treeDump(rootArray[0]);
/*
* delete event 3, and the AVL tree should be like this.
*
* event 6
*/
printf("--\n\n");
printf("remove n3\n");
lockRelease(n3, 0);
treeDump(rootArray[0]);
}
/*
* @brief: randomly lock MAX_NODES/2 logical block
* address range requests (no pending), then randomly
* queue MAX_NODES/2 logical block address range
* requests, and randomly release locks.
*/
void random_test1(){
int i, j;
for(j = 0; j < 100; j++){
treeInit();
/*
* to check the unoverlapped lock
*/
for(i = 0; i < MAX_NODES/2; i++){
unsigned start_lba = i*5;
unsigned end_lba = start_lba + 4;
unsigned type = rand()%2;
lockRequest(start_lba, end_lba, type, 1, 0);
}
/*
* to check the overlapped lock
*/
for(i = 0; i < MAX_NODES/2; i++){
unsigned start_lba = i + 1;
unsigned end_lba = start_lba + rand()%10;
unsigned type = rand()%2;
lockRequest(start_lba, end_lba, type, 1, 0);
}
// treeDump(rootArray[0]);
/*
* to check the lock release
*/
for(i = 0; i < MAX_NODES; i++){
printf("\n\ndeleting case %d with ", i+ 1);
int index = (MAX_NODES - i - 1);
printf("event index %d\n", nodes[index].eventIndex);
lockRelease(&nodes[index], 0);
}
treeDump(rootArray[0]);
printf("\n");
}
}
/**
* @brief: random test 2, unoverlapped range lock and unlock
*
*/
void random_test2(){
int i, j;
for(j = 0; j < 1; j++){
treeInit();
/*
*to check the lock
*/
for(i = 0; i < MAX_NODES; i++){
unsigned start_lba = i*5;
unsigned end_lba = start_lba + 4;
unsigned type = rand()%2;
lockRequest(start_lba, end_lba, type, 1, 0);
}
//treeDump(rootArray[0]);
/*
*to check the unlock
*/
for(i = 0; i < MAX_NODES; i++){
printf("\n\ndeleting case %d with", i+ 1);
int index = (MAX_NODES - i - 1);
printf(" event index %d\n", nodes[index].eventIndex);
lockRelease(&nodes[index], 0);
}
// treeDump(rootArray[0]);
/*
*to check the index overflow
*/
for(i = 0; i < MAX_NODES; i++){
unsigned start_lba = i*5;
unsigned end_lba = start_lba + 4;
unsigned type = rand()%2;
lockRequest(start_lba, end_lba, type, 1, 0);
}
printf("\n");
}
}
int main(){
int i = 0;
/**
* @brief overnight running to test random insert and random delete
*/
/*for(i = 0; i < INT_MAX; i++){
test_insert_AVL();
test_delete_AVL();
}*/
/*
* @brief test the example presented in the document
*/
//test1();
//random_test1();
random_test2();
return 1;
}