]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* |
b249c083 | 2 | * Memory and dynamic module VTY routine |
718e3744 | 3 | * |
b249c083 DL |
4 | * Copyright (C) 1998 Kunihiro Ishiguro |
5 | * Copyright (C) 2016-2017 David Lamparter for NetDEF, Inc. | |
718e3744 | 6 | * |
b249c083 DL |
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) | |
10 | * any later version. | |
718e3744 | 11 | * |
b249c083 DL |
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 | |
15 | * more details. | |
718e3744 | 16 | * |
896014f4 DL |
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 | |
718e3744 | 20 | */ |
21 | ||
22 | #include <zebra.h> | |
d09552d0 | 23 | /* malloc.h is generally obsolete, however GNU Libc mallinfo wants it. */ |
fa896a1d | 24 | #ifdef HAVE_MALLOC_H |
41be32bf | 25 | #include <malloc.h> |
fa896a1d DL |
26 | #endif |
27 | #ifdef HAVE_MALLOC_MALLOC_H | |
28 | #include <malloc/malloc.h> | |
29 | #endif | |
b249c083 DL |
30 | #include <dlfcn.h> |
31 | #include <link.h> | |
718e3744 | 32 | |
33 | #include "log.h" | |
34 | #include "memory.h" | |
b249c083 | 35 | #include "module.h" |
fc7948fa | 36 | #include "memory_vty.h" |
6b0655a2 | 37 | |
718e3744 | 38 | /* Looking up memory status from vty interface. */ |
39 | #include "vector.h" | |
40 | #include "vty.h" | |
41 | #include "command.h" | |
42 | ||
41be32bf | 43 | #ifdef HAVE_MALLINFO |
d62a17ae | 44 | static int show_memory_mallinfo(struct vty *vty) |
41be32bf | 45 | { |
d62a17ae | 46 | struct mallinfo minfo = mallinfo(); |
47 | char buf[MTYPE_MEMSTR_LEN]; | |
48 | ||
49 | vty_out(vty, "System allocator statistics:\n"); | |
50 | vty_out(vty, " Total heap allocated: %s\n", | |
51 | mtype_memstr(buf, MTYPE_MEMSTR_LEN, minfo.arena)); | |
52 | vty_out(vty, " Holding block headers: %s\n", | |
53 | mtype_memstr(buf, MTYPE_MEMSTR_LEN, minfo.hblkhd)); | |
54 | vty_out(vty, " Used small blocks: %s\n", | |
55 | mtype_memstr(buf, MTYPE_MEMSTR_LEN, minfo.usmblks)); | |
56 | vty_out(vty, " Used ordinary blocks: %s\n", | |
57 | mtype_memstr(buf, MTYPE_MEMSTR_LEN, minfo.uordblks)); | |
58 | vty_out(vty, " Free small blocks: %s\n", | |
59 | mtype_memstr(buf, MTYPE_MEMSTR_LEN, minfo.fsmblks)); | |
60 | vty_out(vty, " Free ordinary blocks: %s\n", | |
61 | mtype_memstr(buf, MTYPE_MEMSTR_LEN, minfo.fordblks)); | |
62 | vty_out(vty, " Ordinary blocks: %ld\n", | |
63 | (unsigned long)minfo.ordblks); | |
64 | vty_out(vty, " Small blocks: %ld\n", | |
65 | (unsigned long)minfo.smblks); | |
66 | vty_out(vty, " Holding blocks: %ld\n", | |
67 | (unsigned long)minfo.hblks); | |
68 | vty_out(vty, "(see system documentation for 'mallinfo' for meaning)\n"); | |
69 | return 1; | |
41be32bf PJ |
70 | } |
71 | #endif /* HAVE_MALLINFO */ | |
72 | ||
3b4cd783 DL |
73 | static int qmem_walker(void *arg, struct memgroup *mg, struct memtype *mt) |
74 | { | |
75 | struct vty *vty = arg; | |
76 | if (!mt) | |
d62a17ae | 77 | vty_out(vty, "--- qmem %s ---\n", mg->name); |
3b4cd783 | 78 | else { |
3694c43a DS |
79 | if (mt->n_alloc != 0) { |
80 | char size[32]; | |
81 | snprintf(size, sizeof(size), "%6zu", mt->size); | |
602a6584 DL |
82 | |
83 | #ifdef HAVE_MALLOC_USABLE_SIZE | |
84 | #define TSTR " %9zu" | |
85 | #define TARG , mt->total | |
86 | #else | |
87 | #define TSTR "" | |
88 | #define TARG | |
89 | #endif | |
90 | vty_out(vty, "%-30s: %10zu %-16s"TSTR"\n", mt->name, | |
d62a17ae | 91 | mt->n_alloc, |
92 | mt->size == 0 ? "" | |
93 | : mt->size == SIZE_VAR | |
94 | ? "(variably sized)" | |
602a6584 DL |
95 | : size |
96 | TARG); | |
3694c43a | 97 | } |
3b4cd783 DL |
98 | } |
99 | return 0; | |
100 | } | |
101 | ||
102 | ||
0d05fa51 DS |
103 | DEFUN (show_memory, |
104 | show_memory_cmd, | |
105 | "show memory", | |
718e3744 | 106 | "Show running system information\n" |
0d05fa51 | 107 | "Memory statistics\n") |
718e3744 | 108 | { |
41be32bf | 109 | #ifdef HAVE_MALLINFO |
d62a17ae | 110 | show_memory_mallinfo(vty); |
41be32bf | 111 | #endif /* HAVE_MALLINFO */ |
fc7948fa | 112 | |
d62a17ae | 113 | qmem_walk(qmem_walker, vty); |
114 | return CMD_SUCCESS; | |
718e3744 | 115 | } |
116 | ||
b249c083 DL |
117 | DEFUN (show_modules, |
118 | show_modules_cmd, | |
119 | "show modules", | |
120 | "Show running system information\n" | |
121 | "Loaded modules\n") | |
122 | { | |
d62a17ae | 123 | struct frrmod_runtime *plug = frrmod_list; |
b249c083 | 124 | |
d62a17ae | 125 | vty_out(vty, "%-12s %-25s %s\n\n", "Module Name", "Version", |
126 | "Description"); | |
127 | while (plug) { | |
128 | const struct frrmod_info *i = plug->info; | |
b249c083 | 129 | |
d62a17ae | 130 | vty_out(vty, "%-12s %-25s %s\n", i->name, i->version, |
131 | i->description); | |
132 | if (plug->dl_handle) { | |
b249c083 | 133 | #ifdef HAVE_DLINFO_ORIGIN |
d62a17ae | 134 | char origin[MAXPATHLEN] = ""; |
135 | dlinfo(plug->dl_handle, RTLD_DI_ORIGIN, &origin); | |
136 | #ifdef HAVE_DLINFO_LINKMAP | |
137 | const char *name; | |
138 | struct link_map *lm = NULL; | |
139 | dlinfo(plug->dl_handle, RTLD_DI_LINKMAP, &lm); | |
140 | if (lm) { | |
141 | name = strrchr(lm->l_name, '/'); | |
142 | name = name ? name + 1 : lm->l_name; | |
143 | vty_out(vty, "\tfrom: %s/%s\n", origin, name); | |
144 | } | |
b249c083 | 145 | #else |
d62a17ae | 146 | vty_out(vty, "\tfrom: %s \n", origin, plug->load_name); |
b249c083 | 147 | #endif |
d62a17ae | 148 | #else |
149 | vty_out(vty, "\tfrom: %s\n", plug->load_name); | |
150 | #endif | |
151 | } | |
152 | plug = plug->next; | |
153 | } | |
154 | return CMD_SUCCESS; | |
b249c083 DL |
155 | } |
156 | ||
d62a17ae | 157 | void memory_init(void) |
718e3744 | 158 | { |
d62a17ae | 159 | install_element(VIEW_NODE, &show_memory_cmd); |
160 | install_element(VIEW_NODE, &show_modules_cmd); | |
718e3744 | 161 | } |
6b0655a2 | 162 | |
41be32bf PJ |
163 | /* Stats querying from users */ |
164 | /* Return a pointer to a human friendly string describing | |
165 | * the byte count passed in. E.g: | |
166 | * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc. | |
167 | * Up to 4 significant figures will be given. | |
168 | * The pointer returned may be NULL (indicating an error) | |
169 | * or point to the given buffer, or point to static storage. | |
170 | */ | |
d62a17ae | 171 | const char *mtype_memstr(char *buf, size_t len, unsigned long bytes) |
41be32bf | 172 | { |
d62a17ae | 173 | unsigned int m, k; |
174 | ||
175 | /* easy cases */ | |
176 | if (!bytes) | |
177 | return "0 bytes"; | |
178 | if (bytes == 1) | |
179 | return "1 byte"; | |
180 | ||
181 | /* | |
182 | * When we pass the 2gb barrier mallinfo() can no longer report | |
183 | * correct data so it just does something odd... | |
184 | * Reporting like Terrabytes of data. Which makes users... | |
185 | * edgy.. yes edgy that's the term for it. | |
186 | * So let's just give up gracefully | |
187 | */ | |
188 | if (bytes > 0x7fffffff) | |
189 | return "> 2GB"; | |
190 | ||
191 | m = bytes >> 20; | |
192 | k = bytes >> 10; | |
193 | ||
194 | if (m > 10) { | |
195 | if (bytes & (1 << 19)) | |
196 | m++; | |
197 | snprintf(buf, len, "%d MiB", m); | |
198 | } else if (k > 10) { | |
199 | if (bytes & (1 << 9)) | |
200 | k++; | |
201 | snprintf(buf, len, "%d KiB", k); | |
202 | } else | |
203 | snprintf(buf, len, "%ld bytes", bytes); | |
204 | ||
205 | return buf; | |
41be32bf | 206 | } |