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 | |
| 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 * |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 115 | zstrdup (int type, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
| 126 | #ifdef MEMORY_LOG |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 127 | static struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 128 | { |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 129 | const char *name; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 130 | 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 | |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 140 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 141 | mtype_log (char *func, void *memory, const char *file, int line, int type) |
| 142 | { |
ajs | b9e7028 | 2004-12-08 17:14:45 +0000 | [diff] [blame] | 143 | 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] | 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 * |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 201 | mtype_zstrdup (const char *file, int line, int type, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 214 | static struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 215 | { |
| 216 | char *name; |
| 217 | unsigned long alloc; |
| 218 | } mstat [MTYPE_MAX]; |
| 219 | #endif /* MTPYE_LOG */ |
| 220 | |
| 221 | /* Increment allocation counter. */ |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 222 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 223 | alloc_inc (int type) |
| 224 | { |
| 225 | mstat[type].alloc++; |
| 226 | } |
| 227 | |
| 228 | /* Decrement allocation counter. */ |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 229 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 230 | alloc_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 | |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 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) |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 252 | zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc); |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 256 | static void |
ajs | 24065a3 | 2005-10-20 22:28:14 +0000 | [diff] [blame] | 257 | show_separator(struct vty *vty) |
| 258 | { |
| 259 | vty_out (vty, "-----------------------------\r\n"); |
| 260 | } |
| 261 | |
| 262 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 263 | show_memory_vty (struct vty *vty, struct memory_list *list) |
| 264 | { |
| 265 | struct memory_list *m; |
ajs | 24065a3 | 2005-10-20 22:28:14 +0000 | [diff] [blame] | 266 | int needsep = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 267 | |
| 268 | for (m = list; m->index >= 0; m++) |
| 269 | if (m->index == 0) |
ajs | 24065a3 | 2005-10-20 22:28:14 +0000 | [diff] [blame] | 270 | { |
| 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; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 285 | #ifdef HAVE_MALLINFO |
| 286 | static int |
| 287 | show_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 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 326 | DEFUN (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 | { |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 333 | struct mlist *ml; |
ajs | 24065a3 | 2005-10-20 22:28:14 +0000 | [diff] [blame] | 334 | int needsep = 0; |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 335 | |
| 336 | #ifdef HAVE_MALLINFO |
| 337 | needsep = show_memory_mallinfo (vty); |
| 338 | #endif /* HAVE_MALLINFO */ |
| 339 | |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 340 | for (ml = mlists; ml->list; ml++) |
| 341 | { |
ajs | 24065a3 | 2005-10-20 22:28:14 +0000 | [diff] [blame] | 342 | if (needsep) |
| 343 | show_separator (vty); |
| 344 | needsep = show_memory_vty (vty, ml->list); |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 345 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 346 | |
| 347 | return CMD_SUCCESS; |
| 348 | } |
| 349 | |
| 350 | ALIAS (show_memory_all, |
| 351 | show_memory_cmd, |
| 352 | "show memory", |
| 353 | "Show running system information\n" |
| 354 | "Memory statistics\n") |
| 355 | |
| 356 | DEFUN (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 | |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 367 | DEFUN (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 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 378 | DEFUN (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 | |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 389 | DEFUN (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 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 400 | DEFUN (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 | |
| 411 | DEFUN (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 | |
| 422 | DEFUN (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 | |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 433 | DEFUN (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 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 444 | void |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 445 | memory_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 446 | { |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 447 | 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 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 457 | 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); |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 461 | install_element (VIEW_NODE, &show_memory_ripng_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 462 | 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); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 465 | install_element (VIEW_NODE, &show_memory_isis_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 466 | |
| 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); |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 470 | install_element (ENABLE_NODE, &show_memory_zebra_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 471 | install_element (ENABLE_NODE, &show_memory_rip_cmd); |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 472 | install_element (ENABLE_NODE, &show_memory_ripng_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 473 | 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); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 476 | install_element (ENABLE_NODE, &show_memory_isis_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 477 | } |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 478 | |
| 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 | */ |
| 487 | const char * |
| 488 | mtype_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 Jakma | 912df1e | 2008-01-08 13:50:11 +0000 | [diff] [blame] | 514 | if (bytes & (1UL << (sizeof (unsigned long) >= 8 ? 39 : 0))) |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 515 | 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 | |
| 542 | unsigned long |
| 543 | mtype_stats_alloc (int type) |
| 544 | { |
| 545 | return mstat[type].alloc; |
| 546 | } |