1#!/bin/sh 2 3# 4# SPDX-License-Identifier: BSD-2-Clause 5# 6# Copyright (c) 2021 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# Hunt for https://people.freebsd.org/~pho/stress/log/log0018.txt 31 32# Test scenario idea by kib@ 33# Scenario should be the following (without paging load, so that pages stay 34# resident): 35# - create some large file, say 2G 36# - map it and touch every page to ensure that they all allocated 37# - unmap, and map again, so while the pages are resident, they are not 38# installed into page table 39# Do mincore(2) on the mapped region. 40# 41 42# The problem was not reproduced. 43 44. ../default.cfg 45[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1 46[ `sysctl -n vm.swap_total` -eq 0 ] && exit 0 47path=`dirname $diskimage` 48[ `df -k $path | tail -1 | awk '{print int($4 / 1024 / 1024)}'` -lt 4 ] && exit 0 49 50dir=/tmp 51odir=`pwd` 52cd $dir 53sed '1,/^EOF/d' < $odir/$0 > $dir/mincore.c 54mycc -o mincore -Wall -Wextra -O0 -g mincore.c || exit 1 55rm -f mincore.c 56cd $odir 57 58(cd ../testcases/swap; ./swap -t 10m -i 20 -l 100) & 59for i in `jot 20`; do 60 [ `swapinfo | tail -1 | awk '{gsub("%", ""); print $5}'` -gt 0 ] && break 61 sleep 5 62 pgrep -q swap || break 63done 64echo "`date +%T` Go" 65 66$dir/mincore $path; s=$? 67 68while pkill swap; do :; done 69wait 70rm -rf $dir/mincore 71exit $s 72 73EOF 74#include <sys/param.h> 75#include <sys/mman.h> 76#include <sys/stat.h> 77#include <sys/wait.h> 78 79#include <fcntl.h> 80#include <err.h> 81#include <errno.h> 82#include <stdlib.h> 83#include <stdio.h> 84#include <string.h> 85#include <time.h> 86#include <unistd.h> 87 88#define PARALLEL 2 89#define RUNTIME 300 90#define SIZE (2LL * 1024 * 1024 * 1024) 91 92static void 93test(char *dir) 94{ 95 size_t i, len; 96 int fd; 97 char file[128], *p, *vec; 98 99 len = SIZE; 100 snprintf(file, sizeof(file), "%s/mincore.%d.file", dir, getpid()); 101 if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE)) 102 == -1) 103 err(1, "open(%s)", file); 104 if (unlink(file) == -1) 105 err(1, "unlink(%s)", file); 106 if (ftruncate(fd, len) == -1) 107 err(1, "ftruncete()"); 108 if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == 109 MAP_FAILED) 110 err(1, "mmap()"); 111 for (i = 0; i < len; i += PAGE_SIZE) 112 p[i] = 1; 113 if (munmap(p, len) == -1) 114 err(1, "munmap()"); 115 if ((vec = malloc(len / PAGE_SIZE)) == NULL) 116 err(1, "malloc"); 117 if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == 118 MAP_FAILED) 119 err(1, "mmap()"); 120 if (mincore(p, len, vec) == -1) 121 err(1, "mincore()"); 122 if (munmap(p, len) == -1) 123 err(1, "munmap()"); 124 close(fd); 125 126 _exit(0); 127} 128 129int 130main(int argc __unused, char *argv[]) 131{ 132 pid_t pids[PARALLEL]; 133 time_t start; 134 int i; 135 136 for (i = 0; i < PARALLEL; i++) { 137 if ((pids[i] = fork()) == 0) 138 test(argv[1]); 139 } 140 141 start = time(NULL); 142 while (time(NULL) - start < RUNTIME) { 143 for (i = 0; i < PARALLEL; i++) { 144 if (waitpid(pids[i], NULL, WNOHANG) == pids[i]) { 145 if ((pids[i] = fork()) == 0) 146 test(argv[1]); 147 } 148 } 149 } 150 151 for (i = 0; i < PARALLEL; i++) { 152 if (waitpid(pids[i], NULL, 0) != pids[i]) 153 err(1, "waitpid"); 154 } 155 156 return (0); 157} 158