xref: /freebsd/sys/kern/kern_tslog.c (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
1 /*-
2  * Copyright (c) 2017 Colin Percival
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/sbuf.h>
32 #include <sys/sysctl.h>
33 #include <sys/systm.h>
34 #include <sys/tslog.h>
35 
36 #include <machine/atomic.h>
37 #include <machine/cpu.h>
38 
39 #ifndef TSLOGSIZE
40 #define TSLOGSIZE 262144
41 #endif
42 
43 static volatile long nrecs = 0;
44 static struct timestamp {
45 	void * td;
46 	int type;
47 	const char * f;
48 	const char * s;
49 	uint64_t tsc;
50 } timestamps[TSLOGSIZE];
51 
52 void
53 tslog(void * td, int type, const char * f, const char * s)
54 {
55 	uint64_t tsc = get_cyclecount();
56 	long pos;
57 
58 	/* Grab a slot. */
59 	pos = atomic_fetchadd_long(&nrecs, 1);
60 
61 	/* Store record. */
62 	if (pos < nitems(timestamps)) {
63 		timestamps[pos].td = td;
64 		timestamps[pos].type = type;
65 		timestamps[pos].f = f;
66 		timestamps[pos].s = s;
67 		timestamps[pos].tsc = tsc;
68 	}
69 }
70 
71 static int
72 sysctl_debug_tslog(SYSCTL_HANDLER_ARGS)
73 {
74 	int error;
75 	struct sbuf *sb;
76 	size_t i, limit;
77 
78 	/*
79 	 * This code can race against the code in tslog() which stores
80 	 * records: Theoretically we could end up reading a record after
81 	 * its slots have been reserved but before it has been written.
82 	 * Since this code takes orders of magnitude longer to run than
83 	 * tslog() takes to write a record, it is highly unlikely that
84 	 * anyone will ever experience this race.
85 	 */
86 	sb = sbuf_new_for_sysctl(NULL, NULL, 1024, req);
87 	limit = MIN(nrecs, nitems(timestamps));
88 	for (i = 0; i < limit; i++) {
89 		sbuf_printf(sb, "%p", timestamps[i].td);
90 		sbuf_printf(sb, " %llu",
91 		    (unsigned long long)timestamps[i].tsc);
92 		switch (timestamps[i].type) {
93 		case TS_ENTER:
94 			sbuf_printf(sb, " ENTER");
95 			break;
96 		case TS_EXIT:
97 			sbuf_printf(sb, " EXIT");
98 			break;
99 		case TS_THREAD:
100 			sbuf_printf(sb, " THREAD");
101 			break;
102 		case TS_EVENT:
103 			sbuf_printf(sb, " EVENT");
104 			break;
105 		}
106 		sbuf_printf(sb, " %s", timestamps[i].f ? timestamps[i].f : "(null)");
107 		if (timestamps[i].s)
108 			sbuf_printf(sb, " %s\n", timestamps[i].s);
109 		else
110 			sbuf_printf(sb, "\n");
111 	}
112 	error = sbuf_finish(sb);
113 	sbuf_delete(sb);
114 	return (error);
115 }
116 
117 SYSCTL_PROC(_debug, OID_AUTO, tslog, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
118     0, 0, sysctl_debug_tslog, "", "Dump recorded event timestamps");
119