xref: /freebsd/tools/test/stress2/misc/swap.sh (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1#!/bin/sh
2
3#
4# Copyright (c) 2013 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# Swap test. Variation of testcases/swap.
30# OOM seen: https://people.freebsd.org/~pho/stress/log/swap.txt
31
32. ../default.cfg
33
34dir=/tmp
35odir=`pwd`
36cd $dir
37sed '1,/^EOF/d' < $odir/$0 > $dir/swap.c
38mycc -o swap -Wall -Wextra -O2 swap.c || exit 1
39rm -f swap.c
40
41usermem=`sysctl hw.usermem | sed 's/.* //'`
42
43if [ `sysctl -n vm.swap_total` -gt 0 ]; then
44	size=$((usermem/10*11))
45else
46	size=$((usermem/10*8))
47fi
48
49log=/tmp/swap.log
50tail -F -n 0 /var/log/messages > $log & lpid=$!
51/tmp/swap $((size / 4096))
52kill $lpid
53grep -m 1 "swp_pager_getswapspace" $log && s=1 || s=0
54rm $log
55
56rm -f /tmp/swap
57exit $s
58EOF
59#include <sys/types.h>
60#include <err.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <sys/resource.h>
64#include <sys/sysctl.h>
65#include <sys/time.h>
66#include <sys/wait.h>
67#include <unistd.h>
68
69#define RUNTIME (5 * 60)
70#define INCARNATIONS 32
71
72static unsigned long size, original;
73
74void
75setup(void)
76{
77	struct rlimit rlp;
78
79	size = size / INCARNATIONS;
80	original = size;
81	if (size == 0)
82		errx(1, "Argument too small");
83
84	if (getrlimit(RLIMIT_DATA, &rlp) < 0)
85		err(1,"getrlimit");
86	rlp.rlim_cur -= 1024 * 1024;
87
88	if (size > (unsigned long)rlp.rlim_cur)
89		size = rlp.rlim_cur;
90
91#if 0
92	printf("setup: pid %d. Total %luMb\n",
93		getpid(), size / 1024 / 1024 * INCARNATIONS);
94#endif
95
96	if (size == 0)
97		errx(1, "Argument too small");
98
99	return;
100}
101
102int
103test(void)
104{
105	volatile char *c;
106	int page;
107	unsigned long i, j;
108	time_t start;
109
110	c = malloc(size);
111	while (c == NULL) {
112		size -=  1024 * 1024;
113		c = malloc(size);
114	}
115	if (size != original)
116		printf("Malloc size changed from %ld Mb to %ld Mb\n",
117		    original / 1024 / 1024, size / 1024 / 1024);
118	page = getpagesize();
119	start = time(NULL);
120	while ((time(NULL) - start) < RUNTIME) {
121		i = j = 0;
122		while (i < size) {
123			c[i] = 0;
124			i += page;
125			if (++j % 1024 == 0) {
126				if ((time(NULL) - start) >= RUNTIME)
127					break;
128				if (arc4random() % 100 < 5)
129					usleep(1000);
130			}
131		}
132	}
133	free((void *)c);
134
135	_exit(0);
136}
137
138int
139main(int argc, char **argv)
140{
141	int i;
142
143	if (argc != 2)
144		errx(1, "Usage: %s bytes", argv[0]);
145
146	size = atol(argv[1]) * 4096;
147	setup();
148
149	for (i = 0; i < INCARNATIONS; i++)
150		if (fork() == 0)
151			test();
152
153	for (i = 0; i < INCARNATIONS; i++)
154		wait(NULL);
155
156	return (0);
157}
158