threadstorage.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Kevin P. Fleming <kpfleming@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Debugging support for thread-local-storage objects
  21. *
  22. * \author Kevin P. Fleming <kpfleming@digium.com>
  23. */
  24. #include "asterisk.h"
  25. #include "asterisk/_private.h"
  26. #if !defined(DEBUG_THREADLOCALS)
  27. void threadstorage_init(void)
  28. {
  29. }
  30. #else /* !defined(DEBUG_THREADLOCALS) */
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/strings.h"
  33. #include "asterisk/utils.h"
  34. #include "asterisk/threadstorage.h"
  35. #include "asterisk/linkedlists.h"
  36. #include "asterisk/cli.h"
  37. struct tls_object {
  38. void *key;
  39. size_t size;
  40. const char *file;
  41. const char *function;
  42. unsigned int line;
  43. pthread_t thread;
  44. AST_LIST_ENTRY(tls_object) entry;
  45. };
  46. static AST_LIST_HEAD_NOLOCK_STATIC(tls_objects, tls_object);
  47. /* Allow direct use of pthread_mutex_t and friends */
  48. #undef pthread_mutex_t
  49. #undef pthread_mutex_lock
  50. #undef pthread_mutex_unlock
  51. #undef pthread_mutex_init
  52. #undef pthread_mutex_destroy
  53. /*!
  54. * \brief lock for the tls_objects list
  55. *
  56. * \note We can not use an ast_mutex_t for this. The reason is that this
  57. * lock is used within the context of thread-local data destructors,
  58. * and the ast_mutex_* API uses thread-local data. Allocating more
  59. * thread-local data at that point just causes a memory leak.
  60. */
  61. static pthread_mutex_t threadstoragelock;
  62. void __ast_threadstorage_object_add(void *key, size_t len, const char *file, const char *function, unsigned int line)
  63. {
  64. struct tls_object *to;
  65. if (!(to = ast_calloc(1, sizeof(*to))))
  66. return;
  67. to->key = key;
  68. to->size = len;
  69. to->file = file;
  70. to->function = function;
  71. to->line = line;
  72. to->thread = pthread_self();
  73. pthread_mutex_lock(&threadstoragelock);
  74. AST_LIST_INSERT_TAIL(&tls_objects, to, entry);
  75. pthread_mutex_unlock(&threadstoragelock);
  76. }
  77. void __ast_threadstorage_object_remove(void *key)
  78. {
  79. struct tls_object *to;
  80. pthread_mutex_lock(&threadstoragelock);
  81. AST_LIST_TRAVERSE_SAFE_BEGIN(&tls_objects, to, entry) {
  82. if (to->key == key) {
  83. AST_LIST_REMOVE_CURRENT(entry);
  84. break;
  85. }
  86. }
  87. AST_LIST_TRAVERSE_SAFE_END;
  88. pthread_mutex_unlock(&threadstoragelock);
  89. if (to)
  90. ast_free(to);
  91. }
  92. void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len)
  93. {
  94. struct tls_object *to;
  95. pthread_mutex_lock(&threadstoragelock);
  96. AST_LIST_TRAVERSE_SAFE_BEGIN(&tls_objects, to, entry) {
  97. if (to->key == key_old) {
  98. to->key = key_new;
  99. to->size = len;
  100. break;
  101. }
  102. }
  103. AST_LIST_TRAVERSE_SAFE_END;
  104. pthread_mutex_unlock(&threadstoragelock);
  105. }
  106. static char *handle_cli_threadstorage_show_allocations(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  107. {
  108. char *fn = NULL;
  109. size_t len = 0;
  110. unsigned int count = 0;
  111. struct tls_object *to;
  112. switch (cmd) {
  113. case CLI_INIT:
  114. e->command = "threadstorage show allocations";
  115. e->usage =
  116. "Usage: threadstorage show allocations [<file>]\n"
  117. " Dumps a list of all thread-specific memory allocations,\n"
  118. " optionally limited to those from a specific file\n";
  119. return NULL;
  120. case CLI_GENERATE:
  121. return NULL;
  122. }
  123. if (a->argc > 4)
  124. return CLI_SHOWUSAGE;
  125. if (a->argc > 3)
  126. fn = a->argv[3];
  127. pthread_mutex_lock(&threadstoragelock);
  128. AST_LIST_TRAVERSE(&tls_objects, to, entry) {
  129. if (fn && strcasecmp(to->file, fn))
  130. continue;
  131. ast_cli(a->fd, "%10d bytes allocated in %20s at line %5d of %25s (thread %p)\n",
  132. (int) to->size, to->function, to->line, to->file, (void *) to->thread);
  133. len += to->size;
  134. count++;
  135. }
  136. pthread_mutex_unlock(&threadstoragelock);
  137. ast_cli(a->fd, "%10d bytes allocated in %d allocation%s\n", (int) len, count, count > 1 ? "s" : "");
  138. return CLI_SUCCESS;
  139. }
  140. static char *handle_cli_threadstorage_show_summary(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  141. {
  142. char *fn = NULL;
  143. size_t len = 0;
  144. unsigned int count = 0;
  145. struct tls_object *to;
  146. struct file {
  147. const char *name;
  148. size_t len;
  149. unsigned int count;
  150. AST_LIST_ENTRY(file) entry;
  151. } *file;
  152. AST_LIST_HEAD_NOLOCK_STATIC(file_summary, file);
  153. switch (cmd) {
  154. case CLI_INIT:
  155. e->command = "threadstorage show summary";
  156. e->usage =
  157. "Usage: threadstorage show summary [<file>]\n"
  158. " Summarizes thread-specific memory allocations by file, or optionally\n"
  159. " by function, if a file is specified\n";
  160. return NULL;
  161. case CLI_GENERATE:
  162. return NULL;
  163. }
  164. if (a->argc > 4)
  165. return CLI_SHOWUSAGE;
  166. if (a->argc > 3)
  167. fn = a->argv[3];
  168. pthread_mutex_lock(&threadstoragelock);
  169. AST_LIST_TRAVERSE(&tls_objects, to, entry) {
  170. if (fn && strcasecmp(to->file, fn))
  171. continue;
  172. AST_LIST_TRAVERSE(&file_summary, file, entry) {
  173. if ((!fn && (file->name == to->file)) || (fn && (file->name == to->function)))
  174. break;
  175. }
  176. if (!file) {
  177. file = alloca(sizeof(*file));
  178. memset(file, 0, sizeof(*file));
  179. file->name = fn ? to->function : to->file;
  180. AST_LIST_INSERT_TAIL(&file_summary, file, entry);
  181. }
  182. file->len += to->size;
  183. file->count++;
  184. }
  185. pthread_mutex_unlock(&threadstoragelock);
  186. AST_LIST_TRAVERSE(&file_summary, file, entry) {
  187. len += file->len;
  188. count += file->count;
  189. if (fn) {
  190. ast_cli(a->fd, "%10d bytes in %d allocation%ss in function %s\n",
  191. (int) file->len, file->count, file->count > 1 ? "s" : "", file->name);
  192. } else {
  193. ast_cli(a->fd, "%10d bytes in %d allocation%s in file %s\n",
  194. (int) file->len, file->count, file->count > 1 ? "s" : "", file->name);
  195. }
  196. }
  197. ast_cli(a->fd, "%10d bytes allocated in %d allocation%s\n", (int) len, count, count > 1 ? "s" : "");
  198. return CLI_SUCCESS;
  199. }
  200. static struct ast_cli_entry cli[] = {
  201. AST_CLI_DEFINE(handle_cli_threadstorage_show_allocations, "Display outstanding thread local storage allocations"),
  202. AST_CLI_DEFINE(handle_cli_threadstorage_show_summary, "Summarize outstanding memory allocations")
  203. };
  204. void threadstorage_init(void)
  205. {
  206. pthread_mutex_init(&threadstoragelock, NULL);
  207. ast_cli_register_multiple(cli, ARRAY_LEN(cli));
  208. }
  209. #endif /* !defined(DEBUG_THREADLOCALS) */