blob: f5d0cba6b2cc9b05b0bd35e9142cd5688b9a2825 [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);
paul718e3742002-12-13 20:15:29 +000035
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};
45
46/* 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
61/* Memory allocation. */
62void *
63zmalloc (int type, size_t size)
64{
65 void *memory;
66
67 memory = malloc (size);
68
69 if (memory == NULL)
70 zerror ("malloc", type, size);
71
72 alloc_inc (type);
73
74 return memory;
75}
76
77/* Memory allocation with num * size with cleared. */
78void *
79zcalloc (int type, size_t size)
80{
81 void *memory;
82
83 memory = calloc (1, size);
84
85 if (memory == NULL)
86 zerror ("calloc", type, size);
87
88 alloc_inc (type);
89
90 return memory;
91}
92
93/* Memory reallocation. */
94void *
95zrealloc (int type, void *ptr, size_t size)
96{
97 void *memory;
98
99 memory = realloc (ptr, size);
100 if (memory == NULL)
101 zerror ("realloc", type, size);
102 return memory;
103}
104
105/* Memory free. */
106void
107zfree (int type, void *ptr)
108{
109 alloc_dec (type);
110 free (ptr);
111}
112
113/* String duplication. */
114char *
hassob04c6992004-10-04 19:10:31 +0000115zstrdup (int type, const char *str)
paul718e3742002-12-13 20:15:29 +0000116{
117 void *dup;
118
119 dup = strdup (str);
120 if (dup == NULL)
121 zerror ("strdup", type, strlen (str));
122 alloc_inc (type);
123 return dup;
124}
125
126#ifdef MEMORY_LOG
ajsf858e492004-11-16 14:25:30 +0000127static struct
paul718e3742002-12-13 20:15:29 +0000128{
hassob04c6992004-10-04 19:10:31 +0000129 const char *name;
paul718e3742002-12-13 20:15:29 +0000130 unsigned long alloc;
131 unsigned long t_malloc;
132 unsigned long c_malloc;
133 unsigned long t_calloc;
134 unsigned long c_calloc;
135 unsigned long t_realloc;
136 unsigned long t_free;
137 unsigned long c_strdup;
138} mstat [MTYPE_MAX];
139
ajsf858e492004-11-16 14:25:30 +0000140static void
paul718e3742002-12-13 20:15:29 +0000141mtype_log (char *func, void *memory, const char *file, int line, int type)
142{
ajsb9e70282004-12-08 17:14:45 +0000143 zlog_debug ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line);
paul718e3742002-12-13 20:15:29 +0000144}
145
146void *
147mtype_zmalloc (const char *file, int line, int type, size_t size)
148{
149 void *memory;
150
151 mstat[type].c_malloc++;
152 mstat[type].t_malloc++;
153
154 memory = zmalloc (type, size);
155 mtype_log ("zmalloc", memory, file, line, type);
156
157 return memory;
158}
159
160void *
161mtype_zcalloc (const char *file, int line, int type, size_t size)
162{
163 void *memory;
164
165 mstat[type].c_calloc++;
166 mstat[type].t_calloc++;
167
168 memory = zcalloc (type, size);
169 mtype_log ("xcalloc", memory, file, line, type);
170
171 return memory;
172}
173
174void *
175mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size)
176{
177 void *memory;
178
179 /* Realloc need before allocated pointer. */
180 mstat[type].t_realloc++;
181
182 memory = zrealloc (type, ptr, size);
183
184 mtype_log ("xrealloc", memory, file, line, type);
185
186 return memory;
187}
188
189/* Important function. */
190void
191mtype_zfree (const char *file, int line, int type, void *ptr)
192{
193 mstat[type].t_free++;
194
195 mtype_log ("xfree", ptr, file, line, type);
196
197 zfree (type, ptr);
198}
199
200char *
hassob04c6992004-10-04 19:10:31 +0000201mtype_zstrdup (const char *file, int line, int type, const char *str)
paul718e3742002-12-13 20:15:29 +0000202{
203 char *memory;
204
205 mstat[type].c_strdup++;
206
207 memory = zstrdup (type, str);
208
209 mtype_log ("xstrdup", memory, file, line, type);
210
211 return memory;
212}
213#else
ajsf858e492004-11-16 14:25:30 +0000214static struct
paul718e3742002-12-13 20:15:29 +0000215{
216 char *name;
217 unsigned long alloc;
218} mstat [MTYPE_MAX];
219#endif /* MTPYE_LOG */
220
221/* Increment allocation counter. */
ajsf858e492004-11-16 14:25:30 +0000222static void
paul718e3742002-12-13 20:15:29 +0000223alloc_inc (int type)
224{
225 mstat[type].alloc++;
226}
227
228/* Decrement allocation counter. */
ajsf858e492004-11-16 14:25:30 +0000229static void
paul718e3742002-12-13 20:15:29 +0000230alloc_dec (int type)
231{
232 mstat[type].alloc--;
233}
234
235/* Looking up memory status from vty interface. */
236#include "vector.h"
237#include "vty.h"
238#include "command.h"
239
ajs7fa25ff2004-11-15 16:12:32 +0000240static void
241log_memstats(int pri)
242{
243 struct mlist *ml;
244
245 for (ml = mlists; ml->list; ml++)
246 {
247 struct memory_list *m;
248
249 zlog (NULL, pri, "Memory utilization in module %s:", ml->name);
250 for (m = ml->list; m->index >= 0; m++)
251 if (m->index && mstat[m->index].alloc)
paul2fd2fd52005-04-15 11:47:15 +0000252 zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc);
ajs7fa25ff2004-11-15 16:12:32 +0000253 }
254}
255
ajsf858e492004-11-16 14:25:30 +0000256static void
ajs24065a32005-10-20 22:28:14 +0000257show_separator(struct vty *vty)
258{
259 vty_out (vty, "-----------------------------\r\n");
260}
261
262static int
paul718e3742002-12-13 20:15:29 +0000263show_memory_vty (struct vty *vty, struct memory_list *list)
264{
265 struct memory_list *m;
ajs24065a32005-10-20 22:28:14 +0000266 int needsep = 0;
paul718e3742002-12-13 20:15:29 +0000267
268 for (m = list; m->index >= 0; m++)
269 if (m->index == 0)
ajs24065a32005-10-20 22:28:14 +0000270 {
271 if (needsep)
272 {
273 show_separator (vty);
274 needsep = 0;
275 }
276 }
277 else if (mstat[m->index].alloc)
278 {
279 vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc);
280 needsep = 1;
281 }
282 return needsep;
paul718e3742002-12-13 20:15:29 +0000283}
284
Paul Jakma41be32b2006-03-30 13:53:59 +0000285#ifdef HAVE_MALLINFO
286static int
287show_memory_mallinfo (struct vty *vty)
288{
289 struct mallinfo minfo = mallinfo();
290 char buf[MTYPE_MEMSTR_LEN];
291
292 vty_out (vty, "System allocator statistics:%s", VTY_NEWLINE);
293 vty_out (vty, " Total heap allocated: %s%s",
294 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.arena),
295 VTY_NEWLINE);
296 vty_out (vty, " Holding block headers: %s%s",
297 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.hblkhd),
298 VTY_NEWLINE);
299 vty_out (vty, " Used small blocks: %s%s",
300 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.usmblks),
301 VTY_NEWLINE);
302 vty_out (vty, " Used ordinary blocks: %s%s",
303 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.uordblks),
304 VTY_NEWLINE);
305 vty_out (vty, " Free small blocks: %s%s",
306 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fsmblks),
307 VTY_NEWLINE);
308 vty_out (vty, " Free ordinary blocks: %s%s",
309 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fordblks),
310 VTY_NEWLINE);
311 vty_out (vty, " Ordinary blocks: %ld%s",
312 (unsigned long)minfo.ordblks,
313 VTY_NEWLINE);
314 vty_out (vty, " Small blocks: %ld%s",
315 (unsigned long)minfo.smblks,
316 VTY_NEWLINE);
317 vty_out (vty, " Holding blocks: %ld%s",
318 (unsigned long)minfo.hblks,
319 VTY_NEWLINE);
320 vty_out (vty, "(see system documentation for 'mallinfo' for meaning)%s",
321 VTY_NEWLINE);
322 return 1;
323}
324#endif /* HAVE_MALLINFO */
325
paul718e3742002-12-13 20:15:29 +0000326DEFUN (show_memory_all,
327 show_memory_all_cmd,
328 "show memory all",
329 "Show running system information\n"
330 "Memory statistics\n"
331 "All memory statistics\n")
332{
ajs7fa25ff2004-11-15 16:12:32 +0000333 struct mlist *ml;
ajs24065a32005-10-20 22:28:14 +0000334 int needsep = 0;
Paul Jakma41be32b2006-03-30 13:53:59 +0000335
336#ifdef HAVE_MALLINFO
337 needsep = show_memory_mallinfo (vty);
338#endif /* HAVE_MALLINFO */
339
ajs7fa25ff2004-11-15 16:12:32 +0000340 for (ml = mlists; ml->list; ml++)
341 {
ajs24065a32005-10-20 22:28:14 +0000342 if (needsep)
343 show_separator (vty);
344 needsep = show_memory_vty (vty, ml->list);
ajs7fa25ff2004-11-15 16:12:32 +0000345 }
paul718e3742002-12-13 20:15:29 +0000346
347 return CMD_SUCCESS;
348}
349
350ALIAS (show_memory_all,
351 show_memory_cmd,
352 "show memory",
353 "Show running system information\n"
354 "Memory statistics\n")
355
356DEFUN (show_memory_lib,
357 show_memory_lib_cmd,
358 "show memory lib",
359 SHOW_STR
360 "Memory statistics\n"
361 "Library memory\n")
362{
363 show_memory_vty (vty, memory_list_lib);
364 return CMD_SUCCESS;
365}
366
paul2fd2fd52005-04-15 11:47:15 +0000367DEFUN (show_memory_zebra,
368 show_memory_zebra_cmd,
369 "show memory zebra",
370 SHOW_STR
371 "Memory statistics\n"
372 "Zebra memory\n")
373{
374 show_memory_vty (vty, memory_list_zebra);
375 return CMD_SUCCESS;
376}
377
paul718e3742002-12-13 20:15:29 +0000378DEFUN (show_memory_rip,
379 show_memory_rip_cmd,
380 "show memory rip",
381 SHOW_STR
382 "Memory statistics\n"
383 "RIP memory\n")
384{
385 show_memory_vty (vty, memory_list_rip);
386 return CMD_SUCCESS;
387}
388
hassoa94434b2003-05-25 17:10:12 +0000389DEFUN (show_memory_ripng,
390 show_memory_ripng_cmd,
391 "show memory ripng",
392 SHOW_STR
393 "Memory statistics\n"
394 "RIPng memory\n")
395{
396 show_memory_vty (vty, memory_list_ripng);
397 return CMD_SUCCESS;
398}
399
paul718e3742002-12-13 20:15:29 +0000400DEFUN (show_memory_bgp,
401 show_memory_bgp_cmd,
402 "show memory bgp",
403 SHOW_STR
404 "Memory statistics\n"
405 "BGP memory\n")
406{
407 show_memory_vty (vty, memory_list_bgp);
408 return CMD_SUCCESS;
409}
410
411DEFUN (show_memory_ospf,
412 show_memory_ospf_cmd,
413 "show memory ospf",
414 SHOW_STR
415 "Memory statistics\n"
416 "OSPF memory\n")
417{
418 show_memory_vty (vty, memory_list_ospf);
419 return CMD_SUCCESS;
420}
421
422DEFUN (show_memory_ospf6,
423 show_memory_ospf6_cmd,
424 "show memory ospf6",
425 SHOW_STR
426 "Memory statistics\n"
427 "OSPF6 memory\n")
428{
429 show_memory_vty (vty, memory_list_ospf6);
430 return CMD_SUCCESS;
431}
432
jardin9e867fe2003-12-23 08:56:18 +0000433DEFUN (show_memory_isis,
434 show_memory_isis_cmd,
435 "show memory isis",
436 SHOW_STR
437 "Memory statistics\n"
438 "ISIS memory\n")
439{
440 show_memory_vty (vty, memory_list_isis);
441 return CMD_SUCCESS;
442}
443
paul718e3742002-12-13 20:15:29 +0000444void
ajsf858e492004-11-16 14:25:30 +0000445memory_init (void)
paul718e3742002-12-13 20:15:29 +0000446{
Paul Jakma62687ff2008-08-23 14:27:06 +0100447 install_element (RESTRICTED_NODE, &show_memory_cmd);
448 install_element (RESTRICTED_NODE, &show_memory_all_cmd);
449 install_element (RESTRICTED_NODE, &show_memory_lib_cmd);
450 install_element (RESTRICTED_NODE, &show_memory_rip_cmd);
451 install_element (RESTRICTED_NODE, &show_memory_ripng_cmd);
452 install_element (RESTRICTED_NODE, &show_memory_bgp_cmd);
453 install_element (RESTRICTED_NODE, &show_memory_ospf_cmd);
454 install_element (RESTRICTED_NODE, &show_memory_ospf6_cmd);
455 install_element (RESTRICTED_NODE, &show_memory_isis_cmd);
456
paul718e3742002-12-13 20:15:29 +0000457 install_element (VIEW_NODE, &show_memory_cmd);
458 install_element (VIEW_NODE, &show_memory_all_cmd);
459 install_element (VIEW_NODE, &show_memory_lib_cmd);
460 install_element (VIEW_NODE, &show_memory_rip_cmd);
hassoa94434b2003-05-25 17:10:12 +0000461 install_element (VIEW_NODE, &show_memory_ripng_cmd);
paul718e3742002-12-13 20:15:29 +0000462 install_element (VIEW_NODE, &show_memory_bgp_cmd);
463 install_element (VIEW_NODE, &show_memory_ospf_cmd);
464 install_element (VIEW_NODE, &show_memory_ospf6_cmd);
jardin9e867fe2003-12-23 08:56:18 +0000465 install_element (VIEW_NODE, &show_memory_isis_cmd);
paul718e3742002-12-13 20:15:29 +0000466
467 install_element (ENABLE_NODE, &show_memory_cmd);
468 install_element (ENABLE_NODE, &show_memory_all_cmd);
469 install_element (ENABLE_NODE, &show_memory_lib_cmd);
paul2fd2fd52005-04-15 11:47:15 +0000470 install_element (ENABLE_NODE, &show_memory_zebra_cmd);
paul718e3742002-12-13 20:15:29 +0000471 install_element (ENABLE_NODE, &show_memory_rip_cmd);
hassoa94434b2003-05-25 17:10:12 +0000472 install_element (ENABLE_NODE, &show_memory_ripng_cmd);
paul718e3742002-12-13 20:15:29 +0000473 install_element (ENABLE_NODE, &show_memory_bgp_cmd);
474 install_element (ENABLE_NODE, &show_memory_ospf_cmd);
475 install_element (ENABLE_NODE, &show_memory_ospf6_cmd);
jardin9e867fe2003-12-23 08:56:18 +0000476 install_element (ENABLE_NODE, &show_memory_isis_cmd);
paul718e3742002-12-13 20:15:29 +0000477}
Paul Jakma41be32b2006-03-30 13:53:59 +0000478
479/* Stats querying from users */
480/* Return a pointer to a human friendly string describing
481 * the byte count passed in. E.g:
482 * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc.
483 * Up to 4 significant figures will be given.
484 * The pointer returned may be NULL (indicating an error)
485 * or point to the given buffer, or point to static storage.
486 */
487const char *
488mtype_memstr (char *buf, size_t len, unsigned long bytes)
489{
490 unsigned int t, g, m, k;
491
492 /* easy cases */
493 if (!bytes)
494 return "0 bytes";
495 if (bytes == 1)
496 return "1 byte";
497
498 if (sizeof (unsigned long) >= 8)
499 /* Hacked to make it not warn on ILP32 machines
500 * Shift will always be 40 at runtime. See below too */
501 t = bytes >> (sizeof (unsigned long) >= 8 ? 40 : 0);
502 else
503 t = 0;
504 g = bytes >> 30;
505 m = bytes >> 20;
506 k = bytes >> 10;
507
508 if (t > 10)
509 {
510 /* The shift will always be 39 at runtime.
511 * Just hacked to make it not warn on 'smaller' machines.
512 * Static compiler analysis should mean no extra code
513 */
Paul Jakma912df1e2008-01-08 13:50:11 +0000514 if (bytes & (1UL << (sizeof (unsigned long) >= 8 ? 39 : 0)))
Paul Jakma41be32b2006-03-30 13:53:59 +0000515 t++;
516 snprintf (buf, len, "%4d TiB", t);
517 }
518 else if (g > 10)
519 {
520 if (bytes & (1 << 29))
521 g++;
522 snprintf (buf, len, "%d GiB", g);
523 }
524 else if (m > 10)
525 {
526 if (bytes & (1 << 19))
527 m++;
528 snprintf (buf, len, "%d MiB", m);
529 }
530 else if (k > 10)
531 {
532 if (bytes & (1 << 9))
533 k++;
534 snprintf (buf, len, "%d KiB", k);
535 }
536 else
537 snprintf (buf, len, "%ld bytes", bytes);
538
539 return buf;
540}
541
542unsigned long
543mtype_stats_alloc (int type)
544{
545 return mstat[type].alloc;
546}