Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/regress/sys/kern Lockf regression test.



details:   https://anonhg.NetBSD.org/src/rev/62a5012000c1
branches:  trunk
changeset: 487754:62a5012000c1
user:      sommerfeld <sommerfeld%NetBSD.org@localhost>
date:      Mon Jun 12 14:42:02 2000 +0000

description:
Lockf regression test.

diffstat:

 regress/sys/kern/Makefile       |    4 +-
 regress/sys/kern/lockf/Makefile |   14 +++
 regress/sys/kern/lockf/lockf.c  |  145 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 161 insertions(+), 2 deletions(-)

diffs (178 lines):

diff -r 6a87b8b7c325 -r 62a5012000c1 regress/sys/kern/Makefile
--- a/regress/sys/kern/Makefile Mon Jun 12 14:33:04 2000 +0000
+++ b/regress/sys/kern/Makefile Mon Jun 12 14:42:02 2000 +0000
@@ -1,5 +1,5 @@
-#      $NetBSD: Makefile,v 1.9 1999/08/25 05:16:41 thorpej Exp $
+#      $NetBSD: Makefile,v 1.10 2000/06/12 14:42:02 sommerfeld Exp $
 
-SUBDIR+= execve extent unfdpass getcwd sigtramp sysvmsg sysvsem sysvshm
+SUBDIR+= execve extent getcwd lockf sigtramp sysvmsg sysvsem sysvshm unfdpass 
 
 .include <bsd.subdir.mk>
diff -r 6a87b8b7c325 -r 62a5012000c1 regress/sys/kern/lockf/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/sys/kern/lockf/Makefile   Mon Jun 12 14:42:02 2000 +0000
@@ -0,0 +1,14 @@
+#      $NetBSD: Makefile,v 1.1 2000/06/12 14:42:03 sommerfeld Exp $
+
+PROG=  lockf
+MKMAN= no
+WARNS= 2
+
+regress:
+       @if ./lockf ; then \
+               echo "PASSED"; \
+       else \
+               echo "FAILED"; \
+       fi
+
+.include <bsd.prog.mk>
diff -r 6a87b8b7c325 -r 62a5012000c1 regress/sys/kern/lockf/lockf.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/sys/kern/lockf/lockf.c    Mon Jun 12 14:42:02 2000 +0000
@@ -0,0 +1,145 @@
+/*
+ * lockf regression test:
+ *
+ * Tests:
+ * 1) fork N child processes, do a bunch of random byte range lock/unlock.
+ */
+
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/ptrace.h> 
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <err.h>
+#include <signal.h>
+
+int nlocks = 1000;             /* number of locks per thread */
+int nprocs = 10;               /* number of processes to spawn */
+int sleeptime = 500000;                /* sleep time between locks, usec */
+int size = 8192;               /* size of file to lock */
+const char *lockfile = "/tmp/lockf_test";
+
+static u_int32_t
+random_uint32(void)
+{
+       return lrand48();
+}
+
+
+static void
+trylocks(int id)
+{
+       int i, ret, fd;
+       const char *which;
+       
+       srand48(getpid());
+
+       fd = open (lockfile, O_RDWR, 0);
+       
+       if (fd < 0)
+               err(1, lockfile);
+       
+       printf("%d: start\n", id);
+       
+       for (i=0; i<nlocks; i++) {
+               struct flock fl;
+
+               fl.l_start = random_uint32() % size;
+               fl.l_len = random_uint32() % size;
+               switch (random_uint32() % 3) {
+               case 0:
+                       which = "read";
+                       fl.l_type = F_RDLCK;
+                       break;
+               case 1:
+                       which = "write";
+                       fl.l_type = F_WRLCK;
+                       break;
+               case 2:
+                       which = "un";
+                       fl.l_type = F_UNLCK;
+                       break;
+               }
+               fl.l_whence = SEEK_SET;
+
+               printf("%d: try %slock %d to %d\n", id, which, (int)fl.l_start,
+                   (int)(fl.l_start + fl.l_len));
+               
+               ret = fcntl(fd, F_SETLKW, &fl);
+
+               if (ret < 0)
+                       perror("fcntl");
+               printf("%d: got %slock %d to %d\n", id, which, (int)fl.l_start,
+                   ((int)(fl.l_start + fl.l_len)));
+               
+               if (usleep(sleeptime) < 0) 
+                 err(1, "usleep");
+       }
+       printf("%d: done\n", id);
+       close (fd);
+}
+
+int
+main(int argc, char **argv)
+{
+       int i, j;
+       int *pid;
+       int status;
+       int fd;
+       
+       (void)unlink(lockfile);
+
+       fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
+       if (fd < 0)
+               err(1, "%s", lockfile);
+
+       if (ftruncate(fd, size) < 0)
+               err(1, "ftruncate of %s failed", lockfile);
+
+       fsync(fd);
+       close(fd);
+       
+       pid = malloc(nprocs * sizeof(int));
+       
+       for (i=0; i<nprocs; i++) {
+               pid[i] = fork();
+               switch (pid[i]) {
+               case 0:
+                       trylocks(i);
+                       _exit(0);
+                       break;
+               case -1:
+                       err(1, "fork failed");
+                       break;
+               default:
+                       break;
+               }
+       }
+       for (j=0; j<100; j++) {
+               for (i=0; i<nprocs; i++) {
+                       printf("stop %d\n", i);
+                       if (ptrace(PT_ATTACH, pid[i], 0, 0) < 0)
+                               err(1, "ptrace");
+                       printf("wait %d\n", i);
+                       if (waitpid(pid[i], &status, WUNTRACED) < 0)
+                               err(1, "waitpid(ptrace)");
+                       printf("awake %d\n", i);
+                       usleep(sleeptime/3);
+                       if (ptrace(PT_DETACH, pid[i], 0, 0) < 0)
+                               err(1, "ptrace");
+                       printf("done %d\n", i);
+                       usleep(sleeptime/3);
+               }
+       }
+       for (i=0; i<nprocs; i++) {
+               printf("reap %d: ", i);
+               fflush(stdout);
+               waitpid(pid[i], &status, 0);
+               printf(" status %d\n", status);
+       }
+       exit(0);
+}
+



Home | Main Index | Thread Index | Old Index