blob: 172ddfc49c559d272b9888f7a36a2afbe98c7308 [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
111 memory = realloc (ptr, size);
112 if (memory == NULL)
113 zerror ("realloc", type, size);
Chris Hallcca85d22010-07-23 11:27:11 -0700114 if (ptr == NULL)
115 alloc_inc (type);
116
paul718e3742002-12-13 20:15:29 +0000117 return memory;
118}
119
Greg Troxelb1679382010-09-17 12:19:13 -0400120/*
121 * Free memory allocated by z*alloc or zstrdup.
122 * Requires: ptr was returned by zmalloc, zcalloc, or zrealloc with the
123 * same type.
124 * Effects: The memory is freed and may no longer be referenced.
125 */
paul718e3742002-12-13 20:15:29 +0000126void
127zfree (int type, void *ptr)
128{
Chris Hallcca85d22010-07-23 11:27:11 -0700129 if (ptr != NULL)
130 {
131 alloc_dec (type);
132 free (ptr);
133 }
paul718e3742002-12-13 20:15:29 +0000134}
135
Greg Troxelb1679382010-09-17 12:19:13 -0400136/*
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
140 * succeed or abort.
141 */
paul718e3742002-12-13 20:15:29 +0000142char *
hassob04c6992004-10-04 19:10:31 +0000143zstrdup (int type, const char *str)
paul718e3742002-12-13 20:15:29 +0000144{
145 void *dup;
146
147 dup = strdup (str);
148 if (dup == NULL)
149 zerror ("strdup", type, strlen (str));
150 alloc_inc (type);
151 return dup;
152}
David Lamparter6b0655a2014-06-04 06:53:35 +0200153
paul718e3742002-12-13 20:15:29 +0000154#ifdef MEMORY_LOG
ajsf858e492004-11-16 14:25:30 +0000155static struct
paul718e3742002-12-13 20:15:29 +0000156{
hassob04c6992004-10-04 19:10:31 +0000157 const char *name;
Chris Caputo228da422009-07-18 05:44:03 +0000158 long alloc;
paul718e3742002-12-13 20:15:29 +0000159 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;
166} mstat [MTYPE_MAX];
167
ajsf858e492004-11-16 14:25:30 +0000168static void
paul718e3742002-12-13 20:15:29 +0000169mtype_log (char *func, void *memory, const char *file, int line, int type)
170{
ajsb9e70282004-12-08 17:14:45 +0000171 zlog_debug ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line);
paul718e3742002-12-13 20:15:29 +0000172}
173
174void *
175mtype_zmalloc (const char *file, int line, int type, size_t size)
176{
177 void *memory;
178
179 mstat[type].c_malloc++;
180 mstat[type].t_malloc++;
181
182 memory = zmalloc (type, size);
183 mtype_log ("zmalloc", memory, file, line, type);
184
185 return memory;
186}
187
188void *
189mtype_zcalloc (const char *file, int line, int type, size_t size)
190{
191 void *memory;
192
193 mstat[type].c_calloc++;
194 mstat[type].t_calloc++;
195
196 memory = zcalloc (type, size);
197 mtype_log ("xcalloc", memory, file, line, type);
198
199 return memory;
200}
201
202void *
203mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size)
204{
205 void *memory;
206
207 /* Realloc need before allocated pointer. */
208 mstat[type].t_realloc++;
209
210 memory = zrealloc (type, ptr, size);
211
212 mtype_log ("xrealloc", memory, file, line, type);
213
214 return memory;
215}
216
217/* Important function. */
218void
219mtype_zfree (const char *file, int line, int type, void *ptr)
220{
221 mstat[type].t_free++;
222
223 mtype_log ("xfree", ptr, file, line, type);
224
225 zfree (type, ptr);
226}
227
228char *
hassob04c6992004-10-04 19:10:31 +0000229mtype_zstrdup (const char *file, int line, int type, const char *str)
paul718e3742002-12-13 20:15:29 +0000230{
231 char *memory;
232
233 mstat[type].c_strdup++;
234
235 memory = zstrdup (type, str);
236
237 mtype_log ("xstrdup", memory, file, line, type);
238
239 return memory;
240}
241#else
ajsf858e492004-11-16 14:25:30 +0000242static struct
paul718e3742002-12-13 20:15:29 +0000243{
244 char *name;
Chris Caputo228da422009-07-18 05:44:03 +0000245 long alloc;
paul718e3742002-12-13 20:15:29 +0000246} mstat [MTYPE_MAX];
Chris Caputo228da422009-07-18 05:44:03 +0000247#endif /* MEMORY_LOG */
paul718e3742002-12-13 20:15:29 +0000248
249/* Increment allocation counter. */
ajsf858e492004-11-16 14:25:30 +0000250static void
paul718e3742002-12-13 20:15:29 +0000251alloc_inc (int type)
252{
253 mstat[type].alloc++;
254}
255
256/* Decrement allocation counter. */
ajsf858e492004-11-16 14:25:30 +0000257static void
paul718e3742002-12-13 20:15:29 +0000258alloc_dec (int type)
259{
260 mstat[type].alloc--;
261}
David Lamparter6b0655a2014-06-04 06:53:35 +0200262
paul718e3742002-12-13 20:15:29 +0000263/* Looking up memory status from vty interface. */
264#include "vector.h"
265#include "vty.h"
266#include "command.h"
267
ajs7fa25ff2004-11-15 16:12:32 +0000268static void
269log_memstats(int pri)
270{
271 struct mlist *ml;
272
273 for (ml = mlists; ml->list; ml++)
274 {
275 struct memory_list *m;
276
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)
paul2fd2fd52005-04-15 11:47:15 +0000280 zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc);
ajs7fa25ff2004-11-15 16:12:32 +0000281 }
282}
283
Chris Caputo228da422009-07-18 05:44:03 +0000284void
285log_memstats_stderr (const char *prefix)
286{
287 struct mlist *ml;
288 struct memory_list *m;
289 int i;
290 int j = 0;
291
292 for (ml = mlists; ml->list; ml++)
293 {
294 i = 0;
295
296 for (m = ml->list; m->index >= 0; m++)
297 if (m->index && mstat[m->index].alloc)
298 {
299 if (!i)
300 fprintf (stderr,
301 "%s: memstats: Current memory utilization in module %s:\n",
302 prefix,
303 ml->name);
304 fprintf (stderr,
305 "%s: memstats: %-30s: %10ld%s\n",
306 prefix,
307 m->format,
308 mstat[m->index].alloc,
309 mstat[m->index].alloc < 0 ? " (REPORT THIS BUG!)" : "");
310 i = j = 1;
311 }
312 }
313
314 if (j)
315 fprintf (stderr,
316 "%s: memstats: NOTE: If configuration exists, utilization may be "
317 "expected.\n",
318 prefix);
319 else
320 fprintf (stderr,
321 "%s: memstats: No remaining tracked memory utilization.\n",
322 prefix);
323}
324
ajsf858e492004-11-16 14:25:30 +0000325static void
ajs24065a32005-10-20 22:28:14 +0000326show_separator(struct vty *vty)
327{
328 vty_out (vty, "-----------------------------\r\n");
329}
330
331static int
paul718e3742002-12-13 20:15:29 +0000332show_memory_vty (struct vty *vty, struct memory_list *list)
333{
334 struct memory_list *m;
ajs24065a32005-10-20 22:28:14 +0000335 int needsep = 0;
paul718e3742002-12-13 20:15:29 +0000336
337 for (m = list; m->index >= 0; m++)
338 if (m->index == 0)
ajs24065a32005-10-20 22:28:14 +0000339 {
340 if (needsep)
341 {
342 show_separator (vty);
343 needsep = 0;
344 }
345 }
346 else if (mstat[m->index].alloc)
347 {
348 vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc);
349 needsep = 1;
350 }
351 return needsep;
paul718e3742002-12-13 20:15:29 +0000352}
353
Paul Jakma41be32b2006-03-30 13:53:59 +0000354#ifdef HAVE_MALLINFO
355static int
356show_memory_mallinfo (struct vty *vty)
357{
358 struct mallinfo minfo = mallinfo();
359 char buf[MTYPE_MEMSTR_LEN];
360
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),
364 VTY_NEWLINE);
365 vty_out (vty, " Holding block headers: %s%s",
366 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.hblkhd),
367 VTY_NEWLINE);
368 vty_out (vty, " Used small blocks: %s%s",
369 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.usmblks),
370 VTY_NEWLINE);
371 vty_out (vty, " Used ordinary blocks: %s%s",
372 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.uordblks),
373 VTY_NEWLINE);
374 vty_out (vty, " Free small blocks: %s%s",
375 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fsmblks),
376 VTY_NEWLINE);
377 vty_out (vty, " Free ordinary blocks: %s%s",
378 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fordblks),
379 VTY_NEWLINE);
380 vty_out (vty, " Ordinary blocks: %ld%s",
381 (unsigned long)minfo.ordblks,
382 VTY_NEWLINE);
383 vty_out (vty, " Small blocks: %ld%s",
384 (unsigned long)minfo.smblks,
385 VTY_NEWLINE);
386 vty_out (vty, " Holding blocks: %ld%s",
387 (unsigned long)minfo.hblks,
388 VTY_NEWLINE);
389 vty_out (vty, "(see system documentation for 'mallinfo' for meaning)%s",
390 VTY_NEWLINE);
391 return 1;
392}
393#endif /* HAVE_MALLINFO */
394
Donald Sharpfbc3e972015-08-12 14:32:47 -0400395DEFUN (show_memory,
396 show_memory_cmd,
397 "show memory",
paul718e3742002-12-13 20:15:29 +0000398 "Show running system information\n"
Donald Sharpfbc3e972015-08-12 14:32:47 -0400399 "Memory statistics\n")
paul718e3742002-12-13 20:15:29 +0000400{
ajs7fa25ff2004-11-15 16:12:32 +0000401 struct mlist *ml;
ajs24065a32005-10-20 22:28:14 +0000402 int needsep = 0;
Paul Jakma41be32b2006-03-30 13:53:59 +0000403
404#ifdef HAVE_MALLINFO
405 needsep = show_memory_mallinfo (vty);
406#endif /* HAVE_MALLINFO */
407
ajs7fa25ff2004-11-15 16:12:32 +0000408 for (ml = mlists; ml->list; ml++)
409 {
ajs24065a32005-10-20 22:28:14 +0000410 if (needsep)
411 show_separator (vty);
412 needsep = show_memory_vty (vty, ml->list);
ajs7fa25ff2004-11-15 16:12:32 +0000413 }
paul718e3742002-12-13 20:15:29 +0000414
415 return CMD_SUCCESS;
416}
417
Everton Marques871dbcf2009-08-11 15:43:05 -0300418
paul718e3742002-12-13 20:15:29 +0000419void
ajsf858e492004-11-16 14:25:30 +0000420memory_init (void)
paul718e3742002-12-13 20:15:29 +0000421{
Paul Jakma62687ff2008-08-23 14:27:06 +0100422 install_element (RESTRICTED_NODE, &show_memory_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +0100423
paul718e3742002-12-13 20:15:29 +0000424 install_element (VIEW_NODE, &show_memory_cmd);
paul718e3742002-12-13 20:15:29 +0000425
426 install_element (ENABLE_NODE, &show_memory_cmd);
paul718e3742002-12-13 20:15:29 +0000427}
David Lamparter6b0655a2014-06-04 06:53:35 +0200428
Paul Jakma41be32b2006-03-30 13:53:59 +0000429/* Stats querying from users */
430/* Return a pointer to a human friendly string describing
431 * the byte count passed in. E.g:
432 * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc.
433 * Up to 4 significant figures will be given.
434 * The pointer returned may be NULL (indicating an error)
435 * or point to the given buffer, or point to static storage.
436 */
437const char *
438mtype_memstr (char *buf, size_t len, unsigned long bytes)
439{
Donald Sharp929a26a2015-10-28 19:59:30 -0400440 unsigned int m, k;
441
Paul Jakma41be32b2006-03-30 13:53:59 +0000442 /* easy cases */
443 if (!bytes)
444 return "0 bytes";
445 if (bytes == 1)
446 return "1 byte";
Donald Sharp929a26a2015-10-28 19:59:30 -0400447
448 /*
449 * When we pass the 2gb barrier mallinfo() can no longer report
450 * correct data so it just does something odd...
451 * Reporting like Terrabytes of data. Which makes users...
452 * edgy.. yes edgy that's the term for it.
453 * So let's just give up gracefully
454 */
455 if (bytes > 0x7fffffff)
456 return "> 2GB";
457
Paul Jakma41be32b2006-03-30 13:53:59 +0000458 m = bytes >> 20;
459 k = bytes >> 10;
Donald Sharp929a26a2015-10-28 19:59:30 -0400460
461 if (m > 10)
Paul Jakma41be32b2006-03-30 13:53:59 +0000462 {
463 if (bytes & (1 << 19))
464 m++;
465 snprintf (buf, len, "%d MiB", m);
466 }
467 else if (k > 10)
468 {
469 if (bytes & (1 << 9))
470 k++;
471 snprintf (buf, len, "%d KiB", k);
472 }
473 else
474 snprintf (buf, len, "%ld bytes", bytes);
475
476 return buf;
477}
478
479unsigned long
480mtype_stats_alloc (int type)
481{
482 return mstat[type].alloc;
483}