Main Page   Compound List   File List   Compound Members   File Members  

fstest.cc

Go to the documentation of this file.
00001 // fstest.cc 
00002 //      Simple test routines for the file system.  
00003 //
00004 //      We implement:
00005 //         Copy -- copy a file from UNIX to Nachos
00006 //         Print -- cat the contents of a Nachos file 
00007 //         Perftest -- a stress test for the Nachos file system
00008 //              read and write a really large file in tiny chunks
00009 //              (won't work on baseline system!)
00010 //
00011 // Copyright (c) 1992-1993 The Regents of the University of California.
00012 // All rights reserved.  See copyright.h for copyright notice and limitation 
00013 // of liability and disclaimer of warranty provisions.
00014 
00015 #include "copyright.h"
00016 
00017 #include "utility.h"
00018 #include "filesys.h"
00019 #include "system.h"
00020 #include "thread.h"
00021 #include "disk.h"
00022 #include "stats.h"
00023 
00024 #define TransferSize    10      // make it small, just to be difficult
00025 
00026 //----------------------------------------------------------------------
00027 // Copy
00028 //      Copy the contents of the UNIX file "from" to the Nachos file "to"
00029 //----------------------------------------------------------------------
00030 
00031 void
00032 Copy(char *from, char *to)
00033 {
00034     FILE *fp;
00035     OpenFile* openFile;
00036     int amountRead, fileLength;
00037     char *buffer;
00038 
00039 // Open UNIX file
00040     if ((fp = fopen(from, "r")) == NULL) {       
00041         printf("Copy: couldn't open input file %s\n", from);
00042         return;
00043     }
00044 
00045 // Figure out length of UNIX file
00046     fseek(fp, 0, 2);            
00047     fileLength = ftell(fp);
00048     fseek(fp, 0, 0);
00049 
00050 // Create a Nachos file of the same length
00051     DEBUG('f', "Copying file %s, size %d, to file %s\n", from, fileLength, to);
00052     if (!fileSystem->Create(to, fileLength)) {   // Create Nachos file
00053         printf("Copy: couldn't create output file %s\n", to);
00054         fclose(fp);
00055         return;
00056     }
00057     
00058     openFile = fileSystem->Open(to);
00059     ASSERT(openFile != NULL);
00060     
00061 // Copy the data in TransferSize chunks
00062     buffer = new char[TransferSize];
00063     while ((amountRead = fread(buffer, sizeof(char), TransferSize, fp)) > 0)
00064         openFile->Write(buffer, amountRead);    
00065     delete [] buffer;
00066 
00067 // Close the UNIX and the Nachos files
00068     delete openFile;
00069     fclose(fp);
00070 }
00071 
00072 //----------------------------------------------------------------------
00073 // Print
00074 //      Print the contents of the Nachos file "name".
00075 //----------------------------------------------------------------------
00076 
00077 void
00078 Print(char *name)
00079 {
00080     OpenFile *openFile;    
00081     int i, amountRead;
00082     char *buffer;
00083 
00084     if ((openFile = fileSystem->Open(name)) == NULL) {
00085         printf("Print: unable to open file %s\n", name);
00086         return;
00087     }
00088     
00089     buffer = new char[TransferSize];
00090     while ((amountRead = openFile->Read(buffer, TransferSize)) > 0)
00091         for (i = 0; i < amountRead; i++)
00092             printf("%c", buffer[i]);
00093     delete [] buffer;
00094 
00095     delete openFile;            // close the Nachos file
00096     return;
00097 }
00098 
00099 //----------------------------------------------------------------------
00100 // PerformanceTest
00101 //      Stress the Nachos file system by creating a large file, writing
00102 //      it out a bit at a time, reading it back a bit at a time, and then
00103 //      deleting the file.
00104 //
00105 //      Implemented as three separate routines:
00106 //        FileWrite -- write the file
00107 //        FileRead -- read the file
00108 //        PerformanceTest -- overall control, and print out performance #'s
00109 //----------------------------------------------------------------------
00110 
00111 #define FileName        "TestFile"
00112 #define Contents        "1234567890"
00113 #define ContentSize     strlen(Contents)
00114 #define FileSize        ((int)(ContentSize * 5000))
00115 
00116 static void 
00117 FileWrite()
00118 {
00119     OpenFile *openFile;    
00120     int i, numBytes;
00121 
00122     printf("Sequential write of %d byte file, in %d byte chunks\n", 
00123         FileSize, (int) ContentSize);
00124     if (!fileSystem->Create(FileName, 0)) {
00125       printf("Perf test: can't create %s\n", FileName);
00126       return;
00127     }
00128     openFile = fileSystem->Open(FileName);
00129     if (openFile == NULL) {
00130         printf("Perf test: unable to open %s\n", FileName);
00131         return;
00132     }
00133     for (i = 0; i < FileSize; i += ContentSize) {
00134         numBytes = openFile->Write(Contents, ContentSize);
00135         if (numBytes < 10) {
00136             printf("Perf test: unable to write %s\n", FileName);
00137             delete openFile;
00138             return;
00139         }
00140     }
00141     delete openFile;    // close file
00142 }
00143 
00144 static void 
00145 FileRead()
00146 {
00147     OpenFile *openFile;    
00148     char *buffer = new char[ContentSize];
00149     int i, numBytes;
00150 
00151     printf("Sequential read of %d byte file, in %d byte chunks\n", 
00152         FileSize, (int) ContentSize);
00153 
00154     if ((openFile = fileSystem->Open(FileName)) == NULL) {
00155         printf("Perf test: unable to open file %s\n", FileName);
00156         delete [] buffer;
00157         return;
00158     }
00159     for (i = 0; i < FileSize; i += ContentSize) {
00160         numBytes = openFile->Read(buffer, ContentSize);
00161         if ((numBytes < 10) || strncmp(buffer, Contents, ContentSize)) {
00162             printf("Perf test: unable to read %s\n", FileName);
00163             delete openFile;
00164             delete [] buffer;
00165             return;
00166         }
00167     }
00168     delete [] buffer;
00169     delete openFile;    // close file
00170 }
00171 
00172 void
00173 PerformanceTest()
00174 {
00175     printf("Starting file system performance test:\n");
00176     stats->Print();
00177     FileWrite();
00178     FileRead();
00179     if (!fileSystem->Remove(FileName)) {
00180       printf("Perf test: unable to remove %s\n", FileName);
00181       return;
00182     }
00183     stats->Print();
00184 }
00185 

Generated on Mon Feb 10 09:54:45 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