]>
git.proxmox.com Git - mirror_frr.git/blob - lib/memory.c
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
);
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.
67 zmalloc (int type
, size_t size
)
71 memory
= malloc (size
);
74 zerror ("malloc", type
, size
);
82 * Allocate memory as in zmalloc, and also clear the memory.
85 zcalloc (int type
, size_t size
)
89 memory
= calloc (1, size
);
92 zerror ("calloc", type
, size
);
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
104 * Effects: Returns a pointer to the new memory, or aborts.
107 zrealloc (int type
, void *ptr
, size_t size
)
111 memory
= realloc (ptr
, size
);
113 zerror ("realloc", type
, size
);
121 * Free memory allocated by z*alloc or zstrdup.
122 * Requires: ptr was returned by zmalloc, zcalloc, or zrealloc with the
124 * Effects: The memory is freed and may no longer be referenced.
127 zfree (int type
, void *ptr
)
137 * Duplicate a string, counting memory usage by type.
138 * Effects: The string is duplicated, and the return value must
139 * eventually be passed to zfree with the same type. The function will
143 zstrdup (int type
, const char *str
)
149 zerror ("strdup", type
, strlen (str
));
159 unsigned long t_malloc
;
160 unsigned long c_malloc
;
161 unsigned long t_calloc
;
162 unsigned long c_calloc
;
163 unsigned long t_realloc
;
164 unsigned long t_free
;
165 unsigned long c_strdup
;
169 mtype_log (char *func
, void *memory
, const char *file
, int line
, int type
)
171 zlog_debug ("%s: %s %p %s %d", func
, lookup (mstr
, type
), memory
, file
, line
);
175 mtype_zmalloc (const char *file
, int line
, int type
, size_t size
)
179 mstat
[type
].c_malloc
++;
180 mstat
[type
].t_malloc
++;
182 memory
= zmalloc (type
, size
);
183 mtype_log ("zmalloc", memory
, file
, line
, type
);
189 mtype_zcalloc (const char *file
, int line
, int type
, size_t size
)
193 mstat
[type
].c_calloc
++;
194 mstat
[type
].t_calloc
++;
196 memory
= zcalloc (type
, size
);
197 mtype_log ("xcalloc", memory
, file
, line
, type
);
203 mtype_zrealloc (const char *file
, int line
, int type
, void *ptr
, size_t size
)
207 /* Realloc need before allocated pointer. */
208 mstat
[type
].t_realloc
++;
210 memory
= zrealloc (type
, ptr
, size
);
212 mtype_log ("xrealloc", memory
, file
, line
, type
);
217 /* Important function. */
219 mtype_zfree (const char *file
, int line
, int type
, void *ptr
)
221 mstat
[type
].t_free
++;
223 mtype_log ("xfree", ptr
, file
, line
, type
);
229 mtype_zstrdup (const char *file
, int line
, int type
, const char *str
)
233 mstat
[type
].c_strdup
++;
235 memory
= zstrdup (type
, str
);
237 mtype_log ("xstrdup", memory
, file
, line
, type
);
247 #endif /* MEMORY_LOG */
249 /* Increment allocation counter. */
256 /* Decrement allocation counter. */
263 /* Looking up memory status from vty interface. */
269 log_memstats(int pri
)
273 for (ml
= mlists
; ml
->list
; ml
++)
275 struct memory_list
*m
;
277 zlog (NULL
, pri
, "Memory utilization in module %s:", ml
->name
);
278 for (m
= ml
->list
; m
->index
>= 0; m
++)
279 if (m
->index
&& mstat
[m
->index
].alloc
)
280 zlog (NULL
, pri
, " %-30s: %10ld", m
->format
, mstat
[m
->index
].alloc
);
285 log_memstats_stderr (const char *prefix
)
288 struct memory_list
*m
;
292 for (ml
= mlists
; ml
->list
; ml
++)
296 for (m
= ml
->list
; m
->index
>= 0; m
++)
297 if (m
->index
&& mstat
[m
->index
].alloc
)
301 "%s: memstats: Current memory utilization in module %s:\n",
305 "%s: memstats: %-30s: %10ld%s\n",
308 mstat
[m
->index
].alloc
,
309 mstat
[m
->index
].alloc
< 0 ? " (REPORT THIS BUG!)" : "");
316 "%s: memstats: NOTE: If configuration exists, utilization may be "
321 "%s: memstats: No remaining tracked memory utilization.\n",
326 show_separator(struct vty
*vty
)
328 vty_out (vty
, "-----------------------------\r\n");
332 show_memory_vty (struct vty
*vty
, struct memory_list
*list
)
334 struct memory_list
*m
;
337 for (m
= list
; m
->index
>= 0; m
++)
342 show_separator (vty
);
346 else if (mstat
[m
->index
].alloc
)
348 vty_out (vty
, "%-30s: %10ld\r\n", m
->format
, mstat
[m
->index
].alloc
);
356 show_memory_mallinfo (struct vty
*vty
)
358 struct mallinfo minfo
= mallinfo();
359 char buf
[MTYPE_MEMSTR_LEN
];
361 vty_out (vty
, "System allocator statistics:%s", VTY_NEWLINE
);
362 vty_out (vty
, " Total heap allocated: %s%s",
363 mtype_memstr (buf
, MTYPE_MEMSTR_LEN
, minfo
.arena
),
365 vty_out (vty
, " Holding block headers: %s%s",
366 mtype_memstr (buf
, MTYPE_MEMSTR_LEN
, minfo
.hblkhd
),
368 vty_out (vty
, " Used small blocks: %s%s",
369 mtype_memstr (buf
, MTYPE_MEMSTR_LEN
, minfo
.usmblks
),
371 vty_out (vty
, " Used ordinary blocks: %s%s",
372 mtype_memstr (buf
, MTYPE_MEMSTR_LEN
, minfo
.uordblks
),
374 vty_out (vty
, " Free small blocks: %s%s",
375 mtype_memstr (buf
, MTYPE_MEMSTR_LEN
, minfo
.fsmblks
),
377 vty_out (vty
, " Free ordinary blocks: %s%s",
378 mtype_memstr (buf
, MTYPE_MEMSTR_LEN
, minfo
.fordblks
),
380 vty_out (vty
, " Ordinary blocks: %ld%s",
381 (unsigned long)minfo
.ordblks
,
383 vty_out (vty
, " Small blocks: %ld%s",
384 (unsigned long)minfo
.smblks
,
386 vty_out (vty
, " Holding blocks: %ld%s",
387 (unsigned long)minfo
.hblks
,
389 vty_out (vty
, "(see system documentation for 'mallinfo' for meaning)%s",
393 #endif /* HAVE_MALLINFO */
395 DEFUN (show_memory_all
,
398 "Show running system information\n"
399 "Memory statistics\n"
400 "All memory statistics\n")
406 needsep
= show_memory_mallinfo (vty
);
407 #endif /* HAVE_MALLINFO */
409 for (ml
= mlists
; ml
->list
; ml
++)
412 show_separator (vty
);
413 needsep
= show_memory_vty (vty
, ml
->list
);
419 ALIAS (show_memory_all
,
422 "Show running system information\n"
423 "Memory statistics\n")
425 DEFUN (show_memory_lib
,
429 "Memory statistics\n"
432 show_memory_vty (vty
, memory_list_lib
);
436 DEFUN (show_memory_zebra
,
437 show_memory_zebra_cmd
,
440 "Memory statistics\n"
443 show_memory_vty (vty
, memory_list_zebra
);
447 DEFUN (show_memory_rip
,
451 "Memory statistics\n"
454 show_memory_vty (vty
, memory_list_rip
);
458 DEFUN (show_memory_ripng
,
459 show_memory_ripng_cmd
,
462 "Memory statistics\n"
465 show_memory_vty (vty
, memory_list_ripng
);
469 DEFUN (show_memory_bgp
,
473 "Memory statistics\n"
476 show_memory_vty (vty
, memory_list_bgp
);
480 DEFUN (show_memory_ospf
,
481 show_memory_ospf_cmd
,
484 "Memory statistics\n"
487 show_memory_vty (vty
, memory_list_ospf
);
491 DEFUN (show_memory_ospf6
,
492 show_memory_ospf6_cmd
,
495 "Memory statistics\n"
498 show_memory_vty (vty
, memory_list_ospf6
);
502 DEFUN (show_memory_isis
,
503 show_memory_isis_cmd
,
506 "Memory statistics\n"
509 show_memory_vty (vty
, memory_list_isis
);
516 install_element (RESTRICTED_NODE
, &show_memory_cmd
);
517 install_element (RESTRICTED_NODE
, &show_memory_all_cmd
);
518 install_element (RESTRICTED_NODE
, &show_memory_lib_cmd
);
519 install_element (RESTRICTED_NODE
, &show_memory_rip_cmd
);
520 install_element (RESTRICTED_NODE
, &show_memory_ripng_cmd
);
521 install_element (RESTRICTED_NODE
, &show_memory_bgp_cmd
);
522 install_element (RESTRICTED_NODE
, &show_memory_ospf_cmd
);
523 install_element (RESTRICTED_NODE
, &show_memory_ospf6_cmd
);
524 install_element (RESTRICTED_NODE
, &show_memory_isis_cmd
);
526 install_element (VIEW_NODE
, &show_memory_cmd
);
527 install_element (VIEW_NODE
, &show_memory_all_cmd
);
528 install_element (VIEW_NODE
, &show_memory_lib_cmd
);
529 install_element (VIEW_NODE
, &show_memory_rip_cmd
);
530 install_element (VIEW_NODE
, &show_memory_ripng_cmd
);
531 install_element (VIEW_NODE
, &show_memory_bgp_cmd
);
532 install_element (VIEW_NODE
, &show_memory_ospf_cmd
);
533 install_element (VIEW_NODE
, &show_memory_ospf6_cmd
);
534 install_element (VIEW_NODE
, &show_memory_isis_cmd
);
536 install_element (ENABLE_NODE
, &show_memory_cmd
);
537 install_element (ENABLE_NODE
, &show_memory_all_cmd
);
538 install_element (ENABLE_NODE
, &show_memory_lib_cmd
);
539 install_element (ENABLE_NODE
, &show_memory_zebra_cmd
);
540 install_element (ENABLE_NODE
, &show_memory_rip_cmd
);
541 install_element (ENABLE_NODE
, &show_memory_ripng_cmd
);
542 install_element (ENABLE_NODE
, &show_memory_bgp_cmd
);
543 install_element (ENABLE_NODE
, &show_memory_ospf_cmd
);
544 install_element (ENABLE_NODE
, &show_memory_ospf6_cmd
);
545 install_element (ENABLE_NODE
, &show_memory_isis_cmd
);
548 /* Stats querying from users */
549 /* Return a pointer to a human friendly string describing
550 * the byte count passed in. E.g:
551 * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc.
552 * Up to 4 significant figures will be given.
553 * The pointer returned may be NULL (indicating an error)
554 * or point to the given buffer, or point to static storage.
557 mtype_memstr (char *buf
, size_t len
, unsigned long bytes
)
559 unsigned int t
, g
, m
, k
;
567 if (sizeof (unsigned long) >= 8)
568 /* Hacked to make it not warn on ILP32 machines
569 * Shift will always be 40 at runtime. See below too */
570 t
= bytes
>> (sizeof (unsigned long) >= 8 ? 40 : 0);
579 /* The shift will always be 39 at runtime.
580 * Just hacked to make it not warn on 'smaller' machines.
581 * Static compiler analysis should mean no extra code
583 if (bytes
& (1UL << (sizeof (unsigned long) >= 8 ? 39 : 0)))
585 snprintf (buf
, len
, "%4d TiB", t
);
589 if (bytes
& (1 << 29))
591 snprintf (buf
, len
, "%d GiB", g
);
595 if (bytes
& (1 << 19))
597 snprintf (buf
, len
, "%d MiB", m
);
601 if (bytes
& (1 << 9))
603 snprintf (buf
, len
, "%d KiB", k
);
606 snprintf (buf
, len
, "%ld bytes", bytes
);
612 mtype_stats_alloc (int type
)
614 return mstat
[type
].alloc
;