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