f_test.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. * Razvan Surdulescu
  31. * abhi shelat
  32. * April 28 1997
  33. *
  34. * Test suite for Nachos HW4--The Filesystem
  35. *
  36. * Modified by dholland 1/31/2001 for OS/161
  37. *
  38. * This should run successfully (on SFS) when the file system
  39. * assignment is complete.
  40. */
  41. #include <sys/types.h>
  42. #include <sys/stat.h>
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <unistd.h>
  46. #include <err.h>
  47. #include "f_hdr.h"
  48. #define SECTOR_SIZE 512
  49. #define BUFFER_SIZE (2 * SECTOR_SIZE + 1)
  50. #define BIGFILE_SIZE (270 * BUFFER_SIZE)
  51. #define BIGFILE_NAME "large-f"
  52. #define LETTER(x) ('a' + (x % 31))
  53. char fbuffer[BUFFER_SIZE];
  54. char ibuffer[32];
  55. #define DIR_DEPTH 8
  56. #define DIR_NAME "/t"
  57. #define DIRFILE_NAME "a"
  58. #define FNAME "f-testfile"
  59. #define TMULT 50
  60. #define FSIZE ((SECTOR_SIZE + 1) * TMULT)
  61. #define READCHAR 'r'
  62. #define WRITECHAR 'w'
  63. char cbuffer[SECTOR_SIZE + 1];
  64. /* ===================================================
  65. */
  66. static
  67. pid_t
  68. forkoff(void (*func)(void))
  69. {
  70. pid_t pid = fork();
  71. switch (pid) {
  72. case -1:
  73. warn("fork");
  74. return -1;
  75. case 0:
  76. func();
  77. _exit(0);
  78. default: break;
  79. }
  80. return pid;
  81. }
  82. static
  83. void
  84. dowait(int pid)
  85. {
  86. int status;
  87. if (waitpid(pid, &status, 0)<0) {
  88. warn("waitpid for %d", pid);
  89. }
  90. else if (WIFSIGNALED(status)) {
  91. warnx("pid %d: signal %d", pid, WTERMSIG(status));
  92. }
  93. else if (WEXITSTATUS(status) != 0) {
  94. warnx("pid %d: exit %d", pid, WEXITSTATUS(status));
  95. }
  96. }
  97. /* ===================================================
  98. */
  99. static
  100. void
  101. big_file(int size)
  102. {
  103. int i, j, fileid;
  104. printf("[BIGFILE] test starting :\n");
  105. printf("\tCreating a file of size: %d\n", size);
  106. fileid = open(BIGFILE_NAME, O_WRONLY|O_CREAT|O_TRUNC, 0664);
  107. if (fileid < 0) {
  108. err(1, "[BIGFILE]: %s: open for write", BIGFILE_NAME);
  109. }
  110. for(i = 0; i < BUFFER_SIZE; i++) {
  111. fbuffer[i] = LETTER(i);
  112. }
  113. printf("\tWriting to file.\n");
  114. for (i = 0; i < size; i += BUFFER_SIZE) {
  115. write(fileid, fbuffer, BUFFER_SIZE);
  116. if (!(i % (10 * BUFFER_SIZE))) {
  117. printf("\rBW : %d", i);
  118. }
  119. }
  120. printf("\n\tReading from file.\n");
  121. close(fileid);
  122. fileid = open(BIGFILE_NAME, O_RDONLY);
  123. if (fileid < 0) {
  124. err(1, "[BIGFILE]: %s: open for read", BIGFILE_NAME);
  125. }
  126. for (i = 0; i < size; i += BUFFER_SIZE) {
  127. j = read(fileid, fbuffer, BUFFER_SIZE);
  128. if (j<0) {
  129. err(1, "[BIGFILE]: read");
  130. }
  131. if (j != BUFFER_SIZE) {
  132. errx(1, "[BIGFILE]: read: only %d bytes", j);
  133. }
  134. }
  135. if (!(i % (10 * BUFFER_SIZE))) {
  136. printf("\rBR : %d", i);
  137. }
  138. /* Check to see that the data is consistent : */
  139. for (j = 0; j < BUFFER_SIZE; j++) {
  140. if (fbuffer[j] != LETTER(j)) {
  141. errx(1, "[BIGFILE] : Failed read check : "
  142. "inconsistent data read: %d", i+j);
  143. }
  144. }
  145. close(fileid);
  146. if (remove(BIGFILE_NAME)) {
  147. err(1, "[BIGFILE]: %s: remove", BIGFILE_NAME);
  148. }
  149. printf("\n[BIGFILE] : Success!\n");
  150. }
  151. /* ===================================================
  152. */
  153. static
  154. void
  155. concur(void)
  156. {
  157. int i, fd;
  158. int r1, r2, w1;
  159. printf("Spawning 2 readers, 1 writer.\n");
  160. fd = open(FNAME, O_WRONLY|O_CREAT|O_TRUNC, 0664);
  161. if (fd < 0) {
  162. err(1, "[CONCUR]: %s: open", FNAME);
  163. }
  164. printf("Initializing test file: ");
  165. for (i = 0; i < SECTOR_SIZE + 1; i++) {
  166. cbuffer[i] = READCHAR;
  167. }
  168. for (i = 0; i < TMULT; i++) {
  169. write(fd, cbuffer, SECTOR_SIZE + 1);
  170. }
  171. close(fd);
  172. printf("Done initializing. Starting processes...\n");
  173. r1 = forkoff(subproc_read);
  174. w1 = forkoff(subproc_write);
  175. r2 = forkoff(subproc_read);
  176. printf("Waiting for processes.\n");
  177. dowait(r1);
  178. dowait(r2);
  179. dowait(w1);
  180. if (remove(FNAME)) {
  181. err(1, "[CONCUR]: %s: remove", FNAME);
  182. }
  183. printf("[CONCUR] Done!\n");
  184. }
  185. /* ===================================================
  186. */
  187. static
  188. void
  189. dir_test(int depth)
  190. {
  191. int i, fd;
  192. char tmp[] = DIR_NAME;
  193. char fmp[] = DIRFILE_NAME;
  194. char dirname[64];
  195. strcpy(dirname, ".");
  196. for (i = 0; i < depth; i++) {
  197. strcat(dirname, tmp);
  198. printf("\tCreating dir : %s\n", dirname);
  199. if (mkdir(dirname, 0775) < 0) {
  200. err(1, "[DIRTEST]: %s: mkdir", dirname);
  201. }
  202. strcat(dirname, fmp);
  203. printf("\tCreating file: %s\n", dirname);
  204. fd = open(dirname, O_WRONLY|O_CREAT|O_TRUNC, 0664);
  205. if (fd<0) {
  206. err(1, "[DIRTEST]: %s: open", dirname);
  207. }
  208. dirname[strlen(dirname) - strlen(fmp)] = '\0';
  209. }
  210. printf("[DIRTEST] : Passed directory creation test.\n");
  211. for (i = 0; i < depth; i++) {
  212. strcat(dirname, fmp);
  213. printf("\tDeleting file: %s\n", dirname);
  214. if (remove(dirname)) {
  215. err(1, "[DIRTEST]: %s: remove", dirname);
  216. }
  217. dirname[strlen(dirname) - strlen(fmp)] = '\0';
  218. printf("\tRemoving dir : %s\n", dirname);
  219. if (rmdir(dirname)) {
  220. err(1, "[DIRTEST]: %s: rmdir", dirname);
  221. }
  222. dirname[strlen(dirname) - strlen(tmp)] = '\0';
  223. }
  224. printf("[DIRTEST] : Passed directory removal test.\n");
  225. printf("[DIRTEST] : Success!\n");
  226. }
  227. /* ===================================================
  228. */
  229. #define RUNBIGFILE 0x1
  230. #define RUNDIRTEST 0x2
  231. #define RUNCONCUR 0x4
  232. #define RUNTHEMALL (RUNBIGFILE | RUNDIRTEST | RUNCONCUR)
  233. int
  234. main(int argc, char * argv[])
  235. {
  236. int tv = 0;
  237. if (argc > 1) {
  238. if (*argv[1]=='1') {
  239. tv = RUNBIGFILE;
  240. }
  241. else if (*argv[1]=='2') {
  242. tv = RUNDIRTEST;
  243. }
  244. else if (*argv[1]=='3') {
  245. tv = RUNCONCUR;
  246. }
  247. }
  248. else {
  249. tv = RUNTHEMALL;
  250. }
  251. if (tv & RUNBIGFILE) {
  252. printf("[BIGFILE] : Run #1\n");
  253. big_file(BIGFILE_SIZE);
  254. printf("[BIGFILE] : Run #2\n");
  255. big_file(BIGFILE_SIZE);
  256. }
  257. if (tv & RUNDIRTEST) {
  258. printf("[DIRTEST] : Run #1\n");
  259. dir_test(DIR_DEPTH);
  260. printf("[DIRTEST] : Run #2\n");
  261. dir_test(DIR_DEPTH);
  262. }
  263. if (tv & RUNCONCUR) {
  264. printf("[CONCUR]\n");
  265. concur();
  266. }
  267. return 0;
  268. }