svn commit: r278953 - user/pho/stress2/misc
Peter Holm
pho at FreeBSD.org
Wed Feb 18 09:02:59 UTC 2015
Author: pho
Date: Wed Feb 18 09:02:58 2015
New Revision: 278953
URL: https://svnweb.freebsd.org/changeset/base/278953
Log:
Added two tests using
pthread_mutexattr_setprotocol(..., PTHREAD_PRIO_INHERIT).
Sponsored by: EMC / Isilon storage division
Added:
user/pho/stress2/misc/pthread7.sh (contents, props changed)
user/pho/stress2/misc/pthread8.sh (contents, props changed)
Added: user/pho/stress2/misc/pthread7.sh
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ user/pho/stress2/misc/pthread7.sh Wed Feb 18 09:02:58 2015 (r278953)
@@ -0,0 +1,287 @@
+#!/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$
+#
+
+# PTHREAD_PRIO_INHERIT version of pthread2.sh
+
+. ../default.cfg
+
+export LANG=C
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pthread7.c
+mycc -o pthread7 -Wall -Wextra -O2 -g -gdwarf-2 pthread7.c -lpthread || exit 1
+rm -f pthread7.c
+
+for i in `jot 5`; do
+ for i in `jot 8`; do
+ /tmp/pthread7 &
+ done
+ wait
+done
+rm -f /tmp/pthread7
+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);
+ if ((rc = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT)) != 0)
+ errc(1, rc, "PTHREAD_PRIO_INHERIT");
+ 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/pthread8.sh
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ user/pho/stress2/misc/pthread8.sh Wed Feb 18 09:02:58 2015 (r278953)
@@ -0,0 +1,139 @@
+#!/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$
+#
+
+# PTHREAD_PRIO_INHERIT test scenario
+
+. ../default.cfg
+
+here=`pwd`
+cd /tmp
+sed '1,/^EOF/d' < $here/$0 > pthread8.c
+mycc -o pthread8 -Wall -Wextra -O0 -g pthread8.c -lpthread || exit 1
+#rm -f pthread8.c /tmp/pthread8.core
+
+/tmp/pthread8
+
+#rm -f /tmp/pthread8
+exit 0
+EOF
+/* $Id: pi.c,v 1.2 2015/01/31 11:36:07 kostik Exp kostik $ */
+
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <err.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+struct runner_arg {
+ pthread_mutex_t *locks;
+ u_int lock_cnt;
+};
+
+int stop;
+
+void *
+runner(void *arg)
+{
+ struct runner_arg *ra;
+ pthread_mutex_t *l;
+ u_int i;
+ int error;
+
+ ra = arg;
+ while (stop == 0) {
+ for (i = 0; i < ra->lock_cnt; i++) {
+ l = &ra->locks[i];
+ error = pthread_mutex_lock(l);
+ if (error != 0)
+ errc(1, error, "pthread_mutex_lock");
+ pthread_yield();
+ }
+ for (i = 0; i < ra->lock_cnt; i++) {
+ l = &ra->locks[i];
+ error = pthread_mutex_unlock(l);
+ if (error != 0)
+ errc(1, error, "pthread_mutex_lock");
+ pthread_yield();
+ }
+ }
+ return (NULL);
+}
+
+int
+main(void)
+{
+ struct runner_arg ra;
+ time_t start;
+ pthread_t *threads;
+ pthread_mutexattr_t mattr;
+ u_int i, ncpus;
+ int error;
+ size_t ncpus_len;
+
+ ncpus_len = sizeof(ncpus);
+ error = sysctlbyname("hw.ncpu", &ncpus, &ncpus_len, NULL, 0);
+ if (error != 0)
+ err(1, "sysctl hw.ncpus");
+ threads = calloc(ncpus, sizeof(pthread_t));
+ if (threads == NULL)
+ err(1, "calloc threads");
+
+ ra.lock_cnt = 100;
+ ra.locks = calloc(ra.lock_cnt, sizeof(pthread_mutex_t));
+ if (ra.locks == NULL)
+ err(1, "calloc locks");
+ error = pthread_mutexattr_init(&mattr);
+ if (error != 0)
+ errc(1, error, "pthread_mutexattr_init");
+ error = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
+ if (error != 0)
+ errc(1, error, "pthread_mutexattr_setprotocol PRIO_INHERIT");
+ for (i = 0; i < ra.lock_cnt; i++) {
+ error = pthread_mutex_init(&ra.locks[i], &mattr);
+ if (error != 0)
+ errc(1, error, "pthread_mutex_init");
+ }
+
+ for (i = 0; i < ncpus; i++) {
+ error = pthread_create(&threads[i], NULL, runner, &ra);
+ if (error != 0)
+ errc(1, error, "pthread_create");
+ }
+ start = time(NULL);
+ while (time(NULL) - start < 180)
+ sleep(1);
+ stop = 1;
+ for (i = 0; i < ncpus; i++)
+ pthread_join(threads[i], NULL);
+
+ return (0);
+}
More information about the svn-src-user
mailing list