xref: /freebsd/tools/test/stress2/misc/jumbo.sh (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1#!/bin/sh
2
3#
4# Copyright (c) 2014 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# Stress the jumbo mbuf allocation
30# vmstat -z | sed -n '1p;/jumbo/p'
31
32. ../default.cfg
33
34here=`pwd`
35cd /tmp
36sed '1,/^EOF/d' < $here/$0 > jumbo.c
37mycc -o jumbo -Wall -Wextra -O2 jumbo.c || exit 1
38rm -f jumbo.c
39
40log=/tmp/mbuf.log
41nb=80
42host=localhost
43[ $# -eq 1 ] && host=$1
44(
45	for i in `jot $nb 0`; do
46		/tmp/jumbo $((12345 + $i)) &
47	done
48	sleep 30
49	for i in `jot $nb 0`; do
50		/tmp/jumbo $host $((12345 + $i)) &
51	done
52	for i in `jot $nb 0`; do
53		wait
54	done
55) > $log 2>&1
56
57rm -f /tmp/jumbo
58grep -q FAIL $log && { cat $log 1>&2; exit 1; }
59rm -f $log
60exit 0
61EOF
62#include <sys/param.h>
63#include <sys/socket.h>
64#include <sys/wait.h>
65
66#include <netinet/in.h>
67
68#include <err.h>
69#include <errno.h>
70#include <fcntl.h>
71#include <netdb.h>
72#include <signal.h>
73#include <stdio.h>
74#include <stdlib.h>
75#include <string.h>
76#include <unistd.h>
77
78#define MX (1024 * 1024)
79#define RETRIES 5
80#define TIMEOUT 1200
81
82char *host;
83int loops, port, server;
84
85void
86ahandler(int s __unused)
87{
88	if (server)
89		fprintf(stderr, "FAIL Server timed out after %d loops.\n",
90		    loops);
91	else
92		fprintf(stderr, "FAIL Client timed out after %d loops.\n",
93		    loops);
94	_exit(0);
95}
96
97static void
98reader(void) {
99	socklen_t len;
100	struct sockaddr_in inetaddr, inetpeer;
101	int *buf, i, n, msgsock, on, tcpsock;
102
103	setproctitle("reader - init");
104	on = 1;
105	if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
106		err(1, "socket(), %s:%d", __FILE__, __LINE__);
107
108	if (setsockopt(tcpsock,
109	    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
110		err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
111
112	inetaddr.sin_family = AF_INET;
113	inetaddr.sin_addr.s_addr = INADDR_ANY;
114	inetaddr.sin_port = htons(port);
115	inetaddr.sin_len = sizeof(inetaddr);
116
117	signal(SIGALRM, ahandler);
118	alarm(TIMEOUT);
119	if (bind(tcpsock,
120	    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0)
121		err(1, "bind(), %s:%d", __FILE__, __LINE__);
122
123	if (listen(tcpsock, 5) < 0)
124		err(1, "listen(), %s:%d", __FILE__, __LINE__);
125
126	len = sizeof(inetpeer);
127	if ((msgsock = accept(tcpsock,
128	    (struct sockaddr *)&inetpeer, &len)) < 0)
129		err(1, "accept(), %s:%d", __FILE__, __LINE__);
130
131	if ((buf = malloc(MX)) == NULL)
132			err(1, "malloc(%d), %s:%d", MX, __FILE__, __LINE__);
133	setproctitle("reader");
134	for (i = sysconf(_SC_PAGESIZE); i < MX; i += 1024) {
135		alarm(TIMEOUT);
136		if ((n = recvfrom(msgsock, buf, i, MSG_WAITALL, NULL,
137		    NULL)) < 0) {
138			if (errno == EAGAIN)
139				continue;
140			err(1, "read(), %s:%d", __FILE__, __LINE__);
141		}
142		if (n == 0)
143			break;
144
145		loops++;
146	}
147	close(msgsock);
148	_exit(0);
149}
150
151static void
152writer(void) {
153	struct sockaddr_in inetaddr;
154	struct hostent *hostent;
155	int i, on, r, tcpsock;
156	char *line;
157
158	setproctitle("writer - init");
159	signal(SIGALRM, ahandler);
160	alarm(TIMEOUT);
161	on = 1;
162	for (i = 0; i < RETRIES; i++) {
163		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
164			err(1, "socket(), %s:%d", __FILE__, __LINE__);
165
166		if (setsockopt(tcpsock,
167		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
168			err(1, "setsockopt(), %s:%d", __FILE__, __LINE__);
169
170		if ((hostent = gethostbyname (host)) == NULL)
171			err(1, "gethostbyname(%s)", host);
172		bzero((char *) &inetaddr, sizeof(inetaddr));
173		memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr,
174			sizeof (struct in_addr));
175
176		inetaddr.sin_family = AF_INET;
177		inetaddr.sin_port = htons(port);
178		inetaddr.sin_len = sizeof(inetaddr);
179
180		r = connect(tcpsock, (struct sockaddr *) &inetaddr,
181			sizeof(inetaddr));
182		if (r == 0)
183			break;
184		sleep(1);
185		close(tcpsock);
186	}
187	if (r < 0)
188		err(1, "connect(%s, %d), %s:%d", host, port, __FILE__,
189		    __LINE__);
190
191	setproctitle("writer");
192	if ((line = malloc(MX)) == NULL)
193			err(1, "malloc(%d), %s:%d", MX, __FILE__, __LINE__);
194	alarm(TIMEOUT);
195	for (i = sysconf(_SC_PAGESIZE); i < MX; i += 1024) {
196		if (write(tcpsock, line, i) < 0)
197			err(1, "socket write(). %s:%d", __FILE__, __LINE__);
198		loops++;
199	}
200	close(tcpsock);
201
202	return;
203}
204
205int
206main(int argc, char **argv)
207{
208
209	if (argc == 2) {
210		server = 1;
211		port = atoi(argv[1]);
212		reader();
213	} else if (argc == 3) {
214		host = argv[1];
215		port = atoi(argv[2]);
216		writer();
217	} else
218		errx(1, "Usage: %s {<host>} <port number>\n", argv[0]);
219
220	return (0);
221}
222