1 /* 2 * Copyright (c) 2005 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 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 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 /* 35 * The following copyright applies to the DDB command code: 36 * 37 * Copyright (c) 2000 John Baldwin <jhb@FreeBSD.org> 38 * All rights reserved. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. Neither the name of the author nor the names of any co-contributors 49 * may be used to endorse or promote products derived from this software 50 * without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * SUCH DAMAGE. 63 */ 64 65 /* 66 * Kernel tracepoint facility. 67 */ 68 69 #include "opt_ddb.h" 70 #include "opt_ktr.h" 71 72 #include <sys/param.h> 73 #include <sys/cons.h> 74 #include <sys/kernel.h> 75 #include <sys/libkern.h> 76 #include <sys/proc.h> 77 #include <sys/sysctl.h> 78 #include <sys/ktr.h> 79 #include <sys/systm.h> 80 #include <sys/time.h> 81 #include <sys/malloc.h> 82 #include <sys/spinlock.h> 83 #include <sys/thread2.h> 84 #include <sys/spinlock2.h> 85 #include <sys/ctype.h> 86 87 #include <machine/cpu.h> 88 #include <machine/cpufunc.h> 89 #include <machine/specialreg.h> 90 #include <machine/md_var.h> 91 92 #include <ddb/ddb.h> 93 94 #ifndef KTR_ENTRIES 95 #define KTR_ENTRIES 2048 96 #elif (KTR_ENTRIES & KTR_ENTRIES - 1) 97 #error KTR_ENTRIES must be a power of two 98 #endif 99 #define KTR_ENTRIES_MASK (KTR_ENTRIES - 1) 100 101 /* 102 * test logging support. When ktr_testlogcnt is non-zero each synchronization 103 * interrupt will issue six back-to-back ktr logging messages on cpu 0 104 * so the user can determine KTR logging overheads. 105 */ 106 #if !defined(KTR_TESTLOG) 107 #define KTR_TESTLOG KTR_ALL 108 #endif 109 KTR_INFO_MASTER(testlog); 110 #if KTR_TESTLOG 111 KTR_INFO(KTR_TESTLOG, testlog, test1, 0, "test1 %d %d %d %d", int dummy1, int dummy2, int dummy3, int dummy4); 112 KTR_INFO(KTR_TESTLOG, testlog, test2, 1, "test2 %d %d %d %d", int dummy1, int dummy2, int dummy3, int dummy4); 113 KTR_INFO(KTR_TESTLOG, testlog, test3, 2, "test3 %d %d %d %d", int dummy1, int dummy2, int dummy3, int dummy4); 114 KTR_INFO(KTR_TESTLOG, testlog, test4, 3, "test4"); 115 KTR_INFO(KTR_TESTLOG, testlog, test5, 4, "test5"); 116 KTR_INFO(KTR_TESTLOG, testlog, test6, 5, "test6"); 117 #ifdef SMP 118 KTR_INFO(KTR_TESTLOG, testlog, pingpong, 6, "pingpong"); 119 KTR_INFO(KTR_TESTLOG, testlog, pipeline, 7, "pipeline"); 120 KTR_INFO(KTR_TESTLOG, testlog, crit_beg, 8, "crit_beg"); 121 KTR_INFO(KTR_TESTLOG, testlog, crit_end, 9, "crit_end"); 122 KTR_INFO(KTR_TESTLOG, testlog, spin_beg, 10, "spin_beg"); 123 KTR_INFO(KTR_TESTLOG, testlog, spin_end, 11, "spin_end"); 124 #endif 125 #define logtest(name) KTR_LOG(testlog_ ## name, 0, 0, 0, 0) 126 #define logtest_noargs(name) KTR_LOG(testlog_ ## name) 127 #endif 128 129 MALLOC_DEFINE(M_KTR, "ktr", "ktr buffers"); 130 131 SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RW, 0, "ktr"); 132 133 int ktr_entries = KTR_ENTRIES; 134 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, 135 "Size of the event buffer"); 136 137 int ktr_version = KTR_VERSION; 138 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, &ktr_version, 0, ""); 139 140 static int ktr_stacktrace = 1; 141 SYSCTL_INT(_debug_ktr, OID_AUTO, stacktrace, CTLFLAG_RD, &ktr_stacktrace, 0, ""); 142 143 static int ktr_resynchronize = 0; 144 SYSCTL_INT(_debug_ktr, OID_AUTO, resynchronize, CTLFLAG_RW, 145 &ktr_resynchronize, 0, "Resynchronize TSC 10 times a second"); 146 147 #if KTR_TESTLOG 148 static int ktr_testlogcnt = 0; 149 SYSCTL_INT(_debug_ktr, OID_AUTO, testlogcnt, CTLFLAG_RW, &ktr_testlogcnt, 0, ""); 150 static int ktr_testipicnt = 0; 151 #ifdef SMP 152 static int ktr_testipicnt_remainder; 153 #endif 154 SYSCTL_INT(_debug_ktr, OID_AUTO, testipicnt, CTLFLAG_RW, &ktr_testipicnt, 0, ""); 155 static int ktr_testcritcnt = 0; 156 SYSCTL_INT(_debug_ktr, OID_AUTO, testcritcnt, CTLFLAG_RW, &ktr_testcritcnt, 0, ""); 157 static int ktr_testspincnt = 0; 158 SYSCTL_INT(_debug_ktr, OID_AUTO, testspincnt, CTLFLAG_RW, &ktr_testspincnt, 0, ""); 159 #endif 160 161 /* 162 * Give cpu0 a static buffer so the tracepoint facility can be used during 163 * early boot (note however that we still use a critical section, XXX). 164 */ 165 static struct ktr_entry ktr_buf0[KTR_ENTRIES]; 166 167 __cachealign struct ktr_cpu ktr_cpu[MAXCPU] = { 168 { .core.ktr_buf = &ktr_buf0[0] } 169 }; 170 171 #ifdef SMP 172 static int64_t ktr_sync_tsc; 173 #endif 174 struct callout ktr_resync_callout; 175 176 #ifdef KTR_VERBOSE 177 int ktr_verbose = KTR_VERBOSE; 178 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose); 179 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, 180 "Log events to the console as well"); 181 #endif 182 183 static void ktr_resync_callback(void *dummy __unused); 184 185 extern int64_t tsc_offsets[]; 186 187 static void 188 ktr_sysinit(void *dummy) 189 { 190 struct ktr_cpu_core *kcpu; 191 int i; 192 193 for(i = 1; i < ncpus; ++i) { 194 kcpu = &ktr_cpu[i].core; 195 kcpu->ktr_buf = kmalloc(KTR_ENTRIES * sizeof(struct ktr_entry), 196 M_KTR, M_WAITOK | M_ZERO); 197 } 198 callout_init_mp(&ktr_resync_callout); 199 callout_reset(&ktr_resync_callout, hz / 10, ktr_resync_callback, NULL); 200 } 201 SYSINIT(ktr_sysinit, SI_BOOT2_KLD, SI_ORDER_ANY, ktr_sysinit, NULL); 202 203 /* 204 * Try to resynchronize the TSC's for all cpus. This is really, really nasty. 205 * We have to send an IPIQ message to all remote cpus, wait until they 206 * get into their IPIQ processing code loop, then do an even stricter hard 207 * loop to get the cpus as close to synchronized as we can to get the most 208 * accurate reading. 209 * 210 * This callback occurs on cpu0. 211 */ 212 #if KTR_TESTLOG 213 #ifdef SMP 214 static void ktr_pingpong_remote(void *dummy); 215 static void ktr_pipeline_remote(void *dummy); 216 #endif 217 #endif 218 219 #if defined(SMP) && defined(_RDTSC_SUPPORTED_) 220 221 static void ktr_resync_remote(void *dummy); 222 extern cpumask_t smp_active_mask; 223 224 /* 225 * We use a callout callback instead of a systimer because we cannot afford 226 * to preempt anyone to do this, or we might deadlock a spin-lock or 227 * serializer between two cpus. 228 */ 229 static 230 void 231 ktr_resync_callback(void *dummy __unused) 232 { 233 struct lwkt_cpusync cs; 234 #if KTR_TESTLOG 235 int count; 236 #endif 237 238 KKASSERT(mycpu->gd_cpuid == 0); 239 240 #if KTR_TESTLOG 241 /* 242 * Test logging 243 */ 244 if (ktr_testlogcnt) { 245 --ktr_testlogcnt; 246 cpu_disable_intr(); 247 logtest(test1); 248 logtest(test2); 249 logtest(test3); 250 logtest_noargs(test4); 251 logtest_noargs(test5); 252 logtest_noargs(test6); 253 cpu_enable_intr(); 254 } 255 256 /* 257 * Test IPI messaging 258 */ 259 if (ktr_testipicnt && ktr_testipicnt_remainder == 0 && ncpus > 1) { 260 ktr_testipicnt_remainder = ktr_testipicnt; 261 ktr_testipicnt = 0; 262 lwkt_send_ipiq_bycpu(1, ktr_pingpong_remote, NULL); 263 } 264 265 /* 266 * Test critical sections 267 */ 268 if (ktr_testcritcnt) { 269 crit_enter(); 270 crit_exit(); 271 logtest_noargs(crit_beg); 272 for (count = ktr_testcritcnt; count; --count) { 273 crit_enter(); 274 crit_exit(); 275 } 276 logtest_noargs(crit_end); 277 ktr_testcritcnt = 0; 278 } 279 280 /* 281 * Test spinlock sections 282 */ 283 if (ktr_testspincnt) { 284 struct spinlock spin; 285 286 spin_init(&spin); 287 spin_lock(&spin); 288 spin_unlock(&spin); 289 logtest_noargs(spin_beg); 290 for (count = ktr_testspincnt; count; --count) { 291 spin_lock(&spin); 292 spin_unlock(&spin); 293 } 294 logtest_noargs(spin_end); 295 ktr_testspincnt = 0; 296 } 297 #endif 298 299 /* 300 * Resynchronize the TSC 301 */ 302 if (ktr_resynchronize == 0) 303 goto done; 304 if ((cpu_feature & CPUID_TSC) == 0) 305 return; 306 307 crit_enter(); 308 lwkt_cpusync_init(&cs, smp_active_mask, ktr_resync_remote, 309 (void *)(intptr_t)mycpu->gd_cpuid); 310 lwkt_cpusync_interlock(&cs); 311 ktr_sync_tsc = rdtsc(); 312 lwkt_cpusync_deinterlock(&cs); 313 crit_exit(); 314 done: 315 callout_reset(&ktr_resync_callout, hz / 10, ktr_resync_callback, NULL); 316 } 317 318 /* 319 * The remote-end of the KTR synchronization protocol runs on all cpus. 320 * The one we run on the controlling cpu updates its tsc continuously 321 * until the others have finished syncing (theoretically), but we don't 322 * loop forever. 323 * 324 * This is a bit ad-hoc but we need to avoid livelocking inside an IPI 325 * callback. rdtsc() is a synchronizing instruction (I think). 326 */ 327 static void 328 ktr_resync_remote(void *arg) 329 { 330 globaldata_t gd = mycpu; 331 int64_t delta; 332 int i; 333 334 if (gd->gd_cpuid == (int)(intptr_t)arg) { 335 for (i = 0; i < 2000; ++i) 336 ktr_sync_tsc = rdtsc(); 337 } else { 338 delta = rdtsc() - ktr_sync_tsc; 339 if (tsc_offsets[gd->gd_cpuid] == 0) 340 tsc_offsets[gd->gd_cpuid] = delta; 341 tsc_offsets[gd->gd_cpuid] = 342 (tsc_offsets[gd->gd_cpuid] * 7 + delta) / 8; 343 } 344 } 345 346 #if KTR_TESTLOG 347 348 static 349 void 350 ktr_pingpong_remote(void *dummy __unused) 351 { 352 int other_cpu; 353 354 logtest_noargs(pingpong); 355 other_cpu = 1 - mycpu->gd_cpuid; 356 if (ktr_testipicnt_remainder) { 357 --ktr_testipicnt_remainder; 358 lwkt_send_ipiq_bycpu(other_cpu, ktr_pingpong_remote, NULL); 359 } else { 360 lwkt_send_ipiq_bycpu(other_cpu, ktr_pipeline_remote, NULL); 361 lwkt_send_ipiq_bycpu(other_cpu, ktr_pipeline_remote, NULL); 362 lwkt_send_ipiq_bycpu(other_cpu, ktr_pipeline_remote, NULL); 363 lwkt_send_ipiq_bycpu(other_cpu, ktr_pipeline_remote, NULL); 364 lwkt_send_ipiq_bycpu(other_cpu, ktr_pipeline_remote, NULL); 365 } 366 } 367 368 static 369 void 370 ktr_pipeline_remote(void *dummy __unused) 371 { 372 logtest_noargs(pipeline); 373 } 374 375 #endif 376 377 #else /* !SMP */ 378 379 /* 380 * The resync callback for UP doesn't do anything other then run the test 381 * log messages. If test logging is not enabled, don't bother resetting 382 * the callout. 383 */ 384 static 385 void 386 ktr_resync_callback(void *dummy __unused) 387 { 388 #if KTR_TESTLOG 389 /* 390 * Test logging 391 */ 392 if (ktr_testlogcnt) { 393 --ktr_testlogcnt; 394 cpu_disable_intr(); 395 logtest(test1); 396 logtest(test2); 397 logtest(test3); 398 logtest_noargs(test4); 399 logtest_noargs(test5); 400 logtest_noargs(test6); 401 cpu_enable_intr(); 402 } 403 callout_reset(&ktr_resync_callout, hz / 10, ktr_resync_callback, NULL); 404 #endif 405 } 406 407 #endif 408 409 /* 410 * Setup the next empty slot and return it to the caller to store the data 411 * directly. 412 */ 413 struct ktr_entry * 414 ktr_begin_write_entry(struct ktr_info *info, const char *file, int line) 415 { 416 struct ktr_cpu_core *kcpu; 417 struct ktr_entry *entry; 418 int cpu; 419 420 cpu = mycpu->gd_cpuid; 421 kcpu = &ktr_cpu[cpu].core; 422 if (kcpu->ktr_buf == NULL) 423 return NULL; 424 425 crit_enter(); 426 entry = kcpu->ktr_buf + (kcpu->ktr_idx & KTR_ENTRIES_MASK); 427 ++kcpu->ktr_idx; 428 #ifdef _RDTSC_SUPPORTED_ 429 if (cpu_feature & CPUID_TSC) { 430 #ifdef SMP 431 entry->ktr_timestamp = rdtsc() - tsc_offsets[cpu]; 432 #else 433 entry->ktr_timestamp = rdtsc(); 434 #endif 435 } else 436 #endif 437 { 438 entry->ktr_timestamp = get_approximate_time_t(); 439 } 440 entry->ktr_info = info; 441 entry->ktr_file = file; 442 entry->ktr_line = line; 443 crit_exit(); 444 return entry; 445 } 446 447 int 448 ktr_finish_write_entry(struct ktr_info *info, struct ktr_entry *entry) 449 { 450 if (ktr_stacktrace) 451 cpu_ktr_caller(entry); 452 #ifdef KTR_VERBOSE 453 if (ktr_verbose && info->kf_format) { 454 #ifdef SMP 455 kprintf("cpu%d ", mycpu->gd_cpuid); 456 #endif 457 if (ktr_verbose > 1) { 458 kprintf("%s.%d\t", entry->ktr_file, entry->ktr_line); 459 } 460 return !0; 461 } 462 #endif 463 return 0; 464 } 465 466 #ifdef DDB 467 468 #define NUM_LINES_PER_PAGE 19 469 470 struct tstate { 471 int cur; 472 int first; 473 }; 474 475 static int db_ktr_verbose; 476 static int db_mach_vtrace(int cpu, struct ktr_entry *kp, int idx); 477 478 DB_SHOW_COMMAND(ktr, db_ktr_all) 479 { 480 struct ktr_cpu_core *kcpu; 481 int a_flag = 0; 482 int c; 483 int nl = 0; 484 int i; 485 struct tstate tstate[MAXCPU]; 486 int printcpu = -1; 487 488 for(i = 0; i < ncpus; i++) { 489 kcpu = &ktr_cpu[i].core; 490 tstate[i].first = -1; 491 tstate[i].cur = (kcpu->ktr_idx - 1) & KTR_ENTRIES_MASK; 492 } 493 db_ktr_verbose = 0; 494 while ((c = *(modif++)) != '\0') { 495 if (c == 'v') { 496 db_ktr_verbose = 1; 497 } 498 else if (c == 'a') { 499 a_flag = 1; 500 } 501 else if (c == 'c') { 502 printcpu = 0; 503 while ((c = *(modif++)) != '\0') { 504 if (isdigit(c)) { 505 printcpu *= 10; 506 printcpu += c - '0'; 507 } 508 else { 509 modif++; 510 break; 511 } 512 } 513 modif--; 514 } 515 } 516 if (printcpu > ncpus - 1) { 517 db_printf("Invalid cpu number\n"); 518 return; 519 } 520 /* 521 * Lopp throug all the buffers and print the content of them, sorted 522 * by the timestamp. 523 */ 524 while (1) { 525 int counter; 526 u_int64_t highest_ts; 527 int highest_cpu; 528 struct ktr_entry *kp; 529 530 if (a_flag == 1 && cncheckc() != -1) 531 return; 532 highest_ts = 0; 533 highest_cpu = -1; 534 /* 535 * Find the lowest timestamp 536 */ 537 for (i = 0, counter = 0; i < ncpus; i++) { 538 kcpu = &ktr_cpu[i].core; 539 if (kcpu->ktr_buf == NULL) 540 continue; 541 if (printcpu != -1 && printcpu != i) 542 continue; 543 if (tstate[i].cur == -1) { 544 counter++; 545 if (counter == ncpus) { 546 db_printf("--- End of trace buffer ---\n"); 547 return; 548 } 549 continue; 550 } 551 if (kcpu->ktr_buf[tstate[i].cur].ktr_timestamp > highest_ts) { 552 highest_ts = kcpu->ktr_buf[tstate[i].cur].ktr_timestamp; 553 highest_cpu = i; 554 } 555 } 556 if (highest_cpu < 0) { 557 db_printf("no KTR data available\n"); 558 break; 559 } 560 i = highest_cpu; 561 kcpu = &ktr_cpu[i].core; 562 kp = &kcpu->ktr_buf[tstate[i].cur]; 563 if (tstate[i].first == -1) 564 tstate[i].first = tstate[i].cur; 565 if (--tstate[i].cur < 0) 566 tstate[i].cur = KTR_ENTRIES - 1; 567 if (tstate[i].first == tstate[i].cur) { 568 db_mach_vtrace(i, kp, tstate[i].cur + 1); 569 tstate[i].cur = -1; 570 continue; 571 } 572 if (kcpu->ktr_buf[tstate[i].cur].ktr_info == NULL) 573 tstate[i].cur = -1; 574 if (db_more(&nl) == -1) 575 break; 576 if (db_mach_vtrace(i, kp, tstate[i].cur + 1) == 0) 577 tstate[i].cur = -1; 578 } 579 } 580 581 static int 582 db_mach_vtrace(int cpu, struct ktr_entry *kp, int idx) 583 { 584 if (kp->ktr_info == NULL) 585 return(0); 586 #ifdef SMP 587 db_printf("cpu%d ", cpu); 588 #endif 589 db_printf("%d: ", idx); 590 if (db_ktr_verbose) { 591 db_printf("%10.10lld %s.%d\t", (long long)kp->ktr_timestamp, 592 kp->ktr_file, kp->ktr_line); 593 } 594 db_printf("%s\t", kp->ktr_info->kf_name); 595 db_printf("from(%p,%p) ", kp->ktr_caller1, kp->ktr_caller2); 596 #ifdef __i386__ 597 if (kp->ktr_info->kf_format) 598 db_vprintf(kp->ktr_info->kf_format, (__va_list)kp->ktr_data); 599 #endif 600 db_printf("\n"); 601 602 return(1); 603 } 604 605 #endif /* DDB */ 606