xref: /freebsd/stand/libsa/tslog.c (revision e193d3ba337757a27148a38494d3df106b826d1c)
1*e193d3baSColin Percival /*-
2*e193d3baSColin Percival  * Copyright (c) 2021 Colin Percival
3*e193d3baSColin Percival  * All rights reserved.
4*e193d3baSColin Percival  *
5*e193d3baSColin Percival  * Redistribution and use in source and binary forms, with or without
6*e193d3baSColin Percival  * modification, are permitted provided that the following conditions
7*e193d3baSColin Percival  * are met:
8*e193d3baSColin Percival  * 1. Redistributions of source code must retain the above copyright
9*e193d3baSColin Percival  *    notice, this list of conditions and the following disclaimer.
10*e193d3baSColin Percival  * 2. Redistributions in binary form must reproduce the above copyright
11*e193d3baSColin Percival  *    notice, this list of conditions and the following disclaimer in the
12*e193d3baSColin Percival  *    documentation and/or other materials provided with the distribution.
13*e193d3baSColin Percival  *
14*e193d3baSColin Percival  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*e193d3baSColin Percival  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*e193d3baSColin Percival  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*e193d3baSColin Percival  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*e193d3baSColin Percival  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*e193d3baSColin Percival  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*e193d3baSColin Percival  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*e193d3baSColin Percival  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*e193d3baSColin Percival  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*e193d3baSColin Percival  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*e193d3baSColin Percival  * SUCH DAMAGE.
25*e193d3baSColin Percival  */
26*e193d3baSColin Percival 
27*e193d3baSColin Percival #include <sys/cdefs.h>
28*e193d3baSColin Percival __FBSDID("$FreeBSD$");
29*e193d3baSColin Percival 
30*e193d3baSColin Percival #include <sys/types.h>
31*e193d3baSColin Percival 
32*e193d3baSColin Percival #include <machine/cpufunc.h>
33*e193d3baSColin Percival 
34*e193d3baSColin Percival #include <stand.h>
35*e193d3baSColin Percival 
36*e193d3baSColin Percival /* Buffer for holding tslog data in string format. */
37*e193d3baSColin Percival static char * tslog_buf = NULL;
38*e193d3baSColin Percival static size_t tslog_buflen = 0;
39*e193d3baSColin Percival static size_t tslog_bufpos = 0;
40*e193d3baSColin Percival 
41*e193d3baSColin Percival void
42*e193d3baSColin Percival tslog_setbuf(void * buf, size_t len)
43*e193d3baSColin Percival {
44*e193d3baSColin Percival 
45*e193d3baSColin Percival 	tslog_buf = (char *)buf;
46*e193d3baSColin Percival 	tslog_buflen = len;
47*e193d3baSColin Percival 	tslog_bufpos = 0;
48*e193d3baSColin Percival }
49*e193d3baSColin Percival 
50*e193d3baSColin Percival void
51*e193d3baSColin Percival tslog_getbuf(void ** buf, size_t * len)
52*e193d3baSColin Percival {
53*e193d3baSColin Percival 
54*e193d3baSColin Percival 	*buf = (void *)tslog_buf;
55*e193d3baSColin Percival 	*len = tslog_bufpos;
56*e193d3baSColin Percival }
57*e193d3baSColin Percival 
58*e193d3baSColin Percival void
59*e193d3baSColin Percival tslog(const char * type, const char * f, const char * s)
60*e193d3baSColin Percival {
61*e193d3baSColin Percival #if defined(__amd64__) || defined(__i386__)
62*e193d3baSColin Percival 	uint64_t tsc = rdtsc();
63*e193d3baSColin Percival #else
64*e193d3baSColin Percival 	uint64_t tsc = 0;
65*e193d3baSColin Percival #endif
66*e193d3baSColin Percival 	int len;
67*e193d3baSColin Percival 
68*e193d3baSColin Percival 	/* If we have no buffer, do nothing. */
69*e193d3baSColin Percival 	if (tslog_buf == NULL)
70*e193d3baSColin Percival 		return;
71*e193d3baSColin Percival 
72*e193d3baSColin Percival 	/* Append to existing buffer, if we have enough space. */
73*e193d3baSColin Percival 	len = snprintf(&tslog_buf[tslog_bufpos],
74*e193d3baSColin Percival 	    tslog_buflen - tslog_bufpos, "0x0 %llu %s %s%s%s\n",
75*e193d3baSColin Percival 	    (unsigned long long)tsc, type, f, s ? " " : "", s ? s : "");
76*e193d3baSColin Percival 	if ((len > 0) && (tslog_bufpos + len <= tslog_buflen))
77*e193d3baSColin Percival 		tslog_bufpos += len;
78*e193d3baSColin Percival }
79