2 * Memory management routine
3 * Copyright (C) 1998 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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
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.
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
24 /* malloc.h is generally obsolete, however GNU Libc mallinfo wants it. */
25 #if !defined(HAVE_STDLIB_H) || (defined(GNU_LINUX) && defined(HAVE_MALLINFO))
27 #endif /* !HAVE_STDLIB_H || HAVE_MALLINFO */
32 static void alloc_inc (int);
33 static void alloc_dec (int);
34 static void log_memstats(int log_priority
);
36 static const struct message mstr
[] =
38 { MTYPE_THREAD
, "thread" },
39 { MTYPE_THREAD_MASTER
, "thread_master" },
40 { MTYPE_VECTOR
, "vector" },
41 { MTYPE_VECTOR_INDEX
, "vector_index" },
42 { MTYPE_IF
, "interface" },
46 /* Fatal memory allocation error occured. */
47 static void __attribute__ ((noreturn
))
48 zerror (const char *fname
, int type
, size_t size
)
50 zlog_err ("%s : can't allocate memory for `%s' size %d: %s\n",
51 fname
, lookup (mstr
, type
), (int) size
, safe_strerror(errno
));
52 log_memstats(LOG_WARNING
);
53 /* N.B. It might be preferable to call zlog_backtrace_sigsafe here, since
54 that function should definitely be safe in an OOM condition. But
55 unfortunately zlog_backtrace_sigsafe does not support syslog logging at
57 zlog_backtrace(LOG_WARNING
);
61 /* Memory allocation. */
63 zmalloc (int type
, size_t size
)
67 memory
= malloc (size
);
70 zerror ("malloc", type
, size
);
77 /* Memory allocation with num * size with cleared. */
79 zcalloc (int type
, size_t size
)
83 memory
= calloc (1, size
);
86 zerror ("calloc", type
, size
);
93 /* Memory reallocation. */
95 zrealloc (int type
, void *ptr
, size_t size
)
99 memory
= realloc (ptr
, size
);
101 zerror ("realloc", type
, size
);
107 zfree (int type
, void *ptr
)
113 /* String duplication. */
115 zstrdup (int type
, const char *str
)
121 zerror ("strdup", type
, strlen (str
));
131 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
;
141 mtype_log (char *func
, void *memory
, const char *file
, int line
, int type
)
143 zlog_debug ("%s: %s %p %s %d", func
, lookup (mstr
, type
), memory
, file
, line
);
147 mtype_zmalloc (const char *file
, int line
, int type
, size_t size
)
151 mstat
[type
].c_malloc
++;
152 mstat
[type
].t_malloc
++;
154 memory
= zmalloc (type
, size
);
155 mtype_log ("zmalloc", memory
, file
, line
, type
);
161 mtype_zcalloc (const char *file
, int line
, int type
, size_t size
)
165 mstat
[type
].c_calloc
++;
166 mstat
[type
].t_calloc
++;
168 memory
= zcalloc (type
, size
);
169 mtype_log ("xcalloc", memory
, file
, line
, type
);
175 mtype_zrealloc (const char *file
, int line
, int type
, void *ptr
, size_t size
)
179 /* Realloc need before allocated pointer. */
180 mstat
[type
].t_realloc
++;
182 memory
= zrealloc (type
, ptr
, size
);
184 mtype_log ("xrealloc", memory
, file
, line
, type
);
189 /* Important function. */
191 mtype_zfree (const char *file
, int line
, int type
, void *ptr
)
193 mstat
[type
].t_free
++;
195 mtype_log ("xfree", ptr
, file
, line
, type
);
201 mtype_zstrdup (const char *file
, int line
, int type
, const char *str
)
205 mstat
[type
].c_strdup
++;
207 memory
= zstrdup (type
, str
);
209 mtype_log ("xstrdup", memory
, file
, line
, type
);
219 #endif /* MEMORY_LOG */
221 /* Increment allocation counter. */
228 /* Decrement allocation counter. */
235 /* Looking up memory status from vty interface. */
241 log_memstats(int pri
)
245 for (ml
= mlists
; ml
->list
; ml
++)
247 struct memory_list
*m
;
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
)
252 zlog (NULL
, pri
, " %-30s: %10ld", m
->format
, mstat
[m
->index
].alloc
);
257 log_memstats_stderr (const char *prefix
)
260 struct memory_list
*m
;
264 for (ml
= mlists
; ml
->list
; ml
++)
268 for (m
= ml
->list
; m
->index
>= 0; m
++)
269 if (m
->index
&& mstat
[m
->index
].alloc
)
273 "%s: memstats: Current memory utilization in module %s:\n",
277 "%s: memstats: %-30s: %10ld%s\n",
280 mstat
[m
->index
].alloc
,
281 mstat
[m
->index
].alloc
< 0 ? " (REPORT THIS BUG!)" : "");
288 "%s: memstats: NOTE: If configuration exists, utilization may be "
293 "%s: memstats: No remaining tracked memory utilization.\n",
298 show_separator(struct vty
*vty
)
300 vty_out (vty
, "-----------------------------\r\n");
304 show_memory_vty (struct vty
*vty
, struct memory_list
*list
)
306 struct memory_list
*m
;
309 for (m
= list
; m
->index
>= 0; m
++)
314 show_separator (vty
);
318 else if (mstat
[m
->index
].alloc
)
320 vty_out (vty
, "%-30s: %10ld\r\n", m
->format
, mstat
[m
->index
].alloc
);
328 show_memory_mallinfo (struct vty
*vty
)
330 struct mallinfo minfo
= mallinfo();
331 char buf
[MTYPE_MEMSTR_LEN
];
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
),
337 vty_out (vty
, " Holding block headers: %s%s",
338 mtype_memstr (buf
, MTYPE_MEMSTR_LEN
, minfo
.hblkhd
),
340 vty_out (vty
, " Used small blocks: %s%s",
341 mtype_memstr (buf
, MTYPE_MEMSTR_LEN
, minfo
.usmblks
),
343 vty_out (vty
, " Used ordinary blocks: %s%s",
344 mtype_memstr (buf
, MTYPE_MEMSTR_LEN
, minfo
.uordblks
),
346 vty_out (vty
, " Free small blocks: %s%s",
347 mtype_memstr (buf
, MTYPE_MEMSTR_LEN
, minfo
.fsmblks
),
349 vty_out (vty
, " Free ordinary blocks: %s%s",
350 mtype_memstr (buf
, MTYPE_MEMSTR_LEN
, minfo
.fordblks
),
352 vty_out (vty
, " Ordinary blocks: %ld%s",
353 (unsigned long)minfo
.ordblks
,
355 vty_out (vty
, " Small blocks: %ld%s",
356 (unsigned long)minfo
.smblks
,
358 vty_out (vty
, " Holding blocks: %ld%s",
359 (unsigned long)minfo
.hblks
,
361 vty_out (vty
, "(see system documentation for 'mallinfo' for meaning)%s",
365 #endif /* HAVE_MALLINFO */
367 DEFUN (show_memory_all
,
370 "Show running system information\n"
371 "Memory statistics\n"
372 "All memory statistics\n")
378 needsep
= show_memory_mallinfo (vty
);
379 #endif /* HAVE_MALLINFO */
381 for (ml
= mlists
; ml
->list
; ml
++)
384 show_separator (vty
);
385 needsep
= show_memory_vty (vty
, ml
->list
);
391 ALIAS (show_memory_all
,
394 "Show running system information\n"
395 "Memory statistics\n")
397 DEFUN (show_memory_lib
,
401 "Memory statistics\n"
404 show_memory_vty (vty
, memory_list_lib
);
408 DEFUN (show_memory_zebra
,
409 show_memory_zebra_cmd
,
412 "Memory statistics\n"
415 show_memory_vty (vty
, memory_list_zebra
);
419 DEFUN (show_memory_rip
,
423 "Memory statistics\n"
426 show_memory_vty (vty
, memory_list_rip
);
430 DEFUN (show_memory_ripng
,
431 show_memory_ripng_cmd
,
434 "Memory statistics\n"
437 show_memory_vty (vty
, memory_list_ripng
);
441 DEFUN (show_memory_bgp
,
445 "Memory statistics\n"
448 show_memory_vty (vty
, memory_list_bgp
);
452 DEFUN (show_memory_ospf
,
453 show_memory_ospf_cmd
,
456 "Memory statistics\n"
459 show_memory_vty (vty
, memory_list_ospf
);
463 DEFUN (show_memory_ospf6
,
464 show_memory_ospf6_cmd
,
467 "Memory statistics\n"
470 show_memory_vty (vty
, memory_list_ospf6
);
474 DEFUN (show_memory_isis
,
475 show_memory_isis_cmd
,
478 "Memory statistics\n"
481 show_memory_vty (vty
, memory_list_isis
);
488 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
);
498 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
);
502 install_element (VIEW_NODE
, &show_memory_ripng_cmd
);
503 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
);
506 install_element (VIEW_NODE
, &show_memory_isis_cmd
);
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
);
511 install_element (ENABLE_NODE
, &show_memory_zebra_cmd
);
512 install_element (ENABLE_NODE
, &show_memory_rip_cmd
);
513 install_element (ENABLE_NODE
, &show_memory_ripng_cmd
);
514 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
);
517 install_element (ENABLE_NODE
, &show_memory_isis_cmd
);
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.
529 mtype_memstr (char *buf
, size_t len
, unsigned long bytes
)
531 unsigned int t
, g
, m
, k
;
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);
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
555 if (bytes
& (1UL << (sizeof (unsigned long) >= 8 ? 39 : 0)))
557 snprintf (buf
, len
, "%4d TiB", t
);
561 if (bytes
& (1 << 29))
563 snprintf (buf
, len
, "%d GiB", g
);
567 if (bytes
& (1 << 19))
569 snprintf (buf
, len
, "%d MiB", m
);
573 if (bytes
& (1 << 9))
575 snprintf (buf
, len
, "%d KiB", k
);
578 snprintf (buf
, len
, "%ld bytes", bytes
);
584 mtype_stats_alloc (int type
)
586 return mstat
[type
].alloc
;