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