From owner-svn-src-user@FreeBSD.ORG Mon Jun 1 08:52:11 2015 Return-Path: Delivered-To: svn-src-user@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id C73A25AF; Mon, 1 Jun 2015 08:52:11 +0000 (UTC) (envelope-from pho@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id A962F1186; Mon, 1 Jun 2015 08:52:11 +0000 (UTC) (envelope-from pho@FreeBSD.org) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.9/8.14.9) with ESMTP id t518qBOF041886; Mon, 1 Jun 2015 08:52:11 GMT (envelope-from pho@FreeBSD.org) Received: (from pho@localhost) by svn.freebsd.org (8.14.9/8.14.9/Submit) id t518qBTs041885; Mon, 1 Jun 2015 08:52:11 GMT (envelope-from pho@FreeBSD.org) Message-Id: <201506010852.t518qBTs041885@svn.freebsd.org> X-Authentication-Warning: svn.freebsd.org: pho set sender to pho@FreeBSD.org using -f From: Peter Holm Date: Mon, 1 Jun 2015 08:52:11 +0000 (UTC) To: src-committers@freebsd.org, svn-src-user@freebsd.org Subject: svn commit: r283874 - user/pho/stress2/misc X-SVN-Group: user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 01 Jun 2015 08:52:11 -0000 Author: pho Date: Mon Jun 1 08:52:11 2015 New Revision: 283874 URL: https://svnweb.freebsd.org/changeset/base/283874 Log: Added a new test scenario. Sponsored by: EMC / Isilon storage division Added: user/pho/stress2/misc/db.sh (contents, props changed) Added: user/pho/stress2/misc/db.sh ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/pho/stress2/misc/db.sh Mon Jun 1 08:52:11 2015 (r283874) @@ -0,0 +1,177 @@ +#!/bin/sh + +# +# Copyright (c) 2015 EMC Corp. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ +# + +# Demonstrate resource starvation using msync(2). + +. ../default.cfg + +dir=/tmp +odir=`pwd` +cd $dir +sed '1,/^EOF/d' < $odir/$0 > $dir/db.c +mycc -o db -Wall -Wextra -O0 -g db.c -lpthread || exit 1 +rm -f db.c +cd $odir + +diskimage=/var/tmp/diskimage +dd if=/dev/zero of=$diskimage bs=1m count=10 2>&1 | \ + egrep -v "records|transferred" + +/tmp/db $diskimage & + +start=`date '+%s'` +ls -l $diskimage > /dev/null +[ `date '+%s'` -gt $((start + 90)) ] && { fail="yes"; echo FAIL; } +wait +rm -f /tmp/db $diskimage +[ "$fail" = "yes" ] && exit 1 || exit 0 + +EOF +#include +#include +#include +#include +#include + +#include +#include +#include +#ifdef __FreeBSD__ +#include +#define __NP__ +#endif +#include +#include +#include +#include +#include + +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +size_t len; +void *p; +int wthreads; + +#define BZ 128 /* buffer size */ +#define RUNTIME 180 /* runtime for test */ +#define RTHREADS 64 /* reader threads */ +#define WTHREADS 64 /* writer threads */ + +void * +wt(void *arg __unused) +{ + time_t start; + int64_t pos; + void *c; + char buf[BZ]; + +#ifdef __NP__ + pthread_set_name_np(pthread_self(), __func__); +#endif + if (pthread_mutex_lock(&mutex) == -1) + err(1, "pthread_mutex_lock"); + wthreads++; + if (pthread_mutex_unlock(&mutex) == -1) + err(1, "pthread_mutex_unlock"); + + start = time(NULL); + while (time(NULL) - start < RUNTIME) { + pos = arc4random() % (len / BZ); + pos = pos * BZ; + c = p + pos; + bcopy(buf, c, BZ); + c = (void *)trunc_page((unsigned long)c); + if (msync((void *)c, round_page(BZ), MS_SYNC) == -1) + err(1, "msync(%p)", c); + usleep(10000 + arc4random() % 1000); + } + + if (pthread_mutex_lock(&mutex) == -1) + err(1, "pthread_mutex_lock"); + wthreads--; + if (pthread_mutex_unlock(&mutex) == -1) + err(1, "pthread_mutex_unlock"); + + return (NULL); +} + +void * +rt(void *arg __unused) +{ + int64_t pos; + char buf[BZ], *c; + +#ifdef __NP__ + pthread_set_name_np(pthread_self(), __func__); +#endif + c = p; + do { + pos = arc4random() % (len / BZ); + pos = pos * BZ; + bcopy(&c[pos], buf, BZ); + usleep(10000 + arc4random() % 1000); + } while (wthreads != 0); + + return (NULL); +} + +int +main(int argc, char *argv[]) +{ + pthread_t cp[RTHREADS + WTHREADS]; + struct stat st; + int fd, i, j, rc; + + if (argc != 2) + errx(1, "Usage: %s ", argv[0]); + if ((fd = open(argv[1], O_RDWR)) == -1) + err(1, "open %s", argv[1]); + if (fstat(fd, &st) == -1) + err(1, "stat"); + len = round_page(st.st_size); + p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if ((void *)p == MAP_FAILED) + err(1, "mmap"); + + i = 0; + for (j = 0; j < WTHREADS; j++) { + if ((rc = pthread_create(&cp[i++], NULL, wt, NULL)) != 0) + errc(1, rc, "pthread_create()"); + } + usleep(100); + for (j = 0; j < RTHREADS; j++) { + if ((rc = pthread_create(&cp[i++], NULL, rt, NULL)) != 0) + errc(1, rc, "pthread_create()"); + } + + for (j = 0; j < RTHREADS + WTHREADS; j++) + pthread_join(cp[--i], NULL); + + return (0); +}