From owner-svn-src-projects@FreeBSD.ORG Wed Feb 18 19:02:52 2009 Return-Path: Delivered-To: svn-src-projects@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 11F3A1065676; Wed, 18 Feb 2009 19:02:52 +0000 (UTC) (envelope-from pho@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id F307A8FC1E; Wed, 18 Feb 2009 19:02:51 +0000 (UTC) (envelope-from pho@FreeBSD.org) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id n1IJ2pPB072177; Wed, 18 Feb 2009 19:02:51 GMT (envelope-from pho@svn.freebsd.org) Received: (from pho@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id n1IJ2ppm072175; Wed, 18 Feb 2009 19:02:51 GMT (envelope-from pho@svn.freebsd.org) Message-Id: <200902181902.n1IJ2ppm072175@svn.freebsd.org> From: Peter Holm Date: Wed, 18 Feb 2009 19:02:51 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org X-SVN-Group: projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r188758 - projects/stress2/misc X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Feb 2009 19:02:52 -0000 Author: pho Date: Wed Feb 18 19:02:51 2009 New Revision: 188758 URL: http://svn.freebsd.org/changeset/base/188758 Log: Test scenarios for the mmap and other process address space manipulation functions. Approved by: kib Added: projects/stress2/misc/mmap2.sh (contents, props changed) projects/stress2/misc/mmap3.sh (contents, props changed) Added: projects/stress2/misc/mmap2.sh ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ projects/stress2/misc/mmap2.sh Wed Feb 18 19:02:51 2009 (r188758) @@ -0,0 +1,176 @@ +#!/bin/sh + +# +# Copyright (c) 2009 Peter Holm +# All rights reserved. +# +# 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. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. +# +# $FreeBSD$ +# + +# Stress mmap by having at most 100 threads mapping random areas within +# a 100 Mb range. + +# Test scenario by kib@ + +odir=`pwd` +cd /tmp +sed '1,/^EOF/d' < $odir/$0 > mmap2.c +cc -o mmap2 -Wall mmap2.c -lpthread +rm -f mmap2.c + +for i in `jot 10`; do + ./mmap2 +done +rm -f ./mmap2 +exit + +EOF +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define THREADS 100 +#define STARTADDR 0x50000000U +#define ADRSPACE 0x06400000U /* 100 Mb */ + +static void +work(int nr) +{ + int fd, m; + void *p; + size_t left, len; + char path[128]; + + p = (void *)STARTADDR + trunc_page(arc4random() % ADRSPACE); + left = ADRSPACE - (size_t)p + STARTADDR; + len = trunc_page(arc4random() % left) + PAGE_SIZE; + fd = -1; + + if (arc4random() % 100 < 90) + sprintf(path, "/tmp/mmap.%06d.%04d", getpid(), nr); + else + sprintf(path, "/dev/zero"); + if (arc4random() % 2 == 0) { + if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0622)) == -1) + err(1,"open()"); + if (ftruncate(fd, len) == -1) + err(1, "ftruncate"); + if (arc4random() % 2 == 0) { + if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == + MAP_FAILED) { + if (errno == ENOMEM) + return; + err(1, "mmap()"); + } + } else { + if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) == + MAP_FAILED) { + if (errno == ENOMEM) + return; + err(1, "mmap()"); + } + } + if (fd > 0 && strcmp(path, "/dev/zero")) + if (unlink(path) == -1) + err(1, "unlink(%s)", path); + } else { + if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0)) == MAP_FAILED) { + if (errno == ENOMEM) + return; + err(1, "mmap()"); + } + strcpy(path, "anon"); + } +#if 0 + printf("nr = %d, %-14s, start = %p, end = %p, len = 0x%08x, (%5d pages)\n", + nr, path, p, p + len, len, len>>PAGE_SHIFT); +#endif + + *(int *)p = 1; + + if (arc4random() % 2 == 0) { + m = arc4random() % 10; + if (madvise(p, len, m) == -1) + warn("madvise(%p, %d, %d)", p, len, m); + } + if (arc4random() %2 == 0) + if (mprotect(p, trunc_page(arc4random() % len), PROT_READ) == -1 ) + err(1, "mprotect failed with error:"); + if (arc4random() % 2 == 0) { + if (arc4random() %2 == 0) { + if (msync(p, 0, MS_SYNC) == -1) + err(1, "msync(%p)", p); + } else { + if (msync(p, 0, MS_INVALIDATE) == -1) + err(1, "msync(%p)", p); + } + } + if (munmap(p, len) == -1) + err(1, "munmap(%p)", p); + close(fd); +} + +void * +thr(void *arg) +{ + int i; + + for (i = 0; i < 512; i++) { + work(*(int *)arg); + } + return (0); +} + +int +main(int argc, char **argv) +{ + pthread_t threads[THREADS]; + int nr[THREADS]; + int i, n, r; + +// printf("Address start 0x%x, address end 0x%x, pages %d\n", +// STARTADDR, STARTADDR + ADRSPACE, ADRSPACE>>PAGE_SHIFT); + n = arc4random() % THREADS + 1; + for (i = 0; i < n; i++) { + nr[i] = i; + if ((r = pthread_create(&threads[i], NULL, thr, (void *)&nr[i])) != 0) + err(1, "pthread_create(): %s\n", strerror(r)); + } + + for (i = 0; i < n; i++) { + if (pthread_join(threads[i], NULL) != 0) + err(1, "pthread_join(%d)", i); + } + + return (0); +} Added: projects/stress2/misc/mmap3.sh ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ projects/stress2/misc/mmap3.sh Wed Feb 18 19:02:51 2009 (r188758) @@ -0,0 +1,159 @@ +#!/bin/sh + +# +# Copyright (c) 2009 Peter Holm +# All rights reserved. +# +# 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. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. +# +# $FreeBSD$ +# + +# Variation of mmap2.sh with focus on random arguments for mprotect() + +odir=`pwd` +cd /tmp +sed '1,/^EOF/d' < $odir/$0 > mmap3.c +cc -o mmap3 -Wall mmap3.c -lpthread +rm -f mmap3.c + +for i in `jot 20`; do + ./mmap3 +done +for i in `jot 100`; do + ./mmap3 random +done +rm -f mmap3 mmap3.core /tmp/mmap.0* +exit + +EOF +/* + Stress mmap by having max 18 threads mapping random areas within + a 100 Mb range. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define THREADS 100 +#define STARTADDR 0x50000000U +#define ADRSPACE 0x06400000U /* 100 Mb */ + +static int +ra; + +void +trash(void *p) +{ + mprotect(p, 0x570e3d38, 0x2c8fd54f); + + if (ra) { + madvise((void *)arc4random(), arc4random(), arc4random()); + mprotect((void *)arc4random(), arc4random(), arc4random()); + msync((void *)arc4random(), arc4random(), arc4random()); + } + +} + +void +work(int nr) +{ + int fd, m; + void *p; + size_t len; + char path[128]; + + p = (void *)STARTADDR; + len = ADRSPACE; + + sprintf(path, "/tmp/mmap.%06d.%04d", getpid(), nr); + if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0622)) == -1) + err(1,"open()"); + if (ftruncate(fd, len) == -1) + err(1, "ftruncate"); + if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == + MAP_FAILED) { + if (errno == ENOMEM) + return; + err(1, "mmap()"); + } + if (unlink(path) == -1) + err(1, "unlink(%s)", path); + + trash(p); + + m = arc4random() % 10; + if (madvise(p, len, m) == -1) + warn("madvise(%p, %d, %d)", p, len, m); + if (mprotect(p, trunc_page(arc4random() % len), PROT_READ) == -1 ) + err(1, "mprotect failed with error:"); + if (msync(p, 0, MS_SYNC) == -1) + err(1, "msync(%p)", p); + if (munmap(p, len) == -1) + err(1, "munmap(%p)", p); + close(fd); +} + +void * +thr(void *arg) +{ + int i; + + for (i = 0; i < 512; i++) { + work(*(int *)arg); + } + return (0); +} + +int +main(int argc, char **argv) +{ + pthread_t threads[THREADS]; + int nr[THREADS]; + int i, n, r; + + n = arc4random() % 14 + 5; + ra = argc != 1; +// printf("Address start 0x%x, address end 0x%x, pages %d, n %d\n", +// STARTADDR, STARTADDR + ADRSPACE, ADRSPACE>>PAGE_SHIFT, n); + for (i = 0; i < n; i++) { + nr[i] = i; + if ((r = pthread_create(&threads[i], NULL, thr, (void *)&nr[i])) != 0) + err(1, "pthread_create(): %s\n", strerror(r)); + } + + for (i = 0; i < n; i++) { + if (pthread_join(threads[i], NULL) != 0) + err(1, "pthread_join(%d)", i); + } + + return (0); +}