xref: /freebsd/tools/test/stress2/misc/sendfile16.sh (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1#!/bin/sh
2
3#
4# Copyright (c) 2018 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 unmodified, this list of conditions and the following
12#    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# sendfile() test.
31# Copy of sendfile15.sh
32
33. ../default.cfg
34[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
35
36dir=/tmp
37odir=`pwd`
38cd $dir
39sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile16.c
40mycc -o sendfile16 -Wall -Wextra -O0 -g sendfile16.c || exit 1
41rm -f sendfile16.c
42cd $odir
43
44set -e
45mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
46[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
47mdconfig -a -t swap -s 2g -u $mdstart
48newfs $newfs_flags md$mdstart > /dev/null
49mount /dev/md$mdstart $mntpoint
50set +e
51
52cd $mntpoint
53dd if=/dev/random of=input bs=4k count=1 status=none
54export MAXSWAPPCT=85
55(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -l 100 > /dev/null) &
56sleep 5
57$dir/sendfile16
58s=$?
59while pgrep -q swap; do
60	pkill swap
61done
62wait
63[ -f sendfile16.core -a $s -eq 0 ] &&
64    { ls -l sendfile16.core; mv sendfile16.core $dir; s=1; }
65cd $odir
66
67for i in `jot 6`; do
68	mount | grep -q "on $mntpoint " || break
69	umount $mntpoint && break || sleep 10
70	[ $i -eq 6 ] &&
71	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
72done
73mdconfig -d -u $mdstart
74rm -rf $dir/sendfile16
75exit $s
76
77EOF
78#include <sys/param.h>
79#include <sys/fcntl.h>
80#include <sys/mman.h>
81#include <sys/socket.h>
82#include <sys/stat.h>
83#include <sys/stat.h>
84#include <sys/uio.h>
85#include <sys/wait.h>
86
87#include <machine/atomic.h>
88
89#include <err.h>
90#include <errno.h>
91#include <fcntl.h>
92#include <stdio.h>
93#include <stdlib.h>
94#include <time.h>
95#include <unistd.h>
96
97static volatile u_int *share;
98
99#define PARALLEL 128
100#define RUNTIME (5 * 60)
101#define SYNC 0
102#define SYNC2 1
103#define TIMEOUT 180
104
105static void
106test(void)
107{
108	struct stat st;
109	off_t written, pos;
110	const char *from_name;
111	int sv[2];
112	int child, error, from, n, status;
113	char *buf;
114
115	alarm(TIMEOUT);
116	setproctitle("spinup");
117	atomic_add_int(&share[SYNC], 1);
118	while (share[SYNC] != PARALLEL)
119		usleep(100);
120	alarm(0);
121
122	from_name = "input";
123
124	if ((from = open(from_name, O_RDONLY)) == -1)
125		err(1, "open read %s", from_name);
126
127	if ((error = fstat(from, &st)) == -1)
128		err(1, "stat %s", from_name);
129
130	if ((error = socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) == -1)
131		err(1, "socketpair");
132
133	child = fork();
134	if (child == -1)
135		err(1, "fork");
136	else if (child != 0) {
137		setproctitle("parent");
138		close(sv[1]);
139		pos = 0;
140		for (;;) {
141			error = sendfile(from, sv[0], pos, st.st_size - pos,
142			    NULL, &written, 0);
143			if (error == -1) {
144				if (errno != EAGAIN)
145					err(1, "sendfile");
146			}
147			pos += written;
148			if (pos == st.st_size)
149				break;
150		}
151		close(sv[0]);
152		if (waitpid(child, &status, 0) != child)
153			err(1, "waitpid(%d)", child);
154	} else {
155		setproctitle("child");
156		close(sv[0]);
157		buf = malloc(st.st_size);
158		if (buf == NULL)
159			err(1, "malloc %jd", st.st_size);
160		pos = 0;
161		for (;;) {
162			written = st.st_size - pos;
163			n = read(sv[1], buf + pos, written);
164			if (n == -1)
165				err(1, "read");
166			else if (n == 0)
167				errx(1, "Short read");
168			pos += n;
169			if (pos == st.st_size)
170				break;
171		}
172		close(sv[1]);
173		_exit(0);
174	}
175	setproctitle("spindown");
176	n = 0;
177	atomic_add_int(&share[SYNC2], 1);
178	alarm(TIMEOUT);
179	while (share[SYNC2] != PARALLEL)
180		usleep(100000);
181
182	_exit(0);
183}
184
185int
186main(void)
187{
188	pid_t pids[PARALLEL];
189	size_t len;
190	time_t start;
191	int e, i, status;
192
193	e = 0;
194	len = PAGE_SIZE;
195	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
196	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
197		err(1, "mmap");
198
199	start = time(NULL);
200	while ((time(NULL) - start) < RUNTIME && e == 0) {
201		for (i = 0; i < PARALLEL; i++) {
202			if ((pids[i] = fork()) == 0)
203				test();
204			if (pids[i] == -1)
205				err(1, "fork()");
206		}
207		for (i = 0; i < PARALLEL; i++) {
208			if (waitpid(pids[i], &status, 0) == -1)
209				err(1, "waitpid(%d)", pids[i]);
210			if (status != 0) {
211				if (WIFSIGNALED(status))
212					fprintf(stderr,
213					    "pid %d exit signal %d\n",
214					    pids[i], WTERMSIG(status));
215			}
216			e += status == 0 ? 0 : 1;
217		}
218		share[SYNC] = 0;
219		share[SYNC2] = 0;
220	}
221
222	return (e);
223}
224