Index: head/share/man/man9/counter.9 =================================================================== --- head/share/man/man9/counter.9 +++ head/share/man/man9/counter.9 @@ -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 @@ 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 @@ .Xr malloc 9 , .Xr ratecheck 9 , .Xr sysctl 9 , +.Xr SYSINIT 9 , .Xr uma 9 .Sh HISTORY The Index: head/sys/kern/subr_counter.c =================================================================== --- head/sys/kern/subr_counter.c +++ head/sys/kern/subr_counter.c @@ -172,3 +172,21 @@ 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); +} Index: head/sys/kern/subr_pcpu.c =================================================================== --- head/sys/kern/subr_pcpu.c +++ head/sys/kern/subr_pcpu.c @@ -148,7 +148,7 @@ 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 Index: head/sys/sys/counter.h =================================================================== --- head/sys/sys/counter.h +++ head/sys/sys/counter.h @@ -74,5 +74,18 @@ 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__ */ Index: head/sys/sys/kernel.h =================================================================== --- head/sys/sys/kernel.h +++ head/sys/sys/kernel.h @@ -91,7 +91,8 @@ 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