xref: /freebsd/tools/test/stress2/misc/spin.sh (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
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[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
30
31# Demonstrate starvation: Thread stuck in "ufs" for minutes.
32# Only seen with >= 16 CPUs.
33# Not a problem with 4BSD.
34# http://people.freebsd.org/~pho/stress/log/spin.txt
35# Fixed by r273966.
36
37. ../default.cfg
38
39timeout=1200
40
41here=`pwd`
42cd /tmp
43sed '1,/^EOF/d' < $here/$0 > spin.c
44mycc -o spin -Wall -Wextra -O0 spin.c || exit 1
45rm -f spin.c
46cd $here
47
48mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
49mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
50
51mdconfig -a -t swap -s 1g -u $mdstart || exit 1
52newfs $newfs_flags -n md$mdstart > /dev/null
53mount /dev/md$mdstart $mntpoint
54
55cpus=`sysctl hw.ncpu | sed 's/.*: //'`
56(cd $mntpoint; /tmp/spin $((cpus + 1))) &
57error=0
58n=0
59while kill -0 $! 2>/dev/null; do
60	sleep 1
61	if [ $((n += 1)) -gt $timeout ]; then
62		echo FAIL
63		ps -l | grep -v sed | sed -n '1p;/ufs/p'
64		pkill spin
65		error=1
66	fi
67done
68wait
69
70while mount | grep $mntpoint | grep -q /dev/md; do
71	umount $mntpoint || sleep 1
72done
73mdconfig -d -u $mdstart
74rm -f /tmp/spin
75exit $error
76EOF
77#include <sys/param.h>
78#include <sys/wait.h>
79
80#include <err.h>
81#include <fcntl.h>
82#include <stdio.h>
83#include <stdlib.h>
84#include <unistd.h>
85
86void
87work(void)
88{
89
90	while (access("rendezvous", R_OK) != 0)
91		;
92
93	_exit(0);
94}
95
96int
97main(int argc, char **argv)
98{
99	int fd, i, parallel;
100
101	if (argc == 2)
102		parallel = atoi(argv[1]);
103	else
104		errx(1, "Usage: %s <cpus>", argv[0]);
105
106	for (i = 0; i < parallel; i++) {
107		if (fork() == 0)
108			work();
109	}
110
111	/* open(2) blocked on "ufs" for minutes */
112	if ((fd = open("rendezvous", O_CREAT, 0644)) == -1)
113		err(1, "open()");
114	close(fd);
115
116	for (i = 0; i < parallel; i++)
117		wait(NULL);
118
119	if (unlink("rendezvous") == -1)
120		err(1, "unlink()");
121
122	return (0);
123}
124