svn commit: r264752 - user/pho/stress2/misc

Peter Holm pho at FreeBSD.org
Tue Apr 22 07:19:00 UTC 2014


Author: pho
Date: Tue Apr 22 07:18:59 2014
New Revision: 264752
URL: http://svnweb.freebsd.org/changeset/base/264752

Log:
  Added more pthread test scenarios.
  
  Sponsored by:	EMC / Isilon storage division

Added:
  user/pho/stress2/misc/pthread3.sh   (contents, props changed)
  user/pho/stress2/misc/pthread4.sh   (contents, props changed)
  user/pho/stress2/misc/pthread5.sh   (contents, props changed)
  user/pho/stress2/misc/pthread6.sh   (contents, props changed)

Added: user/pho/stress2/misc/pthread3.sh
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/pho/stress2/misc/pthread3.sh	Tue Apr 22 07:18:59 2014	(r264752)
@@ -0,0 +1,299 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 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$
+#
+
+# _exit(2) test scenario with focus on shared channel tear down.
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pthread3.c
+cc -o pthread3 -Wall -Wextra -O2 -g -gdwarf-2 pthread3.c -lpthread || exit 1
+rm -f pthread3.c
+
+for i in `jot 8`; do
+	/tmp/pthread3 &
+done
+for i in `jot 8`; do
+	wait
+done
+
+rm -f /tmp/pthread3
+
+exit 0
+EOF
+/*
+ * Threaded producer-consumer test.
+ * Loosly based on work by
+ * Andrey Zonov (c) 2012
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/queue.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#ifdef __FreeBSD__
+#include <pthread_np.h>
+#define	__NP__
+#endif
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#define	LOCK(x)		plock(&x.mtx)
+#define	UNLOCK(x)	punlock(&x.mtx)
+#define	SIGNAL(x)	psig(&x.wait)
+#define	WAIT(x)		pwait(&x.wait, &x.mtx)
+
+long ncreate, nrename, nunlink;
+int max;
+char *dirname1;
+char *dirname2;
+
+struct file {
+	char *name;
+	STAILQ_ENTRY(file) next;
+};
+
+struct files {
+	pthread_mutex_t mtx;
+	pthread_cond_t wait;
+	STAILQ_HEAD(, file) list;
+};
+
+static struct files newfiles;
+static struct files renamedfiles;
+
+
+static void
+hand(int i __unused) {	/* handler */
+	fprintf(stderr, "max = %d, ncreate = %ld, nrename = %ld, nunlink = %ld\n",
+			max, ncreate, nrename, nunlink);
+}
+
+static void
+ahand(int i __unused) {	/* handler */
+	fprintf(stderr, "FAIL\n");
+	hand(0);
+	_exit(0);
+}
+
+void
+plock(pthread_mutex_t *l)
+{
+	int rc;
+
+	if ((rc = pthread_mutex_lock(l)) != 0)
+		errc(1, rc, "pthread_mutex_lock");
+}
+
+void
+punlock(pthread_mutex_t *l)
+{
+	int rc;
+
+	if ((rc = pthread_mutex_unlock(l)) != 0)
+		errc(1, rc, "pthread_mutex_unlock");
+}
+
+void
+psig(pthread_cond_t *c)
+{
+	int rc;
+
+	if ((rc = pthread_cond_signal(c)) != 0)
+		errc(1, rc, "pthread_cond_signal");
+}
+
+void
+pwait(pthread_cond_t *c, pthread_mutex_t *l)
+{
+	int rc;
+
+	if ((rc = pthread_cond_wait(c, l)) != 0)
+		errc(1, rc, "pthread_cond_wait");
+}
+
+void *
+loop_create(void *arg __unused)
+{
+	int i, j;
+	struct file *file;
+
+#ifdef __NP__
+	pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+	for (i = 0; i < max; i++) {
+		file = malloc(sizeof(*file));
+		asprintf(&file->name, "%s/filename_too-long:%d", dirname1, i);
+		LOCK(newfiles);
+		STAILQ_INSERT_TAIL(&newfiles.list, file, next);
+		ncreate++;
+		UNLOCK(newfiles);
+		SIGNAL(newfiles);
+		if ((i > 0) && (i % 100000 == 0))
+			for (j = 0; j < 10 && ncreate != nrename; j++)
+				usleep(400);
+	}
+	return (NULL);
+}
+
+void *
+loop_rename(void *arg __unused)
+{
+	char *filename, *newname;
+	struct file *file;
+
+#ifdef	__NP__
+	pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+	while (nrename < max) {
+		LOCK(newfiles);
+		while (STAILQ_EMPTY(&newfiles.list)) {
+			WAIT(newfiles);
+		}
+		file = STAILQ_FIRST(&newfiles.list);
+		STAILQ_REMOVE_HEAD(&newfiles.list, next);
+		UNLOCK(newfiles);
+		filename = strrchr(file->name, '/');
+		asprintf(&newname, "%s/%s", dirname2, filename);
+		nrename++;
+		free(file->name);
+		file->name = newname;
+		LOCK(renamedfiles);
+		STAILQ_INSERT_TAIL(&renamedfiles.list, file, next);
+		UNLOCK(renamedfiles);
+		SIGNAL(renamedfiles);
+	}
+	return (NULL);
+}
+
+void *
+loop_unlink(void *arg __unused)
+{
+	struct file *file;
+
+#ifdef	__NP__
+	pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+	while (nunlink < max) {
+		LOCK(renamedfiles);
+		while (STAILQ_EMPTY(&renamedfiles.list)) {
+			WAIT(renamedfiles);
+		}
+		file = STAILQ_FIRST(&renamedfiles.list);
+		STAILQ_REMOVE_HEAD(&renamedfiles.list, next);
+		nunlink++;
+		UNLOCK(renamedfiles);
+		free(file->name);
+		free(file);
+		if (arc4random() % 100 == 1)
+			if (arc4random() % 100 == 1)
+				if (arc4random() % 100 < 10)
+					_exit(0);
+	}
+	return (NULL);
+}
+
+void
+test(void)
+{
+	int i;
+	int rc;
+	pthread_t tid[3];
+
+	asprintf(&dirname1, "%s.1", "f1");
+	asprintf(&dirname2, "%s.2", "f2");
+	max = 15000000;
+
+	STAILQ_INIT(&newfiles.list);
+	STAILQ_INIT(&renamedfiles.list);
+
+	if ((rc = pthread_mutex_init(&newfiles.mtx, NULL)) != 0)
+		errc(1, rc, "pthread_mutex_init()");
+	if ((rc = pthread_cond_init(&newfiles.wait, NULL)) != 0)
+		errc(1, rc, "pthread_cond_init()");
+	if ((rc = pthread_mutex_init(&renamedfiles.mtx, NULL)) != 0)
+		errc(1, rc, "pthread_mutex_init()");
+	if ((rc = pthread_cond_init(&renamedfiles.wait, NULL)) != 0)
+		errc(1, rc, "pthread_cond_init()");
+
+	signal(SIGINFO, hand);
+	signal(SIGALRM, ahand);
+	alarm(300);
+	if ((rc = pthread_create(&tid[0], NULL, loop_create, NULL)) != 0)
+		errc(1, rc, "pthread_create()");
+	if ((rc = pthread_create(&tid[1], NULL, loop_rename, NULL)) != 0)
+		errc(1, rc, "pthread_create()");
+	if ((rc = pthread_create(&tid[2], NULL, loop_unlink, NULL)) != 0)
+		errc(1, rc, "pthread_create()");
+
+	for (i = 0; i < 3; i++) {
+		if ((rc = pthread_join(tid[i], NULL)) != 0)
+			errc(1, rc, "pthread_join(%d)", i);
+	}
+
+	if ((rc = pthread_mutex_destroy(&newfiles.mtx)) != 0)
+		errc(1, rc, "pthread_mutex_destroy(newfiles)");
+	if ((rc = pthread_cond_destroy(&newfiles.wait)) != 0)
+		errc(1, rc, "pthread_cond_destroy(newfiles)");
+	if ((rc = pthread_mutex_destroy(&renamedfiles.mtx)) != 0)
+		errc(1, rc, "pthread_mutex_destroy(renamedfiles)");
+	if ((rc = pthread_cond_destroy(&renamedfiles.wait)) != 0)
+		errc(1, rc, "pthread_cond_destroy(renamedfiles)");
+	free(dirname1);
+	free(dirname2);
+
+	_exit(0);
+}
+
+int
+main(void)
+{
+	int i;
+
+	for (i = 0; i < 1000; i++) {
+		if (fork() == 0)
+			test();
+		wait(NULL);
+	}
+
+	return (0);
+}

