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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | #include <mach/mach.h> #include <stdlib.h> #include <stdio.h> #include <signal.h> #include <unistd.h> #include <sys/time.h> #include <time.h> #include <mach/error.h> #include <mach/mach_error.h> #include <mach/mig_errors.h> #include <mach/machine.h> #include <mach/processor_info.h> #include <assert.h> #include <nlist.h> #include <fcntl.h> #include <string.h> #include <mach/mach.h> #include <mach/host_info.h> /* * lockstat.c * * Utility to display kernel lock contention statistics. * Usage: * lockstat [all, spin, mutex, rw, <lock group name>] {<repeat interval>} {abs} * * Argument 1 specifies the type of lock to display contention statistics * for; alternatively, a lock group (a logically grouped set of locks, * which can encompass multiple types of locks) can be specified by name. * When argument 1 is "all", statistics are displayed for all lock groups * which have statistics enabled. * Lock types include mutexes, reader-writer locks and spin locks. * Note that support for gathering contention statistics may not be present * for all types of locks on all platforms. * * Argument 2 specifies a periodic interval. The program will display an * updated list of statistics every <repeat interval> seconds. This * argument is optional. The updates display the deltas from the previous * set of statistics, unless "abs" is specified as argument 3. * * Argument 3, if "abs", causes the periodically refreshed lock statistics * to be displayed as absolute values rather than deltas from the previous * display. * * Types of statistics: * Acquisitions: These can include both normal acquisitions, as well * as acquisition attempts. These are listed in the first column. * Examples include calls to lck_mtx_lock and lck_mtx_try_lock * Misses: Incremented if a lock acquisition attempt failed, due to * contention. * Waits (Meaningful only for lock types that can block): Incremented * if a lock acquisition attempt proceeded to block. * * Direct Waits (currently implemented only on i386/x86_64): For adaptive * locks, such as mutexes, incremented if the owner of the mutex * wasn't active on another processor at the time of the lock * attempt. This indicates that no adaptive spin occurred. */ /* * HISTORY * 2005: Bernard Semeria * Created. * 2006: Derek Kumar * Display i386 specific stats, fix incremental display, add * explanatory block comment. */ void usage(void); void print_spin_hdr(void); void print_spin(int requested, lockgroup_info_t *lockgroup); void print_all_spin(lockgroup_info_t *lockgroup); void print_mutex_hdr(void); void print_mutex(int requested, lockgroup_info_t *lockgroup); void print_all_mutex(lockgroup_info_t *lockgroup); void print_rw_hdr(void); void print_rw(int requested, lockgroup_info_t *lockgroup); void print_all_rw(lockgroup_info_t *lockgroup); void prime_lockgroup_deltas(void); void get_lockgroup_deltas(void); char *pgmname; mach_port_t host_control; lockgroup_info_t *lockgroup_info, *lockgroup_start, *lockgroup_deltas; unsigned int count; unsigned int gDebug = 1; int main(int argc, char **argv) { kern_return_t kr; int arg2; unsigned int i; int found; setlinebuf(stdout); pgmname = argv[0]; gDebug = (NULL != strstr(argv[0], "debug")); host_control = mach_host_self(); kr = host_lockgroup_info(host_control, &lockgroup_info, &count); if (kr != KERN_SUCCESS) { mach_error("host_statistics", kr); exit(EXIT_FAILURE); } if (gDebug) { printf("count = %d\n", count); for (i = 0; i < count; i++) { printf("%s\n", lockgroup_info[i].lockgroup_name); } } switch (argc) { case 2: if (strcmp(argv[1], "all") == 0) { print_spin_hdr(); print_all_spin(lockgroup_info); print_mutex_hdr(); print_all_mutex(lockgroup_info); print_rw_hdr(); print_all_rw(lockgroup_info); } else if (strcmp(argv[1], "spin") == 0) { print_spin_hdr(); print_all_spin(lockgroup_info); } else if (strcmp(argv[1], "mutex") == 0) { print_mutex_hdr(); print_all_mutex(lockgroup_info); } else if (strcmp(argv[1], "rw") == 0) { print_rw_hdr(); print_all_rw(lockgroup_info); } else { found = 0; for (i = 0; i < count; i++) { if (strcmp(argv[1], lockgroup_info[i].lockgroup_name) == 0) { found = 1; print_spin_hdr(); print_spin(i, lockgroup_info); print_mutex_hdr(); print_mutex(i, lockgroup_info); print_rw_hdr(); print_rw(i, lockgroup_info); break; } } if (found == 0) { usage(); } } break; case 3: if (sscanf(argv[2], "%d", &arg2) != 1) { usage(); } if (arg2 < 0) { usage(); } prime_lockgroup_deltas(); if (strcmp(argv[1], "all") == 0) { while (1) { sleep(arg2); get_lockgroup_deltas(); print_spin_hdr(); print_all_spin(lockgroup_deltas); print_mutex_hdr(); print_all_mutex(lockgroup_deltas); print_rw_hdr(); print_all_rw(lockgroup_deltas); } } else if (strcmp(argv[1], "spin") == 0) { while (1) { sleep(arg2); get_lockgroup_deltas(); print_spin_hdr(); print_all_spin(lockgroup_deltas); } } else if (strcmp(argv[1], "mutex") == 0) { while (1) { sleep(arg2); get_lockgroup_deltas(); print_mutex_hdr(); print_all_mutex(lockgroup_deltas); } } else if (strcmp(argv[1], "rw") == 0) { while (1) { sleep(arg2); get_lockgroup_deltas(); print_rw_hdr(); print_all_rw(lockgroup_deltas); } } else { found = 0; for (i = 0; i < count; i++) { if (strcmp(argv[1], lockgroup_info[i].lockgroup_name) == 0) { found = 1; while (1) { sleep(arg2); get_lockgroup_deltas(); print_spin_hdr(); print_spin(i, lockgroup_deltas); print_mutex_hdr(); print_mutex(i, lockgroup_deltas); print_rw_hdr(); print_rw(i, lockgroup_deltas); } } } if (found == 0) { usage(); } } break; case 4: if (strcmp(argv[3], "abs") != 0) { usage(); } if (sscanf(argv[2], "%d", &arg2) != 1) { usage(); } if (strcmp(argv[1], "all") == 0) { while (1) { print_spin_hdr(); print_all_spin(lockgroup_info); print_mutex_hdr(); print_all_mutex(lockgroup_info); print_rw_hdr(); print_all_rw(lockgroup_info); sleep(arg2); } } else if (strcmp(argv[1], "spin") == 0) { while (1) { print_all_spin(lockgroup_info); sleep(arg2); } } else if (strcmp(argv[1], "mutex") == 0) { print_mutex_hdr(); while (1) { print_all_mutex(lockgroup_info); sleep(arg2); } } else if (strcmp(argv[1], "rw") == 0) { print_rw_hdr(); while (1) { print_all_rw(lockgroup_info); sleep(arg2); } } else { found = 0; for (i = 0; i < count; i++) { if (strcmp(argv[1], lockgroup_info[i].lockgroup_name) == 0) { found = 1; while (1) { print_spin_hdr(); print_spin(i, lockgroup_info); print_mutex_hdr(); print_mutex(i, lockgroup_info); print_rw_hdr(); print_rw(i, lockgroup_info); sleep(arg2); } } } if (found == 0) { usage(); } } break; default: usage(); break; } exit(0); } void usage() { fprintf(stderr, "Usage: %s [all, spin, mutex, rw, <lock group name>] {<repeat interval>} {abs}\n", pgmname); exit(EXIT_FAILURE); } void print_spin_hdr(void) { printf(" Spinlock acquires misses Name\n"); } void print_spin(int requested, lockgroup_info_t *lockgroup) { lockgroup_info_t *curptr = &lockgroup[requested]; if (curptr->lock_spin_cnt != 0 && curptr->lock_spin_util_cnt != 0) { printf("%16lld ", curptr->lock_spin_util_cnt); printf("%16lld ", curptr->lock_spin_miss_cnt); printf("%-14s\n", curptr->lockgroup_name); } } void print_all_spin(lockgroup_info_t *lockgroup) { unsigned int i; for (i = 0; i < count; i++) { print_spin(i, lockgroup); } printf("\n"); } void print_mutex_hdr(void) { #if defined(__i386__) || defined(__x86_64__) printf("Mutex lock attempts Misses Waits Direct Waits Name\n"); #else printf(" mutex locks misses waits name\n"); #endif } void print_mutex(int requested, lockgroup_info_t *lockgroup) { lockgroup_info_t *curptr = &lockgroup[requested]; if (curptr->lock_mtx_cnt != 0 && curptr->lock_mtx_util_cnt != 0) { printf("%16lld ", curptr->lock_mtx_util_cnt); #if defined(__i386__) || defined(__x86_64__) printf("%10lld %10lld %10lld ", curptr->lock_mtx_miss_cnt, curptr->lock_mtx_wait_cnt, curptr->lock_mtx_held_cnt); #else printf("%16lld %16lld ", curptr->lock_mtx_miss_cnt, curptr->lock_mtx_wait_cnt); #endif printf("%-14s\n", curptr->lockgroup_name); } } void print_all_mutex(lockgroup_info_t *lockgroup) { unsigned int i; for (i = 0; i < count; i++) { print_mutex(i, lockgroup); } printf("\n"); } void print_rw_hdr(void) { printf(" RW locks Misses Waits Name\n"); } void print_rw(int requested, lockgroup_info_t *lockgroup) { lockgroup_info_t *curptr = &lockgroup[requested]; if (curptr->lock_rw_cnt != 0 && curptr->lock_rw_util_cnt != 0) { printf("%16lld ", curptr->lock_rw_util_cnt); printf("%16lld %16lld ", curptr->lock_rw_miss_cnt, curptr->lock_rw_wait_cnt); printf("%-14s\n", curptr->lockgroup_name); } } void print_all_rw(lockgroup_info_t *lockgroup) { unsigned int i; for (i = 0; i < count; i++) { print_rw(i, lockgroup); } printf("\n"); } void prime_lockgroup_deltas(void) { lockgroup_start = calloc(count, sizeof(lockgroup_info_t)); if (lockgroup_start == NULL) { fprintf(stderr, "Can't allocate memory for lockgroup info\n"); exit(EXIT_FAILURE); } memcpy(lockgroup_start, lockgroup_info, count * sizeof(lockgroup_info_t)); lockgroup_deltas = calloc(count, sizeof(lockgroup_info_t)); if (lockgroup_deltas == NULL) { fprintf(stderr, "Can't allocate memory for lockgroup info\n"); exit(EXIT_FAILURE); } } void get_lockgroup_deltas(void) { kern_return_t kr; unsigned int i; kr = host_lockgroup_info(host_control, &lockgroup_info, &count); if (kr != KERN_SUCCESS) { mach_error("host_statistics", kr); exit(EXIT_FAILURE); } memcpy(lockgroup_deltas, lockgroup_info, count * sizeof(lockgroup_info_t)); for (i = 0; i < count; i++) { lockgroup_deltas[i].lock_spin_util_cnt = lockgroup_info[i].lock_spin_util_cnt - lockgroup_start[i].lock_spin_util_cnt; lockgroup_deltas[i].lock_spin_miss_cnt = lockgroup_info[i].lock_spin_miss_cnt - lockgroup_start[i].lock_spin_miss_cnt; lockgroup_deltas[i].lock_mtx_util_cnt = lockgroup_info[i].lock_mtx_util_cnt - lockgroup_start[i].lock_mtx_util_cnt; lockgroup_deltas[i].lock_mtx_miss_cnt = lockgroup_info[i].lock_mtx_miss_cnt - lockgroup_start[i].lock_mtx_miss_cnt; lockgroup_deltas[i].lock_mtx_wait_cnt = lockgroup_info[i].lock_mtx_wait_cnt - lockgroup_start[i].lock_mtx_wait_cnt; lockgroup_deltas[i].lock_mtx_held_cnt = lockgroup_info[i].lock_mtx_held_cnt - lockgroup_start[i].lock_mtx_held_cnt; lockgroup_deltas[i].lock_rw_util_cnt = lockgroup_info[i].lock_rw_util_cnt - lockgroup_start[i].lock_rw_util_cnt; lockgroup_deltas[i].lock_rw_miss_cnt = lockgroup_info[i].lock_rw_miss_cnt - lockgroup_start[i].lock_rw_miss_cnt; lockgroup_deltas[i].lock_rw_wait_cnt = lockgroup_info[i].lock_rw_wait_cnt - lockgroup_start[i].lock_rw_wait_cnt; } memcpy(lockgroup_start, lockgroup_info, count * sizeof(lockgroup_info_t)); } |