1#!/bin/sh 2 3# 4# Copyright (c) 2012 Peter Holm <pho@FreeBSD.org> 5# All rights reserved. 6# 7# Redistribution and use in source and binary forms, with or without 8# modification, are permitted provided that the following conditions 9# are met: 10# 1. Redistributions of source code must retain the above copyright 11# notice, this list of conditions and the following disclaimer. 12# 2. Redistributions in binary form must reproduce the above copyright 13# notice, this list of conditions and the following disclaimer in the 14# documentation and/or other materials provided with the distribution. 15# 16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26# SUCH DAMAGE. 27# 28 29[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 30 31# Unmapped I/O test scenario: 32# http://people.freebsd.org/~pho/stress/log/kostik515.txt 33 34. ../default.cfg 35 36here=`pwd` 37cd /tmp 38sed '1,/^EOF/d' < $here/$0 > rot.c 39mycc -o rot -Wall -Wextra -O2 -g rot.c || exit 1 40rm -f rot.c 41cd $here 42 43mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint 44mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart 45 46mdconfig -a -t swap -s 2g -u $mdstart || exit 1 47bsdlabel -w md$mdstart auto 48 49newfs $newfs_flags md${mdstart}$part > /dev/null 50 51mount /dev/md${mdstart}$part $mntpoint 52chmod 777 $mntpoint 53 54(cd $mntpoint; /tmp/rot) 55(cd `dirname $diskimage`; /tmp/rot) 56 57while mount | grep $mntpoint | grep -q /dev/md; do 58 umount $mntpoint || sleep 1 59done 60mdconfig -d -u $mdstart 61rm -f /tmp/rot 62exit 0 63EOF 64#include <err.h> 65#include <fcntl.h> 66#include <stdio.h> 67#include <stdlib.h> 68#include <sys/mman.h> 69#include <sys/wait.h> 70#include <time.h> 71#include <unistd.h> 72 73#define N 10240 /* 40 Mb */ 74#define PARALLEL 20 75#define RUNTIME (60 * 15) 76 77int 78test(void) 79{ 80 int fd, i, j, s; 81 unsigned char *buf; 82 char path[128]; 83 84 s = getpagesize(); 85 86 sprintf(path,"%s.%05d", getprogname(), getpid()); 87 if ((fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0) 88 err(1, "open(%s)", path); 89 if (lseek(fd, s * N - 1, SEEK_SET) == -1) 90 err(1, "lseek error"); 91 92 /* write a dummy byte at the last location */ 93 if (write(fd, "", 1) != 1) 94 err(1, "write error"); 95 96 for (i = 0; i < N; i++) { 97 if ((buf = mmap(0, s, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 98 i * s)) == MAP_FAILED) 99 err(1, "write map"); 100 for (j = 0; j < s; j++) 101 buf[j] = i % 256; 102 } 103 if (munmap(buf, s) == -1) 104 err(1, "unmap (write)"); 105 close(fd); 106 107 if ((fd = open(path, O_RDONLY)) < 0) 108 err(1, "open(%s)", path); 109 for (i = 0; i < N; i++) { 110 if ((buf = mmap(0, s, PROT_READ, MAP_SHARED, fd, i * s)) == 111 MAP_FAILED) 112 err(1, "write map"); 113 for (j = 0; j < s; j++) 114 if (buf[j] != i % 256) 115 fprintf(stderr, "read %d, expected %d at %d\n", 116 buf[j], i % 256, i); 117 } 118 if (munmap(buf, s) == -1) 119 err(1, "unmap (read)"); 120 close(fd); 121 unlink(path); 122 123 _exit(0); 124} 125 126int 127main(void) 128{ 129 time_t start; 130 int i; 131 132 start = time(NULL); 133 while (time(NULL) - start < RUNTIME) { 134 for (i = 0; i < PARALLEL; i++) 135 if (fork() == 0) 136 test(); 137 for (i = 0; i < PARALLEL; i++) 138 wait(NULL); 139 } 140 141 return(0); 142} 143