xref: /freebsd/tools/test/stress2/misc/largepage.sh (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
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# Test scenario for of non-transparent superpages.
31
32# No problems seen.
33
34. ../default.cfg
35[ `uname -p` = "i386" ] && exit 0
36[ -z "`sysctl -i vm.largepages`" ] && exit 0
37
38odir=`pwd`
39cd /tmp
40sed '1,/^EOF/d' < $odir/$0 > largepage.c
41mycc -o largepage -Wall -Wextra -g -O0 largepage.c || exit 1
42rm -f largepage.c
43cd $odir
44
45/tmp/largepage
46s=$?
47
48for path in `posixshmcontrol ls | grep largepage | awk '{print $NF}'`; do
49	echo "posixshmcontrol rm $path"
50	posixshmcontrol rm $path
51done
52rm -f ./largepage /tmp/largepage.0* /tmp/largepage
53exit 0
54
55EOF
56#include <sys/param.h>
57#include <sys/mman.h>
58#include <sys/wait.h>
59
60#include <err.h>
61#include <errno.h>
62#include <fcntl.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <time.h>
67#include <unistd.h>
68
69static size_t ps[MAXPAGESIZES];
70static int fd;
71
72#define PARALLEL 4
73#define RUNTIME 60
74
75static void
76work(int idx)
77{
78	size_t len;
79	int i,r;
80	char *p;
81	volatile char val __unused;
82	char path[PATH_MAX];
83
84	len = ps[idx];
85	sprintf(path, "/tmp/largepage.%06d", getpid());
86	if ((fd = shm_create_largepage(path, O_CREAT | O_RDWR, idx,
87	    SHM_LARGEPAGE_ALLOC_DEFAULT, 0600)) == -1)
88		err(1,"shm_create_largepage(%zu)", len);
89
90	for (i = 0; i < 100; i++) {
91		r = ftruncate(fd, len);
92		if (r == 0)
93			break;
94		usleep(200000);
95	}
96	if (r == -1) {
97		shm_unlink(path);
98		err(1, "ftruncate(%zu)", len);
99	}
100	close(fd);
101	if ((fd = shm_open(path, O_RDWR, 0)) == -1)
102		err(1, "shm_open()");
103
104	if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
105			MAP_FAILED) {
106		if (errno == ENOMEM)
107			return;
108		err(1, "mmap()");
109	}
110
111	val = p[arc4random() % len];
112
113	close(fd);
114	if (munmap(p, len) == -1)
115		err(1, "munmap(%p)", p);
116	if (shm_unlink(path) == -1)
117		err(1, "shm_unlink()");
118
119	_exit(0);
120}
121
122int
123main(void)
124{
125	pid_t pids[PARALLEL];
126	time_t start;
127	int e, i, n, nps, status;
128
129	nps = getpagesizes(ps, MAXPAGESIZES);
130	e = 0;
131	n = 1;
132	start = time(NULL);
133	while (time(NULL) - start < RUNTIME && e == 0) {
134		for (i = 0; i < PARALLEL; i++) {
135			if ((pids[i] = fork()) == 0)
136				work(n);
137		}
138
139		for (i = 0; i < PARALLEL; i++) {
140			if (waitpid(pids[i], &status, 0) != pids[i])
141				err(1, "waitpid(%d)", pids[i]);
142			if (status != 0) e = 1;
143		}
144		n++;
145		n = n % nps;
146		if (n == 0) /* skip 4k pages */
147			n = 1;
148	}
149	close(fd);
150
151	return (e);
152}
153