xref: /freebsd/tools/test/stress2/misc/vm_fault_dontneed.sh (revision 19261079b74319502c6ffa1249920079f0f69a72)
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# Verify that vm_fault_dontneed() is called during sequential access of a
30# mapped file on a file system.
31
32# Test scenario description by alc@
33
34. ../default.cfg
35[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
36dtrace -n 'dtrace:::BEGIN { exit(0); }' > /dev/null 2>&1 || exit 0
37
38dir=/tmp
39odir=`pwd`
40cd $dir
41sed '1,/^EOF/d' < $odir/$0 > $dir/vm_fault_dontneed.c
42mycc -o vm_fault_dontneed -Wall -Wextra -O0 -g vm_fault_dontneed.c || exit 1
43rm -f vm_fault_dontneed.c
44cd $odir
45
46size=$((`sysctl -n hw.physmem` / 1024 / 1024))
47[ $size -gt $((2 * 1024)) ] &&
48    { echo "RAM must be capped to 2GB for this test."
49    rm /tmp/vm_fault_dontneed; exit 0; }
50need=2048
51have=`df -k $(dirname $diskimage) | tail -1 | awk '{print int($4 / 1024)}'`
52[ $need -gt $have ] && need=$((have - 1))
53dd if=/dev/zero of=$diskimage bs=1m count=$need status=none
54
55log=/tmp/dtrace.$$
56trap "rm -f $log" EXIT INT
57dtrace -w -n '::*vm_fault_dontneed:entry { @[execname] = count(); }' \
58-c "/tmp/vm_fault_dontneed $diskimage" > $log 2>&1
59
60count=`grep vm_fault_dontneed $log | awk 'NF == 2 {print $NF}'`
61[ -z "$count" ] && count=0
62[ $count -lt 1000 ] &&
63    { echo "vm_fault_dontneed count = $count"; s=1; cat $log; }
64
65rm -rf /tmp/vm_fault_dontneed $diskimage
66exit $s
67
68EOF
69#include <sys/types.h>
70#include <sys/mman.h>
71#include <sys/stat.h>
72
73#include <err.h>
74#include <fcntl.h>
75#include <stdio.h>
76#include <unistd.h>
77
78int
79main(int argc __unused, char **argv)
80{
81	struct stat fs;
82	off_t i;
83	char *ptr;
84	int accum, fd;
85
86	fd = open(argv[1], O_RDONLY);
87	if (fd == -1)
88		err(1, "open");
89	if (fstat(fd, &fs) == -1 || !S_ISREG(fs.st_mode))
90		err(1, "fstat");
91	ptr = mmap(0, (size_t)fs.st_size, PROT_READ /*| PROT_EXEC*/, MAP_PRIVATE, fd, 0);
92	if (ptr == MAP_FAILED)
93		err(1, "mmap");
94	accum = 0;
95	for (i = 0; i < fs.st_size; i++)
96		accum += ptr[i];
97	if (accum != 0) /* just to trick the optimizer */
98		printf("accum: %d\nptr: %p\nsize: %ld\n", accum, ptr, (long)fs.st_size);
99}
100