Added: user/pho/stress2/misc/pthread4.sh
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/pho/stress2/misc/pthread4.sh	Tue Apr 22 07:18:59 2014	(r264752)
@@ -0,0 +1,299 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 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$
+#
+
+# PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP version of pthread2.sh
+
+export LANG=C
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pthread4.c
+cc -o pthread4 -Wall -Wextra -O2 -g -gdwarf-2 pthread4.c -lpthread || exit 1
+rm -f pthread4.c /tmp/pthread4.core
+
+log=/tmp/pthread4.`date '+%Y%m%d-%H%M'`
+for i in `jot 5`; do
+	[ $i -eq 1 ] && echo "# `uname -v`"
+	time sh -c '
+	for i in `jot 8`; do
+		/tmp/pthread4 &
+	done
+	for i in `jot 8`; do
+		wait
+	done
+	'
+done > $log 2>&1
+rm -f /tmp/pthread4
+
+if [ -n "$bench" ]; then
+	pair=`ls /tmp/pthread4* | egrep "pthread4\.[0-9]{8}-" | sort |
+	    tail -2 | tr '\n' ' '`
+	ministat -w 72 $pair
+else
+	rm -f $log
+fi
+
+exit 0
+EOF
+/*
+ * Threaded producer-consumer test.
+ * Loosly based on work by
+ * Andrey Zonov (c) 2012
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/queue.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#ifdef __FreeBSD__
+#include <pthread_np.h>
+#define	__NP__
+#endif
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#define	LOCK(x)		plock(&x.mtx)
+#define	UNLOCK(x)	punlock(&x.mtx)
+#define	SIGNAL(x)	psig(&x.wait)
+#define	WAIT(x)		pwait(&x.wait, &x.mtx)
+
+long ncreate, nrename, nunlink;
+int bench, max;
+char *dirname1;
+char *dirname2;
+
+struct file {
+	char *name;
+	STAILQ_ENTRY(file) next;
+};
+
+struct files {
+	pthread_mutex_t mtx;
+	pthread_cond_t wait;
+	STAILQ_HEAD(, file) list;
+};
+
+static struct files newfiles;
+static struct files renamedfiles;
+
+
+static void
+hand(int i __unused) {	/* handler */
+	fprintf(stderr, "max = %d, ncreate = %ld, nrename = %ld, nunlink = %ld\n",
+			max, ncreate, nrename, nunlink);
+}
+
+static void
+ahand(int i __unused) {	/* handler */
+	fprintf(stderr, "FAIL\n");
+	hand(0);
+	_exit(0);
+}
+
+void
+plock(pthread_mutex_t *l)
+{
+	int rc;
+
+	if ((rc = pthread_mutex_lock(l)) != 0)
+		errc(1, rc, "pthread_mutex_lock");
+}
+
+void
+punlock(pthread_mutex_t *l)
+{
+	int rc;
+
+	if ((rc = pthread_mutex_unlock(l)) != 0)
+		errc(1, rc, "pthread_mutex_unlock");
+}
+
+void
+psig(pthread_cond_t *c)
+{
+	int rc;
+
+	if ((rc = pthread_cond_signal(c)) != 0)
+		errc(1, rc, "pthread_cond_signal");
+}
+
+void
+pwait(pthread_cond_t *c, pthread_mutex_t *l)
+{
+	int rc;
+
+	if ((rc = pthread_cond_wait(c, l)) != 0)
+		errc(1, rc, "pthread_cond_wait");
+}
+
+void *
+loop_create(void *arg __unused)
+{
+	int i, j;
+	struct file *file;
+
+#ifdef __NP__
+	pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+	for (i = 0; i < max; i++) {
+		file = malloc(sizeof(*file));
+		asprintf(&file->name, "%s/filename_too-long:%d", dirname1, i);
+		LOCK(newfiles);
+		STAILQ_INSERT_TAIL(&newfiles.list, file, next);
+		ncreate++;
+		UNLOCK(newfiles);
+		SIGNAL(newfiles);
+		if ((bench == 0) && (i > 0) && (i % 100000 == 0))
+			for (j = 0; j < 10 && ncreate != nrename; j++)
+				usleep(400);
+	}
+	return (NULL);
+}
+
+void *
+loop_rename(void *arg __unused)
+{
+	char *filename, *newname;
+	struct file *file;
+
+#ifdef	__NP__
+	pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+	while (nrename < max) {
+		LOCK(newfiles);
+		while (STAILQ_EMPTY(&newfiles.list)) {
+			WAIT(newfiles);
+		}
+		file = STAILQ_FIRST(&newfiles.list);
+		STAILQ_REMOVE_HEAD(&newfiles.list, next);
+		UNLOCK(newfiles);
+		filename = strrchr(file->name, '/');
+		asprintf(&newname, "%s/%s", dirname2, filename);
+		nrename++;
+		free(file->name);
+		file->name = newname;
+		LOCK(renamedfiles);
+		STAILQ_INSERT_TAIL(&renamedfiles.list, file, next);
+		UNLOCK(renamedfiles);
+		SIGNAL(renamedfiles);
+	}
+	return (NULL);
+}
+
+void *
+loop_unlink(void *arg __unused)
+{
+	struct file *file;
+
+#ifdef	__NP__
+	pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+	while (nunlink < max) {
+		LOCK(renamedfiles);
+		while (STAILQ_EMPTY(&renamedfiles.list)) {
+			WAIT(renamedfiles);
+		}
+		file = STAILQ_FIRST(&renamedfiles.list);
+		STAILQ_REMOVE_HEAD(&renamedfiles.list, next);
+		nunlink++;
+		UNLOCK(renamedfiles);
+		free(file->name);
+		free(file);
+	}
+	return (NULL);
+}
+
+int
+main(void)
+{
+	int i;
+	int rc;
+	pthread_t tid[3];
+	pthread_mutexattr_t attr, *pattr = NULL;
+
+	bench = getenv("bench") != NULL;
+	asprintf(&dirname1, "%s.1", "f1");
+	asprintf(&dirname2, "%s.2", "f2");
+	max = 15000000;
+
+	STAILQ_INIT(&newfiles.list);
+	STAILQ_INIT(&renamedfiles.list);
+
+	pthread_mutexattr_init (&attr);
+	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
+	pattr = &attr;
+	if ((rc = pthread_mutex_init(&newfiles.mtx, pattr)) != 0)
+		errc(1, rc, "pthread_mutex_init()");
+	if ((rc = pthread_cond_init(&newfiles.wait, NULL)) != 0)
+		errc(1, rc, "pthread_cond_init()");
+	if ((rc = pthread_mutex_init(&renamedfiles.mtx, NULL)) != 0)
+		errc(1, rc, "pthread_mutex_init()");
+	if ((rc = pthread_cond_init(&renamedfiles.wait, NULL)) != 0)
+		errc(1, rc, "pthread_cond_init()");
+
+	signal(SIGINFO, hand);
+	signal(SIGALRM, ahand);
+	alarm(300);
+	if ((rc = pthread_create(&tid[0], NULL, loop_create, NULL)) != 0)
+		errc(1, rc, "pthread_create()");
+	if ((rc = pthread_create(&tid[1], NULL, loop_rename, NULL)) != 0)
+		errc(1, rc, "pthread_create()");
+	if ((rc = pthread_create(&tid[2], NULL, loop_unlink, NULL)) != 0)
+		errc(1, rc, "pthread_create()");
+
+	for (i = 0; i < 3; i++) {
+		if ((rc = pthread_join(tid[i], NULL)) != 0)
+			errc(1, rc, "pthread_join(%d)", i);
+	}
+
+	if ((rc = pthread_mutex_destroy(&newfiles.mtx)) != 0)
+		errc(1, rc, "pthread_mutex_destroy(newfiles)");
+	if ((rc = pthread_cond_destroy(&newfiles.wait)) != 0)
+		errc(1, rc, "pthread_cond_destroy(newfiles)");
+	if ((rc = pthread_mutex_destroy(&renamedfiles.mtx)) != 0)
+		errc(1, rc, "pthread_mutex_destroy(renamedfiles)");
+	if ((rc = pthread_cond_destroy(&renamedfiles.wait)) != 0)
+		errc(1, rc, "pthread_cond_destroy(renamedfiles)");
+	free(dirname1);
+	free(dirname2);
+
+	return (0);
+}

