]> git.proxmox.com Git - mirror_frr.git/blame - lib/memory_vty.c
lib: dynamic module loading
[mirror_frr.git] / lib / memory_vty.c
CommitLineData
718e3744 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>
d09552d0 24/* malloc.h is generally obsolete, however GNU Libc mallinfo wants it. */
24f5e2fc 25#if (defined(GNU_LINUX) && defined(HAVE_MALLINFO))
41be32bf 26#include <malloc.h>
24f5e2fc 27#endif /* HAVE_MALLINFO */
718e3744 28
29#include "log.h"
30#include "memory.h"
fc7948fa 31#include "memory_vty.h"
6b0655a2 32
718e3744 33/* Looking up memory status from vty interface. */
34#include "vector.h"
35#include "vty.h"
36#include "command.h"
37
41be32bf
PJ
38#ifdef HAVE_MALLINFO
39static int
40show_memory_mallinfo (struct vty *vty)
41{
42 struct mallinfo minfo = mallinfo();
43 char buf[MTYPE_MEMSTR_LEN];
44
45 vty_out (vty, "System allocator statistics:%s", VTY_NEWLINE);
46 vty_out (vty, " Total heap allocated: %s%s",
47 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.arena),
48 VTY_NEWLINE);
49 vty_out (vty, " Holding block headers: %s%s",
50 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.hblkhd),
51 VTY_NEWLINE);
52 vty_out (vty, " Used small blocks: %s%s",
53 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.usmblks),
54 VTY_NEWLINE);
55 vty_out (vty, " Used ordinary blocks: %s%s",
56 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.uordblks),
57 VTY_NEWLINE);
58 vty_out (vty, " Free small blocks: %s%s",
59 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fsmblks),
60 VTY_NEWLINE);
61 vty_out (vty, " Free ordinary blocks: %s%s",
62 mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fordblks),
63 VTY_NEWLINE);
64 vty_out (vty, " Ordinary blocks: %ld%s",
65 (unsigned long)minfo.ordblks,
66 VTY_NEWLINE);
67 vty_out (vty, " Small blocks: %ld%s",
68 (unsigned long)minfo.smblks,
69 VTY_NEWLINE);
70 vty_out (vty, " Holding blocks: %ld%s",
71 (unsigned long)minfo.hblks,
72 VTY_NEWLINE);
73 vty_out (vty, "(see system documentation for 'mallinfo' for meaning)%s",
74 VTY_NEWLINE);
75 return 1;
76}
77#endif /* HAVE_MALLINFO */
78
3b4cd783
DL
79static int qmem_walker(void *arg, struct memgroup *mg, struct memtype *mt)
80{
81 struct vty *vty = arg;
82 if (!mt)
83 vty_out (vty, "--- qmem %s ---%s", mg->name, VTY_NEWLINE);
84 else {
3694c43a
DS
85 if (mt->n_alloc != 0) {
86 char size[32];
87 snprintf(size, sizeof(size), "%6zu", mt->size);
88 vty_out (vty, "%-30s: %10zu %s%s",
89 mt->name, mt->n_alloc,
90 mt->size == 0 ? "" :
91 mt->size == SIZE_VAR ? "(variably sized)" :
92 size, VTY_NEWLINE);
93 }
3b4cd783
DL
94 }
95 return 0;
96}
97
98
0d05fa51
DS
99DEFUN (show_memory,
100 show_memory_cmd,
101 "show memory",
718e3744 102 "Show running system information\n"
0d05fa51 103 "Memory statistics\n")
718e3744 104{
41be32bf 105#ifdef HAVE_MALLINFO
7a13c923 106 show_memory_mallinfo (vty);
41be32bf 107#endif /* HAVE_MALLINFO */
fc7948fa 108
3b4cd783 109 qmem_walk(qmem_walker, vty);
718e3744 110 return CMD_SUCCESS;
111}
112
718e3744 113void
f858e49b 114memory_init (void)
718e3744 115{
116 install_element (VIEW_NODE, &show_memory_cmd);
718e3744 117}
6b0655a2 118
41be32bf
PJ
119/* Stats querying from users */
120/* Return a pointer to a human friendly string describing
121 * the byte count passed in. E.g:
122 * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc.
123 * Up to 4 significant figures will be given.
124 * The pointer returned may be NULL (indicating an error)
125 * or point to the given buffer, or point to static storage.
126 */
127const char *
128mtype_memstr (char *buf, size_t len, unsigned long bytes)
129{
6896f650
DS
130 unsigned int m, k;
131
41be32bf
PJ
132 /* easy cases */
133 if (!bytes)
134 return "0 bytes";
135 if (bytes == 1)
136 return "1 byte";
6896f650
DS
137
138 /*
139 * When we pass the 2gb barrier mallinfo() can no longer report
140 * correct data so it just does something odd...
141 * Reporting like Terrabytes of data. Which makes users...
142 * edgy.. yes edgy that's the term for it.
143 * So let's just give up gracefully
144 */
145 if (bytes > 0x7fffffff)
146 return "> 2GB";
147
41be32bf
PJ
148 m = bytes >> 20;
149 k = bytes >> 10;
6896f650
DS
150
151 if (m > 10)
41be32bf
PJ
152 {
153 if (bytes & (1 << 19))
154 m++;
155 snprintf (buf, len, "%d MiB", m);
156 }
157 else if (k > 10)
158 {
159 if (bytes & (1 << 9))
160 k++;
161 snprintf (buf, len, "%d KiB", k);
162 }
163 else
164 snprintf (buf, len, "%ld bytes", bytes);
165
166 return buf;
167}