xref: /freebsd/tools/test/stress2/misc/spin.sh (revision 19261079b74319502c6ffa1249920079f0f69a72)
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
52bsdlabel -w md$mdstart auto
53newfs $newfs_flags -n md${mdstart}$part > /dev/null
54mount /dev/md${mdstart}$part $mntpoint
55
56cpus=`sysctl hw.ncpu | sed 's/.*: //'`
57(cd $mntpoint; /tmp/spin $((cpus + 1))) &
58error=0
59n=0
60while kill -0 $! 2>/dev/null; do
61	sleep 1
62	if [ $((n += 1)) -gt $timeout ]; then
63		echo FAIL
64		ps -l | grep -v sed | sed -n '1p;/ufs/p'
65		pkill spin
66		error=1
67	fi
68done
69wait
70
71while mount | grep $mntpoint | grep -q /dev/md; do
72	umount $mntpoint || sleep 1
73done
74mdconfig -d -u $mdstart
75rm -f /tmp/spin
76exit $error
77EOF
78#include <sys/param.h>
79#include <sys/wait.h>
80
81#include <err.h>
82#include <fcntl.h>
83#include <stdio.h>
84#include <stdlib.h>
85#include <unistd.h>
86
87void
88work(void)
89{
90
91	while (access("rendezvous", R_OK) != 0)
92		;
93
94	_exit(0);
95}
96
97int
98main(int argc, char **argv)
99{
100	int fd, i, parallel;
101
102	if (argc == 2)
103		parallel = atoi(argv[1]);
104	else
105		errx(1, "Usage: %s <cpus>", argv[0]);
106
107	for (i = 0; i < parallel; i++) {
108		if (fork() == 0)
109			work();
110	}
111
112	/* open(2) blocked on "ufs" for minutes */
113	if ((fd = open("rendezvous", O_CREAT, 0644)) == -1)
114		err(1, "open()");
115	close(fd);
116
117	for (i = 0; i < parallel; i++)
118		wait(NULL);
119
120	if (unlink("rendezvous") == -1)
121		err(1, "unlink()");
122
123	return (0);
124}
125