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