xref: /freebsd/tools/test/stress2/misc/laundry.sh (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1#!/bin/sh
2
3#
4# Copyright (c) 2017 Dell EMC Isilon
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# vm.stats.vm.v_laundry_count test. WiP. No problems seen.
30
31. ../default.cfg
32[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
33
34dir=/tmp
35odir=`pwd`
36cd $dir
37sed '1,/^EOF/d' < $odir/$0 > $dir/laundry.c
38mycc -o laundry -Wall -Wextra -O0 -g laundry.c || exit 1
39rm -f laundry.c
40cd $odir
41
42size=`sysctl -n hw.usermem`
43swaptotal=`sysctl -n vm.swap_total`
44[ $swaptotal -eq 0 ] && exit 0
45[ $size -gt $swaptotal ] && size=$swaptotal
46size=$((size / 10 * 8))
47set -e
48mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
49[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
50mdconfig -a -t swap -s 1g -u $mdstart
51newfs $newfs_flags md$mdstart > /dev/null
52mount /dev/md$mdstart $mntpoint
53set +e
54
55cd $mntpoint
56$dir/laundry $size
57s=$?
58[ -f laundry.core -a $s -eq 0 ] &&
59    { ls -l laundry.core; s=1; }
60cd $odir
61
62for i in `jot 6`; do
63	mount | grep -q "on $mntpoint " || break
64	umount $mntpoint && break || sleep 10
65done
66[ $i -eq 6 ] && exit 1
67mdconfig -d -u $mdstart
68rm -rf $dir/laundry
69exit $s
70
71EOF
72#include <sys/param.h>
73#include <sys/mman.h>
74#include <sys/stat.h>
75#include <sys/wait.h>
76
77#include <machine/atomic.h>
78
79#include <err.h>
80#include <errno.h>
81#include <fcntl.h>
82#include <stdio.h>
83#include <stdlib.h>
84#include <time.h>
85#include <unistd.h>
86
87static volatile u_int *share;
88size_t size;
89
90#define PARALLEL 4
91#define RUNTIME (5 * 60)
92#define SYNC 0
93
94static void
95test(void)
96{
97	size_t i, sz;
98	time_t start;
99	int n;
100	char *cp;
101
102	atomic_add_int(&share[SYNC], 1);
103	while (share[SYNC] != PARALLEL)
104		;
105
106	sz = size;
107	if ((cp = mmap(0, sz, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0)) ==
108		(caddr_t) - 1)
109		err(1, "mmap size %zd", sz);
110
111	n = 0;
112	start = time(NULL);
113	while ((time(NULL) - start) < RUNTIME) {
114		n++;
115		n = n & 0xff;
116		for (i = 0; i < sz; i += PAGE_SIZE)
117			cp[i] = n;
118		usleep(100);
119	}
120	munmap(cp, sz);
121
122	_exit(0);
123}
124
125int
126main(int argc __unused, char *argv[])
127{
128	pid_t pids[PARALLEL];
129	size_t len;
130	time_t start;
131	int e, status;
132	u_int i;
133
134	sscanf(argv[1], "%zd", &size);
135	size /= PARALLEL;
136	e = 0;
137	len = PAGE_SIZE;
138	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
139	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
140		err(1, "mmap");
141
142	start = time(NULL);
143	while ((time(NULL) - start) < RUNTIME && e == 0) {
144		share[SYNC] = 0;
145		for (i = 0; i < PARALLEL; i++) {
146			if ((pids[i] = fork()) == 0)
147				test();
148			if (pids[i] == -1)
149				err(1, "fork()");
150		}
151		for (i = 0; i < PARALLEL; i++) {
152			if (waitpid(pids[i], &status, 0) == -1)
153				err(1, "waitpid(%d)", pids[i]);
154			if (status != 0) {
155				if (WIFEXITED(status)) {
156					printf("exited, status=%d\n",
157					    WEXITSTATUS(status));
158				} else if (WIFSIGNALED(status)) {
159					printf("killed by signal %d\n",
160					    WTERMSIG(status));
161				} else if (WIFSTOPPED(status)) {
162					printf("stopped by signal %d\n",
163					    WSTOPSIG(status));
164				} else if (WIFCONTINUED(status)) {
165					printf("continued\n");
166				}
167				fprintf(stderr, "pid %d exit code %d\n",
168						pids[i], status);
169			}
170			e += status == 0 ? 0 : 1;
171		}
172	}
173
174	return (e);
175}
176
177