Added: user/pho/stress2/misc/pthread5.sh
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/pho/stress2/misc/pthread5.sh	Tue Apr 22 07:18:59 2014	(r264752)
@@ -0,0 +1,120 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 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$
+#
+
+# Stress shchan allocations.
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pthread5.c
+cc -o pthread5 -Wall -Wextra -O2 pthread5.c -lpthread || exit 1
+rm -f pthread5.c
+
+/tmp/pthread5
+
+rm -f /tmp/pthread5
+exit 0
+EOF
+#include <sys/types.h>
+#include <err.h>
+#include <errno.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/event.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define ITER 2
+#define PARALLEL 1000
+#define THREADS 100
+
+pthread_cond_t cond  = PTHREAD_COND_INITIALIZER;
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void *
+nicethreads(void *data __unused)
+{
+	struct timespec ts;
+	struct timeval tp;
+
+	pthread_mutex_lock(&mutex);
+	gettimeofday(&tp, NULL);
+
+	ts.tv_sec  = tp.tv_sec;
+	ts.tv_nsec = tp.tv_usec * 1000;
+	ts.tv_sec += 30;
+
+	pthread_cond_timedwait(&cond, &mutex, &ts);
+	pthread_mutex_unlock(&mutex);
+
+	return (NULL);
+}
+
+int
+test(void)
+{
+	int num_thread = THREADS;
+	pthread_t tid[num_thread];
+	int i, iter, rc;
+
+	for (iter = 0; iter < ITER; iter++) {
+		for (i = 0; i < num_thread; i++) {
+			if ((rc = pthread_create(&tid[i], NULL, nicethreads,
+			    NULL)) == -1)
+				errc(1, rc, "pthread_create");
+		}
+		usleep(20000);
+		for (i = 0; i < num_thread; i++) {
+			rc = pthread_mutex_lock(&mutex);
+			rc = pthread_cond_signal(&cond);
+			rc = pthread_mutex_unlock(&mutex);
+		}
+		for (i = 0; i < num_thread; i++)
+			if ((rc = pthread_join(tid[i], NULL)) == -1)
+				errc(1, rc, "pthread_join");
+	}
+
+	_exit(0);
+}
+
+int
+main(void)
+{
+	int i;
+
+	for (i = 0; i < PARALLEL; i++)
+		if (fork() == 0)
+			test();
+	for (i = 0; i < PARALLEL; i++)
+		wait(NULL);
+
+	return (0);
+}

