blob: b8305dde4d008a9e51400ad06efe4c5326cd57e8 [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.
Baruch Siach9ed99f02016-08-21 09:23:05 +030083 * Add an extra 'z' prefix to function name to avoid collision when linking
84 * statically with zlib that exports the 'zcalloc' symbol.
Greg Troxelb1679382010-09-17 12:19:13 -040085 */
paul718e3742002-12-13 20:15:29 +000086void *
Baruch Siach9ed99f02016-08-21 09:23:05 +030087zzcalloc (int type, size_t size)
paul718e3742002-12-13 20:15:29 +000088{
89 void *memory;
90
91 memory = calloc (1, size);
92
93 if (memory == NULL)
94 zerror ("calloc", type, size);
95
96 alloc_inc (type);
97
98 return memory;
99}
100
Greg Troxelb1679382010-09-17 12:19:13 -0400101/*
Baruch Siach9ed99f02016-08-21 09:23:05 +0300102 * Given a pointer returned by zmalloc or zzcalloc, free it and
Greg Troxelb1679382010-09-17 12:19:13 -0400103 * return a pointer to a new size, basically acting like realloc().
Baruch Siach9ed99f02016-08-21 09:23:05 +0300104 * Requires: ptr was returned by zmalloc, zzcalloc, or zrealloc with the
Greg Troxelb1679382010-09-17 12:19:13 -0400105 * same type.
106 * Effects: Returns a pointer to the new memory, or aborts.
107 */
paul718e3742002-12-13 20:15:29 +0000108void *
109zrealloc (int type, void *ptr, size_t size)
110{
111 void *memory;
112
Lou Berger9248b612016-01-12 13:41:47 -0500113 if (ptr == NULL) /* is really alloc */
Baruch Siach9ed99f02016-08-21 09:23:05 +0300114 return zzcalloc(type, size);
Lou Berger9248b612016-01-12 13:41:47 -0500115
paul718e3742002-12-13 20:15:29 +0000116 memory = realloc (ptr, size);
117 if (memory == NULL)
118 zerror ("realloc", type, size);
Chris Hallcca85d22010-07-23 11:27:11 -0700119 if (ptr == NULL)
120 alloc_inc (type);
121
paul718e3742002-12-13 20:15:29 +0000122 return memory;
123}
124
Greg Troxelb1679382010-09-17 12:19:13 -0400125/*
126 * Free memory allocated by z*alloc or zstrdup.
Baruch Siach9ed99f02016-08-21 09:23:05 +0300127 * Requires: ptr was returned by zmalloc, zzcalloc, or zrealloc with the
Greg Troxelb1679382010-09-17 12:19:13 -0400128 * same type.
129 * Effects: The memory is freed and may no longer be referenced.
130 */
paul718e3742002-12-13 20:15:29 +0000131void
132zfree (int type, void *ptr)
133{
Chris Hallcca85d22010-07-23 11:27:11 -0700134 if (ptr != NULL)
135 {
136 alloc_dec (type);
137 free (ptr);
138 }
paul718e3742002-12-13 20:15:29 +0000139}
140
Greg Troxelb1679382010-09-17 12:19:13 -0400141/*
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
145 * succeed or abort.
146 */
paul718e3742002-12-13 20:15:29 +0000147char *
hassob04c6992004-10-04 19:10:31 +0000148zstrdup (int type, const char *str)
paul718e3742002-12-13 20:15:29 +0000149{
150 void *dup;
151
152 dup = strdup (str);
153 if (dup == NULL)
154 zerror ("strdup", type, strlen (str));
155 alloc_inc (type);
156 return dup;
157}
David Lamparter6b0655a2014-06-04 06:53:35 +0200158
paul718e3742002-12-13 20:15:29 +0000159#ifdef MEMORY_LOG
ajsf858e492004-11-16 14:25:30 +0000160static struct
paul718e3742002-12-13 20:15:29 +0000161{
hassob04c6992004-10-04 19:10:31 +0000162 const char *name;
Chris Caputo228da422009-07-18 05:44:03 +0000163 long alloc;
paul718e3742002-12-13 20:15:29 +0000164 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;
171} mstat [MTYPE_MAX];
172
ajsf858e492004-11-16 14:25:30 +0000173static void
paul718e3742002-12-13 20:15:29 +0000174mtype_log (char *func, void *memory, const char *file, int line, int type)
175{
ajsb9e70282004-12-08 17:14:45 +0000176 zlog_debug ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line);
paul718e3742002-12-13 20:15:29 +0000177}
178
179void *
180mtype_zmalloc (const char *file, int line, int type, size_t size)
181{
182 void *memory;
183
184 mstat[type].c_malloc++;
185 mstat[type].t_malloc++;
186
187 memory = zmalloc (type, size);
188 mtype_log ("zmalloc", memory, file, line, type);
189
190 return memory;
191}
192
193void *
194mtype_zcalloc (const char *file, int line, int type, size_t size)
195{
196 void *memory;
197
198 mstat[type].c_calloc++;
199 mstat[type].t_calloc++;
200
Baruch Siach9ed99f02016-08-21 09:23:05 +0300201 memory = zzcalloc (type, size);
paul718e3742002-12-13 20:15:29 +0000202 mtype_log ("xcalloc", memory, file, line, type);
203
204 return memory;
205}
206
207void *
208mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size)
209{
210 void *memory;
211
212 /* Realloc need before allocated pointer. */
213 mstat[type].t_realloc++;
214
215 memory = zrealloc (type, ptr, size);
216
217 mtype_log ("xrealloc", memory, file, line, type);
218
219 return memory;
220}
221
222/* Important function. */
223void
224mtype_zfree (const char *file, int line, int type, void *ptr)
225{
226 mstat[type].t_free++;
227
228 mtype_log ("xfree", ptr, file, line, type);
229
230 zfree (type, ptr);
231}
232
233char *
hassob04c6992004-10-04 19:10:31 +0000234mtype_zstrdup (const char *file, int line, int type, const char *str)
paul718e3742002-12-13 20:15:29 +0000235{
236 char *memory;
237
238 mstat[type].c_strdup++;
239
240 memory = zstrdup (type, str);
241
242 mtype_log ("xstrdup", memory, file, line, type);
243
244 return memory;
245}
246#else
ajsf858e492004-11-16 14:25:30 +0000247static struct
paul718e3742002-12-13 20:15:29 +0000248{
249 char *name;
Chris Caputo228da422009-07-18 05:44:03 +0000250 long alloc;
paul718e3742002-12-13 20:15:29 +0000251} mstat [MTYPE_MAX];
Chris Caputo228da422009-07-18 05:44:03 +0000252#endif /* MEMORY_LOG */
paul718e3742002-12-13 20:15:29 +0000253
254/* Increment allocation counter. */
ajsf858e492004-11-16 14:25:30 +0000255static void
paul718e3742002-12-13 20:15:29 +0000256alloc_inc (int type)
257{
258 mstat[type].alloc++;
259}
260
261/* Decrement allocation counter. */
ajsf858e492004-11-16 14:25:30 +0000262static void
paul718e3742002-12-13 20:15:29 +0000263alloc_dec (int type)
264{
265 mstat[type].alloc--;
266}
David Lamparter6b0655a2014-06-04 06:53:35 +0200267
paul718e3742002-12-13 20:15:29 +0000268/* Looking up memory status from vty interface. */
269#include "vector.h"
270#include "vty.h"
271#include "command.h"
272
ajs7fa25ff2004-11-15 16:12:32 +0000273static void
274log_memstats(int pri)
275{
276 struct mlist *ml;
277
278 for (ml = mlists; ml->list; ml++)
279 {
280 struct memory_list *m;
281
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)
paul2fd2fd52005-04-15 11:47:15 +0000285 zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc);
ajs7fa25ff2004-11-15 16:12:32 +0000286 }
287}
288
Chris Caputo228da422009-07-18 05:44:03 +0000289void
290log_memstats_stderr (const char *prefix)
291{
292 struct mlist *ml;
293 struct memory_list *m;
294 int i;
295 int j = 0;
296
297 for (ml = mlists; ml->list; ml++)
298 {
299 i = 0;
300
301 for (m = ml->list; m->index >= 0; m++)
302 if (m->index && mstat[m->index].alloc)
303 {
304 if (!i)
305 fprintf (stderr,
306 "%s: memstats: Current memory utilization in module %s:\n",
307 prefix,
308 ml->name);
309 fprintf (stderr,
310 "%s: memstats: %-30s: %10ld%s\n",
311 prefix,
312 m->format,
313 mstat[m->index].alloc,
314 mstat[m->index].alloc < 0 ? " (REPORT THIS BUG!)" : "");
315 i = j = 1;
316 }
317 }
318
319 if (j)
320 fprintf (stderr,
321 "%s: memstats: NOTE: If configuration exists, utilization may be "
322 "expected.\n",
323 prefix);
324 else
325 fprintf (stderr,
326 "%s: memstats: No remaining tracked memory utilization.\n",
327 prefix);
328}
329
ajsf858e492004-11-16 14:25:30 +0000330static void
ajs24065a32005-10-20 22:28:14 +0000331show_separator(struct vty *vty)
332{
333 vty_out (vty, "-----------------------------\r\n");
334}
335
336static int
paul718e3742002-12-13 20:15:29 +0000337show_memory_vty (struct vty *vty, struct memory_list *list)
338{
339 struct memory_list *m;
ajs24065a32005-10-20 22:28:14 +0000340 int needsep = 0;
paul718e3742002-12-13 20:15:29 +0000341
342 for (m = list; m->index >= 0; m++)
343 if (m->index == 0)
ajs24065a32005-10-20 22:28:14 +0000344 {
345 if (needsep)
346 {
347 show_separator (vty);
348 needsep = 0;
349 }
350 }
351 else if (mstat[m->index].alloc)
352 {
353 vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc);
354 needsep = 1;
355 }
356 return needsep;
paul718e3742002-12-13 20:15:29 +0000357}
358
Paul Jakma41be32b2006-03-30 13:53:59 +0000359#ifdef HAVE_MALLINFO
360static int
361show_memory_mallinfo (struct vty *vty)
362{
363 struct mallinfo minfo = mallinfo();
364 char buf[MTYPE_MEMSTR_LEN];
365
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),
369 VTY_NEWLINE);
370 vty_out (vty, " Holding block headers: %s%s",
371 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.hblkhd),
372 VTY_NEWLINE);
373 vty_out (vty, " Used small blocks: %s%s",
374 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.usmblks),
375 VTY_NEWLINE);
376 vty_out (vty, " Used ordinary blocks: %s%s",
377 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.uordblks),
378 VTY_NEWLINE);
379 vty_out (vty, " Free small blocks: %s%s",
380 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fsmblks),
381 VTY_NEWLINE);
382 vty_out (vty, " Free ordinary blocks: %s%s",
383 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fordblks),
384 VTY_NEWLINE);
385 vty_out (vty, " Ordinary blocks: %ld%s",
386 (unsigned long)minfo.ordblks,
387 VTY_NEWLINE);
388 vty_out (vty, " Small blocks: %ld%s",
389 (unsigned long)minfo.smblks,
390 VTY_NEWLINE);
391 vty_out (vty, " Holding blocks: %ld%s",
392 (unsigned long)minfo.hblks,
393 VTY_NEWLINE);
394 vty_out (vty, "(see system documentation for 'mallinfo' for meaning)%s",
395 VTY_NEWLINE);
396 return 1;
397}
398#endif /* HAVE_MALLINFO */
399
Donald Sharpfbc3e972015-08-12 14:32:47 -0400400DEFUN (show_memory,
401 show_memory_cmd,
402 "show memory",
paul718e3742002-12-13 20:15:29 +0000403 "Show running system information\n"
Donald Sharpfbc3e972015-08-12 14:32:47 -0400404 "Memory statistics\n")
paul718e3742002-12-13 20:15:29 +0000405{
ajs7fa25ff2004-11-15 16:12:32 +0000406 struct mlist *ml;
ajs24065a32005-10-20 22:28:14 +0000407 int needsep = 0;
Paul Jakma41be32b2006-03-30 13:53:59 +0000408
409#ifdef HAVE_MALLINFO
410 needsep = show_memory_mallinfo (vty);
411#endif /* HAVE_MALLINFO */
412
ajs7fa25ff2004-11-15 16:12:32 +0000413 for (ml = mlists; ml->list; ml++)
414 {
ajs24065a32005-10-20 22:28:14 +0000415 if (needsep)
416 show_separator (vty);
417 needsep = show_memory_vty (vty, ml->list);
ajs7fa25ff2004-11-15 16:12:32 +0000418 }
paul718e3742002-12-13 20:15:29 +0000419
420 return CMD_SUCCESS;
421}
422
Everton Marques871dbcf2009-08-11 15:43:05 -0300423
paul718e3742002-12-13 20:15:29 +0000424void
ajsf858e492004-11-16 14:25:30 +0000425memory_init (void)
paul718e3742002-12-13 20:15:29 +0000426{
Paul Jakma62687ff2008-08-23 14:27:06 +0100427 install_element (RESTRICTED_NODE, &show_memory_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +0100428
paul718e3742002-12-13 20:15:29 +0000429 install_element (VIEW_NODE, &show_memory_cmd);
paul718e3742002-12-13 20:15:29 +0000430}
David Lamparter6b0655a2014-06-04 06:53:35 +0200431
Paul Jakma41be32b2006-03-30 13:53:59 +0000432/* 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.
439 */
440const char *
441mtype_memstr (char *buf, size_t len, unsigned long bytes)
442{
Donald Sharp929a26a2015-10-28 19:59:30 -0400443 unsigned int m, k;
444
Paul Jakma41be32b2006-03-30 13:53:59 +0000445 /* easy cases */
446 if (!bytes)
447 return "0 bytes";
448 if (bytes == 1)
449 return "1 byte";
Donald Sharp929a26a2015-10-28 19:59:30 -0400450
451 /*
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
457 */
458 if (bytes > 0x7fffffff)
459 return "> 2GB";
460
Paul Jakma41be32b2006-03-30 13:53:59 +0000461 m = bytes >> 20;
462 k = bytes >> 10;
Donald Sharp929a26a2015-10-28 19:59:30 -0400463
464 if (m > 10)
Paul Jakma41be32b2006-03-30 13:53:59 +0000465 {
466 if (bytes & (1 << 19))
467 m++;
468 snprintf (buf, len, "%d MiB", m);
469 }
470 else if (k > 10)
471 {
472 if (bytes & (1 << 9))
473 k++;
474 snprintf (buf, len, "%d KiB", k);
475 }
476 else
477 snprintf (buf, len, "%ld bytes", bytes);
478
479 return buf;
480}
481
482unsigned long
483mtype_stats_alloc (int type)
484{
485 return mstat[type].alloc;
486}