[asterisk-commits] kpfleming: branch 1.4 r49553 - in /branches/1.4: build_tools/ include/ includ...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Thu Jan 4 15:51:02 MST 2007


Author: kpfleming
Date: Thu Jan  4 16:51:01 2007
New Revision: 49553

URL: http://svn.digium.com/view/asterisk?view=rev&rev=49553
Log:
add support for tracking thread-local-storage objects that exist via 'threadstorage' CLI commands

Added:
    branches/1.4/main/threadstorage.c   (with props)
Modified:
    branches/1.4/build_tools/cflags.xml
    branches/1.4/include/asterisk.h
    branches/1.4/include/asterisk/threadstorage.h
    branches/1.4/main/Makefile
    branches/1.4/main/asterisk.c
    branches/1.4/main/utils.c

Modified: branches/1.4/build_tools/cflags.xml
URL: http://svn.digium.com/view/asterisk/branches/1.4/build_tools/cflags.xml?view=diff&rev=49553&r1=49552&r2=49553
==============================================================================
--- branches/1.4/build_tools/cflags.xml (original)
+++ branches/1.4/build_tools/cflags.xml Thu Jan  4 16:51:01 2007
@@ -4,6 +4,8 @@
 		<member name="DEBUG_SCHEDULER" displayname="Enable Scheduler Debugging Output">
 		</member>
 		<member name="DEBUG_THREADS" displayname="Enable Thread Debugging">
+		</member>
+		<member name="DEBUG_THREADLOCALS" displayname="Enable Thread-Local-Storage Debugging">
 		</member>
 		<member name="DETECT_DEADLOCKS" displayname="Detect Deadlocks">
 		</member>

Modified: branches/1.4/include/asterisk.h
URL: http://svn.digium.com/view/asterisk/branches/1.4/include/asterisk.h?view=diff&rev=49553&r1=49552&r2=49553
==============================================================================
--- branches/1.4/include/asterisk.h (original)
+++ branches/1.4/include/asterisk.h Thu Jan  4 16:51:01 2007
@@ -71,6 +71,7 @@
 int dnsmgr_init(void);				/*!< Provided by dnsmgr.c */ 
 void dnsmgr_start_refresh(void);		/*!< Provided by dnsmgr.c */
 int dnsmgr_reload(void);			/*!< Provided by dnsmgr.c */
+void threadstorage_init(void);			/*!< Provided by threadstorage.c */
 
 /* Many headers need 'ast_channel' to be defined */
 struct ast_channel;

Modified: branches/1.4/include/asterisk/threadstorage.h
URL: http://svn.digium.com/view/asterisk/branches/1.4/include/asterisk/threadstorage.h?view=diff&rev=49553&r1=49552&r2=49553
==============================================================================
--- branches/1.4/include/asterisk/threadstorage.h (original)
+++ branches/1.4/include/asterisk/threadstorage.h Thu Jan  4 16:51:01 2007
@@ -43,6 +43,13 @@
 	void (*key_init)(void);
 };
 
+
+#if defined(DEBUG_THREADLOCALS)
+void __ast_threadstorage_object_add(void *key, size_t len, const char *file, const char *function, unsigned int line);
+void __ast_threadstorage_object_remove(void *key);
+void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len);
+#endif /* defined(DEBUG_THREADLOCALS) */
+
 /*!
  * \brief Define a thread storage variable
  *
@@ -61,6 +68,7 @@
 #define AST_THREADSTORAGE(name, name_init) \
 	AST_THREADSTORAGE_CUSTOM(name, name_init, ast_free) 
 
+#if !defined(DEBUG_THREADLOCALS)
 #define AST_THREADSTORAGE_CUSTOM(name, name_init, cleanup)  \
 static void name_init(void);                                \
 static struct ast_threadstorage name = {                    \
@@ -71,6 +79,23 @@
 {                                                           \
 	pthread_key_create(&(name).key, cleanup);           \
 }
+#else /* defined(DEBUG_THREADLOCALS) */
+#define AST_THREADSTORAGE_CUSTOM(name, name_init, cleanup)  \
+static void name_init(void);                                \
+static struct ast_threadstorage name = {                    \
+	.once = PTHREAD_ONCE_INIT,                          \
+	.key_init = name_init,                              \
+};                                                          \
+static void __cleanup_##name(void *data)		    \
+{							    \
+	__ast_threadstorage_object_remove(data);	    \
+	cleanup(data);					    \
+}							    \
+static void name_init(void)                                 \
+{                                                           \
+	pthread_key_create(&(name).key, __cleanup_##name);  \
+}
+#endif /* defined(DEBUG_THREADLOCALS) */
 
 /*!
  * \brief Retrieve thread storage
@@ -102,6 +127,7 @@
  * }
  * \endcode
  */
+#if !defined(DEBUG_THREADLOCALS)
 AST_INLINE_API(
 void *ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size),
 {
@@ -117,6 +143,26 @@
 	return buf;
 }
 )
