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); |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [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 | }; |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 45 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
Lou Berger | 9248b61 | 2016-01-12 13:41:47 -0500 | [diff] [blame] | 111 | if (ptr == NULL) /* is really alloc */ |
| 112 | return zcalloc(type, size); |
| 113 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 114 | memory = realloc (ptr, size); |
| 115 | if (memory == NULL) |
| 116 | zerror ("realloc", type, size); |
Chris Hall | cca85d2 | 2010-07-23 11:27:11 -0700 | [diff] [blame] | 117 | if (ptr == NULL) |
| 118 | alloc_inc (type); |
| 119 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 120 | return memory; |
| 121 | } |
| 122 | |
Greg Troxel | b167938 | 2010-09-17 12:19:13 -0400 | [diff] [blame] | 123 | /* |
| 124 | * Free memory allocated by z*alloc or zstrdup. |
| 125 | * Requires: ptr was returned by zmalloc, zcalloc, or zrealloc with the |
| 126 | * same type. |
| 127 | * Effects: The memory is freed and may no longer be referenced. |
| 128 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 129 | void |
| 130 | zfree (int type, void *ptr) |
| 131 | { |
Chris Hall | cca85d2 | 2010-07-23 11:27:11 -0700 | [diff] [blame] | 132 | if (ptr != NULL) |
| 133 | { |
| 134 | alloc_dec (type); |
| 135 | free (ptr); |
| 136 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Greg Troxel | b167938 | 2010-09-17 12:19:13 -0400 | [diff] [blame] | 139 | /* |
| 140 | * Duplicate a string, counting memory usage by type. |
| 141 | * Effects: The string is duplicated, and the return value must |
| 142 | * eventually be passed to zfree with the same type. The function will |
| 143 | * succeed or abort. |
| 144 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 145 | char * |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 146 | zstrdup (int type, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 147 | { |
| 148 | void *dup; |
| 149 | |
| 150 | dup = strdup (str); |
| 151 | if (dup == NULL) |
| 152 | zerror ("strdup", type, strlen (str)); |
| 153 | alloc_inc (type); |
| 154 | return dup; |
| 155 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 156 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 157 | #ifdef MEMORY_LOG |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 158 | static struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 159 | { |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 160 | const char *name; |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 161 | long alloc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 162 | unsigned long t_malloc; |
| 163 | unsigned long c_malloc; |
| 164 | unsigned long t_calloc; |
| 165 | unsigned long c_calloc; |
| 166 | unsigned long t_realloc; |
| 167 | unsigned long t_free; |
| 168 | unsigned long c_strdup; |
| 169 | } mstat [MTYPE_MAX]; |
| 170 | |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 171 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 172 | mtype_log (char *func, void *memory, const char *file, int line, int type) |
| 173 | { |
ajs | b9e7028 | 2004-12-08 17:14:45 +0000 | [diff] [blame] | 174 | 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] | 175 | } |
| 176 | |
| 177 | void * |
| 178 | mtype_zmalloc (const char *file, int line, int type, size_t size) |
| 179 | { |
| 180 | void *memory; |
| 181 | |
| 182 | mstat[type].c_malloc++; |
| 183 | mstat[type].t_malloc++; |
| 184 | |
| 185 | memory = zmalloc (type, size); |
| 186 | mtype_log ("zmalloc", memory, file, line, type); |
| 187 | |
| 188 | return memory; |
| 189 | } |
| 190 | |
| 191 | void * |
| 192 | mtype_zcalloc (const char *file, int line, int type, size_t size) |
| 193 | { |
| 194 | void *memory; |
| 195 | |
| 196 | mstat[type].c_calloc++; |
| 197 | mstat[type].t_calloc++; |
| 198 | |
| 199 | memory = zcalloc (type, size); |
| 200 | mtype_log ("xcalloc", memory, file, line, type); |
| 201 | |
| 202 | return memory; |
| 203 | } |
| 204 | |
| 205 | void * |
| 206 | mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size) |
| 207 | { |
| 208 | void *memory; |
| 209 | |
| 210 | /* Realloc need before allocated pointer. */ |
| 211 | mstat[type].t_realloc++; |
| 212 | |
| 213 | memory = zrealloc (type, ptr, size); |
| 214 | |
| 215 | mtype_log ("xrealloc", memory, file, line, type); |
| 216 | |
| 217 | return memory; |
| 218 | } |
| 219 | |
| 220 | /* Important function. */ |
| 221 | void |
| 222 | mtype_zfree (const char *file, int line, int type, void *ptr) |
| 223 | { |
| 224 | mstat[type].t_free++; |
| 225 | |
| 226 | mtype_log ("xfree", ptr, file, line, type); |
| 227 | |
| 228 | zfree (type, ptr); |
| 229 | } |
| 230 | |
| 231 | char * |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 232 | mtype_zstrdup (const char *file, int line, int type, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 233 | { |
| 234 | char *memory; |
| 235 | |
| 236 | mstat[type].c_strdup++; |
| 237 | |
| 238 | memory = zstrdup (type, str); |
| 239 | |
| 240 | mtype_log ("xstrdup", memory, file, line, type); |
| 241 | |
| 242 | return memory; |
| 243 | } |
| 244 | #else |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 245 | static struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 246 | { |
| 247 | char *name; |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 248 | long alloc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 249 | } mstat [MTYPE_MAX]; |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 250 | #endif /* MEMORY_LOG */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 251 | |
| 252 | /* Increment allocation counter. */ |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 253 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 254 | alloc_inc (int type) |
| 255 | { |
| 256 | mstat[type].alloc++; |
| 257 | } |
| 258 | |
| 259 | /* Decrement allocation counter. */ |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 260 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 261 | alloc_dec (int type) |
| 262 | { |
| 263 | mstat[type].alloc--; |
| 264 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 265 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 266 | /* Looking up memory status from vty interface. */ |
| 267 | #include "vector.h" |
| 268 | #include "vty.h" |
| 269 | #include "command.h" |
| 270 | |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 271 | static void |
| 272 | log_memstats(int pri) |
| 273 | { |
| 274 | struct mlist *ml; |
| 275 | |
| 276 | for (ml = mlists; ml->list; ml++) |
| 277 | { |
| 278 | struct memory_list *m; |
| 279 | |
| 280 | zlog (NULL, pri, "Memory utilization in module %s:", ml->name); |
| 281 | for (m = ml->list; m->index >= 0; m++) |
| 282 | if (m->index && mstat[m->index].alloc) |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 283 | zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc); |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 287 | void |
| 288 | log_memstats_stderr (const char *prefix) |
| 289 | { |
| 290 | struct mlist *ml; |
| 291 | struct memory_list *m; |
| 292 | int i; |
| 293 | int j = 0; |
| 294 | |
| 295 | for (ml = mlists; ml->list; ml++) |
| 296 | { |
| 297 | i = 0; |
| 298 | |
| 299 | for (m = ml->list; m->index >= 0; m++) |
| 300 | if (m->index && mstat[m->index].alloc) |
| 301 | { |
| 302 | if (!i) |
| 303 | fprintf (stderr, |
| 304 | "%s: memstats: Current memory utilization in module %s:\n", |
| 305 | prefix, |
| 306 | ml->name); |
| 307 | fprintf (stderr, |
| 308 | "%s: memstats: %-30s: %10ld%s\n", |
| 309 | prefix, |
| 310 | m->format, |
| 311 | mstat[m->index].alloc, |
| 312 | mstat[m->index].alloc < 0 ? " (REPORT THIS BUG!)" : ""); |
| 313 | i = j = 1; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if (j) |
| 318 | fprintf (stderr, |
| 319 | "%s: memstats: NOTE: If configuration exists, utilization may be " |
| 320 | "expected.\n", |
| 321 | prefix); |
| 322 | else |
| 323 | fprintf (stderr, |
| 324 | "%s: memstats: No remaining tracked memory utilization.\n", |
| 325 | prefix); |
| 326 | } |
| 327 | |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 328 | static void |
ajs | 24065a3 | 2005-10-20 22:28:14 +0000 | [diff] [blame] | 329 | show_separator(struct vty *vty) |
| 330 | { |
| 331 | vty_out (vty, "-----------------------------\r\n"); |
| 332 | } |
| 333 | |
| 334 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 335 | show_memory_vty (struct vty *vty, struct memory_list *list) |
| 336 | { |
| 337 | struct memory_list *m; |
ajs | 24065a3 | 2005-10-20 22:28:14 +0000 | [diff] [blame] | 338 | int needsep = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 339 | |
| 340 | for (m = list; m->index >= 0; m++) |
| 341 | if (m->index == 0) |
ajs | 24065a3 | 2005-10-20 22:28:14 +0000 | [diff] [blame] | 342 | { |
| 343 | if (needsep) |
| 344 | { |
| 345 | show_separator (vty); |
| 346 | needsep = 0; |
| 347 | } |
| 348 | } |
| 349 | else if (mstat[m->index].alloc) |
| 350 | { |
| 351 | vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc); |
| 352 | needsep = 1; |
| 353 | } |
| 354 | return needsep; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 357 | #ifdef HAVE_MALLINFO |
| 358 | static int |
| 359 | show_memory_mallinfo (struct vty *vty) |
| 360 | { |
| 361 | struct mallinfo minfo = mallinfo(); |
| 362 | char buf[MTYPE_MEMSTR_LEN]; |
| 363 | |
| 364 | vty_out (vty, "System allocator statistics:%s", VTY_NEWLINE); |
| 365 | vty_out (vty, " Total heap allocated: %s%s", |
| 366 | mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.arena), |
| 367 | VTY_NEWLINE); |
| 368 | vty_out (vty, " Holding block headers: %s%s", |
| 369 | mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.hblkhd), |
| 370 | VTY_NEWLINE); |
| 371 | vty_out (vty, " Used small blocks: %s%s", |
| 372 | mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.usmblks), |
| 373 | VTY_NEWLINE); |
| 374 | vty_out (vty, " Used ordinary blocks: %s%s", |
| 375 | mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.uordblks), |
| 376 | VTY_NEWLINE); |
| 377 | vty_out (vty, " Free small blocks: %s%s", |
| 378 | mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fsmblks), |
| 379 | VTY_NEWLINE); |
| 380 | vty_out (vty, " Free ordinary blocks: %s%s", |
| 381 | mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fordblks), |
| 382 | VTY_NEWLINE); |
| 383 | vty_out (vty, " Ordinary blocks: %ld%s", |
| 384 | (unsigned long)minfo.ordblks, |
| 385 | VTY_NEWLINE); |
| 386 | vty_out (vty, " Small blocks: %ld%s", |
| 387 | (unsigned long)minfo.smblks, |
| 388 | VTY_NEWLINE); |
| 389 | vty_out (vty, " Holding blocks: %ld%s", |
| 390 | (unsigned long)minfo.hblks, |
| 391 | VTY_NEWLINE); |
| 392 | vty_out (vty, "(see system documentation for 'mallinfo' for meaning)%s", |
| 393 | VTY_NEWLINE); |
| 394 | return 1; |
| 395 | } |
| 396 | #endif /* HAVE_MALLINFO */ |
| 397 | |
Donald Sharp | fbc3e97 | 2015-08-12 14:32:47 -0400 | [diff] [blame] | 398 | DEFUN (show_memory, |
| 399 | show_memory_cmd, |
| 400 | "show memory", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 401 | "Show running system information\n" |
Donald Sharp | fbc3e97 | 2015-08-12 14:32:47 -0400 | [diff] [blame] | 402 | "Memory statistics\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 403 | { |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 404 | struct mlist *ml; |
ajs | 24065a3 | 2005-10-20 22:28:14 +0000 | [diff] [blame] | 405 | int needsep = 0; |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 406 | |
| 407 | #ifdef HAVE_MALLINFO |
| 408 | needsep = show_memory_mallinfo (vty); |
| 409 | #endif /* HAVE_MALLINFO */ |
| 410 | |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 411 | for (ml = mlists; ml->list; ml++) |
| 412 | { |
ajs | 24065a3 | 2005-10-20 22:28:14 +0000 | [diff] [blame] | 413 | if (needsep) |
| 414 | show_separator (vty); |
| 415 | needsep = show_memory_vty (vty, ml->list); |
ajs | 7fa25ff | 2004-11-15 16:12:32 +0000 | [diff] [blame] | 416 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 417 | |
| 418 | return CMD_SUCCESS; |
| 419 | } |
| 420 | |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 421 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 422 | void |
ajs | f858e49 | 2004-11-16 14:25:30 +0000 | [diff] [blame] | 423 | memory_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 424 | { |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 425 | install_element (RESTRICTED_NODE, &show_memory_cmd); |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 426 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 427 | install_element (VIEW_NODE, &show_memory_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 428 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 429 | |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 430 | /* Stats querying from users */ |
| 431 | /* Return a pointer to a human friendly string describing |
| 432 | * the byte count passed in. E.g: |
| 433 | * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc. |
| 434 | * Up to 4 significant figures will be given. |
| 435 | * The pointer returned may be NULL (indicating an error) |
| 436 | * or point to the given buffer, or point to static storage. |
| 437 | */ |
| 438 | const char * |
| 439 | mtype_memstr (char *buf, size_t len, unsigned long bytes) |
| 440 | { |
Donald Sharp | 929a26a | 2015-10-28 19:59:30 -0400 | [diff] [blame] | 441 | unsigned int m, k; |
| 442 | |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 443 | /* easy cases */ |
| 444 | if (!bytes) |
| 445 | return "0 bytes"; |
| 446 | if (bytes == 1) |
| 447 | return "1 byte"; |
Donald Sharp | 929a26a | 2015-10-28 19:59:30 -0400 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | * When we pass the 2gb barrier mallinfo() can no longer report |
| 451 | * correct data so it just does something odd... |
| 452 | * Reporting like Terrabytes of data. Which makes users... |
| 453 | * edgy.. yes edgy that's the term for it. |
| 454 | * So let's just give up gracefully |
| 455 | */ |
| 456 | if (bytes > 0x7fffffff) |
| 457 | return "> 2GB"; |
| 458 | |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 459 | m = bytes >> 20; |
| 460 | k = bytes >> 10; |
Donald Sharp | 929a26a | 2015-10-28 19:59:30 -0400 | [diff] [blame] | 461 | |
| 462 | if (m > 10) |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 463 | { |
| 464 | if (bytes & (1 << 19)) |
| 465 | m++; |
| 466 | snprintf (buf, len, "%d MiB", m); |
| 467 | } |
| 468 | else if (k > 10) |
| 469 | { |
| 470 | if (bytes & (1 << 9)) |
| 471 | k++; |
| 472 | snprintf (buf, len, "%d KiB", k); |
| 473 | } |
| 474 | else |
| 475 | snprintf (buf, len, "%ld bytes", bytes); |
| 476 | |
| 477 | return buf; |
| 478 | } |
| 479 | |
| 480 | unsigned long |
| 481 | mtype_stats_alloc (int type) |
| 482 | { |
| 483 | return mstat[type].alloc; |
| 484 | } |