subr_evcnt.c revision 1.8 1 1.8 matt /* $NetBSD: subr_evcnt.c,v 1.8 2011/01/18 08:16:43 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.8 matt __KERNEL_RCSID(0, "$NetBSD: subr_evcnt.c,v 1.8 2011/01/18 08:16:43 matt Exp $");
81 1.1 rtr
82 1.1 rtr #include <sys/param.h>
83 1.1 rtr #include <sys/device.h>
84 1.6 pooka #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.6 pooka static kmutex_t evmtx;
90 1.8 matt static bool init_done;
91 1.1 rtr
92 1.1 rtr /*
93 1.1 rtr * We need a dummy object to stuff into the evcnt link set to
94 1.1 rtr * ensure that there always is at least one object in the set.
95 1.1 rtr */
96 1.1 rtr static struct evcnt dummy_static_evcnt;
97 1.1 rtr __link_set_add_bss(evcnts, dummy_static_evcnt);
98 1.1 rtr
99 1.1 rtr /*
100 1.1 rtr * Initialize event counters. This does the attach procedure for
101 1.1 rtr * each of the static event counters in the "evcnts" link set.
102 1.1 rtr */
103 1.1 rtr void
104 1.1 rtr evcnt_init(void)
105 1.1 rtr {
106 1.1 rtr __link_set_decl(evcnts, struct evcnt);
107 1.1 rtr struct evcnt * const *evp;
108 1.1 rtr
109 1.8 matt KASSERT(!init_done);
110 1.8 matt
111 1.6 pooka mutex_init(&evmtx, MUTEX_DEFAULT, IPL_NONE);
112 1.6 pooka
113 1.8 matt init_done = true;
114 1.8 matt
115 1.1 rtr __link_set_foreach(evp, evcnts) {
116 1.1 rtr if (*evp == &dummy_static_evcnt)
117 1.1 rtr continue;
118 1.1 rtr evcnt_attach_static(*evp);
119 1.1 rtr }
120 1.1 rtr }
121 1.1 rtr
122 1.1 rtr /*
123 1.1 rtr * Attach a statically-initialized event. The type and string pointers
124 1.1 rtr * are already set up.
125 1.1 rtr */
126 1.1 rtr void
127 1.1 rtr evcnt_attach_static(struct evcnt *ev)
128 1.1 rtr {
129 1.1 rtr int len;
130 1.1 rtr
131 1.8 matt KASSERTMSG(init_done,
132 1.8 matt ("%s: evcnt non initialized: group=<%s> name=<%s>",
133 1.8 matt __func__, ev->ev_group, ev->ev_name));
134 1.8 matt
135 1.1 rtr len = strlen(ev->ev_group);
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: group length (%s)", ev->ev_group);
139 1.1 rtr #endif
140 1.1 rtr ev->ev_grouplen = len;
141 1.1 rtr
142 1.1 rtr len = strlen(ev->ev_name);
143 1.1 rtr #ifdef DIAGNOSTIC
144 1.1 rtr if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
145 1.1 rtr panic("evcnt_attach_static: name length (%s)", ev->ev_name);
146 1.1 rtr #endif
147 1.1 rtr ev->ev_namelen = len;
148 1.1 rtr
149 1.6 pooka mutex_enter(&evmtx);
150 1.1 rtr TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
151 1.6 pooka mutex_exit(&evmtx);
152 1.1 rtr }
153 1.1 rtr
154 1.1 rtr /*
155 1.1 rtr * Attach a dynamically-initialized event. Zero it, set up the type
156 1.1 rtr * and string pointers and then act like it was statically initialized.
157 1.1 rtr */
158 1.1 rtr void
159 1.7 matt evcnt_attach_dynamic_nozero(struct evcnt *ev, int type,
160 1.7 matt const struct evcnt *parent, const char *group, const char *name)
161 1.1 rtr {
162 1.1 rtr
163 1.1 rtr ev->ev_type = type;
164 1.1 rtr ev->ev_parent = parent;
165 1.1 rtr ev->ev_group = group;
166 1.1 rtr ev->ev_name = name;
167 1.1 rtr evcnt_attach_static(ev);
168 1.1 rtr }
169 1.7 matt /*
170 1.7 matt * Attach a dynamically-initialized event. Zero it, set up the type
171 1.7 matt * and string pointers and then act like it was statically initialized.
172 1.7 matt */
173 1.7 matt void
174 1.7 matt evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent,
175 1.7 matt const char *group, const char *name)
176 1.7 matt {
177 1.7 matt
178 1.7 matt memset(ev, 0, sizeof *ev);
179 1.7 matt evcnt_attach_dynamic_nozero(ev, type, parent, group, name);
180 1.7 matt }
181 1.1 rtr
182 1.1 rtr /*
183 1.1 rtr * Detach an event.
184 1.1 rtr */
185 1.1 rtr void
186 1.1 rtr evcnt_detach(struct evcnt *ev)
187 1.1 rtr {
188 1.1 rtr
189 1.6 pooka mutex_enter(&evmtx);
190 1.1 rtr TAILQ_REMOVE(&allevents, ev, ev_list);
191 1.6 pooka mutex_exit(&evmtx);
192 1.1 rtr }
193