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