blob: dae2b9aebf949d7b9ce4049b7f227ea8629e4c72 [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. */
paul8cc41982005-05-06 21:25:49 +000043static void __attribute__ ((noreturn))
paul718e3742002-12-13 20:15:29 +000044zerror (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 void
ajs24065a32005-10-20 22:28:14 +0000253show_separator(struct vty *vty)
254{
255 vty_out (vty, "-----------------------------\r\n");
256}
257
258static int
paul718e3742002-12-13 20:15:29 +0000259show_memory_vty (struct vty *vty, struct memory_list *list)
260{
261 struct memory_list *m;
ajs24065a32005-10-20 22:28:14 +0000262 int needsep = 0;
paul718e3742002-12-13 20:15:29 +0000263
264 for (m = list; m->index >= 0; m++)
265 if (m->index == 0)
ajs24065a32005-10-20 22:28:14 +0000266 {
267 if (needsep)
268 {
269 show_separator (vty);
270 needsep = 0;
271 }
272 }
273 else if (mstat[m->index].alloc)
274 {
275 vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc);
276 needsep = 1;
277 }
278 return needsep;
paul718e3742002-12-13 20:15:29 +0000279}
280
281DEFUN (show_memory_all,
282 show_memory_all_cmd,
283 "show memory all",
284 "Show running system information\n"
285 "Memory statistics\n"
286 "All memory statistics\n")
287{
ajs7fa25ff2004-11-15 16:12:32 +0000288 struct mlist *ml;
ajs24065a32005-10-20 22:28:14 +0000289 int needsep = 0;
ajs7fa25ff2004-11-15 16:12:32 +0000290
291 for (ml = mlists; ml->list; ml++)
292 {
ajs24065a32005-10-20 22:28:14 +0000293 if (needsep)
294 show_separator (vty);
295 needsep = show_memory_vty (vty, ml->list);
ajs7fa25ff2004-11-15 16:12:32 +0000296 }
paul718e3742002-12-13 20:15:29 +0000297
298 return CMD_SUCCESS;
299}
300
301ALIAS (show_memory_all,
302 show_memory_cmd,
303 "show memory",
304 "Show running system information\n"
305 "Memory statistics\n")
306
307DEFUN (show_memory_lib,
308 show_memory_lib_cmd,
309 "show memory lib",
310 SHOW_STR
311 "Memory statistics\n"
312 "Library memory\n")
313{
314 show_memory_vty (vty, memory_list_lib);
315 return CMD_SUCCESS;
316}
317
paul2fd2fd52005-04-15 11:47:15 +0000318DEFUN (show_memory_zebra,
319 show_memory_zebra_cmd,
320 "show memory zebra",
321 SHOW_STR
322 "Memory statistics\n"
323 "Zebra memory\n")
324{
325 show_memory_vty (vty, memory_list_zebra);
326 return CMD_SUCCESS;
327}
328
paul718e3742002-12-13 20:15:29 +0000329DEFUN (show_memory_rip,
330 show_memory_rip_cmd,
331 "show memory rip",
332 SHOW_STR
333 "Memory statistics\n"
334 "RIP memory\n")
335{
336 show_memory_vty (vty, memory_list_rip);
337 return CMD_SUCCESS;
338}
339
hassoa94434b2003-05-25 17:10:12 +0000340DEFUN (show_memory_ripng,
341 show_memory_ripng_cmd,
342 "show memory ripng",
343 SHOW_STR
344 "Memory statistics\n"
345 "RIPng memory\n")
346{
347 show_memory_vty (vty, memory_list_ripng);
348 return CMD_SUCCESS;
349}
350
paul718e3742002-12-13 20:15:29 +0000351DEFUN (show_memory_bgp,
352 show_memory_bgp_cmd,
353 "show memory bgp",
354 SHOW_STR
355 "Memory statistics\n"
356 "BGP memory\n")
357{
358 show_memory_vty (vty, memory_list_bgp);
359 return CMD_SUCCESS;
360}
361
362DEFUN (show_memory_ospf,
363 show_memory_ospf_cmd,
364 "show memory ospf",
365 SHOW_STR
366 "Memory statistics\n"
367 "OSPF memory\n")
368{
369 show_memory_vty (vty, memory_list_ospf);
370 return CMD_SUCCESS;
371}
372
373DEFUN (show_memory_ospf6,
374 show_memory_ospf6_cmd,
375 "show memory ospf6",
376 SHOW_STR
377 "Memory statistics\n"
378 "OSPF6 memory\n")
379{
380 show_memory_vty (vty, memory_list_ospf6);
381 return CMD_SUCCESS;
382}
383
jardin9e867fe2003-12-23 08:56:18 +0000384DEFUN (show_memory_isis,
385 show_memory_isis_cmd,
386 "show memory isis",
387 SHOW_STR
388 "Memory statistics\n"
389 "ISIS memory\n")
390{
391 show_memory_vty (vty, memory_list_isis);
392 return CMD_SUCCESS;
393}
394
paul718e3742002-12-13 20:15:29 +0000395void
ajsf858e492004-11-16 14:25:30 +0000396memory_init (void)
paul718e3742002-12-13 20:15:29 +0000397{
398 install_element (VIEW_NODE, &show_memory_cmd);
399 install_element (VIEW_NODE, &show_memory_all_cmd);
400 install_element (VIEW_NODE, &show_memory_lib_cmd);
401 install_element (VIEW_NODE, &show_memory_rip_cmd);
hassoa94434b2003-05-25 17:10:12 +0000402 install_element (VIEW_NODE, &show_memory_ripng_cmd);
paul718e3742002-12-13 20:15:29 +0000403 install_element (VIEW_NODE, &show_memory_bgp_cmd);
404 install_element (VIEW_NODE, &show_memory_ospf_cmd);
405 install_element (VIEW_NODE, &show_memory_ospf6_cmd);
jardin9e867fe2003-12-23 08:56:18 +0000406 install_element (VIEW_NODE, &show_memory_isis_cmd);
paul718e3742002-12-13 20:15:29 +0000407
408 install_element (ENABLE_NODE, &show_memory_cmd);
409 install_element (ENABLE_NODE, &show_memory_all_cmd);
410 install_element (ENABLE_NODE, &show_memory_lib_cmd);
paul2fd2fd52005-04-15 11:47:15 +0000411 install_element (ENABLE_NODE, &show_memory_zebra_cmd);
paul718e3742002-12-13 20:15:29 +0000412 install_element (ENABLE_NODE, &show_memory_rip_cmd);
hassoa94434b2003-05-25 17:10:12 +0000413 install_element (ENABLE_NODE, &show_memory_ripng_cmd);
paul718e3742002-12-13 20:15:29 +0000414 install_element (ENABLE_NODE, &show_memory_bgp_cmd);
415 install_element (ENABLE_NODE, &show_memory_ospf_cmd);
416 install_element (ENABLE_NODE, &show_memory_ospf6_cmd);
jardin9e867fe2003-12-23 08:56:18 +0000417 install_element (ENABLE_NODE, &show_memory_isis_cmd);
paul718e3742002-12-13 20:15:29 +0000418}