Path: blob/main/tools/test/stress2/testcases/swap/swap.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#include <sys/types.h>28#include <sys/resource.h>29#include <sys/sysctl.h>30#include <sys/time.h>3132#include <err.h>33#include <stdio.h>34#include <stdlib.h>35#include <unistd.h>3637#include "stress.h"3839#if defined(__LP64__)40#define MINLEFT (1792LL * 1024 * 1024)41#else42#define MINLEFT (1024LL * 1024 * 1024)43#endif4445static int64_t size;4647int48setup(int nb)49{50struct rlimit rlp;51int64_t mem, swapinfo;52int mi, mx, pct;53char *cp;5455if (nb == 0) {56mem = usermem();57swapinfo = swap();5859pct = 0;60if (op->hog == 0) {61mi = 80;62mx = 100;63}6465if (op->hog == 1) {66mi = 100;67mx = 110;68}6970if (op->hog == 2) {71mi = 110;72mx = 120;73}7475if (op->hog >= 3) {76mi = 120;77mx = 130;78}79if ((cp = getenv("MAXSWAPPCT")) != NULL && *cp != '\0') {80mx = atoi(cp);81mi = mx - 10;82}83pct = random_int(mi, mx);8485if (swapinfo == 0) {86pct = random_int(30, 50);87if (mem <= MINLEFT) {88putval(0);89_exit(1);90}91mem -= MINLEFT;92size = mem / 100 * pct;93} else {94size = mem / 100 * pct;95if (size > mem + swapinfo / 4) {96size = mem + swapinfo / 4;97pct = size * 100 / mem;98}99}100101size = size / op->incarnations;102103if (getrlimit(RLIMIT_DATA, &rlp) < 0)104err(1,"getrlimit");105rlp.rlim_cur -= 1024 * 1024;106107if (size > rlp.rlim_cur)108size = rlp.rlim_cur;109putval(size);110111if (op->verbose > 1 && nb == 0)112printf("setup: pid %d, %d%%. Total %dMb, %d thread(s).\n",113getpid(), pct, (int)(size / 1024 / 1024 *114op->incarnations), op->incarnations);115} else116size = getval();117118if (size == 0)119exit(1);120121return (0);122}123124void125cleanup(void)126{127}128129int130test(void)131{132time_t start;133int64_t i, oldsize;134int page;135char *c;136137if (size == 0)138return (0);139oldsize = size;140c = malloc(size);141while (c == NULL && done_testing == 0) {142size -= 1024 * 1024;143c = malloc(size);144}145if (op->verbose > 1 && size != oldsize)146printf("Malloc size changed from %d Mb to %d Mb\n",147(int)(oldsize / 1024 / 1024), (int)(size / 1024 / 102));148page = getpagesize();149start = time(NULL); /* Livelock workaround */150while (done_testing == 0 &&151(time(NULL) - start) < op->run_time) {152i = 0;153while (i < size && done_testing == 0) {154c[i] = 0;155i += page;156}157if (arc4random() % 100 < 10)158usleep(10000);159}160free((void *)c);161162return (0);163}164165166