xref: /freebsd/tools/test/stress2/misc/write.sh (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1#!/bin/sh
2
3#
4# Copyright (c) 2016 EMC Corp.
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# Parallel write test.
30
31# fsync: giving up on dirty
32# 0xc9172168: tag devfs, type VCHR
33#     usecount 1, writecount 0, refcount 31 mountedhere 0xc96bd300
34#     flags (VI_ACTIVE)
35#     v_object 0xc96e55a0 ref 0 pages 983 cleanbuf 27 dirtybuf 2
36#     lock type devfs: EXCL by thread 0xcbedf340 (pid 58752, write, tid 100254)
37# #0 0xc0c23f33 at __lockmgr_args+0xae3
38# #1 0xc0cff943 at vop_stdlock+0x53
39# #2 0xc12027c8 at VOP_LOCK1_APV+0x118
40# #3 0xc0d2400a at _vn_lock+0xba
41# #4 0xc0f3288b at ffs_sync+0x34b
42# #5 0xc0f16cc5 at softdep_ast_cleanup_proc+0x205
43# #6 0xc0ca3ac7 at userret+0x37
44# #7 0xc11d021e at syscall+0x50e
45# #8 0xc11bae3f at Xint0x80_syscall+0x2f
46#         dev label/tmp
47
48# Deadlock seen:
49# https://people.freebsd.org/~pho/stress/log/write.txt
50
51. ../default.cfg
52
53dir=/tmp
54odir=`pwd`
55cd $dir
56sed '1,/^EOF/d' < $odir/$0 > $dir/write.c
57mycc -o write -Wall -Wextra -O2 -g write.c || exit 1
58rm -f write.c
59cd $odir
60
61need=$((25 * 1024))
62[ `df -k $(dirname $diskimage) | tail -1 | awk '{print int($4 / 1024)}'` \
63    -lt $need ] &&
64    printf "Need %d MB on %s.\n" $need `dirname $diskimage` && exit 0
65wd=`dirname $diskimage`
66wd="$wd/write.dir"
67rm -rf $wd
68mkdir -p $wd
69
70(cd $wd; /tmp/write)
71s=$?
72
73rm -rf /tmp/write $wd
74exit $s
75
76EOF
77#include <sys/param.h>
78#include <sys/mman.h>
79#include <sys/stat.h>
80#include <sys/wait.h>
81
82#include <machine/atomic.h>
83
84#include <err.h>
85#include <errno.h>
86#include <fcntl.h>
87#include <stdio.h>
88#include <stdlib.h>
89#include <time.h>
90#include <unistd.h>
91
92#define DONE 1
93#define MAXBLK (2 * 1024 * 1024)
94#define MAXPROC 32
95#define MAXSIZ (9LL * 1024 * 1024 *1024)
96#define RUNTIME (15 * 60)
97#define SYNC 0
98
99volatile u_int *share;
100int parallel;
101
102struct typ {
103	off_t blocksize;
104	off_t blocks;
105	int sequential;
106	int mindelay;
107	int maxdelay;
108} t[MAXPROC];
109
110int
111rnd(int mi, int ma)
112{
113        return (arc4random()  % (ma - mi + 1) + mi);
114}
115
116void
117test(int indx, int num)
118{
119	ssize_t i, r;
120	time_t start;
121	int fd, n;
122	char *buf, file[80];
123
124	atomic_add_int(&share[SYNC], 1);
125	while (share[SYNC] != (unsigned int)parallel)
126		;
127
128	if ((buf = malloc(t[indx].blocksize)) == NULL)
129		err(1, "malloc");
130	snprintf(file, sizeof(file), "file.%06d.%06d", indx, num);
131	n = 0;
132	start = time(NULL);
133	while (share[DONE] != (unsigned int)parallel) {
134		setproctitle("test(%d) num %d, n %d", indx, num, n);
135		if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE))
136		    == -1)
137			err(1, "open(%s)", file);
138
139		for (i = 0; i < t[indx].blocks; i++) {
140			if (t[indx].sequential == 0)
141				if (lseek(fd, 2LL << (arc4random() % 18),
142				    SEEK_SET) == -1)
143					err(1, "lseek");
144			if ((r = write(fd, buf, t[indx].blocksize)) !=
145			    t[indx].blocksize) {
146				warn("write returned %zd\n", r);
147				goto done;
148			}
149			usleep(rnd(t[indx].mindelay, t[indx].maxdelay));
150		}
151
152		close(fd);
153		if (n++ == 0)
154			atomic_add_int(&share[DONE], 1);
155		if (time(NULL) - start >= RUNTIME / 4) {
156#if defined(DEBUG)
157			fprintf(stderr, "test(%d), %d Timed out\n", indx, num);
158#endif
159			break;
160		}
161	}
162done:
163	if (n++ == 0)
164		atomic_add_int(&share[DONE], 1);
165
166	_exit(0);
167}
168
169void
170setup(void)
171{
172	int i;
173
174	parallel = arc4random() % MAXPROC + 1;
175	for (i = 0; i < parallel; i++) {
176		if (arc4random() % 100 < 10)
177			t[i].blocksize = (arc4random() + 1) % MAXBLK;
178		else
179			t[i].blocksize = 2 << (arc4random() % 20);
180		t[i].sequential = arc4random() % 2;
181		t[i].mindelay = arc4random() % 50;
182		t[i].maxdelay = t[i].mindelay + arc4random() % 100;
183		t[i].blocks = 2LL << (arc4random() % 18);
184		if (t[i].blocks * t[i].blocksize > MAXSIZ)
185			t[i].blocks = MAXSIZ / t[i].blocksize;
186#if defined(DEBUG)
187		fprintf(stderr, "%3d: blocksize %7lld, sequential %d, "
188		    "mindelay %3d, maxdelay %3d, blocks %6lld, size %4lld MB\n",
189		    i, (long long)t[i].blocksize, t[i].sequential, t[i].mindelay,
190		    t[i].maxdelay, (long long)t[i].blocks,
191		    (long long)t[i].blocksize * t[i].blocks / 1024 / 1024);
192#endif
193	}
194}
195
196int
197main(void)
198{
199	size_t len;
200	time_t start;
201	int e, i, n, *pids, status;
202
203	e = 0;
204	len = PAGE_SIZE;
205	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
206	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
207		err(1, "mmap");
208
209	n = 0;
210	start = time(NULL);
211	while ((time(NULL) - start) < RUNTIME && e == 0) {
212		setup();
213
214		pids = malloc(sizeof(pid_t) * parallel);
215		share[SYNC] = share[DONE] = 0;
216		for (i = 0; i < parallel; i++) {
217			if ((pids[i] = fork()) == 0)
218				test(i, n);
219		}
220		for (i = 0; i < parallel; i++) {
221			if (waitpid(pids[i], &status, 0) != pids[i])
222				err(1, "waitpid %d", pids[i]);
223			e += status == 0 ? 0 : 1;
224		}
225		n++;
226		n = n % 10;
227		free(pids);
228	}
229
230	return (e);
231}
232