| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 1999 - 2005, Digium, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Kevin P. Fleming <kpfleming@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@digium.com> | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "asterisk.h"
 | 
					
						
							| 
									
										
										
										
											2007-11-20 22:18:21 +00:00
										 |  |  | #include "asterisk/_private.h"
 | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-20 22:18:21 +00:00
										 |  |  | #if !defined(DEBUG_THREADLOCALS)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void threadstorage_init(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #else /* !defined(DEBUG_THREADLOCALS) */
 | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #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; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-05 17:49:03 +00:00
										 |  |  | static AST_LIST_HEAD_NOLOCK_STATIC(tls_objects, tls_object); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-16 17:14:01 +00:00
										 |  |  | /* Allow direct use of pthread_mutex_t and friends */ | 
					
						
							|  |  |  | #undef pthread_mutex_t
 | 
					
						
							|  |  |  | #undef pthread_mutex_lock
 | 
					
						
							|  |  |  | #undef pthread_mutex_unlock
 | 
					
						
							|  |  |  | #undef pthread_mutex_init
 | 
					
						
							|  |  |  | #undef pthread_mutex_destroy
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*!
 | 
					
						
							|  |  |  |  * \brief lock for the tls_objects list | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \note We can not use an ast_mutex_t for this.  The reason is that this | 
					
						
							|  |  |  |  *       lock is used within the context of thread-local data destructors, | 
					
						
							|  |  |  |  *       and the ast_mutex_* API uses thread-local data.  Allocating more | 
					
						
							|  |  |  |  *       thread-local data at that point just causes a memory leak. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static pthread_mutex_t threadstoragelock; | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void __ast_threadstorage_object_add(void *key, size_t len, const char *file, const char *function, unsigned int line) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct tls_object *to; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-16 14:21:27 +00:00
										 |  |  | 	if (!(to = ast_calloc(1, sizeof(*to)))) | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	to->key = key; | 
					
						
							|  |  |  | 	to->size = len; | 
					
						
							|  |  |  | 	to->file = file; | 
					
						
							|  |  |  | 	to->function = function; | 
					
						
							|  |  |  | 	to->line = line; | 
					
						
							|  |  |  | 	to->thread = pthread_self(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-16 17:14:01 +00:00
										 |  |  | 	pthread_mutex_lock(&threadstoragelock); | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 	AST_LIST_INSERT_TAIL(&tls_objects, to, entry); | 
					
						
							| 
									
										
										
										
											2008-12-16 17:14:01 +00:00
										 |  |  | 	pthread_mutex_unlock(&threadstoragelock); | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void __ast_threadstorage_object_remove(void *key) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct tls_object *to; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-16 17:14:01 +00:00
										 |  |  | 	pthread_mutex_lock(&threadstoragelock); | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 	AST_LIST_TRAVERSE_SAFE_BEGIN(&tls_objects, to, entry) { | 
					
						
							|  |  |  | 		if (to->key == key) { | 
					
						
							| 
									
										
										
										
											2007-11-08 21:31:06 +00:00
										 |  |  | 			AST_LIST_REMOVE_CURRENT(entry); | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	AST_LIST_TRAVERSE_SAFE_END; | 
					
						
							| 
									
										
										
										
											2008-12-16 17:14:01 +00:00
										 |  |  | 	pthread_mutex_unlock(&threadstoragelock); | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 	if (to) | 
					
						
							| 
									
										
										
										
											2007-06-06 21:20:11 +00:00
										 |  |  | 		ast_free(to); | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct tls_object *to; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-16 17:14:01 +00:00
										 |  |  | 	pthread_mutex_lock(&threadstoragelock); | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 	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; | 
					
						
							| 
									
										
										
										
											2008-12-16 17:14:01 +00:00
										 |  |  | 	pthread_mutex_unlock(&threadstoragelock); | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | static char *handle_cli_threadstorage_show_allocations(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-06-01 15:23:21 +00:00
										 |  |  | 	const char *fn = NULL; | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 	size_t len = 0; | 
					
						
							|  |  |  | 	unsigned int count = 0; | 
					
						
							|  |  |  | 	struct tls_object *to; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 	switch (cmd) { | 
					
						
							|  |  |  | 	case CLI_INIT: | 
					
						
							|  |  |  | 		e->command = "threadstorage show allocations"; | 
					
						
							|  |  |  | 		e->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"; | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	case CLI_GENERATE: | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (a->argc > 4) | 
					
						
							|  |  |  | 		return CLI_SHOWUSAGE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (a->argc > 3) | 
					
						
							|  |  |  | 		fn = a->argv[3]; | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-16 17:14:01 +00:00
										 |  |  | 	pthread_mutex_lock(&threadstoragelock); | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	AST_LIST_TRAVERSE(&tls_objects, to, entry) { | 
					
						
							|  |  |  | 		if (fn && strcasecmp(to->file, fn)) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 		ast_cli(a->fd, "%10d bytes allocated in %20s at line %5d of %25s (thread %p)\n", | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 			(int) to->size, to->function, to->line, to->file, (void *) to->thread); | 
					
						
							|  |  |  | 		len += to->size; | 
					
						
							|  |  |  | 		count++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-16 17:14:01 +00:00
										 |  |  | 	pthread_mutex_unlock(&threadstoragelock); | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 	ast_cli(a->fd, "%10d bytes allocated in %d allocation%s\n", (int) len, count, count > 1 ? "s" : ""); | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 	return CLI_SUCCESS; | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | static char *handle_cli_threadstorage_show_summary(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-06-01 15:23:21 +00:00
										 |  |  | 	const char *fn = NULL; | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 	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; | 
					
						
							| 
									
										
										
										
											2007-10-16 14:27:27 +00:00
										 |  |  | 	AST_LIST_HEAD_NOLOCK_STATIC(file_summary, file); | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	switch (cmd) { | 
					
						
							|  |  |  | 	case CLI_INIT: | 
					
						
							|  |  |  | 		e->command = "threadstorage show summary"; | 
					
						
							|  |  |  | 		e->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"; | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	case CLI_GENERATE: | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (a->argc > 4) | 
					
						
							|  |  |  | 		return CLI_SHOWUSAGE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (a->argc > 3) | 
					
						
							|  |  |  | 		fn = a->argv[3]; | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-16 17:14:01 +00:00
										 |  |  | 	pthread_mutex_lock(&threadstoragelock); | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-16 17:14:01 +00:00
										 |  |  | 	pthread_mutex_unlock(&threadstoragelock); | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	AST_LIST_TRAVERSE(&file_summary, file, entry) { | 
					
						
							|  |  |  | 		len += file->len; | 
					
						
							|  |  |  | 		count += file->count; | 
					
						
							|  |  |  | 		if (fn) { | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 			ast_cli(a->fd, "%10d bytes in %d allocation%ss in function %s\n", | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 				(int) file->len, file->count, file->count > 1 ? "s" : "", file->name); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 			ast_cli(a->fd, "%10d bytes in %d allocation%s in file %s\n", | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | 				(int) file->len, file->count, file->count > 1 ? "s" : "", file->name); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 	ast_cli(a->fd, "%10d bytes allocated in %d allocation%s\n", (int) len, count, count > 1 ? "s" : ""); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return CLI_SUCCESS; | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct ast_cli_entry cli[] = { | 
					
						
							| 
									
										
										
										
											2007-10-22 20:05:18 +00:00
										 |  |  | 	AST_CLI_DEFINE(handle_cli_threadstorage_show_allocations, "Display outstanding thread local storage allocations"), | 
					
						
							|  |  |  | 	AST_CLI_DEFINE(handle_cli_threadstorage_show_summary,     "Summarize outstanding memory allocations") | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void threadstorage_init(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-16 17:14:01 +00:00
										 |  |  | 	pthread_mutex_init(&threadstoragelock, NULL); | 
					
						
							| 
									
										
										
										
											2008-07-11 18:09:35 +00:00
										 |  |  | 	ast_cli_register_multiple(cli, ARRAY_LEN(cli)); | 
					
						
							| 
									
										
										
										
											2007-01-04 23:18:36 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* !defined(DEBUG_THREADLOCALS) */
 | 
					
						
							|  |  |  | 
 |