blob: adf23b19b3639999974f0ff74218d094f4c11b97 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Memory management routine
3 * Copyright (C) 1998 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "log.h"
26#include "memory.h"
27
ajsf858e492004-11-16 14:25:30 +000028static void alloc_inc (int);
29static void alloc_dec (int);
ajs7fa25ff2004-11-15 16:12:32 +000030static void log_memstats(int log_priority);
paul718e3742002-12-13 20:15:29 +000031
ajsf858e492004-11-16 14:25:30 +000032static struct message mstr [] =
paul718e3742002-12-13 20:15:29 +000033{
34 { MTYPE_THREAD, "thread" },
35 { MTYPE_THREAD_MASTER, "thread_master" },
36 { MTYPE_VECTOR, "vector" },
37 { MTYPE_VECTOR_INDEX, "vector_index" },
38 { MTYPE_IF, "interface" },
39 { 0, NULL },
40};
41
42/* Fatal memory allocation error occured. */
43static void
44zerror (const char *fname, int type, size_t size)
45{
ajs7fa25ff2004-11-15 16:12:32 +000046 zlog_err ("%s : can't allocate memory for `%s' size %d: %s\n",
ajs6099b3b2004-11-20 02:06:59 +000047 fname, lookup (mstr, type), (int) size, safe_strerror(errno));
ajs7fa25ff2004-11-15 16:12:32 +000048 log_memstats(LOG_WARNING);
ajs48d6c692004-11-26 20:52:59 +000049 /* N.B. It might be preferable to call zlog_backtrace_sigsafe here, since
ajs063ee522004-11-26 18:11:14 +000050 that function should definitely be safe in an OOM condition. But
ajs48d6c692004-11-26 20:52:59 +000051 unfortunately zlog_backtrace_sigsafe does not support syslog logging at
ajs063ee522004-11-26 18:11:14 +000052 this time... */
53 zlog_backtrace(LOG_WARNING);
ajs7fa25ff2004-11-15 16:12:32 +000054 abort();
paul718e3742002-12-13 20:15:29 +000055}
56
57/* Memory allocation. */
58void *
59zmalloc (int type, size_t size)
60{
61 void *memory;
62
63 memory = malloc (size);
64
65 if (memory == NULL)
66 zerror ("malloc", type, size);
67
68 alloc_inc (type);
69
70 return memory;
71}
72
73/* Memory allocation with num * size with cleared. */
74void *
75zcalloc (int type, size_t size)
76{
77 void *memory;
78
79 memory = calloc (1, size);
80
81 if (memory == NULL)
82 zerror ("calloc", type, size);
83
84 alloc_inc (type);
85
86 return memory;
87}
88
89/* Memory reallocation. */
90void *
91zrealloc (int type, void *ptr, size_t size)
92{
93 void *memory;
94
95 memory = realloc (ptr, size);
96 if (memory == NULL)
97 zerror ("realloc", type, size);
98 return memory;
99}
100
101/* Memory free. */
102void
103zfree (int type, void *ptr)
104{
105 alloc_dec (type);
106 free (ptr);
107}
108
109/* String duplication. */
110char *
hassob04c6992004-10-04 19:10:31 +0000111zstrdup (int type, const char *str)
paul718e3742002-12-13 20:15:29 +0000112{
113 void *dup;
114
115 dup = strdup (str);
116 if (dup == NULL)
117 zerror ("strdup", type, strlen (str));
118 alloc_inc (type);
119 return dup;
120}
121
122#ifdef MEMORY_LOG
ajsf858e492004-11-16 14:25:30 +0000123static struct
paul718e3742002-12-13 20:15:29 +0000124{
hassob04c6992004-10-04 19:10:31 +0000125 const char *name;
paul718e3742002-12-13 20:15:29 +0000126 unsigned long alloc;
127 unsigned long t_malloc;
128 unsigned long c_malloc;
129 unsigned long t_calloc;
130 unsigned long c_calloc;
131 unsigned long t_realloc;
132 unsigned long t_free;
133 unsigned long c_strdup;
134} mstat [MTYPE_MAX];
135
ajsf858e492004-11-16 14:25:30 +0000136static void
paul718e3742002-12-13 20:15:29 +0000137mtype_log (char *func, void *memory, const char *file, int line, int type)
138{
ajsb9e70282004-12-08 17:14:45 +0000139 zlog_debug ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line);
paul718e3742002-12-13 20:15:29 +0000140}
141
142void *
143mtype_zmalloc (const char *file, int line, int type, size_t size)
144{
145 void *memory;
146
147 mstat[type].c_malloc++;
148 mstat[type].t_malloc++;
149
150 memory = zmalloc (type, size);
151 mtype_log ("zmalloc", memory, file, line, type);
152
153 return memory;
154}
155
156void *
157mtype_zcalloc (const char *file, int line, int type, size_t size)
158{
159 void *memory;
160
161 mstat[type].c_calloc++;
162 mstat[type].t_calloc++;
163
164 memory = zcalloc (type, size);
165 mtype_log ("xcalloc", memory, file, line, type);
166
167 return memory;
168}
169
170void *
171mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size)
172{
173 void *memory;
174
175 /* Realloc need before allocated pointer. */
176 mstat[type].t_realloc++;
177
178 memory = zrealloc (type, ptr, size);
179
180 mtype_log ("xrealloc", memory, file, line, type);
181
182 return memory;
183}
184
185/* Important function. */
186void
187mtype_zfree (const char *file, int line, int type, void *ptr)
188{
189 mstat[type].t_free++;
190
191 mtype_log ("xfree", ptr, file, line, type);
192
193 zfree (type, ptr);
194}
195
196char *
hassob04c6992004-10-04 19:10:31 +0000197mtype_zstrdup (const char *file, int line, int type, const char *str)
paul718e3742002-12-13 20:15:29 +0000198{
199 char *memory;
200
201 mstat[type].c_strdup++;
202
203 memory = zstrdup (type, str);
204
205 mtype_log ("xstrdup", memory, file, line, type);
206
207 return memory;
208}
209#else
ajsf858e492004-11-16 14:25:30 +0000210static struct
paul718e3742002-12-13 20:15:29 +0000211{
212 char *name;
213 unsigned long alloc;
214} mstat [MTYPE_MAX];
215#endif /* MTPYE_LOG */
216
217/* Increment allocation counter. */
ajsf858e492004-11-16 14:25:30 +0000218static void
paul718e3742002-12-13 20:15:29 +0000219alloc_inc (int type)
220{
221 mstat[type].alloc++;
222}
223
224/* Decrement allocation counter. */
ajsf858e492004-11-16 14:25:30 +0000225static void
paul718e3742002-12-13 20:15:29 +0000226alloc_dec (int type)
227{
228 mstat[type].alloc--;
229}
230
231/* Looking up memory status from vty interface. */
232#include "vector.h"
233#include "vty.h"
234#include "command.h"
235
ajs7fa25ff2004-11-15 16:12:32 +0000236static void
237log_memstats(int pri)
238{
239 struct mlist *ml;
240
241 for (ml = mlists; ml->list; ml++)
242 {
243 struct memory_list *m;
244
245 zlog (NULL, pri, "Memory utilization in module %s:", ml->name);
246 for (m = ml->list; m->index >= 0; m++)
247 if (m->index && mstat[m->index].alloc)
paul2fd2fd52005-04-15 11:47:15 +0000248 zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc);
ajs7fa25ff2004-11-15 16:12:32 +0000249 }
250}
251
ajsf858e492004-11-16 14:25:30 +0000252static struct memory_list memory_list_separator[] =
paul718e3742002-12-13 20:15:29 +0000253{
254 { 0, NULL},
255 {-1, NULL}
256};
257
ajsf858e492004-11-16 14:25:30 +0000258static void
paul718e3742002-12-13 20:15:29 +0000259show_memory_vty (struct vty *vty, struct memory_list *list)
260{
261 struct memory_list *m;
262
263 for (m = list; m->index >= 0; m++)
264 if (m->index == 0)
265 vty_out (vty, "-----------------------------\r\n");
266 else
paul2c1de2a2005-04-25 14:02:44 +0000267 vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc);
paul718e3742002-12-13 20:15:29 +0000268}
269
270DEFUN (show_memory_all,
271 show_memory_all_cmd,
272 "show memory all",
273 "Show running system information\n"
274 "Memory statistics\n"
275 "All memory statistics\n")
276{
ajs7fa25ff2004-11-15 16:12:32 +0000277 struct mlist *ml;
278
279 for (ml = mlists; ml->list; ml++)
280 {
281 if (ml != mlists)
282 show_memory_vty (vty, memory_list_separator);
283 show_memory_vty (vty, ml->list);
284 }
paul718e3742002-12-13 20:15:29 +0000285
286 return CMD_SUCCESS;
287}
288
289ALIAS (show_memory_all,
290 show_memory_cmd,
291 "show memory",
292 "Show running system information\n"
293 "Memory statistics\n")
294
295DEFUN (show_memory_lib,
296 show_memory_lib_cmd,
297 "show memory lib",
298 SHOW_STR
299 "Memory statistics\n"
300 "Library memory\n")
301{
302 show_memory_vty (vty, memory_list_lib);
303 return CMD_SUCCESS;
304}
305
paul2fd2fd52005-04-15 11:47:15 +0000306DEFUN (show_memory_zebra,
307 show_memory_zebra_cmd,
308 "show memory zebra",
309 SHOW_STR
310 "Memory statistics\n"
311 "Zebra memory\n")
312{
313 show_memory_vty (vty, memory_list_zebra);
314 return CMD_SUCCESS;
315}
316
paul718e3742002-12-13 20:15:29 +0000317DEFUN (show_memory_rip,
318 show_memory_rip_cmd,
319 "show memory rip",
320 SHOW_STR
321 "Memory statistics\n"
322 "RIP memory\n")
323{
324 show_memory_vty (vty, memory_list_rip);
325 return CMD_SUCCESS;
326}
327
hassoa94434b2003-05-25 17:10:12 +0000328DEFUN (show_memory_ripng,
329 show_memory_ripng_cmd,
330 "show memory ripng",
331 SHOW_STR
332 "Memory statistics\n"
333 "RIPng memory\n")
334{
335 show_memory_vty (vty, memory_list_ripng);
336 return CMD_SUCCESS;
337}
338
paul718e3742002-12-13 20:15:29 +0000339DEFUN (show_memory_bgp,
340 show_memory_bgp_cmd,
341 "show memory bgp",
342 SHOW_STR
343 "Memory statistics\n"
344 "BGP memory\n")
345{
346 show_memory_vty (vty, memory_list_bgp);
347 return CMD_SUCCESS;
348}
349
350DEFUN (show_memory_ospf,
351 show_memory_ospf_cmd,
352 "show memory ospf",
353 SHOW_STR
354 "Memory statistics\n"
355 "OSPF memory\n")
356{
357 show_memory_vty (vty, memory_list_ospf);
358 return CMD_SUCCESS;
359}
360
361DEFUN (show_memory_ospf6,
362 show_memory_ospf6_cmd,
363 "show memory ospf6",
364 SHOW_STR
365 "Memory statistics\n"
366 "OSPF6 memory\n")
367{
368 show_memory_vty (vty, memory_list_ospf6);
369 return CMD_SUCCESS;
370}
371
jardin9e867fe2003-12-23 08:56:18 +0000372DEFUN (show_memory_isis,
373 show_memory_isis_cmd,
374 "show memory isis",
375 SHOW_STR
376 "Memory statistics\n"
377 "ISIS memory\n")
378{
379 show_memory_vty (vty, memory_list_isis);
380 return CMD_SUCCESS;
381}
382
paul718e3742002-12-13 20:15:29 +0000383void
ajsf858e492004-11-16 14:25:30 +0000384memory_init (void)
paul718e3742002-12-13 20:15:29 +0000385{
386 install_element (VIEW_NODE, &show_memory_cmd);
387 install_element (VIEW_NODE, &show_memory_all_cmd);
388 install_element (VIEW_NODE, &show_memory_lib_cmd);
389 install_element (VIEW_NODE, &show_memory_rip_cmd);
hassoa94434b2003-05-25 17:10:12 +0000390 install_element (VIEW_NODE, &show_memory_ripng_cmd);
paul718e3742002-12-13 20:15:29 +0000391 install_element (VIEW_NODE, &show_memory_bgp_cmd);
392 install_element (VIEW_NODE, &show_memory_ospf_cmd);
393 install_element (VIEW_NODE, &show_memory_ospf6_cmd);
jardin9e867fe2003-12-23 08:56:18 +0000394 install_element (VIEW_NODE, &show_memory_isis_cmd);
paul718e3742002-12-13 20:15:29 +0000395
396 install_element (ENABLE_NODE, &show_memory_cmd);
397 install_element (ENABLE_NODE, &show_memory_all_cmd);
398 install_element (ENABLE_NODE, &show_memory_lib_cmd);
paul2fd2fd52005-04-15 11:47:15 +0000399 install_element (ENABLE_NODE, &show_memory_zebra_cmd);
paul718e3742002-12-13 20:15:29 +0000400 install_element (ENABLE_NODE, &show_memory_rip_cmd);
hassoa94434b2003-05-25 17:10:12 +0000401 install_element (ENABLE_NODE, &show_memory_ripng_cmd);
paul718e3742002-12-13 20:15:29 +0000402 install_element (ENABLE_NODE, &show_memory_bgp_cmd);
403 install_element (ENABLE_NODE, &show_memory_ospf_cmd);
404 install_element (ENABLE_NODE, &show_memory_ospf6_cmd);
jardin9e867fe2003-12-23 08:56:18 +0000405 install_element (ENABLE_NODE, &show_memory_isis_cmd);
paul718e3742002-12-13 20:15:29 +0000406}