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