+#else /* defined(DEBUG_THREADLOCALS) */
+AST_INLINE_API(
+void *__ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size, const char *file, const char *function, unsigned int line),
+{
+	void *buf;
+
+	pthread_once(&ts->once, ts->key_init);
+	if (!(buf = pthread_getspecific(ts->key))) {
+		if (!(buf = ast_calloc(1, init_size)))
+			return NULL;
+		pthread_setspecific(ts->key, buf);
+		__ast_threadstorage_object_add(buf, init_size, file, function, line);
+	}
+
+	return buf;
+}
+)
+
+#define ast_threadstorage_get(ts, init_size) __ast_threadstorage_get(ts, init_size, __FILE__, __PRETTY_FUNCTION__, __LINE__)
+#endif /* defined(DEBUG_THREADLOCALS) */
 
 /*!
  * \brief A dynamic length string
@@ -184,6 +230,7 @@
  * }
  * \endcode
  */
+#if !defined(DEBUG_THREADLOCALS)
 AST_INLINE_API(
 struct ast_dynamic_str *ast_dynamic_str_thread_get(struct ast_threadstorage *ts,
 	size_t init_len),
@@ -199,6 +246,25 @@
 	return buf;
 }
 )
+#else /* defined(DEBUG_THREADLOCALS) */
+AST_INLINE_API(
+struct ast_dynamic_str *__ast_dynamic_str_thread_get(struct ast_threadstorage *ts,
+	size_t init_len, const char *file, const char *function, unsigned int line),
+{
+	struct ast_dynamic_str *buf;
+
+	if (!(buf = __ast_threadstorage_get(ts, sizeof(*buf) + init_len, file, function, line)))
+		return NULL;
+	
+	if (!buf->len)
+		buf->len = init_len;
+
+	return buf;
+}
+)
+
+#define ast_dynamic_str_thread_get(ts, init_len) __ast_dynamic_str_thread_get(ts, init_len, __FILE__, __PRETTY_FUNCTION__, __LINE__)
+#endif /* defined(DEBUG_THREADLOCALS) */ 
 
 /*!
  * \brief Error codes from ast_dynamic_str_thread_build_va()

Modified: branches/1.4/main/Makefile
URL: http://svn.digium.com/view/asterisk/branches/1.4/main/Makefile?view=diff&rev=49553&r1=49552&r2=49553
==============================================================================
--- branches/1.4/main/Makefile (original)
+++ branches/1.4/main/Makefile Thu Jan  4 16:51:01 2007
@@ -26,7 +26,7 @@
 	utils.o plc.o jitterbuf.o dnsmgr.o devicestate.o \
 	netsock.o slinfactory.o ast_expr2.o ast_expr2f.o \
 	cryptostub.o sha1.o http.o fixedjitterbuf.o abstract_jb.o \
-	strcompat.o
+	strcompat.o threadstorage.o
 
 # we need to link in the objects statically, not as a library, because
 # otherwise modules will not have them available if none of the static

Modified: branches/1.4/main/asterisk.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/main/asterisk.c?view=diff&rev=49553&r1=49552&r2=49553
==============================================================================
--- branches/1.4/main/asterisk.c (original)
+++ branches/1.4/main/asterisk.c Thu Jan  4 16:51:01 2007
@@ -2714,6 +2714,9 @@
 		printf(term_quit());
 		exit(1);
 	}
+
+	threadstorage_init();
+
 	if (load_modules(1)) {
 		printf(term_quit());
 		exit(1);

Added: branches/1.4/main/threadstorage.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/main/threadstorage.c?view=auto&rev=49553
==============================================================================
--- branches/1.4/main/threadstorage.c (added)
+++ branches/1.4/main/threadstorage.c Thu Jan  4 16:51:01 2007
@@ -1,0 +1,227 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2005, Digium, Inc.
+ *
+ * Kevin P. Fleming <kpfleming at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Debugging support for thread-local-storage objects
+ *
+ * \author Kevin P. Fleming <kpfleming at digium.com>
+ */
+
+#include "asterisk.h"
+
+#if defined(DEBUG_THREADLOCALS)
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "asterisk/logger.h"
+#include "asterisk/strings.h"
+#include "asterisk/utils.h"
+#include "asterisk/threadstorage.h"
+#include "asterisk/linkedlists.h"
+#include "asterisk/cli.h"
+
+struct tls_object {
+	void *key;
+	size_t size;
+	const char *file;
+	const char *function;
+	unsigned int line;
+	pthread_t thread;
+	AST_LIST_ENTRY(tls_object) entry;
+};
+
+static AST_LIST_HEAD_STATIC(tls_objects, tls_object);
+
+void __ast_threadstorage_object_add(void *key, size_t len, const char *file, const char *function, unsigned int line)
+{
+	struct tls_object *to;
+
+	if (!(to = ast_calloc(sizeof(*to), 1)))
+		return;
+
+	to->key = key;
+	to->size = len;
+	to->file = file;
+	to->function = function;
+	to->line = line;
+	to->thread = pthread_self();
+
+	AST_LIST_LOCK(&tls_objects);
+	AST_LIST_INSERT_TAIL(&tls_objects, to, entry);
+	AST_LIST_UNLOCK(&tls_objects);
+}
+
+void __ast_threadstorage_object_remove(void *key)
+{
+	struct tls_object *to;
+
+	AST_LIST_LOCK(&tls_objects);
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&tls_objects, to, entry) {
+		if (to->key == key) {
+			AST_LIST_REMOVE_CURRENT(&tls_objects, entry);
+			break;
+		}
+	}
+	AST_LIST_TRAVERSE_SAFE_END;
+	AST_LIST_UNLOCK(&tls_objects);
+	if (to)
+		free(to);
+}
+
+void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len)
+{
+	struct tls_object *to;
+
+	AST_LIST_LOCK(&tls_objects);
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&tls_objects, to, entry) {
+		if (to->key == key_old) {
+			to->key = key_new;
+			to->size = len;
+			break;
+		}
+	}
+	AST_LIST_TRAVERSE_SAFE_END;
+	AST_LIST_UNLOCK(&tls_objects);
+}
+
+static int handle_show_allocations(int fd, int argc, char *argv[])
+{
+	char *fn = NULL;
+	size_t len = 0;
+	unsigned int count = 0;
+	struct tls_object *to;
+
+	if (argc > 3)
+		fn = argv[3];
+
+	AST_LIST_LOCK(&tls_objects);
+
+	AST_LIST_TRAVERSE(&tls_objects, to, entry) {
+		if (fn && strcasecmp(to->file, fn))
+			continue;
+
+		ast_cli(fd, "%10d bytes allocated in %20s at line %5d of %25s (thread %p)\n",
+			(int) to->size, to->function, to->line, to->file, (void *) to->thread);
+		len += to->size;
+		count++;
+	}
+
+	AST_LIST_UNLOCK(&tls_objects);
+
+	ast_cli(fd, "%10d bytes allocated in %d allocation%s\n", (int) len, count, count > 1 ? "s" : "");
+	
+	return RESULT_SUCCESS;
+}
+
+static int handle_show_summary(int fd, int argc, char *argv[])
+{
+	char *fn = NULL;
+	size_t len = 0;
+	unsigned int count = 0;
+	struct tls_object *to;
+	struct file {
+		const char *name;
+		size_t len;
+		unsigned int count;
+		AST_LIST_ENTRY(file) entry;
+	} *file;
+	AST_LIST_HEAD_NOLOCK_STATIC(file_summary, file);
+
+	if (argc > 3)
+		fn = argv[3];
+
+	AST_LIST_LOCK(&tls_objects);
+
+	AST_LIST_TRAVERSE(&tls_objects, to, entry) {
+		if (fn && strcasecmp(to->file, fn))
+			continue;
+
+		AST_LIST_TRAVERSE(&file_summary, file, entry) {
+			if ((!fn && (file->name == to->file)) || (fn && (file->name == to->function)))
+				break;
+		}
+
+		if (!file) {
+			file = alloca(sizeof(*file));
+			memset(file, 0, sizeof(*file));
+			file->name = fn ? to->function : to->file;
+			AST_LIST_INSERT_TAIL(&file_summary, file, entry);
+		}
+
+		file->len += to->size;
+		file->count++;
+	}
+
+	AST_LIST_UNLOCK(&tls_objects);
+	
+	AST_LIST_TRAVERSE(&file_summary, file, entry) {
+		len += file->len;
+		count += file->count;
+		if (fn) {
+			ast_cli(fd, "%10d bytes in %d allocation%ss in function %s\n",
+				(int) file->len, file->count, file->count > 1 ? "s" : "", file->name);
+		} else {
+			ast_cli(fd, "%10d bytes in %d allocation%s in file %s\n",
+				(int) file->len, file->count, file->count > 1 ? "s" : "", file->name);
+		}
+	}
+
+	ast_cli(fd, "%10d bytes allocated in %d allocation%s\n", (int) len, count, count > 1 ? "s" : "");
+	
+	return RESULT_SUCCESS;
+}
+
+static struct ast_cli_entry cli[] = {
+	{
+		.cmda = { "threadstorage", "show", "allocations", NULL },
+		.handler = handle_show_allocations,
+		.summary = "Display outstanding thread local storage allocations",
+		.usage =
+		"Usage: threadstorage show allocations [<file>]\n"
+		"       Dumps a list of all thread-specific memory allocations,\n"
+		"optionally limited to those from a specific file\n",
+	},
+	{
+		.cmda = { "threadstorage", "show", "summary", NULL },
+		.handler = handle_show_summary,
+		.summary = "Summarize outstanding memory allocations",
+		.usage =
+		"Usage: threadstorage show summary [<file>]\n"
+		"       Summarizes thread-specific memory allocations by file, or optionally\n"
+		"by function, if a file is specified\n",
+	},
+};
+
+void threadstorage_init(void)
+{
+	ast_cli_register_multiple(cli, sizeof(cli) / sizeof(cli[0]));
+}
+
+#else /* !defined(DEBUG_THREADLOCALS) */
+
+void threadstorage_init(void)
+{
+}
+
+#endif /* !defined(DEBUG_THREADLOCALS) */
+

