nfs-ganesha 1.4

test5.c

Go to the documentation of this file.
00001 /*
00002  *      @(#)test5.c     1.8     2003/12/01 Connectathon Testsuite
00003  *      1.5 Lachman ONC Test Suite source
00004  *
00005  * Test read and write
00006  *
00007  * Uses the following important system calls against the server:
00008  *
00009  *      chdir()
00010  *      mkdir()         (for initial directory creation if not -m)
00011  *      creat()
00012  *      open()
00013  *      read()
00014  *      write()
00015  *      stat()
00016  *      fstat()
00017  *      unlink()
00018  */
00019 
00020 #if defined (DOS) || defined (WIN32)
00021 /* If Dos, Windows or Win32 */
00022 #define DOSorWIN32
00023 /* Synchronous Write is Not supported in the Windows version yet.*/
00024 #undef O_SYNC
00025 #endif
00026 
00027 #ifndef DOSorWIN32
00028 #include <sys/param.h>
00029 #include <unistd.h>
00030 #endif
00031 
00032 #include <fcntl.h>
00033 #include <sys/stat.h>
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <sys/types.h>
00037 #ifdef DOSorWIN32
00038 #include <time.h>
00039 #else
00040 #include <sys/time.h>
00041 #endif
00042 #ifdef MMAP
00043 #include <sys/mman.h>
00044 #endif
00045 
00046 #include "tests.h"
00047 #include "Connectathon_config_parsing.h"
00048 
00049 #if defined(O_SYNC) || defined(DOSorWIN32)
00050 #define USE_OPEN
00051 #endif
00052 
00053 #ifndef MIN
00054 #define MIN(a, b)       ((a) < (b) ? (a) : (b))
00055 #endif
00056 
00057 #define BUFSZ   8192
00058 
00059 static int Tflag = 0;           /* print timing */
00060 static int Fflag = 0;           /* test function only;  set count to 1, negate -t */
00061 static int Nflag = 0;           /* Suppress directory operations */
00062 #ifdef O_SYNC
00063 static int Sflag = 0;           /* use synchronous writes */
00064 #endif
00065 
00066 static void usage()
00067 {
00068   fprintf(stdout, "usage: %s [-htfns] <config_file>\n", Myname);
00069   fprintf(stdout, "  Flags:  h    Help - print this usage info\n");
00070   fprintf(stdout, "          t    Print execution time statistics\n");
00071   fprintf(stdout, "          f    Test function only (negate -t)\n");
00072   fprintf(stdout, "          n    Suppress test directory create operations\n");
00073 #ifdef O_SYNC
00074   fprintf(stdout, "          s    Use synchronous writes\n");
00075 #endif
00076 }
00077 
00078 int main(int argc, char *argv[])
00079 {
00080   int count;                    /* times to do each file */
00081   int ct;
00082   off_t size;
00083   off_t si;
00084   int i;
00085   int fd;
00086   off_t bytes = 0;
00087   int roflags;                  /* open read-only flags */
00088   int woflags;                  /* write-only create flags */
00089   char *bigfile;
00090   struct timeval time;
00091   struct stat statb;
00092   char *opts;
00093   char buf[BUFSZ];
00094   double etime1, etime2;
00095 #ifdef MMAP
00096   caddr_t maddr;
00097 #endif
00098   struct testparam *param;
00099   struct btest *b;
00100   char *config_file;
00101   char *test_dir;
00102   char *log_file;
00103   FILE *log;
00104   long g, d;
00105 
00106   umask(0);
00107   setbuf(stdout, NULL);
00108   Myname = *argv++;
00109   argc--;
00110   while(argc && **argv == '-')
00111     {
00112       for(opts = &argv[0][1]; *opts; opts++)
00113         {
00114           switch (*opts)
00115             {
00116             case 'h':          /* help */
00117               usage();
00118               exit(1);
00119               break;
00120 
00121             case 't':          /* time */
00122               Tflag++;
00123               break;
00124 
00125             case 'f':          /* funtionality */
00126               Fflag++;
00127               break;
00128 
00129             case 'n':          /* No Test Directory create */
00130               Nflag++;
00131               break;
00132 
00133 #ifdef O_SYNC
00134             case 's':          /* synchronous writes */
00135               Sflag++;
00136               break;
00137 #endif
00138 
00139             default:
00140               error("unknown option '%c'", *opts);
00141               usage();
00142               exit(1);
00143             }
00144         }
00145       argc--;
00146       argv++;
00147     }
00148 
00149   if(argc)
00150     {
00151       config_file = *argv;
00152       argc--;
00153       argv++;
00154     }
00155   else
00156     {
00157       fprintf(stderr, "Missing config_file");
00158       exit(1);
00159     }
00160 
00161   if(argc != 0)
00162     {
00163       fprintf(stderr, "too many parameters");
00164       usage();
00165       exit(1);
00166     }
00167 
00168   param = readin_config(config_file);
00169   if(param == NULL)
00170     {
00171       fprintf(stderr, "Nothing built\n");
00172       exit(1);
00173     }
00174 
00175   b = get_btest_args(param, FIVE);
00176   if(b == NULL)
00177     {
00178       fprintf(stderr, "Missing basic test number 5 in the config file '%s'\n",
00179               config_file);
00180       free_testparam(param);
00181       exit(1);
00182     }
00183 
00184   if(b->count == -1)
00185     {
00186       fprintf(stderr,
00187               "Missing 'count' parameter in the config file '%s' for the basic test number 5\n",
00188               config_file);
00189       free_testparam(param);
00190       exit(1);
00191     }
00192   if(b->size == -1)
00193     {
00194       fprintf(stderr,
00195               "Missing 'size' parameter in the config file '%s' for the basic test number 5\n",
00196               config_file);
00197       free_testparam(param);
00198       exit(1);
00199     }
00200   count = b->count;
00201   size = b->size;
00202   bigfile = b->bigfile;
00203   test_dir = get_test_directory(param);
00204   log_file = get_log_file(param);
00205 
00206   free_testparam(param);
00207 
00208   if(count == 0)
00209     {
00210       fprintf(stderr, "count == 0 !\n");
00211       exit(1);
00212     }
00213 
00214   if(!Fflag)
00215     {
00216       Tflag = 0;
00217       count = 1;
00218     }
00219 
00220   woflags = O_WRONLY | O_CREAT | O_TRUNC;
00221   roflags = O_RDONLY;
00222 #ifdef O_SYNC
00223   if(Sflag)
00224     {
00225       woflags |= O_SYNC;
00226     }
00227 #endif
00228 #ifdef DOSorWIN32
00229   woflags |= O_BINARY | O_RDWR; /* create and open file */
00230   roflags |= O_BINARY;
00231 #endif
00232 
00233   fprintf(stdout, "%s: read and write\n", Myname);
00234 
00235   if(!Nflag)
00236     testdir(test_dir);
00237   else
00238     mtestdir(test_dir);
00239 
00240   for(i = 0; i < BUFSZ / sizeof(int); i++)
00241     {
00242       ((int *)buf)[i] = i;
00243     }
00244 
00245   starttime();
00246   for(ct = 0; ct < count; ct++)
00247     {
00248 #ifdef USE_OPEN
00249       if((fd = open(bigfile, woflags, CHMOD_RW)) < 0)
00250         {
00251 #else
00252       if((fd = creat(bigfile, CHMOD_RW)) < 0)
00253         {
00254 #endif
00255           error("can't create '%s'", bigfile);
00256           exit(1);
00257         }
00258       if(stat(bigfile, &statb) < 0)
00259         {
00260           error("can't stat '%s'", bigfile);
00261           exit(1);
00262         }
00263       if(statb.st_size != 0)
00264         {
00265           error("'%s' has size %ld, should be 0", bigfile, (long)statb.st_size);
00266           exit(1);
00267         }
00268       for(si = size; si > 0; si -= bytes)
00269         {
00270           bytes = MIN(BUFSZ, si);
00271           if(write(fd, buf, bytes) != bytes)
00272             {
00273               error("'%s' write failed", bigfile);
00274               exit(1);
00275             }
00276         }
00277       if(close(fd) < 0)
00278         {
00279           error("can't close %s", bigfile);
00280           exit(1);
00281         }
00282       if(stat(bigfile, &statb) < 0)
00283         {
00284           error("can't stat '%s'", bigfile);
00285           exit(1);
00286         }
00287       if(statb.st_size != size)
00288         {
00289           error("'%s' has size %ld, should be %ld",
00290                 bigfile, (long)(statb.st_size), (long)size);
00291           exit(1);
00292         }
00293     }
00294   endtime(&time);
00295 
00296   if((fd = open(bigfile, roflags)) < 0)
00297     {
00298       error("can't open '%s'", bigfile);
00299       exit(1);
00300     }
00301 #ifdef MMAP
00302   maddr = mmap((caddr_t) 0, (size_t) size, PROT_READ, MAP_PRIVATE, fd, (off_t) 0);
00303   if(maddr == MAP_FAILED)
00304     {
00305       error("can't mmap '%s'", bigfile);
00306       exit(1);
00307     }
00308   if(msync(maddr, (size_t) size, MS_INVALIDATE) < 0)
00309     {
00310       error("can't invalidate pages for '%s'", bigfile);
00311       exit(1);
00312     }
00313   if(munmap(maddr, (size_t) size) < 0)
00314     {
00315       error("can't munmap '%s'", bigfile);
00316       exit(1);
00317     }
00318 #endif
00319   for(si = size; si > 0; si -= bytes)
00320     {
00321       bytes = MIN(BUFSZ, si);
00322       if(read(fd, buf, bytes) != bytes)
00323         {
00324           error("'%s' read failed", bigfile);
00325           exit(1);
00326         }
00327       for(i = 0; i < bytes / sizeof(int); i++)
00328         {
00329           if(((int *)buf)[i] != i)
00330             {
00331               error("bad data in '%s'", bigfile);
00332               exit(1);
00333             }
00334         }
00335     }
00336   close(fd);
00337 
00338   fprintf(stdout, "\twrote %ld byte file %d times", (long)size, count);
00339 
00340   if(Tflag)
00341     {
00342       etime1 = (double)time.tv_sec + (double)time.tv_usec / 1000000.0;
00343       if(etime1 != 0.0)
00344         {
00345           fprintf(stdout, " in %ld.%02ld seconds (%d bytes/sec)",
00346                   (long)time.tv_sec, (long)time.tv_usec / 10000,
00347                   (int)((double)size * ((double)count / etime1)));
00348         }
00349       else
00350         {
00351           fprintf(stdout, " in %ld.%02ld seconds (> %ld bytes/sec)",
00352                   (long)time.tv_sec, (long)time.tv_usec / 10000, (long)size * count);
00353         }
00354     }
00355   fprintf(stdout, "\n");
00356 
00357   g = time.tv_sec;
00358   d = (long)time.tv_usec / 10000;
00359 
00360   starttime();
00361   for(ct = 0; ct < count; ct++)
00362     {
00363       if((fd = open(bigfile, roflags)) < 0)
00364         {
00365           error("can't open '%s'", bigfile);
00366           exit(1);
00367         }
00368 #ifdef MMAP
00369       maddr = mmap((caddr_t) 0, (size_t) size, PROT_READ, MAP_PRIVATE, fd, (off_t) 0);
00370       if(maddr == MAP_FAILED)
00371         {
00372           error("can't mmap '%s'", bigfile);
00373           exit(1);
00374         }
00375       if(msync(maddr, (size_t) size, MS_INVALIDATE) < 0)
00376         {
00377           error("can't invalidate pages for '%s'", bigfile);
00378           exit(1);
00379         }
00380       if(munmap(maddr, (size_t) size) < 0)
00381         {
00382           error("can't munmap '%s'", bigfile);
00383           exit(1);
00384         }
00385 #endif
00386       for(si = size; si > 0; si -= bytes)
00387         {
00388           bytes = MIN(BUFSZ, si);
00389           if(read(fd, buf, bytes) != bytes)
00390             {
00391               error("'%s' read failed", bigfile);
00392               exit(1);
00393             }
00394         }
00395       close(fd);
00396     }
00397   endtime(&time);
00398 
00399   fprintf(stdout, "\tread %ld byte file %d times", (long)size, count);
00400   if(Tflag)
00401     {
00402       etime2 = (double)time.tv_sec + (double)time.tv_usec / 1000000.0;
00403       if(etime2 != 0.0)
00404         {
00405           fprintf(stdout, " in %ld.%02ld seconds (%d bytes/sec)",
00406                   (long)time.tv_sec, (long)time.tv_usec / 10000,
00407                   (int)((double)size * ((double)count / etime2)));
00408         }
00409       else
00410         {
00411           fprintf(stdout, " in %ld.%02ld seconds (> %ld bytes/sec)",
00412                   (long)time.tv_sec, (long)time.tv_usec / 10000, (long)size * count);
00413         }
00414     }
00415   fprintf(stdout, "\n");
00416 
00417   if(unlink(bigfile) < 0)
00418     {
00419       error("can't unlink '%s'", bigfile);
00420       exit(1);
00421     }
00422 
00423   if((log = fopen(log_file, "a")) == NULL)
00424     {
00425       printf("Enable to open the file '%s'\n", log_file);
00426       complete();
00427     }
00428 #ifdef _TOTO
00429   if(etime1 != 0.0)
00430     {
00431       if(etime2 != 0.0)
00432         {
00433           fprintf(log, "b5\t%d\t%d\t%d.%02ld\t%ld.%02ld\t%ld\t%ld\n", (long)size, count,
00434                   g, d, (long)time.tv_sec, (long)time.tv_usec / 10000,
00435                   (long)((double)size * ((double)count / etime1)),
00436                   (long)((double)size * ((double)count / etime2)));
00437         }
00438       else
00439         {
00440           fprintf(log, "b5\t%d\t%d\t%ld.%02ld\t%ld.%02ld\t%ld\t%ld\n", (long)size, count,
00441                   g, d, (long)time.tv_sec, (long)time.tv_usec / 10000,
00442                   (long)((double)size * ((double)count / etime1)), (long)size * count);
00443         }
00444     }
00445   else
00446     {
00447       if(etime2 != 0.0)
00448         {
00449           fprintf(log, "b5\t%d\t%d\t%ld.%02ld\t%ld.%02ld\t%ld\t%ld\n", (long)size, count,
00450                   g, d, (long)time.tv_sec, (long)time.tv_usec / 10000, (long)size * count,
00451                   (long)((double)size * ((double)count / etime2)));
00452         }
00453       else
00454         {
00455           fprintf(log, "b5\t%d\t%d\t%ld.%02ld\t%ld.%02ld\t%ld\t%ld\n", (long)size, count,
00456                   g, d, (long)time.tv_sec, (long)time.tv_usec / 10000, (long)size * count,
00457                   (long)size * count);
00458         }
00459     }
00460 #endif
00461   fclose(log);
00462 
00463   complete();
00464 }