blob: dc09d8a6c9a4294a4651f5d8cc292607a775b097 [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>
Paul Jakmad09552d2006-05-28 08:15:46 +000024/* malloc.h is generally obsolete, however GNU Libc mallinfo wants it. */
Paul Jakma74176d22006-06-30 16:49:02 +000025#if !defined(HAVE_STDLIB_H) || (defined(GNU_LINUX) && defined(HAVE_MALLINFO))
Paul Jakma41be32b2006-03-30 13:53:59 +000026#include <malloc.h>
Paul Jakmad09552d2006-05-28 08:15:46 +000027#endif /* !HAVE_STDLIB_H || HAVE_MALLINFO */
paul718e3742002-12-13 20:15:29 +000028
29#include "log.h"
30#include "memory.h"
31
ajsf858e492004-11-16 14:25:30 +000032static void alloc_inc (int);
33static void alloc_dec (int);
ajs7fa25ff2004-11-15 16:12:32 +000034static void log_memstats(int log_priority);
paul718e3742002-12-13 20:15:29 +000035
Stephen Hemminger1423c802008-08-14 17:59:25 +010036static const struct message mstr [] =
paul718e3742002-12-13 20:15:29 +000037{
38 { MTYPE_THREAD, "thread" },
39 { MTYPE_THREAD_MASTER, "thread_master" },
40 { MTYPE_VECTOR, "vector" },
41 { MTYPE_VECTOR_INDEX, "vector_index" },
42 { MTYPE_IF, "interface" },
43 { 0, NULL },
44};
45
46/* Fatal memory allocation error occured. */
paul8cc41982005-05-06 21:25:49 +000047static void __attribute__ ((noreturn))
paul718e3742002-12-13 20:15:29 +000048zerror (const char *fname, int type, size_t size)
49{
ajs7fa25ff2004-11-15 16:12:32 +000050 zlog_err ("%s : can't allocate memory for `%s' size %d: %s\n",
ajs6099b3b2004-11-20 02:06:59 +000051 fname, lookup (mstr, type), (int) size, safe_strerror(errno));
ajs7fa25ff2004-11-15 16:12:32 +000052 log_memstats(LOG_WARNING);
ajs48d6c692004-11-26 20:52:59 +000053 /* N.B. It might be preferable to call zlog_backtrace_sigsafe here, since
ajs063ee522004-11-26 18:11:14 +000054 that function should definitely be safe in an OOM condition. But
ajs48d6c692004-11-26 20:52:59 +000055 unfortunately zlog_backtrace_sigsafe does not support syslog logging at
ajs063ee522004-11-26 18:11:14 +000056 this time... */
57 zlog_backtrace(LOG_WARNING);
ajs7fa25ff2004-11-15 16:12:32 +000058 abort();
paul718e3742002-12-13 20:15:29 +000059}
60
61/* Memory allocation. */
62void *
63zmalloc (int type, size_t size)
64{
65 void *memory;
66
67 memory = malloc (size);
68
69 if (memory == NULL)
70 zerror ("malloc", type, size);
71
72 alloc_inc (type);
73
74 return memory;
75}
76
77/* Memory allocation with num * size with cleared. */
78void *
79zcalloc (int type, size_t size)
80{
81 void *memory;
82
83 memory = calloc (1, size);
84
85 if (memory == NULL)
86 zerror ("calloc", type, size);
87
88 alloc_inc (type);
89
90 return memory;
91}
92
93/* Memory reallocation. */
94void *
95zrealloc (int type, void *ptr, size_t size)
96{
97 void *memory;
98
99 memory = realloc (ptr, size);
100 if (memory == NULL)
101 zerror ("realloc", type, size);
102 return memory;
103}
104
105/* Memory free. */
106void
107zfree (int type, void *ptr)
108{
109 alloc_dec (type);
110 free (ptr);
111}
112
113/* String duplication. */
114char *
hassob04c6992004-10-04 19:10:31 +0000115zstrdup (int type, const char *str)
paul718e3742002-12-13 20:15:29 +0000116{
117 void *dup;
118
119 dup = strdup (str);
120 if (dup == NULL)
121 zerror ("strdup", type, strlen (str));
122 alloc_inc (type);
123 return dup;
124}
125
126#ifdef MEMORY_LOG
ajsf858e492004-11-16 14:25:30 +0000127static struct
paul718e3742002-12-13 20:15:29 +0000128{
hassob04c6992004-10-04 19:10:31 +0000129 const char *name;
Chris Caputo228da422009-07-18 05:44:03 +0000130 long alloc;
paul718e3742002-12-13 20:15:29 +0000131 unsigned long t_malloc;
132 unsigned long c_malloc;
133 unsigned long t_calloc;
134 unsigned long c_calloc;
135 unsigned long t_realloc;
136 unsigned long t_free;
137 unsigned long c_strdup;
138} mstat [MTYPE_MAX];
139
ajsf858e492004-11-16 14:25:30 +0000140static void
paul718e3742002-12-13 20:15:29 +0000141mtype_log (char *func, void *memory, const char *file, int line, int type)
142{
ajsb9e70282004-12-08 17:14:45 +0000143 zlog_debug ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line);
paul718e3742002-12-13 20:15:29 +0000144}
145
146void *
147mtype_zmalloc (const char *file, int line, int type, size_t size)
148{
149 void *memory;
150
151 mstat[type].c_malloc++;
152 mstat[type].t_malloc++;
153
154 memory = zmalloc (type, size);
155 mtype_log ("zmalloc", memory, file, line, type);
156
157 return memory;
158}
159
160void *
161mtype_zcalloc (const char *file, int line, int type, size_t size)
162{
163 void *memory;
164
165 mstat[type].c_calloc++;
166 mstat[type].t_calloc++;
167
168 memory = zcalloc (type, size);
169 mtype_log ("xcalloc", memory, file, line, type);
170
171 return memory;
172}
173
174void *
175mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size)
176{
177 void *memory;
178
179 /* Realloc need before allocated pointer. */
180 mstat[type].t_realloc++;
181
182 memory = zrealloc (type, ptr, size);
183
184 mtype_log ("xrealloc", memory, file, line, type);
185
186 return memory;
187}
188
189/* Important function. */
190void
191mtype_zfree (const char *file, int line, int type, void *ptr)
192{
193 mstat[type].t_free++;
194
195 mtype_log ("xfree", ptr, file, line, type);
196
197 zfree (type, ptr);
198}
199
200char *
hassob04c6992004-10-04 19:10:31 +0000201mtype_zstrdup (const char *file, int line, int type, const char *str)
paul718e3742002-12-13 20:15:29 +0000202{
203 char *memory;
204
205 mstat[type].c_strdup++;
206
207 memory = zstrdup (type, str);
208
209 mtype_log ("xstrdup", memory, file, line, type);
210
211 return memory;
212}
213#else
ajsf858e492004-11-16 14:25:30 +0000214static struct
paul718e3742002-12-13 20:15:29 +0000215{
216 char *name;
Chris Caputo228da422009-07-18 05:44:03 +0000217 long alloc;
paul718e3742002-12-13 20:15:29 +0000218} mstat [MTYPE_MAX];
Chris Caputo228da422009-07-18 05:44:03 +0000219#endif /* MEMORY_LOG */
paul718e3742002-12-13 20:15:29 +0000220
221/* Increment allocation counter. */
ajsf858e492004-11-16 14:25:30 +0000222static void
paul718e3742002-12-13 20:15:29 +0000223alloc_inc (int type)
224{
225 mstat[type].alloc++;
226}
227
228/* Decrement allocation counter. */
ajsf858e492004-11-16 14:25:30 +0000229static void
paul718e3742002-12-13 20:15:29 +0000230alloc_dec (int type)
231{
232 mstat[type].alloc--;
233}
234
235/* Looking up memory status from vty interface. */
236#include "vector.h"
237#include "vty.h"
238#include "command.h"
239
ajs7fa25ff2004-11-15 16:12:32 +0000240static void
241log_memstats(int pri)
242{
243 struct mlist *ml;
244
245 for (ml = mlists; ml->list; ml++)
246 {
247 struct memory_list *m;
248
249 zlog (NULL, pri, "Memory utilization in module %s:", ml->name);
250 for (m = ml->list; m->index >= 0; m++)
251 if (m->index && mstat[m->index].alloc)
paul2fd2fd52005-04-15 11:47:15 +0000252 zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc);
ajs7fa25ff2004-11-15 16:12:32 +0000253 }
254}
255
Chris Caputo228da422009-07-18 05:44:03 +0000256void
257log_memstats_stderr (const char *prefix)
258{
259 struct mlist *ml;
260 struct memory_list *m;
261 int i;
262 int j = 0;
263
264 for (ml = mlists; ml->list; ml++)
265 {
266 i = 0;
267
268 for (m = ml->list; m->index >= 0; m++)
269 if (m->index && mstat[m->index].alloc)
270 {
271 if (!i)
272 fprintf (stderr,
273 "%s: memstats: Current memory utilization in module %s:\n",
274 prefix,
275 ml->name);
276 fprintf (stderr,
277 "%s: memstats: %-30s: %10ld%s\n",
278 prefix,
279 m->format,
280 mstat[m->index].alloc,
281 mstat[m->index].alloc < 0 ? " (REPORT THIS BUG!)" : "");
282 i = j = 1;
283 }
284 }
285
286 if (j)
287 fprintf (stderr,
288 "%s: memstats: NOTE: If configuration exists, utilization may be "
289 "expected.\n",
290 prefix);
291 else
292 fprintf (stderr,
293 "%s: memstats: No remaining tracked memory utilization.\n",
294 prefix);
295}
296
ajsf858e492004-11-16 14:25:30 +0000297static void
ajs24065a32005-10-20 22:28:14 +0000298show_separator(struct vty *vty)
299{
300 vty_out (vty, "-----------------------------\r\n");
301}
302
303static int
paul718e3742002-12-13 20:15:29 +0000304show_memory_vty (struct vty *vty, struct memory_list *list)
305{
306 struct memory_list *m;
ajs24065a32005-10-20 22:28:14 +0000307 int needsep = 0;
paul718e3742002-12-13 20:15:29 +0000308
309 for (m = list; m->index >= 0; m++)
310 if (m->index == 0)
ajs24065a32005-10-20 22:28:14 +0000311 {
312 if (needsep)
313 {
314 show_separator (vty);
315 needsep = 0;
316 }
317 }
318 else if (mstat[m->index].alloc)
319 {
320 vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc);
321 needsep = 1;
322 }
323 return needsep;
paul718e3742002-12-13 20:15:29 +0000324}
325
Paul Jakma41be32b2006-03-30 13:53:59 +0000326#ifdef HAVE_MALLINFO
327static int
328show_memory_mallinfo (struct vty *vty)
329{
330 struct mallinfo minfo = mallinfo();
331 char buf[MTYPE_MEMSTR_LEN];
332
333 vty_out (vty, "System allocator statistics:%s", VTY_NEWLINE);
334 vty_out (vty, " Total heap allocated: %s%s",
335 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.arena),
336 VTY_NEWLINE);
337 vty_out (vty, " Holding block headers: %s%s",
338 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.hblkhd),
339 VTY_NEWLINE);
340 vty_out (vty, " Used small blocks: %s%s",
341 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.usmblks),
342 VTY_NEWLINE);
343 vty_out (vty, " Used ordinary blocks: %s%s",
344 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.uordblks),
345 VTY_NEWLINE);
346 vty_out (vty, " Free small blocks: %s%s",
347 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fsmblks),
348 VTY_NEWLINE);
349 vty_out (vty, " Free ordinary blocks: %s%s",
350 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fordblks),
351 VTY_NEWLINE);
352 vty_out (vty, " Ordinary blocks: %ld%s",
353 (unsigned long)minfo.ordblks,
354 VTY_NEWLINE);
355 vty_out (vty, " Small blocks: %ld%s",
356 (unsigned long)minfo.smblks,
357 VTY_NEWLINE);
358 vty_out (vty, " Holding blocks: %ld%s",
359 (unsigned long)minfo.hblks,
360 VTY_NEWLINE);
361 vty_out (vty, "(see system documentation for 'mallinfo' for meaning)%s",
362 VTY_NEWLINE);
363 return 1;
364}
365#endif /* HAVE_MALLINFO */
366
paul718e3742002-12-13 20:15:29 +0000367DEFUN (show_memory_all,
368 show_memory_all_cmd,
369 "show memory all",
370 "Show running system information\n"
371 "Memory statistics\n"
372 "All memory statistics\n")
373{
ajs7fa25ff2004-11-15 16:12:32 +0000374 struct mlist *ml;
ajs24065a32005-10-20 22:28:14 +0000375 int needsep = 0;
Paul Jakma41be32b2006-03-30 13:53:59 +0000376
377#ifdef HAVE_MALLINFO
378 needsep = show_memory_mallinfo (vty);
379#endif /* HAVE_MALLINFO */
380
ajs7fa25ff2004-11-15 16:12:32 +0000381 for (ml = mlists; ml->list; ml++)
382 {
ajs24065a32005-10-20 22:28:14 +0000383 if (needsep)
384 show_separator (vty);
385 needsep = show_memory_vty (vty, ml->list);
ajs7fa25ff2004-11-15 16:12:32 +0000386 }
paul718e3742002-12-13 20:15:29 +0000387
388 return CMD_SUCCESS;
389}
390
391ALIAS (show_memory_all,
392 show_memory_cmd,
393 "show memory",
394 "Show running system information\n"
395 "Memory statistics\n")
396
397DEFUN (show_memory_lib,
398 show_memory_lib_cmd,
399 "show memory lib",
400 SHOW_STR
401 "Memory statistics\n"
402 "Library memory\n")
403{
404 show_memory_vty (vty, memory_list_lib);
405 return CMD_SUCCESS;
406}
407
paul2fd2fd52005-04-15 11:47:15 +0000408DEFUN (show_memory_zebra,
409 show_memory_zebra_cmd,
410 "show memory zebra",
411 SHOW_STR
412 "Memory statistics\n"
413 "Zebra memory\n")
414{
415 show_memory_vty (vty, memory_list_zebra);
416 return CMD_SUCCESS;
417}
418
paul718e3742002-12-13 20:15:29 +0000419DEFUN (show_memory_rip,
420 show_memory_rip_cmd,
421 "show memory rip",
422 SHOW_STR
423 "Memory statistics\n"
424 "RIP memory\n")
425{
426 show_memory_vty (vty, memory_list_rip);
427 return CMD_SUCCESS;
428}
429
hassoa94434b2003-05-25 17:10:12 +0000430DEFUN (show_memory_ripng,
431 show_memory_ripng_cmd,
432 "show memory ripng",
433 SHOW_STR
434 "Memory statistics\n"
435 "RIPng memory\n")
436{
437 show_memory_vty (vty, memory_list_ripng);
438 return CMD_SUCCESS;
439}
440
paul718e3742002-12-13 20:15:29 +0000441DEFUN (show_memory_bgp,
442 show_memory_bgp_cmd,
443 "show memory bgp",
444 SHOW_STR
445 "Memory statistics\n"
446 "BGP memory\n")
447{
448 show_memory_vty (vty, memory_list_bgp);
449 return CMD_SUCCESS;
450}
451
452DEFUN (show_memory_ospf,
453 show_memory_ospf_cmd,
454 "show memory ospf",
455 SHOW_STR
456 "Memory statistics\n"
457 "OSPF memory\n")
458{
459 show_memory_vty (vty, memory_list_ospf);
460 return CMD_SUCCESS;
461}
462
463DEFUN (show_memory_ospf6,
464 show_memory_ospf6_cmd,
465 "show memory ospf6",
466 SHOW_STR
467 "Memory statistics\n"
468 "OSPF6 memory\n")
469{
470 show_memory_vty (vty, memory_list_ospf6);
471 return CMD_SUCCESS;
472}
473
jardin9e867fe2003-12-23 08:56:18 +0000474DEFUN (show_memory_isis,
475 show_memory_isis_cmd,
476 "show memory isis",
477 SHOW_STR
478 "Memory statistics\n"
479 "ISIS memory\n")
480{
481 show_memory_vty (vty, memory_list_isis);
482 return CMD_SUCCESS;
483}
484
paul718e3742002-12-13 20:15:29 +0000485void
ajsf858e492004-11-16 14:25:30 +0000486memory_init (void)
paul718e3742002-12-13 20:15:29 +0000487{
Paul Jakma62687ff2008-08-23 14:27:06 +0100488 install_element (RESTRICTED_NODE, &show_memory_cmd);
489 install_element (RESTRICTED_NODE, &show_memory_all_cmd);
490 install_element (RESTRICTED_NODE, &show_memory_lib_cmd);
491 install_element (RESTRICTED_NODE, &show_memory_rip_cmd);
492 install_element (RESTRICTED_NODE, &show_memory_ripng_cmd);
493 install_element (RESTRICTED_NODE, &show_memory_bgp_cmd);
494 install_element (RESTRICTED_NODE, &show_memory_ospf_cmd);
495 install_element (RESTRICTED_NODE, &show_memory_ospf6_cmd);
496 install_element (RESTRICTED_NODE, &show_memory_isis_cmd);
497
paul718e3742002-12-13 20:15:29 +0000498 install_element (VIEW_NODE, &show_memory_cmd);
499 install_element (VIEW_NODE, &show_memory_all_cmd);
500 install_element (VIEW_NODE, &show_memory_lib_cmd);
501 install_element (VIEW_NODE, &show_memory_rip_cmd);
hassoa94434b2003-05-25 17:10:12 +0000502 install_element (VIEW_NODE, &show_memory_ripng_cmd);
paul718e3742002-12-13 20:15:29 +0000503 install_element (VIEW_NODE, &show_memory_bgp_cmd);
504 install_element (VIEW_NODE, &show_memory_ospf_cmd);
505 install_element (VIEW_NODE, &show_memory_ospf6_cmd);
jardin9e867fe2003-12-23 08:56:18 +0000506 install_element (VIEW_NODE, &show_memory_isis_cmd);
paul718e3742002-12-13 20:15:29 +0000507
508 install_element (ENABLE_NODE, &show_memory_cmd);
509 install_element (ENABLE_NODE, &show_memory_all_cmd);
510 install_element (ENABLE_NODE, &show_memory_lib_cmd);
paul2fd2fd52005-04-15 11:47:15 +0000511 install_element (ENABLE_NODE, &show_memory_zebra_cmd);
paul718e3742002-12-13 20:15:29 +0000512 install_element (ENABLE_NODE, &show_memory_rip_cmd);
hassoa94434b2003-05-25 17:10:12 +0000513 install_element (ENABLE_NODE, &show_memory_ripng_cmd);
paul718e3742002-12-13 20:15:29 +0000514 install_element (ENABLE_NODE, &show_memory_bgp_cmd);
515 install_element (ENABLE_NODE, &show_memory_ospf_cmd);
516 install_element (ENABLE_NODE, &show_memory_ospf6_cmd);
jardin9e867fe2003-12-23 08:56:18 +0000517 install_element (ENABLE_NODE, &show_memory_isis_cmd);
paul718e3742002-12-13 20:15:29 +0000518}
Paul Jakma41be32b2006-03-30 13:53:59 +0000519
520/* Stats querying from users */
521/* Return a pointer to a human friendly string describing
522 * the byte count passed in. E.g:
523 * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc.
524 * Up to 4 significant figures will be given.
525 * The pointer returned may be NULL (indicating an error)
526 * or point to the given buffer, or point to static storage.
527 */
528const char *
529mtype_memstr (char *buf, size_t len, unsigned long bytes)
530{
531 unsigned int t, g, m, k;
532
533 /* easy cases */
534 if (!bytes)
535 return "0 bytes";
536 if (bytes == 1)
537 return "1 byte";
538
539 if (sizeof (unsigned long) >= 8)
540 /* Hacked to make it not warn on ILP32 machines
541 * Shift will always be 40 at runtime. See below too */
542 t = bytes >> (sizeof (unsigned long) >= 8 ? 40 : 0);
543 else
544 t = 0;
545 g = bytes >> 30;
546 m = bytes >> 20;
547 k = bytes >> 10;
548
549 if (t > 10)
550 {
551 /* The shift will always be 39 at runtime.
552 * Just hacked to make it not warn on 'smaller' machines.
553 * Static compiler analysis should mean no extra code
554 */
Paul Jakma912df1e2008-01-08 13:50:11 +0000555 if (bytes & (1UL << (sizeof (unsigned long) >= 8 ? 39 : 0)))
Paul Jakma41be32b2006-03-30 13:53:59 +0000556 t++;
557 snprintf (buf, len, "%4d TiB", t);
558 }
559 else if (g > 10)
560 {
561 if (bytes & (1 << 29))
562 g++;
563 snprintf (buf, len, "%d GiB", g);
564 }
565 else if (m > 10)
566 {
567 if (bytes & (1 << 19))
568 m++;
569 snprintf (buf, len, "%d MiB", m);
570 }
571 else if (k > 10)
572 {
573 if (bytes & (1 << 9))
574 k++;
575 snprintf (buf, len, "%d KiB", k);
576 }
577 else
578 snprintf (buf, len, "%ld bytes", bytes);
579
580 return buf;
581}
582
583unsigned long
584mtype_stats_alloc (int type)
585{
586 return mstat[type].alloc;
587}