1#!/bin/sh 2 3# 4# Copyright (c) 2017 Dell EMC Isilon 5# All rights reserved. 6# 7# Redistribution and use in source and binary forms, with or without 8# modification, are permitted provided that the following conditions 9# are met: 10# 1. Redistributions of source code must retain the above copyright 11# notice, this list of conditions and the following disclaimer. 12# 2. Redistributions in binary form must reproduce the above copyright 13# notice, this list of conditions and the following disclaimer in the 14# documentation and/or other materials provided with the distribution. 15# 16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26# SUCH DAMAGE. 27# 28 29# Copy of swap.sh, but without a swap disk and using 110% of hw.usermem 30# All memory is in active or in laundry, and there are several processes 31# swapped out: 32# https://people.freebsd.org/~pho/stress/log/kostik1068.txt 33 34. ../default.cfg 35 36dir=/tmp 37odir=`pwd` 38cd $dir 39sed '1,/^EOF/d' < $odir/$0 > $dir/swap4.c 40mycc -o swap4 -Wall -Wextra -O2 swap4.c || exit 1 41rm -f swap4.c 42 43usermem=`sysctl hw.usermem | sed 's/.* //'` 44size=$((usermem/10*11)) 45 46[ `sysctl -n vm.swap_total` -gt 0 ] && { swapoff -a; off=1; } 47echo "Expect: swap4, uid 0, was killed: out of swap space" 48$dir/swap4 $((size / 4096)) & 49sleep .5 50while [ `pgrep swap4 | wc -l` -eq 11 ]; do sleep 5; done 51pkill swap4 52wait 53[ $off ] && swapon -a 54 55rm -f $dir/swap4 56exit 0 57EOF 58#include <sys/types.h> 59#include <sys/resource.h> 60#include <sys/sysctl.h> 61#include <sys/time.h> 62#include <sys/wait.h> 63 64#include <err.h> 65#include <stdio.h> 66#include <stdlib.h> 67#include <unistd.h> 68 69#define INCARNATIONS 10 70#define RUNTIME (5 * 60) 71 72static unsigned long size, original; 73 74static void 75setup(void) 76{ 77 struct rlimit rlp; 78 79 size = size / INCARNATIONS; 80 original = size; 81 if (size == 0) 82 errx(1, "Argument too small"); 83 84 if (getrlimit(RLIMIT_DATA, &rlp) < 0) 85 err(1,"getrlimit"); 86 rlp.rlim_cur -= 1024 * 1024; 87 88 if (size > (unsigned long)rlp.rlim_cur) 89 size = rlp.rlim_cur; 90 91 if (size == 0) 92 errx(1, "Argument too small"); 93 94 return; 95} 96 97static int 98test(void) 99{ 100 time_t start; 101 unsigned long i, j; 102 int page; 103 volatile char *c; 104 105 c = malloc(size); 106 while (c == NULL) { 107 size -= 1024 * 1024; 108 c = malloc(size); 109 } 110 if (size != original) 111 printf("Malloc size changed from %ld Mb to %ld Mb\n", 112 original / 1024 / 1024, size / 1024 / 1024); 113 page = getpagesize(); 114 start = time(NULL); 115 while ((time(NULL) - start) < RUNTIME) { 116 i = j = 0; 117 while (i < size) { 118 c[i] = 0; 119 i += page; 120 if (++j % 1024 == 0) { 121 if ((time(NULL) - start) >= RUNTIME) 122 break; 123 if (arc4random() % 100 < 5) 124 usleep(1000); 125 } 126 } 127 } 128 free((void *)c); 129 130 _exit(0); 131} 132 133int 134main(int argc, char **argv) 135{ 136 int i; 137 138 if (argc != 2) 139 errx(1, "Usage: %s bytes", argv[0]); 140 141 size = atol(argv[1]) * 4096; 142 setup(); 143 144 for (i = 0; i < INCARNATIONS; i++) 145 if (fork() == 0) 146 test(); 147 148 for (i = 0; i < INCARNATIONS; i++) 149 wait(NULL); 150 151 return (0); 152} 153