Author: asomers
Date: Fri Apr 29 21:11:31 2016
New Revision: 298811
URL: https://svnweb.freebsd.org/changeset/base/298811

Log:
  Automate the subr_unit test.
  
  Build and install the subr_unit test program originally written by phk, and
  run it with the other ATF tests.
  
  tests/sys/kern/Makefile
        * Build and install the subr_unit test as a plain test
  
  sys/kern/subr_unit.c
        * Reduce the default number of repetitions from 100 to 1, and add a
          command-line parser to override it.
        * Don't be so noisy by default
        * Fix an include problem for the test build
  
  Reviewed by:  ngie
  MFC after:    4 weeks
  Sponsored by: Spectra Logic Corp
  Differential Revision:        https://reviews.freebsd.org/D6038

Modified:
  head/sys/kern/subr_unit.c
  head/tests/sys/kern/Makefile

Modified: head/sys/kern/subr_unit.c
==============================================================================
--- head/sys/kern/subr_unit.c   Fri Apr 29 21:05:48 2016        (r298810)
+++ head/sys/kern/subr_unit.c   Fri Apr 29 21:11:31 2016        (r298811)
@@ -68,11 +68,11 @@
  */
 
 #include <sys/types.h>
-#include <sys/bitstring.h>
 #include <sys/_unrhdr.h>
 
 #ifdef _KERNEL
 
+#include <sys/bitstring.h>
 #include <sys/param.h>
 #include <sys/malloc.h>
 #include <sys/kernel.h>
