xref: /freebsd/tools/test/stress2/misc/perf.sh (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following 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# O_CREAT / unlink() timing test with different FFS options.
31
32[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
33[ `uname -m` = "i386" ] && exit 0 # very long runtime
34
35. ../default.cfg
36[ $# -eq 0 ] && half=1	# SU and SUJ workaround
37first=1
38export LANG=en_US.ISO8859-1
39odir=`pwd`
40dir=$mntpoint
41
42cd /tmp
43sed '1,/^EOF/d' < $odir/$0 > perf.c
44mycc -o perf -Wall -Wextra perf.c || exit 1
45rm -f perf.c
46cd $odir
47
48mount | grep -q "on $mntpoint " && umount $mntpoint
49mdconfig -l | grep md$mdstart > /dev/null &&  mdconfig -d -u $mdstart
50
51mdconfig -a -t swap -s 2g -u $mdstart
52
53tst() {
54	local i j k s
55
56	s=0
57	cd $dir
58	inodes=`df -ik $mntpoint | tail -1 | \
59	    awk '{printf "%d\n", $7}'`
60#	SU and SUJ tests fail with ENOSPC
61	[ $half ] &&
62	    i=$((inodes / 4)) ||
63	    i=$(((inodes - 500) / 2))
64	[ $first -eq 1 ] &&
65		printf "Using %'\''d inodes out of a total of %'\''d.\n" \
66			$((i * 2)) $inodes
67	first=0
68
69	for k in `jot 3`; do
70		pids=""
71		for j in `jot 2`; do
72			/tmp/perf $i &
73			pids="$pids $!"
74		done
75		for pid in $pids; do
76			wait $pid; r=$?
77			[ $r -ne 0 ] && s=$r
78		done
79	done
80	cd $odir
81	return $s
82}
83
84s=0
85for i in "" "-U" "-j"; do
86	newfs $i /dev/md$mdstart > /dev/null 2>&1
87	mount /dev/md$mdstart $mntpoint
88
89	t1=`date +%s`
90	tst; r=$?
91	t2=$((`date +%s` - t1))
92
93	umount -f $mntpoint
94	t2=$((`date +%s` - t1))
95	[ $t2 -eq 0 ] && t2=1
96	[ -z "$base" ] && base=$t2
97	pct=$(((t2 - base) * 100 / base))
98	printf '%3d seconds elapsed for newfs option "%2s" (%+4d%%)\n' \
99	    $t2 "$i" $pct
100	[ $pct -gt 10 ] && s=111
101	[ $s -eq 0 -a $r -ne 0 ] && s=$r
102done
103rm -f /tmp/perf
104mdconfig -d -u $mdstart
105exit $s
106EOF
107#include <sys/types.h>
108#include <sys/stat.h>
109#include <err.h>
110#include <errno.h>
111#include <fcntl.h>
112#include <stdio.h>
113#include <stdlib.h>
114#include <unistd.h>
115
116int
117main(int argc __unused, char **argv)
118{
119	pid_t pid;
120	int64_t size;
121	int e, fd, i, j;
122	char file[128];
123
124	size = atol(argv[1]);
125
126	e = 0;
127	pid = getpid();
128	for (j = 0; j < size; j++) {
129		sprintf(file,"p%05d.%05d", pid, j);
130		if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
131		    DEFFILEMODE)) == -1) {
132			e = errno;
133			if (errno != EINTR) {
134				warn("open(%s)", file);
135				printf("break out at %d, errno %d\n", j,
136				    errno);
137				break;
138			}
139		}
140		close(fd);
141	}
142
143	for (i = --j; i >= 0; i--) {
144		sprintf(file,"p%05d.%05d", pid, i);
145		if (unlink(file) == -1)
146			err(3, "unlink(%s)", file);
147
148	}
149
150	return (e);
151}
152