Main Page   Compound List   File List   Compound Members   File Members  

Disk Class Reference

#include <disk.h>

List of all members.

Public Methods

 Disk (char *name, VoidFunctionPtr callWhenDone, int callArg)
 ~Disk ()
void ReadRequest (int sectorNumber, char *data)
void WriteRequest (int sectorNumber, char *data)
void HandleInterrupt ()
int ComputeLatency (int newSector, bool writing)

Private Methods

int TimeToSeek (int newSector, int *rotate)
int ModuloDiff (int to, int from)
void UpdateLast (int newSector)

Private Attributes

int fileno
VoidFunctionPtr handler
int handlerArg
bool active
int lastSector
int bufferInit


Constructor & Destructor Documentation

Disk::Disk char *    name,
VoidFunctionPtr    callWhenDone,
int    callArg
 

Definition at line 43 of file disk.cc.

References active, ASSERT, bufferInit, DEBUG, DiskSize, FALSE, fileno, handler, handlerArg, lastSector, Lseek, MagicNumber, MagicSize, OpenForReadWrite, OpenForWrite, Read, VoidFunctionPtr, and WriteFile.

00044 {
00045     int magicNum;
00046     int tmp = 0;
00047 
00048     DEBUG('d', "Initializing the disk, 0x%x 0x%x\n", callWhenDone, callArg);
00049     handler = callWhenDone;
00050     handlerArg = callArg;
00051     lastSector = 0;
00052     bufferInit = 0;
00053     
00054     fileno = OpenForReadWrite(name, FALSE);
00055     if (fileno >= 0) {                  // file exists, check magic number 
00056         Read(fileno, (char *) &magicNum, MagicSize);
00057         ASSERT(magicNum == MagicNumber);
00058     } else {                            // file doesn't exist, create it
00059         fileno = OpenForWrite(name);
00060         magicNum = MagicNumber;  
00061         WriteFile(fileno, (char *) &magicNum, MagicSize); // write magic number
00062 
00063         // need to write at end of file, so that reads will not return EOF
00064         Lseek(fileno, DiskSize - sizeof(int), 0);       
00065         WriteFile(fileno, (char *)&tmp, sizeof(int));  
00066     }
00067     active = FALSE;
00068 }

Disk::~Disk  
 

Definition at line 76 of file disk.cc.

References Close, and fileno.

00077 {
00078     Close(fileno);
00079 }


Member Function Documentation

int Disk::ComputeLatency int    newSector,
bool    writing
 

Definition at line 233 of file disk.cc.

References bufferInit, DEBUG, FALSE, ModuloDiff, RotationTime, and TimeToSeek.

Referenced by ReadRequest, and WriteRequest.

00234 {
00235     int rotation;
00236     int seek = TimeToSeek(newSector, &rotation);
00237     int timeAfter = stats->totalTicks + seek + rotation;
00238 
00239 #ifndef NOTRACKBUF      // turn this on if you don't want the track buffer stuff
00240     // check if track buffer applies
00241     if ((writing == FALSE) && (seek == 0) 
00242                 && (((timeAfter - bufferInit) / RotationTime) 
00243                         > ModuloDiff(newSector, bufferInit / RotationTime))) {
00244         DEBUG('d', "Request latency = %d\n", RotationTime);
00245         return RotationTime; // time to transfer sector from the track buffer
00246     }
00247 #endif
00248 
00249     rotation += ModuloDiff(newSector, timeAfter / RotationTime) * RotationTime;
00250 
00251     DEBUG('d', "Request latency = %d\n", seek + rotation + RotationTime);
00252     return(seek + rotation + RotationTime);
00253 }

void Disk::HandleInterrupt  
 

Definition at line 162 of file disk.cc.

References active, and FALSE.

00163 { 
00164     active = FALSE;
00165     (*handler)(handlerArg);
00166 }

int Disk::ModuloDiff int    to,
int    from
[private]
 

Definition at line 203 of file disk.cc.

References SectorsPerTrack.

Referenced by ComputeLatency.

00204 {
00205     int toOffset = to % SectorsPerTrack;
00206     int fromOffset = from % SectorsPerTrack;
00207 
00208     return ((toOffset - fromOffset) + SectorsPerTrack) % SectorsPerTrack;
00209 }

void Disk::ReadRequest int    sectorNumber,
char *    data
 

Definition at line 116 of file disk.cc.

References active, ASSERT, ComputeLatency, DEBUG, DebugIsEnabled, DiskDone, DiskInt, FALSE, fileno, Lseek, MagicSize, NumSectors, PrintSector, Read, SectorSize, TRUE, and UpdateLast.

