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> |
| 24 | |
| 25 | #include "log.h" |
| 26 | #include "memory.h" |
| 27 | |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 28 | static void alloc_inc (int); |
| 29 | static void alloc_dec (int); |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 30 | static void log_memstats(int log_priority); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 31 | |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 32 | static struct message mstr [] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | { |
| 34 | { MTYPE_THREAD, "thread" }, |
| 35 | { MTYPE_THREAD_MASTER, "thread_master" }, |
| 36 | { MTYPE_VECTOR, "vector" }, |
| 37 | { MTYPE_VECTOR_INDEX, "vector_index" }, |
| 38 | { MTYPE_IF, "interface" }, |
| 39 | { 0, NULL }, |
| 40 | }; |
| 41 | |
| 42 | /* Fatal memory allocation error occured. */ |
| 43 | static void |
| 44 | zerror (const char *fname, int type, size_t size) |
| 45 | { |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 46 | zlog_err ("%s : can't allocate memory for `%s' size %d: %s\n", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 47 | fname, lookup (mstr, type), (int) size, safe_strerror(errno)); |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 48 | log_memstats(LOG_WARNING); |
ajs | 48d6c69 | 2004-11-26 20:52:59 +0000 | [diff] [blame] | 49 | /* N.B. It might be preferable to call zlog_backtrace_sigsafe here, since |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 50 | that function should definitely be safe in an OOM condition. But |
ajs | 48d6c69 | 2004-11-26 20:52:59 +0000 | [diff] [blame] | 51 | unfortunately zlog_backtrace_sigsafe does not support syslog logging at |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 52 | this time... */ |
| 53 | zlog_backtrace(LOG_WARNING); |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 54 | abort(); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* Memory allocation. */ |
| 58 | void * |
| 59 | zmalloc (int type, size_t size) |
| 60 | { |
| 61 | void *memory; |
| 62 | |
| 63 | memory = malloc (size); |
| 64 | |
| 65 | if (memory == NULL) |
| 66 | zerror ("malloc", type, size); |
| 67 | |
| 68 | alloc_inc (type); |
| 69 | |
| 70 | return memory; |
| 71 | } |
| 72 | |
| 73 | /* Memory allocation with num * size with cleared. */ |
| 74 | void * |
| 75 | zcalloc (int type, size_t size) |
| 76 | { |
| 77 | void *memory; |
| 78 | |
| 79 | memory = calloc (1, size); |
| 80 | |
| 81 | if (memory == NULL) |
| 82 | zerror ("calloc", type, size); |
| 83 | |
| 84 | alloc_inc (type); |
| 85 | |
| 86 | return memory; |
| 87 | } |
| 88 | |
| 89 | /* Memory reallocation. */ |
| 90 | void * |
| 91 | zrealloc (int type, void *ptr, size_t size) |
| 92 | { |
| 93 | void *memory; |
| 94 | |
| 95 | memory = realloc (ptr, size); |
| 96 | if (memory == NULL) |
| 97 | zerror ("realloc", type, size); |
| 98 | return memory; |
| 99 | } |
| 100 | |
| 101 | /* Memory free. */ |
| 102 | void |
| 103 | zfree (int type, void *ptr) |
| 104 | { |
| 105 | alloc_dec (type); |
| 106 | free (ptr); |
| 107 | } |
| 108 | |
| 109 | /* String duplication. */ |
| 110 | char * |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 111 | zstrdup (int type, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 112 | { |
| 113 | void *dup; |
| 114 | |
| 115 | dup = strdup (str); |
| 116 | if (dup == NULL) |
| 117 | zerror ("strdup", type, strlen (str)); |
| 118 | alloc_inc (type); |
| 119 | return dup; |
| 120 | } |
| 121 | |
| 122 | #ifdef MEMORY_LOG |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 123 | static struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 124 | { |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 125 | const char *name; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 126 | unsigned long alloc; |
| 127 | unsigned long t_malloc; |
| 128 | unsigned long c_malloc; |
| 129 | unsigned long t_calloc; |
| 130 | unsigned long c_calloc; |
| 131 | unsigned long t_realloc; |
| 132 | unsigned long t_free; |
| 133 | unsigned long c_strdup; |
| 134 | } mstat [MTYPE_MAX]; |
| 135 | |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 136 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 137 | mtype_log (char *func, void *memory, const char *file, int line, int type) |
| 138 | { |
ajs | b9e7028 | 2004-12-08 17:14:45 +0000 | [diff] [blame] | 139 | 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] | 140 | } |
| 141 | |
| 142 | void * |
| 143 | mtype_zmalloc (const char *file, int line, int type, size_t size) |
| 144 | { |
| 145 | void *memory; |
| 146 | |
| 147 | mstat[type].c_malloc++; |
| 148 | mstat[type].t_malloc++; |
| 149 | |
| 150 | memory = zmalloc (type, size); |
| 151 | mtype_log ("zmalloc", memory, file, line, type); |
| 152 | |
| 153 | return memory; |
| 154 | } |
| 155 | |
| 156 | void * |
| 157 | mtype_zcalloc (const char *file, int line, int type, size_t size) |
| 158 | { |
| 159 | void *memory; |
| 160 | |
| 161 | mstat[type].c_calloc++; |
| 162 | mstat[type].t_calloc++; |
| 163 | |
| 164 | memory = zcalloc (type, size); |
| 165 | mtype_log ("xcalloc", memory, file, line, type); |
| 166 | |
| 167 | return memory; |
| 168 | } |
| 169 | |
| 170 | void * |
| 171 | mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size) |
| 172 | { |
| 173 | void *memory; |
| 174 | |
| 175 | /* Realloc need before allocated pointer. */ |
| 176 | mstat[type].t_realloc++; |
| 177 | |
| 178 | memory = zrealloc (type, ptr, size); |
| 179 | |
| 180 | mtype_log ("xrealloc", memory, file, line, type); |
| 181 | |
| 182 | return memory; |
| 183 | } |
| 184 | |
| 185 | /* Important function. */ |
| 186 | void |
| 187 | mtype_zfree (const char *file, int line, int type, void *ptr) |
| 188 | { |
| 189 | mstat[type].t_free++; |
| 190 | |
| 191 | mtype_log ("xfree", ptr, file, line, type); |
| 192 | |
| 193 | zfree (type, ptr); |
| 194 | } |
| 195 | |
| 196 | char * |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 197 | mtype_zstrdup (const char *file, int line, int type, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 198 | { |
| 199 | char *memory; |
| 200 | |
| 201 | mstat[type].c_strdup++; |
| 202 | |
| 203 | memory = zstrdup (type, str); |
| 204 | |
| 205 | mtype_log ("xstrdup", memory, file, line, type); |
| 206 | |
| 207 | return memory; |
| 208 | } |
| 209 | #else |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 210 | static struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 211 | { |
| 212 | char *name; |
| 213 | unsigned long alloc; |
| 214 | } mstat [MTYPE_MAX]; |
| 215 | #endif /* MTPYE_LOG */ |
| 216 | |
| 217 | /* Increment allocation counter. */ |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 218 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 219 | alloc_inc (int type) |
| 220 | { |
| 221 | mstat[type].alloc++; |
| 222 | } |
| 223 | |
| 224 | /* Decrement allocation counter. */ |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 225 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 226 | alloc_dec (int type) |
| 227 | { |
| 228 | mstat[type].alloc--; |
| 229 | } |
| 230 | |
| 231 | /* Looking up memory status from vty interface. */ |
| 232 | #include "vector.h" |
| 233 | #include "vty.h" |
| 234 | #include "command.h" |
| 235 | |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 236 | static void |
| 237 | log_memstats(int pri) |
| 238 | { |
| 239 | struct mlist *ml; |
| 240 | |
| 241 | for (ml = mlists; ml->list; ml++) |
| 242 | { |
| 243 | struct memory_list *m; |
| 244 | |
| 245 | zlog (NULL, pri, "Memory utilization in module %s:", ml->name); |
| 246 | for (m = ml->list; m->index >= 0; m++) |
| 247 | if (m->index && mstat[m->index].alloc) |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 248 | zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc); |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 252 | static struct memory_list memory_list_separator[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 253 | { |
| 254 | { 0, NULL}, |
| 255 | {-1, NULL} |
| 256 | }; |
| 257 | |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 258 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 259 | show_memory_vty (struct vty *vty, struct memory_list *list) |
| 260 | { |
| 261 | struct memory_list *m; |
| 262 | |
| 263 | for (m = list; m->index >= 0; m++) |
| 264 | if (m->index == 0) |
| 265 | vty_out (vty, "-----------------------------\r\n"); |
| 266 | else |
paul | 2c1de2a | 2005-04-25 14:02:44 +0000 | [diff] [blame] | 267 | vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | DEFUN (show_memory_all, |
| 271 | show_memory_all_cmd, |
| 272 | "show memory all", |
| 273 | "Show running system information\n" |
| 274 | "Memory statistics\n" |
| 275 | "All memory statistics\n") |
| 276 | { |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 277 | struct mlist *ml; |
| 278 | |
| 279 | for (ml = mlists; ml->list; ml++) |
| 280 | { |
| 281 | if (ml != mlists) |
| 282 | show_memory_vty (vty, memory_list_separator); |
| 283 | show_memory_vty (vty, ml->list); |
| 284 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 285 | |
| 286 | return CMD_SUCCESS; |
| 287 | } |
| 288 | |
| 289 | ALIAS (show_memory_all, |
| 290 | show_memory_cmd, |
| 291 | "show memory", |
| 292 | "Show running system information\n" |
| 293 | "Memory statistics\n") |
| 294 | |
| 295 | DEFUN (show_memory_lib, |
| 296 | show_memory_lib_cmd, |
| 297 | "show memory lib", |
| 298 | SHOW_STR |
| 299 | "Memory statistics\n" |
| 300 | "Library memory\n") |
| 301 | { |
| 302 | show_memory_vty (vty, memory_list_lib); |
| 303 | return CMD_SUCCESS; |
| 304 | } |
| 305 | |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 306 | DEFUN (show_memory_zebra, |
| 307 | show_memory_zebra_cmd, |
| 308 | "show memory zebra", |
| 309 | SHOW_STR |
| 310 | "Memory statistics\n" |
| 311 | "Zebra memory\n") |
| 312 | { |
| 313 | show_memory_vty (vty, memory_list_zebra); |
| 314 | return CMD_SUCCESS; |
| 315 | } |
| 316 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 317 | DEFUN (show_memory_rip, |
| 318 | show_memory_rip_cmd, |
| 319 | "show memory rip", |
| 320 | SHOW_STR |
| 321 | "Memory statistics\n" |
| 322 | "RIP memory\n") |
| 323 | { |
| 324 | show_memory_vty (vty, memory_list_rip); |
| 325 | return CMD_SUCCESS; |
| 326 | } |
| 327 | |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 328 | DEFUN (show_memory_ripng, |
| 329 | show_memory_ripng_cmd, |
| 330 | "show memory ripng", |
| 331 | SHOW_STR |
| 332 | "Memory statistics\n" |
| 333 | "RIPng memory\n") |
| 334 | { |
| 335 | show_memory_vty (vty, memory_list_ripng); |
| 336 | return CMD_SUCCESS; |
| 337 | } |
| 338 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 339 | DEFUN (show_memory_bgp, |
| 340 | show_memory_bgp_cmd, |
| 341 | "show memory bgp", |
| 342 | SHOW_STR |
| 343 | "Memory statistics\n" |
| 344 | "BGP memory\n") |
| 345 | { |
| 346 | show_memory_vty (vty, memory_list_bgp); |
| 347 | return CMD_SUCCESS; |
| 348 | } |
| 349 | |
| 350 | DEFUN (show_memory_ospf, |
| 351 | show_memory_ospf_cmd, |
| 352 | "show memory ospf", |
| 353 | SHOW_STR |
| 354 | "Memory statistics\n" |
| 355 | "OSPF memory\n") |
| 356 | { |
| 357 | show_memory_vty (vty, memory_list_ospf); |
| 358 | return CMD_SUCCESS; |
| 359 | } |
| 360 | |
| 361 | DEFUN (show_memory_ospf6, |
| 362 | show_memory_ospf6_cmd, |
| 363 | "show memory ospf6", |
| 364 | SHOW_STR |
| 365 | "Memory statistics\n" |
| 366 | "OSPF6 memory\n") |
| 367 | { |
| 368 | show_memory_vty (vty, memory_list_ospf6); |
| 369 | return CMD_SUCCESS; |
| 370 | } |
| 371 | |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 372 | DEFUN (show_memory_isis, |
| 373 | show_memory_isis_cmd, |
| 374 | "show memory isis", |
| 375 | SHOW_STR |
| 376 | "Memory statistics\n" |
| 377 | "ISIS memory\n") |
| 378 | { |
| 379 | show_memory_vty (vty, memory_list_isis); |
| 380 | return CMD_SUCCESS; |
| 381 | } |
| 382 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 383 | void |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 384 | memory_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 385 | { |
| 386 | install_element (VIEW_NODE, &show_memory_cmd); |
| 387 | install_element (VIEW_NODE, &show_memory_all_cmd); |
| 388 | install_element (VIEW_NODE, &show_memory_lib_cmd); |
| 389 | install_element (VIEW_NODE, &show_memory_rip_cmd); |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 390 | install_element (VIEW_NODE, &show_memory_ripng_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 391 | install_element (VIEW_NODE, &show_memory_bgp_cmd); |
| 392 | install_element (VIEW_NODE, &show_memory_ospf_cmd); |
| 393 | install_element (VIEW_NODE, &show_memory_ospf6_cmd); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 394 | install_element (VIEW_NODE, &show_memory_isis_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 395 | |
| 396 | install_element (ENABLE_NODE, &show_memory_cmd); |
| 397 | install_element (ENABLE_NODE, &show_memory_all_cmd); |
| 398 | install_element (ENABLE_NODE, &show_memory_lib_cmd); |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 399 | install_element (ENABLE_NODE, &show_memory_zebra_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 400 | install_element (ENABLE_NODE, &show_memory_rip_cmd); |
hasso | a94434b | 2003-05-25 17:10:12 +0000 | [diff] [blame] | 401 | install_element (ENABLE_NODE, &show_memory_ripng_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 402 | install_element (ENABLE_NODE, &show_memory_bgp_cmd); |
| 403 | install_element (ENABLE_NODE, &show_memory_ospf_cmd); |
| 404 | install_element (ENABLE_NODE, &show_memory_ospf6_cmd); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 405 | install_element (ENABLE_NODE, &show_memory_isis_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 406 | } |