Propchange: branches/1.4/main/threadstorage.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: branches/1.4/main/threadstorage.c
------------------------------------------------------------------------------
    svn:keywords = Author Id Date Revision

Propchange: branches/1.4/main/threadstorage.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: branches/1.4/main/utils.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/main/utils.c?view=diff&rev=49553&r1=49552&r2=49553
==============================================================================
--- branches/1.4/main/utils.c (original)
+++ branches/1.4/main/utils.c Thu Jan  4 16:51:01 2007
@@ -962,6 +962,9 @@
 {
 	int res;
 	int offset = (append && (*buf)->len) ? strlen((*buf)->str) : 0;
+#if defined(DEBUG_THREADLOCALS)
+	struct ast_dynamic_str *old_buf = *buf;
+#endif /* defined(DEBUG_THREADLOCALS) */
 
 	res = vsnprintf((*buf)->str + offset, (*buf)->len - offset, fmt, ap);
 
@@ -984,8 +987,12 @@
 		if (append)
 			(*buf)->str[offset] = '\0';
 
-		if (ts)
+		if (ts) {
 			pthread_setspecific(ts->key, *buf);
+#if defined(DEBUG_THREADLOCALS)
+			__ast_threadstorage_object_replace(old_buf, *buf, (*buf)->len + sizeof(*(*buf)));
+#endif /* defined(DEBUG_THREADLOCALS) */
+		}
 
 		/* va_end() and va_start() must be done before calling
 		 * vsnprintf() again. */



More information about the asterisk-commits mailing list