xref: /freebsd/tools/test/stress2/misc/bench.sh (revision 19261079)
1#!/bin/sh
2
3#
4# Copyright (c) 2017 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, 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# "benchmark" for file system use, using no physical disk.
30
31. ../default.cfg
32[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
33
34export LANG=C
35dir=/tmp
36odir=`pwd`
37cd $dir
38sed '1,/^EOF/d' < $odir/$0 > $dir/bench.c
39mycc -o bench -Wall -Wextra -O0 -g bench.c || exit 1
40rm -f bench.c
41cd $odir
42
43mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
44[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
45mdconfig -a -t swap -s 1g -u $mdstart || exit 1
46bsdlabel -w md$mdstart auto
47
48log=/tmp/stress2.d/bench.sh.log
49[ -f $log ] && old=`tail -1 $log | awk '{print $2}'`
50tmp=/tmp/bench.sh.tmp
51s=0
52for j in `jot 5`; do
53	newfs -n -b 4096 -f 512 -i 1024 md${mdstart}$part > \
54	    /dev/null
55	mount -o async /dev/md${mdstart}$part $mntpoint
56	/usr/bin/time sh -c "(cd $mntpoint; /tmp/bench)" 2>&1 | \
57	    awk '{print $1}'
58	[ $? -ne 0 ] && s=1
59	umount $mntpoint
60done | ministat -n | tail -1 | awk '{printf "%.3f %.3f\n",$6,$7}' > $tmp
61r=`cat $tmp`
62echo "`date +%Y%m%d%H%M` $r `uname -a`" >> $log
63tail -5 $log | cut -c 1-92
64rm $tmp
65
66if [ $old ]; then
67    awk -v old=$old -v new=$(echo $r | awk '{print $1}') \
68    'BEGIN {if ((new - old) * 100 / old > 5) exit 1; else exit 0}'
69    s=$?
70fi
71
72for i in `jot 6`; do
73	mount | grep -q "on $mntpoint " || break
74	umount $mntpoint && break || sleep 10
75done
76[ $i -eq 6 ] && exit 1
77mdconfig -d -u $mdstart
78rm -rf /tmp/bench
79exit 0
80
81EOF
82#include <sys/param.h>
83#include <sys/mman.h>
84#include <sys/stat.h>
85#include <sys/wait.h>
86
87#include <err.h>
88#include <errno.h>
89#include <fcntl.h>
90#include <stdio.h>
91#include <stdlib.h>
92#include <string.h>
93#include <time.h>
94#include <unistd.h>
95
96#define LOOPS 50000
97#define TESTS 6
98#define TIMEOUT 600
99
100static void (*functions[TESTS])();
101
102static void
103t1(void)
104{
105	int fd, i;
106	char file[128];
107
108	alarm(TIMEOUT);
109	for (i = 0; i < LOOPS; i++) {
110		if (i % 1000 == 0)
111			setproctitle("%s @ %d", __func__, i);
112		snprintf(file, sizeof(file), "t1.%06d.%03d", getpid(), i);
113		if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
114		    DEFFILEMODE)) == -1)
115			err(1, "open(%s)", file);
116		close(fd);
117		if (unlink(file) == -1)
118			err(1, "unlink(%s)", file);
119		usleep(100);
120	}
121	_exit(0);
122}
123
124static void
125t2(void)
126{
127	int fd, i;
128	char file[128];
129
130	alarm(TIMEOUT);
131	for (i = 0; i < LOOPS; i++) {
132		if (i % 1000 == 0)
133			setproctitle("%s @ %d", __func__, i);
134		snprintf(file, sizeof(file), "t2.%06d.%03d", getpid(), i);
135		if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
136		    DEFFILEMODE)) == -1)
137			err(1, "open(%s)", file);
138		close(fd);
139		usleep(100);
140	}
141	for (i = 0; i < LOOPS; i++) {
142		snprintf(file, sizeof(file), "t2.%06d.%03d", getpid(), i);
143		if (unlink(file) == -1)
144			err(1, "unlink(%s)", file);
145	}
146	_exit(0);
147}
148
149static void
150t3(void)
151{
152	int fd, i;
153	char dir[128], file[128];
154
155	alarm(TIMEOUT);
156	snprintf(dir, sizeof(dir), "t3.%06d.dir", getpid());
157	if (mkdir(dir, 700) == -1)
158		err(1, "mkdir(%s)", dir);
159	for (i = 0; i < LOOPS; i++) {
160		if (i % 1000 == 0)
161			setproctitle("%s @ %d", __func__, i);
162		snprintf(file, sizeof(file), "%s/t3.%06d.%03d", dir,
163		    getpid(), i);
164		if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
165		    DEFFILEMODE)) == -1)
166			err(1, "open(%s)", file);
167		close(fd);
168		if (unlink(file) == -1)
169			err(1, "unlink(%s)", file);
170		usleep(100);
171	}
172	if (rmdir(dir) == -1)
173		err(1, "rmdir(%s)", dir);
174
175	_exit(0);
176}
177
178static void
179t4(void)
180{
181	int fd, i;
182	char file[128], new[128];
183
184	alarm(TIMEOUT);
185	for (i = 0; i < LOOPS / 2; i++) {
186		if (i % 1000 == 0)
187			setproctitle("%s @ %d", __func__, i);
188		snprintf(file, sizeof(file), "t4.%06d.%03d", getpid(), i);
189		if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
190		    DEFFILEMODE)) == -1)
191			err(1, "open(%s)", file);
192		close(fd);
193		snprintf(new, sizeof(new), "t4.%06d.%03d.new", getpid(), i);
194		if (rename(file, new) == -1)
195			err(1, "rename(%s, %s)", file, new);
196		if (unlink(new) == -1)
197			err(1, "unlink(%s)", new);
198		usleep(100);
199	}
200	_exit(0);
201}
202
203static void
204t5(void)
205{
206	int fd, i;
207	char buf[512], file[128];
208
209	alarm(TIMEOUT);
210	memset(buf, 0, sizeof(buf));
211	for (i = 0; i < LOOPS; i++) {
212		if (i % 1000 == 0)
213			setproctitle("%s @ %d", __func__, i);
214		snprintf(file, sizeof(file), "t5.%06d.%03d", getpid(), i);
215		if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
216		    DEFFILEMODE)) == -1)
217			err(1, "open(%s)", file);
218		if (write(fd, buf, sizeof(buf)) != sizeof(buf))
219			err(1, "write(%s)", file);
220		close(fd);
221		if (unlink(file) == -1)
222			err(1, "unlink(%s)", file);
223		usleep(100);
224	}
225	_exit(0);
226}
227
228static void
229t6(void)
230{
231	int fd, i;
232	char buf[512], file[128];
233
234	alarm(TIMEOUT);
235	memset(buf, 0, sizeof(buf));
236	for (i = 0; i < LOOPS / 2; i++) {
237		if (i % 1000 == 0)
238			setproctitle("%s/write @ %d", __func__, i);
239		snprintf(file, sizeof(file), "t6.%06d.%03d", getpid(), i);
240		if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
241		    DEFFILEMODE)) == -1)
242			err(1, "open(%s)", file);
243		if (write(fd, buf, sizeof(buf)) != sizeof(buf))
244			err(1, "write(%s)", file);
245		close(fd);
246	}
247	for (i = 0; i < LOOPS / 2; i++) {
248		if (i % 1000 == 0)
249			setproctitle("%s/read @ %d", __func__, i);
250		snprintf(file, sizeof(file), "t6.%06d.%03d", getpid(), i);
251		if ((fd = open(file, O_RDONLY)) == -1)
252			err(1, "open(%s)", file);
253		if (read(fd, buf, sizeof(buf)) != sizeof(buf))
254			err(1, "write(%s)", file);
255		close(fd);
256		usleep(100);
257	}
258	for (i = 0; i < LOOPS / 2; i++) {
259		snprintf(file, sizeof(file), "t6.%06d.%03d", getpid(), i);
260		if (unlink(file) == -1)
261			err(1, "unlink(%s)", file);
262	}
263	_exit(0);
264}
265
266static int
267test(void)
268{
269	pid_t pids[TESTS];
270	int e, i, status;
271
272	e = 0;
273	for (i = 0; i < TESTS; i++)
274		if ((pids[i] = fork()) == 0)
275			functions[i]();
276	for (i = 0; i < TESTS; i++) {
277		if (waitpid(pids[i], &status, 0) != pids[i])
278			err(1, "waitpid(%d)", pids[i]);
279		e += status != 0;
280	}
281
282	return (e);
283}
284
285int
286main(void)
287{
288	int e;
289
290	functions[0] = &t1;
291	functions[1] = &t2;
292	functions[2] = &t3;
293	functions[3] = &t4;
294	functions[4] = &t5;
295	functions[5] = &t6;
296
297	e = test();
298
299	return (e);
300}
301