diff --git a/regress/sys/Makefile b/regress/sys/Makefile index 6edbdba27461..cf15301a9044 100644 --- a/regress/sys/Makefile +++ b/regress/sys/Makefile @@ -1,6 +1,6 @@ -# $NetBSD: Makefile,v 1.8 1999/03/10 18:57:06 bouyer Exp $ +# $NetBSD: Makefile,v 1.9 1999/06/19 00:53:31 thorpej Exp $ -SUBDIR= kern fs +SUBDIR= kern fs uvm .if exists(arch/${MACHINE}) SUBDIR+= arch/${MACHINE} .endif diff --git a/regress/sys/uvm/Makefile b/regress/sys/uvm/Makefile new file mode 100644 index 000000000000..a3ff943bb41c --- /dev/null +++ b/regress/sys/uvm/Makefile @@ -0,0 +1,5 @@ +# $NetBSD: Makefile,v 1.1 1999/06/19 00:53:32 thorpej Exp $ + +SUBDIR+= mmap + +.include diff --git a/regress/sys/uvm/mmap/Makefile b/regress/sys/uvm/mmap/Makefile new file mode 100644 index 000000000000..dc921e038f73 --- /dev/null +++ b/regress/sys/uvm/mmap/Makefile @@ -0,0 +1,16 @@ +# $NetBSD: Makefile,v 1.1 1999/06/19 00:53:32 thorpej Exp $ + +PROG= mmap +MKMAN= no + +# This is hopefully just some file not mmap'd very often. +SOME_FILE=/usr/share/misc/zipcodes + +regress: + @if ./mmap ${SOME_FILE}; then \ + echo "PASSED"; \ + else \ + echo "FAILED"; \ + fi + +.include diff --git a/regress/sys/uvm/mmap/mmap.c b/regress/sys/uvm/mmap/mmap.c new file mode 100644 index 000000000000..5206fde87bd7 --- /dev/null +++ b/regress/sys/uvm/mmap/mmap.c @@ -0,0 +1,224 @@ +/* $NetBSD: mmap.c,v 1.1 1999/06/19 00:53:32 thorpej Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Test various memory mapping facilities. + */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +int main __P((int, char *[])); +void usage __P((void)); + +int check_residency __P((void *, int)); + +int pgsize; +int verbose; + +int +main(argc, argv) + int argc; + char *argv[]; +{ + struct stat st; + void *addr, *addr2; + int ch, ecode, fd, npgs; + const char *filename; + + while ((ch = getopt(argc, argv, "v")) != -1) { + switch (ch) { + case 'v': + verbose = 1; + break; + + default: + usage(); + } + } + argv += optind; + argc -= optind; + + if (argc != 1) + usage(); + + filename = argv[0]; + + ecode = 0; + + pgsize = sysconf(_SC_PAGESIZE); + + /* + * TEST MLOCKING A FILE. + */ + + printf(">>> MAPPING FILE <<<\n"); + + fd = open(filename, O_RDONLY, 0666); + if (fd == -1) + err(1, "open %s", filename); + + if (fstat(fd, &st) == -1) + err(1, "fstat %s", filename); + + addr = mmap(NULL, (size_t) st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, + fd, (off_t) 0); + if (addr == MAP_FAILED) + err(1, "mmap %s", filename); + + (void) close(fd); + + npgs = (st.st_size / pgsize) + 1; + + printf(" CHECKING RESIDENCY\n"); + + (void) check_residency(addr, npgs); + + printf(" LOCKING RANGE\n"); + + if (mlock(addr, npgs * pgsize) == -1) + err(1, "mlock %s", filename); + + printf(" CHECKING RESIDENCY\n"); + + if (check_residency(addr, npgs) != npgs) { + printf(" RESIDENCY CHECK FAILED!\n"); + ecode = 1; + } + + printf(" UNLOCKING RANGE\n"); + + if (munlock(addr, pgsize) == -1) + err(1, "munlock %s", filename); + + (void) munmap(addr, st.st_size); + + /* + * TEST MLOCKALL'ING AN ANONYMOUS MEMORY RANGE. + */ + + npgs = 8; + + printf(">>> MAPPING %d PAGE ANONYMOUS REGION <<<\n", npgs); + + addr = mmap(NULL, npgs * pgsize, PROT_READ|PROT_WRITE, + MAP_ANON|MAP_PRIVATE, -1, (off_t) 0); + if (addr == MAP_FAILED) + err(1, "mmap anon #1"); + + printf(" CHECKING RESIDENCY\n"); + + (void) check_residency(addr, npgs); + + printf(" LOCKING ALL - CURRENT and FUTURE\n"); + + if (mlockall(MCL_CURRENT|MCL_FUTURE) == -1) + err(1, "mlockall current/future"); + + printf(" CHECKING RESIDENCY\n"); + + if (check_residency(addr, npgs) != npgs) { + printf(" RESIDENCY CHECK FAILED!\n"); + ecode = 1; + } + + printf(">>> MAPPING ANOTHER %d PAGE ANONYMOUS REGION <<<\n", npgs); + + addr2 = mmap(NULL, npgs * pgsize, PROT_READ, MAP_ANON, -1, (off_t) 0); + if (addr == MAP_FAILED) + err(1, "mmap anon #2"); + + printf(" CHECKING RESIDENCY\n"); + + if (check_residency(addr2, npgs) != npgs) { + printf(" RESIDENCY CHECK FAILED!\n"); + ecode = 1; + } + + printf(" UNLOCKING ALL\n"); + + (void) munlockall(); + + exit(ecode); +} + +int +check_residency(addr, npgs) + void *addr; + int npgs; +{ + char *vec; + int i, resident; + + vec = malloc(npgs); + if (vec == NULL) + err(1, "malloc mincore vec"); + + if (mincore(addr, npgs * pgsize, vec) == -1) + err(1, "mincore"); + + for (i = 0, resident = 0; i < npgs; i++) { + if (vec[i] != 0) + resident++; + if (verbose) + printf("page 0x%lx is %sresident\n", + addr + (i * pgsize), vec[i] ? "" : "not "); + } + + free(vec); + + return (resident); +} + +void +usage() +{ + extern const char *__progname; + + fprintf(stderr, "usage: %s [-v] filename\n", __progname); + exit(1); +}