From 16b3b63f06c1ea17da484aeebea4a57fb2a6e44a Mon Sep 17 00:00:00 2001 From: Robert Morris Date: Wed, 6 Nov 2019 11:18:43 -0500 Subject: [PATCH] grind: run parallel system calls forever --- Makefile | 1 + user/grind.c | 160 +++++++++++++++++++++++++++++++++++++++++++++++ user/usertests.c | 14 +++-- 3 files changed, 170 insertions(+), 5 deletions(-) create mode 100644 user/grind.c diff --git a/Makefile b/Makefile index 7229d5e..46fd956 100644 --- a/Makefile +++ b/Makefile @@ -127,6 +127,7 @@ UPROGS=\ $U/_sh\ $U/_stressfs\ $U/_usertests\ + $U/_grind\ $U/_wc\ $U/_zombie\ diff --git a/user/grind.c b/user/grind.c new file mode 100644 index 0000000..97a8a93 --- /dev/null +++ b/user/grind.c @@ -0,0 +1,160 @@ +// +// run random system calls in parallel forever. +// + +#include "kernel/param.h" +#include "kernel/types.h" +#include "kernel/stat.h" +#include "user/user.h" +#include "kernel/fs.h" +#include "kernel/fcntl.h" +#include "kernel/syscall.h" +#include "kernel/memlayout.h" +#include "kernel/riscv.h" + +// from FreeBSD. +int +do_rand(unsigned long *ctx) +{ +/* + * Compute x = (7^5 * x) mod (2^31 - 1) + * without overflowing 31 bits: + * (2^31 - 1) = 127773 * (7^5) + 2836 + * From "Random number generators: good ones are hard to find", + * Park and Miller, Communications of the ACM, vol. 31, no. 10, + * October 1988, p. 1195. + */ + long hi, lo, x; + + /* Transform to [1, 0x7ffffffe] range. */ + x = (*ctx % 0x7ffffffe) + 1; + hi = x / 127773; + lo = x % 127773; + x = 16807 * lo - 2836 * hi; + if (x < 0) + x += 0x7fffffff; + /* Transform to [0, 0x7ffffffd] range. */ + x--; + *ctx = x; + return (x); +} + +unsigned long rand_next = 1; + +int +rand(void) +{ + return (do_rand(&rand_next)); +} + +void +go() +{ + int fd = -1; + static char buf[999]; + char *break0 = sbrk(0); + + while(1){ + int what = rand() % 20; + if(what == 1){ + close(open("a", O_CREATE|O_RDWR)); + } else if(what == 2){ + close(open("b", O_CREATE|O_RDWR)); + } else if(what == 3){ + unlink("a"); + } else if(what == 4){ + unlink("b"); + } else if(what == 5){ + close(fd); + fd = open("a", O_CREATE|O_RDWR); + } else if(what == 6){ + close(fd); + fd = open("b", O_CREATE|O_RDWR); + } else if(what == 7){ + write(fd, buf, sizeof(buf)); + } else if(what == 8){ + read(fd, buf, sizeof(buf)); + } else if(what == 9){ + mkdir("a"); + } else if(what == 10){ + mkdir("b"); + } else if(what == 11){ + unlink("b"); + link("a", "b"); + } else if(what == 12){ + unlink("a"); + link("b", "a"); + } else if(what == 13){ + int pid = fork(); + if(pid == 0){ + exit(0); + } + wait(0); + } else if(what == 14){ + int pid = fork(); + if(pid == 0){ + fork(); + fork(); + exit(0); + } + wait(0); + } else if(what == 15){ + sbrk(6011); + } else if(what == 16){ + if(sbrk(0) > break0) + sbrk(-(sbrk(0) - break0)); + } else if(what == 17){ + int pid = fork(); + if(pid == 0){ + close(open("a", O_CREATE|O_RDWR)); + exit(0); + } + kill(pid); + wait(0); + } else if(what == 18){ + int pid = fork(); + if(pid == 0){ + kill(getpid()); + exit(0); + } + wait(0); + } + } +} + +int +main() +{ + int pid1 = fork(); + if(pid1 < 0){ + printf("grind: fork failed\n"); + exit(1); + } + if(pid1 == 0){ + rand_next = 31; + go(); + exit(0); + } + + int pid2 = fork(); + if(pid2 < 0){ + printf("grind: fork failed\n"); + exit(1); + } + if(pid2 == 0){ + rand_next = 7177; + go(); + exit(0); + } + + int st1 = -1; + wait(&st1); + if(st1 != 0){ + kill(pid1); + kill(pid2); + } + int st2 = -1; + wait(&st2); + + exit(0); +} diff --git a/user/usertests.c b/user/usertests.c index 8d1d06a..9aa0ed4 100644 --- a/user/usertests.c +++ b/user/usertests.c @@ -1038,11 +1038,15 @@ concreate(char *s) close(open(file, 0)); close(open(file, 0)); close(open(file, 0)); + close(open(file, 0)); + close(open(file, 0)); } else { unlink(file); unlink(file); unlink(file); unlink(file); + unlink(file); + unlink(file); } if(pid == 0) exit(0); @@ -1106,7 +1110,7 @@ bigdir(char *s) name[2] = '0' + (i % 64); name[3] = '\0'; if(link("bd", name) != 0){ - printf("%s: bigdir link failed\n", s); + printf("%s: bigdir link(bd, %s) failed\n", s, name); exit(1); } } @@ -1335,8 +1339,8 @@ bigfile(char *s) enum { N = 20, SZ=600 }; int fd, i, total, cc; - unlink("bigfile"); - fd = open("bigfile", O_CREATE | O_RDWR); + unlink("bigfile.dat"); + fd = open("bigfile.dat", O_CREATE | O_RDWR); if(fd < 0){ printf("%s: cannot create bigfile", s); exit(1); @@ -1350,7 +1354,7 @@ bigfile(char *s) } close(fd); - fd = open("bigfile", 0); + fd = open("bigfile.dat", 0); if(fd < 0){ printf("%s: cannot open bigfile\n", s); exit(1); @@ -1379,7 +1383,7 @@ bigfile(char *s) printf("%s: read bigfile wrong total\n", s); exit(1); } - unlink("bigfile"); + unlink("bigfile.dat"); } void