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 56bsdlabel -w md$mdstart auto 57newfs $newfs_flags md${mdstart}$part > /dev/null 58mount /dev/md${mdstart}$part $mntpoint 59 60(cd $mntpoint; /tmp/flock) 61e=$? 62 63while mount | grep "on $mntpoint " | grep -q /dev/md; do 64 umount $mntpoint || sleep 1 65done 66mdconfig -d -u $mdstart 67rm -rf /tmp/flock 68exit $e 69 70EOF 71#include <sys/param.h> 72#include <sys/mman.h> 73#include <sys/stat.h> 74#include <sys/wait.h> 75 76#include <machine/atomic.h> 77 78#include <err.h> 79#include <errno.h> 80#include <fcntl.h> 81#include <stdio.h> 82#include <stdlib.h> 83#include <string.h> 84#include <time.h> 85#include <unistd.h> 86 87volatile u_int *share; 88int fd; 89 90#define RENDEZVOUS 0 91 92#define CHILDREN 8 93#define LOOPS 8000 94#define PARALLEL 1 95#define RUNTIME (1 * 60) 96 97void 98chld(int id) 99{ 100 while (share[RENDEZVOUS] == 0) 101 ; 102 103 while (share[RENDEZVOUS] == 1) { 104 if (flock(fd, LOCK_SH) == -1) 105 err(1, "fcntl @ %d", __LINE__); 106 atomic_add_int(&share[id + 1], 1); 107 if (flock(fd, LOCK_UN) == -1) 108 err(1, "fcntl @ %d", __LINE__); 109 usleep(100); 110 } 111 112 _exit(0); 113} 114 115void 116test(void) 117{ 118 int i; 119 char file[80]; 120 121 snprintf(file, sizeof(file), "file.%05d", getpid()); 122 if ((fd = open(file, O_RDWR | O_CREAT, 0640)) == -1) 123 err(1, "open(%s)", file); 124 if (flock(fd, LOCK_EX) == -1) 125 err(1, "fcntl @ %d", __LINE__); 126 127 for (i = 0; i < CHILDREN; i++) { 128 if (fork() == 0) 129 chld(i); 130 } 131 132 usleep(200); 133 atomic_add_int(&share[RENDEZVOUS], 1); /* start chld */ 134 for (i = 0; i < LOOPS; i++) { 135 if (flock(fd, LOCK_UN) == -1) 136 err(1, "fcntl @ %d", __LINE__); 137 if (flock(fd, LOCK_EX) == -1) 138 err(1, "fcntl @ %d", __LINE__); 139 } 140 atomic_add_int(&share[RENDEZVOUS], 1); /* stop chld */ 141 142 for (i = 0; i < CHILDREN; i++) 143 wait(NULL); 144 145 close(fd); 146 unlink(file); 147 148 _exit(0); 149} 150 151int 152main(void) 153{ 154 size_t len; 155 time_t start; 156 int i, n, pct; 157 158 len = getpagesize(); 159 if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON | 160 MAP_SHARED, -1, 0)) == MAP_FAILED) 161 err(1, "mmap"); 162 163 start = time(NULL); 164 while ((time(NULL) - start) < RUNTIME) { 165 share[RENDEZVOUS] = 0; 166 for (i = 0; i < PARALLEL; i++) { 167 if (fork() == 0) 168 test(); 169 } 170 for (i = 0; i < PARALLEL; i++) 171 wait(NULL); 172 } 173 n = 0; 174 for (i = 0; i < CHILDREN; i++) 175 n += share[i + 1]; 176 n /= CHILDREN; 177 for (i = 0; i < CHILDREN; i++) { 178 pct = abs((int)share[i + 1] - n) * 100 / n; 179 if (pct > 1) { 180 fprintf(stderr, "Unfair scheduling?\n"); 181 for (i = 0; i < CHILDREN; i++) { 182 pct = abs((int)share[i + 1] - n) * 100 / n; 183 fprintf(stderr, "share[%d] = %d\n", 184 i+1, share[i+1]); 185 } 186 break; 187 } 188 } 189 190 return (0); 191} 192