xref: /freebsd/sys/sys/pmclog.h (revision e51ef8ae490fc9f73191f33e7ad388c2511c454a)
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	_SYS_PMCLOG_H_
34 #define	_SYS_PMCLOG_H_
35 
36 #include <sys/pmc.h>
37 
38 enum pmclog_type {
39 	/* V1 ABI */
40 	PMCLOG_TYPE_CLOSELOG = 1,
41 	PMCLOG_TYPE_DROPNOTIFY = 2,
42 	PMCLOG_TYPE_INITIALIZE = 3,
43 
44 	PMCLOG_TYPE_PMCALLOCATE = 5,
45 	PMCLOG_TYPE_PMCATTACH = 6,
46 	PMCLOG_TYPE_PMCDETACH = 7,
47 	PMCLOG_TYPE_PROCCSW = 8,
48 	PMCLOG_TYPE_PROCEXEC = 9,
49 	PMCLOG_TYPE_PROCEXIT = 10,
50 	PMCLOG_TYPE_PROCFORK = 11,
51 	PMCLOG_TYPE_SYSEXIT = 12,
52 	PMCLOG_TYPE_USERDATA = 13,
53 	/*
54 	 * V2 ABI
55 	 *
56 	 * The MAP_{IN,OUT} event types obsolete the MAPPING_CHANGE
57 	 * event type.  The CALLCHAIN event type obsoletes the
58 	 * PCSAMPLE event type.
59 	 */
60 	PMCLOG_TYPE_MAP_IN = 14,
61 	PMCLOG_TYPE_MAP_OUT = 15,
62 	PMCLOG_TYPE_CALLCHAIN = 16,
63 	/*
64 	 * V3 ABI
65 	 *
66 	 * New variant of PMCLOG_TYPE_PMCALLOCATE for dynamic event.
67 	 */
68 	PMCLOG_TYPE_PMCALLOCATEDYN = 17,
69 	/*
70 	 * V6 ABI
71 	 */
72 	PMCLOG_TYPE_THR_CREATE = 18,
73 	PMCLOG_TYPE_THR_EXIT = 19,
74 	PMCLOG_TYPE_PROC_CREATE = 20
75 };
76 
77 /*
78  * A log entry descriptor comprises of a 32 bit header and a 64 bit
79  * time stamp followed by as many 32 bit words are required to record
80  * the event.
81  *
82  * Header field format:
83  *
84  *  31           24           16                                   0
85  *   +------------+------------+-----------------------------------+
86  *   |    MAGIC   |    TYPE    |               LENGTH              |
87  *   +------------+------------+-----------------------------------+
88  *
89  * MAGIC 	is the constant PMCLOG_HEADER_MAGIC.
90  * TYPE  	contains a value of type enum pmclog_type.
91  * LENGTH	contains the length of the event record, in bytes.
92  */
93 
94 #define	PMCLOG_ENTRY_HEADER				\
95 	uint32_t		pl_header;			\
96 	uint32_t		pl_spare;			\
97 	uint64_t		pl_tsc;			\
98 
99 struct pmclog_header {
100 	PMCLOG_ENTRY_HEADER;
101 };
102 
103 /*
104  * The following structures are used to describe the size of each kind
105  * of log entry to sizeof().  To keep the compiler from adding
106  * padding, the fields of each structure are aligned to their natural
107  * boundaries, and the structures are marked as 'packed'.
108  *
109  * The actual reading and writing of the log file is always in terms
110  * of 4 byte quantities.
111  */
112 
113 struct pmclog_callchain {
114 	PMCLOG_ENTRY_HEADER
115 	uint32_t		pl_pid;
116 	uint32_t		pl_tid;
117 	uint32_t		pl_pmcid;
118 	uint32_t		pl_cpuflags;
119 	/* 8 byte aligned */
120 	uintptr_t		pl_pc[PMC_CALLCHAIN_DEPTH_MAX];
121 } __packed;
122 
123 #define	PMC_CALLCHAIN_CPUFLAGS_TO_CPU(CF)	(((CF) >> 16) & 0xFFFF)
124 #define	PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(CF)	((CF) & PMC_CC_F_USERSPACE)
125 #define	PMC_CALLCHAIN_TO_CPUFLAGS(CPU,FLAGS)	\
126 	(((CPU) << 16) | ((FLAGS) & 0xFFFF))
127 
128 /*
129  * If the multipart flag is set, then pl_pc contains multiple data types.  The
130  * first 8 bytes is a header made up of a 1 byte type and 1 byte length that
131  * describes the use of the remaining pl_pc array.
132  */
133 
134 #define PMC_MULTIPART_HEADER_LENGTH	8
135 #define PMC_MULTIPART_HEADER_ENTRIES	4
136 
137 #define	PMC_CC_MULTIPART_NONE		0
138 #define	PMC_CC_MULTIPART_CALLCHAIN	1
139 #define	PMC_CC_MULTIPART_IBS_FETCH	2
140 #define	PMC_CC_MULTIPART_IBS_OP		3
141 
142 struct pmclog_closelog {
143 	PMCLOG_ENTRY_HEADER
144 };
145 
146 struct pmclog_dropnotify {
147 	PMCLOG_ENTRY_HEADER
148 };
149 
150 struct pmclog_initialize {
151 	PMCLOG_ENTRY_HEADER
152 	uint32_t		pl_version;	/* driver version */
153 	uint32_t		pl_cpu;		/* enum pmc_cputype */
154 	uint64_t		pl_tsc_freq;
155 	struct timespec	pl_ts;
156 	char			pl_cpuid[PMC_CPUID_LEN];
157 } __packed;
158 
159 struct pmclog_map_in {
160 	PMCLOG_ENTRY_HEADER
161 	uint32_t		pl_pid;
162 	uint32_t		pl_pad;
163 	uintfptr_t		pl_start;	/* 8 byte aligned */
164 	char			pl_pathname[PATH_MAX];
165 } __packed;
166 
167 struct pmclog_map_out {
168 	PMCLOG_ENTRY_HEADER
169 	uint32_t		pl_pid;
170 	uint32_t		pl_pad;
171 	uintfptr_t		pl_start;	/* 8 byte aligned */
172 	uintfptr_t		pl_end;
173 } __packed;
174 
175 struct pmclog_pmcallocate {
176 	PMCLOG_ENTRY_HEADER
177 	uint32_t		pl_pmcid;
178 	uint32_t		pl_event;
179 	uint32_t		pl_flags;
180 	uint32_t		pl_pad;
181 	uint64_t		pl_rate;
182 } __packed;
183 
184 struct pmclog_pmcattach {
185 	PMCLOG_ENTRY_HEADER
186 	uint32_t		pl_pmcid;
187 	uint32_t		pl_pid;
188 	char			pl_pathname[PATH_MAX];
189 } __packed;
190 
191 struct pmclog_pmcdetach {
192 	PMCLOG_ENTRY_HEADER
193 	uint32_t		pl_pmcid;
194 	uint32_t		pl_pid;
195 } __packed;
196 
197 struct pmclog_proccsw {
198 	PMCLOG_ENTRY_HEADER
199 	uint64_t		pl_value;	/* keep 8 byte aligned */
200 	uint32_t		pl_pmcid;
201 	uint32_t		pl_pid;
202 	uint32_t		pl_tid;
203 	uint32_t		pl_pad;
204 } __packed;
205 
206 struct pmclog_proccreate {
207 	PMCLOG_ENTRY_HEADER
208 	uint32_t		pl_pid;
209 	uint32_t		pl_flags;
210 	char			pl_pcomm[MAXCOMLEN+1];	/* keep 8 byte aligned */
211 } __packed;
212 
213 struct pmclog_procexec {
214 	PMCLOG_ENTRY_HEADER
215 	uint32_t		pl_pid;
216 	uint32_t		pl_pmcid;
217 	/* keep 8 byte aligned */
218 	uintptr_t		pl_base;	/* AT_BASE */
219 	/* keep 8 byte aligned */
220 	uintptr_t		pl_dyn;		/* PIE load base */
221 	char			pl_pathname[PATH_MAX];
222 } __packed;
223 
224 struct pmclog_procexit {
225 	PMCLOG_ENTRY_HEADER
226 	uint32_t		pl_pmcid;
227 	uint32_t		pl_pid;
228 	uint64_t		pl_value;	/* keep 8 byte aligned */
229 } __packed;
230 
231 struct pmclog_procfork {
232 	PMCLOG_ENTRY_HEADER
233 	uint32_t		pl_oldpid;
234 	uint32_t		pl_newpid;
235 } __packed;
236 
237 struct pmclog_sysexit {
238 	PMCLOG_ENTRY_HEADER
239 	uint32_t		pl_pid;
240 	uint32_t		pl_pad;
241 } __packed;
242 
243 struct pmclog_threadcreate {
244 	PMCLOG_ENTRY_HEADER
245 	uint32_t		pl_tid;
246 	uint32_t		pl_pid;
247 	uint32_t		pl_flags;
248 	uint32_t		pl_pad;
249 	char			pl_tdname[MAXCOMLEN+1];	/* keep 8 byte aligned */
250 } __packed;
251 
252 struct pmclog_threadexit {
253 	PMCLOG_ENTRY_HEADER
254 	uint32_t		pl_tid;
255 	uint32_t		pl_pad;
256 } __packed;
257 
258 struct pmclog_userdata {
259 	PMCLOG_ENTRY_HEADER
260 	uint32_t		pl_userdata;
261 	uint32_t		pl_pad;
262 } __packed;
263 
264 struct pmclog_pmcallocatedyn {
265 	PMCLOG_ENTRY_HEADER
266 	uint32_t		pl_pmcid;
267 	uint32_t		pl_event;
268 	uint32_t		pl_flags;
269 	uint32_t		pl_pad;
270 	char			pl_evname[PMC_NAME_MAX];
271 } __packed;
272 
273 union pmclog_entry {		/* only used to size scratch areas */
274 	struct pmclog_callchain		pl_cc;
275 	struct pmclog_closelog		pl_cl;
276 	struct pmclog_dropnotify	pl_dn;
277 	struct pmclog_initialize	pl_i;
278 	struct pmclog_map_in		pl_mi;
279 	struct pmclog_map_out		pl_mo;
280 	struct pmclog_pmcallocate	pl_a;
281 	struct pmclog_pmcallocatedyn	pl_ad;
282 	struct pmclog_pmcattach		pl_t;
283 	struct pmclog_pmcdetach		pl_d;
284 	struct pmclog_proccsw		pl_c;
285 	struct pmclog_proccreate	pl_pc;
286 	struct pmclog_procexec		pl_x;
287 	struct pmclog_procexit		pl_e;
288 	struct pmclog_procfork		pl_f;
289 	struct pmclog_sysexit		pl_se;
290 	struct pmclog_threadcreate	pl_tc;
291 	struct pmclog_threadexit	pl_te;
292 	struct pmclog_userdata		pl_u;
293 };
294 
295 #define	PMCLOG_HEADER_MAGIC					0xEEU
296 
297 #define	PMCLOG_HEADER_TO_LENGTH(H)				\
298 	((H) & 0x0000FFFF)
299 #define	PMCLOG_HEADER_TO_TYPE(H)				\
300 	(((H) & 0x00FF0000) >> 16)
301 #define	PMCLOG_HEADER_TO_MAGIC(H)				\
302 	(((H) & 0xFF000000) >> 24)
303 #define	PMCLOG_HEADER_CHECK_MAGIC(H)				\
304 	(PMCLOG_HEADER_TO_MAGIC(H) == PMCLOG_HEADER_MAGIC)
305 
306 #ifdef	_KERNEL
307 
308 /*
309  * Prototypes
310  */
311 int	pmclog_configure_log(struct pmc_mdep *_md, struct pmc_owner *_po,
312     int _logfd);
313 int	pmclog_deconfigure_log(struct pmc_owner *_po);
314 int	pmclog_flush(struct pmc_owner *_po, int force);
315 int	pmclog_close(struct pmc_owner *_po);
316 void	pmclog_initialize(void);
317 int	pmclog_proc_create(struct thread *td, void **handlep);
318 void	pmclog_proc_ignite(void *handle, struct pmc_owner *po);
319 void	pmclog_process_callchain(struct pmc *_pm, struct pmc_sample *_ps);
320 void	pmclog_process_closelog(struct pmc_owner *po);
321 void	pmclog_process_dropnotify(struct pmc_owner *po);
322 void	pmclog_process_map_in(struct pmc_owner *po, pid_t pid,
323     uintfptr_t start, const char *path);
324 void	pmclog_process_map_out(struct pmc_owner *po, pid_t pid,
325     uintfptr_t start, uintfptr_t end);
326 void	pmclog_process_pmcallocate(struct pmc *_pm);
327 void	pmclog_process_pmcattach(struct pmc *_pm, pid_t _pid, char *_path);
328 void	pmclog_process_pmcdetach(struct pmc *_pm, pid_t _pid);
329 void	pmclog_process_proccsw(struct pmc *_pm, struct pmc_process *_pp,
330     pmc_value_t _v, struct thread *);
331 void	pmclog_process_procexec(struct pmc_owner *_po, pmc_id_t _pmid, pid_t _pid,
332     uintfptr_t _baseaddr, uintptr_t _dynaddr, char *_path);
333 void	pmclog_process_procexit(struct pmc *_pm, struct pmc_process *_pp);
334 void	pmclog_process_procfork(struct pmc_owner *_po, pid_t _oldpid, pid_t _newpid);
335 void	pmclog_process_sysexit(struct pmc_owner *_po, pid_t _pid);
336 void	pmclog_process_threadcreate(struct pmc_owner *_po, struct thread *td, int sync);
337 void	pmclog_process_threadexit(struct pmc_owner *_po, struct thread *td);
338 void	pmclog_process_proccreate(struct pmc_owner *_po, struct proc *p, int sync);
339 int	pmclog_process_userlog(struct pmc_owner *_po,
340     struct pmc_op_writelog *_wl);
341 void	pmclog_shutdown(void);
342 #endif	/* _KERNEL */
343 
344 #endif	/* _SYS_PMCLOG_H_ */
345