1 /* $NetBSD: subr_evcnt.c,v 1.13 2018/11/24 17:40:37 maxv 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.13 2018/11/24 17:40:37 maxv 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 == 0 || 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 == 0 || 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 bool allowaddr = get_expose_address(curproc); 208 const size_t copylen = offsetof(struct evcnt_sysctl, ev_strings) 209 + ev->ev_grouplen + 1 + ev->ev_namelen + 1; 210 const size_t len = roundup2(copylen, sizeof(uint64_t)); 211 212 if (xevs != NULL) { 213 xevs->evs.ev_count = ev->ev_count; 214 COND_SET_VALUE(xevs->evs.ev_addr, PTRTOUINT64(ev), allowaddr); 215 COND_SET_VALUE(xevs->evs.ev_parent, PTRTOUINT64(ev->ev_parent), 216 allowaddr); 217 xevs->evs.ev_type = ev->ev_type; 218 xevs->evs.ev_grouplen = ev->ev_grouplen; 219 xevs->evs.ev_namelen = ev->ev_namelen; 220 xevs->evs.ev_len = len / sizeof(uint64_t); 221 strcpy(xevs->evs.ev_strings, ev->ev_group); 222 strcpy(xevs->evs.ev_strings + ev->ev_grouplen + 1, ev->ev_name); 223 } 224 225 *copylenp = copylen; 226 return len; 227 } 228 229 static int 230 sysctl_doevcnt(SYSCTLFN_ARGS) 231 { 232 struct xevcnt_sysctl *xevs0 = NULL, *xevs; 233 const struct evcnt *ev; 234 int error; 235 int retries; 236 size_t needed, len; 237 char *dp; 238 239 if (namelen == 1 && name[0] == CTL_QUERY) 240 return (sysctl_query(SYSCTLFN_CALL(rnode))); 241 242 if (namelen != 2) 243 return (EINVAL); 244 245 /* 246 * We can filter on the type of evcnt. 247 */ 248 const int filter = name[0]; 249 if (filter != EVCNT_TYPE_ANY 250 && filter != EVCNT_TYPE_MISC 251 && filter != EVCNT_TYPE_INTR 252 && filter != EVCNT_TYPE_TRAP) 253 return (EINVAL); 254 255 const u_int count = name[1]; 256 if (count != KERN_EVCNT_COUNT_ANY 257 && count != KERN_EVCNT_COUNT_NONZERO) 258 return (EINVAL); 259 260 sysctl_unlock(); 261 262 if (oldp != NULL && xevs0 == NULL) 263 xevs0 = kmem_zalloc(sizeof(*xevs0), KM_SLEEP); 264 265 retries = 100; 266 retry: 267 dp = oldp; 268 len = (oldp != NULL) ? *oldlenp : 0; 269 xevs = xevs0; 270 error = 0; 271 needed = 0; 272 273 mutex_enter(&evcnt_lock); 274 TAILQ_FOREACH(ev, &allevents, ev_list) { 275 if (filter != EVCNT_TYPE_ANY && filter != ev->ev_type) 276 continue; 277 if (count == KERN_EVCNT_COUNT_NONZERO && ev->ev_count == 0) 278 continue; 279 280 /* 281 * Prepare to copy. If xevs is NULL, fillevcnt will just 282 * how big the item is. 283 */ 284 size_t copylen; 285 const size_t elem_size = sysctl_fillevcnt(ev, xevs, ©len); 286 needed += elem_size; 287 288 if (len < elem_size) { 289 xevs = NULL; 290 continue; 291 } 292 293 KASSERT(xevs != NULL); 294 KASSERT(xevs->evs.ev_grouplen != 0); 295 KASSERT(xevs->evs.ev_namelen != 0); 296 KASSERT(xevs->evs.ev_strings[0] != 0); 297 298 const uint32_t last_generation = evcnt_generation; 299 mutex_exit(&evcnt_lock); 300 301 /* 302 * Only copy the actual number of bytes, not the rounded 303 * number. If we did the latter we'd have to zero them 304 * first or we'd leak random kernel memory. 305 */ 306 error = copyout(xevs, dp, copylen); 307 308 mutex_enter(&evcnt_lock); 309 if (error) 310 break; 311 312 if (__predict_false(last_generation != evcnt_generation)) { 313 /* 314 * This sysctl node is only for statistics. 315 * Retry; if the queue keeps changing, then 316 * bail out. 317 */ 318 if (--retries == 0) { 319 error = EAGAIN; 320 break; 321 } 322 mutex_exit(&evcnt_lock); 323 goto retry; 324 } 325 326 /* 327 * Now we deal with the pointer/len since we aren't going to 328 * toss their values away. 329 */ 330 dp += elem_size; 331 len -= elem_size; 332 } 333 mutex_exit(&evcnt_lock); 334 335 if (xevs0 != NULL) 336 kmem_free(xevs0, sizeof(*xevs0)); 337 338 sysctl_relock(); 339 340 *oldlenp = needed; 341 if (oldp == NULL) 342 *oldlenp += 1024; 343 344 return (error); 345 } 346 347 348 349 SYSCTL_SETUP(sysctl_evcnt_setup, "sysctl kern.evcnt subtree setup") 350 { 351 352 sysctl_createv(clog, 0, NULL, NULL, 353 CTLFLAG_PERMANENT, 354 CTLTYPE_STRUCT, "evcnt", 355 SYSCTL_DESCR("Kernel evcnt information"), 356 sysctl_doevcnt, 0, NULL, 0, 357 CTL_KERN, KERN_EVCNT, CTL_EOL); 358 } 359