Path: blob/main/tools/test/stress2/testcases/rw/rw.c
39566 views
/*-1* Copyright (c) 2008 Peter Holm <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25*/2627/* Write and check read a file */2829#include <sys/param.h>30#include <sys/stat.h>31#include <err.h>32#include <fcntl.h>33#include <stdio.h>34#include <stdlib.h>35#include <unistd.h>3637#include "stress.h"3839static char path[128];40static int starting_dir;41static unsigned long size;4243#define MAXSIZE 256 * 10244445int46setup(int nb)47{48int64_t bl;49int64_t in;50int64_t reserve_in;51int64_t reserve_bl;52int pct;5354if (nb == 0) {55getdf(&bl, &in);56size = bl / op->incarnations / 1024;5758pct = 90;59if (op->hog == 0)60pct = random_int(1, 90);61size = size / 100 * pct + 1;6263if (size > MAXSIZE)64size = MAXSIZE; /* arbitrary limit size pr. incarnation */6566/* Resource requirements: */67while (size > 0) {68reserve_in = 2 * op->incarnations + 1;69reserve_bl = size * 1024 * op->incarnations +70(512 * 1024 * op->incarnations) +7164 * 1024;72if (reserve_bl <= bl && reserve_in <= in)73break;74size = size / 10 * 8;75}76if (size == 0)77reserve_bl = reserve_in = 0;7879if (op->verbose > 1)80printf("rw(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",81size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);82reservedf(reserve_bl, reserve_in);83putval(size);84size = size * 1024;85} else {86size = getval();87size = size * 1024;88}89if (size == 0)90exit(0);9192umask(0);93sprintf(path,"%s.%05d", getprogname(), getpid());94(void)mkdir(path, 0770);95if (chdir(path) == -1)96err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);97if ((starting_dir = open(".", 0)) < 0)98err(1, ".");99100return (0);101}102103void104cleanup(void)105{106if (starting_dir == 0)107return;108if (fchdir(starting_dir) == -1)109err(1, "fchdir()");110if (close(starting_dir) < 0)111err(1, "close(starting_dir:%d)", starting_dir);112113(void)system("find . -delete");114115if (chdir("..") == -1)116err(1, "chdir(..)");117if (rmdir(path) == -1)118err(1, "rmdir(%s), %s:%d", path, __FILE__, __LINE__);119size = 0;120}121122int123test(void)124{125int buf[1024], index, to;126#ifdef TEST127int i;128#endif129int fd;130char file[128];131132sprintf(file,"p%05d", getpid());133if ((fd = creat(file, 0660)) == -1)134err(1, "creat(%s)", file);135136to = sizeof(buf);137index = 0;138while (index < (int)size) {139if (index + to > (int)size)140to = size - index;141#ifdef TEST142for (i = 0; i < to; i++)143buf[i] = index + i;144#endif145index += to;146if (write(fd, buf, to) != to)147err(1, "write(%s), %s:%d", file, __FILE__, __LINE__);148}149if (close(fd) == -1)150err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);151152if ((fd = open(file, O_RDONLY)) == -1)153err(1, "open(%s), %s:%d", file, __FILE__, __LINE__);154155index = 0;156while (index < (int)size && done_testing == 0) {157if (index + to > (int)size)158to = size - index;159if (read(fd, buf, to) != to)160err(1, "rw read. %s.%d", __FILE__, __LINE__);161#ifdef TEST162for (i = 0; i < to; i++) {163if (buf[i] != index + i) {164fprintf(stderr,165"%s, pid %d: expected %d @ %d, got %d\n",166getprogname(), getpid(), index+i, index+i,167buf[i]);168exit(EXIT_FAILURE);169}170}171#endif172index += to;173}174if (close(fd) == -1)175err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);176if (unlink(file) == -1)177err(1, "unlink(%s), %s:%d", file, __FILE__, __LINE__);178return (0);179}180181182