Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 07 Aug 2012 19:26:23 +0000
From:      gmiller@FreeBSD.org
To:        svn-soc-all@FreeBSD.org
Subject:   socsvn commit: r240178 - in soc2012/gmiller/locking-head: . include lib/libwitness tools/regression/lib/libthr/lockprof tools/regression/lib/libwitness
Message-ID:  <20120807192623.AC1DC106567C@hub.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: gmiller
Date: Tue Aug  7 19:26:23 2012
New Revision: 240178
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=240178

Log:
   r240239@FreeBSD-dev:  root | 2012-07-25 09:06:18 -0500
   Add XML output file to libwitness.

Added:
  soc2012/gmiller/locking-head/lib/libwitness/xml.c
Modified:
  soc2012/gmiller/locking-head/   (props changed)
  soc2012/gmiller/locking-head/include/pthread_np.h
  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/witness.h
  soc2012/gmiller/locking-head/tools/regression/lib/libthr/lockprof/Makefile
  soc2012/gmiller/locking-head/tools/regression/lib/libwitness/Makefile

Modified: soc2012/gmiller/locking-head/include/pthread_np.h
==============================================================================
--- soc2012/gmiller/locking-head/include/pthread_np.h	Tue Aug  7 19:26:02 2012	(r240177)
+++ soc2012/gmiller/locking-head/include/pthread_np.h	Tue Aug  7 19:26:23 2012	(r240178)
@@ -73,6 +73,7 @@
 struct pthread_lockorder_np {
 	struct _pthread_lockorder_private *_pvt;
 	void		*lock;
+	char		*name;
 	void		*child;
 	void		*sibling;
 };

Modified: soc2012/gmiller/locking-head/lib/libwitness/Makefile
==============================================================================
--- soc2012/gmiller/locking-head/lib/libwitness/Makefile	Tue Aug  7 19:26:02 2012	(r240177)
+++ soc2012/gmiller/locking-head/lib/libwitness/Makefile	Tue Aug  7 19:26:23 2012	(r240178)
@@ -4,7 +4,7 @@
 
 LIB=		witness
 SHLIB_MAJOR=	1
-SRCS=		wrappers.c graph.c lists.c logs.c lockinfo.c
+SRCS=		wrappers.c graph.c lists.c logs.c lockinfo.c xml.c
 DPADD=		${LIBTHR}
 LDADD=		-lthr
 

Modified: soc2012/gmiller/locking-head/lib/libwitness/lists.c
==============================================================================
--- soc2012/gmiller/locking-head/lib/libwitness/lists.c	Tue Aug  7 19:26:02 2012	(r240177)
+++ soc2012/gmiller/locking-head/lib/libwitness/lists.c	Tue Aug  7 19:26:23 2012	(r240178)
@@ -35,8 +35,9 @@
 static _Thread_local SLIST_HEAD(lock_head, lock_entry) lock_head =
     SLIST_HEAD_INITIALIZER(lock_head);
 
-static int reset_count = 0;
+static int	reset_count = 0;
 static _Thread_local int thread_reset_count = 0;
+static int	exit_set = 0;
 
 void
 add_lock(struct lock_info *lock)
@@ -44,6 +45,11 @@
 	struct lock_entry *entry;
 	struct lock_entry *next;
 
+	if (exit_set == 0) {
+		atexit(write_xml);
+		exit_set = 1;
+	}
+
 	if (lock == NULL) {
 		return;
 	}

Modified: soc2012/gmiller/locking-head/lib/libwitness/lockinfo.c
==============================================================================
--- soc2012/gmiller/locking-head/lib/libwitness/lockinfo.c	Tue Aug  7 19:26:02 2012	(r240177)
+++ soc2012/gmiller/locking-head/lib/libwitness/lockinfo.c	Tue Aug  7 19:26:23 2012	(r240178)
@@ -162,6 +162,7 @@
 		ret = ENOMEM;
 	} else {
 		node->_pvt->last_record = NULL;
+		node->name = NULL;
 	}
 
 	pthread_mutex_unlock(&witness_mtx);
@@ -172,35 +173,50 @@
 int
 pthread_lockorder_next_np(struct pthread_lockorder_np *node)
 {
-	if (node->_pvt != NULL) {
-		if (node->_pvt->last_record == NULL) {
-			node->_pvt->last_record = SLIST_FIRST(&lock_info_head);
-		} else {
-			node->_pvt->last_record =
-			    SLIST_NEXT(node->_pvt->last_record,
-				       lock_info_next);
-		}
-
-		if (node->_pvt->last_record != NULL) {
-			node->lock = node->_pvt->last_record->lock;
-			if (node->_pvt->last_record->child != NULL) {
-				node->child =
-				    node->_pvt->last_record->child->lock;
-			} else {
-				node->child = NULL;
-			}
-			if (node->_pvt->last_record->sibling != NULL) {
-				node->sibling =
-				    node->_pvt->last_record->sibling->lock;
-			} else {
-				node->sibling = NULL;
-			}
+	if (node->_pvt == NULL) {
+		return (0);
+	}
+
+	if (node->_pvt->last_record == NULL) {
+		node->_pvt->last_record = SLIST_FIRST(&lock_info_head);
+	} else {
+		node->_pvt->last_record =
+		    SLIST_NEXT(node->_pvt->last_record, lock_info_next);
+	}
+
+	if (node->_pvt->last_record == NULL) {
+		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);
+		node->name = NULL;
+	}
 
-			return (1);
-		}
+	node->lock = node->_pvt->last_record->lock;
+	if (node->_pvt->last_record->name != NULL) {
+		node->name = strdup(node->_pvt->last_record->name);
+	}
+	if (node->_pvt->last_record->child != NULL) {
+		node->child = node->_pvt->last_record->child->lock;
+	} else {
+		node->child = NULL;
+	}
+	if (node->_pvt->last_record->sibling != NULL) {
+		node->sibling = node->_pvt->last_record->sibling->lock;
+	} else {
+		node->sibling = NULL;
 	}
 
-	return (0);
+	pthread_mutex_unlock(&witness_mtx);
+
+	return (1);
 }
 
 void
