xref: /freebsd/tools/test/stress2/testcases/swap/swap.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*-
2  * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/types.h>
29 #include <sys/resource.h>
30 #include <sys/sysctl.h>
31 #include <sys/time.h>
32 
33 #include <err.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 #include "stress.h"
39 
40 #if defined(__LP64__)
41 #define MINLEFT (1792LL * 1024 * 1024)
42 #else
43 #define MINLEFT (1024LL * 1024 * 1024)
44 #endif
45 
46 static int64_t size;
47 
48 int
49 setup(int nb)
50 {
51 	struct rlimit rlp;
52 	int64_t  mem, swapinfo;
53 	int mi, mx, pct;
54 	char *cp;
55 
56 	if (nb == 0) {
57 		mem = usermem();
58 		swapinfo = swap();
59 
60 		pct = 0;
61 		if (op->hog == 0) {
62 			mi = 80;
63 			mx = 100;
64 		}
65 
66 		if (op->hog == 1) {
67 			mi = 100;
68 			mx = 110;
69 		}
70 
71 		if (op->hog == 2) {
72 			mi = 110;
73 			mx = 120;
74 		}
75 
76 		if (op->hog >= 3) {
77 			mi = 120;
78 			mx = 130;
79 		}
80 		if ((cp = getenv("MAXSWAPPCT")) != NULL && *cp != '\0') {
81 			mx = atoi(cp);
82 			mi = mx - 10;
83 		}
84 		pct = random_int(mi, mx);
85 
86 		if (swapinfo == 0) {
87 			pct = random_int(30, 50);
88 			if (mem <= MINLEFT) {
89 				putval(0);
90 				_exit(1);
91 			}
92 			mem -= MINLEFT;
93 			size = mem / 100 * pct;
94 		} else {
95 			size = mem / 100 * pct;
96 			if (size > mem + swapinfo / 4) {
97 				size = mem + swapinfo / 4;
98 				pct = size * 100 / mem;
99 			}
100 		}
101 
102 		size = size / op->incarnations;
103 
104 		if (getrlimit(RLIMIT_DATA, &rlp) < 0)
105 			err(1,"getrlimit");
106 		rlp.rlim_cur -= 1024 * 1024;
107 
108 		if (size > rlp.rlim_cur)
109 			size = rlp.rlim_cur;
110 		putval(size);
111 
112 		if (op->verbose > 1 && nb == 0)
113 			printf("setup: pid %d, %d%%. Total %dMb, %d thread(s).\n",
114 			    getpid(), pct, (int)(size / 1024 / 1024 *
115 			    op->incarnations), op->incarnations);
116 	} else
117 		size = getval();
118 
119 	if (size == 0)
120 		exit(1);
121 
122 	return (0);
123 }
124 
125 void
126 cleanup(void)
127 {
128 }
129 
130 int
131 test(void)
132 {
133 	time_t start;
134 	int64_t i, oldsize;
135 	int page;
136 	char *c;
137 
138 	if (size == 0)
139 		return (0);
140 	oldsize = size;
141 	c = malloc(size);
142 	while (c == NULL && done_testing == 0) {
143 		size -=  1024 * 1024;
144 		c = malloc(size);
145 	}
146 	if (op->verbose > 1 && size != oldsize)
147 		printf("Malloc size changed from %d Mb to %d Mb\n",
148 		    (int)(oldsize / 1024 / 1024), (int)(size / 1024 / 102));
149 	page = getpagesize();
150 	start = time(NULL);	/* Livelock workaround */
151 	while (done_testing == 0 &&
152 			(time(NULL) - start) < op->run_time) {
153 		i = 0;
154 		while (i < size && done_testing == 0) {
155 			c[i] = 0;
156 			i += page;
157 		}
158 #if 0
159 		if (op->hog != 1)
160 			usleep(1000);
161 #endif
162 	}
163 	free((void *)c);
164 
165 	return (0);
166 }
167