xref: /freebsd/tools/test/stress2/misc/posix_fadvise3.sh (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1#!/bin/sh
2
3#
4# Copyright (c) 2016 EMC Corp.
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# Looping thread seen:
30# https://people.freebsd.org/~pho/stress/log/kostik855.txt
31# Fixed by r293197.
32
33# Again on i386:
34# https://people.freebsd.org/~pho/stress/log/kostik867.txt
35# Fixed by r295716.
36
37. ../default.cfg
38
39odir=`pwd`
40cd /tmp
41sed '1,/^EOF/d' < $odir/$0 > posix_fadvise3.c
42mycc -o posix_fadvise3 -Wall -Wextra -O2 -g posix_fadvise3.c
43
44data=/tmp/posix_fadvise3.data
45dd if=/dev/zero of=$data bs=1m count=64 status=none
46/tmp/posix_fadvise3
47
48rm $data
49truncate -s 64m $data
50/tmp/posix_fadvise3
51
52rm -f /tmp/posix_fadvise3 posix_fadvise3.c $data
53exit
54EOF
55#include <err.h>
56#include <fcntl.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <unistd.h>
60
61#define LOOPS 10000
62#define N (128 * 1024 / (int)sizeof(u_int32_t))
63
64u_int32_t r[N];
65
66unsigned long
67makearg(void)
68{
69	unsigned int i;
70	unsigned long val;
71
72	val = arc4random();
73	i   = arc4random() % 100;
74	if (i < 20)
75		val = val & 0xff;
76	if (i >= 20 && i < 40)
77		val = val & 0xffff;
78	if (i >= 40 && i < 60)
79		val = (unsigned long)(r) | (val & 0xffff);
80#if defined(__LP64__)
81	if (i >= 60) {
82		val = (val << 32) | arc4random();
83		if (i > 80)
84			val = val & 0x00007fffffffffffUL;
85	}
86#endif
87
88	return(val);
89}
90
91int
92main(void)
93{
94	off_t len, offset;
95	int advise, fd, i, j;
96
97	if ((fd = open("/tmp/posix_fadvise3.data", O_RDONLY)) == -1)
98		err(1, "open()");
99	offset = 0;
100	len = 0x7fffffffffffffff;
101	advise = 4;
102	if (posix_fadvise(fd, offset, len, advise) == -1)
103		warn("posix_fadvise");
104	close(fd);
105
106	for (i = 0; i < LOOPS; i++) {
107		for (j = 0; j < N; j++)
108			r[j] = arc4random();
109		if ((fd = open("/tmp/posix_fadvise3.data", O_RDONLY)) == -1)
110			err(1, "open()");
111		offset = makearg();
112		len = makearg();
113		advise = arc4random() % 6;
114		if (posix_fadvise(fd, offset, len, advise) == -1)
115			warn("posix_fadvise");
116		close(fd);
117	}
118
119	return (0);
120}
121