socsvn commit: r240481 - in soc2012/gmiller/locking-head: . lib/libwitness

gmiller at FreeBSD.org gmiller at FreeBSD.org
Fri Aug 17 23:05:47 UTC 2012


Author: gmiller
Date: Fri Aug 17 23:05:44 2012
New Revision: 240481
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=240481

Log:
   r240502 at FreeBSD-dev:  root | 2012-08-15 14:29:18 -0500
   Reorganize some witness code, cleaning up and correcting the code that
   prevents witness from including its own lock usage in the graphs.

Added:
  soc2012/gmiller/locking-head/lib/libwitness/api.c
     - copied, changed from r240291, soc2012/gmiller/locking-head/lib/libwitness/wrappers.c
Deleted:
  soc2012/gmiller/locking-head/lib/libwitness/wrappers.c
Modified:
  soc2012/gmiller/locking-head/   (props changed)
  soc2012/gmiller/locking-head/lib/libwitness/Makefile
  soc2012/gmiller/locking-head/lib/libwitness/lists.c
  soc2012/gmiller/locking-head/lib/libwitness/lockinfo.c
  soc2012/gmiller/locking-head/lib/libwitness/logs.c
  soc2012/gmiller/locking-head/lib/libwitness/witness.h

Modified: soc2012/gmiller/locking-head/lib/libwitness/Makefile
==============================================================================
--- soc2012/gmiller/locking-head/lib/libwitness/Makefile	Fri Aug 17 20:15:01 2012	(r240480)
+++ soc2012/gmiller/locking-head/lib/libwitness/Makefile	Fri Aug 17 23:05:44 2012	(r240481)
@@ -4,7 +4,7 @@
 
 LIB=		witness
 SHLIB_MAJOR=	1
-SRCS=		wrappers.c graph.c lists.c logs.c lockinfo.c xml.c unwind.c
+SRCS=		api.c graph.c lists.c logs.c lockinfo.c xml.c unwind.c
 DPADD=		${LIBTHR}
 LDADD=		-lthr
 

Copied and modified: soc2012/gmiller/locking-head/lib/libwitness/api.c (from r240291, soc2012/gmiller/locking-head/lib/libwitness/wrappers.c)
==============================================================================
--- soc2012/gmiller/locking-head/lib/libwitness/wrappers.c	Sun Aug 12 07:09:25 2012	(r240291, copy source)
+++ soc2012/gmiller/locking-head/lib/libwitness/api.c	Fri Aug 17 23:05:44 2012	(r240481)
@@ -50,21 +50,44 @@
 
 pthread_mutex_t witness_mtx = PTHREAD_MUTEX_INITIALIZER;
 
+static int witness_depth = 0;
+
+void
+enter_witness(void)
+{
+	_pthread_mutex_lock(&witness_mtx);
+
+	witness_depth++;
+}
+
+void
+leave_witness(void)
+{
+	witness_depth--;
+
+	_pthread_mutex_unlock(&witness_mtx);
+}
+
+int
+in_witness(void) {
+	return (witness_depth);
+}
+
 int
 pthread_mutex_lock(pthread_mutex_t *mutex)
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	check_default_name(mutex, "mutex");
 
 	ret = _pthread_mutex_lock(mutex);
-	if (mutex != &witness_mtx && ret == 0) {
+	if (ret == 0) {
 		add_lock(mutex);
 	}
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -74,16 +97,16 @@
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	check_default_name(mutex, "mutex");
 
 	ret = _pthread_mutex_trylock(mutex);
-	if (mutex != &witness_mtx && ret == 0) {
+	if (ret == 0) {
 		add_lock(mutex);
 	}
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -93,16 +116,16 @@
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	check_default_name(mutex, "mutex");
 
 	ret = _pthread_mutex_timedlock(mutex, ts);
-	if (mutex != &witness_mtx && ret == 0) {
+	if (ret == 0) {
 		add_lock(mutex);
 	}
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -112,14 +135,14 @@
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	ret = _pthread_mutex_unlock(mutex);
-	if (mutex != &witness_mtx && ret == 0) {
+	if (ret == 0) {
 		remove_lock(mutex);
 	}
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -129,12 +152,12 @@
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	destroy_lock(mutex);
 	ret = _pthread_mutex_destroy(mutex);
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -144,7 +167,7 @@
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	check_default_name(rwlock, "rwlock");
 
@@ -153,7 +176,7 @@
 		add_lock(rwlock);
 	}
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -163,7 +186,7 @@
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	check_default_name(rwlock, "rwlock");
 
@@ -172,7 +195,7 @@
 		add_lock(rwlock);
 	}
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -182,7 +205,7 @@
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	check_default_name(rwlock, "rwlock");
 
@@ -191,7 +214,7 @@
 		add_lock(rwlock);
 	}
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -201,7 +224,7 @@
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	check_default_name(rwlock, "rwlock");
 
@@ -210,7 +233,7 @@
 		add_lock(rwlock);
 	}
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -220,7 +243,7 @@
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	check_default_name(rwlock, "rwlock");
 
