Author: markj
Date: Fri Mar  6 19:09:01 2020
New Revision: 358715
URL: https://svnweb.freebsd.org/changeset/base/358715

Log:
  Add COUNTER_U64_SYSINIT() and COUNTER_U64_DEFINE_EARLY().
  
  The aim is to reduce the boilerplate needed today to define and
  initialize global counters.  Also add SI_SUB_COUNTER to the sysinit
  ordering.
  
  Reviewed by:  kib
  MFC after:    1 week
  Sponsored by: The FreeBSD Foundation
  Differential Revision:        https://reviews.freebsd.org/D23977

Modified:
  head/share/man/man9/counter.9
  head/sys/kern/subr_counter.c
  head/sys/kern/subr_pcpu.c
  head/sys/sys/counter.h
  head/sys/sys/kernel.h

Modified: head/share/man/man9/counter.9
==============================================================================
--- head/share/man/man9/counter.9       Fri Mar  6 18:41:37 2020        
(r358714)
+++ head/share/man/man9/counter.9       Fri Mar  6 19:09:01 2020        
(r358715)
@@ -25,7 +25,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd March 22, 2017
+.Dd March 6, 2020
 .Dt COUNTER 9
 .Os
 .Sh NAME
@@ -53,6 +53,8 @@
 .Fn counter_u64_zero "counter_u64_t c"
 .Ft int64_t
 .Fn counter_ratecheck "struct counter_rate *cr" "int64_t limit"
+.Fn COUNTER_U64_SYSINIT "counter_u64_t c"
+.Fn COUNTER_U64_DEFINE_EARLY "counter_u64_t c"
 .In sys/sysctl.h
 .Fn SYSCTL_COUNTER_U64 parent nbr name access ptr descr
 .Fn SYSCTL_ADD_COUNTER_U64 ctx parent nbr name access ptr descr
@@ -142,6 +144,20 @@ If the limit was reached on previous second, but was j
 then
 .Fn counter_ratecheck
 returns number of events since previous reset.
+.It Fn COUNTER_U64_SYSINIT c
+Define a
+.Xr SYSINIT 9
+initializer for the global counter
+.Fa c .
+.It Fn COUNTER_U64_DEFINE_EARLY c
+Define and initialize a global counter
+.Fa c .
+It is always safe to increment
+.Fa c ,
+though updates prior to the
+.Dv SI_SUB_COUNTER
+.Xr SYSINIT 9
+event are lost.
 .It Fn SYSCTL_COUNTER_U64 parent nbr name access ptr descr
 Declare a static
 .Xr sysctl 9
@@ -245,6 +261,7 @@ SYSCTL_COUNTER_U64_ARRAY(_debug, OID_AUTO, counter_arr
 .Xr malloc 9 ,
 .Xr ratecheck 9 ,
 .Xr sysctl 9 ,
+.Xr SYSINIT 9 ,
 .Xr uma 9
 .Sh HISTORY
 The

Modified: head/sys/kern/subr_counter.c
==============================================================================
--- head/sys/kern/subr_counter.c        Fri Mar  6 18:41:37 2020        
(r358714)
+++ head/sys/kern/subr_counter.c        Fri Mar  6 19:09:01 2020        
(r358715)
@@ -172,3 +172,21 @@ counter_ratecheck(struct counter_rate *cr, int64_t lim
 
        return (val);
 }
+
+void
+counter_u64_sysinit(void *arg)
+{
+       counter_u64_t *cp;
+
+       cp = arg;
+       *cp = counter_u64_alloc(M_WAITOK);
+}
+
+void
+counter_u64_sysuninit(void *arg)
+{
+       counter_u64_t *cp;
+
+       cp = arg;
+       counter_u64_free(*cp);
+}

Modified: head/sys/kern/subr_pcpu.c
==============================================================================
--- head/sys/kern/subr_pcpu.c   Fri Mar  6 18:41:37 2020        (r358714)
+++ head/sys/kern/subr_pcpu.c   Fri Mar  6 19:09:01 2020        (r358715)
@@ -148,7 +148,7 @@ pcpu_zones_startup(void)
        pcpu_zone_64 = uma_zcreate("64 pcpu", sizeof(uint64_t),
            NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_PCPU);
 }
-SYSINIT(pcpu_zones, SI_SUB_VM, SI_ORDER_ANY, pcpu_zones_startup, NULL);
+SYSINIT(pcpu_zones, SI_SUB_COUNTER, SI_ORDER_FIRST, pcpu_zones_startup, NULL);
 
 /*
  * First-fit extent based allocator for allocating space in the per-cpu

Modified: head/sys/sys/counter.h
==============================================================================
--- head/sys/sys/counter.h      Fri Mar  6 18:41:37 2020        (r358714)
+++ head/sys/sys/counter.h      Fri Mar  6 19:09:01 2020        (r358715)
@@ -74,5 +74,18 @@ struct counter_rate {
 
 int64_t        counter_ratecheck(struct counter_rate *, int64_t);
 
+#define        COUNTER_U64_SYSINIT(c)                                  \
+       SYSINIT(c##_counter_sysinit, SI_SUB_COUNTER,            \
+           SI_ORDER_ANY, counter_u64_sysinit, &c);             \
+       SYSUNINIT(c##_counter_sysuninit, SI_SUB_COUNTER,        \
+           SI_ORDER_ANY, counter_u64_sysuninit, &c)
+
+#define        COUNTER_U64_DEFINE_EARLY(c)                             \
+       counter_u64_t __read_mostly c = EARLY_COUNTER;          \
+       COUNTER_U64_SYSINIT(c)
+
+void counter_u64_sysinit(void *);
+void counter_u64_sysuninit(void *);
+
 #endif /* _KERNEL */
 #endif /* ! __SYS_COUNTER_H__ */

Modified: head/sys/sys/kernel.h
==============================================================================
--- head/sys/sys/kernel.h       Fri Mar  6 18:41:37 2020        (r358714)
+++ head/sys/sys/kernel.h       Fri Mar  6 19:09:01 2020        (r358715)
@@ -91,7 +91,8 @@ enum sysinit_sub_id {
        SI_SUB_DONE             = 0x0000001,    /* processed*/
        SI_SUB_TUNABLES         = 0x0700000,    /* establish tunable values */
        SI_SUB_COPYRIGHT        = 0x0800001,    /* first use of console*/
-       SI_SUB_VM               = 0x1000000,    /* virtual memory system init*/
+       SI_SUB_VM               = 0x1000000,    /* virtual memory system init */
+       SI_SUB_COUNTER          = 0x1100000,    /* counter(9) is initialized */
        SI_SUB_KMEM             = 0x1800000,    /* kernel memory*/
        SI_SUB_HYPERVISOR       = 0x1A40000,    /*
                                                 * Hypervisor detection and
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to