1 1.17 mrg /* $NetBSD: subr_evcnt.c,v 1.17 2021/04/17 00:05:31 mrg 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.17 mrg __KERNEL_RCSID(0, "$NetBSD: subr_evcnt.c,v 1.17 2021/04/17 00:05:31 mrg 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.17 mrg typedef char ev_strings[2*EVCNT_STRING_MAX]; 218 1.9 matt 219 1.9 matt static size_t 220 1.17 mrg sysctl_fillevcnt(const struct evcnt *ev, struct evcnt_sysctl *evs, 221 1.9 matt size_t *copylenp) 222 1.9 matt { 223 1.13 maxv const bool allowaddr = get_expose_address(curproc); 224 1.9 matt const size_t copylen = offsetof(struct evcnt_sysctl, ev_strings) 225 1.9 matt + ev->ev_grouplen + 1 + ev->ev_namelen + 1; 226 1.9 matt const size_t len = roundup2(copylen, sizeof(uint64_t)); 227 1.13 maxv 228 1.17 mrg if (evs != NULL) { 229 1.17 mrg evs->ev_count = ev->ev_count; 230 1.17 mrg COND_SET_VALUE(evs->ev_addr, PTRTOUINT64(ev), allowaddr); 231 1.17 mrg COND_SET_VALUE(evs->ev_parent, PTRTOUINT64(ev->ev_parent), 232 1.13 maxv allowaddr); 233 1.17 mrg evs->ev_type = ev->ev_type; 234 1.17 mrg evs->ev_grouplen = ev->ev_grouplen; 235 1.17 mrg evs->ev_namelen = ev->ev_namelen; 236 1.17 mrg evs->ev_len = len / sizeof(uint64_t); 237 1.17 mrg strcpy(evs->ev_strings, ev->ev_group); 238 1.17 mrg strcpy(evs->ev_strings + ev->ev_grouplen + 1, ev->ev_name); 239 1.9 matt } 240 1.9 matt 241 1.9 matt *copylenp = copylen; 242 1.9 matt return len; 243 1.9 matt } 244 1.9 matt 245 1.9 matt static int 246 1.9 matt sysctl_doevcnt(SYSCTLFN_ARGS) 247 1.9 matt { 248 1.17 mrg struct evcnt_sysctl *evs0 = NULL, *evs; 249 1.17 mrg const size_t xevcnt_size = sizeof(*evs0) + sizeof(ev_strings); 250 1.9 matt const struct evcnt *ev; 251 1.9 matt int error; 252 1.9 matt int retries; 253 1.9 matt size_t needed, len; 254 1.9 matt char *dp; 255 1.9 matt 256 1.9 matt if (namelen == 1 && name[0] == CTL_QUERY) 257 1.9 matt return (sysctl_query(SYSCTLFN_CALL(rnode))); 258 1.9 matt 259 1.9 matt if (namelen != 2) 260 1.9 matt return (EINVAL); 261 1.9 matt 262 1.9 matt /* 263 1.9 matt * We can filter on the type of evcnt. 264 1.9 matt */ 265 1.9 matt const int filter = name[0]; 266 1.9 matt if (filter != EVCNT_TYPE_ANY 267 1.9 matt && filter != EVCNT_TYPE_MISC 268 1.9 matt && filter != EVCNT_TYPE_INTR 269 1.9 matt && filter != EVCNT_TYPE_TRAP) 270 1.9 matt return (EINVAL); 271 1.9 matt 272 1.9 matt const u_int count = name[1]; 273 1.9 matt if (count != KERN_EVCNT_COUNT_ANY 274 1.9 matt && count != KERN_EVCNT_COUNT_NONZERO) 275 1.9 matt return (EINVAL); 276 1.9 matt 277 1.9 matt sysctl_unlock(); 278 1.9 matt 279 1.17 mrg if (oldp != NULL) 280 1.17 mrg evs0 = kmem_zalloc(xevcnt_size, KM_SLEEP); 281 1.9 matt 282 1.9 matt retries = 100; 283 1.9 matt retry: 284 1.9 matt dp = oldp; 285 1.9 matt len = (oldp != NULL) ? *oldlenp : 0; 286 1.17 mrg evs = evs0; 287 1.9 matt error = 0; 288 1.9 matt needed = 0; 289 1.9 matt 290 1.9 matt mutex_enter(&evcnt_lock); 291 1.14 simonb #ifdef __HAVE_LEGACY_INTRCNT 292 1.14 simonb evcnt_update_intrcnt(); 293 1.14 simonb #endif 294 1.9 matt TAILQ_FOREACH(ev, &allevents, ev_list) { 295 1.9 matt if (filter != EVCNT_TYPE_ANY && filter != ev->ev_type) 296 1.9 matt continue; 297 1.9 matt if (count == KERN_EVCNT_COUNT_NONZERO && ev->ev_count == 0) 298 1.9 matt continue; 299 1.9 matt 300 1.9 matt /* 301 1.17 mrg * Prepare to copy. If evs is NULL, fillevcnt will just 302 1.9 matt * how big the item is. 303 1.9 matt */ 304 1.9 matt size_t copylen; 305 1.17 mrg const size_t elem_size = sysctl_fillevcnt(ev, evs, ©len); 306 1.9 matt needed += elem_size; 307 1.9 matt 308 1.9 matt if (len < elem_size) { 309 1.17 mrg evs = NULL; 310 1.9 matt continue; 311 1.9 matt } 312 1.9 matt 313 1.17 mrg KASSERT(evs != NULL); 314 1.17 mrg KASSERT(evs->ev_grouplen != 0); 315 1.17 mrg KASSERT(evs->ev_namelen != 0); 316 1.17 mrg KASSERT(evs->ev_strings[0] != 0); 317 1.9 matt 318 1.9 matt const uint32_t last_generation = evcnt_generation; 319 1.9 matt mutex_exit(&evcnt_lock); 320 1.9 matt 321 1.9 matt /* 322 1.9 matt * Only copy the actual number of bytes, not the rounded 323 1.9 matt * number. If we did the latter we'd have to zero them 324 1.9 matt * first or we'd leak random kernel memory. 325 1.9 matt */ 326 1.17 mrg error = copyout(evs, dp, copylen); 327 1.9 matt 328 1.9 matt mutex_enter(&evcnt_lock); 329 1.9 matt if (error) 330 1.9 matt break; 331 1.9 matt 332 1.9 matt if (__predict_false(last_generation != evcnt_generation)) { 333 1.9 matt /* 334 1.9 matt * This sysctl node is only for statistics. 335 1.9 matt * Retry; if the queue keeps changing, then 336 1.9 matt * bail out. 337 1.9 matt */ 338 1.9 matt if (--retries == 0) { 339 1.9 matt error = EAGAIN; 340 1.9 matt break; 341 1.9 matt } 342 1.9 matt mutex_exit(&evcnt_lock); 343 1.9 matt goto retry; 344 1.9 matt } 345 1.9 matt 346 1.9 matt /* 347 1.9 matt * Now we deal with the pointer/len since we aren't going to 348 1.9 matt * toss their values away. 349 1.9 matt */ 350 1.9 matt dp += elem_size; 351 1.9 matt len -= elem_size; 352 1.9 matt } 353 1.9 matt mutex_exit(&evcnt_lock); 354 1.9 matt 355 1.17 mrg if (evs0 != NULL) 356 1.17 mrg kmem_free(evs0, xevcnt_size); 357 1.9 matt 358 1.9 matt sysctl_relock(); 359 1.9 matt 360 1.9 matt *oldlenp = needed; 361 1.9 matt if (oldp == NULL) 362 1.9 matt *oldlenp += 1024; 363 1.9 matt 364 1.9 matt return (error); 365 1.9 matt } 366 1.9 matt 367 1.9 matt 368 1.9 matt 369 1.9 matt SYSCTL_SETUP(sysctl_evcnt_setup, "sysctl kern.evcnt subtree setup") 370 1.9 matt { 371 1.12 pooka 372 1.9 matt sysctl_createv(clog, 0, NULL, NULL, 373 1.9 matt CTLFLAG_PERMANENT, 374 1.9 matt CTLTYPE_STRUCT, "evcnt", 375 1.9 matt SYSCTL_DESCR("Kernel evcnt information"), 376 1.9 matt sysctl_doevcnt, 0, NULL, 0, 377 1.9 matt CTL_KERN, KERN_EVCNT, CTL_EOL); 378 1.1 rtr } 379 1.14 simonb 380 1.14 simonb #ifdef __HAVE_LEGACY_INTRCNT 381 1.15 simonb extern u_int intrcnt[], eintrcnt[]; 382 1.14 simonb extern char intrnames[]; 383 1.14 simonb static size_t nintr; 384 1.14 simonb struct evcnt *intr_evcnts; 385 1.14 simonb /* 386 1.14 simonb * Remove the following when the last intrcnt/intrnames user is cleaned up. 387 1.14 simonb */ 388 1.14 simonb void 389 1.14 simonb evcnt_attach_legacy_intrcnt(void) 390 1.14 simonb { 391 1.14 simonb size_t i; 392 1.14 simonb const char *cp; 393 1.14 simonb 394 1.16 rin nintr = ((intptr_t)eintrcnt - (intptr_t)intrcnt) / sizeof(intrcnt[0]); 395 1.14 simonb intr_evcnts = kmem_alloc(sizeof(struct evcnt) * nintr, KM_SLEEP); 396 1.14 simonb for (cp = intrnames, i = 0; i < nintr; i++) { 397 1.14 simonb evcnt_attach_dynamic(&intr_evcnts[i], EVCNT_TYPE_INTR, 398 1.14 simonb NULL, "cpu", cp); 399 1.14 simonb cp += strlen(cp) + 1; 400 1.14 simonb } 401 1.14 simonb } 402 1.14 simonb 403 1.14 simonb static void 404 1.14 simonb evcnt_update_intrcnt(void) 405 1.14 simonb { 406 1.14 simonb size_t i; 407 1.14 simonb 408 1.14 simonb KASSERT(nintr > 0); 409 1.14 simonb KASSERT(intr_evcnts != NULL); 410 1.14 simonb 411 1.14 simonb for (i = 0; i < nintr; i++) { 412 1.14 simonb intr_evcnts[i].ev_count = intrcnt[i]; 413 1.14 simonb } 414 1.14 simonb } 415 1.14 simonb #endif 416