xref: /freebsd/tools/test/stress2/misc/pthread9.sh (revision 8a272653d9fbd9fc37691c9aad6a05089b4ecb4d)
1*8a272653SPeter Holm#!/bin/sh
2*8a272653SPeter Holm
3*8a272653SPeter Holm#
4*8a272653SPeter Holm# Copyright (c) 2015 EMC Corp.
5*8a272653SPeter Holm# All rights reserved.
6*8a272653SPeter Holm#
7*8a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
8*8a272653SPeter Holm# modification, are permitted provided that the following conditions
9*8a272653SPeter Holm# are met:
10*8a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
11*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
12*8a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
13*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
14*8a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
15*8a272653SPeter Holm#
16*8a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*8a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*8a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8a272653SPeter Holm# SUCH DAMAGE.
27*8a272653SPeter Holm#
28*8a272653SPeter Holm
29*8a272653SPeter Holm# Thread suspend deadlock seen:
30*8a272653SPeter Holm# https://people.freebsd.org/~pho/stress/log/pthread9.txt
31*8a272653SPeter Holm
32*8a272653SPeter Holm# Test scenario by Conrad Meyer.
33*8a272653SPeter Holm# Fixed by r283320.
34*8a272653SPeter Holm
35*8a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
36*8a272653SPeter Holm
37*8a272653SPeter Holm. ../default.cfg
38*8a272653SPeter Holm
39*8a272653SPeter Holmhere=`pwd`
40*8a272653SPeter Holmcd /tmp
41*8a272653SPeter Holmsed '1,/^EOF/d' < $here/$0 > pthread9.c
42*8a272653SPeter Holmmycc -o pthread9 -Wall -Wextra -O2 pthread9.c -lpthread || exit 1
43*8a272653SPeter Holmrm -f pthread9.c
44*8a272653SPeter Holm
45*8a272653SPeter Holmstatus=0
46*8a272653SPeter Holmif ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1; then
47*8a272653SPeter Holm	mount -t nfs -o nfsv3,tcp,nolockd,retrycnt=3,intr $nfs_export \
48*8a272653SPeter Holm	    $mntpoint || exit 1
49*8a272653SPeter Holm	sleep .5
50*8a272653SPeter Holm	echo "Expect core dumps"
51*8a272653SPeter Holm	(cd $mntpoint; /tmp/pthread9) &
52*8a272653SPeter Holm	sleep 200
53*8a272653SPeter Holm	if pgrep -q pthread9; then
54*8a272653SPeter Holm		echo FAIL
55*8a272653SPeter Holm		procstat -k `pgrep pthread9 | grep -v $!`
56*8a272653SPeter Holm		status=1
57*8a272653SPeter Holm	fi
58*8a272653SPeter Holm	rm -f $mntpoint/pthread9.core
59*8a272653SPeter Holm	umount -f $mntpoint
60*8a272653SPeter Holm	wait
61*8a272653SPeter Holmfi
62*8a272653SPeter Holm
63*8a272653SPeter Holmrm -f /tmp/pthread9 /tmp/pthread9.core
64*8a272653SPeter Holmexit $status
65*8a272653SPeter HolmEOF
66*8a272653SPeter Holm#include <sys/types.h>
67*8a272653SPeter Holm
68*8a272653SPeter Holm#include <machine/atomic.h>
69*8a272653SPeter Holm
70*8a272653SPeter Holm#include <err.h>
71*8a272653SPeter Holm#include <errno.h>
72*8a272653SPeter Holm#include <fcntl.h>
73*8a272653SPeter Holm#include <pthread.h>
74*8a272653SPeter Holm#ifdef __FreeBSD__
75*8a272653SPeter Holm#include <pthread_np.h>
76*8a272653SPeter Holm#define	__NP__
77*8a272653SPeter Holm#endif
78*8a272653SPeter Holm#include <stdio.h>
79*8a272653SPeter Holm#include <stdlib.h>
80*8a272653SPeter Holm#include <time.h>
81*8a272653SPeter Holm#include <sys/wait.h>
82*8a272653SPeter Holm#include <unistd.h>
83*8a272653SPeter Holm
84*8a272653SPeter Holm#define LOOPS 50
85*8a272653SPeter Holm#define RUNTIME 180
86*8a272653SPeter Holm
87*8a272653SPeter Holmvolatile u_int go;
88*8a272653SPeter Holmint fd;
89*8a272653SPeter Holmchar file[] = "pthread9.file";
90*8a272653SPeter Holm
91*8a272653SPeter Holmstatic void *
92*8a272653SPeter Holmt1(void *data __unused)
93*8a272653SPeter Holm{
94*8a272653SPeter Holm	int i;
95*8a272653SPeter Holm
96*8a272653SPeter Holm#ifdef __NP__
97*8a272653SPeter Holm	pthread_set_name_np(pthread_self(), __func__);
98*8a272653SPeter Holm#endif
99*8a272653SPeter Holm	while (go == 0)
100*8a272653SPeter Holm		pthread_yield();
101*8a272653SPeter Holm
102*8a272653SPeter Holm	atomic_add_int(&go, 1);
103*8a272653SPeter Holm	for (i = 0; i < 100; i++)
104*8a272653SPeter Holm		if (ftruncate(fd, 0) == -1)
105*8a272653SPeter Holm			err(1, "truncate");
106*8a272653SPeter Holm
107*8a272653SPeter Holm	return (NULL);
108*8a272653SPeter Holm}
109*8a272653SPeter Holm
110*8a272653SPeter Holmstatic void *
111*8a272653SPeter Holmt2(void *data __unused)
112*8a272653SPeter Holm{
113*8a272653SPeter Holm	int i;
114*8a272653SPeter Holm
115*8a272653SPeter Holm#ifdef __NP__
116*8a272653SPeter Holm	pthread_set_name_np(pthread_self(), __func__);
117*8a272653SPeter Holm#endif
118*8a272653SPeter Holm	while (go == 0)
119*8a272653SPeter Holm		pthread_yield();
120*8a272653SPeter Holm
121*8a272653SPeter Holm	atomic_add_int(&go, 1);
122*8a272653SPeter Holm	for (i = 0; i < 100; i++)
123*8a272653SPeter Holm		if (ftruncate(fd, 0) == -1)
124*8a272653SPeter Holm			err(1, "truncate");
125*8a272653SPeter Holm
126*8a272653SPeter Holm	return (NULL);
127*8a272653SPeter Holm}
128*8a272653SPeter Holm
129*8a272653SPeter Holmstatic void *
130*8a272653SPeter Holmt3(void *data __unused)
131*8a272653SPeter Holm{
132*8a272653SPeter Holm
133*8a272653SPeter Holm#ifdef __NP__
134*8a272653SPeter Holm	pthread_set_name_np(pthread_self(), __func__);
135*8a272653SPeter Holm#endif
136*8a272653SPeter Holm	while (go != 3)
137*8a272653SPeter Holm		pthread_yield();
138*8a272653SPeter Holm	abort();
139*8a272653SPeter Holm
140*8a272653SPeter Holm	return (NULL);
141*8a272653SPeter Holm}
142*8a272653SPeter Holm
143*8a272653SPeter Holmint
144*8a272653SPeter Holmtest(void)
145*8a272653SPeter Holm{
146*8a272653SPeter Holm	pthread_t tid[3];
147*8a272653SPeter Holm	int i, rc;
148*8a272653SPeter Holm
149*8a272653SPeter Holm	go = 0;
150*8a272653SPeter Holm	if ((rc = pthread_create(&tid[0], NULL, t1, NULL)) != 0)
151*8a272653SPeter Holm		errc(1, rc, "pthread_create");
152*8a272653SPeter Holm	if ((rc = pthread_create(&tid[1], NULL, t2, NULL)) != 0)
153*8a272653SPeter Holm		errc(1, rc, "pthread_create");
154*8a272653SPeter Holm	if ((rc = pthread_create(&tid[2], NULL, t3, NULL)) != 0)
155*8a272653SPeter Holm		errc(1, rc, "pthread_create");
156*8a272653SPeter Holm	usleep(200);
157*8a272653SPeter Holm	atomic_add_int(&go, 1);
158*8a272653SPeter Holm
159*8a272653SPeter Holm	for (i = 0; i < 3; i++) {
160*8a272653SPeter Holm		if ((rc = pthread_join(tid[i], NULL)) != 0)
161*8a272653SPeter Holm			errc(1, rc, "pthread_join");
162*8a272653SPeter Holm	}
163*8a272653SPeter Holm
164*8a272653SPeter Holm	_exit(0);
165*8a272653SPeter Holm}
166*8a272653SPeter Holm
167*8a272653SPeter Holmint
168*8a272653SPeter Holmmain(void)
169*8a272653SPeter Holm{
170*8a272653SPeter Holm	time_t start;
171*8a272653SPeter Holm
172*8a272653SPeter Holm	if ((fd = open(file, O_RDWR | O_CREAT, 0644)) == -1)
173*8a272653SPeter Holm		err(1, "open(%s)", file);
174*8a272653SPeter Holm
175*8a272653SPeter Holm	start = time(NULL);
176*8a272653SPeter Holm	while (time(NULL) - start < RUNTIME) {
177*8a272653SPeter Holm		if (fork() == 0)
178*8a272653SPeter Holm			test();
179*8a272653SPeter Holm		wait(NULL);
180*8a272653SPeter Holm	}
181*8a272653SPeter Holm	close(fd);
182*8a272653SPeter Holm	unlink(file);
183*8a272653SPeter Holm
184*8a272653SPeter Holm	return (0);
185*8a272653SPeter Holm}
186