]> git.proxmox.com Git - mirror_frr.git/blob - lib/memory.c
[bgpd] Stability fixes including bugs 397, 492
[mirror_frr.git] / lib / memory.c
1 /*
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>
24 /* malloc.h is generally obsolete, however GNU Libc mallinfo wants it. */
25 #if !defined(HAVE_STDLIB_H) || (defined(GNU_LINUX) && defined(HAVE_MALLINFO))
26 #include <malloc.h>
27 #endif /* !HAVE_STDLIB_H || HAVE_MALLINFO */
28
29 #include "log.h"
30 #include "memory.h"
31
32 static void alloc_inc (int);
33 static void alloc_dec (int);
34 static void log_memstats(int log_priority);
35 \f
36 static const struct message mstr [] =
37 {
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 \f
46 /* Fatal memory allocation error occured. */
47 static void __attribute__ ((noreturn))
48 zerror (const char *fname, int type, size_t size)
49 {
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
56 this time... */
57 zlog_backtrace(LOG_WARNING);
58 abort();
59 }
60
61 /* Memory allocation. */
62 void *
63 zmalloc (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. */
78 void *
79 zcalloc (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. */
94 void *
95 zrealloc (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. */
106 void
107 zfree (int type, void *ptr)
108 {
109 alloc_dec (type);
110 free (ptr);
111 }
112
113 /* String duplication. */
114 char *
115 zstrdup (int type, const char *str)
116 {
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 \f
126 #ifdef MEMORY_LOG
127 static struct
128 {
129 const char *name;
130 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
140 static void
141 mtype_log (char *func, void *memory, const char *file, int line, int type)
142 {
143 zlog_debug ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line);
144 }
145
146 void *
147 mtype_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
160 void *
161 mtype_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
174 void *
175 mtype_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. */
190 void
191 mtype_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
200 char *
201 mtype_zstrdup (const char *file, int line, int type, const char *str)
202 {
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
214 static struct
215 {
216 char *name;
217 long alloc;
218 } mstat [MTYPE_MAX];
219 #endif /* MEMORY_LOG */
220
221 /* Increment allocation counter. */
222 static void
223 alloc_inc (int type)
224 {
225 mstat[type].alloc++;
226 }
227
228 /* Decrement allocation counter. */
229 static void
230 alloc_dec (int type)
231 {
232 mstat[type].alloc--;
233 }
234 \f
235 /* Looking up memory status from vty interface. */
236 #include "vector.h"
237 #include "vty.h"
238 #include "command.h"
239
240 static void
241 log_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)
252 zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc);
253 }
254 }
255
256 void
257 log_memstats_stderr (const char *prefix)
258 {
259 struct mlist *ml;
260 struct memory_list *m;
261 int i;
262 int j = 0;
263
264 for (ml = mlists; ml->list; ml++)
265 {
266 i = 0;
267
268 for (m = ml->list; m->index >= 0; m++)
269 if (m->index && mstat[m->index].alloc)
270 {
271 if (!i)
272 fprintf (stderr,
273 "%s: memstats: Current memory utilization in module %s:\n",
274 prefix,
275 ml->name);
276 fprintf (stderr,
277 "%s: memstats: %-30s: %10ld%s\n",
278 prefix,
279 m->format,
280 mstat[m->index].alloc,
281 mstat[m->index].alloc < 0 ? " (REPORT THIS BUG!)" : "");
282 i = j = 1;
283 }
284 }
285
286 if (j)
287 fprintf (stderr,
288 "%s: memstats: NOTE: If configuration exists, utilization may be "
289 "expected.\n",
290 prefix);
291 else
292 fprintf (stderr,
293 "%s: memstats: No remaining tracked memory utilization.\n",
294 prefix);
295 }
296
297 static void
298 show_separator(struct vty *vty)
299 {
300 vty_out (vty, "-----------------------------\r\n");
301 }
302
303 static int
304 show_memory_vty (struct vty *vty, struct memory_list *list)
305 {
306 struct memory_list *m;
307 int needsep = 0;
308
309 for (m = list; m->index >= 0; m++)
310 if (m->index == 0)
311 {
312 if (needsep)
313 {
314 show_separator (vty);
315 needsep = 0;
316 }
317 }
318 else if (mstat[m->index].alloc)
319 {
320 vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc);
321 needsep = 1;
322 }
323 return needsep;
324 }
325
326 #ifdef HAVE_MALLINFO
327 static int
328 show_memory_mallinfo (struct vty *vty)
329 {
330 struct mallinfo minfo = mallinfo();
331 char buf[MTYPE_MEMSTR_LEN];
332
333 vty_out (vty, "System allocator statistics:%s", VTY_NEWLINE);
334 vty_out (vty, " Total heap allocated: %s%s",
335 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.arena),
336 VTY_NEWLINE);
337 vty_out (vty, " Holding block headers: %s%s",
338 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.hblkhd),
339 VTY_NEWLINE);
340 vty_out (vty, " Used small blocks: %s%s",
341 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.usmblks),
342 VTY_NEWLINE);
343 vty_out (vty, " Used ordinary blocks: %s%s",
344 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.uordblks),
345 VTY_NEWLINE);
346 vty_out (vty, " Free small blocks: %s%s",
347 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fsmblks),
348 VTY_NEWLINE);
349 vty_out (vty, " Free ordinary blocks: %s%s",
350 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fordblks),
351 VTY_NEWLINE);
352 vty_out (vty, " Ordinary blocks: %ld%s",
353 (unsigned long)minfo.ordblks,
354 VTY_NEWLINE);
355 vty_out (vty, " Small blocks: %ld%s",
356 (unsigned long)minfo.smblks,
357 VTY_NEWLINE);
358 vty_out (vty, " Holding blocks: %ld%s",
359 (unsigned long)minfo.hblks,
360 VTY_NEWLINE);
361 vty_out (vty, "(see system documentation for 'mallinfo' for meaning)%s",
362 VTY_NEWLINE);
363 return 1;
364 }
365 #endif /* HAVE_MALLINFO */
366
367 DEFUN (show_memory_all,
368 show_memory_all_cmd,
369 "show memory all",
370 "Show running system information\n"
371 "Memory statistics\n"
372 "All memory statistics\n")
373 {
374 struct mlist *ml;
375 int needsep = 0;
376
377 #ifdef HAVE_MALLINFO
378 needsep = show_memory_mallinfo (vty);
379 #endif /* HAVE_MALLINFO */
380
381 for (ml = mlists; ml->list; ml++)
382 {
383 if (needsep)
384 show_separator (vty);
385 needsep = show_memory_vty (vty, ml->list);
386 }
387
388 return CMD_SUCCESS;
389 }
390
391 ALIAS (show_memory_all,
392 show_memory_cmd,
393 "show memory",
394 "Show running system information\n"
395 "Memory statistics\n")
396
397 DEFUN (show_memory_lib,
398 show_memory_lib_cmd,
399 "show memory lib",
400 SHOW_STR
401 "Memory statistics\n"
402 "Library memory\n")
403 {
404 show_memory_vty (vty, memory_list_lib);
405 return CMD_SUCCESS;
406 }
407
408 DEFUN (show_memory_zebra,
409 show_memory_zebra_cmd,
410 "show memory zebra",
411 SHOW_STR
412 "Memory statistics\n"
413 "Zebra memory\n")
414 {
415 show_memory_vty (vty, memory_list_zebra);
416 return CMD_SUCCESS;
417 }
418
419 DEFUN (show_memory_rip,
420 show_memory_rip_cmd,
421 "show memory rip",
422 SHOW_STR
423 "Memory statistics\n"
424 "RIP memory\n")
425 {
426 show_memory_vty (vty, memory_list_rip);
427 return CMD_SUCCESS;
428 }
429
430 DEFUN (show_memory_ripng,
431 show_memory_ripng_cmd,
432 "show memory ripng",
433 SHOW_STR
434 "Memory statistics\n"
435 "RIPng memory\n")
436 {
437 show_memory_vty (vty, memory_list_ripng);
438 return CMD_SUCCESS;
439 }
440
441 DEFUN (show_memory_bgp,
442 show_memory_bgp_cmd,
443 "show memory bgp",
444 SHOW_STR
445 "Memory statistics\n"
446 "BGP memory\n")
447 {
448 show_memory_vty (vty, memory_list_bgp);
449 return CMD_SUCCESS;
450 }
451
452 DEFUN (show_memory_ospf,
453 show_memory_ospf_cmd,
454 "show memory ospf",
455 SHOW_STR
456 "Memory statistics\n"
457 "OSPF memory\n")
458 {
459 show_memory_vty (vty, memory_list_ospf);
460 return CMD_SUCCESS;
461 }
462
463 DEFUN (show_memory_ospf6,
464 show_memory_ospf6_cmd,
465 "show memory ospf6",
466 SHOW_STR
467 "Memory statistics\n"
468 "OSPF6 memory\n")
469 {
470 show_memory_vty (vty, memory_list_ospf6);
471 return CMD_SUCCESS;
472 }
473
474 DEFUN (show_memory_isis,
475 show_memory_isis_cmd,
476 "show memory isis",
477 SHOW_STR
478 "Memory statistics\n"
479 "ISIS memory\n")
480 {
481 show_memory_vty (vty, memory_list_isis);
482 return CMD_SUCCESS;
483 }
484
485 void
486 memory_init (void)
487 {
488 install_element (RESTRICTED_NODE, &show_memory_cmd);
489 install_element (RESTRICTED_NODE, &show_memory_all_cmd);
490 install_element (RESTRICTED_NODE, &show_memory_lib_cmd);
491 install_element (RESTRICTED_NODE, &show_memory_rip_cmd);
492 install_element (RESTRICTED_NODE, &show_memory_ripng_cmd);
493 install_element (RESTRICTED_NODE, &show_memory_bgp_cmd);
494 install_element (RESTRICTED_NODE, &show_memory_ospf_cmd);
495 install_element (RESTRICTED_NODE, &show_memory_ospf6_cmd);
496 install_element (RESTRICTED_NODE, &show_memory_isis_cmd);
497
498 install_element (VIEW_NODE, &show_memory_cmd);
499 install_element (VIEW_NODE, &show_memory_all_cmd);
500 install_element (VIEW_NODE, &show_memory_lib_cmd);
501 install_element (VIEW_NODE, &show_memory_rip_cmd);
502 install_element (VIEW_NODE, &show_memory_ripng_cmd);
503 install_element (VIEW_NODE, &show_memory_bgp_cmd);
504 install_element (VIEW_NODE, &show_memory_ospf_cmd);
505 install_element (VIEW_NODE, &show_memory_ospf6_cmd);
506 install_element (VIEW_NODE, &show_memory_isis_cmd);
507
508 install_element (ENABLE_NODE, &show_memory_cmd);
509 install_element (ENABLE_NODE, &show_memory_all_cmd);
510 install_element (ENABLE_NODE, &show_memory_lib_cmd);
511 install_element (ENABLE_NODE, &show_memory_zebra_cmd);
512 install_element (ENABLE_NODE, &show_memory_rip_cmd);
513 install_element (ENABLE_NODE, &show_memory_ripng_cmd);
514 install_element (ENABLE_NODE, &show_memory_bgp_cmd);
515 install_element (ENABLE_NODE, &show_memory_ospf_cmd);
516 install_element (ENABLE_NODE, &show_memory_ospf6_cmd);
517 install_element (ENABLE_NODE, &show_memory_isis_cmd);
518 }
519 \f
520 /* Stats querying from users */
521 /* Return a pointer to a human friendly string describing
522 * the byte count passed in. E.g:
523 * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc.
524 * Up to 4 significant figures will be given.
525 * The pointer returned may be NULL (indicating an error)
526 * or point to the given buffer, or point to static storage.
527 */
528 const char *
529 mtype_memstr (char *buf, size_t len, unsigned long bytes)
530 {
531 unsigned int t, g, m, k;
532
533 /* easy cases */
534 if (!bytes)
535 return "0 bytes";
536 if (bytes == 1)
537 return "1 byte";
538
539 if (sizeof (unsigned long) >= 8)
540 /* Hacked to make it not warn on ILP32 machines
541 * Shift will always be 40 at runtime. See below too */
542 t = bytes >> (sizeof (unsigned long) >= 8 ? 40 : 0);
543 else
544 t = 0;
545 g = bytes >> 30;
546 m = bytes >> 20;
547 k = bytes >> 10;
548
549 if (t > 10)
550 {
551 /* The shift will always be 39 at runtime.
552 * Just hacked to make it not warn on 'smaller' machines.
553 * Static compiler analysis should mean no extra code
554 */
555 if (bytes & (1UL << (sizeof (unsigned long) >= 8 ? 39 : 0)))
556 t++;
557 snprintf (buf, len, "%4d TiB", t);
558 }
559 else if (g > 10)
560 {
561 if (bytes & (1 << 29))
562 g++;
563 snprintf (buf, len, "%d GiB", g);
564 }
565 else if (m > 10)
566 {
567 if (bytes & (1 << 19))
568 m++;
569 snprintf (buf, len, "%d MiB", m);
570 }
571 else if (k > 10)
572 {
573 if (bytes & (1 << 9))
574 k++;
575 snprintf (buf, len, "%d KiB", k);
576 }
577 else
578 snprintf (buf, len, "%ld bytes", bytes);
579
580 return buf;
581 }
582
583 unsigned long
584 mtype_stats_alloc (int type)
585 {
586 return mstat[type].alloc;
587 }