xref: /freebsd/tools/test/stress2/misc/flock.sh (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1#!/bin/sh
2
3#
4# Copyright (c) 2015 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# flock(2) read (shared) lock test.
30
31# FAIL: Unfair scheduling?
32# share[1] = 359171
33# share[2] = 394437
34# share[3] = 359488
35# share[4] = 394429
36# share[5] = 359441
37# share[6] = 394281
38# share[7] = 359314
39# share[8] = 394615
40
41. ../default.cfg
42
43[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
44
45dir=/tmp
46odir=`pwd`
47cd $dir
48sed '1,/^EOF/d' < $odir/$0 > $dir/flock.c
49mycc -o flock -Wall -Wextra -O0 -g flock.c || exit 1
50rm -f flock.c
51cd $odir
52
53mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
54mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
55mdconfig -a -t swap -s 1g -u $mdstart || exit 1
56newfs $newfs_flags md$mdstart > /dev/null
57mount /dev/md$mdstart $mntpoint
58
59(cd $mntpoint; /tmp/flock)
60e=$?
61
62while mount | grep "on $mntpoint " | grep -q /dev/md; do
63	umount $mntpoint || sleep 1
64done
65mdconfig -d -u $mdstart
66rm -rf /tmp/flock
67exit $e
68
69EOF
70#include <sys/param.h>
71#include <sys/mman.h>
72#include <sys/stat.h>
73#include <sys/wait.h>
74
75#include <machine/atomic.h>
76
77#include <err.h>
78#include <errno.h>
79#include <fcntl.h>
80#include <stdio.h>
81#include <stdlib.h>
82#include <string.h>
83#include <time.h>
84#include <unistd.h>
85
86volatile u_int *share;
87int fd;
88
89#define RENDEZVOUS 0
90
91#define CHILDREN 8
92#define LOOPS 8000
93#define PARALLEL 1
94#define RUNTIME (1 * 60)
95
96void
97chld(int id)
98{
99	while (share[RENDEZVOUS] == 0)
100		;
101
102	while (share[RENDEZVOUS] == 1) {
103		if (flock(fd, LOCK_SH) == -1)
104			err(1, "fcntl @ %d", __LINE__);
105		atomic_add_int(&share[id + 1], 1);
106		if (flock(fd, LOCK_UN) == -1)
107			err(1, "fcntl @ %d", __LINE__);
108		usleep(100);
109	}
110
111	_exit(0);
112}
113
114void
115test(void)
116{
117	int i;
118	char file[80];
119
120	snprintf(file, sizeof(file), "file.%05d", getpid());
121	if ((fd = open(file, O_RDWR | O_CREAT, 0640)) == -1)
122		err(1, "open(%s)", file);
123	if (flock(fd, LOCK_EX) == -1)
124		err(1, "fcntl @ %d", __LINE__);
125
126	for (i = 0; i < CHILDREN; i++) {
127		if (fork() == 0)
128			chld(i);
129	}
130
131	usleep(200);
132	atomic_add_int(&share[RENDEZVOUS], 1); /* start chld */
133	for (i = 0; i < LOOPS; i++) {
134		if (flock(fd, LOCK_UN) == -1)
135			err(1, "fcntl @ %d", __LINE__);
136		if (flock(fd, LOCK_EX) == -1)
137			err(1, "fcntl @ %d", __LINE__);
138	}
139	atomic_add_int(&share[RENDEZVOUS], 1); /* stop chld */
140
141	for (i = 0; i < CHILDREN; i++)
142		wait(NULL);
143
144	close(fd);
145	unlink(file);
146
147	_exit(0);
148}
149
150int
151main(void)
152{
153	size_t len;
154	time_t start;
155	int i, n, pct;
156
157	len = getpagesize();
158	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON |
159	    MAP_SHARED, -1, 0)) == MAP_FAILED)
160		err(1, "mmap");
161
162	start = time(NULL);
163	while ((time(NULL) - start) < RUNTIME) {
164		share[RENDEZVOUS] = 0;
165		for (i = 0; i < PARALLEL; i++) {
166			if (fork() == 0)
167				test();
168		}
169		for (i = 0; i < PARALLEL; i++)
170			wait(NULL);
171	}
172	n = 0;
173	for (i = 0; i < CHILDREN; i++)
174		n += share[i + 1];
175	n /= CHILDREN;
176	for (i = 0; i < CHILDREN; i++) {
177		pct = abs((int)share[i + 1] - n) * 100 / n;
178		if (pct > 1) {
179			fprintf(stderr, "Unfair scheduling?\n");
180			for (i = 0; i < CHILDREN; i++) {
181				pct = abs((int)share[i + 1] - n) * 100 / n;
182				fprintf(stderr, "share[%d] = %d\n",
183				    i+1, share[i+1]);
184			}
185			break;
186		}
187	}
188
189	return (0);
190}
191