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
This commit is contained in:
markj 2020-03-06 19:09:01 +00:00
parent 3c58c3daec
commit f9f4e2feb3
5 changed files with 52 additions and 3 deletions

View File

@ -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

View File

@ -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);
}

View File

@ -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

View File

@ -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__ */

View File

@ -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