]>
git.proxmox.com Git - mirror_frr.git/blob - lib/memory_vty.c
2 * Memory and dynamic module VTY routine
4 * Copyright (C) 1998 Kunihiro Ishiguro
5 * Copyright (C) 2016-2017 David Lamparter for NetDEF, Inc.
7 * This program 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 Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 /* malloc.h is generally obsolete, however GNU Libc mallinfo wants it. */
27 #ifdef HAVE_MALLOC_MALLOC_H
28 #include <malloc/malloc.h>
38 #include "memory_vty.h"
40 /* Looking up memory status from vty interface. */
46 static int show_memory_mallinfo(struct vty
*vty
)
48 struct mallinfo minfo
= mallinfo();
49 char buf
[MTYPE_MEMSTR_LEN
];
51 vty_out(vty
, "System allocator statistics:\n");
52 vty_out(vty
, " Total heap allocated: %s\n",
53 mtype_memstr(buf
, MTYPE_MEMSTR_LEN
, minfo
.arena
));
54 vty_out(vty
, " Holding block headers: %s\n",
55 mtype_memstr(buf
, MTYPE_MEMSTR_LEN
, minfo
.hblkhd
));
56 vty_out(vty
, " Used small blocks: %s\n",
57 mtype_memstr(buf
, MTYPE_MEMSTR_LEN
, minfo
.usmblks
));
58 vty_out(vty
, " Used ordinary blocks: %s\n",
59 mtype_memstr(buf
, MTYPE_MEMSTR_LEN
, minfo
.uordblks
));
60 vty_out(vty
, " Free small blocks: %s\n",
61 mtype_memstr(buf
, MTYPE_MEMSTR_LEN
, minfo
.fsmblks
));
62 vty_out(vty
, " Free ordinary blocks: %s\n",
63 mtype_memstr(buf
, MTYPE_MEMSTR_LEN
, minfo
.fordblks
));
64 vty_out(vty
, " Ordinary blocks: %ld\n",
65 (unsigned long)minfo
.ordblks
);
66 vty_out(vty
, " Small blocks: %ld\n",
67 (unsigned long)minfo
.smblks
);
68 vty_out(vty
, " Holding blocks: %ld\n",
69 (unsigned long)minfo
.hblks
);
70 vty_out(vty
, "(see system documentation for 'mallinfo' for meaning)\n");
73 #endif /* HAVE_MALLINFO */
75 static int qmem_walker(void *arg
, struct memgroup
*mg
, struct memtype
*mt
)
77 struct vty
*vty
= arg
;
79 vty_out(vty
, "--- qmem %s ---\n", mg
->name
);
80 vty_out(vty
, "%-30s: %8s %-8s%s %8s %9s\n",
81 "Type", "Current#", " Size",
82 #ifdef HAVE_MALLOC_USABLE_SIZE
88 #ifdef HAVE_MALLOC_USABLE_SIZE
95 if (mt
->n_alloc
!= 0) {
97 snprintf(size
, sizeof(size
), "%6zu", mt
->size
);
98 #ifdef HAVE_MALLOC_USABLE_SIZE
100 #define TARG , mt->total
101 #define TARG2 , mt->max_size
107 vty_out(vty
, "%-30s: %8zu %-8s"TSTR
" %8zu"TSTR
"\n",
111 : mt
->size
== SIZE_VAR
126 "Show running system information\n"
127 "Memory statistics\n")
130 show_memory_mallinfo(vty
);
131 #endif /* HAVE_MALLINFO */
133 qmem_walk(qmem_walker
, vty
);
140 "Show running system information\n"
143 struct frrmod_runtime
*plug
= frrmod_list
;
145 vty_out(vty
, "%-12s %-25s %s\n\n", "Module Name", "Version",
148 const struct frrmod_info
*i
= plug
->info
;
150 vty_out(vty
, "%-12s %-25s %s\n", i
->name
, i
->version
,
152 if (plug
->dl_handle
) {
153 #ifdef HAVE_DLINFO_ORIGIN
154 char origin
[MAXPATHLEN
] = "";
155 dlinfo(plug
->dl_handle
, RTLD_DI_ORIGIN
, &origin
);
156 #ifdef HAVE_DLINFO_LINKMAP
158 struct link_map
*lm
= NULL
;
159 dlinfo(plug
->dl_handle
, RTLD_DI_LINKMAP
, &lm
);
161 name
= strrchr(lm
->l_name
, '/');
162 name
= name
? name
+ 1 : lm
->l_name
;
163 vty_out(vty
, "\tfrom: %s/%s\n", origin
, name
);
166 vty_out(vty
, "\tfrom: %s \n", origin
, plug
->load_name
);
169 vty_out(vty
, "\tfrom: %s\n", plug
->load_name
);
175 vty_out(vty
, "pid: %u\n", (uint32_t)(getpid()));
180 void memory_init(void)
182 install_element(VIEW_NODE
, &show_memory_cmd
);
183 install_element(VIEW_NODE
, &show_modules_cmd
);
186 /* Stats querying from users */
187 /* Return a pointer to a human friendly string describing
188 * the byte count passed in. E.g:
189 * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc.
190 * Up to 4 significant figures will be given.
191 * The pointer returned may be NULL (indicating an error)
192 * or point to the given buffer, or point to static storage.
194 const char *mtype_memstr(char *buf
, size_t len
, unsigned long bytes
)
205 * When we pass the 2gb barrier mallinfo() can no longer report
206 * correct data so it just does something odd...
207 * Reporting like Terrabytes of data. Which makes users...
208 * edgy.. yes edgy that's the term for it.
209 * So let's just give up gracefully
211 if (bytes
> 0x7fffffff)
218 if (bytes
& (1 << 19))
220 snprintf(buf
, len
, "%d MiB", m
);
222 if (bytes
& (1 << 9))
224 snprintf(buf
, len
, "%d KiB", k
);
226 snprintf(buf
, len
, "%ld bytes", bytes
);