@@ -229,7 +252,7 @@
 		add_lock(rwlock);
 	}
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -239,7 +262,7 @@
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	check_default_name(rwlock, "rwlock");
 
@@ -248,7 +271,7 @@
 		add_lock(rwlock);
 	}
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -258,14 +281,14 @@
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	ret = _pthread_rwlock_unlock(rwlock);
 	if (ret == 0) {
 		remove_lock(rwlock);
 	}
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -275,12 +298,12 @@
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	destroy_lock(rwlock);
 	ret = _pthread_rwlock_destroy(rwlock);
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -290,7 +313,7 @@
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	check_default_name(spin, "spinlock");
 
@@ -299,7 +322,7 @@
 		add_lock(spin);
 	}
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -309,7 +332,7 @@
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	check_default_name(spin, "spinlock");
 
@@ -318,7 +341,7 @@
 		add_lock(spin);
 	}
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -328,14 +351,14 @@
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	ret = _pthread_spin_unlock(spin);
 	if (ret == 0) {
 		remove_lock(spin);
 	}
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -345,12 +368,12 @@
 {
 	int		ret;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	destroy_lock(spin);
 	ret = _pthread_spin_destroy(spin);
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -360,14 +383,14 @@
 {
 	int ret = 0;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	if (insert_lock(get_lock_info(lookup_lock(first)),
 			get_lock_info(lookup_lock(second))) < 0) {
 		ret = EINVAL;
 	}
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -383,7 +406,7 @@
 	struct blessing *second_bless = NULL;
 	int ret = 0;
 
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	first_inst = lookup_lock(first_addr);
 	second_inst = lookup_lock(second_addr);
@@ -410,7 +433,7 @@
 				  bless_next);
 	}
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -418,29 +441,53 @@
 void
 pthread_lockorder_reset_np(void)
 {
-	_pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	reset_lists();
 	reset_lock_instance();
 	reset_lock_info();
 
-	_pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 }
 
 int
 pthread_mutex_setname_np(pthread_mutex_t *mutex, const char *name)
 {
-	return (set_lock_name(mutex, name));
+	int		ret;
+
+	enter_witness();
+
+	ret = set_lock_name(mutex, name);
+
+	leave_witness();
+
+	return (ret);
 }
 
 int
 pthread_rwlock_setname_np(pthread_rwlock_t *rwlock, const char *name)
 {
-	return (set_lock_name(rwlock, name));
+	int		ret;
+
+	enter_witness();
+
+	ret = set_lock_name(rwlock, name);
+
+	leave_witness();
+
+	return (ret);
 }
 
 int
 pthread_spin_setname_np(pthread_spinlock_t *spin, const char *name)
 {
-	return (set_lock_name(spin, name));
+	int		ret;
+
+	enter_witness();
+
+	ret = set_lock_name(spin, name);
+
+	leave_witness();
+
+	return (ret);
 }

Modified: soc2012/gmiller/locking-head/lib/libwitness/lists.c
==============================================================================
--- soc2012/gmiller/locking-head/lib/libwitness/lists.c	Fri Aug 17 20:15:01 2012	(r240480)
+++ soc2012/gmiller/locking-head/lib/libwitness/lists.c	Fri Aug 17 23:05:44 2012	(r240481)
@@ -68,19 +68,16 @@
 	struct lock_entry *next;
 	struct lock_instance *inst;
 	struct lock_info *info;
-	static int	in_add_lock = 0;
 
 	if (exit_set == 0) {
 		atexit(write_xml);
 		exit_set = 1;
 	}
 
-	if (in_add_lock || lock == NULL) {
+	if (in_witness() > 1 || lock == NULL) {
 		return;
 	}
 
-	in_add_lock = 1;
-
 	inst = lookup_lock(lock);
 	info = get_lock_info(inst);
 
@@ -88,7 +85,6 @@
 
 	entry = malloc(sizeof(*entry));
 	if (entry == NULL) {
-		in_add_lock = 0;
 		return;
 	}
 
@@ -116,8 +112,6 @@
 		log_reversal(entry->lock, &entry->trace, next->lock,
 			     &next->trace);
 	}
