Home | History | Annotate | Line # | Download | only in kern
subr_evcnt.c revision 1.4.94.3
      1 /* $NetBSD: subr_evcnt.c,v 1.4.94.3 2014/02/15 10:13:24 matt Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996, 2000 Christopher G. Demetriou
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *          This product includes software developed for the
     18  *          NetBSD Project.  See http://www.NetBSD.org/ for
     19  *          information about NetBSD.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  *
     34  * --(license Id: LICENSE.proto,v 1.1 2000/06/13 21:40:26 cgd Exp )--
     35  */
     36 
     37 /*
     38  * Copyright (c) 1992, 1993
     39  *	The Regents of the University of California.  All rights reserved.
     40  *
     41  * This software was developed by the Computer Systems Engineering group
     42  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     43  * contributed to Berkeley.
     44  *
     45  * All advertising materials mentioning features or use of this software
     46  * must display the following acknowledgement:
     47  *	This product includes software developed by the University of
     48  *	California, Lawrence Berkeley Laboratories.
     49  *
     50  * Redistribution and use in source and binary forms, with or without
     51  * modification, are permitted provided that the following conditions
     52  * are met:
     53  * 1. Redistributions of source code must retain the above copyright
     54  *    notice, this list of conditions and the following disclaimer.
     55  * 2. Redistributions in binary form must reproduce the above copyright
     56  *    notice, this list of conditions and the following disclaimer in the
     57  *    documentation and/or other materials provided with the distribution.
     58  * 3. Neither the name of the University nor the names of its contributors
     59  *    may be used to endorse or promote products derived from this software
     60  *    without specific prior written permission.
     61  *
     62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72  * SUCH DAMAGE.
     73  *
     74  * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp  (LBL)
     75  *
     76  *	@(#)subr_autoconf.c	8.3 (Berkeley) 5/17/94
     77  */
     78 
     79 #include <sys/cdefs.h>
     80 __KERNEL_RCSID(0, "$NetBSD: subr_evcnt.c,v 1.4.94.3 2014/02/15 10:13:24 matt Exp $");
     81 
     82 #include "opt_ddb.h"
     83 
     84 #include <sys/param.h>
     85 #include <sys/device.h>
     86 #include <sys/mutex.h>
     87 #include <sys/systm.h>
     88 
     89 /* list of all events */
     90 struct evcntlist allevents = TAILQ_HEAD_INITIALIZER(allevents);
     91 static kmutex_t evcnt_lock;
     92 
     93 /*
     94  * We need a dummy object to stuff into the evcnt link set to
     95  * ensure that there always is at least one object in the set.
     96  */
     97 static struct evcnt dummy_static_evcnt;
     98 __link_set_add_bss(evcnts, dummy_static_evcnt);
     99 
    100 /*
    101  * Initialize event counters.  This does the attach procedure for
    102  * each of the static event counters in the "evcnts" link set.
    103  */
    104 void
    105 evcnt_init(void)
    106 {
    107 	__link_set_decl(evcnts, struct evcnt);
    108 	struct evcnt * const *evp;
    109 
    110 	mutex_init(&evcnt_lock, MUTEX_DEFAULT, IPL_NONE);
    111 
    112 	__link_set_foreach(evp, evcnts) {
    113 		if (*evp == &dummy_static_evcnt)
    114 			continue;
    115 		evcnt_attach_static(*evp);
    116 	}
    117 }
    118 
    119 /*
    120  * Attach a statically-initialized event.  The type and string pointers
    121  * are already set up.
    122  */
    123 void
    124 evcnt_attach_static(struct evcnt *ev)
    125 {
    126 	int len;
    127 
    128 	len = strlen(ev->ev_group);
    129 #ifdef DIAGNOSTIC
    130 	if (len >= EVCNT_STRING_MAX)		/* ..._MAX includes NUL */
    131 		panic("evcnt_attach_static: group length (%s)", ev->ev_group);
    132 #endif
    133 	ev->ev_grouplen = len;
    134 
    135 	len = strlen(ev->ev_name);
    136 #ifdef DIAGNOSTIC
    137 	if (len >= EVCNT_STRING_MAX)		/* ..._MAX includes NUL */
    138 		panic("evcnt_attach_static: name length (%s)", ev->ev_name);
    139 #endif
    140 	ev->ev_namelen = len;
    141 
    142 	mutex_enter(&evcnt_lock);
    143 	TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
    144 	mutex_exit(&evcnt_lock);
    145 }
    146 
    147 /*
    148  * Attach a dynamically-initialized event.  Zero it, set up the type
    149  * and string pointers and then act like it was statically initialized.
    150  */
    151 void
    152 evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent,
    153     const char *group, const char *name)
    154 {
    155 	memset(ev, 0, sizeof *ev);
    156 	evcnt_attach_dynamic(ev, type, parent, group, name);
    157 }
    158 
    159 void
    160 evcnt_attach_dynamic_nozero(struct evcnt *ev, int type,
    161     const struct evcnt *parent, const char *group, const char *name)
    162 {
    163 
    164 	ev->ev_type = type;
    165 	ev->ev_parent = parent;
    166 	ev->ev_group = group;
    167 	ev->ev_name = name;
    168 	evcnt_attach_static(ev);
    169 }
    170 
    171 /*
    172  * Detach an event.
    173  */
    174 void
    175 evcnt_detach(struct evcnt *ev)
    176 {
    177 
    178 	mutex_enter(&evcnt_lock);
    179 	TAILQ_REMOVE(&allevents, ev, ev_list);
    180 	mutex_exit(&evcnt_lock);
    181 }
    182 
    183 #ifdef DDB
    184 void
    185 event_print(int full, void (*pr)(const char *, ...))
    186 {
    187 	struct evcnt *evp;
    188 
    189 	TAILQ_FOREACH(evp, &allevents, ev_list) {
    190 		if (evp->ev_count == 0 && !full)
    191 			continue;
    192 
    193 		(*pr)("evcnt type %d: %s %s = %lld\n", evp->ev_type,
    194 		    evp->ev_group, evp->ev_name, evp->ev_count);
    195 	}
    196 }
    197 #endif /* DDB */
    198