-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiskUnit.java
executable file
·328 lines (296 loc) · 8.43 KB
/
DiskUnit.java
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
/*
* DiskUnit.java
*
* Quick tests of Disk
*
* You must follow the coding standards distributed
* on the class web page.
*
* (C) 2007,2010 Mike Dahlin
*
*/
import java.util.concurrent.locks.Condition;
import java.io.FileNotFoundException;
public class DiskUnit implements DiskCallback{
private static final int NUM = 100;
boolean anyHaveError;
private static final int INPROGRESS = 234;
private static final int DONE_OK = 9234;
private static final int DONE_ERROR = 22842;
private static final boolean verbose = true;
private static final boolean vverbose = false;
int status[];
int doneCount;
SimpleLock lock;
Condition resultAvailable;
//-------------------------------------------------------
// main() -- java DiskUnit to run this test
//-------------------------------------------------------
public static void main(String args[])
{
testWrites(true, (float)0.0);
System.out.println("Wrote 0 to all");
testWrites(false, (float)0.0);
System.out.println("Wrote data to all");
testReads(true);
System.out.println("Read data from all");
testWrites(true, (float)0.0);
System.out.println("Wrote 0 to all");
testWrites(false, (float)0.1);
System.out.println("Wrote data to some");
testReads(false);
System.out.println("Read data from some");
System.exit(0);
}
//-------------------------------------------------------
// Issue a one write to each of a bunch of disk sectors
// Expect all of the writes to have completed properly...
//-------------------------------------------------------
private static void testWrites(boolean writeZero, float errorPct)
{
DiskUnit du;
Disk d = null;
int ii;
assert(NUM < Disk.NUM_OF_SECTORS);
int ok;
du = new DiskUnit();
try{
d = new Disk(du, errorPct);
}
catch(FileNotFoundException fnf){
System.out.println("Unable to open disk file");
System.exit(-1);
}
du.doClear();
for(ii = 0; ii < NUM; ii++){
//
// Remember: Do not re-use buffer until
// request completes!
//
byte b[] = new byte[Disk.SECTOR_SIZE];
if(writeZero){
setBuffer((byte)0, b);
}
else{
setBuffer((byte)ii, b);
}
try{
d.startRequest(Disk.WRITE, ii, ii, b);
}
catch(Exception e){
System.out.println("Unexpected exception in startReq write"
+ e.toString() + " ii=" + ii);
System.exit(-1);
}
}
if(verbose){
System.out.println("Writes started");
}
ok = du.check(); // All writes done
if(verbose){
System.out.println("Writes done");
}
if(ok < NUM && errorPct == 0){
System.out.println("Fewer writes than expected " + ok);
return;
}
if(ok == NUM && errorPct > 0){ // Make sure expected num writes nonzero
System.out.println("More writes than expected " + ok);
return;
}
}
//-------------------------------------------------------
// Issue a one read to each of a bunch of disk sectors
//-------------------------------------------------------
private static void testReads(boolean expectAll)
{
DiskUnit du;
Disk d = null;
int ii;
assert(NUM < Disk.NUM_OF_SECTORS);
int ok;
du = new DiskUnit();
try{
d = new Disk(du, (float)0.0);
}
catch(FileNotFoundException fnf){
System.out.println("Unable to open disk file");
System.exit(-1);
}
du.doClear();
for(ii = 0; ii < NUM; ii++){
//
// Remember: Do not re-use buffer until
// request completes!
//
byte b[] = new byte[Disk.SECTOR_SIZE];
setBuffer((byte)0, b);
try{
d.startRequest(Disk.READ, ii, ii, b);
}
catch(Exception f){
System.out.println("Unexpected exception in startReq read");
System.exit(-1);
}
}
if(verbose){
System.out.println("Reads started");
}
ok = du.check(); // All reads done
if(verbose){
System.out.println("Reads done");
}
if(expectAll && ok < NUM){
System.out.println("Fewer reads than expected " + ok);
System.exit(-1);
return;
}
if(!expectAll && ok == NUM){
System.out.println("More reads than expected " + ok);
return;
}
return;
}
//-------------------------------------------------------
// set Disk.SECTOR_SIZE bytes to specified value
//-------------------------------------------------------
private static void setBuffer(byte value, byte b[])
{
int ii;
for(ii = 0; ii < Disk.SECTOR_SIZE; ii++){
b[ii] = value;
}
return;
}
//-------------------------------------------------------
// DiskUnit
//-------------------------------------------------------
private DiskUnit()
{
status = new int[NUM];
this.lock = new SimpleLock();
this.resultAvailable = lock.newCondition();
doClear();
}
//-------------------------------------------------------
// clear
//-------------------------------------------------------
public void doClear()
{
int ii;
try{
lock.lock();
for(ii = 0; ii < NUM; ii++){
status[ii] = INPROGRESS;
}
anyHaveError = false;
doneCount = 0;
}
finally{
lock.unlock();
}
}
//-------------------------------------------------------
// check
//-------------------------------------------------------
public int check()
{
try{
lock.lock();
while(!anyHaveError && doneCount < NUM){
resultAvailable.awaitUninterruptibly();
}
return doneCount;
}
finally{
lock.unlock();
}
}
//-------------------------------------------------------
// requesDone -- callback -- check writes for status
// and check reads for
//-------------------------------------------------------
public void requestDone(DiskResult result)
{
int sec;
try{
lock.lock();
//
// For this simple test, we have at most one outstanding request
// per sector, so we can use sector number to know which
// request this reply finishes.
// Could use tag to match requests with replies instead
//
sec = result.getSectorNum();
if(status[sec] == INPROGRESS){
if(result.getStatus() == DiskResult.OK){
if(vverbose){
System.out.println("Request " + result.getSectorNum() + " done");
}
if(checkOK(result)){
doneCount++;
status[sec] = DONE_OK;
if(doneCount == NUM){
resultAvailable.signal();
}
return;
}
else{
//
// Expected case if writes did not complete
//
if(vverbose){
System.out.println("Request " + result.getSectorNum() + " missing data");
}
status[sec] = DONE_ERROR;
anyHaveError = true;
resultAvailable.signal();
return;
}
}
else if(result.getStatus() == DiskResult.REAL_ERROR){
System.out.println("UNEXPECTED ERROR: real IO error");
System.exit(-1);
}
else{
//
// Fake error. Expected case
//
if(vverbose){
System.out.println("Request " + result.getSectorNum() + " fake error");
}
status[sec] = DONE_ERROR;
anyHaveError = true;
resultAvailable.signal();
return;
}
}
System.out.println("UNEXPECTED ERROR: multiple returns for same entry");
System.exit(-1);
return;
}
finally{
lock.unlock();
}
}
//-------------------------------------------------------
// check -- check for expected data in buffer if this is a read
//-------------------------------------------------------
private static boolean checkOK(DiskResult r)
{
int ii;
int secNum;
byte b[];
if(r.getOperation() == Disk.WRITE){
return true;
}
secNum = r.getSectorNum();
b = r.getBuf();
for(ii = 0; ii < Disk.SECTOR_SIZE; ii++){
if(b[ii] != (byte)secNum){
return false;
}
}
return true;
}
}