-
-	in_add_lock = 0;
 }
 
 void

Modified: soc2012/gmiller/locking-head/lib/libwitness/lockinfo.c
==============================================================================
--- soc2012/gmiller/locking-head/lib/libwitness/lockinfo.c	Fri Aug 17 20:15:01 2012	(r240480)
+++ soc2012/gmiller/locking-head/lib/libwitness/lockinfo.c	Fri Aug 17 23:05:44 2012	(r240481)
@@ -241,11 +241,7 @@
 {
 	int		ret = 0;
 
-	/*
-	  The lock isn't needed to prevent races, but it is needed to ensure
-	  that any locks grabbed by malloc() don't get logged.
-	*/
-	pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	node->_pvt = malloc(sizeof(struct _pthread_lockorder_private));
 	if (node->_pvt == NULL) {
@@ -255,7 +251,7 @@
 		node->name = NULL;
 	}
 
-	pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -267,6 +263,8 @@
 		return (0);
 	}
 
+	enter_witness();
+
 	if (node->_pvt->last_record == NULL) {
 		node->_pvt->last_record = SLIST_FIRST(&lock_info_head);
 	} else {
@@ -275,15 +273,11 @@
 	}
 
 	if (node->_pvt->last_record == NULL) {
+		leave_witness();
+
 		return (0);
 	}
 
-	/*
-	  The lock isn't needed to prevent races, but it is needed to ensure
-	  that any locks grabbed for memory allocation don't get logged.
-	*/
-	pthread_mutex_lock(&witness_mtx);
-
 	if (node->name != NULL) {
 		free(node->name);
 	}
@@ -302,7 +296,7 @@
 		node->sibling = NULL;
 	}
 
-	pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (1);
 }

Modified: soc2012/gmiller/locking-head/lib/libwitness/logs.c
==============================================================================
--- soc2012/gmiller/locking-head/lib/libwitness/logs.c	Fri Aug 17 20:15:01 2012	(r240480)
+++ soc2012/gmiller/locking-head/lib/libwitness/logs.c	Fri Aug 17 23:05:44 2012	(r240481)
@@ -68,11 +68,7 @@
 	lor->name_second = NULL;
 	lor->second_trace = NULL;
 
-        /*
-	  The lock isn't needed to prevent races, but it is needed to ensure
-	  that any locks grabbed by malloc() don't get logged.
-	*/
-	pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	lor->_pvt = malloc(sizeof(struct _pthread_lor_private));
 	if (lor->_pvt == NULL) {
@@ -81,7 +77,7 @@
 		lor->_pvt->last_record = NULL;
 	}
 
-	pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (ret);
 }
@@ -91,7 +87,7 @@
 {
 	int		res = 0;
 
-	pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	if (lor->_pvt == NULL || lor->_pvt->last_record == NULL) {
 		lor->_pvt->last_record = STAILQ_FIRST(&lor_head);
@@ -113,7 +109,7 @@
 		res = 1;
 	}
 
-	pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 
 	return (res);
 }
@@ -149,7 +145,7 @@
 	struct lor_entry *lor;
 	struct lor_entry *lor_temp;
 
-	pthread_mutex_lock(&witness_mtx);
+	enter_witness();
 
 	STAILQ_FOREACH_SAFE(lor, &lor_head, lor_next, lor_temp) {
 		STAILQ_REMOVE(&lor_head, lor, lor_entry, lor_next);
@@ -165,5 +161,5 @@
 		free(lor);
 	}
 
-	pthread_mutex_unlock(&witness_mtx);
+	leave_witness();
 }

Modified: soc2012/gmiller/locking-head/lib/libwitness/witness.h
==============================================================================
--- soc2012/gmiller/locking-head/lib/libwitness/witness.h	Fri Aug 17 20:15:01 2012	(r240480)
+++ soc2012/gmiller/locking-head/lib/libwitness/witness.h	Fri Aug 17 23:05:44 2012	(r240481)
@@ -62,8 +62,6 @@
 
 SLIST_HEAD(backtrace, stack_frame);
 
-extern pthread_mutex_t witness_mtx;
-
 void		add_lock(void *lock);
 void		remove_lock(void *lock);
 void		free_frame(struct backtrace *trace);
@@ -92,3 +90,7 @@
 
 void		record_backtrace(struct backtrace *trace);
 char		*trace_str(struct backtrace *trace);
+
+void		enter_witness(void);
+void		leave_witness(void);
+int		in_witness(void);


More information about the svn-soc-all mailing list