diskdevice-model.c
52.7 KB
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
/*
* Copyright (C) 1996-1998 by the Board of Trustees
* of Leland Stanford Junior University.
*
* This file is part of the SimOS distribution.
* See LICENSE file for terms of the license.
*
*/
/* @TITLE "diskdevice-model.c: Simulate a disk device"*/
/*
* diskdevice-model - simulating an HP97560 disk devices, and the bus.
* THIS FILE IS INCLUDED BY DISKDEVICE.C.
*
* exported functions:
* DiskDeviceInit
* DiskDeviceDone
* DiskDeviceTransfer
* DiskDeviceStart
* DiskDeviceFinish
* DiskDeviceSync
* DiskDeviceShape
*
* David Kotz 1993-94, Song Bac Toh 1994
*
* There is a Dartmouth College Technical Report that describes this code.
* The number is PCS-TR94-220. It can be found for ftp at cs.dartmouth.edu.
*/
/* $Id: diskdevice-model.c,v 1.1.1.1 2002/05/29 01:09:11 blythe Exp $ */
#include <math.h>
#ifndef SOLO
#include "simtypes.h"
#include "simos_interface.h"
#include "cpu_interface.h"
#endif
#ifdef DFK
# include "diskdevice-dfk.h" /* only used when in context of DFK's program */
#else
# include "modularize.h" /* miscellaneous stuff */
#endif /* DFK */
#include "diskevent.h" /* link to diskevent.c */
#include "diskdevice.h" /* defines interface to this module */
#include "diskdevices.h" /* defines details about different devices */
/* debugging */
/* you probably want to turn on tracing in diskevent.c too */
/* #define DEBUG */
#ifdef DEBUG
# define DEBUG_TRACE(x) {CYCLE_COUNTING_OFF; printf x; printf("\r"); CYCLE_COUNTING_ON;}
#else
# define DEBUG_TRACE(x) {}
#endif /* DEBUG */
static boolean first_init = TRUE;
/* @SUBTITLE "Per-disk status structure" */
/* structure that contains information about status of the disk */
typedef struct diskstatus
{
boolean done; /* is the device done being used? */
/* when the current request started and finished */
TICS startTime; /* when request started (NEVER if none) */
TICS finishTime; /* when previous request finished */
boolean reading; /* TRUE if reading, FALSE if writing */
/* a way to communicate a new request from DiskDeviceXfer to Controller */
ulong newSector; /* the first sector to transfer */
ulong newLastSector; /* the last sector to transfer */
boolean newReading; /* TRUE if reading, FALSE if writing */
boolean newHaveBuffer; /* does the host have the buffer yet? */
/* state of the disk - which track are heads at now? */
boolean track; /* current track */
/* There is no need to keep track of the disk rotational position since
* we are assuming that disk head is at position 0 at time 0.
* The range for disk head position is 0~1 with (position 0 == position 1)
*/
boolean activeBusXfer; /* is this bus actively using the bus? */
boolean activeRead; /* is a read diskxfer happening now? */
boolean cancelPrefetch; /* should we cancel an outstanding prefetch? */
DDEvent *prefetchReq; /* a cancellable EndDiskXfer request */
boolean needMove; /* do we need DiskMove before restarting xfers */
TICS wantBus; /* after what time would we like to use bus */
boolean haveBuffer; /* do we have the buffer at host end of bus? */
boolean wantSync; /* host wants to know when write-behind done */
TICS restartController; /* when should Controller() be run again */
/* Each variable below contains a logical sector number.
* firstCachedSector always contains the sector of the lowest-numbered
* sector in the cache.
* nextDiskXfer and nextBusXfer always contain the number of the
* sector to be transferred next.
* currentDiskXfer and currentBusXfer contains the number of the
* sector currently being transferred.
*
* Some invariants:
* When reading,
* FCS <= CBX <= NBX <= CDX <= NDX
* FCS is not actually in the cache unless FCS < NBX
* When writing,
* FCS <= CDX <= NDX <= CBX <= NBX
* FCS is not actually in the cache unless FCS < NDX
* NBX-CBX == 0 or 1
* if 0, no bus transfer is active
* if 1, sector CBX is going across the bus
* NDX-CDX == 0 or 1
* if 0, no disk transfer is active
* if 1, sector CBX is going across the bus
*/
ulong firstCachedSector; /* sector at head of cache */
ulong currentDiskXfer; /* sector currently being transferred */
ulong nextDiskXfer; /* sector to be transferred next */
ulong currentBusXfer; /* sector currently being transferred */
ulong nextBusXfer; /* sector to be transferred next */
/* macros to find state of disk cache */
#define CACHE_SLOTS_USED(disk) ((dstatus[disk].reading ? \
dstatus[disk].nextDiskXfer \
:dstatus[disk].nextBusXfer) \
- dstatus[disk].firstCachedSector)
#define CACHE_EMPTY(disk) (CACHE_SLOTS_USED(disk) <= 0)
#define CACHE_FULL(disk) (CACHE_SLOTS_USED(disk) >= CACHE_SLOTS)
ulong firstSector; /* first sector involved in current request */
ulong lastSector; /* last sector involved in current request */
int wait_thread; /* what thread (STID) is waiting on request */
int busId; /* which bus do we use (for tracing)? */
int *busOwner; /* (*busOwner == BUS_FREE_ ==> bus is free */
NCQUEUE *busq; /* place to queue disks waiting for bus */
FILE *datafile; /* Unix file to read/write the data from/to */
} diskstatus;
/* data structure keeping the status of the disks */
PRIVATE struct diskstatus dstatus[NO_OF_DISKS];
/* Records to go on the busq;
* basically, an event to schedule when bus is free
*/
typedef struct busitem {
int disk;
REQUESTCODE event;
} busItem;
/* @SUBTITLE "Utility functions" */
PRIVATE TICS MoveTime_t(int disk, ulong lsector, TICS eventTime);
PRIVATE TICS MoveNextTime_t(int disk, ulong lsector, TICS eventTime);
PRIVATE ulong Logical2Physical(ulong lsector);
PRIVATE TICS RotDelay_t(int disk, ulong lsector, TICS eventTime);
PRIVATE TICS SeekTime_t(ulong dcylinders, ulong dtracks, TICS eventTime);
PRIVATE void SanityCheck(int disk, ulong lsector, ulong nsectors);
PRIVATE boolean GrabBus(int disk, TICS *grabTime, TICS eventTime);
PRIVATE void WaitForBus(int disk, REQUESTCODE event, TICS eventTime);
PRIVATE void PassBus(int disk, TICS eventTime);
/* Print out the requests scheduled (in diskevent.c) */
extern void DDPrintRequests(void);
/* @SUBTITLE "Event-handler functions" */
PRIVATE void DiskMove(int disk, TICS now);
PRIVATE void StartDiskXfer(int disk, TICS now);
PRIVATE void EndDiskXfer(int disk, TICS now);
PRIVATE void ConsiderBusXfer(int disk, TICS now);
PRIVATE void StartBusXfer(int disk, TICS now);
PRIVATE void EndBusXfer(int disk, TICS now);
PRIVATE void SendCommand(int disk, TICS now);
PRIVATE void EndCommand(int disk, TICS now);
PRIVATE void Controller(int disk, TICS now);
PRIVATE void SendSyncCommand(int disk, TICS now);
PRIVATE void EndSyncCommand(int disk, TICS now);
PRIVATE void SendBufferMesg(int disk, TICS eventTime);
PRIVATE void EndBufferMesg(int disk, TICS eventTime);
PRIVATE void SendDoneMessage(int disk, TICS now);
PRIVATE void EndDoneMessage(int disk, TICS now);
/* possible types of requests */
struct dd_handlers DDhandlers[] =
{
{ NULL, "NOP" },
{ DiskMove, "DiskMove" },
{ StartDiskXfer, "StartDiskXfer" },
{ EndDiskXfer, "EndDiskXfer"},
{ ConsiderBusXfer, "ConsiderBusXfer" },
{ StartBusXfer, "StartBusXfer" },
{ EndBusXfer, "EndBusXfer" },
{ SendCommand, "SendCommand" },
{ EndCommand, "EndCommand" },
{ Controller, "Controller" },
{ SendSyncCommand, "SendSyncCommand" },
{ EndSyncCommand, "EndSyncCommand" },
{ SendBufferMesg, "SendBufferMesg" },
{ EndBufferMesg, "EndBufferMesg" },
{ SendDoneMessage, "SendDoneMessage" },
{ EndDoneMessage, "EndDoneMessage" }
};
/* ======================================================================== */
/* @SUBTITLE "DiskDeviceInit: initialize a disk" */
/* The caller must provide a pointer to a boolean, which
* we use to tell when that bus is free. We also need a NCQUEUE, with room
* for as many disks as are attached to that bus, where disks can wait
* for service on the bus. Presumably, several disks share
* the same values; thus, this is how we handle bus contention.
* It must not be deallocated until after DiskDeviceDone is called.
* The busId is used for debugging and tracing, nothing functional.
*/
void
DiskDeviceInit(int disk, int busId, int *busOwner, NCQUEUE *busWaitq,
char *diskFileName)
{
struct diskstatus *ds = &(dstatus[disk]);
if (first_init) {
/* initialize the disk event subsystem, once only */
DDEventInit();
first_init = FALSE;
}
/* initialize the disk status */
ds->track = 0; /* physical track of the disk */
ds->activeRead = FALSE;
ds->activeBusXfer = FALSE;
ds->done = FALSE;
ds->wantBus = NEVER;
ds->needMove = FALSE;
ds->cancelPrefetch = FALSE;
ds->firstSector = 0;
ds->lastSector = 0;
ds->reading = TRUE;
ds->busId = busId;
ds->busq = busWaitq;
ds->busOwner = busOwner;
ds->firstCachedSector = 0; /* Initially cache is empty */
ds->currentDiskXfer = 0; /* Initially the two numbers are equal - */
ds->nextDiskXfer = 0; /* no disk transfer. */
ds->currentBusXfer = 0; /* Initially the two numbers are equal - */
ds->nextBusXfer = 0; /* no bus transfer. */
ds->haveBuffer = FALSE;
ds->wantSync = FALSE;
ds->prefetchReq = NULL;
/* use these to pass parameters to Controller() */
ds->newSector = 0; /* no pending transfer or move request */
ds->newLastSector = 0; /* no request is being processed */
ds->newReading = FALSE;
ds->newHaveBuffer = FALSE;
/* use this flag to determine whether we are the first time through
the controller (NEVER == first time through) */
ds->restartController = NEVER;
/* use this flag to check whether a DiskDevice* is finished */
ds->startTime = NEVER;
ds->finishTime = 0;
/* decide whether to use some Unix file as a pretend "disk" */
if (diskFileName == NULL)
ds->datafile = NULL; /* do not use any disk file; dummy data only */
else {
/* try to open the given file as a place for this "disk"s data */
/* upon error in fopen(), ds->datafile will also be assigned NULL */
ds->datafile = fopen(diskFileName, "r+");
/* file does not exist, try to create the file */
if (ds->datafile == NULL) {
ds->datafile = fopen(diskFileName, "w+");
/* can't even create it, fail it */
INVARIANT3(ds->datafile != NULL,
"DiskDeviceInit: disk file %s cannot be opened\n",
diskFileName);
}
}
INDEX_TIME_EVENT(EV_DISKSTATE, disk, DISKSTATE_IDLE, 0);
}
/* @SUBTITLE "DiskDeviceDone: cleanup" */
void
DiskDeviceDone(int disk)
{
struct diskstatus *ds = &(dstatus[disk]);
INVARIANT3(!ds->activeBusXfer,
"DiskDeviceDone: disk %d bus transfer still active\n", disk);
INVARIANT3(ds->wantBus == NEVER,
"DiskDeviceDone: disk %d still wants the bus\n", disk);
INVARIANT3(ds->startTime == NEVER,
"DiskDeviceDone %d: previous DiskDeviceStart not finished!\n",
disk);
INVARIANT3(ds->currentDiskXfer >= ds->currentBusXfer,
"DiskDeviceDone %d: needs Sync!\n", disk);
DEBUG_TRACE(("DiskDeviceDone \t%d\t@%s\n", disk, time_print(GetTime())));
ds->firstCachedSector = 0; /* reset the cache */
ds->currentDiskXfer = ds->nextDiskXfer = 0;
ds->currentBusXfer = ds->nextBusXfer = 0;
if (ds->datafile != NULL)
fclose(ds->datafile); /* close the datafile */
ds->done = TRUE;
/* record the final idle period */
if (ds->finishTime > 0) /* this disk was used at some point */
SUM_ARRAY_METRIC(stats.diskidle, disk,
(GetTime() - ds->finishTime) * MSECperTIC);
INDEX_TIME_EVENT(EV_DISKIDLE, disk, 0, ds->finishTime);
INDEX_TIME_EVENT(EV_DISKSTATE, disk, DISKSTATE_IDLE, ds->finishTime);
}
/* @SUBTITLE "DiskDeviceStart: start a disk transaction" */
/* Like DiskDeviceTransfer, but split into two parts: the first part
* starts the disk moving to the appropriate location, and the second supplies
* the buffer needed to complete the transaction. Must be followed by a
* corresponding Finish.
*/
void
DiskDeviceStart(int disk, ulong sector, ulong nsectors, boolean write)
{
TICS eventTime;
struct diskstatus *ds = &(dstatus[disk]);
/* check whether the caller of this function is insane */
SanityCheck(disk, sector, nsectors);
/* record the preceding idle period */
if (ds->finishTime > 0) /* this disk has been used before */
SUM_ARRAY_METRIC(stats.diskidle, disk,
(GetTime() - ds->finishTime) * MSECperTIC);
INDEX_TIME_EVENT(EV_DISKIDLE, disk, 0, ds->finishTime);
/* record the event of reading or writing */
INDEX_EVENT(write ? EV_DISKWRITE : EV_DISKREAD, disk, sector);
SUM_ARRAY_METRIC(write ? stats.diskwrite : stats.diskread, disk, 1.0);
INDEX_EVENT(EV_DISKBUSY, disk, 0);
eventTime = GetTime();
ds->startTime = eventTime; /* start the disk */
DEBUG_TRACE(("DiskDeviceStart \t%d\t@%s: sector:%lu nsectors:%lu R/W:%d\n",
disk, time_print(eventTime), sector, nsectors, write));
ds->newReading = !write;
ds->newSector = sector;
/* go through all sectors */
ds->newLastSector = sector + nsectors - 1;
ds->newHaveBuffer = FALSE; /* we don't have the buffer yet */
DEBUG_TRACE(("\t"));
SendCommand(disk, eventTime); /* schedules EndCommand */
DEBUG_TRACE(("\tDiskDeviceStart \t%d\t@%s suspending %d/%d\n",
disk, time_print(eventTime), CURR_PROCESSOR, MY_TID));
/* now wait for EndCommand to wake us up */
ds->wait_thread = MY_STID;
thread_suspend(MY_TID);
}
/* @SUBTITLE "DiskDeviceFinish: finish a disk transaction" */
/* See DiskDeviceStart. This, the second call,
* BLOCKs until the transfer is complete. There should not have been
* any other requests since the corresponding Start. Ideally, we should
* not be late in calling DiskDeviceFinish, ie, not after the disk movement
* has finished.
*/
void
DiskDeviceFinish(int disk, UserData buffer)
{
struct diskstatus *ds = &(dstatus[disk]);
TICS eventTime = GetTime();
ulong xferSize = ds->lastSector - ds->firstSector + 1;
INVARIANT3(ds->startTime != NEVER,
"DiskDeviceFinish %d: no corresponding DiskDeviceStart\n",
disk);
DEBUG_TRACE(("DiskDeviceFinish \t%d\n", disk));
/* process the request */
ds->newHaveBuffer = TRUE; /* EndBufferMesg set hasBuffer */
DEBUG_TRACE(("\t"));
SendBufferMesg(disk, eventTime);
DEBUG_TRACE(("\tDiskDeviceFinish \t%d\t@%s suspending %d/%d\n",
disk, time_print(eventTime), CURR_PROCESSOR, MY_TID));
ds->wait_thread = MY_STID;
thread_suspend(MY_TID); /* send the thread to sleep */
ds->finishTime = GetTime();
AVG_ARRAY_METRIC(stats.diskwait, disk,
(ds->finishTime - ds->startTime) * MSECperTIC);
STDDEV_ARRAY_METRIC(stats.diskwait_sd, stats.diskwait, disk,
(ds->finishTime - ds->startTime) * MSECperTIC);
ds->startTime = NEVER; /* reset the disk */
/* actually read and write the data to vdisk */
if (ds->datafile != NULL) {
fseek(ds->datafile, (long)(ds->firstSector * SECTOR_SIZE), SEEK_SET);
if (ds->reading)
fread(buffer, (size_t)SECTOR_SIZE, xferSize, ds->datafile);
else {
INVARIANT2(fwrite(buffer, (size_t)SECTOR_SIZE, xferSize,
ds->datafile) == xferSize,
"DiskDeviceFinish: error while writing to data file\n");
}
}
}
/* @SUBTITLE "DiskDeviceShape: return the shape of the disk" */
/* Return the shape of the disk. */
void
DiskDeviceShape(int disk, ulong *nSectors, ulong *sectorSize,
ulong *nTracks, ulong *sectorsPerTrack)
{
*nSectors = TOTAL_NSECTORS;
*sectorSize = SECTOR_SIZE;
*nTracks = NTRACKS;
*sectorsPerTrack = NSECTORS;
}
/* ======================================================================= */
/* @SUBTITLE "DiskDeviceTransfer: a complete disk transaction" */
/* Do a disk operation. We give it everything it needs to know: the disk
* number, sector number on that disk, length in sectors,
* a read/write flag, and the buffer for the data.
* This BLOCKS until the time when the transfer would be complete.
*/
void
DiskDeviceTransfer(int disk, ulong sector, ulong nsectors,
boolean write, UserData buffer)
{
TICS eventTime;
struct diskstatus *ds = &(dstatus[disk]);
/* check whether the caller of this function is insane */
SanityCheck(disk, sector, nsectors);
/* record the preceding idle period */
if (ds->finishTime > 0) { /* this disk has been used before */
SUM_ARRAY_METRIC(stats.diskidle, disk,
(GetTime() - ds->finishTime) * MSECperTIC);
INDEX_TIME_EVENT(EV_DISKIDLE, disk, 0, ds->finishTime);
}
/* record the event of reading or writing */
INDEX_EVENT(write ? EV_DISKWRITE : EV_DISKREAD, disk, sector);
SUM_ARRAY_METRIC(write ? stats.diskwrite : stats.diskread, disk, 1.0);
INDEX_EVENT(EV_DISKBUSY, disk, 0);
eventTime = GetTime();
ds->startTime = eventTime; /* start the disk */
DEBUG_TRACE(("DiskDeviceXfer \t%d\t@%s: sector:%lu nsectors:%lu R/W:%d\n",
disk, time_print(eventTime), sector, nsectors, write));
/* only Controller() can change the values */
ds->newReading = !write;
ds->newSector = sector;
/* go through all sectors */
ds->newLastSector = sector + nsectors - 1;
ds->newHaveBuffer = TRUE;
/* ask the controller to process the request */
DEBUG_TRACE(("\t"));
SendCommand(disk, eventTime);
DEBUG_TRACE(("\tDiskDeviceTransfer \t%d\t@%s suspending %d/%d\n",
disk, time_print(eventTime), CURR_PROCESSOR, MY_TID));
ds->wait_thread = MY_STID;
thread_suspend(MY_TID); /* send the thread to sleep */
ds->finishTime = GetTime();
AVG_ARRAY_METRIC(stats.diskwait, disk,
(ds->finishTime - ds->startTime) * MSECperTIC);
STDDEV_ARRAY_METRIC(stats.diskwait_sd, stats.diskwait, disk,
(ds->finishTime - ds->startTime) * MSECperTIC);
ds->startTime = NEVER; /* reset the disk */
/* actually read and write the data to vdisk */
if (ds->datafile != NULL) {
fseek(ds->datafile, ((long) sector * SECTOR_SIZE), SEEK_SET);
if (ds->reading)
fread(buffer, (size_t)SECTOR_SIZE, nsectors, ds->datafile);
else
INVARIANT2(fwrite(buffer, (size_t)SECTOR_SIZE, nsectors,
ds->datafile) == nsectors,
"DiskDeviceTransfer: error while writing to data file\n");
}
}
#ifndef SOLO
static struct {
dmaRoutine_t dmaRoutine;
void *dmaArg;
doneRoutine_t doneRoutine;
int doneArg;
} simosInterface[NO_OF_DISKS];
PRIVATE void
FinishRequest(void *clientData)
{
#ifdef SOLO
int disk = (int) clientData;
#else
int disk = PTR_TO_UINT64(clientData);
#endif
struct diskstatus *ds = &(dstatus[disk]);
ds->finishTime = GetTime();
AVG_ARRAY_METRIC(stats.diskwait, disk,
(ds->finishTime - ds->startTime) * MSECperTIC);
STDDEV_ARRAY_METRIC(stats.diskwait_sd, stats.diskwait, disk,
(ds->finishTime - ds->startTime) * MSECperTIC);
ds->startTime = NEVER; /* reset the disk */
simosInterface[disk].doneRoutine( simosInterface[disk].doneArg);
}
/* ======================================================================= */
/* @SUBTITLE "DiskDeviceTransferStart: a complete disk transaction" */
/* Do a disk operation. We give it everything it needs to know: the disk
* number, sector number on that disk, length in sectors,
* a read/write flag, and the buffer for the data.
*/
void
DiskDeviceTransferStart(int disk, ulong sector, ulong nsectors, boolean write,
dmaRoutine_t dmaRoutine, void *dmaArg,
doneRoutine_t doneRoutine, int doneArg)
{
TICS eventTime;
struct diskstatus *ds = &(dstatus[disk]);
/* check whether the caller of this function is insane */
SanityCheck(disk, sector, nsectors);
/* record the preceding idle period */
if (ds->finishTime > 0) { /* this disk has been used before */
SUM_ARRAY_METRIC(stats.diskidle, disk,
(GetTime() - ds->finishTime) * MSECperTIC);
INDEX_TIME_EVENT(EV_DISKIDLE, disk, 0, ds->finishTime);
}
/* record the event of reading or writing */
INDEX_EVENT(write ? EV_DISKWRITE : EV_DISKREAD, disk, sector);
SUM_ARRAY_METRIC(write ? stats.diskwrite : stats.diskread, disk, 1.0);
INDEX_EVENT(EV_DISKBUSY, disk, 0);
eventTime = GetTime();
ds->startTime = eventTime; /* start the disk */
DEBUG_TRACE(("DiskDeviceXfer \t%d\t@%s: sector:%lu nsectors:%lu R/W:%d\n",
disk, time_print(eventTime), sector, nsectors, write));
/* only Controller() can change the values */
ds->newReading = !write;
ds->newSector = sector;
/* go through all sectors */
ds->newLastSector = sector + nsectors - 1;
ds->newHaveBuffer = TRUE;
/* ask the controller to process the request */
DEBUG_TRACE(("\t"));
SendCommand(disk, eventTime);
DEBUG_TRACE(("\tDiskDeviceTransfer \t%d\t@%s suspending %d/%d\n",
disk, time_print(eventTime), CURR_PROCESSOR, MY_TID));
simosInterface[disk].doneRoutine = doneRoutine;
simosInterface[disk].doneArg = doneArg;
simosInterface[disk].dmaRoutine = dmaRoutine;
simosInterface[disk].dmaArg = dmaArg;
ds->wait_thread = MY_STID;
thread_suspend_continue(MY_TID, FinishRequest, (void *)UINT_TO_PTRSIZE(disk));
/* send the thread to sleep */
}
#endif
/* @SUBTITLE "DiskDeviceSync: Synchronizing the disk." */
/* All the data in the disk cache is written to the disk before returning.
*/
void
DiskDeviceSync(int disk)
{
TICS eventTime;
struct diskstatus *ds = &(dstatus[disk]);
INVARIANT3(ds->startTime == NEVER,
"DiskDeviceSync %d: previous DiskDeviceStart not finished!\n",
disk);
DEBUG_TRACE(("DiskDeviceSync \t%d\n", disk));
eventTime = GetTime();
SendSyncCommand(disk, eventTime);
ds->wait_thread = MY_STID;
thread_suspend(MY_TID); /* send the thread to sleep, SendDoneMessage
wakes it up */
}
/* ======================================================================= */
/* @SUBTITLE "SendCommand: send a command to the disk" */
/* Called by DiskDeviceTransfer to send a command to the disk.
* It schedules an EndCommand() call in the future.
*/
PRIVATE void
SendCommand(int disk, TICS eventTime)
{
TICS grabTime = 0; /* time it took to grab the bus */
DEBUG_TRACE(("SendCommand\t%d\t@%s: nextDiskXfer = %lu\n", disk,
time_print(eventTime), dstatus[disk].nextDiskXfer));
if (GrabBus(disk, &grabTime, eventTime)) {
DDEventRequest(ENDCOMMAND, (eventTime + grabTime + COMMANDTIME_t),
disk);
if (grabTime > 0) {
INDEX_TIME_EVENT(EV_BUSSTATE, dstatus[disk].busId, BUSSTATE_GRAB,
eventTime);
}
INDEX_TIME_EVENT(EV_BUSSTATE, dstatus[disk].busId, BUSSTATE_COMMAND,
eventTime + grabTime);
} else {
DEBUG_TRACE(("SendCommand: Waiting for bus\n"));
WaitForBus(disk, SENDCOMMAND, eventTime);
}
}
/* @SUBTITLE "EndCommand: end command xfer, start controller" */
/* Scheduled by SendCommand() to end a command
* and schedule a Controller() event.
*/
PRIVATE void
EndCommand(int disk, TICS eventTime)
{
struct diskstatus *ds = &(dstatus[disk]);
DEBUG_TRACE(("EndCommand\t%d\t@%s: nextDiskXfer = %lu\n", disk,
time_print(eventTime), ds->nextDiskXfer));
/* give the bus to the next event on the bus queue */
PassBus(disk, eventTime);
/* if command was sent by DiskDeviceStart, wake it up */
/* we can tell DiskDeviceStart sent command, if there is no buffer */
if (!ds->newHaveBuffer)
DDEventWakeup(disk,ds->wait_thread, eventTime);
/* start the controller */
Controller(disk, eventTime);
}
/* @SUBTITLE "Controller: examine new request and get disk going." */
/* Scheduled by EndCommand() to start a disk
* read or write operation and then go to sleep.
* Note that this function may be called twice in some requests; if the
* previous request is a write that was immediately reported, and this
* request does not extend it, we pause until that previous write completes
* to disk; then the Controller is "restarted" to consider the new request.
*/
PRIVATE void
Controller(int disk, TICS eventTime)
{
struct diskstatus *ds = &(dstatus[disk]);
ulong sector;
TICS endCtrllerTime;
boolean extendWrite = FALSE; /* can we extend the current write request? */
DEBUG_TRACE(("Controller\t%d\t@%s: nextDiskXfer = %lu currentBusXfer:%lu\n",
disk, time_print(eventTime),
ds->nextDiskXfer, ds->currentBusXfer));
INDEX_TIME_EVENT(EV_DISKSTATE, disk, DISKSTATE_CONTROL, eventTime);
if (ds->restartController == NEVER)
{
if (ds->newReading)
endCtrllerTime = eventTime + CTRLOVHDREAD_t;
else
endCtrllerTime = eventTime + CTRLOVHDWRITE_t;
}
else
endCtrllerTime = eventTime;
/* Check to see whether the current bus transfer is ahead of the
current disk transfer. If TRUE, previous operation was an
immediate reported write.
If the incoming operation is a read or a non-connecting write, we
simply return and let the second call to Controller() deal with
it. If the next operation is a sequential write, we extend the
write and continue. */
if (ds->currentDiskXfer < ds->currentBusXfer)
{
if (ds->newReading || (ds->newSector != ds->lastSector + 1))
{
ds->restartController = endCtrllerTime;
DEBUG_TRACE(("\trestart Controller %d after @%s and EndDiskXfer\n",
disk, time_print(endCtrllerTime)))
return;
}
else /* need to extend an immediate-reporting write */
{
ds->restartController = NEVER;
extendWrite = TRUE;
}
}
else /* no need to restart */
ds->restartController = NEVER;
sector = ds->firstSector = ds->newSector;
ds->lastSector = ds->newLastSector;
ds->reading = ds->newReading;
ds->haveBuffer = ds->newHaveBuffer;
if (ds->reading) /* read */
{
/* cache hit? */
if (sector >= ds->firstCachedSector && sector < ds->nextDiskXfer)
{ /* cache hit */
SUM_ARRAY_METRIC(stats.diskhit, disk, 1.0);
ds->wantBus = endCtrllerTime; /* want to start transferring */
if (!ds->activeRead) {
/* get prefetching started */
DDEventRequest(DISKMOVE, endCtrllerTime, disk);
ds->needMove = FALSE;
} else /* transfers continue in the background */
INDEX_TIME_EVENT(EV_DISKSTATE, disk,
DISKSTATE_XFER, endCtrllerTime);
/* flush the cache up to that point */
ds->firstCachedSector = sector;
ds->nextBusXfer = ds->currentBusXfer = sector;
/* cache hit on ready item, can start transferring */
if (sector < ds->currentDiskXfer)
DDEventRequest(CONSIDERBUSXFER, endCtrllerTime, disk);
}
else
{ /* cache miss */
SUM_ARRAY_METRIC(stats.diskmiss, disk, 1.0);
/* reset (flush) the cache */
ds->firstCachedSector = sector;
ds->nextDiskXfer = ds->currentDiskXfer = sector;
ds->nextBusXfer = ds->currentBusXfer = sector;
ds->wantBus = endCtrllerTime;
if (ds->activeRead)
{
if (ds->prefetchReq == NULL)
ds->cancelPrefetch = TRUE;
else
{
DDEventCancel(ds->prefetchReq);
ds->cancelPrefetch = FALSE;
ds->activeRead = FALSE;
ds->prefetchReq = NULL;
/* move to new request */
DDEventRequest(DISKMOVE, endCtrllerTime, disk);
ds->needMove = FALSE;
}
}
else
{
DDEventRequest(DISKMOVE, endCtrllerTime, disk);
ds->needMove = FALSE;
}
}
}
else /* write */
{
if (!extendWrite)
{
SUM_ARRAY_METRIC(stats.diskmiss, disk, 1.0);
/* discard all cache only when we are not extending a write */
ds->firstCachedSector = sector;
ds->nextDiskXfer = ds->currentDiskXfer = sector;
ds->nextBusXfer = ds->currentBusXfer = sector;
if (ds->activeRead)
{
if (ds->prefetchReq == NULL)
ds->cancelPrefetch = TRUE;
else
{
DDEventCancel(ds->prefetchReq);
ds->cancelPrefetch = FALSE;
ds->activeRead = FALSE;
ds->prefetchReq = NULL;
/* move to new request */
DDEventRequest(DISKMOVE, endCtrllerTime, disk);
ds->needMove = FALSE;
}
}
else
{
DDEventRequest(DISKMOVE, endCtrllerTime, disk);
ds->needMove = FALSE;
}
}
else {
/* the transfer continues in the background */
SUM_ARRAY_METRIC(stats.diskhit, disk, 1.0);
/* We want to update firstCachedSector to be the same
* as the new request's firstSector, but we can't until the
* current request finishes. To allow the bus xfer to go,
* we will allow firstCachedSector to track currentDiskXfer
* until it hits firstSector (see also EndDiskXfer).
*/
ds->firstCachedSector = (ulong)
min(ds->currentDiskXfer, ds->firstSector);
INDEX_TIME_EVENT(EV_DISKSTATE, disk,
DISKSTATE_XFER, endCtrllerTime);
}
ds->wantBus = endCtrllerTime;
DDEventRequest(CONSIDERBUSXFER, endCtrllerTime, disk);
}
}
/* @SUBTITLE "SendBufferMesg: Give a buffer to the Controller()" */
/* Give a buffer to the Controller() so that disk transfers can take place */
PRIVATE void
SendBufferMesg(int disk, TICS eventTime)
{
TICS grabTime = 0; /* time it took to grab the bus */
DEBUG_TRACE(("SendBufferMesg\t%d\t@%s: nextDiskXfer = %lu\n", disk,
time_print(eventTime), dstatus[disk].nextDiskXfer));
if (GrabBus(disk, &grabTime, eventTime)) {
DDEventRequest(ENDBUFFERMESG, (eventTime + grabTime + COMMANDTIME_t),
disk);
if (grabTime > 0) {
INDEX_TIME_EVENT(EV_BUSSTATE, dstatus[disk].busId, BUSSTATE_GRAB,
eventTime);
}
INDEX_TIME_EVENT(EV_BUSSTATE, dstatus[disk].busId, BUSSTATE_COMMAND,
eventTime + grabTime);
} else {
DEBUG_TRACE(("SendBufferMesg: Waiting for bus\n"));
WaitForBus(disk, SENDBUFFERMESG, eventTime);
}
}
/* @SUBTITLE "EndBufferMesg: Finish sending the buffer" */
/* Finish sending the buffer and consider a bus transfer. */
PRIVATE void
EndBufferMesg(int disk, TICS eventTime)
{
struct diskstatus *ds = &(dstatus[disk]);
DEBUG_TRACE(("EndBufferMesg\t%d\t@%s: nextDiskXfer = %lu\n", disk,
time_print(eventTime), ds->nextDiskXfer));
/* give the bus to the next event on the bus queue */
PassBus(disk, eventTime);
ds->haveBuffer = TRUE;
/* If the controller is not waiting for disk transfer to finish,
we can now consider a bus transfer */
if (ds->restartController == NEVER)
{
DEBUG_TRACE(("\t"));
ConsiderBusXfer(disk, eventTime);
}
}
/* @SUBTITLE "SendSyncCommand: ask disk to sync" */
/* Scheduled by DiskDriverSync() to synchronize the disk. */
PRIVATE void
SendSyncCommand(int disk, TICS eventTime)
{
TICS grabTime = 0; /* time it took to grab the bus */
DEBUG_TRACE(("SendSyncCommand\t%d\t@%s: nextDiskXfer = %lu\n", disk,
time_print(eventTime), dstatus[disk].nextDiskXfer));
if (GrabBus(disk, &grabTime, eventTime)) {
DDEventRequest(ENDSYNCCOMMAND,
(eventTime + grabTime + COMMANDTIME_t), disk);
if (grabTime > 0) {
INDEX_TIME_EVENT(EV_BUSSTATE, dstatus[disk].busId, BUSSTATE_GRAB,
eventTime);
}
INDEX_TIME_EVENT(EV_BUSSTATE, dstatus[disk].busId, BUSSTATE_COMMAND,
eventTime + grabTime);
} else {
DEBUG_TRACE(("SendSyncCommand: Waiting for bus\n"));
WaitForBus(disk, SENDSYNCCOMMAND, eventTime);
}
}
/* @SUBTITLE "EndSyncCommand: set flag to wakeup when disk done" */
/* Scheduled by SendSyncCommand() to synchronize
* the disk by setting a flag so EndDiskXfer can wake
* the DiskDriverSync up.
*/
PRIVATE void
EndSyncCommand(int disk, TICS eventTime)
{
struct diskstatus *ds = &(dstatus[disk]);
DEBUG_TRACE(("EndSyncCommand\t%d\t@%s: nextDiskXfer = %lu\n", disk,
time_print(eventTime), ds->nextDiskXfer));
/* give the bus to the next event on the bus queue */
PassBus(disk, eventTime);
/* only set sync flag when we have outstanding disk operations */
if (ds->currentDiskXfer < ds-> currentBusXfer)
{
ds->wantSync = TRUE;
DEBUG_TRACE(("\tdisk %d want last EndDiskXfer to SendDoneMessage\n",
disk));
}
else
{
ds->wantSync = FALSE;
/* done syncing coz we didn't have to sync in the first place */
DDEventRequest(SENDDONEMESSAGE, eventTime, disk);
}
}
/* @SUBTITLE "DiskMove: move disk head before disk xfer" */
/* Compute the time to move the disk head, then schedule StartDiskXfer at
* the move-completion time.
*/
PRIVATE void
DiskMove(int disk, TICS eventTime)
{
struct diskstatus *ds = &(dstatus[disk]);
TICS endMoveTime;
endMoveTime = MoveTime_t(disk, ds->nextDiskXfer, eventTime);
ds->activeRead = ds->reading; /* so we know a StartDiskXfer is coming */
DEBUG_TRACE(("DiskMove \t%d\t@%s: nextDiskXfer = %lu\n", disk,
time_print(eventTime), ds->nextDiskXfer));
DDEventRequest(STARTDISKXFER, endMoveTime, disk);
INDEX_TIME_EVENT(EV_DISKSTATE, disk, DISKSTATE_MOVE, eventTime);
SUM_ARRAY_METRIC(stats.diskmoves, disk, 1.0);
}
/* @SUBTITLE "StartDiskXfer: start one-sector disk xfer" */
/*
* Start a disk transfer event given the disk and the called time.
* The disk is at the beginning of nextSector at the given eventTime.
*
* !! StartDiskXfer() should only advance ds->nextDiskXfer. It should
* !! by no means change ds->currentDiskXfer.
*/
PRIVATE void
StartDiskXfer(int disk, TICS eventTime)
{
TICS abstime;
struct diskstatus *ds = &(dstatus[disk]);
DEBUG_TRACE(("StartDiskXfer \t%d\t@%s: nextDiskXfer = %lu\n", disk,
time_print(eventTime), ds->nextDiskXfer));
if (ds->done)
{
INDEX_TIME_EVENT(EV_DISKSTATE, disk, DISKSTATE_IDLE, eventTime);
return;
}
if (ds->cancelPrefetch)
{
ds->cancelPrefetch = FALSE;
ds->activeRead = FALSE;
DDEventRequest(DISKMOVE, eventTime, disk); /* move to new request */
}
else
{
if (ds->reading) /* reading */
{
/* try to enqueue the next sector onto the queue */
if (!CACHE_FULL(disk))
{
ds->nextDiskXfer++;
abstime = DSECTORXFER_t(eventTime);
ds->prefetchReq = DDEventRequest(ENDDISKXFER,
abstime, disk);
INDEX_TIME_EVENT(EV_DISKSTATE, disk,
DISKSTATE_XFER, eventTime);
}
else
{
/* cache is full */
ds->activeRead = FALSE;
ds->needMove = TRUE;
INDEX_TIME_EVENT(EV_DISKSTATE, disk,
DISKSTATE_IDLE, eventTime);
}
}
else /* writing */
{
/* there is nothing to be written onto the disk */
if (ds->nextDiskXfer == ds->currentBusXfer)
{
ds->needMove = TRUE; /* not yet */
INDEX_TIME_EVENT(EV_DISKSTATE, disk,
DISKSTATE_IDLE, eventTime);
}
else
{
ds->nextDiskXfer++;
abstime = DSECTORXFER_t(eventTime);
ds->prefetchReq = DDEventRequest(ENDDISKXFER,
abstime, disk);
INDEX_TIME_EVENT(EV_DISKSTATE, disk,
DISKSTATE_XFER, eventTime);
}
}
}
}
/* @SUBTITLE "EndDiskXfer: end of one sector; start another" */
/*
* Just finished transferring nextSector to/from queue. So, consider
* starting another sector xfer, and possibly a bus transfer.
*
* !! EndDiskXfer() should only advance ds->currentDiskXfer. It should
* !! by no means change ds->nextDiskXfer.
*/
PRIVATE void
EndDiskXfer(int disk, TICS eventTime)
{
struct diskstatus *ds = &(dstatus[disk]);
DEBUG_TRACE(("EndDiskXfer \t%d\t@%s: nextDiskXfer = %lu\n", disk,
time_print(eventTime), ds->nextDiskXfer));
ds->prefetchReq = NULL; /* EndDiskXfer has been pulled off the heap */
if (ds->done)
{
INDEX_TIME_EVENT(EV_DISKSTATE, disk, DISKSTATE_IDLE, eventTime);
return;
}
if (ds->cancelPrefetch)
{
ds->cancelPrefetch = FALSE;
ds->activeRead = FALSE;
DDEventRequest(DISKMOVE, eventTime, disk); /* move to new request */
}
else
{
if (ds->reading) /* reading */
{
ds->currentDiskXfer++;
if (!ds->activeBusXfer)
{
DEBUG_TRACE(("\t"));
ConsiderBusXfer(disk, eventTime);
}
/* start next sector */
DDEventRequest(STARTDISKXFER,
MoveNextTime_t(disk, ds->nextDiskXfer,
eventTime), disk);
}
else /* writing */
{
ds->currentDiskXfer++;
/* when writes get extended, sometimes fCS gets behind */
/* (see end of controller() for explanation) */
ds->firstCachedSector = (ulong)
min(ds->currentDiskXfer, ds->firstSector);
#ifndef SOLO
if ((ds->lastSector - ds->firstSector + 1) > CACHE_SLOTS) {
/* Size of transfer is larger than the cache
* To keep the transfer moving, we need to update
* the firstCachedSector field to reflect what
* has been transferred to the disk
*/
ds->firstCachedSector = (ulong)
max(ds->currentDiskXfer, ds->firstCachedSector);
}
#endif
if (!ds->activeBusXfer)
{
DEBUG_TRACE(("\t"));
ConsiderBusXfer(disk, eventTime);
}
/* start disk transfer again if we are not done */
if (ds->nextDiskXfer <= ds->lastSector)
DDEventRequest(STARTDISKXFER,
MoveNextTime_t(disk, ds->nextDiskXfer,
eventTime), disk);
else
{
INDEX_TIME_EVENT(EV_DISKSTATE, disk, DISKSTATE_IDLE,
eventTime);
#ifdef IMMEDIATE_REPORT
if (ds->restartController != NEVER)
DDEventRequest(CONTROLLER,
max(ds->restartController, eventTime),
disk);
else if (ds->wantSync)
DDEventRequest(SENDDONEMESSAGE, eventTime, disk);
#else
DDEventRequest(SENDDONEMESSAGE, eventTime, disk);
#endif /* IMMEDIATE_REPORT */
}
}
}
}
/* @SUBTITLE "ConsiderBusXfer: consider starting a bus xfer" */
PRIVATE void
ConsiderBusXfer(int disk, TICS eventTime)
{
struct diskstatus *ds = &(dstatus[disk]);
ulong readySectors; /* sectors that are ready */
boolean lastSectorReady = FALSE;
boolean start = FALSE;
TICS grabTime = 0; /* time it took to grab the bus */
DEBUG_TRACE(("ConsiderBusXfer\t%d\t@%s: nextBusXfer = %lu\n", disk,
time_print(eventTime), ds->nextBusXfer));
if (eventTime >= ds->wantBus && !ds->activeBusXfer && ds->haveBuffer)
{
if (ds->reading)
{
lastSectorReady = (ds->currentDiskXfer > ds->lastSector);
readySectors = (ds->currentDiskXfer - ds->nextBusXfer);
start = ((readySectors >= READFENCE) || lastSectorReady ||
CACHE_FULL(disk));
}
else
{
lastSectorReady = (ds->nextBusXfer > ds->lastSector);
readySectors = (ds->currentBusXfer - ds->nextDiskXfer);
start = ((readySectors <= WRITEFENCE) && !lastSectorReady &&
!CACHE_FULL(disk));
}
if (start)
{
ds->activeBusXfer = TRUE; /* somehow, we WILL start it */
if (GrabBus(disk, &grabTime, eventTime))
{
DDEventRequest(STARTBUSXFER, (eventTime + grabTime),
disk);
if (grabTime > 0)
INDEX_TIME_EVENT(EV_BUSSTATE, ds->busId, BUSSTATE_GRAB,
eventTime);
}
else
{
DEBUG_TRACE(("ConsiderBusXfer: waiting for bus\n"));
WaitForBus(disk, STARTBUSXFER, eventTime);
}
}
}
}
#ifndef SOLO
extern int SimhdGetDiskNum(int);
PRIVATE void
DmaDone(int encoded_ctrl_unit)
{
int diskNum = SimhdGetDiskNum(encoded_ctrl_unit);
AdvanceTime();
EndBusXfer(diskNum, GetTime());
}
#endif
/* @SUBTITLE "StartBusXfer: start one-sector bus xfer" */
/*
* Start a bus transfer at the given eventTime.
*
* !! StartBusXfer() should only advance ds->nextBusXfer. It should by
* !! no means change ds->currentBusXfer.
*/
PRIVATE void
StartBusXfer(int disk, TICS eventTime)
{
struct diskstatus *ds = &(dstatus[disk]);
TICS finish;
INVARIANT4(disk == *(ds->busOwner),
"StartBusXfer: I (disk %d) do not own the bus, disk %d does\n",
disk, *(ds->busOwner));
DEBUG_TRACE(("StartBusXfer \t%d\t@%s: nextBusXfer = %lu\n", disk,
time_print(eventTime), ds->nextBusXfer));
INDEX_TIME_EVENT(EV_BUSSTATE, ds->busId, BUSSTATE_DATA, eventTime);
finish = BSECTORXFER_t(eventTime); /* find the finish time of the xfer */
ds->nextBusXfer++;
#ifndef SOLO
if (simosInterface[disk].dmaRoutine) {
TICS current_time = CPUVec.CycleCount(0);
if (current_time > finish) {
/* This can only happen under Embra */
finish = current_time;
}
simosInterface[disk].dmaRoutine( SECTOR_SIZE, finish,
DmaDone, simosInterface[disk].doneArg);
} else {
DDEventRequest(ENDBUSXFER, finish, disk);
}
#else
DDEventRequest(ENDBUSXFER, finish, disk);
#endif
}
/* @SUBTITLE "EndBusXfer: end one-sector bus xfer, start another" */
/*
* End a bus transfer at the given eventTime.
*
* !! EndBusXfer() should only advance ds->currentBusXfer. It should by
* !! no means change ds->nextBusXfer.
*/
PRIVATE void
EndBusXfer(int disk, TICS eventTime)
{
struct diskstatus *ds = &(dstatus[disk]);
boolean finished;
DEBUG_TRACE(("EndBusXfer \t%d\t@%s: currentBusXfer = %lu\n", disk,
time_print(eventTime), ds->currentBusXfer));
ds->currentBusXfer++;
#ifndef SOLO
/* If we have transfered all of the active cache across the bus, we can
now flush it, to allow it to be refilled.
This allows us to do disk transfers that are larger than the cache size. */
if (ds->reading && (ds->currentBusXfer == ds->currentDiskXfer))
ds->firstCachedSector = ds->currentDiskXfer;
#endif
if ((ds->nextBusXfer <= ds->lastSector) &&
((ds->reading && ds->nextBusXfer < ds->currentDiskXfer)
|| (!ds->reading && !CACHE_FULL(disk))))
DDEventRequest(STARTBUSXFER, eventTime, disk);
else
{
finished = (ds->nextBusXfer > ds->lastSector);
ds->activeBusXfer = FALSE;
ds->wantBus = (finished ? NEVER : eventTime);
#ifdef IMMEDIATE_REPORT
if (finished)
DDEventRequest(SENDDONEMESSAGE, eventTime, disk);
#else
if (finished)
if (ds->reading)
DDEventRequest(SENDDONEMESSAGE, eventTime, disk);
else
/* give the bus to the next event on the bus queue */
PassBus(disk, eventTime);
#endif /* IMMEDIATE_REPORT */
}
if (ds->needMove)
{
ds->needMove = FALSE; /* reset the value */
/* move to appropriate location */
DDEventRequest(DISKMOVE, eventTime, disk);
}
}
/* @SUBTITLE "SendDoneMessage: send a 'done' message to host" */
PRIVATE void
SendDoneMessage(int disk, TICS eventTime)
{
TICS grabTime = 0; /* time it took to grab the bus */
DEBUG_TRACE(("SendDoneMessage\t%d\t@%s: nextDiskXfer = %lu\n", disk,
time_print(eventTime), dstatus[disk].nextDiskXfer));
if (GrabBus(disk, &grabTime, eventTime))
{
DDEventRequest(ENDDONEMESSAGE,
(eventTime + grabTime +DONETIME_t), disk);
if (grabTime > 0)
INDEX_TIME_EVENT(EV_BUSSTATE, dstatus[disk].busId, BUSSTATE_GRAB, eventTime);
INDEX_TIME_EVENT(EV_BUSSTATE, dstatus[disk].busId, BUSSTATE_COMMAND,
eventTime + grabTime);
}
else
{
DEBUG_TRACE(("SendDoneMessage: Waiting for bus\n"));
WaitForBus(disk, SENDDONEMESSAGE, eventTime);
}
}
/* @SUBTITLE "EndDoneMessage: 'done' message reaches host; wakeup thread" */
PRIVATE void
EndDoneMessage(int disk, TICS eventTime)
{
struct diskstatus *ds = &(dstatus[disk]);
DEBUG_TRACE(("EndDoneMessage \t%d\t@%s: nextDiskXfer = %lu\n", disk,
time_print(eventTime), ds->nextDiskXfer));
/* give the bus to the next event on the bus queue */
PassBus(disk, eventTime);
DDEventWakeup(disk,ds->wait_thread, eventTime);
/* reset the sync flag */
if (ds->wantSync)
ds->wantSync = FALSE;
}
/* == Utility Functions ================================================= */
/* @SUBTITLE "MoveTime_t: return the time when the move is completed" */
/* Calculate and return the time taken to move from one disk
* sector to another.
*
* Given the destination sector of the disk and the starting time,
* calculate the time when the disk head motion is finished.
*/
TICS
MoveTime_t(int disk, ulong lsector, TICS eventTime)
{
TICS abstime; /* amount of time needed */
ulong dcylinders, dtracks;
ulong oldtrack = dstatus[disk].track;
ulong sector = Logical2Physical(lsector);
ulong track = SECTOR2TRACK(sector);
/* calculate the differences in cylinders */
dcylinders = UlongAbsDiff(TRACK2CYLINDER(oldtrack),
TRACK2CYLINDER(track));
dtracks = UlongAbsDiff(oldtrack, track);
abstime = SeekTime_t(dcylinders, dtracks, eventTime);
/* ----------- seek time is calculated, next rotational delay ---------- */
/* include the rotation delay */
abstime = RotDelay_t(disk, sector, abstime);
dstatus[disk].track = (int)track; /* remember the new position */
return(abstime);
}
/* @SUBTITLE "MoveNextTime_t: time needed to move to next logical sector" */
/*
* Enters with: Disk number, Logical sector number to move the disk
* head to and Time the move begins.
* Returns: Time the move completes.
*/
PRIVATE TICS
MoveNextTime_t(int disk, ulong lsector, TICS eventTime)
{
struct diskstatus *ds = &(dstatus[disk]);
ulong trackswitch, headswitch;
ulong oldtrack = SECTOR2TRACK(Logical2Physical(lsector - 1));
ulong newtrack = SECTOR2TRACK(Logical2Physical(lsector));
trackswitch = UlongAbsDiff(TRACK2CYLINDER(oldtrack),
TRACK2CYLINDER(newtrack));
headswitch = UlongAbsDiff(oldtrack, newtrack);
/* more than one cylinder, eg when crossing a sparing region */
if (trackswitch > 1)
{
/* crossing a sparing region */
return(MoveTime_t(disk, lsector, eventTime));
}
else
{
ds->track = (int)newtrack;
if (trackswitch == 1) /* takes precedence over headswitch */
return(eventTime + TRACK_SWITCH_t);
/* only 1 headswitch within a cylinder counts */
else if (headswitch >= 1)
return(eventTime + HEAD_SWITCH_t);
else
return(eventTime); /* no switch occurred */
}
}
/* @SUBTITLE "RotDelay_t: time to rotate to the desired sector" */
/*
* Enters with: Number of the disk of significance and the seektime.
* Parameter requested is the physical sector number.
* Returns: The time the sector requested is reached.
*/
PRIVATE TICS
RotDelay_t(int disk, ulong sector, TICS endSeekTime)
{
double initRotPos = 0; /* initial position is 0 at time 0 */
double dtime;
double endSeekRotPos; /* rotational position after the seek */
double destPosition, dPosition;
ulong tracksector;
endSeekRotPos = initRotPos + (double)(endSeekTime - 0) / ROT_TIME_t;
/* find the position in fractions */
endSeekRotPos = endSeekRotPos - (unsigned long) endSeekRotPos;
tracksector = sector % NSECTORS;
destPosition = ((double) (tracksector +
SECTOR2CYLINDER(sector) * CYLINDERSKEW +
(SECTOR2TRACK(sector) - SECTOR2CYLINDER(sector))
* TRACKSKEW) / NSECTORS);
dPosition = 1 + destPosition - endSeekRotPos;
dPosition = dPosition - (unsigned long) dPosition;
dtime = dPosition * ROT_TIME_t;
return ((TICS)dtime + endSeekTime);
}
/* @SUBTITLE "SanityCheck: check parameters for errors" */
/*
* This function checks whether the parameters given to the function
* is valid. If they are not, the program will be aborted and an error
* message printed.
*/
PRIVATE void
SanityCheck(int disk, ulong lsector, ulong nsectors)
{
INVARIANT3((disk >= 0 && disk < NO_OF_DISKS),
"SanityCheck %d: invalid disk number\n",
disk);
INVARIANT3(dstatus[disk].startTime == NEVER,
"SanityCheck %d: previous DiskDeviceStart not finished!\n",
disk);
INVARIANT3((lsector < TOTAL_NSECTORS),
"SanityCheck %d: starting sector not in disk\n",
disk);
INVARIANT3((nsectors > 0),
"SanityCheck %d: transfer size too small\n",
disk);
INVARIANT3(((lsector + nsectors - 1) < TOTAL_NSECTORS),
"SanityCheck %d: transfer past end of disk\n",
disk);
INVARIANT3((dstatus[disk].wantBus == NEVER),
"%d: previous request not finished\n", disk);
INVARIANT3((!dstatus[disk].activeBusXfer), "%d: bus is still busy\n", disk);
}
/* @SUBTITLE "SeekTime_t: compute the seek-time component of disk move" */
/*
* This function returns the time when a seek from one sector to the
* other will complete.
* The number returned is in TICS.
*/
PRIVATE TICS
SeekTime_t(ulong dcylinders, ulong dtracks, TICS eventTime)
{
double dtime;
/* as given in pg 14 (3.4.2) of Modelling Disks HPL-93-68 revision 1
if (dcylinders < 616)
dtime = (3.45 + 0.597 * sqrt((double)dcylinders));
else
dtime = (10.8 + 0.012 * dcylinders);
*/
/* as given in pg 17, facts for HP 97560 */
if (dcylinders == 0)
{
if (dtracks != 0)
/* time for head switch instead of seek (convert
HEAD_SWITCH_t back from TICS to MSEC) */
dtime = (double) HEAD_SWITCH_t / TICSperMSEC; /* just 1 switch */
else
dtime = 0;
}
else if (dcylinders < 383)
dtime = (3.24 + 0.400 * sqrt((double)dcylinders));
else
dtime = (8.00 + 0.008 * dcylinders);
/* convert dtime from msec to TICS */
return ((TICS) (dtime * TICSperMSEC) + eventTime);
}
/* @SUBTITLE "Logical2Physical: convert logical sector number to physical" */
/* Convert a logical sector number to a physical sector number. */
PRIVATE ulong
Logical2Physical(ulong lsector)
{
ulong region, psector;
for (region = 0; region < NUM_REGIONS; region++)
if (lsector <= Regions[region].logicalend)
break; /* find the region in which the lsector
is situated */
/* calculate the corresponding physical sector number */
psector = Regions[region].physicalstart +
(lsector - Regions[region].logicalstart);
return (psector);
}
/* @SUBTITLE "GrabBus: try to grab the bus for ourselves" */
/* Try to grab control of the bus. */
PRIVATE boolean /* TRUE iff we can use the bus now */
GrabBus(int disk, TICS *grabTime, TICS eventTime)
{
struct diskstatus *ds = &(dstatus[disk]);
int *busOwner = ds->busOwner;
if (*busOwner == BUS_FREE) { /* bus is available */
*busOwner = disk; /* grab it */
*grabTime = BUSGRABTIME_t;
INDEX_TIME_EVENT(EV_BUSCONTEND, ds->busId, 1, eventTime);
return(TRUE);
} else if (*busOwner == disk) { /* we already have the bus */
*grabTime = 0;
return(TRUE); /* ok */
} else {
return (FALSE); /* bus not available */
}
}
/* @SUBTITLE "WaitForBus: wait for our bus to be free" */
/*
* Wait for the given bus, which is busy.
*/
PRIVATE void
WaitForBus(int disk, REQUESTCODE event, TICS eventTime)
{
struct diskstatus *ds = &(dstatus[disk]);
NCQUEUE *busq = ds->busq;
busItem *itemptr = (busItem *) malloc(sizeof(busItem));
INVARIANT2((itemptr != NULL), "WaitForBus: malloc failed\n");
itemptr->disk = disk; /* record who we are and what we want */
itemptr->event = event;
Enqueue_noncyc(busq, itemptr); /* put us into the queue waiting for bus */
INDEX_TIME_EVENT(EV_BUSCONTEND, ds->busId,
InQueue_noncyc(busq)+1, eventTime);
}
/* @SUBTITLE "PassBus: pass our bus to a waiter, if any" */
/*
* Pass the given bus to another disk waiting to use that bus.
* 'disk' should be the disk currently holding the bus.
*/
PRIVATE void
PassBus(int disk, TICS eventTime)
{
struct diskstatus *ds = &(dstatus[disk]);
TICS requestTime;
/* is there anyone waiting for this bus? */
if (!EmptyQueue_noncyc(ds->busq)) {
busItem *itemptr = 0;
/* pull off the first disk waiting for this bus */
Dequeue_noncyc(ds->busq, (void **)&itemptr);
DEBUG_TRACE(("\tPass bus from %d to %d @%s\n",
disk, itemptr->disk, time_print(eventTime)));
INVARIANT4(dstatus[itemptr->disk].busOwner == ds->busOwner,
"PassBus: disks %lld and %lld don't share a bus!\n",
(int64)itemptr->disk, (int64)(ds - dstatus));
INVARIANT3(*(ds->busOwner) == disk,
"PassBus: disk %d doesn't own its bus!\n", disk);
/* special-case when CONSIDERBUSXFER put STARTBUSXFER on queue */
if (itemptr->event == STARTBUSXFER) {
INDEX_TIME_EVENT(EV_BUSSTATE, ds->busId, BUSSTATE_GRAB, eventTime);
/* it actually starts a little later */
requestTime = eventTime + BUSGRABTIME_t;
} else
requestTime = eventTime;
/* Give the bus to them right now */
*(ds->busOwner) = itemptr->disk;
/* and schedule the event they wanted */
DDEventRequest(itemptr->event, requestTime, itemptr->disk);
INDEX_TIME_EVENT(EV_BUSCONTEND, ds->busId,
InQueue_noncyc(ds->busq)+1, eventTime);
free(itemptr);
} else {
*(ds->busOwner) = BUS_FREE;
INDEX_TIME_EVENT(EV_BUSSTATE, ds->busId, BUSSTATE_IDLE, eventTime);
INDEX_TIME_EVENT(EV_BUSCONTEND, ds->busId, 0, eventTime);
}
}