subr_evcnt.c revision 1.16 1 1.16 rin /* $NetBSD: subr_evcnt.c,v 1.16 2021/04/15 00:37:31 rin 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.16 rin __KERNEL_RCSID(0, "$NetBSD: subr_evcnt.c,v 1.16 2021/04/15 00:37:31 rin 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.14 simonb /*
90 1.14 simonb * Everything related to __HAVE_LEGACY_INTRCNT can disappear once
91 1.14 simonb * no more ports are using old-style intrcnt/intrnames interrupt
92 1.14 simonb * accounting. The follow files have __HAVE_LEGACY_INTRCNT code:
93 1.14 simonb *
94 1.14 simonb * sys/kern/init_main.c
95 1.14 simonb * sys/kern/subr_evcnt.c
96 1.14 simonb * sys/sys/evcnt.h
97 1.14 simonb * sys/arch/<port>/include/types.h
98 1.14 simonb */
99 1.14 simonb #ifdef _RUMPKERNEL
100 1.14 simonb /* RUMP doesn't need/want to know about intrcnts */
101 1.14 simonb #undef __HAVE_LEGACY_INTRCNT
102 1.14 simonb #endif
103 1.14 simonb
104 1.14 simonb #ifdef __HAVE_LEGACY_INTRCNT
105 1.14 simonb static void evcnt_update_intrcnt(void);
106 1.14 simonb #endif
107 1.14 simonb
108 1.1 rtr /* list of all events */
109 1.1 rtr struct evcntlist allevents = TAILQ_HEAD_INITIALIZER(allevents);
110 1.9 matt static kmutex_t evcnt_lock __cacheline_aligned;
111 1.8 matt static bool init_done;
112 1.9 matt static uint32_t evcnt_generation;
113 1.1 rtr
114 1.1 rtr /*
115 1.1 rtr * We need a dummy object to stuff into the evcnt link set to
116 1.1 rtr * ensure that there always is at least one object in the set.
117 1.1 rtr */
118 1.1 rtr static struct evcnt dummy_static_evcnt;
119 1.1 rtr __link_set_add_bss(evcnts, dummy_static_evcnt);
120 1.1 rtr
121 1.1 rtr /*
122 1.1 rtr * Initialize event counters. This does the attach procedure for
123 1.1 rtr * each of the static event counters in the "evcnts" link set.
124 1.1 rtr */
125 1.1 rtr void
126 1.1 rtr evcnt_init(void)
127 1.1 rtr {
128 1.1 rtr __link_set_decl(evcnts, struct evcnt);
129 1.1 rtr struct evcnt * const *evp;
130 1.1 rtr
131 1.8 matt KASSERT(!init_done);
132 1.8 matt
133 1.9 matt mutex_init(&evcnt_lock, MUTEX_DEFAULT, IPL_NONE);
134 1.6 pooka
135 1.8 matt init_done = true;
136 1.8 matt
137 1.1 rtr __link_set_foreach(evp, evcnts) {
138 1.1 rtr if (*evp == &dummy_static_evcnt)
139 1.1 rtr continue;
140 1.1 rtr evcnt_attach_static(*evp);
141 1.1 rtr }
142 1.1 rtr }
143 1.1 rtr
144 1.1 rtr /*
145 1.1 rtr * Attach a statically-initialized event. The type and string pointers
146 1.1 rtr * are already set up.
147 1.1 rtr */
148 1.1 rtr void
149 1.1 rtr evcnt_attach_static(struct evcnt *ev)
150 1.1 rtr {
151 1.1 rtr int len;
152 1.1 rtr
153 1.8 matt KASSERTMSG(init_done,
154 1.11 jym "%s: evcnt non initialized: group=<%s> name=<%s>",
155 1.11 jym __func__, ev->ev_group, ev->ev_name);
156 1.8 matt
157 1.1 rtr len = strlen(ev->ev_group);
158 1.1 rtr #ifdef DIAGNOSTIC
159 1.10 christos if (len == 0 || len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
160 1.1 rtr panic("evcnt_attach_static: group length (%s)", ev->ev_group);
161 1.1 rtr #endif
162 1.1 rtr ev->ev_grouplen = len;
163 1.1 rtr
164 1.1 rtr len = strlen(ev->ev_name);
165 1.1 rtr #ifdef DIAGNOSTIC
166 1.10 christos if (len == 0 || len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
167 1.1 rtr panic("evcnt_attach_static: name length (%s)", ev->ev_name);
168 1.1 rtr #endif
169 1.1 rtr ev->ev_namelen = len;
170 1.1 rtr
171 1.9 matt mutex_enter(&evcnt_lock);
172 1.1 rtr TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
173 1.9 matt mutex_exit(&evcnt_lock);
174 1.1 rtr }
175 1.1 rtr
176 1.1 rtr /*
177 1.1 rtr * Attach a dynamically-initialized event. Zero it, set up the type
178 1.1 rtr * and string pointers and then act like it was statically initialized.
179 1.1 rtr */
180 1.1 rtr void
181 1.7 matt evcnt_attach_dynamic_nozero(struct evcnt *ev, int type,
182 1.7 matt const struct evcnt *parent, const char *group, const char *name)
183 1.1 rtr {
184 1.1 rtr
185 1.1 rtr ev->ev_type = type;
186 1.1 rtr ev->ev_parent = parent;
187 1.1 rtr ev->ev_group = group;
188 1.1 rtr ev->ev_name = name;
189 1.1 rtr evcnt_attach_static(ev);
190 1.1 rtr }
191 1.7 matt /*
192 1.7 matt * Attach a dynamically-initialized event. Zero it, set up the type
193 1.7 matt * and string pointers and then act like it was statically initialized.
194 1.7 matt */
195 1.7 matt void
196 1.7 matt evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent,
197 1.7 matt const char *group, const char *name)
198 1.7 matt {
199 1.7 matt
200 1.7 matt memset(ev, 0, sizeof *ev);
201 1.7 matt evcnt_attach_dynamic_nozero(ev, type, parent, group, name);
202 1.7 matt }
203 1.1 rtr
204 1.1 rtr /*
205 1.1 rtr * Detach an event.
206 1.1 rtr */
207 1.1 rtr void
208 1.1 rtr evcnt_detach(struct evcnt *ev)
209 1.1 rtr {
210 1.1 rtr
211 1.9 matt mutex_enter(&evcnt_lock);
212 1.1 rtr TAILQ_REMOVE(&allevents, ev, ev_list);
213 1.9 matt evcnt_generation++;
214 1.9 matt mutex_exit(&evcnt_lock);
215 1.9 matt }
216 1.9 matt
217 1.9 matt struct xevcnt_sysctl {
218 1.9 matt struct evcnt_sysctl evs;
219 1.9 matt char ev_strings[2*EVCNT_STRING_MAX];
220 1.9 matt };
221 1.9 matt
222 1.9 matt static size_t
223 1.9 matt sysctl_fillevcnt(const struct evcnt *ev, struct xevcnt_sysctl *xevs,
224 1.9 matt size_t *copylenp)
225 1.9 matt {
226 1.13 maxv const bool allowaddr = get_expose_address(curproc);
227 1.9 matt const size_t copylen = offsetof(struct evcnt_sysctl, ev_strings)
228 1.9 matt + ev->ev_grouplen + 1 + ev->ev_namelen + 1;
229 1.9 matt const size_t len = roundup2(copylen, sizeof(uint64_t));
230 1.13 maxv
231 1.9 matt if (xevs != NULL) {
232 1.9 matt xevs->evs.ev_count = ev->ev_count;
233 1.13 maxv COND_SET_VALUE(xevs->evs.ev_addr, PTRTOUINT64(ev), allowaddr);
234 1.13 maxv COND_SET_VALUE(xevs->evs.ev_parent, PTRTOUINT64(ev->ev_parent),
235 1.13 maxv allowaddr);
236 1.9 matt xevs->evs.ev_type = ev->ev_type;
237 1.9 matt xevs->evs.ev_grouplen = ev->ev_grouplen;
238 1.9 matt xevs->evs.ev_namelen = ev->ev_namelen;
239 1.9 matt xevs->evs.ev_len = len / sizeof(uint64_t);
240 1.9 matt strcpy(xevs->evs.ev_strings, ev->ev_group);
241 1.9 matt strcpy(xevs->evs.ev_strings + ev->ev_grouplen + 1, ev->ev_name);
242 1.9 matt }
243 1.9 matt
244 1.9 matt *copylenp = copylen;
245 1.9 matt return len;
246 1.9 matt }
247 1.9 matt
248 1.9 matt static int
249 1.9 matt sysctl_doevcnt(SYSCTLFN_ARGS)
250 1.9 matt {
251 1.9 matt struct xevcnt_sysctl *xevs0 = NULL, *xevs;
252 1.9 matt const struct evcnt *ev;
253 1.9 matt int error;
254 1.9 matt int retries;
255 1.9 matt size_t needed, len;
256 1.9 matt char *dp;
257 1.9 matt
258 1.9 matt if (namelen == 1 && name[0] == CTL_QUERY)
259 1.9 matt return (sysctl_query(SYSCTLFN_CALL(rnode)));
260 1.9 matt
261 1.9 matt if (namelen != 2)
262 1.9 matt return (EINVAL);
263 1.9 matt
264 1.9 matt /*
265 1.9 matt * We can filter on the type of evcnt.
266 1.9 matt */
267 1.9 matt const int filter = name[0];
268 1.9 matt if (filter != EVCNT_TYPE_ANY
269 1.9 matt && filter != EVCNT_TYPE_MISC
270 1.9 matt && filter != EVCNT_TYPE_INTR
271 1.9 matt && filter != EVCNT_TYPE_TRAP)
272 1.9 matt return (EINVAL);
273 1.9 matt
274 1.9 matt const u_int count = name[1];
275 1.9 matt if (count != KERN_EVCNT_COUNT_ANY
276 1.9 matt && count != KERN_EVCNT_COUNT_NONZERO)
277 1.9 matt return (EINVAL);
278 1.9 matt
279 1.9 matt sysctl_unlock();
280 1.9 matt
281 1.9 matt if (oldp != NULL && xevs0 == NULL)
282 1.13 maxv xevs0 = kmem_zalloc(sizeof(*xevs0), KM_SLEEP);
283 1.9 matt
284 1.9 matt retries = 100;
285 1.9 matt retry:
286 1.9 matt dp = oldp;
287 1.9 matt len = (oldp != NULL) ? *oldlenp : 0;
288 1.9 matt xevs = xevs0;
289 1.9 matt error = 0;
290 1.9 matt needed = 0;
291 1.9 matt
292 1.9 matt mutex_enter(&evcnt_lock);
293 1.14 simonb #ifdef __HAVE_LEGACY_INTRCNT
294 1.14 simonb evcnt_update_intrcnt();
295 1.14 simonb #endif
296 1.9 matt TAILQ_FOREACH(ev, &allevents, ev_list) {
297 1.9 matt if (filter != EVCNT_TYPE_ANY && filter != ev->ev_type)
298 1.9 matt continue;
299 1.9 matt if (count == KERN_EVCNT_COUNT_NONZERO && ev->ev_count == 0)
300 1.9 matt continue;
301 1.9 matt
302 1.9 matt /*
303 1.9 matt * Prepare to copy. If xevs is NULL, fillevcnt will just
304 1.9 matt * how big the item is.
305 1.9 matt */
306 1.9 matt size_t copylen;
307 1.9 matt const size_t elem_size = sysctl_fillevcnt(ev, xevs, ©len);
308 1.9 matt needed += elem_size;
309 1.9 matt
310 1.9 matt if (len < elem_size) {
311 1.9 matt xevs = NULL;
312 1.9 matt continue;
313 1.9 matt }
314 1.9 matt
315 1.9 matt KASSERT(xevs != NULL);
316 1.9 matt KASSERT(xevs->evs.ev_grouplen != 0);
317 1.9 matt KASSERT(xevs->evs.ev_namelen != 0);
318 1.9 matt KASSERT(xevs->evs.ev_strings[0] != 0);
319 1.9 matt
320 1.9 matt const uint32_t last_generation = evcnt_generation;
321 1.9 matt mutex_exit(&evcnt_lock);
322 1.9 matt
323 1.9 matt /*
324 1.9 matt * Only copy the actual number of bytes, not the rounded
325 1.9 matt * number. If we did the latter we'd have to zero them
326 1.9 matt * first or we'd leak random kernel memory.
327 1.9 matt */
328 1.9 matt error = copyout(xevs, dp, copylen);
329 1.9 matt
330 1.9 matt mutex_enter(&evcnt_lock);
331 1.9 matt if (error)
332 1.9 matt break;
333 1.9 matt
334 1.9 matt if (__predict_false(last_generation != evcnt_generation)) {
335 1.9 matt /*
336 1.9 matt * This sysctl node is only for statistics.
337 1.9 matt * Retry; if the queue keeps changing, then
338 1.9 matt * bail out.
339 1.9 matt */
340 1.9 matt if (--retries == 0) {
341 1.9 matt error = EAGAIN;
342 1.9 matt break;
343 1.9 matt }
344 1.9 matt mutex_exit(&evcnt_lock);
345 1.9 matt goto retry;
346 1.9 matt }
347 1.9 matt
348 1.9 matt /*
349 1.9 matt * Now we deal with the pointer/len since we aren't going to
350 1.9 matt * toss their values away.
351 1.9 matt */
352 1.9 matt dp += elem_size;
353 1.9 matt len -= elem_size;
354 1.9 matt }
355 1.9 matt mutex_exit(&evcnt_lock);
356 1.9 matt
357 1.9 matt if (xevs0 != NULL)
358 1.9 matt kmem_free(xevs0, sizeof(*xevs0));
359 1.9 matt
360 1.9 matt sysctl_relock();
361 1.9 matt
362 1.9 matt *oldlenp = needed;
363 1.9 matt if (oldp == NULL)
364 1.9 matt *oldlenp += 1024;
365 1.9 matt
366 1.9 matt return (error);
367 1.9 matt }
368 1.9 matt
369 1.9 matt
370 1.9 matt
371 1.9 matt SYSCTL_SETUP(sysctl_evcnt_setup, "sysctl kern.evcnt subtree setup")
372 1.9 matt {
373 1.12 pooka
374 1.9 matt sysctl_createv(clog, 0, NULL, NULL,
375 1.9 matt CTLFLAG_PERMANENT,
376 1.9 matt CTLTYPE_STRUCT, "evcnt",
377 1.9 matt SYSCTL_DESCR("Kernel evcnt information"),
378 1.9 matt sysctl_doevcnt, 0, NULL, 0,
379 1.9 matt CTL_KERN, KERN_EVCNT, CTL_EOL);
380 1.1 rtr }
381 1.14 simonb
382 1.14 simonb #ifdef __HAVE_LEGACY_INTRCNT
383 1.15 simonb extern u_int intrcnt[], eintrcnt[];
384 1.14 simonb extern char intrnames[];
385 1.14 simonb static size_t nintr;
386 1.14 simonb struct evcnt *intr_evcnts;
387 1.14 simonb /*
388 1.14 simonb * Remove the following when the last intrcnt/intrnames user is cleaned up.
389 1.14 simonb */
390 1.14 simonb void
391 1.14 simonb evcnt_attach_legacy_intrcnt(void)
392 1.14 simonb {
393 1.14 simonb size_t i;
394 1.14 simonb const char *cp;
395 1.14 simonb
396 1.16 rin nintr = ((intptr_t)eintrcnt - (intptr_t)intrcnt) / sizeof(intrcnt[0]);
397 1.14 simonb intr_evcnts = kmem_alloc(sizeof(struct evcnt) * nintr, KM_SLEEP);
398 1.14 simonb for (cp = intrnames, i = 0; i < nintr; i++) {
399 1.14 simonb evcnt_attach_dynamic(&intr_evcnts[i], EVCNT_TYPE_INTR,
400 1.14 simonb NULL, "cpu", cp);
401 1.14 simonb cp += strlen(cp) + 1;
402 1.14 simonb }
403 1.14 simonb }
404 1.14 simonb
405 1.14 simonb static void
406 1.14 simonb evcnt_update_intrcnt(void)
407 1.14 simonb {
408 1.14 simonb size_t i;
409 1.14 simonb
410 1.14 simonb KASSERT(nintr > 0);
411 1.14 simonb KASSERT(intr_evcnts != NULL);
412 1.14 simonb
413 1.14 simonb for (i = 0; i < nintr; i++) {
414 1.14 simonb intr_evcnts[i].ev_count = intrcnt[i];
415 1.14 simonb }
416 1.14 simonb }
417 1.14 simonb #endif
418