blob: 13e1fb7c0c43053a108120447ce836c98d41ec59 [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);
David Lamparter6b0655a2014-06-04 06:53:35 +020035
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};
David Lamparter6b0655a2014-06-04 06:53:35 +020045
paul718e3742002-12-13 20:15:29 +000046/* 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
Lou Berger9248b612016-01-12 13:41:47 -0500111 if (ptr == NULL) /* is really alloc */
112 return zcalloc(type, size);
113
paul718e3742002-12-13 20:15:29 +0000114 memory = realloc (ptr, size);
115 if (memory == NULL)
116 zerror ("realloc", type, size);
Chris Hallcca85d22010-07-23 11:27:11 -0700117 if (ptr == NULL)
118 alloc_inc (type);
119
paul718e3742002-12-13 20:15:29 +0000120 return memory;
121}
122
Greg Troxelb1679382010-09-17 12:19:13 -0400123/*
124 * Free memory allocated by z*alloc or zstrdup.
125 * Requires: ptr was returned by zmalloc, zcalloc, or zrealloc with the
126 * same type.
127 * Effects: The memory is freed and may no longer be referenced.
128 */
paul718e3742002-12-13 20:15:29 +0000129void
130zfree (int type, void *ptr)
131{
Chris Hallcca85d22010-07-23 11:27:11 -0700132 if (ptr != NULL)
133 {
134 alloc_dec (type);
135 free (ptr);
136 }
paul718e3742002-12-13 20:15:29 +0000137}
138
Greg Troxelb1679382010-09-17 12:19:13 -0400139/*
140 * Duplicate a string, counting memory usage by type.
141 * Effects: The string is duplicated, and the return value must
142 * eventually be passed to zfree with the same type. The function will
143 * succeed or abort.
144 */
paul718e3742002-12-13 20:15:29 +0000145char *
hassob04c6992004-10-04 19:10:31 +0000146zstrdup (int type, const char *str)
paul718e3742002-12-13 20:15:29 +0000147{
148 void *dup;
149
150 dup = strdup (str);
151 if (dup == NULL)
152 zerror ("strdup", type, strlen (str));
153 alloc_inc (type);
154 return dup;
155}
David Lamparter6b0655a2014-06-04 06:53:35 +0200156
paul718e3742002-12-13 20:15:29 +0000157#ifdef MEMORY_LOG
ajsf858e492004-11-16 14:25:30 +0000158static struct
paul718e3742002-12-13 20:15:29 +0000159{
hassob04c6992004-10-04 19:10:31 +0000160 const char *name;
Chris Caputo228da422009-07-18 05:44:03 +0000161 long alloc;
paul718e3742002-12-13 20:15:29 +0000162 unsigned long t_malloc;
163 unsigned long c_malloc;
164 unsigned long t_calloc;
165 unsigned long c_calloc;
166 unsigned long t_realloc;
167 unsigned long t_free;
168 unsigned long c_strdup;
169} mstat [MTYPE_MAX];
170
ajsf858e492004-11-16 14:25:30 +0000171static void
paul718e3742002-12-13 20:15:29 +0000172mtype_log (char *func, void *memory, const char *file, int line, int type)
173{
ajsb9e70282004-12-08 17:14:45 +0000174 zlog_debug ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line);
paul718e3742002-12-13 20:15:29 +0000175}
176
177void *
178mtype_zmalloc (const char *file, int line, int type, size_t size)
179{
180 void *memory;
181
182 mstat[type].c_malloc++;
183 mstat[type].t_malloc++;
184
185 memory = zmalloc (type, size);
186 mtype_log ("zmalloc", memory, file, line, type);
187
188 return memory;
189}
190
191void *
192mtype_zcalloc (const char *file, int line, int type, size_t size)
193{
194 void *memory;
195
196 mstat[type].c_calloc++;
197 mstat[type].t_calloc++;
198
199 memory = zcalloc (type, size);
200 mtype_log ("xcalloc", memory, file, line, type);
201
202 return memory;
203}
204
205void *
206mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size)
207{
208 void *memory;
209
210 /* Realloc need before allocated pointer. */
211 mstat[type].t_realloc++;
212
213 memory = zrealloc (type, ptr, size);
214
215 mtype_log ("xrealloc", memory, file, line, type);
216
217 return memory;
218}
219
220/* Important function. */
221void
222mtype_zfree (const char *file, int line, int type, void *ptr)
223{
224 mstat[type].t_free++;
225
226 mtype_log ("xfree", ptr, file, line, type);
227
228 zfree (type, ptr);
229}
230
231char *
hassob04c6992004-10-04 19:10:31 +0000232mtype_zstrdup (const char *file, int line, int type, const char *str)
paul718e3742002-12-13 20:15:29 +0000233{
234 char *memory;
235
236 mstat[type].c_strdup++;
237
238 memory = zstrdup (type, str);
239
240 mtype_log ("xstrdup", memory, file, line, type);
241
242 return memory;
243}
244#else
ajsf858e492004-11-16 14:25:30 +0000245static struct
paul718e3742002-12-13 20:15:29 +0000246{
247 char *name;
Chris Caputo228da422009-07-18 05:44:03 +0000248 long alloc;
paul718e3742002-12-13 20:15:29 +0000249} mstat [MTYPE_MAX];
Chris Caputo228da422009-07-18 05:44:03 +0000250#endif /* MEMORY_LOG */
paul718e3742002-12-13 20:15:29 +0000251
252/* Increment allocation counter. */
ajsf858e492004-11-16 14:25:30 +0000253static void
paul718e3742002-12-13 20:15:29 +0000254alloc_inc (int type)
255{
256 mstat[type].alloc++;
257}
258
259/* Decrement allocation counter. */
ajsf858e492004-11-16 14:25:30 +0000260static void
paul718e3742002-12-13 20:15:29 +0000261alloc_dec (int type)
262{
263 mstat[type].alloc--;
264}
David Lamparter6b0655a2014-06-04 06:53:35 +0200265
paul718e3742002-12-13 20:15:29 +0000266/* Looking up memory status from vty interface. */
267#include "vector.h"
268#include "vty.h"
269#include "command.h"
270
ajs7fa25ff2004-11-15 16:12:32 +0000271static void
272log_memstats(int pri)
273{
274 struct mlist *ml;
275
276 for (ml = mlists; ml->list; ml++)
277 {
278 struct memory_list *m;
279
280 zlog (NULL, pri, "Memory utilization in module %s:", ml->name);
281 for (m = ml->list; m->index >= 0; m++)
282 if (m->index && mstat[m->index].alloc)
paul2fd2fd52005-04-15 11:47:15 +0000283 zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc);
ajs7fa25ff2004-11-15 16:12:32 +0000284 }
285}
286
Chris Caputo228da422009-07-18 05:44:03 +0000287void
288log_memstats_stderr (const char *prefix)
289{
290 struct mlist *ml;
291 struct memory_list *m;
292 int i;
293 int j = 0;
294
295 for (ml = mlists; ml->list; ml++)
296 {
297 i = 0;
298
299 for (m = ml->list; m->index >= 0; m++)
300 if (m->index && mstat[m->index].alloc)
301 {
302 if (!i)
303 fprintf (stderr,
304 "%s: memstats: Current memory utilization in module %s:\n",
305 prefix,
306 ml->name);
307 fprintf (stderr,
308 "%s: memstats: %-30s: %10ld%s\n",
309 prefix,
310 m->format,
311 mstat[m->index].alloc,
312 mstat[m->index].alloc < 0 ? " (REPORT THIS BUG!)" : "");
313 i = j = 1;
314 }
315 }
316
317 if (j)
318 fprintf (stderr,
319 "%s: memstats: NOTE: If configuration exists, utilization may be "
320 "expected.\n",
321 prefix);
322 else
323 fprintf (stderr,
324 "%s: memstats: No remaining tracked memory utilization.\n",
325 prefix);
326}
327
ajsf858e492004-11-16 14:25:30 +0000328static void
ajs24065a32005-10-20 22:28:14 +0000329show_separator(struct vty *vty)
330{
331 vty_out (vty, "-----------------------------\r\n");
332}
333
334static int
paul718e3742002-12-13 20:15:29 +0000335show_memory_vty (struct vty *vty, struct memory_list *list)
336{
337 struct memory_list *m;
ajs24065a32005-10-20 22:28:14 +0000338 int needsep = 0;
paul718e3742002-12-13 20:15:29 +0000339
340 for (m = list; m->index >= 0; m++)
341 if (m->index == 0)
ajs24065a32005-10-20 22:28:14 +0000342 {
343 if (needsep)
344 {
345 show_separator (vty);
346 needsep = 0;
347 }
348 }
349 else if (mstat[m->index].alloc)
350 {
351 vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc);
352 needsep = 1;
353 }
354 return needsep;
paul718e3742002-12-13 20:15:29 +0000355}
356
Paul Jakma41be32b2006-03-30 13:53:59 +0000357#ifdef HAVE_MALLINFO
358static int
359show_memory_mallinfo (struct vty *vty)
360{
361 struct mallinfo minfo = mallinfo();
362 char buf[MTYPE_MEMSTR_LEN];
363
364 vty_out (vty, "System allocator statistics:%s", VTY_NEWLINE);
365 vty_out (vty, " Total heap allocated: %s%s",
366 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.arena),
367 VTY_NEWLINE);
368 vty_out (vty, " Holding block headers: %s%s",
369 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.hblkhd),
370 VTY_NEWLINE);
371 vty_out (vty, " Used small blocks: %s%s",
372 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.usmblks),
373 VTY_NEWLINE);
374 vty_out (vty, " Used ordinary blocks: %s%s",
375 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.uordblks),
376 VTY_NEWLINE);
377 vty_out (vty, " Free small blocks: %s%s",
378 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fsmblks),
379 VTY_NEWLINE);
380 vty_out (vty, " Free ordinary blocks: %s%s",
381 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fordblks),
382 VTY_NEWLINE);
383 vty_out (vty, " Ordinary blocks: %ld%s",
384 (unsigned long)minfo.ordblks,
385 VTY_NEWLINE);
386 vty_out (vty, " Small blocks: %ld%s",
387 (unsigned long)minfo.smblks,
388 VTY_NEWLINE);
389 vty_out (vty, " Holding blocks: %ld%s",
390 (unsigned long)minfo.hblks,
391 VTY_NEWLINE);
392 vty_out (vty, "(see system documentation for 'mallinfo' for meaning)%s",
393 VTY_NEWLINE);
394 return 1;
395}
396#endif /* HAVE_MALLINFO */
397
Donald Sharpfbc3e972015-08-12 14:32:47 -0400398DEFUN (show_memory,
399 show_memory_cmd,
400 "show memory",
paul718e3742002-12-13 20:15:29 +0000401 "Show running system information\n"
Donald Sharpfbc3e972015-08-12 14:32:47 -0400402 "Memory statistics\n")
paul718e3742002-12-13 20:15:29 +0000403{
ajs7fa25ff2004-11-15 16:12:32 +0000404 struct mlist *ml;
ajs24065a32005-10-20 22:28:14 +0000405 int needsep = 0;
Paul Jakma41be32b2006-03-30 13:53:59 +0000406
407#ifdef HAVE_MALLINFO
408 needsep = show_memory_mallinfo (vty);
409#endif /* HAVE_MALLINFO */
410
ajs7fa25ff2004-11-15 16:12:32 +0000411 for (ml = mlists; ml->list; ml++)
412 {
ajs24065a32005-10-20 22:28:14 +0000413 if (needsep)
414 show_separator (vty);
415 needsep = show_memory_vty (vty, ml->list);
ajs7fa25ff2004-11-15 16:12:32 +0000416 }
paul718e3742002-12-13 20:15:29 +0000417
418 return CMD_SUCCESS;
419}
420
Everton Marques871dbcf2009-08-11 15:43:05 -0300421
paul718e3742002-12-13 20:15:29 +0000422void
ajsf858e492004-11-16 14:25:30 +0000423memory_init (void)
paul718e3742002-12-13 20:15:29 +0000424{
Paul Jakma62687ff2008-08-23 14:27:06 +0100425 install_element (RESTRICTED_NODE, &show_memory_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +0100426
paul718e3742002-12-13 20:15:29 +0000427 install_element (VIEW_NODE, &show_memory_cmd);
paul718e3742002-12-13 20:15:29 +0000428}
David Lamparter6b0655a2014-06-04 06:53:35 +0200429
Paul Jakma41be32b2006-03-30 13:53:59 +0000430/* Stats querying from users */
431/* Return a pointer to a human friendly string describing
432 * the byte count passed in. E.g:
433 * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc.
434 * Up to 4 significant figures will be given.
435 * The pointer returned may be NULL (indicating an error)
436 * or point to the given buffer, or point to static storage.
437 */
438const char *
439mtype_memstr (char *buf, size_t len, unsigned long bytes)
440{
Donald Sharp929a26a2015-10-28 19:59:30 -0400441 unsigned int m, k;
442
Paul Jakma41be32b2006-03-30 13:53:59 +0000443 /* easy cases */
444 if (!bytes)
445 return "0 bytes";
446 if (bytes == 1)
447 return "1 byte";
Donald Sharp929a26a2015-10-28 19:59:30 -0400448
449 /*
450 * When we pass the 2gb barrier mallinfo() can no longer report
451 * correct data so it just does something odd...
452 * Reporting like Terrabytes of data. Which makes users...
453 * edgy.. yes edgy that's the term for it.
454 * So let's just give up gracefully
455 */
456 if (bytes > 0x7fffffff)
457 return "> 2GB";
458
Paul Jakma41be32b2006-03-30 13:53:59 +0000459 m = bytes >> 20;
460 k = bytes >> 10;
Donald Sharp929a26a2015-10-28 19:59:30 -0400461
462 if (m > 10)
Paul Jakma41be32b2006-03-30 13:53:59 +0000463 {
464 if (bytes & (1 << 19))
465 m++;
466 snprintf (buf, len, "%d MiB", m);
467 }
468 else if (k > 10)
469 {
470 if (bytes & (1 << 9))
471 k++;
472 snprintf (buf, len, "%d KiB", k);
473 }
474 else
475 snprintf (buf, len, "%ld bytes", bytes);
476
477 return buf;
478}
479
480unsigned long
481mtype_stats_alloc (int type)
482{
483 return mstat[type].alloc;
484}