5a6f4b3e86fd1bbd833b0bef9f63bc7889d1b541
[asterisk/asterisk.git] / main / threadstorage.c
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
19 /*! \file
20  *
21  * \brief Debugging support for thread-local-storage objects
22  *
23  * \author Kevin P. Fleming <kpfleming@digium.com>
24  */
25
26 #include "asterisk.h"
27
28 #if defined(DEBUG_THREADLOCALS)
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
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
38 struct tls_object {
39         void *key;
40         size_t size;
41         const char *file;
42         const char *function;
43         unsigned int line;
44         pthread_t thread;
45         AST_LIST_ENTRY(tls_object) entry;
46 };
47
48 static AST_RWLIST_HEAD_STATIC(tls_objects, tls_object);
49
50 void __ast_threadstorage_object_add(void *key, size_t len, const char *file, const char *function, unsigned int line)
51 {
52         struct tls_object *to;
53
54         if (!(to = ast_calloc(1, sizeof(*to))))
55                 return;
56
57         to->key = key;
58         to->size = len;
59         to->file = file;
60         to->function = function;
61         to->line = line;
62         to->thread = pthread_self();
63
64         AST_RWLIST_WRLOCK(&tls_objects);
65         AST_LIST_INSERT_TAIL(&tls_objects, to, entry);
66         AST_RWLIST_UNLOCK(&tls_objects);
67 }
68
69 void __ast_threadstorage_object_remove(void *key)
70 {
71         struct tls_object *to;
72
73         AST_RWLIST_WRLOCK(&tls_objects);
74         AST_LIST_TRAVERSE_SAFE_BEGIN(&tls_objects, to, entry) {
75                 if (to->key == key) {
76                         AST_LIST_REMOVE_CURRENT(entry);
77                         break;
78                 }
79         }
80         AST_LIST_TRAVERSE_SAFE_END;
81         AST_RWLIST_UNLOCK(&tls_objects);
82         if (to)
83                 ast_free(to);
84 }
85
86 void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len)
87 {
88         struct tls_object *to;
89
90         AST_RWLIST_WRLOCK(&tls_objects);
91         AST_LIST_TRAVERSE_SAFE_BEGIN(&tls_objects, to, entry) {
92                 if (to->key == key_old) {
93                         to->key = key_new;
94                         to->size = len;
95                         break;
96                 }
97         }
98         AST_LIST_TRAVERSE_SAFE_END;
99         AST_RWLIST_UNLOCK(&tls_objects);
100 }
101
102 static char *handle_cli_threadstorage_show_allocations(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
103 {
104         char *fn = NULL;
105         size_t len = 0;
106         unsigned int count = 0;
107         struct tls_object *to;
108
109         switch (cmd) {
110         case CLI_INIT:
111                 e->command = "threadstorage show allocations";
112                 e->usage =
113                         "Usage: threadstorage show allocations [<file>]\n"
114                         "       Dumps a list of all thread-specific memory allocations,\n"
115                         "       optionally limited to those from a specific file\n";
116                 return NULL;
117         case CLI_GENERATE:
118                 return NULL;
119         }
120
121         if (a->argc > 4)
122                 return CLI_SHOWUSAGE;
123
124         if (a->argc > 3)
125                 fn = a->argv[3];
126
127         AST_RWLIST_RDLOCK(&tls_objects);
128
129         AST_LIST_TRAVERSE(&tls_objects, to, entry) {
130                 if (fn && strcasecmp(to->file, fn))
131                         continue;
132
133                 ast_cli(a->fd, "%10d bytes allocated in %20s at line %5d of %25s (thread %p)\n",
134                         (int) to->size, to->function, to->line, to->file, (void *) to->thread);
135                 len += to->size;
136                 count++;
137         }
138
139         AST_RWLIST_UNLOCK(&tls_objects);
140
141         ast_cli(a->fd, "%10d bytes allocated in %d allocation%s\n", (int) len, count, count > 1 ? "s" : "");
142         
143         return CLI_SUCCESS;
144 }
145
146 static char *handle_cli_threadstorage_show_summary(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
147 {
148         char *fn = NULL;
149         size_t len = 0;
150         unsigned int count = 0;
151         struct tls_object *to;
152         struct file {
153                 const char *name;
154                 size_t len;
155                 unsigned int count;
156                 AST_LIST_ENTRY(file) entry;
157         } *file;
158         AST_LIST_HEAD_NOLOCK_STATIC(file_summary, file);
159
160         switch (cmd) {
161         case CLI_INIT:
162                 e->command = "threadstorage show summary";
163                 e->usage =
164                         "Usage: threadstorage show summary [<file>]\n"
165                         "       Summarizes thread-specific memory allocations by file, or optionally\n"
166                         "       by function, if a file is specified\n";
167                 return NULL;
168         case CLI_GENERATE:
169                 return NULL;
170         }
171
172         if (a->argc > 4)
173                 return CLI_SHOWUSAGE;
174
175         if (a->argc > 3)
176                 fn = a->argv[3];
177
178         AST_RWLIST_RDLOCK(&tls_objects);
179
180         AST_LIST_TRAVERSE(&tls_objects, to, entry) {
181                 if (fn && strcasecmp(to->file, fn))
182                         continue;
183
184                 AST_LIST_TRAVERSE(&file_summary, file, entry) {
185                         if ((!fn && (file->name == to->file)) || (fn && (file->name == to->function)))
186                                 break;
187                 }
188
189                 if (!file) {
190                         file = alloca(sizeof(*file));
191                         memset(file, 0, sizeof(*file));
192                         file->name = fn ? to->function : to->file;
193                         AST_LIST_INSERT_TAIL(&file_summary, file, entry);
194                 }
195
196                 file->len += to->size;
197                 file->count++;
198         }
199
200         AST_RWLIST_UNLOCK(&tls_objects);
201         
202         AST_LIST_TRAVERSE(&file_summary, file, entry) {
203                 len += file->len;
204                 count += file->count;
205                 if (fn) {
206                         ast_cli(a->fd, "%10d bytes in %d allocation%ss in function %s\n",
207                                 (int) file->len, file->count, file->count > 1 ? "s" : "", file->name);
208                 } else {
209                         ast_cli(a->fd, "%10d bytes in %d allocation%s in file %s\n",
210                                 (int) file->len, file->count, file->count > 1 ? "s" : "", file->name);
211                 }
212         }
213
214         ast_cli(a->fd, "%10d bytes allocated in %d allocation%s\n", (int) len, count, count > 1 ? "s" : "");
215
216         return CLI_SUCCESS;
217 }
218
219 static struct ast_cli_entry cli[] = {
220         AST_CLI_DEFINE(handle_cli_threadstorage_show_allocations, "Display outstanding thread local storage allocations"),
221         AST_CLI_DEFINE(handle_cli_threadstorage_show_summary,     "Summarize outstanding memory allocations")
222 };
223
224 void threadstorage_init(void)
225 {
226         ast_cli_register_multiple(cli, sizeof(cli) / sizeof(cli[0]));
227 }
228
229 #else /* !defined(DEBUG_THREADLOCALS) */
230
231 void threadstorage_init(void)
232 {
233 }
234
235 #endif /* !defined(DEBUG_THREADLOCALS) */
236