Added: user/pho/stress2/misc/pthread6.sh
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/pho/stress2/misc/pthread6.sh	Tue Apr 22 07:18:59 2014	(r264752)
@@ -0,0 +1,307 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2014 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$
+#
+
+# "panic: pmap active 0xfffff80128cd84b8" seen:
+# http://people.freebsd.org/~pho/stress/log/attilio101.txt
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pthread6.c
+cc -o pthread6 -Wall -Wextra -O2 -g -gdwarf-2 pthread6.c -lpthread || exit 1
+rm -f pthread6.c /tmp/pthread6.core
+rm -f /tmp/pthread6.core
+
+daemon sh -c "(cd $here/../testcases/swap; ./swap -t 2m -i 20 -k)"
+rnd=`od -An -N1 -t u1 /dev/urandom | sed 's/ //g'`
+sleep $((rnd % 10))
+for i in `jot 50`; do
+	/tmp/pthread6
+done
+killall -q swap
+
+rm -f /tmp/pthread6 /tmp/pthread6.core
+exit 0
+EOF
+/*
+ * Threaded producer-consumer test.
+ * Loosly based on work by
+ * Andrey Zonov (c) 2012
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/queue.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#ifdef __FreeBSD__
+#include <pthread_np.h>
+#define	__NP__
+#endif
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+#define	LOCK(x)		plock(&x.mtx)
+#define	UNLOCK(x)	punlock(&x.mtx)
+#define	SIGNAL(x)	psig(&x.wait)
+#define	WAIT(x)		pwait(&x.wait, &x.mtx)
+
+#define PARALLEL 4
+#define LOOPS 2
+
+long ncreate, nrename, nunlink;
+int bench, max;
+char *dirname1;
+char *dirname2;
+
+struct file {
+	char *name;
+	STAILQ_ENTRY(file) next;
+};
+
+struct files {
+	pthread_mutex_t mtx;
+	pthread_cond_t wait;
+	STAILQ_HEAD(, file) list;
+};
+
+static struct files newfiles;
+static struct files renamedfiles;
+
+
+static void
+hand(int i __unused) {	/* handler */
+}
+
+static void
+ahand(int i __unused) {	/* handler */
+	abort();
+}
+
+void
+plock(pthread_mutex_t *l)
+{
+	int rc;
+
+	if ((rc = pthread_mutex_lock(l)) != 0)
+		errc(1, rc, "pthread_mutex_lock");
+}
+
+void
+punlock(pthread_mutex_t *l)
+{
+	int rc;
+
+	if ((rc = pthread_mutex_unlock(l)) != 0)
+		errc(1, rc, "pthread_mutex_unlock");
+}
+
+void
+psig(pthread_cond_t *c)
+{
+	int rc;
+
+	if ((rc = pthread_cond_signal(c)) != 0)
+		errc(1, rc, "pthread_cond_signal");
+}
+
+void
+pwait(pthread_cond_t *c, pthread_mutex_t *l)
+{
+	int rc;
+
+	if ((rc = pthread_cond_wait(c, l)) != 0)
+		errc(1, rc, "pthread_cond_wait");
+}
+
+void *
+loop_create(void *arg __unused)
+{
+	int i, j;
+	struct file *file;
+
+#ifdef __NP__
+	pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+	for (i = 0; i < max; i++) {
+		file = malloc(sizeof(*file));
+		asprintf(&file->name, "%s/filename_too-long:%d", dirname1, i);
+		LOCK(newfiles);
+		STAILQ_INSERT_TAIL(&newfiles.list, file, next);
+		ncreate++;
+		UNLOCK(newfiles);
+		SIGNAL(newfiles);
+		if ((bench == 0) && (i > 0) && (i % 100000 == 0))
+			for (j = 0; j < 10 && ncreate != nrename; j++)
+				usleep(400);
+	}
+	return (NULL);
+}
+
+void *
+loop_rename(void *arg __unused)
+{
+	char *filename, *newname;
+	struct file *file;
+
+#ifdef	__NP__
+	pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+	while (nrename < max) {
+		LOCK(newfiles);
+		while (STAILQ_EMPTY(&newfiles.list)) {
+			WAIT(newfiles);
+		}
+		file = STAILQ_FIRST(&newfiles.list);
+		STAILQ_REMOVE_HEAD(&newfiles.list, next);
+		UNLOCK(newfiles);
+		filename = strrchr(file->name, '/');
+		asprintf(&newname, "%s/%s", dirname2, filename);
+		nrename++;
+		free(file->name);
+		file->name = newname;
+		LOCK(renamedfiles);
+		STAILQ_INSERT_TAIL(&renamedfiles.list, file, next);
+		UNLOCK(renamedfiles);
+		SIGNAL(renamedfiles);
+	}
+	return (NULL);
+}
+
+void *
+loop_unlink(void *arg __unused)
+{
+	struct file *file;
+
+#ifdef	__NP__
+	pthread_set_name_np(pthread_self(), __func__);
+#endif
+
+	while (nunlink < max) {
+		LOCK(renamedfiles);
+		while (STAILQ_EMPTY(&renamedfiles.list)) {
+			WAIT(renamedfiles);
+		}
+		file = STAILQ_FIRST(&renamedfiles.list);
+		STAILQ_REMOVE_HEAD(&renamedfiles.list, next);
+		nunlink++;
+		UNLOCK(renamedfiles);
+		free(file->name);
+		free(file);
+	}
+	return (NULL);
+}
+
+void
+test(void)
+{
+	int i;
+	int rc;
+	pthread_t tid[3];
+	pthread_mutexattr_t attr, *pattr = NULL;
+
+	bench = getenv("bench") != NULL;
+	bench = 1;
+	asprintf(&dirname1, "%s.1", "f1");
+	asprintf(&dirname2, "%s.2", "f2");
+	max = 15000000;
+
+	STAILQ_INIT(&newfiles.list);
+	STAILQ_INIT(&renamedfiles.list);
+
+	pthread_mutexattr_init (&attr);
+	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
+	pattr = &attr;
+	if ((rc = pthread_mutex_init(&newfiles.mtx, pattr)) != 0)
+		errc(1, rc, "pthread_mutex_init()");
+	if ((rc = pthread_cond_init(&newfiles.wait, NULL)) != 0)
+		errc(1, rc, "pthread_cond_init()");
+	if ((rc = pthread_mutex_init(&renamedfiles.mtx, NULL)) != 0)
+		errc(1, rc, "pthread_mutex_init()");
+	if ((rc = pthread_cond_init(&renamedfiles.wait, NULL)) != 0)
+		errc(1, rc, "pthread_cond_init()");
+
+	signal(SIGINFO, hand);
+	signal(SIGALRM, ahand);
+	if ((rc = pthread_create(&tid[0], NULL, loop_create, NULL)) != 0)
+		errc(1, rc, "pthread_create()");
+	if ((rc = pthread_create(&tid[1], NULL, loop_rename, NULL)) != 0)
+		errc(1, rc, "pthread_create()");
+	if ((rc = pthread_create(&tid[2], NULL, loop_unlink, NULL)) != 0)
+		errc(1, rc, "pthread_create()");
+
+	usleep(1000);

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***


More information about the svn-src-user mailing list