xref: /freebsd/tools/test/stress2/misc/db.sh (revision 8a272653d9fbd9fc37691c9aad6a05089b4ecb4d)
1*8a272653SPeter Holm#!/bin/sh
2*8a272653SPeter Holm
3*8a272653SPeter Holm#
4*8a272653SPeter Holm# Copyright (c) 2015 EMC Corp.
5*8a272653SPeter Holm# All rights reserved.
6*8a272653SPeter Holm#
7*8a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
8*8a272653SPeter Holm# modification, are permitted provided that the following conditions
9*8a272653SPeter Holm# are met:
10*8a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
11*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
12*8a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
13*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
14*8a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
15*8a272653SPeter Holm#
16*8a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*8a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*8a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8a272653SPeter Holm# SUCH DAMAGE.
27*8a272653SPeter Holm#
28*8a272653SPeter Holm
29*8a272653SPeter Holm# Demonstrate resource starvation using msync(2).
30*8a272653SPeter Holm
31*8a272653SPeter Holm. ../default.cfg
32*8a272653SPeter Holm
33*8a272653SPeter Holmdir=/tmp
34*8a272653SPeter Holmodir=`pwd`
35*8a272653SPeter Holmcd $dir
36*8a272653SPeter Holmsed '1,/^EOF/d' < $odir/$0 > $dir/db.c
37*8a272653SPeter Holmmycc -o db -Wall -Wextra -O0 -g db.c -lpthread || exit 1
38*8a272653SPeter Holmrm -f db.c
39*8a272653SPeter Holmcd $odir
40*8a272653SPeter Holm
41*8a272653SPeter Holmdd if=/dev/zero of=$diskimage bs=1m count=10 status=none
42*8a272653SPeter Holm
43*8a272653SPeter Holm/tmp/db $diskimage &
44*8a272653SPeter Holm
45*8a272653SPeter Holmstart=`date '+%s'`
46*8a272653SPeter Holmls -l $diskimage > /dev/null # Will wait for more than 90 seconds
47*8a272653SPeter Holm[ `date '+%s'` -gt $((start + 90)) ] && fail="yes"
48*8a272653SPeter Holmwait
49*8a272653SPeter Holme=$((`date +%s` - start))
50*8a272653SPeter Holm[ $fail ] &&
51*8a272653SPeter Holm    echo "Time for a ls is $((e / 60)) minutes $((e % 60)) seconds"
52*8a272653SPeter Holmrm -f /tmp/db $diskimage
53*8a272653SPeter Holm#[ $fail ] && exit 1 || exit 0 # Known issue
54*8a272653SPeter Holmexit 0
55*8a272653SPeter Holm
56*8a272653SPeter HolmEOF
57*8a272653SPeter Holm#include <sys/types.h>
58*8a272653SPeter Holm#include <sys/param.h>
59*8a272653SPeter Holm#include <sys/fcntl.h>
60*8a272653SPeter Holm#include <sys/mman.h>
61*8a272653SPeter Holm#include <sys/stat.h>
62*8a272653SPeter Holm
63*8a272653SPeter Holm#include <err.h>
64*8a272653SPeter Holm#include <errno.h>
65*8a272653SPeter Holm#include <pthread.h>
66*8a272653SPeter Holm#ifdef __FreeBSD__
67*8a272653SPeter Holm#include <pthread_np.h>
68*8a272653SPeter Holm#define	__NP__
69*8a272653SPeter Holm#endif
70*8a272653SPeter Holm#include <stdio.h>
71*8a272653SPeter Holm#include <stdlib.h>
72*8a272653SPeter Holm#include <strings.h>
73*8a272653SPeter Holm#include <time.h>
74*8a272653SPeter Holm#include <unistd.h>
75*8a272653SPeter Holm
76*8a272653SPeter Holmpthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
77*8a272653SPeter Holmsize_t len;
78*8a272653SPeter Holmvoid *p;
79*8a272653SPeter Holmint wthreads;
80*8a272653SPeter Holm
81*8a272653SPeter Holm#define BZ 128		/* buffer size */
82*8a272653SPeter Holm#define RUNTIME 180	/* runtime for test */
83*8a272653SPeter Holm#define RTHREADS 64	/* reader threads */
84*8a272653SPeter Holm#define WTHREADS 64	/* writer threads */
85*8a272653SPeter Holm
86*8a272653SPeter Holmvoid *
87*8a272653SPeter Holmwt(void *arg __unused)
88*8a272653SPeter Holm{
89*8a272653SPeter Holm	time_t start;
90*8a272653SPeter Holm	int64_t pos;
91*8a272653SPeter Holm	void *c;
92*8a272653SPeter Holm	int r;
93*8a272653SPeter Holm	char buf[BZ];
94*8a272653SPeter Holm
95*8a272653SPeter Holm#ifdef __NP__
96*8a272653SPeter Holm	pthread_set_name_np(pthread_self(), __func__);
97*8a272653SPeter Holm#endif
98*8a272653SPeter Holm	if ((r = pthread_mutex_lock(&mutex)) != 0)
99*8a272653SPeter Holm		errc(1, r, "pthread_mutex_lock");
100*8a272653SPeter Holm	wthreads++;
101*8a272653SPeter Holm	if ((r = pthread_mutex_unlock(&mutex)) != 0)
102*8a272653SPeter Holm		errc(1, r, "pthread_mutex_unlock");
103*8a272653SPeter Holm
104*8a272653SPeter Holm	start = time(NULL);
105*8a272653SPeter Holm	while (time(NULL) - start < RUNTIME) {
106*8a272653SPeter Holm		pos = arc4random() % (len / BZ);
107*8a272653SPeter Holm		pos = pos * BZ;
108*8a272653SPeter Holm		c = p + pos;
109*8a272653SPeter Holm		bcopy(buf, c, BZ);
110*8a272653SPeter Holm		c = (void *)trunc_page((unsigned long)c);
111*8a272653SPeter Holm		if (msync((void *)c, round_page(BZ), MS_SYNC) == -1)
112*8a272653SPeter Holm			err(1, "msync(%p)", c);
113*8a272653SPeter Holm		usleep(10000 + arc4random() % 1000);
114*8a272653SPeter Holm	}
115*8a272653SPeter Holm
116*8a272653SPeter Holm	if ((r = pthread_mutex_lock(&mutex)) != 0)
117*8a272653SPeter Holm		errc(1, r, "pthread_mutex_lock");
118*8a272653SPeter Holm	wthreads--;
119*8a272653SPeter Holm	if ((r = pthread_mutex_unlock(&mutex)) != 0)
120*8a272653SPeter Holm		errc(1, r, "pthread_mutex_unlock");
121*8a272653SPeter Holm
122*8a272653SPeter Holm	return (NULL);
123*8a272653SPeter Holm}
124*8a272653SPeter Holm
125*8a272653SPeter Holmvoid *
126*8a272653SPeter Holmrt(void *arg __unused)
127*8a272653SPeter Holm{
128*8a272653SPeter Holm	int64_t pos;
129*8a272653SPeter Holm	char buf[BZ], *c;
130*8a272653SPeter Holm
131*8a272653SPeter Holm#ifdef __NP__
132*8a272653SPeter Holm	pthread_set_name_np(pthread_self(), __func__);
133*8a272653SPeter Holm#endif
134*8a272653SPeter Holm	c = p;
135*8a272653SPeter Holm	do {
136*8a272653SPeter Holm		pos = arc4random() % (len / BZ);
137*8a272653SPeter Holm		pos = pos * BZ;
138*8a272653SPeter Holm		bcopy(&c[pos], buf, BZ);
139*8a272653SPeter Holm		usleep(10000 + arc4random() % 1000);
140*8a272653SPeter Holm	} while (wthreads != 0);
141*8a272653SPeter Holm
142*8a272653SPeter Holm	return (NULL);
143*8a272653SPeter Holm}
144*8a272653SPeter Holm
145*8a272653SPeter Holmint
146*8a272653SPeter Holmmain(int argc, char *argv[])
147*8a272653SPeter Holm{
148*8a272653SPeter Holm	pthread_t cp[RTHREADS + WTHREADS];
149*8a272653SPeter Holm	struct stat st;
150*8a272653SPeter Holm	int fd, i, j, rc;
151*8a272653SPeter Holm
152*8a272653SPeter Holm	if (argc != 2)
153*8a272653SPeter Holm		errx(1, "Usage: %s <file>", argv[0]);
154*8a272653SPeter Holm	if ((fd = open(argv[1], O_RDWR)) == -1)
155*8a272653SPeter Holm		err(1, "open %s", argv[1]);
156*8a272653SPeter Holm	if (fstat(fd, &st) == -1)
157*8a272653SPeter Holm		err(1, "stat");
158*8a272653SPeter Holm	len = round_page(st.st_size);
159*8a272653SPeter Holm	p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
160*8a272653SPeter Holm	if ((void *)p == MAP_FAILED)
161*8a272653SPeter Holm		err(1, "mmap");
162*8a272653SPeter Holm
163*8a272653SPeter Holm	i = 0;
164*8a272653SPeter Holm	for (j = 0; j < WTHREADS; j++) {
165*8a272653SPeter Holm		if ((rc = pthread_create(&cp[i++], NULL, wt, NULL)) != 0)
166*8a272653SPeter Holm			errc(1, rc, "pthread_create()");
167*8a272653SPeter Holm	}
168*8a272653SPeter Holm	usleep(100);
169*8a272653SPeter Holm	for (j = 0; j < RTHREADS; j++) {
170*8a272653SPeter Holm		if ((rc = pthread_create(&cp[i++], NULL, rt, NULL)) != 0)
171*8a272653SPeter Holm			errc(1, rc, "pthread_create()");
172*8a272653SPeter Holm	}
173*8a272653SPeter Holm
174*8a272653SPeter Holm	for (j = 0; j < RTHREADS + WTHREADS; j++)
175*8a272653SPeter Holm		pthread_join(cp[--i], NULL);
176*8a272653SPeter Holm
177*8a272653SPeter Holm	return (0);
178*8a272653SPeter Holm}
179