xref: /freebsd/lib/libpmc/pmclog.h (revision 2449fa9c4d8e60cca863396dc3c7d67cbc16359f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005-2007 Joseph Koshy
5  * Copyright (c) 2007 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * Portions of this software were developed by A. Joseph Koshy under
9  * sponsorship from the FreeBSD Foundation and Google, Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef	_PMCLOG_H_
34 #define	_PMCLOG_H_
35 
36 #include <sys/cdefs.h>
37 #include <sys/pmclog.h>
38 
39 enum pmclog_state {
40 	PMCLOG_OK,
41 	PMCLOG_EOF,
42 	PMCLOG_REQUIRE_DATA,
43 	PMCLOG_ERROR
44 };
45 
46 struct pmclog_ev_callchain {
47 	uint32_t	pl_pid;
48 	uint32_t	pl_tid;
49 	uint32_t	pl_pmcid;
50 	uint32_t	pl_cpuflags;
51 	uint32_t	pl_cpuflags2;
52 	uint32_t	pl_npc;
53 	uintfptr_t	pl_pc[PMC_CALLCHAIN_DEPTH_MAX];
54 };
55 
56 struct pmclog_ev_dropnotify {
57 };
58 
59 struct pmclog_ev_closelog {
60 };
61 
62 struct pmclog_ev_initialize {
63 	uint32_t	pl_version;
64 	uint32_t	pl_arch;
65 	uint64_t	pl_tsc_freq;
66 	struct timespec pl_ts;
67 	char		pl_cpuid[PATH_MAX];
68 };
69 
70 struct pmclog_ev_map_in {
71 	pid_t		pl_pid;
72 	union {
73 		uint32_t	pl_u;
74 		struct {
75 			uint8_t	pl_pageshift;
76 			uint8_t	pl_pad[3];
77 		};
78 	};
79 	uintfptr_t	pl_start;
80 	char		pl_pathname[PATH_MAX];
81 };
82 
83 struct pmclog_ev_map_out {
84 	pid_t		pl_pid;
85 	uintfptr_t	pl_start;
86 	uintfptr_t	pl_end;
87 };
88 
89 struct pmclog_ev_pcsample {
90 	uintfptr_t	pl_pc;
91 	pid_t		pl_pid;
92 	pid_t		pl_tid;
93 	pmc_id_t	pl_pmcid;
94 	uint32_t	pl_flags;
95 	uint32_t	pl_usermode;
96 };
97 
98 struct pmclog_ev_pmcallocate {
99 	const char *	pl_evname;
100 	uint64_t	pl_rate;
101 	uint32_t	pl_event;
102 	uint32_t	pl_flags;
103 	pmc_id_t	pl_pmcid;
104 };
105 
106 struct pmclog_ev_pmcallocatedyn {
107 	char 		pl_evname[PMC_NAME_MAX];
108 	uint32_t	pl_event;
109 	uint32_t	pl_flags;
110 	pmc_id_t	pl_pmcid;
111 };
112 
113 struct pmclog_ev_pmcattach {
114 	pmc_id_t	pl_pmcid;
115 	pid_t		pl_pid;
116 	char		pl_pathname[PATH_MAX];
117 };
118 
119 struct pmclog_ev_pmcdetach {
120 	pmc_id_t	pl_pmcid;
121 	pid_t		pl_pid;
122 };
123 
124 struct pmclog_ev_proccsw {
125 	pid_t		pl_pid;
126 	pid_t		pl_tid;
127 	pmc_id_t	pl_pmcid;
128 	pmc_value_t	pl_value;
129 };
130 
131 struct pmclog_ev_proccreate {
132 	pid_t		pl_pid;
133 	uint32_t	pl_flags;
134 	char		pl_pcomm[MAXCOMLEN+1];
135 };
136 
137 struct pmclog_ev_procexec {
138 	pid_t		pl_pid;
139 	pmc_id_t	pl_pmcid;
140 	uintptr_t	pl_baseaddr;
141 	uintptr_t	pl_dynaddr;
142 	char		pl_pathname[PATH_MAX];
143 };
144 
145 struct pmclog_ev_procexit {
146 	uint32_t	pl_pid;
147 	pmc_id_t	pl_pmcid;
148 	pmc_value_t	pl_value;
149 };
150 
151 struct pmclog_ev_procfork {
152 	pid_t		pl_oldpid;
153 	pid_t		pl_newpid;
154 };
155 
156 struct pmclog_ev_sysexit {
157 	pid_t		pl_pid;
158 };
159 
160 struct pmclog_ev_threadcreate {
161 	pid_t		pl_tid;
162 	pid_t		pl_pid;
163 	uint32_t	pl_flags;
164 	char		pl_tdname[MAXCOMLEN+1];
165 };
166 
167 struct pmclog_ev_threadexit {
168 	pid_t		pl_tid;
169 };
170 
171 struct pmclog_ev_userdata {
172 	uint32_t	pl_userdata;
173 };
174 
175 struct pmclog_ev {
176 	enum pmclog_state pl_state;	/* state after 'get_event()' */
177 	off_t		  pl_offset;	/* byte offset in stream */
178 	size_t		  pl_count;	/* count of records so far */
179 	union {
180 		uint64_t	  pl_tsc;	/* TSC timestamp */
181 		struct timespec pl_ts;		/* log entry timestamp (legacy) */
182 	};
183 	enum pmclog_type  pl_type;	/* type of log entry */
184 	void		 *pl_data;
185 	int		  pl_len;
186 	union { 			/* log entry data */
187 		struct pmclog_ev_callchain	pl_cc;
188 		struct pmclog_ev_closelog	pl_cl;
189 		struct pmclog_ev_dropnotify	pl_dn;
190 		struct pmclog_ev_initialize	pl_i;
191 		struct pmclog_ev_map_in		pl_mi;
192 		struct pmclog_ev_map_out	pl_mo;
193 		struct pmclog_ev_pmcallocate	pl_a;
194 		struct pmclog_ev_pmcallocatedyn	pl_ad;
195 		struct pmclog_ev_pmcattach	pl_t;
196 		struct pmclog_ev_pmcdetach	pl_d;
197 		struct pmclog_ev_proccsw	pl_c;
198 		struct pmclog_ev_proccreate	pl_pc;
199 		struct pmclog_ev_procexec	pl_x;
200 		struct pmclog_ev_procexit	pl_e;
201 		struct pmclog_ev_procfork	pl_f;
202 		struct pmclog_ev_sysexit	pl_se;
203 		struct pmclog_ev_threadcreate	pl_tc;
204 		struct pmclog_ev_threadexit	pl_te;
205 		struct pmclog_ev_userdata	pl_u;
206 	} pl_u;
207 };
208 
209 enum pmclog_parser_state {
210 	PL_STATE_NEW_RECORD,		/* in-between records */
211 	PL_STATE_EXPECTING_HEADER,	/* header being read */
212 	PL_STATE_PARTIAL_RECORD,	/* header present but not the record */
213 	PL_STATE_ERROR			/* parsing error encountered */
214 };
215 
216 struct pmclog_parse_state {
217 	enum pmclog_parser_state ps_state;
218 	enum pmc_cputype	ps_arch;	/* log file architecture */
219 	uint32_t		ps_version;	/* hwpmc version */
220 	int			ps_initialized;	/* whether initialized */
221 	int			ps_count;	/* count of records processed */
222 	off_t			ps_offset;	/* stream byte offset */
223 	union pmclog_entry	ps_saved;	/* saved partial log entry */
224 	int			ps_svcount;	/* #bytes saved */
225 	int			ps_fd;		/* active fd or -1 */
226 	char			*ps_buffer;	/* scratch buffer if fd != -1 */
227 	char			*ps_data;	/* current parse pointer */
228 	char			*ps_cpuid;	/* log cpuid */
229 	size_t			ps_len;		/* length of buffered data */
230 };
231 
232 #define	PMCLOG_FD_NONE				(-1)
233 
234 __BEGIN_DECLS
235 void	*pmclog_open(int _fd);
236 int	pmclog_feed(void *_cookie, char *_data, int _len);
237 int	pmclog_read(void *_cookie, struct pmclog_ev *_ev);
238 void	pmclog_close(void *_cookie);
239 __END_DECLS
240 
241 #endif
242