Home | History | Annotate | Line # | Download | only in kern
subr_evcnt.c revision 1.11.2.1
      1  1.11.2.1      yamt /* $NetBSD: subr_evcnt.c,v 1.11.2.1 2014/05/22 11:41:03 yamt 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.11.2.1      yamt __KERNEL_RCSID(0, "$NetBSD: subr_evcnt.c,v 1.11.2.1 2014/05/22 11:41:03 yamt Exp $");
     81       1.1       rtr 
     82       1.1       rtr #include <sys/param.h>
     83       1.9      matt #include <sys/evcnt.h>
     84       1.9      matt #include <sys/kmem.h>
     85       1.6     pooka #include <sys/mutex.h>
     86       1.9      matt #include <sys/sysctl.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.9      matt static kmutex_t evcnt_lock __cacheline_aligned;
     92       1.8      matt static bool init_done;
     93       1.9      matt static uint32_t evcnt_generation;
     94       1.1       rtr 
     95       1.1       rtr /*
     96       1.1       rtr  * We need a dummy object to stuff into the evcnt link set to
     97       1.1       rtr  * ensure that there always is at least one object in the set.
     98       1.1       rtr  */
     99       1.1       rtr static struct evcnt dummy_static_evcnt;
    100       1.1       rtr __link_set_add_bss(evcnts, dummy_static_evcnt);
    101       1.1       rtr 
    102       1.1       rtr /*
    103       1.1       rtr  * Initialize event counters.  This does the attach procedure for
    104       1.1       rtr  * each of the static event counters in the "evcnts" link set.
    105       1.1       rtr  */
    106       1.1       rtr void
    107       1.1       rtr evcnt_init(void)
    108       1.1       rtr {
    109       1.1       rtr 	__link_set_decl(evcnts, struct evcnt);
    110       1.1       rtr 	struct evcnt * const *evp;
    111       1.1       rtr 
    112       1.8      matt 	KASSERT(!init_done);
    113       1.8      matt 
    114       1.9      matt 	mutex_init(&evcnt_lock, MUTEX_DEFAULT, IPL_NONE);
    115       1.6     pooka 
    116       1.8      matt 	init_done = true;
    117       1.8      matt 
    118       1.1       rtr 	__link_set_foreach(evp, evcnts) {
    119       1.1       rtr 		if (*evp == &dummy_static_evcnt)
    120       1.1       rtr 			continue;
    121       1.1       rtr 		evcnt_attach_static(*evp);
    122       1.1       rtr 	}
    123       1.1       rtr }
    124       1.1       rtr 
    125       1.1       rtr /*
    126       1.1       rtr  * Attach a statically-initialized event.  The type and string pointers
    127       1.1       rtr  * are already set up.
    128       1.1       rtr  */
    129       1.1       rtr void
    130       1.1       rtr evcnt_attach_static(struct evcnt *ev)
    131       1.1       rtr {
    132       1.1       rtr 	int len;
    133       1.1       rtr 
    134       1.8      matt 	KASSERTMSG(init_done,
    135      1.11       jym 	    "%s: evcnt non initialized: group=<%s> name=<%s>",
    136      1.11       jym 	    __func__, ev->ev_group, ev->ev_name);
    137       1.8      matt 
    138       1.1       rtr 	len = strlen(ev->ev_group);
    139       1.1       rtr #ifdef DIAGNOSTIC
    140      1.10  christos 	if (len == 0 || len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
    141       1.1       rtr 		panic("evcnt_attach_static: group length (%s)", ev->ev_group);
    142       1.1       rtr #endif
    143       1.1       rtr 	ev->ev_grouplen = len;
    144       1.1       rtr 
    145       1.1       rtr 	len = strlen(ev->ev_name);
    146       1.1       rtr #ifdef DIAGNOSTIC
    147      1.10  christos 	if (len == 0 || len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
    148       1.1       rtr 		panic("evcnt_attach_static: name length (%s)", ev->ev_name);
    149       1.1       rtr #endif
    150       1.1       rtr 	ev->ev_namelen = len;
    151       1.1       rtr 
    152       1.9      matt 	mutex_enter(&evcnt_lock);
    153       1.1       rtr 	TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
    154       1.9      matt 	mutex_exit(&evcnt_lock);
    155       1.1       rtr }
    156       1.1       rtr 
    157       1.1       rtr /*
    158       1.1       rtr  * Attach a dynamically-initialized event.  Zero it, set up the type
    159       1.1       rtr  * and string pointers and then act like it was statically initialized.
    160       1.1       rtr  */
    161       1.1       rtr void
    162       1.7      matt evcnt_attach_dynamic_nozero(struct evcnt *ev, int type,
    163       1.7      matt     const struct evcnt *parent, const char *group, const char *name)
    164       1.1       rtr {
    165       1.1       rtr 
    166       1.1       rtr 	ev->ev_type = type;
    167       1.1       rtr 	ev->ev_parent = parent;
    168       1.1       rtr 	ev->ev_group = group;
    169       1.1       rtr 	ev->ev_name = name;
    170       1.1       rtr 	evcnt_attach_static(ev);
    171       1.1       rtr }
    172       1.7      matt /*
    173       1.7      matt  * Attach a dynamically-initialized event.  Zero it, set up the type
    174       1.7      matt  * and string pointers and then act like it was statically initialized.
    175       1.7      matt  */
    176       1.7      matt void
    177       1.7      matt evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent,
    178       1.7      matt     const char *group, const char *name)
    179       1.7      matt {
    180       1.7      matt 
    181       1.7      matt 	memset(ev, 0, sizeof *ev);
    182       1.7      matt 	evcnt_attach_dynamic_nozero(ev, type, parent, group, name);
    183       1.7      matt }
    184       1.1       rtr 
    185       1.1       rtr /*
    186       1.1       rtr  * Detach an event.
    187       1.1       rtr  */
    188       1.1       rtr void
    189       1.1       rtr evcnt_detach(struct evcnt *ev)
    190       1.1       rtr {
    191       1.1       rtr 
    192       1.9      matt 	mutex_enter(&evcnt_lock);
    193       1.1       rtr 	TAILQ_REMOVE(&allevents, ev, ev_list);
    194       1.9      matt 	evcnt_generation++;
    195       1.9      matt 	mutex_exit(&evcnt_lock);
    196       1.9      matt }
    197       1.9      matt 
    198       1.9      matt struct xevcnt_sysctl {
    199       1.9      matt 	struct evcnt_sysctl evs;
    200       1.9      matt 	char ev_strings[2*EVCNT_STRING_MAX];
    201       1.9      matt };
    202       1.9      matt 
    203       1.9      matt static size_t
    204       1.9      matt sysctl_fillevcnt(const struct evcnt *ev, struct xevcnt_sysctl *xevs,
    205       1.9      matt 	size_t *copylenp)
    206       1.9      matt {
    207       1.9      matt 	const size_t copylen = offsetof(struct evcnt_sysctl, ev_strings)
    208       1.9      matt 	    + ev->ev_grouplen + 1 + ev->ev_namelen + 1;
    209       1.9      matt 	const size_t len = roundup2(copylen, sizeof(uint64_t));
    210       1.9      matt 	if (xevs != NULL) {
    211       1.9      matt 		xevs->evs.ev_count = ev->ev_count;
    212       1.9      matt 		xevs->evs.ev_addr = PTRTOUINT64(ev);
    213       1.9      matt 		xevs->evs.ev_parent = PTRTOUINT64(ev->ev_parent);
    214       1.9      matt 		xevs->evs.ev_type = ev->ev_type;
    215       1.9      matt 		xevs->evs.ev_grouplen = ev->ev_grouplen;
    216       1.9      matt 		xevs->evs.ev_namelen = ev->ev_namelen;
    217       1.9      matt 		xevs->evs.ev_len = len / sizeof(uint64_t);
    218       1.9      matt 		strcpy(xevs->evs.ev_strings, ev->ev_group);
    219       1.9      matt 		strcpy(xevs->evs.ev_strings + ev->ev_grouplen + 1, ev->ev_name);
    220       1.9      matt 	}
    221       1.9      matt 
    222       1.9      matt 	*copylenp = copylen;
    223       1.9      matt 	return len;
    224       1.9      matt }
    225       1.9      matt 
    226       1.9      matt static int
    227       1.9      matt sysctl_doevcnt(SYSCTLFN_ARGS)
    228       1.9      matt {
    229       1.9      matt 	struct xevcnt_sysctl *xevs0 = NULL, *xevs;
    230       1.9      matt 	const struct evcnt *ev;
    231       1.9      matt 	int error;
    232       1.9      matt 	int retries;
    233       1.9      matt 	size_t needed, len;
    234       1.9      matt 	char *dp;
    235       1.9      matt 
    236       1.9      matt         if (namelen == 1 && name[0] == CTL_QUERY)
    237       1.9      matt                 return (sysctl_query(SYSCTLFN_CALL(rnode)));
    238       1.9      matt 
    239       1.9      matt 	if (namelen != 2)
    240       1.9      matt 		return (EINVAL);
    241       1.9      matt 
    242       1.9      matt 	/*
    243       1.9      matt 	 * We can filter on the type of evcnt.
    244       1.9      matt 	 */
    245       1.9      matt 	const int filter = name[0];
    246       1.9      matt 	if (filter != EVCNT_TYPE_ANY
    247       1.9      matt 	    && filter != EVCNT_TYPE_MISC
    248       1.9      matt 	    && filter != EVCNT_TYPE_INTR
    249       1.9      matt 	    && filter != EVCNT_TYPE_TRAP)
    250       1.9      matt 		return (EINVAL);
    251       1.9      matt 
    252       1.9      matt 	const u_int count = name[1];
    253       1.9      matt 	if (count != KERN_EVCNT_COUNT_ANY
    254       1.9      matt 	    && count != KERN_EVCNT_COUNT_NONZERO)
    255       1.9      matt 		return (EINVAL);
    256       1.9      matt 
    257       1.9      matt 	sysctl_unlock();
    258       1.9      matt 
    259       1.9      matt 	if (oldp != NULL && xevs0 == NULL)
    260       1.9      matt 		xevs0 = kmem_alloc(sizeof(*xevs0), KM_SLEEP);
    261       1.9      matt 
    262       1.9      matt 	retries = 100;
    263       1.9      matt  retry:
    264       1.9      matt 	dp = oldp;
    265       1.9      matt 	len = (oldp != NULL) ? *oldlenp : 0;
    266       1.9      matt 	xevs = xevs0;
    267       1.9      matt 	error = 0;
    268       1.9      matt 	needed = 0;
    269       1.9      matt 
    270       1.9      matt 	mutex_enter(&evcnt_lock);
    271       1.9      matt 	TAILQ_FOREACH(ev, &allevents, ev_list) {
    272       1.9      matt 		if (filter != EVCNT_TYPE_ANY && filter != ev->ev_type)
    273       1.9      matt 			continue;
    274       1.9      matt 		if (count == KERN_EVCNT_COUNT_NONZERO && ev->ev_count == 0)
    275       1.9      matt 			continue;
    276       1.9      matt 
    277       1.9      matt 		/*
    278       1.9      matt 		 * Prepare to copy.  If xevs is NULL, fillevcnt will just
    279       1.9      matt 		 * how big the item is.
    280       1.9      matt 		 */
    281       1.9      matt 		size_t copylen;
    282       1.9      matt 		const size_t elem_size = sysctl_fillevcnt(ev, xevs, &copylen);
    283       1.9      matt 		needed += elem_size;
    284       1.9      matt 
    285       1.9      matt 		if (len < elem_size) {
    286       1.9      matt 			xevs = NULL;
    287       1.9      matt 			continue;
    288       1.9      matt 		}
    289       1.9      matt 
    290       1.9      matt 		KASSERT(xevs != NULL);
    291       1.9      matt 		KASSERT(xevs->evs.ev_grouplen != 0);
    292       1.9      matt 		KASSERT(xevs->evs.ev_namelen != 0);
    293       1.9      matt 		KASSERT(xevs->evs.ev_strings[0] != 0);
    294       1.9      matt 
    295       1.9      matt 		const uint32_t last_generation = evcnt_generation;
    296       1.9      matt 		mutex_exit(&evcnt_lock);
    297       1.9      matt 
    298       1.9      matt 		/*
    299       1.9      matt 		 * Only copy the actual number of bytes, not the rounded
    300       1.9      matt 		 * number.  If we did the latter we'd have to zero them
    301       1.9      matt 		 * first or we'd leak random kernel memory.
    302       1.9      matt 		 */
    303       1.9      matt 		error = copyout(xevs, dp, copylen);
    304       1.9      matt 
    305       1.9      matt 		mutex_enter(&evcnt_lock);
    306       1.9      matt 		if (error)
    307       1.9      matt 			break;
    308       1.9      matt 
    309       1.9      matt 		if (__predict_false(last_generation != evcnt_generation)) {
    310       1.9      matt 			/*
    311       1.9      matt 			 * This sysctl node is only for statistics.
    312       1.9      matt 			 * Retry; if the queue keeps changing, then
    313       1.9      matt 			 * bail out.
    314       1.9      matt 			 */
    315       1.9      matt 			if (--retries == 0) {
    316       1.9      matt 				error = EAGAIN;
    317       1.9      matt 				break;
    318       1.9      matt 			}
    319       1.9      matt 			mutex_exit(&evcnt_lock);
    320       1.9      matt 			goto retry;
    321       1.9      matt 		}
    322       1.9      matt 
    323       1.9      matt 		/*
    324       1.9      matt 		 * Now we deal with the pointer/len since we aren't going to
    325       1.9      matt 		 * toss their values away.
    326       1.9      matt 		 */
    327       1.9      matt 		dp += elem_size;
    328       1.9      matt 		len -= elem_size;
    329       1.9      matt 	}
    330       1.9      matt 	mutex_exit(&evcnt_lock);
    331       1.9      matt 
    332       1.9      matt 	if (xevs0 != NULL)
    333       1.9      matt 		kmem_free(xevs0, sizeof(*xevs0));
    334       1.9      matt 
    335       1.9      matt 	sysctl_relock();
    336       1.9      matt 
    337       1.9      matt 	*oldlenp = needed;
    338       1.9      matt 	if (oldp == NULL)
    339       1.9      matt 		*oldlenp += 1024;
    340       1.9      matt 
    341       1.9      matt 	return (error);
    342       1.9      matt }
    343       1.9      matt 
    344       1.9      matt 
    345       1.9      matt 
    346       1.9      matt SYSCTL_SETUP(sysctl_evcnt_setup, "sysctl kern.evcnt subtree setup")
    347       1.9      matt {
    348  1.11.2.1      yamt 
    349       1.9      matt 	sysctl_createv(clog, 0, NULL, NULL,
    350       1.9      matt 		       CTLFLAG_PERMANENT,
    351       1.9      matt 		       CTLTYPE_STRUCT, "evcnt",
    352       1.9      matt 		       SYSCTL_DESCR("Kernel evcnt information"),
    353       1.9      matt 		       sysctl_doevcnt, 0, NULL, 0,
    354       1.9      matt 		       CTL_KERN, KERN_EVCNT, CTL_EOL);
    355       1.1       rtr }
    356