From owner-svn-src-user@FreeBSD.ORG Tue Apr 23 10:29:38 2013 Return-Path: Delivered-To: svn-src-user@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id 35FF9ABB; Tue, 23 Apr 2013 10:29:38 +0000 (UTC) (envelope-from pho@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) by mx1.freebsd.org (Postfix) with ESMTP id 283A115C0; Tue, 23 Apr 2013 10:29:38 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.6/8.14.6) with ESMTP id r3NATcxB066421; Tue, 23 Apr 2013 10:29:38 GMT (envelope-from pho@svn.freebsd.org) Received: (from pho@localhost) by svn.freebsd.org (8.14.6/8.14.5/Submit) id r3NATbrB066417; Tue, 23 Apr 2013 10:29:37 GMT (envelope-from pho@svn.freebsd.org) Message-Id: <201304231029.r3NATbrB066417@svn.freebsd.org> From: Peter Holm Date: Tue, 23 Apr 2013 10:29:37 +0000 (UTC) To: src-committers@freebsd.org, svn-src-user@freebsd.org Subject: svn commit: r249793 - user/pho/stress2/misc X-SVN-Group: user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 23 Apr 2013 10:29:38 -0000 Author: pho Date: Tue Apr 23 10:29:37 2013 New Revision: 249793 URL: http://svnweb.freebsd.org/changeset/base/249793 Log: Added some old sendfile(2) test cases. Added: user/pho/stress2/misc/sendfile2.sh (contents, props changed) user/pho/stress2/misc/sendfile3.sh (contents, props changed) user/pho/stress2/misc/sendfile4.sh (contents, props changed) user/pho/stress2/misc/sendfile5.sh (contents, props changed) Added: user/pho/stress2/misc/sendfile2.sh ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/pho/stress2/misc/sendfile2.sh Tue Apr 23 10:29:37 2013 (r249793) @@ -0,0 +1,141 @@ +#!/bin/sh + +# +# Copyright (c) 2010 Peter Holm +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ +# + +# Test scenario for sendfile corruption of read only indput file + +# Scenario by Ming Fu + +. ../default.cfg + +odir=`pwd` + +cd /tmp +sed '1,/^EOF/d' < $odir/$0 > sendfile2.c +cc -o sendfile2 -Wall sendfile2.c +rm -f sendfile2.c +[ -d "$RUNDIR" ] || mkdir -p $RUNDIR +cd $RUNDIR + +cp /usr/lib/libc_pic.a large +md1=`md5 large` + +nc -l 7000 > lf & +sleep 0.1 +/tmp/sendfile2 + +md2=`md5 large` +[ "$md1" != "$md2" ] && printf "%s\n%s\n" "$md1" "$md2" + +rm -f /tmp/sendfile2 large lf +exit +EOF +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int +main () { + int s, f; + struct sockaddr_in addr; + struct hostent *hostent; + int flags; + char str[32]="\r\n800\r\n"; + char *p = str; + struct stat sb; + int n; + fd_set wset; + int64_t size; + off_t sbytes; + off_t sent = 0; + int chunk; + + s = socket(AF_INET, SOCK_STREAM, 0); + bzero(&addr, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(7000); + hostent = gethostbyname ("localhost"); + memcpy (&addr.sin_addr.s_addr, hostent->h_addr, + sizeof (struct in_addr)); + + n = connect(s, (struct sockaddr *)&addr, sizeof (addr)); + if (n < 0) + warn ("fail to connect"); + flags = fcntl(s, F_GETFL); + flags |= O_NONBLOCK; + fcntl(s, F_SETFL); + + f = open("large", O_RDONLY); + if (f < 0) + warn("fail to open file"); + n = fstat(f, &sb); + if (n < 0) + warn("fstat failed"); + + size = sb.st_size; + chunk = 0; + while (size > 0) { + FD_ZERO(&wset); + FD_SET(s, &wset); + n = select(f+1, NULL, &wset, NULL, NULL); + if (n < 0) + continue; + if (chunk > 0) { + sbytes = 0; + n = sendfile(f, s, sent, chunk, NULL, &sbytes, 0); + if (n < 0) + continue; + chunk -= sbytes; + size -= sbytes; + sent += sbytes; + continue; + } + if (size > 2048) + chunk = 2048; + else + chunk = size; + n = sprintf(str, "\r\n%x\r\n", 2048); + p = str; + write(s, p, n); + } + + return (0); +} Added: user/pho/stress2/misc/sendfile3.sh ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/pho/stress2/misc/sendfile3.sh Tue Apr 23 10:29:37 2013 (r249793) @@ -0,0 +1,216 @@ +#!/bin/sh + +# +# Copyright (c) 2011 Peter Holm +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ +# + +# Test scenario for sendfile deadlock (processes stuck in sfbufa) + +# Variation of sendfile.sh + +. ../default.cfg + +odir=`pwd` + +cd /tmp +sed '1,/^EOF/d' < $odir/$0 > sendfile3.c +cc -o sendfile3 -Wall -Wextra sendfile3.c +rm -f sendfile3.c +[ -d "$RUNDIR" ] || mkdir -p $RUNDIR +cd $RUNDIR + +in=/tmp/inputFile +out=/tmp/outputFile +parallel=20 + +[ `sysctl kern.ipc.nsfbufs | awk '{print $NF}'` -gt 512 ] && echo "kern.ipc.nsfbufs should be low for this test" +for i in 50m 100m; do + rm -f $in + dd if=/dev/random of=$in bs=$i count=1 2>&1 | \ + egrep -v "records|transferred" + for j in `jot $parallel`; do + rm -f ${out}$j + /tmp/sendfile3 $in ${out}$j 1234$j & + done + for j in `jot $parallel`; do + wait + done + for j in `jot $parallel`; do + rm -f ${out}$j + done +done +rm -f $in /tmp/sendfile3 +exit +EOF +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int port; +char *inputFile; +char *outputFile; +int bufsize = 4096; + +static void +reader(void) { + int tcpsock, msgsock; + int on; + socklen_t len; + struct sockaddr_in inetaddr, inetpeer; + int n, t, *buf, fd; + + on = 1; + if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) + err(1, "socket(), %s:%d", __FILE__, __LINE__); + + if (setsockopt(tcpsock, + SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) + err(1, "setsockopt(), %s:%d", __FILE__, __LINE__); + + inetaddr.sin_family = AF_INET; + inetaddr.sin_addr.s_addr = INADDR_ANY; + inetaddr.sin_port = htons(port); + inetaddr.sin_len = sizeof(inetaddr); + + if (bind(tcpsock, + (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) + err(1, "bind(), %s:%d", __FILE__, __LINE__); + + if (listen(tcpsock, 5) < 0) + err(1, "listen(), %s:%d", __FILE__, __LINE__); + + len = sizeof(inetpeer); + if ((msgsock = accept(tcpsock, + (struct sockaddr *)&inetpeer, &len)) < 0) + err(1, "accept(), %s:%d", __FILE__, __LINE__); + + t = 0; + if ((buf = malloc(bufsize)) == NULL) + err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__); + + if ((fd = open(outputFile, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1) + err(1, "open(%s)", outputFile); + + for (;;) { + if ((n = read(msgsock, buf, bufsize)) < 0) + err(1, "read(), %s:%d", __FILE__, __LINE__); + t += n; + if (n == 0) break; + + if ((write(fd, buf, n)) != n) + err(1, "write"); + } + close(msgsock); + close(fd); + return; +} + +static void +writer(void) { + int tcpsock, on; + struct sockaddr_in inetaddr; + struct hostent *hostent; + struct stat statb; + int i, r, fd; + off_t off = 0; + + on = 1; + for (i = 1; i < 5; i++) { + if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) + err(1, "socket(), %s:%d", __FILE__, __LINE__); + + if (setsockopt(tcpsock, + SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) + err(1, "setsockopt(), %s:%d", __FILE__, __LINE__); + + hostent = gethostbyname ("localhost"); + memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr, + sizeof (struct in_addr)); + + inetaddr.sin_family = AF_INET; + inetaddr.sin_addr.s_addr = INADDR_ANY; + inetaddr.sin_port = htons(port); + inetaddr.sin_len = sizeof(inetaddr); + + r = connect(tcpsock, (struct sockaddr *) &inetaddr, + sizeof(inetaddr)); + if (r == 0) + break; + sleep(1); + close(tcpsock); + } + if (r < 0) + err(1, "connect(), %s:%d", __FILE__, __LINE__); + + if (stat(inputFile, &statb) != 0) + err(1, "stat(%s)", inputFile); + + if ((fd = open(inputFile, O_RDONLY)) == -1) + err(1, "open(%s)", inputFile); + + if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1) + err(1, "sendfile"); + + return; +} + +int +main(int argc, char **argv) +{ + pid_t pid; + + if (argc != 4) { + fprintf(stderr, "Usage: %s 0) { + reader(); + kill(pid, SIGINT); + waitpid(pid, NULL, 0); + } else + err(1, "fork(), %s:%d", __FILE__, __LINE__); + + return (0); +} Added: user/pho/stress2/misc/sendfile4.sh ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/pho/stress2/misc/sendfile4.sh Tue Apr 23 10:29:37 2013 (r249793) @@ -0,0 +1,134 @@ +#!/bin/sh + +# +# Copyright (c) 2011 Peter Holm +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ +# + +# sendfile(2) test by kib@ +# Both processes will sit in the sbwait state forever: + +. ../default.cfg + +here=`pwd` +cd /tmp +sed '1,/^EOF/d' < $here/$0 > sendfile4.c +cc -o sendfile4 -Wall -Wextra -O2 sendfile4.c +rm -f sendfile4.c +cd $here + +/tmp/sendfile4 /usr/libexec/cc1 + +rm -f /tmp/sendfile4 +exit +EOF +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + const char *from_name; + char *buf; + int sv[2]; + struct stat st; + off_t written, pos; + int child, error, from, status; + + if (argc != 2) + errx(1, "Usage: %s from", argv[0]); + from_name = argv[1]; + + from = open(from_name, O_RDONLY); + if (from == -1) + err(1, "open read %s", from_name); + + error = fstat(from, &st); + if (error == -1) + err(1, "stat %s", from_name); + + error = socketpair(AF_UNIX, SOCK_STREAM, 0, sv); + if (error == -1) + err(1, "socketpair"); + + child = fork(); + if (child == -1) + err(1, "fork"); + else if (child != 0) { + close(sv[1]); + pos = 0; + for (;;) { + error = sendfile(from, sv[0], pos, st.st_size - pos, + NULL, &written, 0); + if (error == -1) { + if (errno != EAGAIN) + err(1, "sendfile"); + } + pos += written; + if (pos == st.st_size) + break; + } + close(sv[0]); + waitpid(child, &status, 0); + } else { + close(sv[0]); + buf = malloc(st.st_size); + if (buf == NULL) + err(1, "malloc %jd", st.st_size); + pos = 0; + for (;;) { + written = 413; + if (written > st.st_size - pos) + written = st.st_size - pos; +#if 0 + written = st.st_size - pos; + if (written > 1000) + written = 1000; + written = arc4random_uniform(written) + 1; +#endif + error = read(sv[1], buf + pos, written); + if (error == -1) + err(1, "read"); + else if (error == 0) + errx(1, "short read"); + pos += error; + if (pos == st.st_size) + break; + } + close(sv[1]); + _exit(0); + } + + return (0); +} Added: user/pho/stress2/misc/sendfile5.sh ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/pho/stress2/misc/sendfile5.sh Tue Apr 23 10:29:37 2013 (r249793) @@ -0,0 +1,159 @@ +#!/bin/sh + +# +# Copyright (c) 2011 Peter Holm +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ +# + +# sendfile(2) test by kib@ +# Deadlock seen. + +[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 + +. ../default.cfg + +here=`pwd` +cd /tmp +sed '1,/^EOF/d' < $here/$0 > sendfile5.c +cc -o sendfile5 -Wall -Wextra -O2 sendfile5.c +rm -f sendfile5.c +dd if=/dev/zero of=/tmp/big bs=1m count=1k 2>&1 | egrep -v "records|transferred" +cd $here + +mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint +mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart + +mount -t tmpfs tmpfs $mntpoint +echo "Testing tmpfs(5)" +cp /tmp/big $mntpoint +/tmp/sendfile5 $mntpoint/big +umount $mntpoint + +mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart +mdconfig -a -t swap -s 2g -u $mdstart || exit 1 +bsdlabel -w md$mdstart auto +newfs -U md${mdstart}$part > /dev/null +mount /dev/md${mdstart}$part $mntpoint +echo "Testing FFS" +cp /tmp/big $mntpoint +/tmp/sendfile5 $mntpoint/big +umount $mntpoint + +mount -t nullfs /tmp $mntpoint +echo "Testing nullfs(5)" +/tmp/sendfile5 $mntpoint/big +umount $mntpoint + +rm -f /tmp/sendfile5 /tmp/big +exit +EOF +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + const char *from_name; + char *buf; + int sv[2]; + struct stat st; + off_t written, pos; + int child, error, from, status; + + if (argc != 2) + errx(1, "Usage: %s from", argv[0]); + from_name = argv[1]; + + from = open(from_name, O_RDONLY); + if (from == -1) + err(1, "open read %s", from_name); + + error = fstat(from, &st); + if (error == -1) + err(1, "stat %s", from_name); + + error = socketpair(AF_UNIX, SOCK_STREAM, 0, sv); + if (error == -1) + err(1, "socketpair"); + + child = fork(); + if (child == -1) + err(1, "fork"); + else if (child != 0) { + close(sv[1]); + pos = 0; + for (;;) { + error = sendfile(from, sv[0], pos, st.st_size - pos, + NULL, &written, 0); + if (error == -1) { + if (errno != EAGAIN) + err(1, "sendfile"); + } + pos += written; + if (pos == st.st_size) + break; + } + close(sv[0]); + waitpid(child, &status, 0); + } else { + close(sv[0]); + buf = malloc(st.st_size); + if (buf == NULL) + err(1, "malloc %jd", st.st_size); + pos = 0; + for (;;) { + written = 413; + if (written > st.st_size - pos) + written = st.st_size - pos; +#if 0 + written = st.st_size - pos; + if (written > 1000) + written = 1000; + written = arc4random_uniform(written) + 1; +#endif + error = read(sv[1], buf + pos, written); + if (error == -1) + err(1, "read"); + else if (error == 0) + errx(1, "short read"); + pos += error; + if (pos == st.st_size) + break; + } + close(sv[1]); + _exit(0); + } + + return (0); +}