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