Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/tprof tprof: Use percpu rather than a MAXCPUS-elemen...



details:   https://anonhg.NetBSD.org/src/rev/9ea34a3345df
branches:  trunk
changeset: 946435:9ea34a3345df
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Fri Nov 27 20:10:25 2020 +0000

description:
tprof: Use percpu rather than a MAXCPUS-element array.

diffstat:

 sys/dev/tprof/tprof.c |  62 ++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 47 insertions(+), 15 deletions(-)

diffs (130 lines):

diff -r f16db07862e8 -r 9ea34a3345df sys/dev/tprof/tprof.c
--- a/sys/dev/tprof/tprof.c     Fri Nov 27 17:01:18 2020 +0000
+++ b/sys/dev/tprof/tprof.c     Fri Nov 27 20:10:25 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tprof.c,v 1.14 2018/07/13 07:56:29 maxv Exp $  */
+/*     $NetBSD: tprof.c,v 1.15 2020/11/27 20:10:25 riastradh Exp $     */
 
 /*-
  * Copyright (c)2008,2009,2010 YAMAMOTO Takashi,
@@ -27,20 +27,21 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: tprof.c,v 1.14 2018/07/13 07:56:29 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tprof.c,v 1.15 2020/11/27 20:10:25 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/kernel.h>
 
-#include <sys/cpu.h>
+#include <sys/callout.h>
 #include <sys/conf.h>
-#include <sys/callout.h>
+#include <sys/cpu.h>
 #include <sys/kmem.h>
 #include <sys/module.h>
+#include <sys/percpu.h>
 #include <sys/proc.h>
+#include <sys/queue.h>
 #include <sys/workqueue.h>
-#include <sys/queue.h>
 
 #include <dev/tprof/tprof.h>
 #include <dev/tprof/tprof_ioctl.h>
@@ -97,7 +98,7 @@
 static STAILQ_HEAD(, tprof_buf) tprof_list; /* L: global buffer list */
 static u_int tprof_nbuf_on_list;       /* L: # of buffers on tprof_list */
 static struct workqueue *tprof_wq;
-static tprof_cpu_t tprof_cpus[MAXCPUS] __aligned(CACHE_LINE_SIZE);
+static struct percpu *tprof_cpus __read_mostly;        /* tprof_cpu_t * */
 static u_int tprof_samples_per_buf;
 
 static tprof_backend_t *tprof_backend; /* S: */
@@ -116,8 +117,20 @@
 static tprof_cpu_t *
 tprof_cpu(struct cpu_info *ci)
 {
+       tprof_cpu_t **cp, *c;
 
-       return &tprof_cpus[cpu_index(ci)];
+       /*
+        * As long as xcalls are blocked -- e.g., by kpreempt_disable
+        * -- the percpu object will not be swapped and destroyed.  We
+        * can't write to it, because the data may have already been
+        * moved to a new buffer, but we can safely read from it.
+        */
+       kpreempt_disable();
+       cp = percpu_getptr_remote(tprof_cpus, ci);
+       c = *cp;
+       kpreempt_enable();
+
+       return c;
 }
 
 static tprof_cpu_t *
@@ -132,7 +145,7 @@
 {
        tprof_buf_t *new;
        u_int size = tprof_samples_per_buf;
-       
+
        new = kmem_alloc(TPROF_BUF_BYTESIZE(size), KM_SLEEP);
        new->b_used = 0;
        new->b_size = size;
@@ -682,9 +695,31 @@
 MODULE(MODULE_CLASS_DRIVER, tprof, NULL);
 
 static void
+tprof_cpu_init(void *vcp, void *vcookie, struct cpu_info *ci)
+{
+       tprof_cpu_t **cp = vcp, *c;
+
+       c = kmem_zalloc(sizeof(*c), KM_SLEEP);
+       c->c_buf = NULL;
+       c->c_cpuid = cpu_index(ci);
+       *cp = c;
+}
+
+static void
+tprof_cpu_fini(void *vcp, void *vcookie, struct cpu_info *ci)
+{
+       tprof_cpu_t **cp = vcp, *c;
+
+       c = *cp;
+       KASSERT(c->c_cpuid == cpu_index(ci));
+       KASSERT(c->c_buf == NULL);
+       kmem_free(c, sizeof(*c));
+       *cp = NULL;
+}
+
+static void
 tprof_driver_init(void)
 {
-       unsigned int i;
 
        mutex_init(&tprof_lock, MUTEX_DEFAULT, IPL_NONE);
        mutex_init(&tprof_reader_lock, MUTEX_DEFAULT, IPL_NONE);
@@ -692,18 +727,15 @@
        cv_init(&tprof_cv, "tprof");
        cv_init(&tprof_reader_cv, "tprof_rd");
        STAILQ_INIT(&tprof_list);
-       for (i = 0; i < __arraycount(tprof_cpus); i++) {
-               tprof_cpu_t * const c = &tprof_cpus[i];
-
-               c->c_buf = NULL;
-               c->c_cpuid = i;
-       }
+       tprof_cpus = percpu_create(sizeof(tprof_cpu_t *),
+           tprof_cpu_init, tprof_cpu_fini, NULL);
 }
 
 static void
 tprof_driver_fini(void)
 {
 
+       percpu_free(tprof_cpus, sizeof(tprof_cpu_t *));
        mutex_destroy(&tprof_lock);
        mutex_destroy(&tprof_reader_lock);
        mutex_destroy(&tprof_startstop_lock);



Home | Main Index | Thread Index | Old Index