nfs-ganesha 1.4

test5a.c

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