From 41be32bfe6d0d6ed0058babdcbc3aac18988dbec Mon Sep 17 00:00:00 2001 From: Paul Jakma Date: Thu, 30 Mar 2006 13:53:59 +0000 Subject: [lib/memory] Add mallinfo support 2006-02-15 Paul Jakma * configure.ac: Check for mallinfo, being careful to link test so we can detect things like umem being used (which doesn't provide a mallinfo). * lib/memory.c: (mtype_memstr) new helper function to return human friendly string for a byte count. (mtype_stats_alloc) new function, for users to retrieve number of objects allocated. (show_memory_mallinfo) New function, show mallinfo statistics if available. (show_memory_all_cmd) Call show_memory_mallinfo, if mallinfo is available. * lib/memory.h: Export mtype_memstr and mtype_stats_alloc. Provide a define for a reasonable buffer size for mtype_memstr. --- lib/memory.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 116 insertions(+), 1 deletion(-) (limited to 'lib/memory.c') diff --git a/lib/memory.c b/lib/memory.c index dae2b9ae..802c07f2 100644 --- a/lib/memory.c +++ b/lib/memory.c @@ -21,6 +21,7 @@ */ #include +#include #include "log.h" #include "memory.h" @@ -278,6 +279,47 @@ show_memory_vty (struct vty *vty, struct memory_list *list) return needsep; } +#ifdef HAVE_MALLINFO +static int +show_memory_mallinfo (struct vty *vty) +{ + struct mallinfo minfo = mallinfo(); + char buf[MTYPE_MEMSTR_LEN]; + + vty_out (vty, "System allocator statistics:%s", VTY_NEWLINE); + vty_out (vty, " Total heap allocated: %s%s", + mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.arena), + VTY_NEWLINE); + vty_out (vty, " Holding block headers: %s%s", + mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.hblkhd), + VTY_NEWLINE); + vty_out (vty, " Used small blocks: %s%s", + mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.usmblks), + VTY_NEWLINE); + vty_out (vty, " Used ordinary blocks: %s%s", + mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.uordblks), + VTY_NEWLINE); + vty_out (vty, " Free small blocks: %s%s", + mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fsmblks), + VTY_NEWLINE); + vty_out (vty, " Free ordinary blocks: %s%s", + mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fordblks), + VTY_NEWLINE); + vty_out (vty, " Ordinary blocks: %ld%s", + (unsigned long)minfo.ordblks, + VTY_NEWLINE); + vty_out (vty, " Small blocks: %ld%s", + (unsigned long)minfo.smblks, + VTY_NEWLINE); + vty_out (vty, " Holding blocks: %ld%s", + (unsigned long)minfo.hblks, + VTY_NEWLINE); + vty_out (vty, "(see system documentation for 'mallinfo' for meaning)%s", + VTY_NEWLINE); + return 1; +} +#endif /* HAVE_MALLINFO */ + DEFUN (show_memory_all, show_memory_all_cmd, "show memory all", @@ -287,7 +329,11 @@ DEFUN (show_memory_all, { struct mlist *ml; int needsep = 0; - + +#ifdef HAVE_MALLINFO + needsep = show_memory_mallinfo (vty); +#endif /* HAVE_MALLINFO */ + for (ml = mlists; ml->list; ml++) { if (needsep) @@ -416,3 +462,72 @@ memory_init (void) install_element (ENABLE_NODE, &show_memory_ospf6_cmd); install_element (ENABLE_NODE, &show_memory_isis_cmd); } + +/* Stats querying from users */ +/* Return a pointer to a human friendly string describing + * the byte count passed in. E.g: + * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc. + * Up to 4 significant figures will be given. + * The pointer returned may be NULL (indicating an error) + * or point to the given buffer, or point to static storage. + */ +const char * +mtype_memstr (char *buf, size_t len, unsigned long bytes) +{ + unsigned int t, g, m, k; + + /* easy cases */ + if (!bytes) + return "0 bytes"; + if (bytes == 1) + return "1 byte"; + + if (sizeof (unsigned long) >= 8) + /* Hacked to make it not warn on ILP32 machines + * Shift will always be 40 at runtime. See below too */ + t = bytes >> (sizeof (unsigned long) >= 8 ? 40 : 0); + else + t = 0; + g = bytes >> 30; + m = bytes >> 20; + k = bytes >> 10; + + if (t > 10) + { + /* The shift will always be 39 at runtime. + * Just hacked to make it not warn on 'smaller' machines. + * Static compiler analysis should mean no extra code + */ + if (bytes & (1 << (sizeof (unsigned long) >= 8 ? 39 : 0))) + t++; + snprintf (buf, len, "%4d TiB", t); + } + else if (g > 10) + { + if (bytes & (1 << 29)) + g++; + snprintf (buf, len, "%d GiB", g); + } + else if (m > 10) + { + if (bytes & (1 << 19)) + m++; + snprintf (buf, len, "%d MiB", m); + } + else if (k > 10) + { + if (bytes & (1 << 9)) + k++; + snprintf (buf, len, "%d KiB", k); + } + else + snprintf (buf, len, "%ld bytes", bytes); + + return buf; +} + +unsigned long +mtype_stats_alloc (int type) +{ + return mstat[type].alloc; +} -- cgit v1.2.3 From d09552d0c7d389f83c3833bf82f7a97da37007da Mon Sep 17 00:00:00 2001 From: Paul Jakma Date: Sun, 28 May 2006 08:15:46 +0000 Subject: [lib] malloc.h is deprecated, try not to include it anymore 2006-05-28 Paul Jakma * memory.c: malloc.h is deprecated in favour of stdlib.h, however we still need it on GNU Libc for mallinfo(). --- lib/memory.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/memory.c') diff --git a/lib/memory.c b/lib/memory.c index 802c07f2..1141e405 100644 --- a/lib/memory.c +++ b/lib/memory.c @@ -21,7 +21,10 @@ */ #include +/* malloc.h is generally obsolete, however GNU Libc mallinfo wants it. */ +#if defined(HAVE_STDLIB_H) || (defined(GNU_LINUX) && defined(HAVE_MALLINFO)) #include +#endif /* !HAVE_STDLIB_H || HAVE_MALLINFO */ #include "log.h" #include "memory.h" -- cgit v1.2.3 From 74176d220ba6fb019a18ebea3e3245a46f33dce1 Mon Sep 17 00:00:00 2001 From: Paul Jakma Date: Fri, 30 Jun 2006 16:49:02 +0000 Subject: [lib] Fix typo in cpp conditional for malloc.h include 2006-06-28 Paul Jakma * memory.c: Fix typo in cpp conditional around malloc.h, from comment in bug #269. --- lib/memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memory.c') diff --git a/lib/memory.c b/lib/memory.c index 1141e405..eb670722 100644 --- a/lib/memory.c +++ b/lib/memory.c @@ -22,7 +22,7 @@ #include /* malloc.h is generally obsolete, however GNU Libc mallinfo wants it. */ -#if defined(HAVE_STDLIB_H) || (defined(GNU_LINUX) && defined(HAVE_MALLINFO)) +#if !defined(HAVE_STDLIB_H) || (defined(GNU_LINUX) && defined(HAVE_MALLINFO)) #include #endif /* !HAVE_STDLIB_H || HAVE_MALLINFO */ -- cgit v1.2.3 From 912df1e8ab61962c29eb4faff17a7d75c4905b84 Mon Sep 17 00:00:00 2001 From: Paul Jakma Date: Tue, 8 Jan 2008 13:50:11 +0000 Subject: [lib] add mising UL qualifier to numerical constant 2008-01-08 Pavol Rusnak * memory.c: (mtype_memstr) Fix accidental shift past width of type, constant should have been forced to UL, rather than being left to default to int. --- lib/memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memory.c') diff --git a/lib/memory.c b/lib/memory.c index eb670722..9ed5e100 100644 --- a/lib/memory.c +++ b/lib/memory.c @@ -501,7 +501,7 @@ mtype_memstr (char *buf, size_t len, unsigned long bytes) * Just hacked to make it not warn on 'smaller' machines. * Static compiler analysis should mean no extra code */ - if (bytes & (1 << (sizeof (unsigned long) >= 8 ? 39 : 0))) + if (bytes & (1UL << (sizeof (unsigned long) >= 8 ? 39 : 0))) t++; snprintf (buf, len, "%4d TiB", t); } -- cgit v1.2.3 From 1423c809cc4ddc2e013ba6264c49a11e5719c6f2 Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Thu, 14 Aug 2008 17:59:25 +0100 Subject: [lib] mes_lookup string lookup table argument should be marked const 2008-08-14 Stephen Hemminger * lib/log.{c,h}: struct message argument should point to const * */*.c: adjust to suit, Signed-off-by: Paul Jakma --- lib/memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/memory.c') diff --git a/lib/memory.c b/lib/memory.c index 9ed5e100..28b3d896 100644 --- a/lib/memory.c +++ b/lib/memory.c @@ -33,7 +33,7 @@ static void alloc_inc (int); static void alloc_dec (int); static void log_memstats(int log_priority); -static struct message mstr [] = +static const struct message mstr [] = { { MTYPE_THREAD, "thread" }, { MTYPE_THREAD_MASTER, "thread_master" }, -- cgit v1.2.3 From 62687ff1cd3d4460cdbd4b0fbf1e3298fe277ad2 Mon Sep 17 00:00:00 2001 From: Paul Jakma Date: Sat, 23 Aug 2008 14:27:06 +0100 Subject: [vty] Add support for a 'restricted mode' with anonymous vty connections * lib/command.h: Add a RESTRICTED_NODE, intended for use with anonymous, 'no login' vtys, to provide a subset of 'view' mode commands. * lib/command.c: Add RESTRICTED_NODE bits, nothing special, just following VIEW_NODE. * lib/vty.c: (vty_auth) enable authentication should fall back to restricted/view node as appropriate. (vty_create) init vty's to restricted/view node as appropriate, for the 'no login' case. (vty_{no_,}restricted_mode_cmd) config commands to enable 'anonymous restricted' in vty configuration. (vty_config_write) 'anonymous restricted' config. (vty_init) Install some commands to restricted mode, and the 'anonymous restricted' config commands into VTY_NODE. * bgpd/*.c: Install some of the safe(r) BGP commands into 'restricted mode', i.e. lookup commands of non-sensitive data. Useful with looking-glass route-servers. --- lib/memory.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'lib/memory.c') diff --git a/lib/memory.c b/lib/memory.c index 28b3d896..f5d0cba6 100644 --- a/lib/memory.c +++ b/lib/memory.c @@ -444,6 +444,16 @@ DEFUN (show_memory_isis, void memory_init (void) { + install_element (RESTRICTED_NODE, &show_memory_cmd); + install_element (RESTRICTED_NODE, &show_memory_all_cmd); + install_element (RESTRICTED_NODE, &show_memory_lib_cmd); + install_element (RESTRICTED_NODE, &show_memory_rip_cmd); + install_element (RESTRICTED_NODE, &show_memory_ripng_cmd); + install_element (RESTRICTED_NODE, &show_memory_bgp_cmd); + install_element (RESTRICTED_NODE, &show_memory_ospf_cmd); + install_element (RESTRICTED_NODE, &show_memory_ospf6_cmd); + install_element (RESTRICTED_NODE, &show_memory_isis_cmd); + install_element (VIEW_NODE, &show_memory_cmd); install_element (VIEW_NODE, &show_memory_all_cmd); install_element (VIEW_NODE, &show_memory_lib_cmd); -- cgit v1.2.3