xref: /freebsd/tools/test/stress2/misc/sendfile20.sh (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# "panic: vm_page_assert_xbusied: page not exclusive busy" seen:
31# https://people.freebsd.org/~pho/stress/log/sendfile20.txt
32# Fixed by r359778
33
34. ../default.cfg
35
36[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
37[ -z "$nfs_export" ] && exit 0
38ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
39    exit 0
40
41odir=`pwd`
42cd /tmp
43sed '1,/^EOF/d' < $odir/$0 > sendfile20.c
44mycc -o sendfile20 -Wall sendfile20.c -pthread || exit 1
45rm -f sendfile20.c
46
47(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -l 100) &
48sleep 5
49mount | grep "$mntpoint" | grep -q nfs && umount $mntpoint
50mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mntpoint
51
52cd $mntpoint
53parallel=2000
54for i in `jot $parallel`; do
55	size=`jot -r 1 1 $((1024 * 1024))`
56	dd if=/dev/zero of=input.$i bs=$size count=1 status=none
57done
58cd $odir
59while mount | grep "$mntpoint " | grep -q nfs; do
60	umount -f $mntpoint
61done
62sleep 1
63mount -t nfs -o tcp -o retrycnt=3 -o intr,soft -o rw $nfs_export $mntpoint
64spid=$!
65cd $mntpoint
66pids=
67for i in `jot $parallel`; do
68	/tmp/sendfile20 input.$i output.$i 1234$i &
69	pids="$pids $!"
70done
71sleep 30
72while pkill sendfile20; do :; done
73for p in $pids; do
74	wait $p
75done
76for i in `jot $parallel`; do
77#	cmp input.$i output.$i || { echo FAIL; ls -l input$i output$i; }
78	rm -f input.$i output.$i
79done
80while pkill swap; do :; done
81wait $spid
82
83cd $odir
84umount $mntpoint
85while mount | grep "$mntpoint " | grep -q nfs; do
86	umount -f $mntpoint
87done
88rm -f /tmp/sendfile20
89exit 0
90EOF
91#include <err.h>
92#include <fcntl.h>
93#include <netdb.h>
94#include <netinet/in.h>
95#include <signal.h>
96#include <stdio.h>
97#include <stdlib.h>
98#include <string.h>
99#include <sys/param.h>
100#include <sys/socket.h>
101#include <sys/stat.h>
102#include <unistd.h>
103
104int port;
105char *inputFile;
106char *outputFile;
107int bufsize = 4096;
108
109static void
110reader(void) {
111	int tcpsock, msgsock;
112	int on;
113	socklen_t len;
114	struct sockaddr_in inetaddr, inetpeer;
115	int n, t, *buf, fd;
116
117	on = 1;
118	if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
119		err(1, "socket(), %s:%d", __FILE__, __LINE__);
120
121	if (setsockopt(tcpsock,
122	    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
123		err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
124
125	inetaddr.sin_family = AF_INET;
126	inetaddr.sin_addr.s_addr = INADDR_ANY;
127	inetaddr.sin_port = htons(port);
128	inetaddr.sin_len = sizeof(inetaddr);
129
130	if (bind(tcpsock,
131	    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
132		err(1, "bind(), %s:%d", __FILE__, __LINE__);
133
134	if (listen(tcpsock, 5) < 0)
135		err(1, "listen(), %s:%d", __FILE__, __LINE__);
136
137	len = sizeof(inetpeer);
138	if ((msgsock = accept(tcpsock,
139	    (struct sockaddr *)&inetpeer, &len)) < 0)
140		err(1, "accept(), %s:%d", __FILE__, __LINE__);
141
142	t = 0;
143	if ((buf = malloc(bufsize)) == NULL)
144		err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
145
146	if ((fd = open(outputFile, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
147		err(1, "open(%s)", outputFile);
148
149	for (;;) {
150		if ((n = read(msgsock, buf, bufsize)) < 0)
151			err(1, "read(), %s:%d", __FILE__, __LINE__);
152		t += n;
153		if (n == 0)
154			break;
155
156		if ((write(fd, buf, n)) != n)
157			err(1, "write");
158	}
159	close(msgsock);
160	close(fd);
161	return;
162}
163
164static void
165writer(void) {
166	int tcpsock, on;
167	struct sockaddr_in inetaddr;
168	struct hostent *hostent;
169	struct stat statb;
170	int i, r, fd;
171	off_t off = 0;
172#if 1
173	size_t size;
174#endif
175
176	on = 1;
177	for (i = 1; i < 5; i++) {
178		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
179			err(1, "socket(), %s:%d", __FILE__, __LINE__);
180
181		if (setsockopt(tcpsock,
182		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
183			err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
184
185#if 1		/* livelock trigger */
186		size = getpagesize() -4;
187		if (setsockopt(tcpsock, SOL_SOCKET, SO_SNDBUF, (void *)&size,
188		    sizeof(size)) < 0)
189			err(1, "setsockopt(SO_SNDBUF), %s:%d",
190			    __FILE__, __LINE__);
191#endif
192
193		hostent = gethostbyname ("localhost");
194		memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
195			sizeof (struct in_addr));
196
197		inetaddr.sin_family = AF_INET;
198		inetaddr.sin_port = htons(port);
199		inetaddr.sin_len = sizeof(inetaddr);
200
201		r = connect(tcpsock, (struct sockaddr *) &inetaddr,
202			sizeof(inetaddr));
203		if (r == 0)
204			break;
205		sleep(1);
206		close(tcpsock);
207	}
208	if (r < 0)
209		err(1, "connect(), %s:%d", __FILE__, __LINE__);
210
211        if (stat(inputFile, &statb) != 0)
212                err(1, "stat(%s)", inputFile);
213
214	if ((fd = open(inputFile, O_RDONLY)) == -1)
215		err(1, "open(%s)", inputFile);
216
217	if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, SF_NOCACHE) == -1)
218		err(1, "sendfile");
219
220	return;
221}
222
223int
224main(int argc, char **argv)
225{
226	pid_t pid;
227
228	if (argc != 4) {
229		fprintf(stderr, "Usage: %s <inputFile outputFile portNumber\n",
230		    argv[0]);
231		return (1);
232	}
233	inputFile = argv[1];
234	outputFile = argv[2];
235	port = atoi(argv[3]);
236
237	if ((pid = fork()) == 0) {
238		writer();
239		exit(EXIT_SUCCESS);
240	} else if (pid > 0) {
241		reader();
242		kill(pid, SIGINT);
243	} else
244		err(1, "fork(), %s:%d",  __FILE__, __LINE__);
245
246	return (0);
247}
248