@@ -98,6 +98,11 @@ MTX_SYSINIT(unit, &unitmtx, "unit# alloc
 
 #else /* ...USERLAND */
 
+#include <bitstring.h>
+#include <err.h>
+#include <errno.h>
+#include <getopt.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -880,9 +885,13 @@ free_unr(struct unrhdr *uh, u_int item)
 #ifndef _KERNEL        /* USERLAND test driver */
 
 /*
- * Simple stochastic test driver for the above functions
+ * Simple stochastic test driver for the above functions.  The code resides
+ * here so that it can access static functions and structures.
  */
 
+static bool verbose;
+#define VPRINTF(...)   {if (verbose) printf(__VA_ARGS__);}
+
 static void
 print_unr(struct unrhdr *uh, struct unr *up)
 {
@@ -933,7 +942,7 @@ test_alloc_unr(struct unrhdr *uh, u_int 
        int j;
 
        if (a[i]) {
-               printf("F %u\n", i);
+               VPRINTF("F %u\n", i);
                free_unr(uh, i);
                a[i] = 0;
        } else {
@@ -941,7 +950,7 @@ test_alloc_unr(struct unrhdr *uh, u_int 
                j = alloc_unr(uh);
                if (j != -1) {
                        a[j] = 1;
-                       printf("A %d\n", j);
+                       VPRINTF("A %d\n", j);
                }
                no_alloc = 0;
        }
@@ -954,40 +963,73 @@ test_alloc_unr_specific(struct unrhdr *u
 
        j = alloc_unr_specific(uh, i);
        if (j == -1) {
-               printf("F %u\n", i);
+               VPRINTF("F %u\n", i);
                a[i] = 0;
                free_unr(uh, i);
        } else {
                a[i] = 1;
-               printf("A %d\n", j);
+               VPRINTF("A %d\n", j);
        }
 }
 
-/* Number of unrs to test */
-#define NN     10000
+static void
+usage(char** argv)
+{
+       printf("%s [-h] [-r REPETITIONS] [-v]\n", argv[0]);
+}
 
 int
-main(int argc __unused, const char **argv __unused)
+main(int argc, char **argv)
 {
        struct unrhdr *uh;
+       char *a;
+       long count = 10000;     /* Number of unrs to test */
+       long reps = 1;
+       int ch;
        u_int i, x, m, j;
-       char a[NN];
+
+       verbose = false;
+
+       while ((ch = getopt(argc, argv, "hr:v")) != -1) {
+               switch (ch) {
+               case 'r':
+                       errno = 0;
+                       reps = strtol(optarg, NULL, 0);
+                       if (errno == ERANGE || errno == EINVAL) {
+                               usage(argv);
+                               exit(2);
+                       }
+                       
+                       break;
+               case 'v':
+                       verbose = true;
+                       break;
+               case 'h':
+               default:
+                       usage(argv);
+                       exit(2);
+               }
+
+
+       }
 
        setbuf(stdout, NULL);
-       uh = new_unrhdr(0, NN - 1, NULL);
+       uh = new_unrhdr(0, count - 1, NULL);
        print_unrhdr(uh);
 
-       memset(a, 0, sizeof a);
+       a = calloc(count, sizeof(char));
+       if (a == NULL)
+               err(1, "calloc failed");
        srandomdev();
 
-       fprintf(stderr, "sizeof(struct unr) %zu\n", sizeof(struct unr));
-       fprintf(stderr, "sizeof(struct unrb) %zu\n", sizeof(struct unrb));
-       fprintf(stderr, "sizeof(struct unrhdr) %zu\n", sizeof(struct unrhdr));
-       fprintf(stderr, "NBITS %d\n", NBITS);
+       printf("sizeof(struct unr) %zu\n", sizeof(struct unr));
+       printf("sizeof(struct unrb) %zu\n", sizeof(struct unrb));
+       printf("sizeof(struct unrhdr) %zu\n", sizeof(struct unrhdr));
+       printf("NBITS %d\n", NBITS);
        x = 1;
-       for (m = 0; m < NN * 100; m++) {
+       for (m = 0; m < count * reps; m++) {
                j = random();
-               i = (j >> 1) % NN;
+               i = (j >> 1) % count;
 #if 0
                if (a[i] && (j & 1))
                        continue;
@@ -997,19 +1039,22 @@ main(int argc __unused, const char **arg
                else
                        test_alloc_unr_specific(uh, i, a);
 
-               if (1)  /* XXX: change this for detailed debug printout */
+               if (verbose)
                        print_unrhdr(uh);
                check_unrhdr(uh, __LINE__);
        }
-       for (i = 0; i < NN; i++) {
+       for (i = 0; i < count; i++) {
                if (a[i]) {
-                       printf("C %u\n", i);
+                       if (verbose) {
+                               printf("C %u\n", i);
+                               print_unrhdr(uh);
+                       }
                        free_unr(uh, i);
-                       print_unrhdr(uh);
                }
        }
        print_unrhdr(uh);
        delete_unrhdr(uh);
+       free(a);
        return (0);
 }
 #endif

Modified: head/tests/sys/kern/Makefile
==============================================================================
--- head/tests/sys/kern/Makefile        Fri Apr 29 21:05:48 2016        
(r298810)
+++ head/tests/sys/kern/Makefile        Fri Apr 29 21:11:31 2016        
(r298811)
@@ -4,12 +4,14 @@ PACKAGE=      tests
 FILESGROUPS=   TESTS
 TESTSPACKAGE=  ${PACKAGE}
 TESTSRC=       ${SRCTOP}/contrib/netbsd-tests/kernel
+.PATH:         ${SRCTOP}/sys/kern
 
 TESTSDIR=      ${TESTSBASE}/sys/kern
 
 ATF_TESTS_C+=  kern_copyin
 ATF_TESTS_C+=  kern_descrip_test
 ATF_TESTS_C+=  ptrace_test
+PLAIN_TESTS_C+=        subr_unit_test
 ATF_TESTS_C+=  unix_seqpacket_test
 ATF_TESTS_C+=  unix_passfd_test
 TEST_METADATA.unix_seqpacket_test+=    timeout="15"
@@ -23,6 +25,14 @@ NETBSD_ATF_TESTS_C+= mqueue_test
 CFLAGS.mqueue_test+=   -I${SRCTOP}/tests
 LIBADD.mqueue_test+=   rt
 
+# subr_unit.c contains functions whose prototypes lie in headers that cannot be
+# included in userland.  But as far as subr_unit_test goes, they're effectively
+# static.  So it's ok to disable -Wmissing-prototypes for this program.
+CFLAGS.subr_unit.c+=   -Wno-missing-prototypes
+# XXX: -Wno-sign-compare will be eliminated as part of D6004
+CFLAGS.subr_unit.c+=   -Wno-sign-compare
+SRCS.subr_unit_test+=  subr_unit.c
+
 WARNS?=        5
 
 TESTS_SUBDIRS+=        acct
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to