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 occurred. */
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.
83 * Add an extra 'z' prefix to function name to avoid collision when linking
84 * statically with zlib that exports the 'zcalloc' symbol.
87 zzcalloc (int type, size_t size)
91 memory = calloc (1, size);
94 zerror ("calloc", type, size);
102 * Given a pointer returned by zmalloc or zzcalloc, free it and
103 * return a pointer to a new size, basically acting like realloc().
104 * Requires: ptr was returned by zmalloc, zzcalloc, or zrealloc with the
106 * Effects: Returns a pointer to the new memory, or aborts.
109 zrealloc (int type, void *ptr, size_t size)
113 if (ptr == NULL) /* is really alloc */
114 return zzcalloc(type, size);
116 memory = realloc (ptr, size);
118 zerror ("realloc", type, size);
126 * Free memory allocated by z*alloc or zstrdup.
127 * Requires: ptr was returned by zmalloc, zzcalloc, or zrealloc with the
129 * Effects: The memory is freed and may no longer be referenced.
132 zfree (int type, void *ptr)
142 * Duplicate a string, counting memory usage by type.
143 * Effects: The string is duplicated, and the return value must
144 * eventually be passed to zfree with the same type. The function will
148 zstrdup (int type, const char *str)
154 zerror ("strdup", type, strlen (str));
164 unsigned long t_malloc;
165 unsigned long c_malloc;
166 unsigned long t_calloc;
167 unsigned long c_calloc;
168 unsigned long t_realloc;
169 unsigned long t_free;
170 unsigned long c_strdup;
174 mtype_log (char *func, void *memory, const char *file, int line, int type)
176 zlog_debug ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line);
180 mtype_zmalloc (const char *file, int line, int type, size_t size)
184 mstat[type].c_malloc++;
185 mstat[type].t_malloc++;
187 memory = zmalloc (type, size);
188 mtype_log ("zmalloc", memory, file, line, type);
194 mtype_zcalloc (const char *file, int line, int type, size_t size)
198 mstat[type].c_calloc++;
199 mstat[type].t_calloc++;
201 memory = zzcalloc (type, size);
202 mtype_log ("xcalloc", memory, file, line, type);
208 mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size)
212 /* Realloc need before allocated pointer. */
213 mstat[type].t_realloc++;
215 memory = zrealloc (type, ptr, size);
217 mtype_log ("xrealloc", memory, file, line, type);
222 /* Important function. */
224 mtype_zfree (const char *file, int line, int type, void *ptr)
226 mstat[type].t_free++;
228 mtype_log ("xfree", ptr, file, line, type);
234 mtype_zstrdup (const char *file, int line, int type, const char *str)
238 mstat[type].c_strdup++;
240 memory = zstrdup (type, str);
242 mtype_log ("xstrdup", memory, file, line, type);
252 #endif /* MEMORY_LOG */
254 /* Increment allocation counter. */
261 /* Decrement allocation counter. */
268 /* Looking up memory status from vty interface. */
274 log_memstats(int pri)
278 for (ml = mlists; ml->list; ml++)
280 struct memory_list *m;
282 zlog (NULL, pri, "Memory utilization in module %s:", ml->name);
283 for (m = ml->list; m->index >= 0; m++)
284 if (m->index && mstat[m->index].alloc)
285 zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc);
290 log_memstats_stderr (const char *prefix)
293 struct memory_list *m;
297 for (ml = mlists; ml->list; ml++)
301 for (m = ml->list; m->index >= 0; m++)
302 if (m->index && mstat[m->index].alloc)
306 "%s: memstats: Current memory utilization in module %s:\n",
310 "%s: memstats: %-30s: %10ld%s\n",
313 mstat[m->index].alloc,
314 mstat[m->index].alloc < 0 ? " (REPORT THIS BUG!)" : "");
321 "%s: memstats: NOTE: If configuration exists, utilization may be "
326 "%s: memstats: No remaining tracked memory utilization.\n",
331 show_separator(struct vty *vty)
333 vty_out (vty, "-----------------------------\r\n");
337 show_memory_vty (struct vty *vty, struct memory_list *list)
339 struct memory_list *m;
342 for (m = list; m->index >= 0; m++)
347 show_separator (vty);
351 else if (mstat[m->index].alloc)
353 vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc);
361 show_memory_mallinfo (struct vty *vty)
363 struct mallinfo minfo = mallinfo();
364 char buf[MTYPE_MEMSTR_LEN];
366 vty_out (vty, "System allocator statistics:%s", VTY_NEWLINE);
367 vty_out (vty, " Total heap allocated: %s%s",
368 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.arena),
370 vty_out (vty, " Holding block headers: %s%s",
371 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.hblkhd),
373 vty_out (vty, " Used small blocks: %s%s",
374 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.usmblks),
376 vty_out (vty, " Used ordinary blocks: %s%s",
377 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.uordblks),
379 vty_out (vty, " Free small blocks: %s%s",
380 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fsmblks),
382 vty_out (vty, " Free ordinary blocks: %s%s",
383 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fordblks),
385 vty_out (vty, " Ordinary blocks: %ld%s",
386 (unsigned long)minfo.ordblks,
388 vty_out (vty, " Small blocks: %ld%s",
389 (unsigned long)minfo.smblks,
391 vty_out (vty, " Holding blocks: %ld%s",
392 (unsigned long)minfo.hblks,
394 vty_out (vty, "(see system documentation for 'mallinfo' for meaning)%s",
398 #endif /* HAVE_MALLINFO */
403 "Show running system information\n"
404 "Memory statistics\n")
410 needsep = show_memory_mallinfo (vty);
411 #endif /* HAVE_MALLINFO */
413 for (ml = mlists; ml->list; ml++)
416 show_separator (vty);
417 needsep = show_memory_vty (vty, ml->list);
427 install_element (RESTRICTED_NODE, &show_memory_cmd);
429 install_element (VIEW_NODE, &show_memory_cmd);
432 /* Stats querying from users */
433 /* Return a pointer to a human friendly string describing
434 * the byte count passed in. E.g:
435 * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc.
436 * Up to 4 significant figures will be given.
437 * The pointer returned may be NULL (indicating an error)
438 * or point to the given buffer, or point to static storage.
441 mtype_memstr (char *buf, size_t len, unsigned long bytes)
452 * When we pass the 2gb barrier mallinfo() can no longer report
453 * correct data so it just does something odd...
454 * Reporting like Terrabytes of data. Which makes users...
455 * edgy.. yes edgy that's the term for it.
456 * So let's just give up gracefully
458 if (bytes > 0x7fffffff)
466 if (bytes & (1 << 19))
468 snprintf (buf, len, "%d MiB", m);
472 if (bytes & (1 << 9))
474 snprintf (buf, len, "%d KiB", k);
477 snprintf (buf, len, "%ld bytes", bytes);
483 mtype_stats_alloc (int type)
485 return mstat[type].alloc;