root/user/testbin/f_test/f_test.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. forkoff
  2. dowait
  3. big_file
  4. concur
  5. dir_test
  6. main

   1 /*
   2  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
   3  *      The President and Fellows of Harvard College.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  * 1. Redistributions of source code must retain the above copyright
   9  *    notice, this list of conditions and the following disclaimer.
  10  * 2. Redistributions in binary form must reproduce the above copyright
  11  *    notice, this list of conditions and the following disclaimer in the
  12  *    documentation and/or other materials provided with the distribution.
  13  * 3. Neither the name of the University nor the names of its contributors
  14  *    may be used to endorse or promote products derived from this software
  15  *    without specific prior written permission.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  */
  29 
  30 /*
  31  * Razvan Surdulescu
  32  * abhi shelat
  33  * April 28 1997
  34  * 
  35  * Test suite for Nachos HW4--The Filesystem
  36  *
  37  * Modified by dholland 1/31/2001 for OS/161
  38  *
  39  * This should run successfully (on SFS) when the file system
  40  * assignment is complete.
  41  */
  42 
  43 #include <sys/types.h>
  44 #include <sys/stat.h>
  45 #include <stdio.h>
  46 #include <string.h>
  47 #include <unistd.h>
  48 #include <err.h>
  49 #include "f_hdr.h"
  50 
  51 #define SECTOR_SIZE  512
  52 
  53 
  54 #define BUFFER_SIZE  (2 * SECTOR_SIZE + 1)
  55 #define BIGFILE_SIZE (270 * BUFFER_SIZE)
  56 #define BIGFILE_NAME "large-f"
  57 
  58 #define LETTER(x) ('a' + (x % 31))
  59 
  60 char fbuffer[BUFFER_SIZE];
  61 char ibuffer[32];
  62 
  63 
  64 #define DIR_DEPTH      8
  65 #define DIR_NAME       "/t"
  66 #define DIRFILE_NAME   "a"
  67 
  68 
  69 #define FNAME        "f-testfile"
  70 #define TMULT        50
  71 #define FSIZE        ((SECTOR_SIZE + 1) * TMULT)
  72 
  73 #define READCHAR     'r'
  74 #define WRITECHAR    'w'
  75 
  76 char cbuffer[SECTOR_SIZE + 1];
  77 
  78 
  79 /* ===================================================
  80 
  81  */
  82 
  83 static
  84 pid_t
  85 forkoff(void (*func)(void))
  86 {
  87         pid_t pid = fork();
  88         switch (pid) {
  89             case -1:
  90                 warn("fork");
  91                 return -1;
  92             case 0: 
  93                 func();
  94                 _exit(0);
  95             default: break;
  96         }
  97         return pid;
  98 }
  99 
 100 static
 101 void
 102 dowait(int pid)
 103 {
 104         int status;
 105 
 106         if (waitpid(pid, &status, 0)<0) {
 107                 warn("waitpid for %d", pid);
 108         }
 109         else if (WIFSIGNALED(status)) {
 110                 warnx("pid %d: signal %d", pid, WTERMSIG(status));
 111         }
 112         else if (WEXITSTATUS(status) != 0) {
 113                 warnx("pid %d: exit %d", pid, WEXITSTATUS(status));
 114         }
 115 }
 116 
 117 /* ===================================================
 118         
 119  */
 120 
 121 static
 122 void
 123 big_file(int size)
 124 {
 125         int i, j, fileid;
 126         
 127         printf("[BIGFILE] test starting :\n");
 128         printf("\tCreating a file of size: %d\n", size);
 129         
 130         fileid = open(BIGFILE_NAME, O_WRONLY|O_CREAT|O_TRUNC, 0664);
 131         if (fileid < 0) {
 132                 err(1, "[BIGFILE]: %s: open for write", BIGFILE_NAME);
 133         }       
 134         
 135         for(i = 0; i < BUFFER_SIZE; i++) {
 136                 fbuffer[i] = LETTER(i);
 137         }
 138 
 139         printf("\tWriting to file.\n");
 140         for (i = 0; i < size; i += BUFFER_SIZE) {
 141                 write(fileid, fbuffer, BUFFER_SIZE);
 142 
 143                 if (!(i % (10 * BUFFER_SIZE))) {
 144                         printf("\rBW : %d", i);
 145                 }
 146         }
 147 
 148         printf("\n\tReading from file.\n");
 149         close(fileid);
 150 
 151         fileid = open(BIGFILE_NAME, O_RDONLY);
 152         if (fileid < 0) {
 153                 err(1, "[BIGFILE]: %s: open for read", BIGFILE_NAME);
 154         }
 155         
 156         for (i = 0; i < size; i += BUFFER_SIZE) {
 157                 j = read(fileid, fbuffer, BUFFER_SIZE);
 158                 if (j<0) {
 159                         err(1, "[BIGFILE]: read");
 160                 }
 161                 if (j != BUFFER_SIZE) {
 162                         errx(1, "[BIGFILE]: read: only %d bytes", j);
 163                 }
 164         }
 165 
 166         if (!(i % (10 * BUFFER_SIZE))) {
 167                 printf("\rBR : %d", i);
 168         }
 169 
 170         /* Check to see that the data is consistent : */
 171         for (j = 0; j < BUFFER_SIZE; j++) {
 172                 if (fbuffer[j] != LETTER(j)) {
 173                         errx(1, "[BIGFILE] : Failed read check : "
 174                              "inconsistent data read: %d", i+j);
 175                 }
 176         }
 177 
 178 
 179         close(fileid);
 180         if (remove(BIGFILE_NAME)) {
 181                 err(1, "[BIGFILE]: %s: remove", BIGFILE_NAME);
 182         }
 183 
 184         printf("\n[BIGFILE] : Success!\n");
 185 }
 186 
 187 /* ===================================================
 188 
 189  */
 190 
 191 static
 192 void
 193 concur(void)
 194 {
 195         int i, fd;
 196         int r1, r2, w1;
 197 
 198         printf("Spawning 2 readers, 1 writer.\n");
 199 
 200 
 201         fd = open(FNAME, O_WRONLY|O_CREAT|O_TRUNC, 0664);
 202         if (fd < 0) {
 203                 err(1, "[CONCUR]: %s: open", FNAME);
 204         }
 205 
 206         printf("Initializing test file: ");
 207 
 208         for (i = 0; i < SECTOR_SIZE + 1; i++) {
 209                 cbuffer[i] = READCHAR;
 210         }
 211 
 212         for (i = 0; i < TMULT; i++) {
 213                 write(fd, cbuffer, SECTOR_SIZE + 1);
 214         }
 215 
 216 
 217         close(fd);
 218 
 219         printf("Done initializing. Starting processes...\n");
 220 
 221         r1 = forkoff(subproc_read);
 222         w1 = forkoff(subproc_write);
 223         r2 = forkoff(subproc_read);
 224 
 225         printf("Waiting for processes.\n");
 226 
 227         dowait(r1);
 228         dowait(r2);
 229         dowait(w1);
 230 
 231         if (remove(FNAME)) {
 232                 err(1, "[CONCUR]: %s: remove", FNAME);
 233         }
 234          
 235         printf("[CONCUR] Done!\n");
 236 }
 237 
 238 /* ===================================================
 239         
 240  */
 241 
 242 static
 243 void
 244 dir_test(int depth)
 245 {
 246         int i, fd;
 247         char tmp[] = DIR_NAME;
 248         char fmp[] = DIRFILE_NAME;
 249         char dirname[64];
 250 
 251         strcpy(dirname, ".");
 252 
 253         for (i = 0; i < depth; i++) {
 254                 strcat(dirname, tmp);
 255                 
 256                 printf("\tCreating dir : %s\n", dirname);
 257 
 258                 if (mkdir(dirname, 0775) < 0) {
 259                         err(1, "[DIRTEST]: %s: mkdir", dirname);
 260                 }
 261 
 262                 strcat(dirname, fmp);
 263                 printf("\tCreating file: %s\n", dirname);
 264 
 265                 fd = open(dirname, O_WRONLY|O_CREAT|O_TRUNC, 0664);
 266                 if (fd<0) {
 267                         err(1, "[DIRTEST]: %s: open", dirname);
 268                 }
 269 
 270                 dirname[strlen(dirname) - strlen(fmp)] = '\0';
 271         }
 272 
 273         printf("[DIRTEST] : Passed directory creation test.\n");
 274 
 275         for (i = 0; i < depth; i++) {
 276                 strcat(dirname, fmp);
 277 
 278                 printf("\tDeleting file: %s\n", dirname);
 279                   
 280                 if (remove(dirname)) {
 281                          err(1, "[DIRTEST]: %s: remove", dirname);
 282                 }
 283 
 284                 dirname[strlen(dirname) - strlen(fmp)] = '\0';
 285                 printf("\tRemoving dir : %s\n", dirname);
 286 
 287                 if (rmdir(dirname)) {
 288                         err(1, "[DIRTEST]: %s: rmdir", dirname);
 289                 }
 290 
 291                 dirname[strlen(dirname) - strlen(tmp)] = '\0';
 292         }
 293 
 294         printf("[DIRTEST] : Passed directory removal test.\n");
 295         printf("[DIRTEST] : Success!\n");
 296 }
 297 
 298 /* ===================================================
 299 
 300  */
 301 
 302 #define   RUNBIGFILE  0x1
 303 #define   RUNDIRTEST  0x2
 304 #define   RUNCONCUR   0x4
 305 #define   RUNTHEMALL  (RUNBIGFILE | RUNDIRTEST | RUNCONCUR) 
 306 
 307 int
 308 main(int argc, char * argv[])
 309 {
 310         int tv = 0;
 311 
 312         if (argc > 1) {
 313                 if (*argv[1]=='1') {
 314                         tv = RUNBIGFILE;
 315                 }
 316                 else if (*argv[1]=='2') {
 317                         tv = RUNDIRTEST;
 318                 }
 319                 else if (*argv[1]=='3') {
 320                         tv = RUNCONCUR;
 321                 }
 322         } 
 323         else {
 324                 tv = RUNTHEMALL;
 325         }
 326          
 327         if (tv & RUNBIGFILE) {
 328                 printf("[BIGFILE] : Run #1\n");
 329                 big_file(BIGFILE_SIZE); 
 330                 printf("[BIGFILE] : Run #2\n");
 331                 big_file(BIGFILE_SIZE);
 332         }
 333          
 334         if (tv & RUNDIRTEST) {
 335                 printf("[DIRTEST] : Run #1\n");
 336                 dir_test(DIR_DEPTH);
 337                 printf("[DIRTEST] : Run #2\n");
 338                 dir_test(DIR_DEPTH);
 339         }
 340          
 341         if (tv & RUNCONCUR) {
 342                 printf("[CONCUR]\n");
 343                 concur();
 344         }
 345         return 0;
 346 }
 347 
 348 

/* [<][>][^][v][top][bottom][index][help] */