On Sat, Oct 28, 2017 at 11:06 +0200, Mike Belopuhov wrote:
> On Thu, Oct 26, 2017 at 08:58 +0200, Claudio Jeker wrote:
> > On Wed, Oct 25, 2017 at 11:46:05PM +0200, Mike Belopuhov wrote:
> > > On Wed, Oct 25, 2017 at 21:56 +0200, Claudio Jeker wrote:
> > > > Would be great if netstat could show the current and peak memory usage.
> > > >
> > > 
> > > Current is 5876.  Maximum is 524288.  Do you want to display them in
> > > the x/y/z format?
> > > 
> > >   5876/xxxx/524288 Kbytes allocated to network, 20% in use 
> > > (current/peak/max)
> > > 
> > > Something like this? Any other ideas?
> > 
> > I think that would be an improvement. I normally look for peak values. The
> > current is normally not interesting when tuning systems. 
> > Maybe we can even drop the use percentage since it more confusing than
> > anything.
> > 
> 
> How about this then?
> 
>   saru:usr.bin/netstat% ./obj/netstat -m
>   532 mbufs in use:
>       379 mbufs allocated to data
>       12 mbufs allocated to packet headers
>       141 mbufs allocated to socket names and addresses
>   18/208 mbuf 2048 byte clusters in use (current/peak)
>   0/45 mbuf 2112 byte clusters in use (current/peak)
>   256/320 mbuf 4096 byte clusters in use (current/peak)
>   0/48 mbuf 8192 byte clusters in use (current/peak)
>   0/42 mbuf 9216 byte clusters in use (current/peak)
>   0/50 mbuf 12288 byte clusters in use (current/peak)
>   0/48 mbuf 16384 byte clusters in use (current/peak)
>   0/48 mbuf 65536 byte clusters in use (current/peak)
>   5952/7236/524288 Kbytes allocated to network (current/peak/max)
>   0 requests for memory denied
>   0 requests for memory delayed
>   0 calls to protocol drain routines
> 
> OK?
> 

Ian Darwin suggested using fmt_scaled for an output like this:
6.1M/7.1M/512Mbytes allocated to network (current/peak/max)

netstat is already linked against libutil.

Any objections?

diff --git usr.bin/netstat/mbuf.c usr.bin/netstat/mbuf.c
index f7970a57c32..27412f9e217 100644
--- usr.bin/netstat/mbuf.c
+++ usr.bin/netstat/mbuf.c
@@ -42,10 +42,11 @@
 #include <errno.h>
 #include <limits.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+#include <util.h>
 #include "netstat.h"
 
 #define        YES     1
 typedef int bool;
 
@@ -85,13 +86,13 @@ bool seen[256];                     /* "have we seen this 
type yet?" */
  * Print mbuf statistics.
  */
 void
 mbpr(void)
 {
-       unsigned long totmem, totused, totmbufs;
-       int totpct;
-       int i, mib[4], npools;
+       unsigned long totmem, totpeak, totmbufs;
+       int i, maxclusters, mib[4], npools;
+       char fmt[FMT_SCALED_STRSIZE];
        struct kinfo_pool pool;
        struct mbtypes *mp;
        size_t size;
 
        if (nmbtypes != 256) {
@@ -99,10 +100,20 @@ mbpr(void)
                    "%s: unexpected change to mbstat; check source\n",
                    __progname);
                return;
        }
 
+       mib[0] = CTL_KERN;
+       mib[1] = KERN_MAXCLUSTERS;
+       size = sizeof(maxclusters);
+
+       if (sysctl(mib, 2, &maxclusters, &size, NULL, 0) < 0) {
+               printf("Can't retrieve value of maxclusters from the "
+                   "kernel: %s\n",  strerror(errno));
+               return;
+       }
+
        mib[0] = CTL_KERN;
        mib[1] = KERN_MBSTAT;
        size = sizeof(mbstat);
 
        if (sysctl(mib, 2, &mbstat, &size, NULL, 0) < 0) {
@@ -174,26 +185,34 @@ mbpr(void)
                        printf("\t%u mbuf%s allocated to <mbuf type %d>\n",
                            mbstat.m_mtypes[i],
                            plural(mbstat.m_mtypes[i]), i);
                }
        totmem = (mbpool.pr_npages * mbpool.pr_pgsize);
-       totused = mbpool.pr_nout * mbpool.pr_size;
+       totpeak = mbpool.pr_hiwat * mbpool.pr_pgsize;
        for (i = 0; i < mclp; i++) {
-               printf("%u/%lu/%lu mbuf %d byte clusters in use"
-                   " (current/peak/max)\n",
+               printf("%u/%lu mbuf %d byte clusters in use"
+                   " (current/peak)\n",
                    mclpools[i].pr_nout,
                    (unsigned long)
                        (mclpools[i].pr_hiwat * mclpools[i].pr_itemsperpage),
-                   (unsigned long)
-                       (mclpools[i].pr_maxpages * mclpools[i].pr_itemsperpage),
                    mclpools[i].pr_size);
                totmem += (mclpools[i].pr_npages * mclpools[i].pr_pgsize);
-               totused += mclpools[i].pr_nout * mclpools[i].pr_size;
+               totpeak += mclpools[i].pr_hiwat * mclpools[i].pr_pgsize;
        }
 
-       totpct = (totmem == 0) ? 0 : (totused/(totmem / 100));
-       printf("%lu Kbytes allocated to network (%d%% in use)\n",
-           totmem / 1024, totpct);
+       if (fmt_scaled(totmem, fmt) == 0)
+               printf("%s/", fmt);
+       else
+               printf("%luK/", totmem / 1024);
+       if (fmt_scaled(totpeak, fmt) == 0)
+               printf("%s/", fmt);
+       else
+               printf("%luK/", totpeak / 1024);
+       if (fmt_scaled(maxclusters * MCLBYTES, fmt) == 0)
+               printf("%s", fmt);
+       else
+               printf("%uK", (maxclusters * MCLBYTES) / 1024);
+       printf("bytes allocated to network (current/peak/max)\n");
        printf("%lu requests for memory denied\n", mbstat.m_drops);
        printf("%lu requests for memory delayed\n", mbstat.m_wait);
        printf("%lu calls to protocol drain routines\n", mbstat.m_drain);
 }

Reply via email to