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