2005-12-26 22:40:14 +03:00
|
|
|
.\" $NetBSD: evcnt.9,v 1.14 2005/12/26 19:48:12 perry Exp $
|
2000-06-04 23:14:14 +04:00
|
|
|
.\"
|
|
|
|
.\" Copyright (c) 2000 Christopher G. Demetriou
|
|
|
|
.\" All rights reserved.
|
2001-05-07 03:48:31 +04:00
|
|
|
.\"
|
2000-06-04 23:14:14 +04:00
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\" 3. All advertising materials mentioning features or use of this software
|
|
|
|
.\" must display the following acknowledgement:
|
2000-06-14 02:36:16 +04:00
|
|
|
.\" This product includes software developed for the
|
2003-02-14 18:20:17 +03:00
|
|
|
.\" NetBSD Project. See http://www.NetBSD.org/ for
|
2000-06-14 02:36:16 +04:00
|
|
|
.\" information about NetBSD.
|
2000-06-04 23:14:14 +04:00
|
|
|
.\" 4. The name of the author may not be used to endorse or promote products
|
|
|
|
.\" derived from this software without specific prior written permission.
|
2001-05-07 03:48:31 +04:00
|
|
|
.\"
|
2000-06-04 23:14:14 +04:00
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
2000-06-14 02:36:16 +04:00
|
|
|
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2001-05-07 03:48:31 +04:00
|
|
|
.\"
|
2000-06-14 02:36:16 +04:00
|
|
|
.\" --(license Id: LICENSE.proto,v 1.1 2000/06/13 21:40:26 cgd Exp )--
|
2000-06-04 23:14:14 +04:00
|
|
|
.\"
|
2005-01-11 10:23:49 +03:00
|
|
|
.Dd January 11, 2005
|
2000-06-04 23:14:14 +04:00
|
|
|
.Dt EVCNT 9
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm evcnt ,
|
|
|
|
.Nm evcnt_attach_dynamic ,
|
|
|
|
.Nm evcnt_attach_static ,
|
|
|
|
.Nm evcnt_detach
|
|
|
|
.Nd generic event counter framework
|
|
|
|
.Sh SYNOPSIS
|
2003-04-16 17:34:34 +04:00
|
|
|
.In sys/device.h
|
2000-06-04 23:14:14 +04:00
|
|
|
.Ft void
|
|
|
|
.Fn evcnt_attach_dynamic "struct evcnt *ev" "int type" "const struct evcnt *parent" "const char *group" "const char *name"
|
|
|
|
.Ft void
|
|
|
|
.Fn evcnt_attach_static "struct evcnt *ev"
|
|
|
|
.Ft void
|
|
|
|
.Fn evcnt_detach "struct evcnt *ev"
|
|
|
|
.Sh DESCRIPTION
|
|
|
|
The
|
|
|
|
.Nx
|
|
|
|
generic event counter framework is designed to provide a flexible and
|
|
|
|
hierarchical event counting facility, which is useful for tracking
|
|
|
|
system events (including device interrupts).
|
|
|
|
.Pp
|
|
|
|
The fundamental component of this framework is the
|
|
|
|
.Nm evcnt
|
2002-10-14 17:43:14 +04:00
|
|
|
structure.
|
|
|
|
Its user-accessible fields are:
|
2000-06-04 23:14:14 +04:00
|
|
|
.Bd -literal
|
|
|
|
struct evcnt {
|
2005-12-26 22:40:14 +03:00
|
|
|
uint64_t ev_count; /* how many have occurred */
|
2000-06-04 23:14:14 +04:00
|
|
|
TAILQ_ENTRY(evcnt) ev_list; /* entry on list of all counters */
|
|
|
|
unsigned char ev_type; /* counter type; see below */
|
|
|
|
unsigned char ev_grouplen; /* 'group' len, excluding NUL */
|
|
|
|
unsigned char ev_namelen; /* 'name' len, excluding NUL */
|
|
|
|
const struct evcnt *ev_parent; /* parent, for hierarchical ctrs */
|
|
|
|
const char *ev_group; /* name of group */
|
|
|
|
const char *ev_name; /* name of specific event */
|
|
|
|
};
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
The system maintains a global linked list of all active event counters.
|
|
|
|
This list, called
|
|
|
|
.Nm allevents ,
|
|
|
|
may grow or shrink over time as event counters are dynamically
|
|
|
|
added to and removed from the system.
|
|
|
|
.Pp
|
|
|
|
Each event counter is marked (in the
|
|
|
|
.Fa ev_type
|
|
|
|
field) with the type of event being counted.
|
|
|
|
The following types are currently defined:
|
|
|
|
.Bl -tag -offset indent -width EVCNT_TYPE_MISC -compact
|
|
|
|
.It Ev EVCNT_TYPE_MISC
|
|
|
|
Miscellaneous; doesn't fit into one of the other types.
|
|
|
|
.It Ev EVCNT_TYPE_INTR
|
|
|
|
Interrupt counter, reported by
|
|
|
|
.Ic vmstat -i .
|
2005-01-11 10:23:49 +03:00
|
|
|
.It Ev EVCNT_TYPE_TRAP
|
|
|
|
Processor trap style events.
|
2000-06-04 23:14:14 +04:00
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
Each event counter also has a group name
|
|
|
|
.Pq Fa ev_group
|
|
|
|
and
|
|
|
|
an event name
|
|
|
|
.Pq Fa ev_name
|
2002-10-14 17:43:14 +04:00
|
|
|
which are used to identify the counter.
|
|
|
|
The group name may be shared by a set of counters.
|
|
|
|
For example, device interrupt counters would use the name of the
|
|
|
|
device whose interrupts are being counted as the group name.
|
2000-06-04 23:14:14 +04:00
|
|
|
The counter
|
|
|
|
name is meant to distinguish the counter from others in its group
|
|
|
|
(and need not be unique across groups).
|
|
|
|
Both names should be understandable by users, since they are printed
|
|
|
|
by commands like
|
2001-05-07 03:48:31 +04:00
|
|
|
.Xr vmstat 1 .
|
2000-06-04 23:14:14 +04:00
|
|
|
The constant
|
|
|
|
.Dv EVCNT_STRING_MAX
|
|
|
|
is defined to be the maximum group or event name length in
|
|
|
|
bytes (including the trailing
|
|
|
|
.Dv NUL ) .
|
|
|
|
In the current implementation it is 256.
|
|
|
|
.Pp
|
|
|
|
To support hierarchical tracking of events, each event counter can
|
|
|
|
name a
|
|
|
|
.Dq parent
|
2002-10-14 17:43:14 +04:00
|
|
|
event counter.
|
|
|
|
For instance, interrupt dispatch code could have an event counter per
|
|
|
|
interrupt line, and devices could each have counters for the number
|
|
|
|
of interrupts that they were responsible for causing.
|
|
|
|
In that case, the counter for a device on a given interrupt line
|
2000-06-04 23:14:14 +04:00
|
|
|
would have the line's counter as its parent.
|
|
|
|
The value
|
|
|
|
.Dv NULL
|
2004-06-05 19:05:02 +04:00
|
|
|
is used to indicate that a counter has no parent.
|
2000-06-04 23:14:14 +04:00
|
|
|
A counter's parent must be attached before the counter is attached,
|
|
|
|
and detached after the counter is detached.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn EVCNT_INITIALIZER
|
|
|
|
macro can be used to provide a static initializer for an event
|
2002-10-14 17:43:14 +04:00
|
|
|
counter structure.
|
|
|
|
It is be invoked as
|
2000-06-04 23:14:14 +04:00
|
|
|
.Fn EVCNT_INITIALIZER "type" "parent" "group" "name" ,
|
|
|
|
and its arguments will be placed into the corresponding fields of
|
2002-10-14 17:43:14 +04:00
|
|
|
the event counter structure it is initializing.
|
|
|
|
The
|
2000-06-04 23:14:14 +04:00
|
|
|
.Fa group
|
|
|
|
and
|
|
|
|
.Fa name
|
|
|
|
arguments must be constant strings.
|
|
|
|
.Pp
|
|
|
|
The following is a brief description of each function in the framework:
|
|
|
|
.Bl -tag -width indent
|
|
|
|
.It Fn "void evcnt_attach_dynamic" "struct evcnt *ev" "int type" "const struct evcnt *parent" "const char *group" "const char *name"
|
|
|
|
.Pp
|
|
|
|
Attach the event counter structure pointed to by
|
|
|
|
.Fa ev
|
2002-10-14 17:43:14 +04:00
|
|
|
to the system event list.
|
|
|
|
The event counter is cleared and its fields initialized using the
|
|
|
|
arguments to the function call.
|
2000-06-04 23:14:14 +04:00
|
|
|
The contents of the remaining elements in the structure (e.g., the
|
|
|
|
name lengths) are calculated, and the counter is added to the
|
|
|
|
system event list.
|
|
|
|
.Pp
|
|
|
|
The strings specified as the group and
|
|
|
|
counter names must persist (with the same value)
|
|
|
|
throughout the life of the event counter; they are referenced by,
|
|
|
|
not copied into, the counter.
|
|
|
|
.It Fn "void evcnt_attach_static" "struct evcnt *ev"
|
|
|
|
.Pp
|
|
|
|
Attach the statically-initialized event counter structure
|
|
|
|
pointed to by
|
|
|
|
.Fa ev
|
2002-10-14 17:43:14 +04:00
|
|
|
to the system event list.
|
|
|
|
The event counter is assumed to be statically initialized using the
|
2000-06-04 23:14:14 +04:00
|
|
|
.Fn EVCNT_INITIALIZER
|
2002-10-14 17:43:14 +04:00
|
|
|
macro.
|
|
|
|
This function simply calculates structure elements' values as appropriate
|
|
|
|
(e.g., the string lengths), and adds the counter to the system event list.
|
2000-06-04 23:14:14 +04:00
|
|
|
.It Fn "void evcnt_detach" "struct evcnt *ev"
|
|
|
|
.Pp
|
|
|
|
Detach the event counter structure pointed to by
|
|
|
|
.Fa ev
|
|
|
|
from the system event list.
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
Note that no method is provided to increment the value of an
|
2002-10-14 17:43:14 +04:00
|
|
|
event counter.
|
|
|
|
Code incrementing an event counter should do so by directly accessing its
|
2000-06-04 23:14:14 +04:00
|
|
|
.Fa ev_count
|
2002-10-14 17:43:14 +04:00
|
|
|
field in a manner that is known to be safe.
|
|
|
|
For instance, additions to a device's event counters in the interrupt
|
|
|
|
handler for that device will often be safe without additional protection
|
2000-06-04 23:14:14 +04:00
|
|
|
(because interrupt handler entries for a given device have to be
|
|
|
|
serialized).
|
|
|
|
However, for other uses of event counters, additional locking
|
2001-05-07 03:48:31 +04:00
|
|
|
or use of machine-dependent atomic operation may be appropriate.
|
2000-06-04 23:14:14 +04:00
|
|
|
(The overhead of using a mechanism that is guaranteed to
|
|
|
|
be safe to increment every counter, regardless of actual need
|
|
|
|
for such a mechanism where the counter is being incremented,
|
2002-10-14 17:43:14 +04:00
|
|
|
would be too great.
|
|
|
|
On some systems, it might involve a global lock and several function calls.)
|
2000-06-04 23:14:14 +04:00
|
|
|
.Sh USING THE FRAMEWORK
|
|
|
|
This section includes a description on basic use of the framework
|
|
|
|
and example usage of its functions.
|
|
|
|
.Pp
|
|
|
|
Device drivers can use the
|
|
|
|
.Fn evcnt_attach_dynamic
|
|
|
|
and
|
|
|
|
.Fn evcnt_detach
|
|
|
|
functions to manage device-specific event counters.
|
|
|
|
Statically configured system modules can use
|
|
|
|
.Fn evcnt_attach_static
|
2002-10-14 17:43:14 +04:00
|
|
|
to configure global event counters.
|
|
|
|
Similarly, loadable modules can use
|
2000-06-04 23:14:14 +04:00
|
|
|
.Fn evcnt_attach_static
|
|
|
|
to configure their global event counters,
|
|
|
|
.Fn evcnt_attach_dynamic
|
|
|
|
to attach device-specific event
|
|
|
|
counters, and
|
|
|
|
.Fn evcnt_detach
|
|
|
|
to detach all counters when being unloaded.
|
|
|
|
.Pp
|
|
|
|
Device drivers that wish to use the generic event counter
|
|
|
|
framework should place event counter structures in their
|
|
|
|
.Dq softc
|
2002-10-14 17:43:14 +04:00
|
|
|
structures.
|
|
|
|
For example, to keep track of the number of interrupts for a given
|
|
|
|
device (broken down further into
|
2000-06-04 23:14:14 +04:00
|
|
|
.Dq device readable
|
|
|
|
and
|
|
|
|
.Dq device writable
|
|
|
|
interrupts) a device driver might use:
|
|
|
|
.Bd -literal
|
|
|
|
struct foo_softc {
|
|
|
|
struct device sc_dev; /* generic device information */
|
|
|
|
[ . . . ]
|
|
|
|
struct evcnt sc_ev_intr; /* interrupt count */
|
|
|
|
struct evcnt sc_ev_intr_rd; /* 'readable' interrupt count */
|
|
|
|
struct evcnt sc_ev_intr_wr; /* 'writable' interrupt count */
|
|
|
|
[ . . . ]
|
|
|
|
};
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
In the device attach function, those counters would be registered with
|
|
|
|
the system using the
|
|
|
|
.Fn evcnt_attach_dynamic
|
|
|
|
function, using code like:
|
|
|
|
.Bd -literal
|
|
|
|
void
|
|
|
|
fooattach(parent, self, aux)
|
|
|
|
struct device *parent, *self;
|
|
|
|
void *aux;
|
|
|
|
{
|
|
|
|
struct foo_softc *sc = (struct foo_softc *)self;
|
|
|
|
|
|
|
|
[ . . . ]
|
|
|
|
|
|
|
|
/* Initialize and attach event counters. */
|
2002-02-13 11:17:26 +03:00
|
|
|
evcnt_attach_dynamic(\*[Am]sc-\*[Gt]sc_ev, EVCNT_TYPE_INTR,
|
|
|
|
NULL, sc-\*[Gt]sc_dev.dv_xname, "intr");
|
|
|
|
evcnt_attach_dynamic(\*[Am]sc-\*[Gt]sc_ev_rd, EVCNT_TYPE_INTR,
|
|
|
|
\*[Am]sc-\*[Gt]sc_ev, sc-\*[Gt]sc_dev.dv_xname, "intr rd");
|
|
|
|
evcnt_attach_dynamic(\*[Am]sc-\*[Gt]sc_ev_wr, EVCNT_TYPE_INTR,
|
|
|
|
\*[Am]sc-\*[Gt]sc_ev, sc-\*[Gt]sc_dev.dv_xname, "intr wr");
|
2000-06-04 23:14:14 +04:00
|
|
|
|
|
|
|
[ . . . ]
|
|
|
|
}
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
If the device can be detached from the system, its detach
|
|
|
|
function should invoke
|
|
|
|
.Fn evcnt_detach
|
|
|
|
on each attached counter (making sure to detach any
|
|
|
|
.Dq parent
|
|
|
|
counters only after detaching all children).
|
|
|
|
.Pp
|
|
|
|
Code like the following might be used to initialize a static
|
|
|
|
event counter (in this example, one used to track CPU alignment traps):
|
|
|
|
.Bd -literal
|
|
|
|
struct evcnt aligntrap_ev = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
|
|
|
|
NULL, "cpu", "aligntrap")
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
To attach this event counter, code like the following could be used:
|
|
|
|
.Bd -literal
|
2002-02-13 11:17:26 +03:00
|
|
|
evcnt_attach_static(\*[Am]aligntrap_ev);
|
2000-06-04 23:14:14 +04:00
|
|
|
.Ed
|
|
|
|
.Sh CODE REFERENCES
|
|
|
|
This section describes places within the
|
|
|
|
.Nx
|
|
|
|
source tree where actual
|
2003-02-05 01:38:15 +03:00
|
|
|
code implementing or using the event counter framework can be found.
|
2000-06-04 23:14:14 +04:00
|
|
|
All pathnames are relative to
|
|
|
|
.Pa /usr/src .
|
|
|
|
.Pp
|
|
|
|
The event counter framework itself is implemented within the file
|
2004-02-18 11:54:30 +03:00
|
|
|
.Pa sys/kern/subr_evcnt.c .
|
2000-06-04 23:14:14 +04:00
|
|
|
Data structures and function prototypes for the framework are located in
|
|
|
|
.Pa sys/sys/device.h .
|
|
|
|
.Pp
|
|
|
|
Event counters are used throughout the system.
|
|
|
|
.Pp
|
|
|
|
The
|
2001-05-07 03:48:31 +04:00
|
|
|
.Xr vmstat 1
|
2000-06-04 23:14:14 +04:00
|
|
|
source file
|
|
|
|
.Pa usr.bin/vmstat/vmstat.c
|
|
|
|
shows an example of how to access event counters from user programs.
|
|
|
|
.Sh SEE ALSO
|
2001-05-07 03:48:31 +04:00
|
|
|
.Xr vmstat 1
|
2000-06-04 23:14:14 +04:00
|
|
|
.Sh HISTORY
|
|
|
|
A set of interrupt counter interfaces with similar names to the interfaces
|
|
|
|
in the
|
|
|
|
.Nx
|
|
|
|
generic event counter framework appeared as part
|
|
|
|
of the new autoconfiguration system in
|
|
|
|
.Bx 4.4 .
|
|
|
|
Those interfaces were never widely adopted in
|
|
|
|
.Nx
|
|
|
|
because of limitations in their applicability.
|
|
|
|
(Their use was limited to non-hierarchical, dynamically
|
|
|
|
attached device interrupt counters.)
|
|
|
|
The
|
|
|
|
.Nx
|
|
|
|
generic event counter framework first appeared in
|
|
|
|
.Nx 1.5 .
|
2001-12-26 04:07:45 +03:00
|
|
|
.Sh AUTHORS
|
|
|
|
The
|
|
|
|
.Nx
|
|
|
|
generic event counter framework was designed and implemented by
|
2003-02-14 18:20:17 +03:00
|
|
|
.An Chris Demetriou
|
|
|
|
.Aq cgd@NetBSD.org .
|