Path: blob/main/tools/test/stress2/misc/cluster.sh
39536 views
#!/bin/sh12#3# Copyright (c) 2015 EMC Corp.4# All rights reserved.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions8# are met:9# 1. Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# 2. Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14#15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25# SUCH DAMAGE.26#2728# Open four (sparse) files for random read and write.2930# "panic: softdep_deallocate_dependencies: dangling deps" seen:31# https://people.freebsd.org/~pho/stress/log/kirk075.txt3233[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 13435. ../default.cfg3637dir=`dirname $diskimage`38free=`df -k $dir | tail -1 | awk '{print $4}'`39[ $((free / 1024 / 1024)) -lt 9 ] && echo "Not enough disk space." && exit4041odir=`pwd`42cd /tmp43sed '1,/^EOF/d' < $odir/$0 > cluster.c44rm -f /tmp/cluster45mycc -o cluster -Wall -Wextra -g -O2 cluster.c || exit 146rm -f cluster.c47cd $odir4849su $testuser -c "/tmp/cluster $dir abc"5051rm -f /tmp/cluster52exit 053EOF54#include <sys/param.h>55#include <sys/wait.h>5657#include <err.h>58#include <errno.h>59#include <fcntl.h>60#include <stdio.h>61#include <stdlib.h>62#include <string.h>63#include <time.h>64#include <unistd.h>6566#define BSIZE (8 * 1024 * 1024)67#define MX (8LL * 1024 * 1024 * 1024)68#define PARALLEL 469#define RUNTIME 60070#define WRLOOPS 10247172int rfd;73char *buf;74char *path;75char *uid;76char file[MAXPATHLEN + 1];7778unsigned long long79rnd(void) {80unsigned long long v;8182read(rfd, &v, sizeof(v));83v = v % MX;84return (v);85}8687void88wr(int idx)89{90off_t offset;91size_t ln;92int fd, i, n;9394snprintf(file, sizeof(file), "%s/f.%s.%06d", path, uid, idx);95setproctitle(__func__);96if ((fd = open(file, O_RDWR | O_CREAT, 0644)) == -1)97err(1, "open(%s)", file);98n = arc4random() % WRLOOPS + 1;99for (i = 0; i < n; i++) {100ln = rnd() % BSIZE + 1;101offset = rnd() % (MX - ln);102if (lseek(fd, offset, SEEK_SET) == -1)103err(1, "lseek in rw 1");104while (lockf(fd, F_LOCK, ln) == -1) {105if (errno != EDEADLK)106err(1, "lockf(%s, F_LOCK)", file);107}108if (write(fd, buf, ln) < 0)109err(1, "write");110if (lseek(fd, offset, SEEK_SET) == -1)111err(1, "lseek in rw 2");112if (lockf(fd, F_ULOCK, ln) == -1)113err(1, "lockf(%s, F_ULOCK)", file);114}115close(fd);116_exit(0);117}118119void120rd(int idx)121{122off_t offset;123size_t ln;124int fd, i, n;125126snprintf(file, sizeof(file), "%s/f.%s.%06d", path, uid, idx);127setproctitle(__func__);128for (i = 0; i < 100; i++) {129if (access(file, R_OK) == 0)130break;131usleep(1000);132}133if ((fd = open(file, O_RDONLY)) == -1)134if (errno != ENOENT)135err(1, "open(%s)for read", file);136n = arc4random() % WRLOOPS + 1;137for (i = 0; i < n; i++) {138ln = rnd() % BSIZE + 1;139offset = rnd() % (MX - ln);140if (lseek(fd, offset, SEEK_SET) == -1) {141if (errno == EBADF)142continue;143err(1, "lseek in rd");144}145if (read(fd, buf, ln) < 0)146err(1, "write");147}148close(fd);149_exit(0);150}151152void153mv(int idx)154{155int i;156char file2[MAXPATHLEN + 1];157158snprintf(file, sizeof(file), "%s/f.%s.%06d", path, uid, idx);159snprintf(file2, sizeof(file2), "%s/f.%s.%06d.old", path, uid, idx);160for (i = 0; i < 100; i++) {161if (access(file, R_OK) == 0)162break;163usleep(1000);164}165if (rename(file, file2) == -1)166if (errno != ENOENT)167warn("rename(%s, %s)", file, file2);168_exit(0);169}170171void172tr(int idx)173{174off_t offset;175int fd;176177if (arc4random() % 100 < 10) {178snprintf(file, sizeof(file), "%s/f.%s.%06d", path, uid, idx);179setproctitle(__func__);180if ((fd = open(file, O_RDWR | O_CREAT, 0644)) == -1)181err(1, "open(%s)for read", file);182offset = rnd() % MX;183offset = rnd();184if (ftruncate(fd, offset) == -1)185err(1, "truncate");186close(fd);187}188_exit(0);189}190191void192rm(int idx)193{194int i;195char file2[MAXPATHLEN + 1];196197snprintf(file2, sizeof(file2), "%s/f.%s.%06d.old", path, uid, idx);198for (i = 0; i < 100; i++) {199if (access(file2, R_OK) == 0)200break;201usleep(1000);202}203if (unlink(file2) == -1)204if (errno != ENOENT)205warn("unlink(%s)", file2);206_exit(0);207}208209void210test2(void (*func)(int nr))211{212time_t start;213int i;214215setproctitle(__func__);216start = time(NULL);217while (time(NULL) - start < RUNTIME) {218for (i = 0; i < PARALLEL; i++) {219if (fork() == 0)220func(i);221}222for (i = 0; i < PARALLEL; i++)223wait(NULL);224}225_exit(0);226227}228229void230test(void (*func)(int nr))231{232233if (fork() == 0)234test2(func);235}236237int238main(int argc, char *argv[])239{240int i;241242if (argc != 3)243errx(1, "Usage: %s <path> <uid>", argv[0]);244245path = argv[1];246uid = argv[2];247248if ((rfd = open("/dev/random", O_RDONLY)) == -1)249err(1, "open(/dev/random)");250setproctitle(__func__);251buf = malloc(BSIZE);252test(wr);253test(rd);254test(tr);255test(mv);256for (i = 0; i < 4; i++)257if (wait(NULL) == -1)258err(1, "wait");259260for (i = 0; i < PARALLEL; i++) {261snprintf(file, sizeof(file), "%s/f.%s.%06d", path, uid, i);262unlink(file);263snprintf(file, sizeof(file), "%s/f.%s.%06d.old", path, uid, i);264unlink(file);265}266267return (0);268}269270271