Add a feature and a sysctl (debug.ktr.testipicnt) to test inter-cpu
[dragonfly.git] / sys / kern / kern_ktr.c
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  * $DragonFly: src/sys/kern/kern_ktr.c,v 1.11 2005/12/10 21:19:30 dillon Exp $
66  */
67 /*
68  * Kernel tracepoint facility.
69  */
70
71 #include "opt_ddb.h"
72 #include "opt_ktr.h"
73
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/thread2.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 #endif
97 #define KTR_ENTRIES_MASK        (KTR_ENTRIES - 1)
98
99 /*
100  * test logging support.  When ktr_testlogcnt is non-zero each synchronization
101  * interrupt will issue six back-to-back ktr logging messages on cpu 0
102  * so the user can determine KTR logging overheads.
103  */
104 #if !defined(KTR_TESTLOG)
105 #define KTR_TESTLOG     KTR_ALL
106 #endif
107 KTR_INFO_MASTER(testlog);
108 KTR_INFO(KTR_TESTLOG, testlog, test1, 0, "test1", sizeof(void *) * 4);
109 KTR_INFO(KTR_TESTLOG, testlog, test2, 1, "test2", sizeof(void *) * 4);
110 KTR_INFO(KTR_TESTLOG, testlog, test3, 2, "test3", sizeof(void *) * 4);
111 KTR_INFO(KTR_TESTLOG, testlog, test4, 3, "test4", 0);
112 KTR_INFO(KTR_TESTLOG, testlog, test5, 4, "test5", 0);
113 KTR_INFO(KTR_TESTLOG, testlog, test6, 5, "test6", 0);
114 KTR_INFO(KTR_TESTLOG, testlog, pingpong, 6, "pingpong", 0);
115 #define logtest(name)   KTR_LOG(testlog_ ## name, 0, 0, 0, 0)
116 #define logtest_noargs(name)    KTR_LOG(testlog_ ## name)
117
118 MALLOC_DEFINE(M_KTR, "ktr", "ktr buffers");
119
120 SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RW, 0, "ktr");
121
122 static int32_t  ktr_cpumask = -1;
123 TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask);
124 SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, "");
125
126 static int      ktr_entries = KTR_ENTRIES;
127 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, "");
128
129 static int      ktr_version = KTR_VERSION;
130 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, &ktr_version, 0, "");
131
132 static int      ktr_stacktrace = 1;
133 SYSCTL_INT(_debug_ktr, OID_AUTO, stacktrace, CTLFLAG_RD, &ktr_stacktrace, 0, "");
134
135 static int      ktr_resynchronize = 0;
136 SYSCTL_INT(_debug_ktr, OID_AUTO, resynchronize, CTLFLAG_RW, &ktr_resynchronize, 0, "");
137
138 #if KTR_TESTLOG
139 static int      ktr_testlogcnt = 0;
140 SYSCTL_INT(_debug_ktr, OID_AUTO, testlogcnt, CTLFLAG_RW, &ktr_testlogcnt, 0, "");
141 static int      ktr_testipicnt = 0;
142 static int      ktr_testipicnt_remainder;
143 SYSCTL_INT(_debug_ktr, OID_AUTO, testipicnt, CTLFLAG_RW, &ktr_testipicnt, 0, "");
144 #endif
145
146 /*
147  * Give cpu0 a static buffer so the tracepoint facility can be used during
148  * early boot (note however that we still use a critical section, XXX).
149  */
150 static struct   ktr_entry ktr_buf0[KTR_ENTRIES];
151 static struct   ktr_entry *ktr_buf[MAXCPU] = { &ktr_buf0[0] };
152 static int      ktr_idx[MAXCPU];
153 #ifdef SMP
154 static int      ktr_sync_state = 0;
155 static int      ktr_sync_count;
156 static int64_t  ktr_sync_tsc;
157 #endif
158 struct callout  ktr_resync_callout;
159
160 #ifdef KTR_VERBOSE
161 int     ktr_verbose = KTR_VERBOSE;
162 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
163 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
164 #endif
165
166 #ifdef SMP
167 int64_t tsc_offsets[MAXCPU];
168 #else
169 int64_t tsc_offsets[1];
170 #endif
171
172 #if KTR_TESTLOG || KTR_ALL
173
174 static void
175 ktr_sysinit(void *dummy)
176 {
177         int i;
178
179         for(i = 1; i < ncpus; ++i) {
180                 ktr_buf[i] = malloc(KTR_ENTRIES * sizeof(struct ktr_entry),
181                                     M_KTR, M_WAITOK | M_ZERO);
182         }
183 }
184 SYSINIT(ktr_sysinit, SI_SUB_INTRINSIC, SI_ORDER_FIRST, ktr_sysinit, NULL);
185
186 #endif
187
188 /*
189  * Try to resynchronize the TSC's for all cpus.  This is really, really nasty.
190  * We have to send an IPIQ message to all remote cpus, wait until they 
191  * get into their IPIQ processing code loop, then do an even stricter hard
192  * loop to get the cpus as close to synchronized as we can to get the most
193  * accurate reading.
194  *
195  * This callback occurs on cpu0.
196  */
197 static void ktr_resync_callback(void *dummy);
198 static void ktr_pingpong_remote(void *dummy);
199
200 static void
201 ktr_resyncinit(void *dummy)
202 {
203         callout_init(&ktr_resync_callout);
204         callout_reset(&ktr_resync_callout, hz / 10, ktr_resync_callback, NULL);
205 }
206 SYSINIT(ktr_resync, SI_SUB_FINISH_SMP+1, SI_ORDER_ANY, ktr_resyncinit, NULL);
207
208 #ifdef SMP
209
210 static void ktr_resync_remote(void *dummy);
211 extern cpumask_t smp_active_mask;
212
213 /*
214  * We use a callout callback instead of a systimer because we cannot afford
215  * to preempt anyone to do this, or we might deadlock a spin-lock or 
216  * serializer between two cpus.
217  */
218 static
219 void 
220 ktr_resync_callback(void *dummy __unused)
221 {
222         int count;
223
224         KKASSERT(mycpu->gd_cpuid == 0);
225
226 #if KTR_TESTLOG
227         /*
228          * Test logging
229          */
230         if (ktr_testlogcnt) {
231                 --ktr_testlogcnt;
232                 cpu_disable_intr();
233                 logtest(test1);
234                 logtest(test2);
235                 logtest(test3);
236                 logtest_noargs(test4);
237                 logtest_noargs(test5);
238                 logtest_noargs(test6);
239                 cpu_enable_intr();
240         }
241
242         /*
243          * Test IPI messaging
244          */
245         if (ktr_testipicnt && ktr_testipicnt_remainder == 0 && ncpus > 1) {
246                 ktr_testipicnt_remainder = ktr_testipicnt;
247                 ktr_testipicnt = 0;
248                 lwkt_send_ipiq_bycpu(1, ktr_pingpong_remote, NULL);
249         }
250 #endif
251
252         /*
253          * Resynchronize the TSC
254          */
255         if (ktr_resynchronize == 0)
256                 goto done;
257         if ((cpu_feature & CPUID_TSC) == 0)
258                 return;
259
260         /*
261          * Send the synchronizing IPI and wait for all cpus to get into
262          * their spin loop.  We must process incoming IPIs while waiting
263          * to avoid a deadlock.
264          */
265         crit_enter();
266         ktr_sync_count = 0;
267         ktr_sync_state = 1;
268         ktr_sync_tsc = rdtsc();
269         count = lwkt_send_ipiq_mask(mycpu->gd_other_cpus & smp_active_mask,
270                                     (ipifunc1_t)ktr_resync_remote, NULL);
271         while (ktr_sync_count != count)
272                 lwkt_process_ipiq();
273
274         /*
275          * Continuously update the TSC for cpu 0 while waiting for all other
276          * cpus to finish stage 2.
277          */
278         cpu_disable_intr();
279         ktr_sync_tsc = rdtsc();
280         cpu_sfence();
281         ktr_sync_state = 2;
282         cpu_sfence();
283         while (ktr_sync_count != 0) {
284                 ktr_sync_tsc = rdtsc();
285                 cpu_lfence();
286                 cpu_nop();
287         }
288         cpu_enable_intr();
289         crit_exit();
290         ktr_sync_state = 0;
291 done:
292         callout_reset(&ktr_resync_callout, hz / 10, ktr_resync_callback, NULL);
293 }
294
295 /*
296  * The remote-end of the KTR synchronization protocol runs on all cpus except
297  * cpu 0.  Since this is an IPI function, it is entered with the current
298  * thread in a critical section.
299  */
300 static void
301 ktr_resync_remote(void *dummy __unused)
302 {
303         volatile int64_t tsc1 = ktr_sync_tsc;
304         volatile int64_t tsc2;
305
306         /*
307          * Inform the master that we have entered our hard loop.
308          */
309         KKASSERT(ktr_sync_state == 1);
310         atomic_add_int(&ktr_sync_count, 1);
311         while (ktr_sync_state == 1) {
312                 lwkt_process_ipiq();
313         }
314
315         /*
316          * Now the master is in a hard loop, synchronize the TSC and
317          * we are done.
318          */
319         cpu_disable_intr();
320         KKASSERT(ktr_sync_state == 2);
321         tsc2 = ktr_sync_tsc;
322         if (tsc2 > tsc1)
323                 tsc_offsets[mycpu->gd_cpuid] = rdtsc() - tsc2;
324         atomic_subtract_int(&ktr_sync_count, 1);
325         cpu_enable_intr();
326 }
327
328 static
329 void
330 ktr_pingpong_remote(void *dummy __unused)
331 {
332         logtest_noargs(pingpong);
333         if (ktr_testipicnt_remainder) {
334                 --ktr_testipicnt_remainder;
335                 lwkt_send_ipiq_bycpu(1 - mycpu->gd_cpuid, 
336                                      ktr_pingpong_remote, NULL);
337         }
338 }
339
340 #else   /* !SMP */
341
342 /*
343  * The resync callback for UP doesn't do anything other then run the test
344  * log messages.  If test logging is not enabled, don't bother resetting
345  * the callout.
346  */
347 static
348 void 
349 ktr_resync_callback(void *dummy __unused)
350 {
351 #if KTR_TESTLOG
352         /*
353          * Test logging
354          */
355         if (ktr_testlogcnt) {
356                 --ktr_testlogcnt;
357                 cpu_disable_intr();
358                 logtest(test1);
359                 logtest(test2);
360                 logtest(test3);
361                 logtest_noargs(test4);
362                 logtest_noargs(test5);
363                 logtest_noargs(test6);
364                 cpu_enable_intr();
365         }
366         callout_reset(&ktr_resync_callout, hz / 10, ktr_resync_callback, NULL);
367 #endif
368 }
369
370 #endif
371
372 /*
373  * KTR_WRITE_ENTRY - Primary entry point for kernel trace logging
374  */
375 static __inline
376 void
377 ktr_write_entry(struct ktr_info *info, const char *file, int line,
378                 const void *ptr)
379 {
380         struct ktr_entry *entry;
381         int cpu;
382
383         cpu = mycpu->gd_cpuid;
384         if (ktr_buf[cpu]) {
385                 crit_enter();
386                 entry = ktr_buf[cpu] + (ktr_idx[cpu] & KTR_ENTRIES_MASK);
387                 ++ktr_idx[cpu];
388                 if (cpu_feature & CPUID_TSC) {
389 #ifdef SMP
390                         entry->ktr_timestamp = rdtsc() - tsc_offsets[cpu];
391 #else
392                         entry->ktr_timestamp = rdtsc();
393 #endif
394                 } else {
395                         entry->ktr_timestamp = get_approximate_time_t();
396                 }
397                 entry->ktr_info = info;
398                 entry->ktr_file = file;
399                 entry->ktr_line = line;
400                 crit_exit();
401                 if (info->kf_data_size > KTR_BUFSIZE)
402                         bcopyi(ptr, entry->ktr_data, KTR_BUFSIZE);
403                 else if (info->kf_data_size)
404                         bcopyi(ptr, entry->ktr_data, info->kf_data_size);
405                 if (ktr_stacktrace)
406                         cpu_ktr_caller(entry);
407         }
408 #ifdef KTR_VERBOSE
409         if (ktr_verbose && info->kf_format) {
410 #ifdef SMP
411                 printf("cpu%d ", cpu);
412 #endif
413                 if (ktr_verbose > 1) {
414                         printf("%s.%d\t", entry->ktr_file, entry->ktr_line);
415                 }
416                 vprintf(info->kf_format, ptr);
417                 printf("\n");
418         }
419 #endif
420 }
421
422 void
423 ktr_log(struct ktr_info *info, const char *file, int line, ...)
424 {
425         __va_list va;
426
427         if (panicstr == NULL) {
428                 __va_start(va, line);
429                 ktr_write_entry(info, file, line, va);
430                 __va_end(va);
431         }
432 }
433
434 void
435 ktr_log_ptr(struct ktr_info *info, const char *file, int line, const void *ptr)
436 {
437         if (panicstr == NULL) {
438                 ktr_write_entry(info, file, line, ptr);
439         }
440 }
441
442 #ifdef DDB
443
444 #define NUM_LINES_PER_PAGE      19
445
446 struct tstate {
447         int     cur;
448         int     first;
449 };
450
451 static  int db_ktr_verbose;
452 static  int db_mach_vtrace(int cpu, struct ktr_entry *kp, int idx);
453
454 DB_SHOW_COMMAND(ktr, db_ktr_all)
455 {
456         int a_flag = 0;
457         int c;
458         int nl = 0;
459         int i;
460         struct tstate tstate[MAXCPU];
461         int printcpu = -1;
462
463         for(i = 0; i < ncpus; i++) {
464                 tstate[i].first = -1;
465                 tstate[i].cur = ktr_idx[i] & KTR_ENTRIES_MASK;
466         }
467         db_ktr_verbose = 0;
468         while ((c = *(modif++)) != '\0') {
469                 if (c == 'v') {
470                         db_ktr_verbose = 1;
471                 }
472                 else if (c == 'a') {
473                         a_flag = 1;
474                 }
475                 else if (c == 'c') {
476                         printcpu = 0;
477                         while ((c = *(modif++)) != '\0') {
478                                 if (isdigit(c)) {
479                                         printcpu *= 10;
480                                         printcpu += c - '0';
481                                 }
482                                 else {
483                                         modif++;
484                                         break;
485                                 }
486                         }
487                         modif--;
488                 }
489         }
490         if (printcpu > ncpus - 1) {
491                 db_printf("Invalid cpu number\n");
492                 return;
493         }
494         /*
495          * Lopp throug all the buffers and print the content of them, sorted
496          * by the timestamp.
497          */
498         while (1) {
499                 int counter;
500                 u_int64_t highest_ts;
501                 int highest_cpu;
502                 struct ktr_entry *kp;
503
504                 if (a_flag == 1 && cncheckc() != -1)
505                         return;
506                 highest_ts = 0;
507                 highest_cpu = -1;
508                 /*
509                  * Find the lowest timestamp
510                  */
511                 for (i = 0, counter = 0; i < ncpus; i++) {
512                         if (ktr_buf[i] == NULL)
513                                 continue;
514                         if (printcpu != -1 && printcpu != i)
515                                 continue;
516                         if (tstate[i].cur == -1) {
517                                 counter++;
518                                 if (counter == ncpus) {
519                                         db_printf("--- End of trace buffer ---\n");
520                                         return;
521                                 }
522                                 continue;
523                         }
524                         if (ktr_buf[i][tstate[i].cur].ktr_timestamp > highest_ts) {
525                                 highest_ts = ktr_buf[i][tstate[i].cur].ktr_timestamp;
526                                 highest_cpu = i;
527                         }
528                 }
529                 i = highest_cpu;
530                 KKASSERT(i != -1);
531                 kp = &ktr_buf[i][tstate[i].cur];
532                 if (tstate[i].first == -1)
533                         tstate[i].first = tstate[i].cur;
534                 if (--tstate[i].cur < 0)
535                         tstate[i].cur = KTR_ENTRIES - 1;
536                 if (tstate[i].first == tstate[i].cur) {
537                         db_mach_vtrace(i, kp, tstate[i].cur + 1);
538                         tstate[i].cur = -1;
539                         continue;
540                 }
541                 if (ktr_buf[i][tstate[i].cur].ktr_info == NULL)
542                         tstate[i].cur = -1;
543                 if (db_more(&nl) == -1)
544                         break;
545                 if (db_mach_vtrace(i, kp, tstate[i].cur + 1) == 0)
546                         tstate[i].cur = -1;
547         }
548 }
549
550 static int
551 db_mach_vtrace(int cpu, struct ktr_entry *kp, int idx)
552 {
553         if (kp->ktr_info == NULL)
554                 return(0);
555 #ifdef SMP
556         db_printf("cpu%d ", cpu);
557 #endif
558         db_printf("%d: ", idx);
559         if (db_ktr_verbose) {
560                 db_printf("%10.10lld %s.%d\t", (long long)kp->ktr_timestamp,
561                     kp->ktr_file, kp->ktr_line);
562         }
563         db_printf("%s\t", kp->ktr_info->kf_name);
564         db_printf("from(%p,%p) ", kp->ktr_caller1, kp->ktr_caller2);
565         if (kp->ktr_info->kf_format) {
566                 int32_t *args = kp->ktr_data;
567                 db_printf(kp->ktr_info->kf_format,
568                           args[0], args[1], args[2], args[3],
569                           args[4], args[5], args[6], args[7],
570                           args[8], args[9], args[10], args[11]);
571             
572         }
573         db_printf("\n");
574
575         return(1);
576 }
577
578 #endif  /* DDB */