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# Open four (sparse) files for random read and write. 30*8a272653SPeter Holm 31*8a272653SPeter Holm# "panic: softdep_deallocate_dependencies: dangling deps" seen: 32*8a272653SPeter Holm# https://people.freebsd.org/~pho/stress/log/kirk075.txt 33*8a272653SPeter Holm 34*8a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 35*8a272653SPeter Holm 36*8a272653SPeter Holm. ../default.cfg 37*8a272653SPeter Holm 38*8a272653SPeter Holmdir=`dirname $diskimage` 39*8a272653SPeter Holmfree=`df -k $dir | tail -1 | awk '{print $4}'` 40*8a272653SPeter Holm[ $((free / 1024 / 1024)) -lt 9 ] && echo "Not enough disk space." && exit 41*8a272653SPeter Holm 42*8a272653SPeter Holmodir=`pwd` 43*8a272653SPeter Holmcd /tmp 44*8a272653SPeter Holmsed '1,/^EOF/d' < $odir/$0 > cluster.c 45*8a272653SPeter Holmrm -f /tmp/cluster 46*8a272653SPeter Holmmycc -o cluster -Wall -Wextra -g -O2 cluster.c || exit 1 47*8a272653SPeter Holmrm -f cluster.c 48*8a272653SPeter Holmcd $odir 49*8a272653SPeter Holm 50*8a272653SPeter Holmsu $testuser -c "/tmp/cluster $dir abc" 51*8a272653SPeter Holm 52*8a272653SPeter Holmrm -f /tmp/cluster 53*8a272653SPeter Holmexit 0 54*8a272653SPeter HolmEOF 55*8a272653SPeter Holm#include <sys/param.h> 56*8a272653SPeter Holm#include <sys/wait.h> 57*8a272653SPeter Holm 58*8a272653SPeter Holm#include <err.h> 59*8a272653SPeter Holm#include <errno.h> 60*8a272653SPeter Holm#include <fcntl.h> 61*8a272653SPeter Holm#include <stdio.h> 62*8a272653SPeter Holm#include <stdlib.h> 63*8a272653SPeter Holm#include <string.h> 64*8a272653SPeter Holm#include <time.h> 65*8a272653SPeter Holm#include <unistd.h> 66*8a272653SPeter Holm 67*8a272653SPeter Holm#define BSIZE (8 * 1024 * 1024) 68*8a272653SPeter Holm#define MX (8LL * 1024 * 1024 * 1024) 69*8a272653SPeter Holm#define PARALLEL 4 70*8a272653SPeter Holm#define RUNTIME 600 71*8a272653SPeter Holm#define WRLOOPS 1024 72*8a272653SPeter Holm 73*8a272653SPeter Holmint rfd; 74*8a272653SPeter Holmchar *buf; 75*8a272653SPeter Holmchar *path; 76*8a272653SPeter Holmchar *uid; 77*8a272653SPeter Holmchar file[MAXPATHLEN + 1]; 78*8a272653SPeter Holm 79*8a272653SPeter Holmunsigned long long 80*8a272653SPeter Holmrnd(void) { 81*8a272653SPeter Holm unsigned long long v; 82*8a272653SPeter Holm 83*8a272653SPeter Holm read(rfd, &v, sizeof(v)); 84*8a272653SPeter Holm v = v % MX; 85*8a272653SPeter Holm return (v); 86*8a272653SPeter Holm} 87*8a272653SPeter Holm 88*8a272653SPeter Holmvoid 89*8a272653SPeter Holmwr(int idx) 90*8a272653SPeter Holm{ 91*8a272653SPeter Holm off_t offset; 92*8a272653SPeter Holm size_t ln; 93*8a272653SPeter Holm int fd, i, n; 94*8a272653SPeter Holm 95*8a272653SPeter Holm snprintf(file, sizeof(file), "%s/f.%s.%06d", path, uid, idx); 96*8a272653SPeter Holm setproctitle(__func__); 97*8a272653SPeter Holm if ((fd = open(file, O_RDWR | O_CREAT, 0644)) == -1) 98*8a272653SPeter Holm err(1, "open(%s)", file); 99*8a272653SPeter Holm n = arc4random() % WRLOOPS + 1; 100*8a272653SPeter Holm for (i = 0; i < n; i++) { 101*8a272653SPeter Holm ln = rnd() % BSIZE + 1; 102*8a272653SPeter Holm offset = rnd() % (MX - ln); 103*8a272653SPeter Holm if (lseek(fd, offset, SEEK_SET) == -1) 104*8a272653SPeter Holm err(1, "lseek in rw 1"); 105*8a272653SPeter Holm while (lockf(fd, F_LOCK, ln) == -1) { 106*8a272653SPeter Holm if (errno != EDEADLK) 107*8a272653SPeter Holm err(1, "lockf(%s, F_LOCK)", file); 108*8a272653SPeter Holm } 109*8a272653SPeter Holm if (write(fd, buf, ln) < 0) 110*8a272653SPeter Holm err(1, "write"); 111*8a272653SPeter Holm if (lseek(fd, offset, SEEK_SET) == -1) 112*8a272653SPeter Holm err(1, "lseek in rw 2"); 113*8a272653SPeter Holm if (lockf(fd, F_ULOCK, ln) == -1) 114*8a272653SPeter Holm err(1, "lockf(%s, F_ULOCK)", file); 115*8a272653SPeter Holm } 116*8a272653SPeter Holm close(fd); 117*8a272653SPeter Holm _exit(0); 118*8a272653SPeter Holm} 119*8a272653SPeter Holm 120*8a272653SPeter Holmvoid 121*8a272653SPeter Holmrd(int idx) 122*8a272653SPeter Holm{ 123*8a272653SPeter Holm off_t offset; 124*8a272653SPeter Holm size_t ln; 125*8a272653SPeter Holm int fd, i, n; 126*8a272653SPeter Holm 127*8a272653SPeter Holm snprintf(file, sizeof(file), "%s/f.%s.%06d", path, uid, idx); 128*8a272653SPeter Holm setproctitle(__func__); 129*8a272653SPeter Holm for (i = 0; i < 100; i++) { 130*8a272653SPeter Holm if (access(file, R_OK) == 0) 131*8a272653SPeter Holm break; 132*8a272653SPeter Holm usleep(1000); 133*8a272653SPeter Holm } 134*8a272653SPeter Holm if ((fd = open(file, O_RDONLY)) == -1) 135*8a272653SPeter Holm if (errno != ENOENT) 136*8a272653SPeter Holm err(1, "open(%s)for read", file); 137*8a272653SPeter Holm n = arc4random() % WRLOOPS + 1; 138*8a272653SPeter Holm for (i = 0; i < n; i++) { 139*8a272653SPeter Holm ln = rnd() % BSIZE + 1; 140*8a272653SPeter Holm offset = rnd() % (MX - ln); 141*8a272653SPeter Holm if (lseek(fd, offset, SEEK_SET) == -1) { 142*8a272653SPeter Holm if (errno == EBADF) 143*8a272653SPeter Holm continue; 144*8a272653SPeter Holm err(1, "lseek in rd"); 145*8a272653SPeter Holm } 146*8a272653SPeter Holm if (read(fd, buf, ln) < 0) 147*8a272653SPeter Holm err(1, "write"); 148*8a272653SPeter Holm } 149*8a272653SPeter Holm close(fd); 150*8a272653SPeter Holm _exit(0); 151*8a272653SPeter Holm} 152*8a272653SPeter Holm 153*8a272653SPeter Holmvoid 154*8a272653SPeter Holmmv(int idx) 155*8a272653SPeter Holm{ 156*8a272653SPeter Holm int i; 157*8a272653SPeter Holm char file2[MAXPATHLEN + 1]; 158*8a272653SPeter Holm 159*8a272653SPeter Holm snprintf(file, sizeof(file), "%s/f.%s.%06d", path, uid, idx); 160*8a272653SPeter Holm snprintf(file2, sizeof(file2), "%s/f.%s.%06d.old", path, uid, idx); 161*8a272653SPeter Holm for (i = 0; i < 100; i++) { 162*8a272653SPeter Holm if (access(file, R_OK) == 0) 163*8a272653SPeter Holm break; 164*8a272653SPeter Holm usleep(1000); 165*8a272653SPeter Holm } 166*8a272653SPeter Holm if (rename(file, file2) == -1) 167*8a272653SPeter Holm if (errno != ENOENT) 168*8a272653SPeter Holm warn("rename(%s, %s)", file, file2); 169*8a272653SPeter Holm _exit(0); 170*8a272653SPeter Holm} 171*8a272653SPeter Holm 172*8a272653SPeter Holmvoid 173*8a272653SPeter Holmtr(int idx) 174*8a272653SPeter Holm{ 175*8a272653SPeter Holm off_t offset; 176*8a272653SPeter Holm int fd; 177*8a272653SPeter Holm 178*8a272653SPeter Holm if (arc4random() % 100 < 10) { 179*8a272653SPeter Holm snprintf(file, sizeof(file), "%s/f.%s.%06d", path, uid, idx); 180*8a272653SPeter Holm setproctitle(__func__); 181*8a272653SPeter Holm if ((fd = open(file, O_RDWR | O_CREAT, 0644)) == -1) 182*8a272653SPeter Holm err(1, "open(%s)for read", file); 183*8a272653SPeter Holm offset = rnd() % MX; 184*8a272653SPeter Holm offset = rnd(); 185*8a272653SPeter Holm if (ftruncate(fd, offset) == -1) 186*8a272653SPeter Holm err(1, "truncate"); 187*8a272653SPeter Holm close(fd); 188*8a272653SPeter Holm } 189*8a272653SPeter Holm _exit(0); 190*8a272653SPeter Holm} 191*8a272653SPeter Holm 192*8a272653SPeter Holmvoid 193*8a272653SPeter Holmrm(int idx) 194*8a272653SPeter Holm{ 195*8a272653SPeter Holm int i; 196*8a272653SPeter Holm char file2[MAXPATHLEN + 1]; 197*8a272653SPeter Holm 198*8a272653SPeter Holm snprintf(file2, sizeof(file2), "%s/f.%s.%06d.old", path, uid, idx); 199*8a272653SPeter Holm for (i = 0; i < 100; i++) { 200*8a272653SPeter Holm if (access(file2, R_OK) == 0) 201*8a272653SPeter Holm break; 202*8a272653SPeter Holm usleep(1000); 203*8a272653SPeter Holm } 204*8a272653SPeter Holm if (unlink(file2) == -1) 205*8a272653SPeter Holm if (errno != ENOENT) 206*8a272653SPeter Holm warn("unlink(%s)", file2); 207*8a272653SPeter Holm _exit(0); 208*8a272653SPeter Holm} 209*8a272653SPeter Holm 210*8a272653SPeter Holmvoid 211*8a272653SPeter Holmtest2(void (*func)(int nr)) 212*8a272653SPeter Holm{ 213*8a272653SPeter Holm time_t start; 214*8a272653SPeter Holm int i; 215*8a272653SPeter Holm 216*8a272653SPeter Holm setproctitle(__func__); 217*8a272653SPeter Holm start = time(NULL); 218*8a272653SPeter Holm while (time(NULL) - start < RUNTIME) { 219*8a272653SPeter Holm for (i = 0; i < PARALLEL; i++) { 220*8a272653SPeter Holm if (fork() == 0) 221*8a272653SPeter Holm func(i); 222*8a272653SPeter Holm } 223*8a272653SPeter Holm for (i = 0; i < PARALLEL; i++) 224*8a272653SPeter Holm wait(NULL); 225*8a272653SPeter Holm } 226*8a272653SPeter Holm _exit(0); 227*8a272653SPeter Holm 228*8a272653SPeter Holm} 229*8a272653SPeter Holm 230*8a272653SPeter Holmvoid 231*8a272653SPeter Holmtest(void (*func)(int nr)) 232*8a272653SPeter Holm{ 233*8a272653SPeter Holm 234*8a272653SPeter Holm if (fork() == 0) 235*8a272653SPeter Holm test2(func); 236*8a272653SPeter Holm} 237*8a272653SPeter Holm 238*8a272653SPeter Holmint 239*8a272653SPeter Holmmain(int argc, char *argv[]) 240*8a272653SPeter Holm{ 241*8a272653SPeter Holm int i; 242*8a272653SPeter Holm 243*8a272653SPeter Holm if (argc != 3) 244*8a272653SPeter Holm errx(1, "Usage: %s <path> <uid>", argv[0]); 245*8a272653SPeter Holm 246*8a272653SPeter Holm path = argv[1]; 247*8a272653SPeter Holm uid = argv[2]; 248*8a272653SPeter Holm 249*8a272653SPeter Holm if ((rfd = open("/dev/random", O_RDONLY)) == -1) 250*8a272653SPeter Holm err(1, "open(/dev/random)"); 251*8a272653SPeter Holm setproctitle(__func__); 252*8a272653SPeter Holm buf = malloc(BSIZE); 253*8a272653SPeter Holm test(wr); 254*8a272653SPeter Holm test(rd); 255*8a272653SPeter Holm test(tr); 256*8a272653SPeter Holm test(mv); 257*8a272653SPeter Holm for (i = 0; i < 4; i++) 258*8a272653SPeter Holm if (wait(NULL) == -1) 259*8a272653SPeter Holm err(1, "wait"); 260*8a272653SPeter Holm 261*8a272653SPeter Holm for (i = 0; i < PARALLEL; i++) { 262*8a272653SPeter Holm snprintf(file, sizeof(file), "%s/f.%s.%06d", path, uid, i); 263*8a272653SPeter Holm unlink(file); 264*8a272653SPeter Holm snprintf(file, sizeof(file), "%s/f.%s.%06d.old", path, uid, i); 265*8a272653SPeter Holm unlink(file); 266*8a272653SPeter Holm } 267*8a272653SPeter Holm 268*8a272653SPeter Holm return (0); 269*8a272653SPeter Holm} 270