xref: /freebsd/tools/test/stress2/misc/shm_super.sh (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2018 Konstantin Belousov <kib@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# Demonstrate superpage mapping.
31
32cat > /tmp/shm_super.c <<EOF
33/* $Id: shm_super.c,v 1.1 2018/10/13 23:49:37 kostik Exp kostik $ */
34
35#include <sys/param.h>
36#include <sys/mman.h>
37#include <err.h>
38#include <fcntl.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
43
44#define M(x)    ((x) * 1024 * 1024)
45#define SZ      M(4)
46
47int
48main(void)
49{
50        char buf[128];
51        void *ptr;
52        off_t cnt;
53        int error, shmfd;
54
55        shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
56        if (shmfd == -1)
57                err(1, "shm_open");
58        error = ftruncate(shmfd, SZ);
59        if (error == -1)
60                err(1, "truncate");
61        memset(buf, 0, sizeof(buf));
62        for (cnt = 0; cnt < SZ; cnt += sizeof(buf)) {
63                error = write(shmfd, buf, sizeof(buf));
64                if (error == -1)
65                        err(1, "write");
66                else if (error != sizeof(buf))
67                        errx(1, "short write %d", (int)error);
68        }
69        ptr = mmap(NULL, SZ, PROT_READ | PROT_WRITE, MAP_SHARED |
70            MAP_ALIGNED_SUPER, shmfd, 0);
71        if (ptr == MAP_FAILED)
72                err(1, "mmap");
73        for (cnt = 0; cnt < SZ; cnt += PAGE_SIZE)
74                *((char *)ptr + cnt) = 0;
75        printf("ptr %p\n", ptr);
76        snprintf(buf, sizeof(buf), "procstat -v %d", getpid());
77        system(buf);
78}
79EOF
80cc -o /tmp/shm_super -Wall -Wextra -O2 -g /tmp/shm_super.c || exit 1
81rm /tmp/shm_super.c
82
83/tmp/shm_super > /tmp/shm_super.log 2>&1
84grep -wq S /tmp/shm_super.log && s=0 || { cat /tmp/shm_super.log; s=1; }
85[ $s -eq 1 ] && echo "No superpage mappings found."
86
87rm -f /tmp/shm_super /tmp/shm_super.log
88exit $s
89