HAMMER 60I/Many: Mirroring
[dragonfly.git] / sys / kern / kern_ktr.c
blob016fac200ad0730c39c41d6f0bb387e486daf990
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:
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.
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.
35 * The following copyright applies to the DDB command code:
37 * Copyright (c) 2000 John Baldwin <jhb@FreeBSD.org>
38 * All rights reserved.
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.
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.
65 * $DragonFly: src/sys/kern/kern_ktr.c,v 1.23 2008/02/12 23:33:23 corecode Exp $
68 * Kernel tracepoint facility.
71 #include "opt_ddb.h"
72 #include "opt_ktr.h"
74 #include <sys/param.h>
75 #include <sys/cons.h>
76 #include <sys/kernel.h>
77 #include <sys/libkern.h>
78 #include <sys/proc.h>
79 #include <sys/sysctl.h>
80 #include <sys/ktr.h>
81 #include <sys/systm.h>
82 #include <sys/time.h>
83 #include <sys/malloc.h>
84 #include <sys/spinlock.h>
85 #include <sys/thread2.h>
86 #include <sys/spinlock2.h>
87 #include <sys/ctype.h>
89 #include <machine/cpu.h>
90 #include <machine/cpufunc.h>
91 #include <machine/specialreg.h>
92 #include <machine/md_var.h>
94 #include <ddb/ddb.h>
96 #ifndef KTR_ENTRIES
97 #define KTR_ENTRIES 2048
98 #endif
99 #define KTR_ENTRIES_MASK (KTR_ENTRIES - 1)
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.
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", sizeof(void *) * 4);
112 KTR_INFO(KTR_TESTLOG, testlog, test2, 1, "test2", sizeof(void *) * 4);
113 KTR_INFO(KTR_TESTLOG, testlog, test3, 2, "test3", sizeof(void *) * 4);
114 KTR_INFO(KTR_TESTLOG, testlog, test4, 3, "test4", 0);
115 KTR_INFO(KTR_TESTLOG, testlog, test5, 4, "test5", 0);
116 KTR_INFO(KTR_TESTLOG, testlog, test6, 5, "test6", 0);
117 #ifdef SMP
118 KTR_INFO(KTR_TESTLOG, testlog, pingpong, 6, "pingpong", 0);
119 KTR_INFO(KTR_TESTLOG, testlog, pipeline, 7, "pipeline", 0);
120 #endif
121 KTR_INFO(KTR_TESTLOG, testlog, crit_beg, 8, "crit_beg", 0);
122 KTR_INFO(KTR_TESTLOG, testlog, crit_end, 9, "crit_end", 0);
123 KTR_INFO(KTR_TESTLOG, testlog, spin_beg, 10, "spin_beg", 0);
124 KTR_INFO(KTR_TESTLOG, testlog, spin_end, 11, "spin_end", 0);
125 #define logtest(name) KTR_LOG(testlog_ ## name, 0, 0, 0, 0)
126 #define logtest_noargs(name) KTR_LOG(testlog_ ## name)
127 #endif
129 MALLOC_DEFINE(M_KTR, "ktr", "ktr buffers");
131 SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RW, 0, "ktr");
133 int ktr_entries = KTR_ENTRIES;
134 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, "");
136 int ktr_version = KTR_VERSION;
137 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, &ktr_version, 0, "");
139 static int ktr_stacktrace = 1;
140 SYSCTL_INT(_debug_ktr, OID_AUTO, stacktrace, CTLFLAG_RD, &ktr_stacktrace, 0, "");
142 static int ktr_resynchronize = 0;
143 SYSCTL_INT(_debug_ktr, OID_AUTO, resynchronize, CTLFLAG_RW, &ktr_resynchronize, 0, "");
145 #if KTR_TESTLOG
146 static int ktr_testlogcnt = 0;
147 SYSCTL_INT(_debug_ktr, OID_AUTO, testlogcnt, CTLFLAG_RW, &ktr_testlogcnt, 0, "");
148 static int ktr_testipicnt = 0;
149 static int ktr_testipicnt_remainder;
150 SYSCTL_INT(_debug_ktr, OID_AUTO, testipicnt, CTLFLAG_RW, &ktr_testipicnt, 0, "");
151 static int ktr_testcritcnt = 0;
152 SYSCTL_INT(_debug_ktr, OID_AUTO, testcritcnt, CTLFLAG_RW, &ktr_testcritcnt, 0, "");
153 static int ktr_testspincnt = 0;
154 SYSCTL_INT(_debug_ktr, OID_AUTO, testspincnt, CTLFLAG_RW, &ktr_testspincnt, 0, "");
155 #endif
158 * Give cpu0 a static buffer so the tracepoint facility can be used during
159 * early boot (note however that we still use a critical section, XXX).
161 static struct ktr_entry ktr_buf0[KTR_ENTRIES];
162 struct ktr_entry *ktr_buf[MAXCPU] = { &ktr_buf0[0] };
163 int ktr_idx[MAXCPU];
164 #ifdef SMP
165 static int ktr_sync_state = 0;
166 static int ktr_sync_count;
167 static int64_t ktr_sync_tsc;
168 #endif
169 struct callout ktr_resync_callout;
171 #ifdef KTR_VERBOSE
172 int ktr_verbose = KTR_VERBOSE;
173 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
174 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
175 #endif
177 static void ktr_resync_callback(void *dummy __unused);
179 extern int64_t tsc_offsets[];
181 static void
182 ktr_sysinit(void *dummy)
184 int i;
186 for(i = 1; i < ncpus; ++i) {
187 ktr_buf[i] = kmalloc(KTR_ENTRIES * sizeof(struct ktr_entry),
188 M_KTR, M_WAITOK | M_ZERO);
190 callout_init(&ktr_resync_callout);
191 callout_reset(&ktr_resync_callout, hz / 10, ktr_resync_callback, NULL);
193 SYSINIT(ktr_sysinit, SI_BOOT2_KLD, SI_ORDER_ANY, ktr_sysinit, NULL);
196 * Try to resynchronize the TSC's for all cpus. This is really, really nasty.
197 * We have to send an IPIQ message to all remote cpus, wait until they
198 * get into their IPIQ processing code loop, then do an even stricter hard
199 * loop to get the cpus as close to synchronized as we can to get the most
200 * accurate reading.
202 * This callback occurs on cpu0.
204 #if KTR_TESTLOG
205 static void ktr_pingpong_remote(void *dummy);
206 static void ktr_pipeline_remote(void *dummy);
207 #endif
209 #if defined(SMP) && defined(_RDTSC_SUPPORTED_)
211 static void ktr_resync_remote(void *dummy);
212 extern cpumask_t smp_active_mask;
215 * We use a callout callback instead of a systimer because we cannot afford
216 * to preempt anyone to do this, or we might deadlock a spin-lock or
217 * serializer between two cpus.
219 static
220 void
221 ktr_resync_callback(void *dummy __unused)
223 int count;
225 KKASSERT(mycpu->gd_cpuid == 0);
227 #if KTR_TESTLOG
229 * Test logging
231 if (ktr_testlogcnt) {
232 --ktr_testlogcnt;
233 cpu_disable_intr();
234 logtest(test1);
235 logtest(test2);
236 logtest(test3);
237 logtest_noargs(test4);
238 logtest_noargs(test5);
239 logtest_noargs(test6);
240 cpu_enable_intr();
244 * Test IPI messaging
246 if (ktr_testipicnt && ktr_testipicnt_remainder == 0 && ncpus > 1) {
247 ktr_testipicnt_remainder = ktr_testipicnt;
248 ktr_testipicnt = 0;
249 lwkt_send_ipiq_bycpu(1, ktr_pingpong_remote, NULL);
253 * Test critical sections
255 if (ktr_testcritcnt) {
256 crit_enter();
257 crit_exit();
258 logtest_noargs(crit_beg);
259 for (count = ktr_testcritcnt; count; --count) {
260 crit_enter();
261 crit_exit();
263 logtest_noargs(crit_end);
264 ktr_testcritcnt = 0;
268 * Test spinlock sections
270 if (ktr_testspincnt) {
271 struct spinlock spin;
273 spin_init(&spin);
274 spin_lock_wr(&spin);
275 spin_unlock_wr(&spin);
276 logtest_noargs(spin_beg);
277 for (count = ktr_testspincnt; count; --count) {
278 spin_lock_wr(&spin);
279 spin_unlock_wr(&spin);
281 logtest_noargs(spin_end);
282 logtest_noargs(spin_beg);
283 for (count = ktr_testspincnt; count; --count) {
284 spin_lock_rd(&spin);
285 spin_unlock_rd(&spin);
287 logtest_noargs(spin_end);
288 ktr_testspincnt = 0;
290 #endif
293 * Resynchronize the TSC
295 if (ktr_resynchronize == 0)
296 goto done;
297 if ((cpu_feature & CPUID_TSC) == 0)
298 return;
301 * Send the synchronizing IPI and wait for all cpus to get into
302 * their spin loop. We must process incoming IPIs while waiting
303 * to avoid a deadlock.
305 crit_enter();
306 ktr_sync_count = 0;
307 ktr_sync_state = 1;
308 ktr_sync_tsc = rdtsc();
309 count = lwkt_send_ipiq_mask(mycpu->gd_other_cpus & smp_active_mask,
310 (ipifunc1_t)ktr_resync_remote, NULL);
311 while (ktr_sync_count != count)
312 lwkt_process_ipiq();
315 * Continuously update the TSC for cpu 0 while waiting for all other
316 * cpus to finish stage 2.
318 cpu_disable_intr();
319 ktr_sync_tsc = rdtsc();
320 cpu_sfence();
321 ktr_sync_state = 2;
322 cpu_sfence();
323 while (ktr_sync_count != 0) {
324 ktr_sync_tsc = rdtsc();
325 cpu_lfence();
326 cpu_nop();
328 cpu_enable_intr();
329 crit_exit();
330 ktr_sync_state = 0;
331 done:
332 callout_reset(&ktr_resync_callout, hz / 10, ktr_resync_callback, NULL);
336 * The remote-end of the KTR synchronization protocol runs on all cpus except
337 * cpu 0. Since this is an IPI function, it is entered with the current
338 * thread in a critical section.
340 static void
341 ktr_resync_remote(void *dummy __unused)
343 volatile int64_t tsc1 = ktr_sync_tsc;
344 volatile int64_t tsc2;
347 * Inform the master that we have entered our hard loop.
349 KKASSERT(ktr_sync_state == 1);
350 atomic_add_int(&ktr_sync_count, 1);
351 while (ktr_sync_state == 1) {
352 lwkt_process_ipiq();
356 * Now the master is in a hard loop, synchronize the TSC and
357 * we are done.
359 cpu_disable_intr();
360 KKASSERT(ktr_sync_state == 2);
361 tsc2 = ktr_sync_tsc;
362 if (tsc2 > tsc1)
363 tsc_offsets[mycpu->gd_cpuid] = rdtsc() - tsc2;
364 atomic_subtract_int(&ktr_sync_count, 1);
365 cpu_enable_intr();
368 #if KTR_TESTLOG
370 static
371 void
372 ktr_pingpong_remote(void *dummy __unused)
374 int other_cpu;
376 logtest_noargs(pingpong);
377 other_cpu = 1 - mycpu->gd_cpuid;
378 if (ktr_testipicnt_remainder) {
379 --ktr_testipicnt_remainder;
380 lwkt_send_ipiq_bycpu(other_cpu, ktr_pingpong_remote, NULL);
381 } else {
382 lwkt_send_ipiq_bycpu(other_cpu, ktr_pipeline_remote, NULL);
383 lwkt_send_ipiq_bycpu(other_cpu, ktr_pipeline_remote, NULL);
384 lwkt_send_ipiq_bycpu(other_cpu, ktr_pipeline_remote, NULL);
385 lwkt_send_ipiq_bycpu(other_cpu, ktr_pipeline_remote, NULL);
386 lwkt_send_ipiq_bycpu(other_cpu, ktr_pipeline_remote, NULL);
390 static
391 void
392 ktr_pipeline_remote(void *dummy __unused)
394 logtest_noargs(pipeline);
397 #endif
399 #else /* !SMP */
402 * The resync callback for UP doesn't do anything other then run the test
403 * log messages. If test logging is not enabled, don't bother resetting
404 * the callout.
406 static
407 void
408 ktr_resync_callback(void *dummy __unused)
410 #if KTR_TESTLOG
412 * Test logging
414 if (ktr_testlogcnt) {
415 --ktr_testlogcnt;
416 cpu_disable_intr();
417 logtest(test1);
418 logtest(test2);
419 logtest(test3);
420 logtest_noargs(test4);
421 logtest_noargs(test5);
422 logtest_noargs(test6);
423 cpu_enable_intr();
425 callout_reset(&ktr_resync_callout, hz / 10, ktr_resync_callback, NULL);
426 #endif
429 #endif
432 * KTR_WRITE_ENTRY - Primary entry point for kernel trace logging
434 static __inline
435 void
436 ktr_write_entry(struct ktr_info *info, const char *file, int line,
437 const void *ptr)
439 struct ktr_entry *entry;
440 int cpu;
442 cpu = mycpu->gd_cpuid;
443 if (!ktr_buf[cpu])
444 return;
446 crit_enter();
447 entry = ktr_buf[cpu] + (ktr_idx[cpu] & KTR_ENTRIES_MASK);
448 ++ktr_idx[cpu];
449 #ifdef _RDTSC_SUPPORTED_
450 if (cpu_feature & CPUID_TSC) {
451 #ifdef SMP
452 entry->ktr_timestamp = rdtsc() - tsc_offsets[cpu];
453 #else
454 entry->ktr_timestamp = rdtsc();
455 #endif
456 } else
457 #endif
459 entry->ktr_timestamp = get_approximate_time_t();
461 entry->ktr_info = info;
462 entry->ktr_file = file;
463 entry->ktr_line = line;
464 crit_exit();
465 if (info->kf_data_size > KTR_BUFSIZE)
466 bcopyi(ptr, entry->ktr_data, KTR_BUFSIZE);
467 else if (info->kf_data_size)
468 bcopyi(ptr, entry->ktr_data, info->kf_data_size);
469 if (ktr_stacktrace)
470 cpu_ktr_caller(entry);
471 #ifdef KTR_VERBOSE
472 if (ktr_verbose && info->kf_format) {
473 #ifdef SMP
474 kprintf("cpu%d ", cpu);
475 #endif
476 if (ktr_verbose > 1) {
477 kprintf("%s.%d\t", entry->ktr_file, entry->ktr_line);
479 kvprintf(info->kf_format, ptr);
480 kprintf("\n");
482 #endif
485 void
486 ktr_log(struct ktr_info *info, const char *file, int line, ...)
488 __va_list va;
490 if (panicstr == NULL) {
491 __va_start(va, line);
492 ktr_write_entry(info, file, line, va);
493 __va_end(va);
497 void
498 ktr_log_ptr(struct ktr_info *info, const char *file, int line, const void *ptr)
500 if (panicstr == NULL) {
501 ktr_write_entry(info, file, line, ptr);
505 #ifdef DDB
507 #define NUM_LINES_PER_PAGE 19
509 struct tstate {
510 int cur;
511 int first;
514 static int db_ktr_verbose;
515 static int db_mach_vtrace(int cpu, struct ktr_entry *kp, int idx);
517 DB_SHOW_COMMAND(ktr, db_ktr_all)
519 int a_flag = 0;
520 int c;
521 int nl = 0;
522 int i;
523 struct tstate tstate[MAXCPU];
524 int printcpu = -1;
526 for(i = 0; i < ncpus; i++) {
527 tstate[i].first = -1;
528 tstate[i].cur = ktr_idx[i] & KTR_ENTRIES_MASK;
530 db_ktr_verbose = 0;
531 while ((c = *(modif++)) != '\0') {
532 if (c == 'v') {
533 db_ktr_verbose = 1;
535 else if (c == 'a') {
536 a_flag = 1;
538 else if (c == 'c') {
539 printcpu = 0;
540 while ((c = *(modif++)) != '\0') {
541 if (isdigit(c)) {
542 printcpu *= 10;
543 printcpu += c - '0';
545 else {
546 modif++;
547 break;
550 modif--;
553 if (printcpu > ncpus - 1) {
554 db_printf("Invalid cpu number\n");
555 return;
558 * Lopp throug all the buffers and print the content of them, sorted
559 * by the timestamp.
561 while (1) {
562 int counter;
563 u_int64_t highest_ts;
564 int highest_cpu;
565 struct ktr_entry *kp;
567 if (a_flag == 1 && cncheckc() != -1)
568 return;
569 highest_ts = 0;
570 highest_cpu = -1;
572 * Find the lowest timestamp
574 for (i = 0, counter = 0; i < ncpus; i++) {
575 if (ktr_buf[i] == NULL)
576 continue;
577 if (printcpu != -1 && printcpu != i)
578 continue;
579 if (tstate[i].cur == -1) {
580 counter++;
581 if (counter == ncpus) {
582 db_printf("--- End of trace buffer ---\n");
583 return;
585 continue;
587 if (ktr_buf[i][tstate[i].cur].ktr_timestamp > highest_ts) {
588 highest_ts = ktr_buf[i][tstate[i].cur].ktr_timestamp;
589 highest_cpu = i;
592 i = highest_cpu;
593 KKASSERT(i != -1);
594 kp = &ktr_buf[i][tstate[i].cur];
595 if (tstate[i].first == -1)
596 tstate[i].first = tstate[i].cur;
597 if (--tstate[i].cur < 0)
598 tstate[i].cur = KTR_ENTRIES - 1;
599 if (tstate[i].first == tstate[i].cur) {
600 db_mach_vtrace(i, kp, tstate[i].cur + 1);
601 tstate[i].cur = -1;
602 continue;
604 if (ktr_buf[i][tstate[i].cur].ktr_info == NULL)
605 tstate[i].cur = -1;
606 if (db_more(&nl) == -1)
607 break;
608 if (db_mach_vtrace(i, kp, tstate[i].cur + 1) == 0)
609 tstate[i].cur = -1;
613 static int
614 db_mach_vtrace(int cpu, struct ktr_entry *kp, int idx)
616 if (kp->ktr_info == NULL)
617 return(0);
618 #ifdef SMP
619 db_printf("cpu%d ", cpu);
620 #endif
621 db_printf("%d: ", idx);
622 if (db_ktr_verbose) {
623 db_printf("%10.10lld %s.%d\t", (long long)kp->ktr_timestamp,
624 kp->ktr_file, kp->ktr_line);
626 db_printf("%s\t", kp->ktr_info->kf_name);
627 db_printf("from(%p,%p) ", kp->ktr_caller1, kp->ktr_caller2);
628 if (kp->ktr_info->kf_format) {
629 int32_t *args = kp->ktr_data;
630 db_printf(kp->ktr_info->kf_format,
631 args[0], args[1], args[2], args[3],
632 args[4], args[5], args[6], args[7],
633 args[8], args[9], args[10], args[11]);
636 db_printf("\n");
638 return(1);
641 #endif /* DDB */