Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | /* * Copyright (c) 2019 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #include <sys/dtrace.h> #include <sys/dtrace_impl.h> #include <kern/lock_group.h> #include <kern/lock_stat.h> #if LOCK_STATS #define SPIN_HELD 0 #define SPIN_MISS 1 #define SPIN_SPIN 2 #define SPIN_HELD_PREFIX "spin-held-" #define SPIN_MISS_PREFIX "spin-miss-" #define SPIN_SPIN_PREFIX "spin-spin-" #define LOCKGROUPSTAT_AFRAMES 1 #define LOCKGROUPSTAT_LEN 64 static dtrace_provider_id_t lockprof_id; decl_lck_mtx_data(extern, lck_grp_lock); extern queue_head_t lck_grp_queue; extern unsigned int lck_grp_cnt; #define LOCKPROF_MAX 10000 /* maximum number of lockprof probes */ static uint32_t lockprof_count; /* current number of lockprof probes */ static const struct { int kind; const char *prefix; bool time_event; } events[] = { {SPIN_HELD, SPIN_HELD_PREFIX, false}, {SPIN_MISS, SPIN_MISS_PREFIX, false}, {SPIN_SPIN, SPIN_SPIN_PREFIX, true}, {0, NULL, false} }; const static int hold_defaults[] = { 100, 1000 }; const static struct { unsigned int time; const char *suffix; uint64_t mult; } cont_defaults[] = { {100, "ms", NANOSEC / MILLISEC} }; typedef struct lockprof_probe { int lockprof_kind; dtrace_id_t lockprof_id; uint64_t lockprof_limit; lck_grp_t *lockprof_grp; } lockprof_probe_t; void lockprof_invoke(lck_grp_t *grp, lck_grp_stat_t *stat, uint64_t val) { dtrace_probe(stat->lgs_probeid, (uintptr_t)grp, val, 0, 0, 0); } static void probe_create(int kind, const char *suffix, const char *grp_name, uint64_t count, uint64_t mult) { char name[LOCKGROUPSTAT_LEN]; lck_mtx_lock(&lck_grp_lock); lck_grp_t *grp = (lck_grp_t*)queue_first(&lck_grp_queue); uint64_t limit = count * mult; if (events[kind].time_event) { nanoseconds_to_absolutetime(limit, &limit); } for (unsigned int i = 0; i < lck_grp_cnt; i++, grp = (lck_grp_t*)queue_next((queue_entry_t)grp)) { if (!grp_name || grp_name[0] == '\0' || strcmp(grp_name, grp->lck_grp_name) == 0) { snprintf(name, sizeof(name), "%s%llu%s", events[kind].prefix, count, suffix ?: ""); if (dtrace_probe_lookup(lockprof_id, grp->lck_grp_name, NULL, name) != 0) { continue; } if (lockprof_count >= LOCKPROF_MAX) { break; } lockprof_probe_t *probe = kmem_zalloc(sizeof(lockprof_probe_t), KM_SLEEP); probe->lockprof_kind = kind; probe->lockprof_limit = limit; probe->lockprof_grp = grp; probe->lockprof_id = dtrace_probe_create(lockprof_id, grp->lck_grp_name, NULL, name, LOCKGROUPSTAT_AFRAMES, probe); lockprof_count++; } } lck_mtx_unlock(&lck_grp_lock); } static void lockprof_provide(void *arg, const dtrace_probedesc_t *desc) { #pragma unused(arg) size_t event_id, i, len; if (desc == NULL) { for (i = 0; i < sizeof(hold_defaults) / sizeof(hold_defaults[0]); i++) { probe_create(SPIN_HELD, NULL, NULL, hold_defaults[i], 1); probe_create(SPIN_MISS, NULL, NULL, hold_defaults[i], 1); } for (i = 0; i < sizeof(cont_defaults) / sizeof(cont_defaults[0]); i++) { probe_create(SPIN_SPIN, cont_defaults[i].suffix, NULL, cont_defaults[i].time, cont_defaults[i].mult); } return; } const char *name, *suffix = NULL; hrtime_t val = 0, mult = 1; const struct { const char *name; hrtime_t mult; } suffixes[] = { { "us", NANOSEC / MICROSEC }, { "usec", NANOSEC / MICROSEC }, { "ms", NANOSEC / MILLISEC }, { "msec", NANOSEC / MILLISEC }, { "s", NANOSEC / SEC }, { "sec", NANOSEC / SEC }, { NULL, 0 } }; name = desc->dtpd_name; for (event_id = 0; events[event_id].prefix != NULL; event_id++) { len = strlen(events[event_id].prefix); if (strncmp(name, events[event_id].prefix, len) != 0) { continue; } break; } if (events[event_id].prefix == NULL) { return; } /* * We need to start before any time suffix. */ for (i = strlen(name); i >= len; i--) { if (name[i] >= '0' && name[i] <= '9') { break; } suffix = &name[i]; } /* * Now determine the numerical value present in the probe name. */ for (uint64_t m = 1; i >= len; i--) { if (name[i] < '0' || name[i] > '9') { return; } val += (name[i] - '0') * m; m *= (hrtime_t)10; } if (val == 0) { return; } if (events[event_id].time_event) { for (i = 0, mult = 0; suffixes[i].name != NULL; i++) { if (strncasecmp(suffixes[i].name, suffix, strlen(suffixes[i].name) + 1) == 0) { mult = suffixes[i].mult; break; } } if (suffixes[i].name == NULL) { return; } } else if (*suffix != '\0') { return; } probe_create(events[event_id].kind, suffix, desc->dtpd_mod, val, mult); } static lck_grp_stat_t* lockprof_stat(lck_grp_t *grp, int kind) { switch (kind) { case SPIN_HELD: return &grp->lck_grp_stats.lgss_spin_held; case SPIN_MISS: return &grp->lck_grp_stats.lgss_spin_miss; case SPIN_SPIN: return &grp->lck_grp_stats.lgss_spin_spin; default: return NULL; } } static int lockprof_enable(void *arg, dtrace_id_t id, void *parg) { #pragma unused(arg, id, parg) lockprof_probe_t *probe = (lockprof_probe_t*)parg; lck_grp_t *grp = probe->lockprof_grp; lck_grp_stat_t *stat; if (grp == NULL) { return -1; } if ((stat = lockprof_stat(grp, probe->lockprof_kind)) == NULL) { return -1; } /* * lockprof_enable/disable are called with * dtrace_lock held */ if (stat->lgs_limit != 0) { return -1; } stat->lgs_limit = probe->lockprof_limit; stat->lgs_enablings++; stat->lgs_probeid = probe->lockprof_id; return 0; } static void lockprof_disable(void *arg, dtrace_id_t id, void *parg) { #pragma unused(arg, id) lockprof_probe_t *probe = (lockprof_probe_t*)parg; lck_grp_t *grp = probe->lockprof_grp; lck_grp_stat_t *stat; if (grp == NULL) { return; } if ((stat = lockprof_stat(grp, probe->lockprof_kind)) == NULL) { return; } if (stat->lgs_limit == 0 || stat->lgs_enablings == 0) { return; } stat->lgs_limit = 0; stat->lgs_enablings--; stat->lgs_probeid = 0; } static void lockprof_destroy(void *arg, dtrace_id_t id, void *parg) { #pragma unused(arg, id) lockprof_probe_t *probe = (lockprof_probe_t*)parg; kmem_free(probe, sizeof(lockprof_probe_t)); lockprof_count--; } static void lockprof_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc) { #pragma unused(arg, id, parg) const char *argdesc = NULL; switch (desc->dtargd_ndx) { case 0: argdesc = "lck_grp_t*"; break; case 1: argdesc = "uint64_t"; break; } if (argdesc) { strlcpy(desc->dtargd_native, argdesc, DTRACE_ARGTYPELEN); } else { desc->dtargd_ndx = DTRACE_ARGNONE; } } static dtrace_pattr_t lockprof_attr = { { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_UNKNOWN }, { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, }; static dtrace_pops_t lockprof_pops = { .dtps_provide = lockprof_provide, .dtps_provide_module = NULL, .dtps_enable = lockprof_enable, .dtps_disable = lockprof_disable, .dtps_suspend = NULL, .dtps_resume = NULL, .dtps_getargdesc = lockprof_getargdesc, .dtps_getargval = NULL, .dtps_usermode = NULL, .dtps_destroy = lockprof_destroy }; #endif /* LOCK_STATS */ void lockprof_init(void); void lockprof_init(void) { #if LOCK_STATS dtrace_register("lockprof", &lockprof_attr, DTRACE_PRIV_KERNEL, NULL, &lockprof_pops, NULL, &lockprof_id); #endif /* LOCK_STATS */ } |