On Sat, Oct 28, 2017 at 11:06:16AM +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?

I like it. Ok claudio@
 
> diff --git usr.bin/netstat/mbuf.c usr.bin/netstat/mbuf.c
> index f7970a57c32..79c2c16a6c3 100644
> --- usr.bin/netstat/mbuf.c
> +++ usr.bin/netstat/mbuf.c
> @@ -85,13 +85,12 @@ 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;
>       struct kinfo_pool pool;
>       struct mbtypes *mp;
>       size_t size;
>  
>       if (nmbtypes != 256) {
> @@ -99,10 +98,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 +183,24 @@ 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);
> +     printf("%lu/%lu/%lu Kbytes allocated to network "
> +         "(current/peak/max)\n", totmem / 1024, totpeak / 1024,
> +         (unsigned long)(maxclusters * MCLBYTES) / 1024);
>       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);
>  }
> 

-- 
:wq Claudio

Reply via email to