@@ -208,4 +224,9 @@
 {
 	free(node->_pvt);
 	node->_pvt = NULL;
+
+	if (node->name != NULL) {
+		free((char *)node->name);
+		node->name = NULL;
+	}
 }

Modified: soc2012/gmiller/locking-head/lib/libwitness/witness.h
==============================================================================
--- soc2012/gmiller/locking-head/lib/libwitness/witness.h	Tue Aug  7 19:26:02 2012	(r240177)
+++ soc2012/gmiller/locking-head/lib/libwitness/witness.h	Tue Aug  7 19:26:23 2012	(r240178)
@@ -67,3 +67,5 @@
 int		blessed(struct lock_info *first, struct lock_info *second);
 void		reset_lock_info(void);
 void		check_default_name(struct lock_info *lock, const char *prefix);
+
+void		write_xml(void);

Added: soc2012/gmiller/locking-head/lib/libwitness/xml.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2012/gmiller/locking-head/lib/libwitness/xml.c	Tue Aug  7 19:26:23 2012	(r240178)
@@ -0,0 +1,134 @@
+/*-
+ * Copyright (c) 2012 Greg Miller <gmiller@freebsd.org>..
+ * 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.
+ *
+ */
+
+#include <pthread_np.h>
+#include <stdio.h>
+
+#include "witness.h"
+
+static int xml_indent = 0;
+static FILE *xml_file;
+
+static void
+indent(void)
+{
+	int		i;
+
+	for (i = 0; i < xml_indent; i++) {
+	 	fputc(' ', xml_file);
+	}
+}
+
+static void
+open_element(const char *tag)
+{
+	indent();
+	fprintf(xml_file, "<%s>\n", tag);
+
+	xml_indent += 2;
+}
+
+static void
+close_element(const char *tag)
+{
+	xml_indent -= 2;
+
+	indent();
+	fprintf(xml_file, "</%s>\n", tag);
+}
+
+static void
+write_element_string(const char *tag, const char *str)
+{
+	indent();
+	fprintf(xml_file, "<%s>%s</%s>\n", tag, str, tag);
+}
+
+static void
+write_element_pointer(const char *tag, void *ptr)
+{
+	indent();
+	fprintf(xml_file, "<%s>%p</%s>\n", tag, ptr, tag);
+}
+
+void
+write_xml(void)
+{
+	struct pthread_lor_np lor;
+	struct pthread_lockorder_np node;
+
+	xml_file = fopen("witness.xml", "wt");
+
+	open_element("witness");
+
+	open_element("log");
+
+	pthread_lor_begin_np(&lor);
+	while (pthread_lor_next_np(&lor)) {
+		open_element("lor");
+
+		open_element("first");
+		write_element_pointer("address", lor.lock_first);
+		write_element_string("name", lor.name_first);
+		close_element("first");
+
+		open_element("second");
+		write_element_pointer("address", lor.lock_second);
+		write_element_string("name", lor.name_second);
+		close_element("second");
+
+		close_element("lor");
+	}
+	pthread_lor_end_np(&lor);
+
+	close_element("log");
+
+	open_element("graph");
+
+	pthread_lockorder_begin_np(&node);
+	while (pthread_lockorder_next_np(&node)) {
+		open_element("node");
+
+		write_element_pointer("address", node.lock);
+		write_element_string("name", node.name);
+		if (node.child != NULL) {
+			write_element_pointer("child", node.child);
+		}
+		if (node.sibling != NULL) {
+			write_element_pointer("sibling", node.sibling);
+		}
+
+		close_element("node");
+	}
+	pthread_lockorder_end_np(&node);
+
+	close_element("graph");
+
+	close_element("witness");
+
+	fclose(xml_file);
+}

Modified: soc2012/gmiller/locking-head/tools/regression/lib/libthr/lockprof/Makefile
==============================================================================
--- soc2012/gmiller/locking-head/tools/regression/lib/libthr/lockprof/Makefile	Tue Aug  7 19:26:02 2012	(r240177)
+++ soc2012/gmiller/locking-head/tools/regression/lib/libthr/lockprof/Makefile	Tue Aug  7 19:26:23 2012	(r240178)
@@ -9,7 +9,7 @@
 
 .PHONY: clean
 clean:
-	-rm -f ${TESTS}
+	-rm -f ${TESTS} lockprof.xml
 
 lock-cycle: lock-cycle.c check.c
 	${CC} -o lock-cycle lock-cycle.c check.c ${CFLAGS}

Modified: soc2012/gmiller/locking-head/tools/regression/lib/libwitness/Makefile
==============================================================================
--- soc2012/gmiller/locking-head/tools/regression/lib/libwitness/Makefile	Tue Aug  7 19:26:02 2012	(r240177)
+++ soc2012/gmiller/locking-head/tools/regression/lib/libwitness/Makefile	Tue Aug  7 19:26:23 2012	(r240178)
@@ -9,7 +9,7 @@
 
 .PHONY: clean
 clean:
-	-rm -f ${TESTS}
+	-rm -f ${TESTS} witness.xml
 
 lor-basic: lor-basic.c check.c
 	${CC} -o lor-basic lor-basic.c check.c ${CFLAGS}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20120807192623.AC1DC106567C>