diff options
author | Mark Johnston <markj@FreeBSD.org> | 2020-03-06 19:09:01 +0000 |
---|---|---|
committer | Mark Johnston <markj@FreeBSD.org> | 2020-03-06 19:09:01 +0000 |
commit | fffcb56f7a7b1ee6fb1a91584ba69b62d133c2d5 (patch) | |
tree | 77b799209f34c69f7b4f9f60202c32118bc658fc | |
parent | f15ccf8836d9d42b92f999dbd6c4746c9547f8a6 (diff) | |
download | src-fffcb56f7a7b1ee6fb1a91584ba69b62d133c2d5.tar.gz src-fffcb56f7a7b1ee6fb1a91584ba69b62d133c2d5.zip |
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
Notes
Notes:
svn path=/head/; revision=358715
-rw-r--r-- | share/man/man9/counter.9 | 19 | ||||
-rw-r--r-- | sys/kern/subr_counter.c | 18 | ||||
-rw-r--r-- | sys/kern/subr_pcpu.c | 2 | ||||
-rw-r--r-- | sys/sys/counter.h | 13 | ||||
-rw-r--r-- | sys/sys/kernel.h | 3 |
5 files changed, 52 insertions, 3 deletions
diff --git a/share/man/man9/counter.9 b/share/man/man9/counter.9 index 9bdb5320b71b..1eb36b571249 100644 --- a/share/man/man9/counter.9 +++ b/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 @@ If the limit was reached on previous second, but was just reset back to zero, 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_array, CTLFLAG_RW, .Xr malloc 9 , .Xr ratecheck 9 , .Xr sysctl 9 , +.Xr SYSINIT 9 , .Xr uma 9 .Sh HISTORY The diff --git a/sys/kern/subr_counter.c b/sys/kern/subr_counter.c index 8eb9f19e158c..fec51b6b635a 100644 --- a/sys/kern/subr_counter.c +++ b/sys/kern/subr_counter.c @@ -172,3 +172,21 @@ counter_ratecheck(struct counter_rate *cr, int64_t limit) 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); +} diff --git a/sys/kern/subr_pcpu.c b/sys/kern/subr_pcpu.c index 101440e3b1ec..f37b9324a919 100644 --- a/sys/kern/subr_pcpu.c +++ b/sys/kern/subr_pcpu.c @@ -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 diff --git a/sys/sys/counter.h b/sys/sys/counter.h index 9960a2c39d8e..3e1b7828d5d4 100644 --- a/sys/sys/counter.h +++ b/sys/sys/counter.h @@ -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__ */ diff --git a/sys/sys/kernel.h b/sys/sys/kernel.h index 839f26706f47..3b0e038b5c83 100644 --- a/sys/sys/kernel.h +++ b/sys/sys/kernel.h @@ -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 |