xref: /dflybsd-src/test/stress/stress2/testcases/swap/swap.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /*-
286d7f5d3SJohn Marino  * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
386d7f5d3SJohn Marino  * All rights reserved.
486d7f5d3SJohn Marino  *
586d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
686d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
786d7f5d3SJohn Marino  * are met:
886d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
986d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
1086d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
1186d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
1286d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
1386d7f5d3SJohn Marino  *
1486d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1586d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1686d7f5d3SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1786d7f5d3SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1886d7f5d3SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1986d7f5d3SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2086d7f5d3SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2186d7f5d3SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2286d7f5d3SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2386d7f5d3SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2486d7f5d3SJohn Marino  * SUCH DAMAGE.
2586d7f5d3SJohn Marino  *
2686d7f5d3SJohn Marino  */
2786d7f5d3SJohn Marino 
2886d7f5d3SJohn Marino #include <sys/types.h>
2986d7f5d3SJohn Marino #include <sys/sysctl.h>
3086d7f5d3SJohn Marino #include <unistd.h>
3186d7f5d3SJohn Marino #include <stdio.h>
3286d7f5d3SJohn Marino #include <stdlib.h>
3386d7f5d3SJohn Marino #include <sys/time.h>
3486d7f5d3SJohn Marino #include <sys/resource.h>
3586d7f5d3SJohn Marino #include <err.h>
3686d7f5d3SJohn Marino 
3786d7f5d3SJohn Marino 
3886d7f5d3SJohn Marino #include "stress.h"
3986d7f5d3SJohn Marino 
4086d7f5d3SJohn Marino static unsigned long size;
4186d7f5d3SJohn Marino 
4286d7f5d3SJohn Marino int
setup(int nb)4386d7f5d3SJohn Marino setup(int nb)
4486d7f5d3SJohn Marino {
4586d7f5d3SJohn Marino 	int pct;
4686d7f5d3SJohn Marino 	unsigned long mem;
4786d7f5d3SJohn Marino 	int64_t  swapinfo = 0;
4886d7f5d3SJohn Marino 	struct rlimit rlp;
4986d7f5d3SJohn Marino 
5086d7f5d3SJohn Marino 	if (nb == 0) {
5186d7f5d3SJohn Marino 		mem = usermem();
5286d7f5d3SJohn Marino 		swapinfo = swap();
5386d7f5d3SJohn Marino 		if (swapinfo > mem)
5486d7f5d3SJohn Marino 			swapinfo = mem;
5586d7f5d3SJohn Marino 
5686d7f5d3SJohn Marino 		if (op->hog == 0)
5786d7f5d3SJohn Marino 			pct = random_int(1, 10);
5886d7f5d3SJohn Marino 
5986d7f5d3SJohn Marino 		if (op->hog == 1)
6086d7f5d3SJohn Marino 			pct = random_int(10, 20);
6186d7f5d3SJohn Marino 
6286d7f5d3SJohn Marino 		if (op->hog == 2)
6386d7f5d3SJohn Marino 			pct = random_int(80, 90);
6486d7f5d3SJohn Marino 
6586d7f5d3SJohn Marino 		if (op->hog >= 3)
6686d7f5d3SJohn Marino 			pct = random_int(100, 110);
6786d7f5d3SJohn Marino 
6886d7f5d3SJohn Marino 		if (swapinfo == 0)
6986d7f5d3SJohn Marino 			size = mem / 100 * pct;
7086d7f5d3SJohn Marino 		else
7186d7f5d3SJohn Marino 			size = swapinfo / 100 * pct + mem;
7286d7f5d3SJohn Marino 
7386d7f5d3SJohn Marino 		size = size / op->incarnations;
7486d7f5d3SJohn Marino 
7586d7f5d3SJohn Marino 		if (getrlimit(RLIMIT_DATA, &rlp) < 0)
7686d7f5d3SJohn Marino 			err(1,"getrlimit");
7786d7f5d3SJohn Marino 		rlp.rlim_cur -= 1024 * 1024;
7886d7f5d3SJohn Marino 
7986d7f5d3SJohn Marino 		if (size > rlp.rlim_cur)
8086d7f5d3SJohn Marino 			size = rlp.rlim_cur;
8186d7f5d3SJohn Marino 		putval(size);
8286d7f5d3SJohn Marino 
8386d7f5d3SJohn Marino 
8486d7f5d3SJohn Marino 		if (op->verbose > 1 && nb == 0)
8586d7f5d3SJohn Marino 			printf("setup: pid %d, %d%%. Total %luMb\n",
8686d7f5d3SJohn Marino 				getpid(), pct, size / 1024 / 1024 * op->incarnations);
8786d7f5d3SJohn Marino 	} else
8886d7f5d3SJohn Marino 		size = getval();
8986d7f5d3SJohn Marino 	return (0);
9086d7f5d3SJohn Marino }
9186d7f5d3SJohn Marino 
9286d7f5d3SJohn Marino void
cleanup(void)9386d7f5d3SJohn Marino cleanup(void)
9486d7f5d3SJohn Marino {
9586d7f5d3SJohn Marino }
9686d7f5d3SJohn Marino 
9786d7f5d3SJohn Marino int
test(void)9886d7f5d3SJohn Marino test(void)
9986d7f5d3SJohn Marino {
10086d7f5d3SJohn Marino 	char *c;
10186d7f5d3SJohn Marino 	int i, page;
10286d7f5d3SJohn Marino 	unsigned long oldsize = size;
10386d7f5d3SJohn Marino 	time_t start;
10486d7f5d3SJohn Marino 
10586d7f5d3SJohn Marino 	c = malloc(size);
10686d7f5d3SJohn Marino 	while (c == NULL && done_testing == 0) {
10786d7f5d3SJohn Marino 		size -=  1024 * 1024;
10886d7f5d3SJohn Marino 		c = malloc(size);
10986d7f5d3SJohn Marino 	}
11086d7f5d3SJohn Marino 	if (op->verbose > 1 && size != oldsize)
11186d7f5d3SJohn Marino 		printf("Malloc size changed from %ld Mb to %ld Mb\n",
11286d7f5d3SJohn Marino 			oldsize / 1024 / 1024, size / 1024 / 1024);
11386d7f5d3SJohn Marino 	page = getpagesize();
11486d7f5d3SJohn Marino 	start = time(NULL);	/* Livelock workaround */
11586d7f5d3SJohn Marino 	while (done_testing == 0 &&
11686d7f5d3SJohn Marino 			(time(NULL) - start) < op->run_time) {
11786d7f5d3SJohn Marino 		i = 0;
11886d7f5d3SJohn Marino 		while (i < size && done_testing == 0) {
11986d7f5d3SJohn Marino 			c[i] = 0;
12086d7f5d3SJohn Marino 			i += page;
12186d7f5d3SJohn Marino 		}
12286d7f5d3SJohn Marino #if 0
12386d7f5d3SJohn Marino 		if (op->hog != 1)
12486d7f5d3SJohn Marino 			usleep(1000);
12586d7f5d3SJohn Marino #endif
12686d7f5d3SJohn Marino 	}
12786d7f5d3SJohn Marino 	free(c);
12886d7f5d3SJohn Marino 
12986d7f5d3SJohn Marino 	return (0);
13086d7f5d3SJohn Marino }
131