Referenced by SynchDisk::ReadSector.

00117 {
00118     int ticks = ComputeLatency(sectorNumber, FALSE);
00119 
00120     ASSERT(!active);                            // only one request at a time
00121     ASSERT((sectorNumber >= 0) && (sectorNumber < NumSectors));
00122     
00123     DEBUG('d', "Reading from sector %d\n", sectorNumber);
00124     Lseek(fileno, SectorSize * sectorNumber + MagicSize, 0);
00125     Read(fileno, data, SectorSize);
00126     if (DebugIsEnabled('d'))
00127         PrintSector(FALSE, sectorNumber, data);
00128     
00129     active = TRUE;
00130     UpdateLast(sectorNumber);
00131     stats->numDiskReads++;
00132     interrupt->Schedule(DiskDone, (int) this, ticks, DiskInt);
00133 }

int Disk::TimeToSeek int    newSector,
int *    rotate
[private]
 

Definition at line 180 of file disk.cc.

References abs, lastSector, RotationTime, SectorsPerTrack, and SeekTime.

Referenced by ComputeLatency, and UpdateLast.

00181 {
00182     int newTrack = newSector / SectorsPerTrack;
00183     int oldTrack = lastSector / SectorsPerTrack;
00184     int seek = abs(newTrack - oldTrack) * SeekTime;
00185                                 // how long will seek take?
00186     int over = (stats->totalTicks + seek) % RotationTime; 
00187                                 // will we be in the middle of a sector when
00188                                 // we finish the seek?
00189 
00190     *rotation = 0;
00191     if (over > 0)               // if so, need to round up to next full sector
00192         *rotation = RotationTime - over;
00193     return seek;
00194 }

void Disk::UpdateLast int    newSector [private]
 

Definition at line 262 of file disk.cc.

References bufferInit, DEBUG, lastSector, and TimeToSeek.

Referenced by ReadRequest, and WriteRequest.

00263 {
00264     int rotate;
00265     int seek = TimeToSeek(newSector, &rotate);
00266     
00267     if (seek != 0)
00268         bufferInit = stats->totalTicks + seek + rotate;
00269     lastSector = newSector;
00270     DEBUG('d', "Updating last sector = %d, %d\n", lastSector, bufferInit);
00271 }

void Disk::WriteRequest int    sectorNumber,
char *    data
 

Definition at line 136 of file disk.cc.

References active, ASSERT, ComputeLatency, DEBUG, DebugIsEnabled, DiskDone, DiskInt, fileno, Lseek, MagicSize, NumSectors, PrintSector, SectorSize, TRUE, UpdateLast, and WriteFile.

Referenced by SynchDisk::WriteSector.

00137 {
00138     int ticks = ComputeLatency(sectorNumber, TRUE);
00139 
00140     ASSERT(!active);
00141     ASSERT((sectorNumber >= 0) && (sectorNumber < NumSectors));
00142     
00143     DEBUG('d', "Writing to sector %d\n", sectorNumber);
00144     Lseek(fileno, SectorSize * sectorNumber + MagicSize, 0);
00145     WriteFile(fileno, data, SectorSize);
00146     if (DebugIsEnabled('d'))
00147         PrintSector(TRUE, sectorNumber, data);
00148     
00149     active = TRUE;
00150     UpdateLast(sectorNumber);
00151     stats->numDiskWrites++;
00152     interrupt->Schedule(DiskDone, (int) this, ticks, DiskInt);
00153 }


Member Data Documentation

bool Disk::active [private]
 

Definition at line 83 of file disk.h.

Referenced by Disk, HandleInterrupt, ReadRequest, and WriteRequest.

int Disk::bufferInit [private]
 

Definition at line 85 of file disk.h.

Referenced by ComputeLatency, Disk, and UpdateLast.

int Disk::fileno [private]
 

Definition at line 79 of file disk.h.

Referenced by Disk, ReadRequest, WriteRequest, and ~Disk.

VoidFunctionPtr Disk::handler [private]
 

Definition at line 80 of file disk.h.

Referenced by Disk.

int Disk::handlerArg [private]
 

Definition at line 82 of file disk.h.

Referenced by Disk.

int Disk::lastSector [private]
 

Definition at line 84 of file disk.h.

Referenced by Disk, TimeToSeek, and UpdateLast.


The documentation for this class was generated from the following files:
Generated on Mon Feb 10 09:54:54 2003 for nachos by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees