Path: blob/main/tools/test/stress2/misc/collapse.sh
39536 views
#!/bin/sh12#3# Copyright (c) 2020 Jeffrey Roberson <[email protected]>4# All rights reserved.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions8# are met:9# 1. Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# 2. Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14#15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25# SUCH DAMAGE.26#2728. ../default.cfg2930# "panic: freeing mapped page 0xfffffe000aa73910" seen:31# https://people.freebsd.org/~pho/stress/log/collapse.txt3233odir=`pwd`34cd /tmp35sed '1,/^EOF/d' < $odir/$0 > collapse.c36mycc -o collapse -Wall -Wextra -g -O0 collapse.c || exit 137rm -f collapse.c38cd $odir3940daemon sh -c '(cd ../testcases/swap; ./swap -t 20m -i 16 -l 85)' > \41/dev/null 2>&142sleep 243/tmp/collapse44while pgrep -q swap; do45pkill -9 swap46done47rm -f /tmp/collapse48exit 04950EOF51#include <sys/param.h>52#include <sys/mman.h>53#include <sys/wait.h>5455#include <err.h>56#include <errno.h>57#include <fcntl.h>58#include <stdio.h>59#include <stdlib.h>60#include <string.h>61#include <time.h>62#include <unistd.h>6364#define ADRSPACE (256 * 1024)65#define DEPTH 666#define WIDTH 367#define PARALLEL 468#define RUNTIME 120069#define CHILDTIME 570#define STARTTIME 571#define TOUCH 167273char *p;7475static void76child(int depth, time_t start)77{78time_t run, delay;79int i, shared, off;80int len;8182/* Pick a random bit of address space to change inherit on. */83for (i = 0; i < ADRSPACE; i += len) {84shared = arc4random() & 0x1;85len = roundup2(arc4random() % ((ADRSPACE - i) / 4),86PAGE_SIZE);87if (minherit(p + i, len, shared ? INHERIT_SHARE :88INHERIT_COPY) != 0)89err(1, "minherit");90}9192for (i = 0; depth != 0 && i < WIDTH; i++)93if (fork() == 0)94child(depth - 1, start);9596/*97* Touch all of the memory and exit at a random time to collapse98* some portion of the chain.99*/100delay = arc4random() % (CHILDTIME - 1);101run = arc4random() % (CHILDTIME - delay);102for (;;) {103if (time(NULL) >= start + delay)104break;105usleep(100);106}107while (time(NULL) - start < run) {108off = rounddown2(arc4random() % ADRSPACE, PAGE_SIZE);109bzero(p + off, PAGE_SIZE);110usleep((run * 1000) / TOUCH);111}112113_exit(0);114}115116static void117work(int depth)118{119120if ((p = mmap(NULL, ADRSPACE, PROT_READ | PROT_WRITE,121MAP_SHARED | MAP_ANON, -1, 0)) == MAP_FAILED) {122if (errno == ENOMEM)123return;124err(1, "mmap()");125}126child(depth, time(NULL) + STARTTIME);127}128129int130main(void)131{132pid_t pids[PARALLEL];133time_t start;134int i, n;135136start = time(NULL);137while (time(NULL) - start < RUNTIME) {138n = arc4random() % PARALLEL + 1;139for (i = 0; i < n; i++) {140if ((pids[i] = fork()) == 0)141work(DEPTH);142}143144sleep(CHILDTIME + STARTTIME + 1);145146for (i = 0; i < n; i++)147if (waitpid(pids[i], NULL, 0) != pids[i])148err(1, "waitpid(%d)", pids[i]);149}150151return (0);152}153154155