Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/vmstat Counter tweaks:



details:   https://anonhg.NetBSD.org/src/rev/b42cc4b1c0a4
branches:  trunk
changeset: 934418:b42cc4b1c0a4
user:      ad <ad%NetBSD.org@localhost>
date:      Thu Jun 11 22:21:05 2020 +0000

description:
Counter tweaks:

- Don't need to count anonpages+filepages any more; clean+unknown+dirty for
  each kind of page can be summed to get the totals.

- Track the number of free pages with a counter so that it's one less thing
  for the allocator to do, which opens up further options there.

- Remove cpu_count_sync_one().  It has no users and doesn't save a whole lot.
  For the cheap option, give cpu_count_sync() a boolean parameter indicating
  that a cached value is okay, and rate limit the updates for cached values
  to hz.

diffstat:

 sys/compat/linux/common/linux_misc.c        |  12 ++-
 sys/compat/linux32/common/linux32_sysinfo.c |  12 ++-
 sys/kern/subr_cpu.c                         |  99 ++++++++++++----------------
 sys/kern/vfs_vnode.c                        |   6 +-
 sys/kern/vfs_vnops.c                        |   6 +-
 sys/miscfs/procfs/procfs_linux.c            |  17 +++-
 sys/sys/cpu_data.h                          |  30 ++++----
 sys/uvm/uvm_loan.c                          |   8 +-
 sys/uvm/uvm_meter.c                         |  28 ++++---
 sys/uvm/uvm_page.c                          |  63 ++++++++----------
 sys/uvm/uvm_pdaemon.c                       |  12 ++-
 sys/uvm/uvm_pdpolicy_clock.c                |  15 ++-
 sys/uvm/uvm_stat.c                          |  26 +++++--
 usr.bin/vmstat/vmstat.c                     |   7 +-
 14 files changed, 174 insertions(+), 167 deletions(-)

diffs (truncated from 831 to 300 lines):

diff -r ea5a88a0218f -r b42cc4b1c0a4 sys/compat/linux/common/linux_misc.c
--- a/sys/compat/linux/common/linux_misc.c      Thu Jun 11 19:20:42 2020 +0000
+++ b/sys/compat/linux/common/linux_misc.c      Thu Jun 11 22:21:05 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: linux_misc.c,v 1.250 2020/06/11 19:20:46 ad Exp $      */
+/*     $NetBSD: linux_misc.c,v 1.251 2020/06/11 22:21:05 ad Exp $      */
 
 /*-
  * Copyright (c) 1995, 1998, 1999, 2008 The NetBSD Foundation, Inc.
@@ -57,7 +57,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: linux_misc.c,v 1.250 2020/06/11 19:20:46 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux_misc.c,v 1.251 2020/06/11 22:21:05 ad Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1347,6 +1347,7 @@
        } */
        struct linux_sysinfo si;
        struct loadavg *la;
+       int64_t filepg;
 
        memset(&si, 0, sizeof(si));
        si.uptime = time_uptime;
@@ -1355,9 +1356,14 @@
        si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
        si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
        si.totalram = ctob((u_long)physmem);
+       /* uvm_availmem() may sync the counters. */
        si.freeram = (u_long)uvm_availmem(true) * uvmexp.pagesize;
+       filepg = cpu_count_get(CPU_COUNT_FILECLEAN) +
+           cpu_count_get(CPU_COUNT_FILEDIRTY) +
+           cpu_count_get(CPU_COUNT_FILEUNKNOWN) -
+           cpu_count_get(CPU_COUNT_EXECPAGES);
        si.sharedram = 0;       /* XXX */
