xref: /freebsd/tools/test/stress2/misc/churn.sh (revision 02e9120893770924227138ba49df1edb3896112a)
1#!/bin/sh
2
3#
4# Copyright (c) 2016 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# VM test. No problems seen.
30
31. ../default.cfg
32
33dir=/tmp
34odir=`pwd`
35cd $dir
36sed '1,/^EOF/d' < $odir/$0 > $dir/churn.c
37mycc -o churn -Wall -Wextra -O0 -g churn.c -lpthread || exit 1
38rm -f churn.c
39
40/tmp/churn `sysctl -n hw.ncpu` `sysctl -n hw.usermem`
41s=$?
42
43rm -rf /tmp/churn
44exit $?
45
46EOF
47#include <sys/param.h>
48#include <sys/mman.h>
49#include <sys/stat.h>
50#include <sys/wait.h>
51
52#include <machine/atomic.h>
53
54#include <assert.h>
55#include <err.h>
56#include <errno.h>
57#include <fcntl.h>
58#include <pthread.h>
59#ifdef __FreeBSD__
60#include <pthread_np.h>
61#define	__NP__
62#endif
63#include <stdio.h>
64#include <stdlib.h>
65#include <time.h>
66#include <unistd.h>
67
68#define ARRAYSIZE (4 * 1024)
69#define RUNTIME (10 * 60)
70#define SYNC 0
71
72volatile u_int *share;
73long mem, parallel;
74int done;
75
76struct {
77	void *addr;
78	long pages;
79	volatile u_int busy;
80} v[ARRAYSIZE];
81
82void *
83test2(void *arg __unused)
84{
85	long i, j, n;
86	volatile char *cp;
87
88#ifdef __NP__
89	pthread_set_name_np(pthread_self(), __func__);
90#endif
91	while (done == 0) {
92		n = 0;
93		for (i = 0; i < ARRAYSIZE; i++) {
94			if (v[i].pages == 0)
95				continue;
96			atomic_add_int(&v[i].busy, 1);
97			if (v[i].busy != 1) {
98				atomic_add_int(&v[i].busy, -1);
99				continue;
100			}
101			cp = v[i].addr;
102			for (j = 0; j < v[i].pages; j++)
103				cp[j * PAGE_SIZE] = 1;
104			atomic_add_int(&v[i].busy, -1);
105			n++;
106		}
107		if (n == 0) {
108			usleep(10000);
109		}
110	}
111
112	return (0);
113}
114
115void
116test(void)
117{
118	pthread_t tp[2];
119	size_t len;
120	time_t start;
121	long i, j, k, size;
122	int r;
123	volatile char *cp;
124
125	atomic_add_int(&share[SYNC], 1);
126	while (share[SYNC] != (volatile u_int)parallel)
127		;
128
129	if ((r = pthread_create(&tp[0], NULL, test2, NULL)) != 0)
130		errc(1, r, "pthread_create");
131	if ((r = pthread_create(&tp[1], NULL, test2, NULL)) != 0)
132		errc(1, r, "pthread_create");
133	size = 0;
134	start = time(NULL);
135	while (time(NULL) - start < RUNTIME) {
136		for (k = 0; k < ARRAYSIZE; k++) {
137			i = arc4random() % ARRAYSIZE;
138			if (v[i].pages != 0)
139				break;
140		}
141		if (v[i].addr != NULL) {
142			atomic_add_int(&v[i].busy, 1);
143			if (v[i].busy != 1) {
144				atomic_add_int(&v[i].busy, -1);
145				continue;
146			}
147			if (munmap(v[i].addr, v[i].pages * PAGE_SIZE) == -1)
148				err(1, "munmap(%p, %ld)", v[i].addr, v[i].pages);
149			v[i].addr = NULL;
150			size -= v[i].pages;
151			v[i].pages = 0;
152			atomic_add_int(&v[i].busy, -1);
153		}
154		if (size < mem) {
155			j = round_page((arc4random() % (mem / 10)) + 1);
156			len = j * PAGE_SIZE;
157			if ((v[i].addr = mmap(NULL, len, PROT_READ | PROT_WRITE,
158			    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED) {
159				v[i].addr = NULL;
160				continue;
161			}
162			atomic_add_int(&v[i].busy, 1);
163			v[i].pages = j;
164			size += j;
165			assert(size > 0);
166			cp = v[i].addr;
167			for (k = 0; k < j * PAGE_SIZE; k += PAGE_SIZE)
168				cp[k] = 1;
169			atomic_add_int(&v[i].busy, -1);
170		}
171	}
172	done = 1;
173	pthread_join(tp[0], NULL);
174	pthread_join(tp[1], NULL);
175
176	_exit(0);
177}
178
179int
180main(int argc, char *argv[])
181{
182	size_t len;
183	int e, i, *pids, status;
184
185	if (argc != 3) {
186		fprintf(stderr, "Usage: %s <memory>\n", argv[0]);
187		_exit(1);
188	}
189
190	parallel = atol(argv[1]);
191	mem = atol(argv[2]) / PAGE_SIZE;
192	mem = mem / parallel;
193
194	e = 0;
195	len = PAGE_SIZE;
196	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
197	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
198		err(1, "mmap");
199
200	pids = malloc(sizeof(void *) * parallel);
201	for (i = 0; i < parallel; i++) {
202		if ((pids[i] = fork()) == 0)
203		test();
204	}
205	for (i = 0; i < parallel; i++) {
206		waitpid(pids[i], &status, 0);
207		e += status == 0 ? 0 : 1;
208	}
209
210	return (e);
211}
212