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