xref: /freebsd/tools/test/stress2/misc/shm2.sh (revision 81ad6265)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2019 Dell EMC Isilon
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# No problems seen.
31
32[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
33
34. ../default.cfg
35
36dir=/tmp
37odir=`pwd`
38cd $dir
39sed '1,/^EOF/d' < $odir/$0 > $dir/shm2.c
40mycc -o shm2 -Wall -Wextra -O0 -g shm2.c || exit 1
41rm -f shm2.c
42
43(cd $odir/../testcases/swap; ./swap -t 2m -i 20 -h -l 100) &
44/tmp/shm2
45while pkill swap; do sleep 1; done
46wait
47
48rm -rf /tmp/shm2
49exit $s
50
51EOF
52#include <sys/types.h>
53#include <sys/resource.h>
54#include <sys/shm.h>
55#include <sys/sysctl.h>
56#include <sys/time.h>
57#include <sys/wait.h>
58
59#include <vm/vm_param.h>
60
61#include <err.h>
62#include <errno.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <unistd.h>
66
67#define INCARNATIONS 32
68
69static unsigned long size, original;
70static int runtime, utime;
71
72static void
73setup(void)
74{
75	struct rlimit rlp;
76
77	size = size / INCARNATIONS;
78	original = size;
79	if (size == 0)
80		errx(1, "Argument too small");
81
82	if (getrlimit(RLIMIT_DATA, &rlp) < 0)
83		err(1,"getrlimit");
84	rlp.rlim_cur -= 1024 * 1024;
85
86	if (size > (unsigned long)rlp.rlim_cur)
87		size = rlp.rlim_cur;
88
89#if 0
90	printf("setup: pid %d. Total %luMb\n",
91		getpid(), size / 1024 / 1024 * INCARNATIONS);
92#endif
93
94	if (size == 0)
95		errx(1, "Argument too small");
96
97	return;
98}
99
100static int
101test(void)
102{
103	key_t shmkey;
104	volatile char *c;
105	int page, shmid;
106	unsigned long i, j;
107	time_t start;
108
109	shmkey = ftok("/tmp", getpid());
110	shmid = -1;
111	while (shmid == -1) {
112		if ((shmid = shmget(shmkey, size, IPC_CREAT | IPC_EXCL | 0640)) == -1) {
113			if (errno != ENOSPC && errno != EEXIST)
114				err(1, "shmget (%s:%d)", __FILE__, __LINE__);
115		} else
116			break;
117		size -=  1024;
118	}
119	if ((c = shmat(shmid, NULL, 0)) == (void *) -1)
120		err(1, "shmat");
121	if (size != original)
122		printf("shm size changed from %ld kb to %ld kb\n",
123		    original / 1024, size / 1024);
124	page = getpagesize();
125	start = time(NULL);
126	while ((time(NULL) - start) < runtime) {
127		i = j = 0;
128		while (i < size) {
129			c[i] = 1;
130			i += page;
131			if ((time(NULL) - start) >= runtime)
132				break;
133//			usleep(utime);
134		}
135	}
136	if (shmdt((void *)c) == -1) {
137		if (errno != EINVAL)
138			warn("shmdt(%p)", c);
139	}
140	if (shmctl(shmid, IPC_RMID, NULL) == -1)
141		warn("shmctl IPC_RMID");
142
143	_exit(0);
144}
145
146int
147main(void)
148{
149	pid_t pids[INCARNATIONS];
150	int i;
151
152	runtime = 120;	/* 2 minutes */
153	utime = 1000;	/* 0.001 sec */
154	size = 512 * 1024 * 1024;
155	setup();
156
157	for (i = 0; i < INCARNATIONS; i++)
158		if ((pids[i] = fork()) == 0)
159			test();
160
161	for (i = 0; i < INCARNATIONS; i++)
162		if (waitpid(pids[i], NULL, 0) != pids[i])
163			err(1, "waitpid(%d)", pids[i]);
164
165	return (0);
166}
167