blob: 4bac31dbb45ba87ef22268d9336f639fee25779c [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
Greg Troxelb1679382010-09-17 12:19:13 -040061/*
62 * Allocate memory of a given size, to be tracked by a given type.
63 * Effects: Returns a pointer to usable memory. If memory cannot
64 * be allocated, aborts execution.
65 */
paul718e3742002-12-13 20:15:29 +000066void *
67zmalloc (int type, size_t size)
68{
69 void *memory;
70
71 memory = malloc (size);
72
73 if (memory == NULL)
74 zerror ("malloc", type, size);
75
76 alloc_inc (type);
77
78 return memory;
79}
80
Greg Troxelb1679382010-09-17 12:19:13 -040081/*
82 * Allocate memory as in zmalloc, and also clear the memory.
83 */
paul718e3742002-12-13 20:15:29 +000084void *
85zcalloc (int type, size_t size)
86{
87 void *memory;
88
89 memory = calloc (1, size);
90
91 if (memory == NULL)
92 zerror ("calloc", type, size);
93
94 alloc_inc (type);
95
96 return memory;
97}
98
Greg Troxelb1679382010-09-17 12:19:13 -040099/*
100 * Given a pointer returned by zmalloc or zcalloc, free it and
101 * return a pointer to a new size, basically acting like realloc().
102 * Requires: ptr was returned by zmalloc, zcalloc, or zrealloc with the
103 * same type.
104 * Effects: Returns a pointer to the new memory, or aborts.
105 */
paul718e3742002-12-13 20:15:29 +0000106void *
107zrealloc (int type, void *ptr, size_t size)
108{
109 void *memory;
110
111 memory = realloc (ptr, size);
112 if (memory == NULL)
113 zerror ("realloc", type, size);
114 return memory;
115}
116
Greg Troxelb1679382010-09-17 12:19:13 -0400117/*
118 * Free memory allocated by z*alloc or zstrdup.
119 * Requires: ptr was returned by zmalloc, zcalloc, or zrealloc with the
120 * same type.
121 * Effects: The memory is freed and may no longer be referenced.
122 */
paul718e3742002-12-13 20:15:29 +0000123void
124zfree (int type, void *ptr)
125{
126 alloc_dec (type);
127 free (ptr);
128}
129
Greg Troxelb1679382010-09-17 12:19:13 -0400130/*
131 * Duplicate a string, counting memory usage by type.
132 * Effects: The string is duplicated, and the return value must
133 * eventually be passed to zfree with the same type. The function will
134 * succeed or abort.
135 */
paul718e3742002-12-13 20:15:29 +0000136char *
hassob04c6992004-10-04 19:10:31 +0000137zstrdup (int type, const char *str)
paul718e3742002-12-13 20:15:29 +0000138{
139 void *dup;
140
141 dup = strdup (str);
142 if (dup == NULL)
143 zerror ("strdup", type, strlen (str));
144 alloc_inc (type);
145 return dup;
146}
147
148#ifdef MEMORY_LOG
ajsf858e492004-11-16 14:25:30 +0000149static struct
paul718e3742002-12-13 20:15:29 +0000150{
hassob04c6992004-10-04 19:10:31 +0000151 const char *name;
Chris Caputo228da422009-07-18 05:44:03 +0000152 long alloc;
paul718e3742002-12-13 20:15:29 +0000153 unsigned long t_malloc;
154 unsigned long c_malloc;
155 unsigned long t_calloc;
156 unsigned long c_calloc;
157 unsigned long t_realloc;
158 unsigned long t_free;
159 unsigned long c_strdup;
160} mstat [MTYPE_MAX];
161
ajsf858e492004-11-16 14:25:30 +0000162static void
paul718e3742002-12-13 20:15:29 +0000163mtype_log (char *func, void *memory, const char *file, int line, int type)
164{
ajsb9e70282004-12-08 17:14:45 +0000165 zlog_debug ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line);
paul718e3742002-12-13 20:15:29 +0000166}
167
168void *
169mtype_zmalloc (const char *file, int line, int type, size_t size)
170{
171 void *memory;
172
173 mstat[type].c_malloc++;
174 mstat[type].t_malloc++;
175
176 memory = zmalloc (type, size);
177 mtype_log ("zmalloc", memory, file, line, type);
178
179 return memory;
180}
181
182void *
183mtype_zcalloc (const char *file, int line, int type, size_t size)
184{
185 void *memory;
186
187 mstat[type].c_calloc++;
188 mstat[type].t_calloc++;
189
190 memory = zcalloc (type, size);
191 mtype_log ("xcalloc", memory, file, line, type);
192
193 return memory;
194}
195
196void *
197mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size)
198{
199 void *memory;
200
201 /* Realloc need before allocated pointer. */
202 mstat[type].t_realloc++;
203
204 memory = zrealloc (type, ptr, size);
205
206 mtype_log ("xrealloc", memory, file, line, type);
207
208 return memory;
209}
210
211/* Important function. */
212void
213mtype_zfree (const char *file, int line, int type, void *ptr)
214{
215 mstat[type].t_free++;
216
217 mtype_log ("xfree", ptr, file, line, type);
218
219 zfree (type, ptr);
220}
221
222char *
hassob04c6992004-10-04 19:10:31 +0000223mtype_zstrdup (const char *file, int line, int type, const char *str)
paul718e3742002-12-13 20:15:29 +0000224{
225 char *memory;
226
227 mstat[type].c_strdup++;
228
229 memory = zstrdup (type, str);
230
231 mtype_log ("xstrdup", memory, file, line, type);
232
233 return memory;
234}
235#else
ajsf858e492004-11-16 14:25:30 +0000236static struct
paul718e3742002-12-13 20:15:29 +0000237{
238 char *name;
Chris Caputo228da422009-07-18 05:44:03 +0000239 long alloc;
paul718e3742002-12-13 20:15:29 +0000240} mstat [MTYPE_MAX];
Chris Caputo228da422009-07-18 05:44:03 +0000241#endif /* MEMORY_LOG */
paul718e3742002-12-13 20:15:29 +0000242
243/* Increment allocation counter. */
ajsf858e492004-11-16 14:25:30 +0000244static void
paul718e3742002-12-13 20:15:29 +0000245alloc_inc (int type)
246{
247 mstat[type].alloc++;
248}
249
250/* Decrement allocation counter. */
ajsf858e492004-11-16 14:25:30 +0000251static void
paul718e3742002-12-13 20:15:29 +0000252alloc_dec (int type)
253{
254 mstat[type].alloc--;
255}
256
257/* Looking up memory status from vty interface. */
258#include "vector.h"
259#include "vty.h"
260#include "command.h"
261
ajs7fa25ff2004-11-15 16:12:32 +0000262static void
263log_memstats(int pri)
264{
265 struct mlist *ml;
266
267 for (ml = mlists; ml->list; ml++)
268 {
269 struct memory_list *m;
270
271 zlog (NULL, pri, "Memory utilization in module %s:", ml->name);
272 for (m = ml->list; m->index >= 0; m++)
273 if (m->index && mstat[m->index].alloc)
paul2fd2fd52005-04-15 11:47:15 +0000274 zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc);
ajs7fa25ff2004-11-15 16:12:32 +0000275 }
276}
277
Chris Caputo228da422009-07-18 05:44:03 +0000278void
279log_memstats_stderr (const char *prefix)
280{
281 struct mlist *ml;
282 struct memory_list *m;
283 int i;
284 int j = 0;
285
286 for (ml = mlists; ml->list; ml++)
287 {
288 i = 0;
289
290 for (m = ml->list; m->index >= 0; m++)
291 if (m->index && mstat[m->index].alloc)
292 {
293 if (!i)
294 fprintf (stderr,
295 "%s: memstats: Current memory utilization in module %s:\n",
296 prefix,
297 ml->name);
298 fprintf (stderr,
299 "%s: memstats: %-30s: %10ld%s\n",
300 prefix,
301 m->format,
302 mstat[m->index].alloc,
303 mstat[m->index].alloc < 0 ? " (REPORT THIS BUG!)" : "");
304 i = j = 1;
305 }
306 }
307
308 if (j)
309 fprintf (stderr,
310 "%s: memstats: NOTE: If configuration exists, utilization may be "
311 "expected.\n",
312 prefix);
313 else
314 fprintf (stderr,
315 "%s: memstats: No remaining tracked memory utilization.\n",
316 prefix);
317}
318
ajsf858e492004-11-16 14:25:30 +0000319static void
ajs24065a32005-10-20 22:28:14 +0000320show_separator(struct vty *vty)
321{
322 vty_out (vty, "-----------------------------\r\n");
323}
324
325static int
paul718e3742002-12-13 20:15:29 +0000326show_memory_vty (struct vty *vty, struct memory_list *list)
327{
328 struct memory_list *m;
ajs24065a32005-10-20 22:28:14 +0000329 int needsep = 0;
paul718e3742002-12-13 20:15:29 +0000330
331 for (m = list; m->index >= 0; m++)
332 if (m->index == 0)
ajs24065a32005-10-20 22:28:14 +0000333 {
334 if (needsep)
335 {
336 show_separator (vty);
337 needsep = 0;
338 }
339 }
340 else if (mstat[m->index].alloc)
341 {
342 vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc);
343 needsep = 1;
344 }
345 return needsep;
paul718e3742002-12-13 20:15:29 +0000346}
347
Paul Jakma41be32b2006-03-30 13:53:59 +0000348#ifdef HAVE_MALLINFO
349static int
350show_memory_mallinfo (struct vty *vty)
351{
352 struct mallinfo minfo = mallinfo();
353 char buf[MTYPE_MEMSTR_LEN];
354
355 vty_out (vty, "System allocator statistics:%s", VTY_NEWLINE);
356 vty_out (vty, " Total heap allocated: %s%s",
357 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.arena),
358 VTY_NEWLINE);
359 vty_out (vty, " Holding block headers: %s%s",
360 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.hblkhd),
361 VTY_NEWLINE);
362 vty_out (vty, " Used small blocks: %s%s",
363 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.usmblks),
364 VTY_NEWLINE);
365 vty_out (vty, " Used ordinary blocks: %s%s",
366 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.uordblks),
367 VTY_NEWLINE);
368 vty_out (vty, " Free small blocks: %s%s",
369 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fsmblks),
370 VTY_NEWLINE);
371 vty_out (vty, " Free ordinary blocks: %s%s",
372 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fordblks),
373 VTY_NEWLINE);
374 vty_out (vty, " Ordinary blocks: %ld%s",
375 (unsigned long)minfo.ordblks,
376 VTY_NEWLINE);
377 vty_out (vty, " Small blocks: %ld%s",
378 (unsigned long)minfo.smblks,
379 VTY_NEWLINE);
380 vty_out (vty, " Holding blocks: %ld%s",
381 (unsigned long)minfo.hblks,
382 VTY_NEWLINE);
383 vty_out (vty, "(see system documentation for 'mallinfo' for meaning)%s",
384 VTY_NEWLINE);
385 return 1;
386}
387#endif /* HAVE_MALLINFO */
388
paul718e3742002-12-13 20:15:29 +0000389DEFUN (show_memory_all,
390 show_memory_all_cmd,
391 "show memory all",
392 "Show running system information\n"
393 "Memory statistics\n"
394 "All memory statistics\n")
395{
ajs7fa25ff2004-11-15 16:12:32 +0000396 struct mlist *ml;
ajs24065a32005-10-20 22:28:14 +0000397 int needsep = 0;
Paul Jakma41be32b2006-03-30 13:53:59 +0000398
399#ifdef HAVE_MALLINFO
400 needsep = show_memory_mallinfo (vty);
401#endif /* HAVE_MALLINFO */
402
ajs7fa25ff2004-11-15 16:12:32 +0000403 for (ml = mlists; ml->list; ml++)
404 {
ajs24065a32005-10-20 22:28:14 +0000405 if (needsep)
406 show_separator (vty);
407 needsep = show_memory_vty (vty, ml->list);
ajs7fa25ff2004-11-15 16:12:32 +0000408 }
paul718e3742002-12-13 20:15:29 +0000409
410 return CMD_SUCCESS;
411}
412
413ALIAS (show_memory_all,
414 show_memory_cmd,
415 "show memory",
416 "Show running system information\n"
417 "Memory statistics\n")
418
419DEFUN (show_memory_lib,
420 show_memory_lib_cmd,
421 "show memory lib",
422 SHOW_STR
423 "Memory statistics\n"
424 "Library memory\n")
425{
426 show_memory_vty (vty, memory_list_lib);
427 return CMD_SUCCESS;
428}
429
paul2fd2fd52005-04-15 11:47:15 +0000430DEFUN (show_memory_zebra,
431 show_memory_zebra_cmd,
432 "show memory zebra",
433 SHOW_STR
434 "Memory statistics\n"
435 "Zebra memory\n")
436{
437 show_memory_vty (vty, memory_list_zebra);
438 return CMD_SUCCESS;
439}
440
paul718e3742002-12-13 20:15:29 +0000441DEFUN (show_memory_rip,
442 show_memory_rip_cmd,
443 "show memory rip",
444 SHOW_STR
445 "Memory statistics\n"
446 "RIP memory\n")
447{
448 show_memory_vty (vty, memory_list_rip);
449 return CMD_SUCCESS;
450}
451
hassoa94434b2003-05-25 17:10:12 +0000452DEFUN (show_memory_ripng,
453 show_memory_ripng_cmd,
454 "show memory ripng",
455 SHOW_STR
456 "Memory statistics\n"
457 "RIPng memory\n")
458{
459 show_memory_vty (vty, memory_list_ripng);
460 return CMD_SUCCESS;
461}
462
paul718e3742002-12-13 20:15:29 +0000463DEFUN (show_memory_bgp,
464 show_memory_bgp_cmd,
465 "show memory bgp",
466 SHOW_STR
467 "Memory statistics\n"
468 "BGP memory\n")
469{
470 show_memory_vty (vty, memory_list_bgp);
471 return CMD_SUCCESS;
472}
473
474DEFUN (show_memory_ospf,
475 show_memory_ospf_cmd,
476 "show memory ospf",
477 SHOW_STR
478 "Memory statistics\n"
479 "OSPF memory\n")
480{
481 show_memory_vty (vty, memory_list_ospf);
482 return CMD_SUCCESS;
483}
484
485DEFUN (show_memory_ospf6,
486 show_memory_ospf6_cmd,
487 "show memory ospf6",
488 SHOW_STR
489 "Memory statistics\n"
490 "OSPF6 memory\n")
491{
492 show_memory_vty (vty, memory_list_ospf6);
493 return CMD_SUCCESS;
494}
495
jardin9e867fe2003-12-23 08:56:18 +0000496DEFUN (show_memory_isis,
497 show_memory_isis_cmd,
498 "show memory isis",
499 SHOW_STR
500 "Memory statistics\n"
501 "ISIS memory\n")
502{
503 show_memory_vty (vty, memory_list_isis);
504 return CMD_SUCCESS;
505}
506
paul718e3742002-12-13 20:15:29 +0000507void
ajsf858e492004-11-16 14:25:30 +0000508memory_init (void)
paul718e3742002-12-13 20:15:29 +0000509{
Paul Jakma62687ff2008-08-23 14:27:06 +0100510 install_element (RESTRICTED_NODE, &show_memory_cmd);
511 install_element (RESTRICTED_NODE, &show_memory_all_cmd);
512 install_element (RESTRICTED_NODE, &show_memory_lib_cmd);
513 install_element (RESTRICTED_NODE, &show_memory_rip_cmd);
514 install_element (RESTRICTED_NODE, &show_memory_ripng_cmd);
515 install_element (RESTRICTED_NODE, &show_memory_bgp_cmd);
516 install_element (RESTRICTED_NODE, &show_memory_ospf_cmd);
517 install_element (RESTRICTED_NODE, &show_memory_ospf6_cmd);
518 install_element (RESTRICTED_NODE, &show_memory_isis_cmd);
519
paul718e3742002-12-13 20:15:29 +0000520 install_element (VIEW_NODE, &show_memory_cmd);
521 install_element (VIEW_NODE, &show_memory_all_cmd);
522 install_element (VIEW_NODE, &show_memory_lib_cmd);
523 install_element (VIEW_NODE, &show_memory_rip_cmd);
hassoa94434b2003-05-25 17:10:12 +0000524 install_element (VIEW_NODE, &show_memory_ripng_cmd);
paul718e3742002-12-13 20:15:29 +0000525 install_element (VIEW_NODE, &show_memory_bgp_cmd);
526 install_element (VIEW_NODE, &show_memory_ospf_cmd);
527 install_element (VIEW_NODE, &show_memory_ospf6_cmd);
jardin9e867fe2003-12-23 08:56:18 +0000528 install_element (VIEW_NODE, &show_memory_isis_cmd);
paul718e3742002-12-13 20:15:29 +0000529
530 install_element (ENABLE_NODE, &show_memory_cmd);
531 install_element (ENABLE_NODE, &show_memory_all_cmd);
532 install_element (ENABLE_NODE, &show_memory_lib_cmd);
paul2fd2fd52005-04-15 11:47:15 +0000533 install_element (ENABLE_NODE, &show_memory_zebra_cmd);
paul718e3742002-12-13 20:15:29 +0000534 install_element (ENABLE_NODE, &show_memory_rip_cmd);
hassoa94434b2003-05-25 17:10:12 +0000535 install_element (ENABLE_NODE, &show_memory_ripng_cmd);
paul718e3742002-12-13 20:15:29 +0000536 install_element (ENABLE_NODE, &show_memory_bgp_cmd);
537 install_element (ENABLE_NODE, &show_memory_ospf_cmd);
538 install_element (ENABLE_NODE, &show_memory_ospf6_cmd);
jardin9e867fe2003-12-23 08:56:18 +0000539 install_element (ENABLE_NODE, &show_memory_isis_cmd);
paul718e3742002-12-13 20:15:29 +0000540}
Paul Jakma41be32b2006-03-30 13:53:59 +0000541
542/* Stats querying from users */
543/* Return a pointer to a human friendly string describing
544 * the byte count passed in. E.g:
545 * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc.
546 * Up to 4 significant figures will be given.
547 * The pointer returned may be NULL (indicating an error)
548 * or point to the given buffer, or point to static storage.
549 */
550const char *
551mtype_memstr (char *buf, size_t len, unsigned long bytes)
552{
553 unsigned int t, g, m, k;
554
555 /* easy cases */
556 if (!bytes)
557 return "0 bytes";
558 if (bytes == 1)
559 return "1 byte";
560
561 if (sizeof (unsigned long) >= 8)
562 /* Hacked to make it not warn on ILP32 machines
563 * Shift will always be 40 at runtime. See below too */
564 t = bytes >> (sizeof (unsigned long) >= 8 ? 40 : 0);
565 else
566 t = 0;
567 g = bytes >> 30;
568 m = bytes >> 20;
569 k = bytes >> 10;
570
571 if (t > 10)
572 {
573 /* The shift will always be 39 at runtime.
574 * Just hacked to make it not warn on 'smaller' machines.
575 * Static compiler analysis should mean no extra code
576 */
Paul Jakma912df1e2008-01-08 13:50:11 +0000577 if (bytes & (1UL << (sizeof (unsigned long) >= 8 ? 39 : 0)))
Paul Jakma41be32b2006-03-30 13:53:59 +0000578 t++;
579 snprintf (buf, len, "%4d TiB", t);
580 }
581 else if (g > 10)
582 {
583 if (bytes & (1 << 29))
584 g++;
585 snprintf (buf, len, "%d GiB", g);
586 }
587 else if (m > 10)
588 {
589 if (bytes & (1 << 19))
590 m++;
591 snprintf (buf, len, "%d MiB", m);
592 }
593 else if (k > 10)
594 {
595 if (bytes & (1 << 9))
596 k++;
597 snprintf (buf, len, "%d KiB", k);
598 }
599 else
600 snprintf (buf, len, "%ld bytes", bytes);
601
602 return buf;
603}
604
605unsigned long
606mtype_stats_alloc (int type)
607{
608 return mstat[type].alloc;
609}