Add some pretty simple tests for various memory mapping facilities.

There's lots more work to do here, but this simple program found a
couple of problems which I've fixed recently.
This commit is contained in:
thorpej 1999-06-19 00:53:31 +00:00
parent 49aa92dfc0
commit 03826522e4
4 changed files with 247 additions and 2 deletions

View File

@ -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

5
regress/sys/uvm/Makefile Normal file
View File

@ -0,0 +1,5 @@
# $NetBSD: Makefile,v 1.1 1999/06/19 00:53:32 thorpej Exp $
SUBDIR+= mmap
.include <bsd.subdir.mk>

View File

@ -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 <bsd.prog.mk>

224
regress/sys/uvm/mmap/mmap.c Normal file
View File

@ -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 <sys/param.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
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);
}