paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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> |
Paul Jakma | d09552d | 2006-05-28 08:15:46 +0000 | [diff] [blame] | 24 | /* malloc.h is generally obsolete, however GNU Libc mallinfo wants it. */ |
Paul Jakma | 74176d2 | 2006-06-30 16:49:02 +0000 | [diff] [blame] | 25 | #if !defined(HAVE_STDLIB_H) || (defined(GNU_LINUX) && defined(HAVE_MALLINFO)) |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 26 | #include <malloc.h> |
Paul Jakma | d09552d | 2006-05-28 08:15:46 +0000 | [diff] [blame] | 27 | #endif /* !HAVE_STDLIB_H || HAVE_MALLINFO */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 28 | |
| 29 | #include "log.h" |
| 30 | #include "memory.h" |
| 31 | |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 32 | static void alloc_inc (int); |
| 33 | static void alloc_dec (int); |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 34 | static void log_memstats(int log_priority); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | |
Stephen Hemminger | 1423c80 | 2008-08-14 17:59:25 +0100 | [diff] [blame] | 36 | static const struct message mstr [] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
| 46 | /* Fatal memory allocation error occured. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 47 | static void __attribute__ ((noreturn)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 48 | zerror (const char *fname, int type, size_t size) |
| 49 | { |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 50 | zlog_err ("%s : can't allocate memory for `%s' size %d: %s\n", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 51 | fname, lookup (mstr, type), (int) size, safe_strerror(errno)); |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 52 | log_memstats(LOG_WARNING); |
ajs | 48d6c69 | 2004-11-26 20:52:59 +0000 | [diff] [blame] | 53 | /* N.B. It might be preferable to call zlog_backtrace_sigsafe here, since |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 54 | that function should definitely be safe in an OOM condition. But |
ajs | 48d6c69 | 2004-11-26 20:52:59 +0000 | [diff] [blame] | 55 | unfortunately zlog_backtrace_sigsafe does not support syslog logging at |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 56 | this time... */ |
| 57 | zlog_backtrace(LOG_WARNING); |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 58 | abort(); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Greg Troxel | b167938 | 2010-09-17 12:19:13 -0400 | [diff] [blame] | 61 | /* |
| 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 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 66 | void * |
| 67 | zmalloc (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 Troxel | b167938 | 2010-09-17 12:19:13 -0400 | [diff] [blame] | 81 | /* |
| 82 | * Allocate memory as in zmalloc, and also clear the memory. |
| 83 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 84 | void * |
| 85 | zcalloc (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 Troxel | b167938 | 2010-09-17 12:19:13 -0400 | [diff] [blame] | 99 | /* |
| 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 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 106 | void * |
| 107 | zrealloc (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); |
| 114 | return memory; |
| 115 | } |
| 116 | |
Greg Troxel | b167938 | 2010-09-17 12:19:13 -0400 | [diff] [blame] | 117 | /* |
| 118 | * Free memory allocated by z*alloc or zstrdup. |
| 119 | * Requires: ptr was returned by zmalloc, zcalloc, or zrealloc with the |
| 120 | * same type. |
| 121 | * Effects: The memory is freed and may no longer be referenced. |
| 122 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 123 | void |
| 124 | zfree (int type, void *ptr) |
| 125 | { |
| 126 | alloc_dec (type); |
| 127 | free (ptr); |
| 128 | } |
| 129 | |
Greg Troxel | b167938 | 2010-09-17 12:19:13 -0400 | [diff] [blame] | 130 | /* |
| 131 | * Duplicate a string, counting memory usage by type. |
| 132 | * Effects: The string is duplicated, and the return value must |
| 133 | * eventually be passed to zfree with the same type. The function will |
| 134 | * succeed or abort. |
| 135 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 136 | char * |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 137 | zstrdup (int type, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 138 | { |
| 139 | void *dup; |
| 140 | |
| 141 | dup = strdup (str); |
| 142 | if (dup == NULL) |
| 143 | zerror ("strdup", type, strlen (str)); |
| 144 | alloc_inc (type); |
| 145 | return dup; |
| 146 | } |
| 147 | |
| 148 | #ifdef MEMORY_LOG |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 149 | static struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 150 | { |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 151 | const char *name; |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 152 | long alloc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 153 | unsigned long t_malloc; |
| 154 | unsigned long c_malloc; |
| 155 | unsigned long t_calloc; |
| 156 | unsigned long c_calloc; |
| 157 | unsigned long t_realloc; |
| 158 | unsigned long t_free; |
| 159 | unsigned long c_strdup; |
| 160 | } mstat [MTYPE_MAX]; |
| 161 | |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 162 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 163 | mtype_log (char *func, void *memory, const char *file, int line, int type) |
| 164 | { |
ajs | b9e7028 | 2004-12-08 17:14:45 +0000 | [diff] [blame] | 165 | zlog_debug ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void * |
| 169 | mtype_zmalloc (const char *file, int line, int type, size_t size) |
| 170 | { |
| 171 | void *memory; |
| 172 | |
| 173 | mstat[type].c_malloc++; |
| 174 | mstat[type].t_malloc++; |
| 175 | |
| 176 | memory = zmalloc (type, size); |
| 177 | mtype_log ("zmalloc", memory, file, line, type); |
| 178 | |
| 179 | return memory; |
| 180 | } |
| 181 | |
| 182 | void * |
| 183 | mtype_zcalloc (const char *file, int line, int type, size_t size) |
| 184 | { |
| 185 | void *memory; |
| 186 | |
| 187 | mstat[type].c_calloc++; |
| 188 | mstat[type].t_calloc++; |
| 189 | |
| 190 | memory = zcalloc (type, size); |
| 191 | mtype_log ("xcalloc", memory, file, line, type); |
| 192 | |
| 193 | return memory; |
| 194 | } |
| 195 | |
| 196 | void * |
| 197 | mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size) |
| 198 | { |
| 199 | void *memory; |
| 200 | |
| 201 | /* Realloc need before allocated pointer. */ |
| 202 | mstat[type].t_realloc++; |
| 203 | |
| 204 | memory = zrealloc (type, ptr, size); |
| 205 | |
| 206 | mtype_log ("xrealloc", memory, file, line, type); |
| 207 | |
| 208 | return memory; |
| 209 | } |
| 210 | |
| 211 | /* Important function. */ |
| 212 | void |
| 213 | mtype_zfree (const char *file, int line, int type, void *ptr) |
| 214 | { |
| 215 | mstat[type].t_free++; |
| 216 | |
| 217 | mtype_log ("xfree", ptr, file, line, type); |
| 218 | |
| 219 | zfree (type, ptr); |
| 220 | } |
| 221 | |
| 222 | char * |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 223 | mtype_zstrdup (const char *file, int line, int type, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 224 | { |
| 225 | char *memory; |
| 226 | |
| 227 | mstat[type].c_strdup++; |
| 228 | |
| 229 | memory = zstrdup (type, str); |
| 230 | |
| 231 | mtype_log ("xstrdup", memory, file, line, type); |
| 232 | |
| 233 | return memory; |
| 234 | } |
| 235 | #else |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 236 | static struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 237 | { |
| 238 | char *name; |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 239 | long alloc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 240 | } mstat [MTYPE_MAX]; |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 241 | #endif /* MEMORY_LOG */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 242 | |
| 243 | /* Increment allocation counter. */ |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 244 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 245 | alloc_inc (int type) |
| 246 | { |
| 247 | mstat[type].alloc++; |
| 248 | } |
| 249 | |
| 250 | /* Decrement allocation counter. */ |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 251 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 252 | alloc_dec (int type) |
| 253 | { |
| 254 | mstat[type].alloc--; |
| 255 | } |
| 256 | |
| 257 | /* Looking up memory status from vty interface. */ |
| 258 | #include "vector.h" |
| 259 | #include "vty.h" |
| 260 | #include "command.h" |
| 261 | |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 262 | static void |
| 263 | log_memstats(int pri) |
| 264 | { |
| 265 | struct mlist *ml; |
| 266 | |
| 267 | for (ml = mlists; ml->list; ml++) |
| 268 | { |
| 269 | struct memory_list *m; |
| 270 | |
| 271 | zlog (NULL, pri, "Memory utilization in module %s:", ml->name); |
| 272 | for (m = ml->list; m->index >= 0; m++) |
| 273 | if (m->index && mstat[m->index].alloc) |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 274 | zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc); |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 278 | void |
| 279 | log_memstats_stderr (const char *prefix) |
| 280 | { |
| 281 | struct mlist *ml; |
| 282 | struct memory_list *m; |
| 283 | int i; |
| 284 | int j = 0; |
| 285 | |
| 286 | for (ml = mlists; ml->list; ml++) |
| 287 | { |
| 288 | i = 0; |
| 289 | |
| 290 | for (m = ml->list; m->index >= 0; m++) |
| 291 | if (m->index && mstat[m->index].alloc) |
| 292 | { |
| 293 | if (!i) |
| 294 | fprintf (stderr, |
| 295 | "%s: memstats: Current memory utilization in module %s:\n", |
| 296 | prefix, |
| 297 | ml->name); |
| 298 | fprintf (stderr, |
| 299 | "%s: memstats: %-30s: %10ld%s\n", |
| 300 | prefix, |
| 301 | m->format, |
| 302 | mstat[m->index].alloc, |
| 303 | mstat[m->index].alloc < 0 ? " (REPORT THIS BUG!)" : ""); |
| 304 | i = j = 1; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | if (j) |
| 309 | fprintf (stderr, |
| 310 | "%s: memstats: NOTE: If configuration exists, utilization may be " |
| 311 | "expected.\n", |
| 312 | prefix); |
| 313 | else |
| 314 | fprintf (stderr, |
| 315 | "%s: memstats: No remaining tracked memory utilization.\n", |
| 316 | prefix); |
| 317 | } |
| 318 | |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 319 | static void |
ajs | 24065a3 | 2005-10-20 22:28:14 +0000 | [diff] [blame] | 320 | show_separator(struct vty *vty) |
| 321 | { |
| 322 | vty_out (vty, "-----------------------------\r\n"); |
| 323 | } |
| 324 | |
| 325 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 326 | show_memory_vty (struct vty *vty, struct memory_list *list) |
| 327 | { |
| 328 | struct memory_list *m; |
ajs | 24065a3 | 2005-10-20 22:28:14 +0000 | [diff] [blame] | 329 | int needsep = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 330 | |
| 331 | for (m = list; m->index >= 0; m++) |
| 332 | if (m->index == 0) |
ajs | 24065a3 | 2005-10-20 22:28:14 +0000 | [diff] [blame] | 333 | { |
| 334 | if (needsep) |
| 335 | { |
| 336 | show_separator (vty); |
| 337 | needsep = 0; |
| 338 | } |
| 339 | } |
| 340 | else if (mstat[m->index].alloc) |
| 341 | { |
| 342 | vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc); |
| 343 | needsep = 1; |
| 344 | } |
| 345 | return needsep; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 348 | #ifdef HAVE_MALLINFO |
| 349 | static int |
| 350 | show_memory_mallinfo (struct vty *vty) |
| 351 | { |
| 352 | struct mallinfo minfo = mallinfo(); |
| 353 | char buf[MTYPE_MEMSTR_LEN]; |
| 354 | |
| 355 | vty_out (vty, "System allocator statistics:%s", VTY_NEWLINE); |
| 356 | vty_out (vty, " Total heap allocated: %s%s", |
| 357 | mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.arena), |
| 358 | VTY_NEWLINE); |
| 359 | vty_out (vty, " Holding block headers: %s%s", |
| 360 | mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.hblkhd), |
| 361 | VTY_NEWLINE); |
| 362 | vty_out (vty, " Used small blocks: %s%s", |
| 363 | mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.usmblks), |
| 364 | VTY_NEWLINE); |
| 365 | vty_out (vty, " Used ordinary blocks: %s%s", |
| 366 | mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.uordblks), |
| 367 | VTY_NEWLINE); |
| 368 | vty_out (vty, " Free small blocks: %s%s", |
| 369 | mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fsmblks), |
| 370 | VTY_NEWLINE); |
| 371 | vty_out (vty, " Free ordinary blocks: %s%s", |
| 372 | mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fordblks), |
| 373 | VTY_NEWLINE); |
| 374 | vty_out (vty, " Ordinary blocks: %ld%s", |
| 375 | (unsigned long)minfo.ordblks, |
| 376 | VTY_NEWLINE); |
| 377 | vty_out (vty, " Small blocks: %ld%s", |
| 378 | (unsigned long)minfo.smblks, |
| 379 | VTY_NEWLINE); |
| 380 | vty_out (vty, " Holding blocks: %ld%s", |
| 381 | (unsigned long)minfo.hblks, |
| 382 | VTY_NEWLINE); |
| 383 | vty_out (vty, "(see system documentation for 'mallinfo' for meaning)%s", |
| 384 | VTY_NEWLINE); |
| 385 | return 1; |
| 386 | } |
| 387 | #endif /* HAVE_MALLINFO */ |
| 388 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 389 | DEFUN (show_memory_all, |
| 390 | show_memory_all_cmd, |
| 391 | "show memory all", |
| 392 | "Show running system information\n" |
| 393 | "Memory statistics\n" |
| 394 | "All memory statistics\n") |
| 395 | { |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 396 | struct mlist *ml; |
ajs | 24065a3 | 2005-10-20 22:28:14 +0000 | [diff] [blame] | 397 | int needsep = 0; |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 398 | |
| 399 | #ifdef HAVE_MALLINFO |
| 400 | needsep = show_memory_mallinfo (vty); |
| 401 | #endif /* HAVE_MALLINFO */ |
| 402 | |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 403 | for (ml = mlists; ml->list; ml++) |
| 404 | { |
ajs | 24065a3 | 2005-10-20 22:28:14 +0000 | [diff] [blame] | 405 | if (needsep) |
| 406 | show_separator (vty); |
| 407 | needsep = show_memory_vty (vty, ml->list); |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 408 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 409 | |
| 410 | return CMD_SUCCESS; |
| 411 | } |
| 412 | |
| 413 | ALIAS (show_memory_all, |
| 414 | show_memory_cmd, |
| 415 | "show memory", |
| 416 | "Show running system information\n" |
| 417 | "Memory statistics\n") |
| 418 | |
| 419 | DEFUN (show_memory_lib, |
| 420 | show_memory_lib_cmd, |
| 421 | "show memory lib", |
| 422 | SHOW_STR |
| 423 | "Memory statistics\n" |
| 424 | "Library memory\n") |
| 425 | { |
| 426 | show_memory_vty (vty, memory_list_lib); |
| 427 | return CMD_SUCCESS; |
| 428 | } |
| 429 | |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 430 | DEFUN (show_memory_zebra, |
| 431 | show_memory_zebra_cmd, |
| 432 | "show memory zebra", |
| 433 | SHOW_STR |
| 434 | "Memory statistics\n" |
| 435 | "Zebra memory\n") |
| 436 | { |
| 437 | show_memory_vty (vty, memory_list_zebra); |
| 438 | return CMD_SUCCESS; |
| 439 | } |
| 440 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 441 | DEFUN (show_memory_rip, |
| 442 | show_memory_rip_cmd, |
| 443 | "show memory rip", |
| 444 | SHOW_STR |
| 445 | "Memory statistics\n" |
| 446 | "RIP memory\n") |
| 447 | { |
| 448 | show_memory_vty (vty, memory_list_rip); |
| 449 | return CMD_SUCCESS; |
| 450 | } |
| 451 | |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 452 | DEFUN (show_memory_ripng, |
| 453 | show_memory_ripng_cmd, |
| 454 | "show memory ripng", |
| 455 | SHOW_STR |
| 456 | "Memory statistics\n" |
| 457 | "RIPng memory\n") |
| 458 | { |
| 459 | show_memory_vty (vty, memory_list_ripng); |
| 460 | return CMD_SUCCESS; |
| 461 | } |
| 462 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 463 | DEFUN (show_memory_bgp, |
| 464 | show_memory_bgp_cmd, |
| 465 | "show memory bgp", |
| 466 | SHOW_STR |
| 467 | "Memory statistics\n" |
| 468 | "BGP memory\n") |
| 469 | { |
| 470 | show_memory_vty (vty, memory_list_bgp); |
| 471 | return CMD_SUCCESS; |
| 472 | } |
| 473 | |
| 474 | DEFUN (show_memory_ospf, |
| 475 | show_memory_ospf_cmd, |
| 476 | "show memory ospf", |
| 477 | SHOW_STR |
| 478 | "Memory statistics\n" |
| 479 | "OSPF memory\n") |
| 480 | { |
| 481 | show_memory_vty (vty, memory_list_ospf); |
| 482 | return CMD_SUCCESS; |
| 483 | } |
| 484 | |
| 485 | DEFUN (show_memory_ospf6, |
| 486 | show_memory_ospf6_cmd, |
| 487 | "show memory ospf6", |
| 488 | SHOW_STR |
| 489 | "Memory statistics\n" |
| 490 | "OSPF6 memory\n") |
| 491 | { |
| 492 | show_memory_vty (vty, memory_list_ospf6); |
| 493 | return CMD_SUCCESS; |
| 494 | } |
| 495 | |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 496 | DEFUN (show_memory_isis, |
| 497 | show_memory_isis_cmd, |
| 498 | "show memory isis", |
| 499 | SHOW_STR |
| 500 | "Memory statistics\n" |
| 501 | "ISIS memory\n") |
| 502 | { |
| 503 | show_memory_vty (vty, memory_list_isis); |
| 504 | return CMD_SUCCESS; |
| 505 | } |
| 506 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 507 | void |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 508 | memory_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 509 | { |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 510 | install_element (RESTRICTED_NODE, &show_memory_cmd); |
| 511 | install_element (RESTRICTED_NODE, &show_memory_all_cmd); |
| 512 | install_element (RESTRICTED_NODE, &show_memory_lib_cmd); |
| 513 | install_element (RESTRICTED_NODE, &show_memory_rip_cmd); |
| 514 | install_element (RESTRICTED_NODE, &show_memory_ripng_cmd); |
| 515 | install_element (RESTRICTED_NODE, &show_memory_bgp_cmd); |
| 516 | install_element (RESTRICTED_NODE, &show_memory_ospf_cmd); |
| 517 | install_element (RESTRICTED_NODE, &show_memory_ospf6_cmd); |
| 518 | install_element (RESTRICTED_NODE, &show_memory_isis_cmd); |
| 519 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 520 | install_element (VIEW_NODE, &show_memory_cmd); |
| 521 | install_element (VIEW_NODE, &show_memory_all_cmd); |
| 522 | install_element (VIEW_NODE, &show_memory_lib_cmd); |
| 523 | install_element (VIEW_NODE, &show_memory_rip_cmd); |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 524 | install_element (VIEW_NODE, &show_memory_ripng_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 525 | install_element (VIEW_NODE, &show_memory_bgp_cmd); |
| 526 | install_element (VIEW_NODE, &show_memory_ospf_cmd); |
| 527 | install_element (VIEW_NODE, &show_memory_ospf6_cmd); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 528 | install_element (VIEW_NODE, &show_memory_isis_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 529 | |
| 530 | install_element (ENABLE_NODE, &show_memory_cmd); |
| 531 | install_element (ENABLE_NODE, &show_memory_all_cmd); |
| 532 | install_element (ENABLE_NODE, &show_memory_lib_cmd); |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 533 | install_element (ENABLE_NODE, &show_memory_zebra_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 534 | install_element (ENABLE_NODE, &show_memory_rip_cmd); |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 535 | install_element (ENABLE_NODE, &show_memory_ripng_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 536 | install_element (ENABLE_NODE, &show_memory_bgp_cmd); |
| 537 | install_element (ENABLE_NODE, &show_memory_ospf_cmd); |
| 538 | install_element (ENABLE_NODE, &show_memory_ospf6_cmd); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 539 | install_element (ENABLE_NODE, &show_memory_isis_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 540 | } |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 541 | |
| 542 | /* Stats querying from users */ |
| 543 | /* Return a pointer to a human friendly string describing |
| 544 | * the byte count passed in. E.g: |
| 545 | * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc. |
| 546 | * Up to 4 significant figures will be given. |
| 547 | * The pointer returned may be NULL (indicating an error) |
| 548 | * or point to the given buffer, or point to static storage. |
| 549 | */ |
| 550 | const char * |
| 551 | mtype_memstr (char *buf, size_t len, unsigned long bytes) |
| 552 | { |
| 553 | unsigned int t, g, m, k; |
| 554 | |
| 555 | /* easy cases */ |
| 556 | if (!bytes) |
| 557 | return "0 bytes"; |
| 558 | if (bytes == 1) |
| 559 | return "1 byte"; |
| 560 | |
| 561 | if (sizeof (unsigned long) >= 8) |
| 562 | /* Hacked to make it not warn on ILP32 machines |
| 563 | * Shift will always be 40 at runtime. See below too */ |
| 564 | t = bytes >> (sizeof (unsigned long) >= 8 ? 40 : 0); |
| 565 | else |
| 566 | t = 0; |
| 567 | g = bytes >> 30; |
| 568 | m = bytes >> 20; |
| 569 | k = bytes >> 10; |
| 570 | |
| 571 | if (t > 10) |
| 572 | { |
| 573 | /* The shift will always be 39 at runtime. |
| 574 | * Just hacked to make it not warn on 'smaller' machines. |
| 575 | * Static compiler analysis should mean no extra code |
| 576 | */ |
Paul Jakma | 912df1e | 2008-01-08 13:50:11 +0000 | [diff] [blame] | 577 | if (bytes & (1UL << (sizeof (unsigned long) >= 8 ? 39 : 0))) |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 578 | t++; |
| 579 | snprintf (buf, len, "%4d TiB", t); |
| 580 | } |
| 581 | else if (g > 10) |
| 582 | { |
| 583 | if (bytes & (1 << 29)) |
| 584 | g++; |
| 585 | snprintf (buf, len, "%d GiB", g); |
| 586 | } |
| 587 | else if (m > 10) |
| 588 | { |
| 589 | if (bytes & (1 << 19)) |
| 590 | m++; |
| 591 | snprintf (buf, len, "%d MiB", m); |
| 592 | } |
| 593 | else if (k > 10) |
| 594 | { |
| 595 | if (bytes & (1 << 9)) |
| 596 | k++; |
| 597 | snprintf (buf, len, "%d KiB", k); |
| 598 | } |
| 599 | else |
| 600 | snprintf (buf, len, "%ld bytes", bytes); |
| 601 | |
| 602 | return buf; |
| 603 | } |
| 604 | |
| 605 | unsigned long |
| 606 | mtype_stats_alloc (int type) |
| 607 | { |
| 608 | return mstat[type].alloc; |
| 609 | } |