xref: /freebsd/tools/test/stress2/misc/rot.sh (revision 9d54812421274e490dc5f0fe4722ab8d35d9b258)
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
47
48newfs $newfs_flags md$mdstart > /dev/null
49
50mount /dev/md$mdstart $mntpoint
51chmod 777 $mntpoint
52
53(cd $mntpoint; /tmp/rot)
54(cd `dirname $diskimage`; /tmp/rot)
55
56while mount | grep $mntpoint | grep -q /dev/md; do
57	umount $mntpoint || sleep 1
58done
59mdconfig -d -u $mdstart
60rm -f /tmp/rot
61exit 0
62EOF
63#include <err.h>
64#include <fcntl.h>
65#include <stdio.h>
66#include <stdlib.h>
67#include <sys/mman.h>
68#include <sys/wait.h>
69#include <time.h>
70#include <unistd.h>
71
72#define N 10240	/* 40 Mb */
73#define PARALLEL 20
74#define RUNTIME (60 * 15)
75
76int
77test(void)
78{
79	int fd, i, j, s;
80	unsigned char *buf;
81	char path[128];
82
83	s = getpagesize();
84
85	sprintf(path,"%s.%05d", getprogname(), getpid());
86	if ((fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
87		err(1, "open(%s)", path);
88	if (lseek(fd, s * N - 1, SEEK_SET) == -1)
89                        err(1, "lseek error");
90
91	/* write a dummy byte at the last location */
92	if (write(fd, "", 1) != 1)
93		err(1, "write error");
94
95	for (i = 0; i < N; i++) {
96		if ((buf = mmap(0, s, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
97		    i * s)) == MAP_FAILED)
98			err(1, "write map");
99		for (j = 0; j < s; j++)
100			buf[j] = i % 256;
101	}
102	if (munmap(buf, s) == -1)
103		err(1, "unmap (write)");
104	close(fd);
105
106	if ((fd = open(path, O_RDONLY)) < 0)
107		err(1, "open(%s)", path);
108	for (i = 0; i < N; i++) {
109		if ((buf = mmap(0, s, PROT_READ, MAP_SHARED, fd, i * s)) ==
110		    MAP_FAILED)
111			err(1, "write map");
112		for (j = 0; j < s; j++)
113			if (buf[j] != i % 256)
114				fprintf(stderr, "read %d, expected %d at %d\n",
115						buf[j], i % 256, i);
116	}
117	if (munmap(buf, s) == -1)
118		err(1, "unmap (read)");
119	close(fd);
120	unlink(path);
121
122	_exit(0);
123}
124
125int
126main(void)
127{
128	time_t start;
129	int i;
130
131	start = time(NULL);
132	while (time(NULL) - start < RUNTIME) {
133		for (i = 0; i < PARALLEL; i++)
134			if (fork() == 0)
135				test();
136		for (i = 0; i < PARALLEL; i++)
137			wait(NULL);
138	}
139
140	return(0);
141}
142