xref: /freebsd/tools/test/stress2/misc/sem_timedwait.sh (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2019 Dell EMC Isilon
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# No problems seen.
31
32. ../default.cfg
33[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
34
35dir=/tmp
36odir=`pwd`
37cd $dir
38sed '1,/^EOF/d' < $odir/$0 > $dir/sem_timedwait.c
39mycc -o sem_timedwait -Wall -Wextra -O0 -g sem_timedwait.c || exit 1
40rm -f sem_timedwait.c
41cd $odir
42
43set -e
44mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
45[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
46mdconfig -a -t swap -s 1g -u $mdstart
47newfs $newfs_flags md$mdstart > /dev/null
48mount /dev/md$mdstart $mntpoint
49set +e
50
51(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -h -l 100) > /dev/null &
52cd $mntpoint
53nice $dir/sem_timedwait
54s=$?
55[ -f sem_timedwait.core -a $s -eq 0 ] &&
56    { ls -l sem_timedwait.core; mv sem_timedwait.core $dir; s=1; }
57cd $odir
58
59while pkill swap; do :; done
60wait
61for i in `jot 6`; do
62	mount | grep -q "on $mntpoint " || break
63	umount $mntpoint && break || sleep 10
64	[ $i -eq 6 ] &&
65	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
66done
67mdconfig -d -u $mdstart
68rm -rf $dir/sem_timedwait
69exit $s
70
71EOF
72#include <sys/param.h>
73#include <sys/mman.h>
74#include <sys/stat.h>
75#include <sys/wait.h>
76
77#include <machine/atomic.h>
78
79#include <err.h>
80#include <errno.h>
81#include <fcntl.h>
82#include <semaphore.h>
83#include <stdio.h>
84#include <stdlib.h>
85#include <time.h>
86#include <unistd.h>
87
88static volatile u_int *share;
89
90#define PARALLEL 64
91#define RUNTIME (5 * 60)
92#define SYNC 0
93
94static void
95test(void)
96{
97	struct timespec tm;
98	sem_t sem;
99	int i;
100
101	atomic_add_int(&share[SYNC], 1);
102	while (share[SYNC] != PARALLEL)
103		;
104
105	sem_init(&sem, 0, 0);
106	i = 0;
107	do {
108		clock_gettime(CLOCK_REALTIME, &tm);
109		tm.tv_nsec += 1000;
110		if (tm.tv_nsec >= 1000000000L) {
111			tm.tv_nsec -= 1000000000L;
112			tm.tv_sec++;
113		}
114
115		if (++i == 1000)
116			sem_post(&sem);
117		else
118			usleep(10000);
119	} while (sem_timedwait(&sem, &tm) == -1);
120
121	_exit(0);
122}
123
124int
125main(void)
126{
127	pid_t pids[PARALLEL];
128	size_t len;
129	time_t start;
130	int e, i, status;
131
132	e = 0;
133	len = PAGE_SIZE;
134	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
135	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
136		err(1, "mmap");
137
138	start = time(NULL);
139	while ((time(NULL) - start) < RUNTIME && e == 0) {
140		share[SYNC] = 0;
141		for (i = 0; i < PARALLEL; i++) {
142			if ((pids[i] = fork()) == 0)
143				test();
144			if (pids[i] == -1)
145				err(1, "fork()");
146		}
147		for (i = 0; i < PARALLEL; i++) {
148			if (waitpid(pids[i], &status, 0) == -1)
149				err(1, "waitpid(%d)", pids[i]);
150			if (status != 0) {
151				if (WIFSIGNALED(status))
152					fprintf(stderr,
153					    "pid %d exit signal %d\n",
154					    pids[i], WTERMSIG(status));
155			}
156			e += status == 0 ? 0 : 1;
157		}
158	}
159
160	return (e);
161}
162