-       si.bufferram = (u_long)uvmexp.filepages * uvmexp.pagesize;
+       si.bufferram = (u_long)(filepg * uvmexp.pagesize);
        si.totalswap = (u_long)uvmexp.swpages * uvmexp.pagesize;
        si.freeswap = 
            (u_long)(uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
diff -r ea5a88a0218f -r b42cc4b1c0a4 sys/compat/linux32/common/linux32_sysinfo.c
--- a/sys/compat/linux32/common/linux32_sysinfo.c       Thu Jun 11 19:20:42 2020 +0000
+++ b/sys/compat/linux32/common/linux32_sysinfo.c       Thu Jun 11 22:21:05 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: linux32_sysinfo.c,v 1.12 2020/06/11 19:20:46 ad Exp $ */
+/*     $NetBSD: linux32_sysinfo.c,v 1.13 2020/06/11 22:21:05 ad Exp $ */
 
 /*-
  * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
@@ -33,7 +33,7 @@
 
 #include <sys/cdefs.h>
 
-__KERNEL_RCSID(0, "$NetBSD: linux32_sysinfo.c,v 1.12 2020/06/11 19:20:46 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux32_sysinfo.c,v 1.13 2020/06/11 22:21:05 ad Exp $");
 
 #include <sys/types.h>
 #include <sys/param.h>
@@ -69,6 +69,7 @@
        } */
        struct linux32_sysinfo si;
        struct loadavg *la;
+       int64_t filepg;
 
        memset(&si, 0, sizeof(si));
        si.uptime = time_uptime;
@@ -77,9 +78,14 @@
        si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
        si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
        si.totalram = ctob((u_long)physmem);
+       /* uvm_availmem() may sync the counters. */
        si.freeram = (u_long)uvm_availmem(true) * uvmexp.pagesize;
+       filepg = cpu_count_get(CPU_COUNT_FILECLEAN) +
+           cpu_count_get(CPU_COUNT_FILEDIRTY) +
+           cpu_count_get(CPU_COUNT_FILEUNKNOWN) -
+           cpu_count_get(CPU_COUNT_EXECPAGES);
        si.sharedram = 0;       /* XXX */
-       si.bufferram = (u_long)uvmexp.filepages * uvmexp.pagesize;
+       si.bufferram = (u_long)(filepg * uvmexp.pagesize);
        si.totalswap = (u_long)uvmexp.swpages * uvmexp.pagesize;
        si.freeswap = 
            (u_long)(uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
diff -r ea5a88a0218f -r b42cc4b1c0a4 sys/kern/subr_cpu.c
--- a/sys/kern/subr_cpu.c       Thu Jun 11 19:20:42 2020 +0000
+++ b/sys/kern/subr_cpu.c       Thu Jun 11 22:21:05 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_cpu.c,v 1.14 2020/03/26 19:23:18 ad Exp $ */
+/*     $NetBSD: subr_cpu.c,v 1.15 2020/06/11 22:21:05 ad Exp $ */
 
 /*-
  * Copyright (c) 2007, 2008, 2009, 2010, 2012, 2019, 2020
@@ -61,9 +61,10 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_cpu.c,v 1.14 2020/03/26 19:23:18 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_cpu.c,v 1.15 2020/06/11 22:21:05 ad Exp $");
 
 #include <sys/param.h>
+#include <sys/atomic.h>
 #include <sys/systm.h>
 #include <sys/sched.h>
 #include <sys/conf.h>
@@ -445,70 +446,56 @@
 
 /*
  * Fetch fresh sum total for all counts.  Expensive - don't call often.
+ *
+ * If poll is true, the the caller is okay with with less recent values (but
+ * no more than 1/hz seconds old).  Where this is called very often that
+ * should be the case.
+ *
+ * This should be reasonably quick so that any value collected get isn't
+ * totally out of whack, and it can also be called from interrupt context,
+ * so go to splvm() while summing the counters.  It's tempting to use a spin
+ * mutex here but this routine is called from DDB.
  */
 void
-cpu_count_sync_all(void)
+cpu_count_sync(bool poll)
 {
        CPU_INFO_ITERATOR cii;
        struct cpu_info *ci;
        int64_t sum[CPU_COUNT_MAX], *ptr;
+       static int lasttick;
+       int curtick, s;
        enum cpu_count i;
-       int s;
 
        KASSERT(sizeof(ci->ci_counts) == sizeof(cpu_counts));
 
-       if (__predict_true(mp_online)) {
-               memset(sum, 0, sizeof(sum));
-               /*
-                * We want this to be reasonably quick, so any value we get
-                * isn't totally out of whack, so don't let the current LWP
-                * get preempted.
-                */
-               s = splvm();
-               curcpu()->ci_counts[CPU_COUNT_SYNC_ALL]++;
-               for (CPU_INFO_FOREACH(cii, ci)) {
-                       ptr = ci->ci_counts;
-                       for (i = 0; i < CPU_COUNT_MAX; i += 8) {
-                               sum[i+0] += ptr[i+0];
-                               sum[i+1] += ptr[i+1];
-                               sum[i+2] += ptr[i+2];
-                               sum[i+3] += ptr[i+3];
-                               sum[i+4] += ptr[i+4];
-                               sum[i+5] += ptr[i+5];
-                               sum[i+6] += ptr[i+6];
-                               sum[i+7] += ptr[i+7];
-                       }
-                       KASSERT(i == CPU_COUNT_MAX);
-               }
-               memcpy(cpu_counts, sum, sizeof(cpu_counts));
+       if (__predict_false(!mp_online)) {
+               memcpy(cpu_counts, curcpu()->ci_counts, sizeof(cpu_counts));
+               return;
+       }
+
+       s = splvm();
+       curtick = getticks();
+       if (poll && atomic_load_acquire(&lasttick) == curtick) {
                splx(s);
-       } else {
-               memcpy(cpu_counts, curcpu()->ci_counts, sizeof(cpu_counts));
+               return;
        }
+       memset(sum, 0, sizeof(sum));
+       curcpu()->ci_counts[CPU_COUNT_SYNC]++;
+       for (CPU_INFO_FOREACH(cii, ci)) {
+               ptr = ci->ci_counts;
+               for (i = 0; i < CPU_COUNT_MAX; i += 8) {
+                       sum[i+0] += ptr[i+0];
+                       sum[i+1] += ptr[i+1];
+                       sum[i+2] += ptr[i+2];
+                       sum[i+3] += ptr[i+3];
+                       sum[i+4] += ptr[i+4];
+                       sum[i+5] += ptr[i+5];
+                       sum[i+6] += ptr[i+6];
+                       sum[i+7] += ptr[i+7];
+               }
+               KASSERT(i == CPU_COUNT_MAX);
+       }
+       memcpy(cpu_counts, sum, sizeof(cpu_counts));
+       atomic_store_release(&lasttick, curtick);
+       splx(s);
 }
-
-/*
- * Fetch a fresh sum total for one single count.  Expensive - don't call often.
- */
-int64_t
-cpu_count_sync(enum cpu_count count)
-{
-       CPU_INFO_ITERATOR cii;
-       struct cpu_info *ci;
-       int64_t sum;
-       int s;
-
-       if (__predict_true(mp_online)) {
-               s = splvm();
-               curcpu()->ci_counts[CPU_COUNT_SYNC_ONE]++;
-               sum = 0;
-               for (CPU_INFO_FOREACH(cii, ci)) {
-                       sum += ci->ci_counts[count];
-               }
-               splx(s);
-       } else {
-               /* XXX Early boot, iterator might not be available. */
-               sum = curcpu()->ci_counts[count];
-       }
-       return cpu_counts[count] = sum;
-}
diff -r ea5a88a0218f -r b42cc4b1c0a4 sys/kern/vfs_vnode.c
--- a/sys/kern/vfs_vnode.c      Thu Jun 11 19:20:42 2020 +0000
+++ b/sys/kern/vfs_vnode.c      Thu Jun 11 22:21:05 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: vfs_vnode.c,v 1.123 2020/05/26 18:38:37 ad Exp $       */
+/*     $NetBSD: vfs_vnode.c,v 1.124 2020/06/11 22:21:05 ad Exp $       */
 
 /*-
  * Copyright (c) 1997-2011, 2019, 2020 The NetBSD Foundation, Inc.
@@ -148,7 +148,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_vnode.c,v 1.123 2020/05/26 18:38:37 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_vnode.c,v 1.124 2020/06/11 22:21:05 ad Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_pax.h"
@@ -900,7 +900,6 @@
                if ((vp->v_iflag & VI_EXECMAP) != 0 &&
                    vp->v_uobj.uo_npages != 0) {
                        cpu_count(CPU_COUNT_EXECPAGES, -vp->v_uobj.uo_npages);
-                       cpu_count(CPU_COUNT_FILEPAGES, vp->v_uobj.uo_npages);
                }
                vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP|VI_WRMAP);
                vp->v_vflag &= ~VV_MAPPED;
@@ -1710,7 +1709,6 @@
        mutex_enter(vp->v_interlock);
        if ((vp->v_iflag & VI_EXECMAP) != 0 && vp->v_uobj.uo_npages != 0) {
                cpu_count(CPU_COUNT_EXECPAGES, -vp->v_uobj.uo_npages);
-               cpu_count(CPU_COUNT_FILEPAGES, vp->v_uobj.uo_npages);
        }
        vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP);
        vp->v_iflag |= VI_DEADCHECK; /* for genfs_getpages() */
diff -r ea5a88a0218f -r b42cc4b1c0a4 sys/kern/vfs_vnops.c
--- a/sys/kern/vfs_vnops.c      Thu Jun 11 19:20:42 2020 +0000
+++ b/sys/kern/vfs_vnops.c      Thu Jun 11 22:21:05 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: vfs_vnops.c,v 1.212 2020/05/23 23:42:43 ad Exp $       */
+/*     $NetBSD: vfs_vnops.c,v 1.213 2020/06/11 22:21:05 ad Exp $       */
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.212 2020/05/23 23:42:43 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.213 2020/06/11 22:21:05 ad Exp $");
 
 #include "veriexec.h"
 
@@ -342,7 +342,6 @@
        rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
        mutex_enter(vp->v_interlock);
        if ((vp->v_iflag & VI_EXECMAP) == 0) {
-               cpu_count(CPU_COUNT_FILEPAGES, -vp->v_uobj.uo_npages);
                cpu_count(CPU_COUNT_EXECPAGES, vp->v_uobj.uo_npages);
                vp->v_iflag |= VI_EXECMAP;
        }
@@ -372,7 +371,6 @@
                return (ETXTBSY);
        }
        if ((vp->v_iflag & VI_EXECMAP) == 0) {
-               cpu_count(CPU_COUNT_FILEPAGES, -vp->v_uobj.uo_npages);
                cpu_count(CPU_COUNT_EXECPAGES, vp->v_uobj.uo_npages);
        }
        vp->v_iflag |= (VI_TEXT | VI_EXECMAP);
diff -r ea5a88a0218f -r b42cc4b1c0a4 sys/miscfs/procfs/procfs_linux.c
--- a/sys/miscfs/procfs/procfs_linux.c  Thu Jun 11 19:20:42 2020 +0000
+++ b/sys/miscfs/procfs/procfs_linux.c  Thu Jun 11 22:21:05 2020 +0000
@@ -1,4 +1,4 @@
-/*      $NetBSD: procfs_linux.c,v 1.85 2020/06/11 19:20:46 ad Exp $      */
+/*      $NetBSD: procfs_linux.c,v 1.86 2020/06/11 22:21:05 ad Exp $      */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: procfs_linux.c,v 1.85 2020/06/11 19:20:46 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: procfs_linux.c,v 1.86 2020/06/11 22:21:05 ad Exp $");
 



Home | Main Index | Thread Index | Old Index