-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.c
91 lines (78 loc) · 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
/*
test program for the RAID project.
*/
#include "disk-array.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int verbose = 1;
int main( int argc, char *argv[] )
{
if(argc!=4) {
printf("use: %s <ndisks> <nblocks> <niter>\n",argv[0]);
return 1;
}
int ndisks = atoi(argv[1]);
int nblocks = atoi(argv[2]);
int niter = atoi(argv[3]);
disk_array_t da = disk_array_create("myvirtualdisk",ndisks,nblocks);
if(!da) {
fprintf(stderr,"couldn't create virtual disk: %s\n",strerror(errno));
return 1;
}
//
// Randomly read/write to disks
//
char buffer[BLOCK_SIZE];
char cmpbuffer[BLOCK_SIZE];
char zerobuffer[BLOCK_SIZE];
int i;
int failed_disk = -1;
memset(zerobuffer,0,sizeof(zerobuffer));
#define PROB_FAIL 10
#define PROB_RECOVER 10
#define NUM_OPS 2
#define OP_READ 0
#define OP_WRITE 1
for (i = 0; i < niter; i++) {
if (failed_disk != -1) {
if ((rand() % PROB_RECOVER) == 0) {
disk_array_recover_disk(da,failed_disk);
failed_disk = -1;
continue;
}
} else if ((rand() % PROB_FAIL) == 0) {
failed_disk = rand() % ndisks;
disk_array_fail_disk(da,failed_disk);
continue;
}
int disk = rand() % ndisks;
int block = rand() % nblocks;
switch (rand() % NUM_OPS) {
case OP_READ:
if (disk_array_read(da, disk, block, buffer) == 0) {
memset(cmpbuffer,(disk+1)*(block+1),sizeof(cmpbuffer));
if ((memcmp(cmpbuffer,buffer,sizeof(buffer)) != 0) &&
(memcmp(zerobuffer,buffer,sizeof(buffer)) != 0))
printf("Error: mismatch disk %d block %d value %d should be %d or zero\n",
disk,
block,
buffer[0],
cmpbuffer[0]);
}
break;
case OP_WRITE:
memset(buffer,(disk+1)*(block+1),sizeof(buffer));
disk_array_write(da, disk, block, buffer);
printf("Wrote disk %d block %d value %d\n",
disk, block, buffer[0]);
break;
default:
printf("Illegal operation value\n");
break;
}
}
disk_array_close(da);
return 0;
}