Home | History | Annotate | Line # | Download | only in kern
subr_evcnt.c revision 1.4.94.3
      1  1.4.94.3   matt /* $NetBSD: subr_evcnt.c,v 1.4.94.3 2014/02/15 10:13:24 matt 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.94.3   matt __KERNEL_RCSID(0, "$NetBSD: subr_evcnt.c,v 1.4.94.3 2014/02/15 10:13:24 matt Exp $");
     81       1.1    rtr 
     82       1.1    rtr #include "opt_ddb.h"
     83       1.1    rtr 
     84       1.1    rtr #include <sys/param.h>
     85       1.1    rtr #include <sys/device.h>
     86  1.4.94.1  cliff #include <sys/mutex.h>
     87       1.2   tron #include <sys/systm.h>
     88       1.1    rtr 
     89       1.1    rtr /* list of all events */
     90       1.1    rtr struct evcntlist allevents = TAILQ_HEAD_INITIALIZER(allevents);
     91  1.4.94.1  cliff static kmutex_t evcnt_lock;
     92       1.1    rtr 
     93       1.1    rtr /*
     94       1.1    rtr  * We need a dummy object to stuff into the evcnt link set to
     95       1.1    rtr  * ensure that there always is at least one object in the set.
     96       1.1    rtr  */
     97       1.1    rtr static struct evcnt dummy_static_evcnt;
     98       1.1    rtr __link_set_add_bss(evcnts, dummy_static_evcnt);
     99       1.1    rtr 
    100       1.1    rtr /*
    101       1.1    rtr  * Initialize event counters.  This does the attach procedure for
    102       1.1    rtr  * each of the static event counters in the "evcnts" link set.
    103       1.1    rtr  */
    104       1.1    rtr void
    105       1.1    rtr evcnt_init(void)
    106       1.1    rtr {
    107       1.1    rtr 	__link_set_decl(evcnts, struct evcnt);
    108       1.1    rtr 	struct evcnt * const *evp;
    109       1.1    rtr 
    110  1.4.94.1  cliff 	mutex_init(&evcnt_lock, MUTEX_DEFAULT, IPL_NONE);
    111  1.4.94.1  cliff 
    112       1.1    rtr 	__link_set_foreach(evp, evcnts) {
    113       1.1    rtr 		if (*evp == &dummy_static_evcnt)
    114       1.1    rtr 			continue;
    115       1.1    rtr 		evcnt_attach_static(*evp);
    116       1.1    rtr 	}
    117       1.1    rtr }
    118       1.1    rtr 
    119       1.1    rtr /*
    120       1.1    rtr  * Attach a statically-initialized event.  The type and string pointers
    121       1.1    rtr  * are already set up.
    122       1.1    rtr  */
    123       1.1    rtr void
    124       1.1    rtr evcnt_attach_static(struct evcnt *ev)
    125       1.1    rtr {
    126       1.1    rtr 	int len;
    127       1.1    rtr 
    128       1.1    rtr 	len = strlen(ev->ev_group);
    129       1.1    rtr #ifdef DIAGNOSTIC
    130       1.1    rtr 	if (len >= EVCNT_STRING_MAX)		/* ..._MAX includes NUL */
    131       1.1    rtr 		panic("evcnt_attach_static: group length (%s)", ev->ev_group);
    132       1.1    rtr #endif
    133       1.1    rtr 	ev->ev_grouplen = len;
    134       1.1    rtr 
    135       1.1    rtr 	len = strlen(ev->ev_name);
    136       1.1    rtr #ifdef DIAGNOSTIC
    137       1.1    rtr 	if (len >= EVCNT_STRING_MAX)		/* ..._MAX includes NUL */
    138       1.1    rtr 		panic("evcnt_attach_static: name length (%s)", ev->ev_name);
    139       1.1    rtr #endif
    140       1.1    rtr 	ev->ev_namelen = len;
    141       1.1    rtr 
    142  1.4.94.1  cliff 	mutex_enter(&evcnt_lock);
    143       1.1    rtr 	TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
    144  1.4.94.1  cliff 	mutex_exit(&evcnt_lock);
    145       1.1    rtr }
    146       1.1    rtr 
    147       1.1    rtr /*
    148       1.1    rtr  * Attach a dynamically-initialized event.  Zero it, set up the type
    149       1.1    rtr  * and string pointers and then act like it was statically initialized.
    150       1.1    rtr  */
    151       1.1    rtr void
    152       1.1    rtr evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent,
    153       1.1    rtr     const char *group, const char *name)
    154       1.1    rtr {
    155       1.1    rtr 	memset(ev, 0, sizeof *ev);
    156  1.4.94.3   matt 	evcnt_attach_dynamic(ev, type, parent, group, name);
    157  1.4.94.3   matt }
    158  1.4.94.3   matt 
    159  1.4.94.3   matt void
    160  1.4.94.3   matt evcnt_attach_dynamic_nozero(struct evcnt *ev, int type,
    161  1.4.94.3   matt     const struct evcnt *parent, const char *group, const char *name)
    162  1.4.94.3   matt {
    163  1.4.94.3   matt 
    164       1.1    rtr 	ev->ev_type = type;
    165       1.1    rtr 	ev->ev_parent = parent;
    166       1.1    rtr 	ev->ev_group = group;
    167       1.1    rtr 	ev->ev_name = name;
    168       1.1    rtr 	evcnt_attach_static(ev);
    169       1.1    rtr }
    170       1.1    rtr 
    171       1.1    rtr /*
    172       1.1    rtr  * Detach an event.
    173       1.1    rtr  */
    174       1.1    rtr void
    175       1.1    rtr evcnt_detach(struct evcnt *ev)
    176       1.1    rtr {
    177       1.1    rtr 
    178  1.4.94.1  cliff 	mutex_enter(&evcnt_lock);
    179       1.1    rtr 	TAILQ_REMOVE(&allevents, ev, ev_list);
    180  1.4.94.1  cliff 	mutex_exit(&evcnt_lock);
    181       1.1    rtr }
    182       1.1    rtr 
    183       1.1    rtr #ifdef DDB
    184       1.1    rtr void
    185       1.1    rtr event_print(int full, void (*pr)(const char *, ...))
    186       1.1    rtr {
    187       1.1    rtr 	struct evcnt *evp;
    188       1.1    rtr 
    189       1.1    rtr 	TAILQ_FOREACH(evp, &allevents, ev_list) {
    190       1.1    rtr 		if (evp->ev_count == 0 && !full)
    191       1.1    rtr 			continue;
    192       1.1    rtr 
    193       1.1    rtr 		(*pr)("evcnt type %d: %s %s = %lld\n", evp->ev_type,
    194       1.1    rtr 		    evp->ev_group, evp->ev_name, evp->ev_count);
    195       1.1    rtr 	}
    196       1.1    rtr }
    197       1.1    rtr #endif /* DDB */
    198