xref: /freebsd/tools/test/stress2/misc/shm2.sh (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
18a272653SPeter Holm#!/bin/sh
28a272653SPeter Holm
38a272653SPeter Holm#
4*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
58a272653SPeter Holm#
68a272653SPeter Holm# Copyright (c) 2019 Dell EMC Isilon
78a272653SPeter Holm#
88a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
98a272653SPeter Holm# modification, are permitted provided that the following conditions
108a272653SPeter Holm# are met:
118a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
128a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
138a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
148a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
158a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
168a272653SPeter Holm#
178a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
188a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
218a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
238a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
258a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278a272653SPeter Holm# SUCH DAMAGE.
288a272653SPeter Holm#
298a272653SPeter Holm
308a272653SPeter Holm# No problems seen.
318a272653SPeter Holm
328a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
338a272653SPeter Holm
348a272653SPeter Holm. ../default.cfg
358a272653SPeter Holm
368a272653SPeter Holmdir=/tmp
378a272653SPeter Holmodir=`pwd`
388a272653SPeter Holmcd $dir
398a272653SPeter Holmsed '1,/^EOF/d' < $odir/$0 > $dir/shm2.c
408a272653SPeter Holmmycc -o shm2 -Wall -Wextra -O0 -g shm2.c || exit 1
418a272653SPeter Holmrm -f shm2.c
428a272653SPeter Holm
438a272653SPeter Holm(cd $odir/../testcases/swap; ./swap -t 2m -i 20 -h -l 100) &
448a272653SPeter Holm/tmp/shm2
458a272653SPeter Holmwhile pkill swap; do sleep 1; done
468a272653SPeter Holmwait
478a272653SPeter Holm
488a272653SPeter Holmrm -rf /tmp/shm2
498a272653SPeter Holmexit $s
508a272653SPeter Holm
518a272653SPeter HolmEOF
528a272653SPeter Holm#include <sys/types.h>
538a272653SPeter Holm#include <sys/resource.h>
548a272653SPeter Holm#include <sys/shm.h>
558a272653SPeter Holm#include <sys/sysctl.h>
568a272653SPeter Holm#include <sys/time.h>
578a272653SPeter Holm#include <sys/wait.h>
588a272653SPeter Holm
598a272653SPeter Holm#include <vm/vm_param.h>
608a272653SPeter Holm
618a272653SPeter Holm#include <err.h>
628a272653SPeter Holm#include <errno.h>
638a272653SPeter Holm#include <stdio.h>
648a272653SPeter Holm#include <stdlib.h>
658a272653SPeter Holm#include <unistd.h>
668a272653SPeter Holm
678a272653SPeter Holm#define INCARNATIONS 32
688a272653SPeter Holm
698a272653SPeter Holmstatic unsigned long size, original;
708a272653SPeter Holmstatic int runtime, utime;
718a272653SPeter Holm
728a272653SPeter Holmstatic void
738a272653SPeter Holmsetup(void)
748a272653SPeter Holm{
758a272653SPeter Holm	struct rlimit rlp;
768a272653SPeter Holm
778a272653SPeter Holm	size = size / INCARNATIONS;
788a272653SPeter Holm	original = size;
798a272653SPeter Holm	if (size == 0)
808a272653SPeter Holm		errx(1, "Argument too small");
818a272653SPeter Holm
828a272653SPeter Holm	if (getrlimit(RLIMIT_DATA, &rlp) < 0)
838a272653SPeter Holm		err(1,"getrlimit");
848a272653SPeter Holm	rlp.rlim_cur -= 1024 * 1024;
858a272653SPeter Holm
868a272653SPeter Holm	if (size > (unsigned long)rlp.rlim_cur)
878a272653SPeter Holm		size = rlp.rlim_cur;
888a272653SPeter Holm
898a272653SPeter Holm#if 0
908a272653SPeter Holm	printf("setup: pid %d. Total %luMb\n",
918a272653SPeter Holm		getpid(), size / 1024 / 1024 * INCARNATIONS);
928a272653SPeter Holm#endif
938a272653SPeter Holm
948a272653SPeter Holm	if (size == 0)
958a272653SPeter Holm		errx(1, "Argument too small");
968a272653SPeter Holm
978a272653SPeter Holm	return;
988a272653SPeter Holm}
998a272653SPeter Holm
1008a272653SPeter Holmstatic int
1018a272653SPeter Holmtest(void)
1028a272653SPeter Holm{
1038a272653SPeter Holm	key_t shmkey;
1048a272653SPeter Holm	volatile char *c;
1058a272653SPeter Holm	int page, shmid;
1068a272653SPeter Holm	unsigned long i, j;
1078a272653SPeter Holm	time_t start;
1088a272653SPeter Holm
1098a272653SPeter Holm	shmkey = ftok("/tmp", getpid());
1108a272653SPeter Holm	shmid = -1;
1118a272653SPeter Holm	while (shmid == -1) {
1128a272653SPeter Holm		if ((shmid = shmget(shmkey, size, IPC_CREAT | IPC_EXCL | 0640)) == -1) {
1138a272653SPeter Holm			if (errno != ENOSPC && errno != EEXIST)
1148a272653SPeter Holm				err(1, "shmget (%s:%d)", __FILE__, __LINE__);
1158a272653SPeter Holm		} else
1168a272653SPeter Holm			break;
1178a272653SPeter Holm		size -=  1024;
1188a272653SPeter Holm	}
1198a272653SPeter Holm	if ((c = shmat(shmid, NULL, 0)) == (void *) -1)
1208a272653SPeter Holm		err(1, "shmat");
1218a272653SPeter Holm	if (size != original)
1228a272653SPeter Holm		printf("shm size changed from %ld kb to %ld kb\n",
1238a272653SPeter Holm		    original / 1024, size / 1024);
1248a272653SPeter Holm	page = getpagesize();
1258a272653SPeter Holm	start = time(NULL);
1268a272653SPeter Holm	while ((time(NULL) - start) < runtime) {
1278a272653SPeter Holm		i = j = 0;
1288a272653SPeter Holm		while (i < size) {
1298a272653SPeter Holm			c[i] = 1;
1308a272653SPeter Holm			i += page;
1318a272653SPeter Holm			if ((time(NULL) - start) >= runtime)
1328a272653SPeter Holm				break;
1338a272653SPeter Holm//			usleep(utime);
1348a272653SPeter Holm		}
1358a272653SPeter Holm	}
1368a272653SPeter Holm	if (shmdt((void *)c) == -1) {
1378a272653SPeter Holm		if (errno != EINVAL)
1388a272653SPeter Holm			warn("shmdt(%p)", c);
1398a272653SPeter Holm	}
1408a272653SPeter Holm	if (shmctl(shmid, IPC_RMID, NULL) == -1)
1418a272653SPeter Holm		warn("shmctl IPC_RMID");
1428a272653SPeter Holm
1438a272653SPeter Holm	_exit(0);
1448a272653SPeter Holm}
1458a272653SPeter Holm
1468a272653SPeter Holmint
1478a272653SPeter Holmmain(void)
1488a272653SPeter Holm{
1498a272653SPeter Holm	pid_t pids[INCARNATIONS];
1508d72c409SPeter Holm	int i;
1518a272653SPeter Holm
1528a272653SPeter Holm	runtime = 120;	/* 2 minutes */
1538a272653SPeter Holm	utime = 1000;	/* 0.001 sec */
1548a272653SPeter Holm	size = 512 * 1024 * 1024;
1558a272653SPeter Holm	setup();
1568a272653SPeter Holm
1578a272653SPeter Holm	for (i = 0; i < INCARNATIONS; i++)
1588a272653SPeter Holm		if ((pids[i] = fork()) == 0)
1598a272653SPeter Holm			test();
1608a272653SPeter Holm
1618a272653SPeter Holm	for (i = 0; i < INCARNATIONS; i++)
1628a272653SPeter Holm		if (waitpid(pids[i], NULL, 0) != pids[i])
1638a272653SPeter Holm			err(1, "waitpid(%d)", pids[i]);
1648a272653SPeter Holm
1658a272653SPeter Holm	return (0);
1668a272653SPeter Holm}
167