xref: /freebsd/lib/libpmc/pmclog.h (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
33  */
34 
35 #ifndef	_PMCLOG_H_
36 #define	_PMCLOG_H_
37 
38 #include <sys/cdefs.h>
39 #include <sys/pmclog.h>
40 
41 enum pmclog_state {
42 	PMCLOG_OK,
43 	PMCLOG_EOF,
44 	PMCLOG_REQUIRE_DATA,
45 	PMCLOG_ERROR
46 };
47 
48 struct pmclog_ev_callchain {
49 	uint32_t	pl_pid;
50 	uint32_t	pl_tid;
51 	uint32_t	pl_pmcid;
52 	uint32_t	pl_cpuflags;
53 	uint32_t	pl_cpuflags2;
54 	uint32_t	pl_npc;
55 	uintfptr_t	pl_pc[PMC_CALLCHAIN_DEPTH_MAX];
56 };
57 
58 struct pmclog_ev_dropnotify {
59 };
60 
61 struct pmclog_ev_closelog {
62 };
63 
64 struct pmclog_ev_initialize {
65 	uint32_t	pl_version;
66 	uint32_t	pl_arch;
67 	uint64_t	pl_tsc_freq;
68 	struct timespec pl_ts;
69 	char		pl_cpuid[PATH_MAX];
70 };
71 
72 struct pmclog_ev_map_in {
73 	pid_t		pl_pid;
74 	uintfptr_t	pl_start;
75 	char		pl_pathname[PATH_MAX];
76 };
77 
78 struct pmclog_ev_map_out {
79 	pid_t		pl_pid;
80 	uintfptr_t	pl_start;
81 	uintfptr_t	pl_end;
82 };
83 
84 struct pmclog_ev_pcsample {
85 	uintfptr_t	pl_pc;
86 	pid_t		pl_pid;
87 	pid_t		pl_tid;
88 	pmc_id_t	pl_pmcid;
89 	uint32_t	pl_flags;
90 	uint32_t	pl_usermode;
91 };
92 
93 struct pmclog_ev_pmcallocate {
94 	const char *	pl_evname;
95 	uint64_t	pl_rate;
96 	uint32_t	pl_event;
97 	uint32_t	pl_flags;
98 	pmc_id_t	pl_pmcid;
99 };
100 
101 struct pmclog_ev_pmcallocatedyn {
102 	char 		pl_evname[PMC_NAME_MAX];
103 	uint32_t	pl_event;
104 	uint32_t	pl_flags;
105 	pmc_id_t	pl_pmcid;
106 };
107 
108 struct pmclog_ev_pmcattach {
109 	pmc_id_t	pl_pmcid;
110 	pid_t		pl_pid;
111 	char		pl_pathname[PATH_MAX];
112 };
113 
114 struct pmclog_ev_pmcdetach {
115 	pmc_id_t	pl_pmcid;
116 	pid_t		pl_pid;
117 };
118 
119 struct pmclog_ev_proccsw {
120 	pid_t		pl_pid;
121 	pid_t		pl_tid;
122 	pmc_id_t	pl_pmcid;
123 	pmc_value_t	pl_value;
124 };
125 
126 struct pmclog_ev_proccreate {
127 	pid_t		pl_pid;
128 	uint32_t	pl_flags;
129 	char		pl_pcomm[MAXCOMLEN+1];
130 };
131 
132 struct pmclog_ev_procexec {
133 	pid_t		pl_pid;
134 	pmc_id_t	pl_pmcid;
135 	uintfptr_t	pl_entryaddr;
136 	char		pl_pathname[PATH_MAX];
137 };
138 
139 struct pmclog_ev_procexit {
140 	uint32_t	pl_pid;
141 	pmc_id_t	pl_pmcid;
142 	pmc_value_t	pl_value;
143 };
144 
145 struct pmclog_ev_procfork {
146 	pid_t		pl_oldpid;
147 	pid_t		pl_newpid;
148 };
149 
150 struct pmclog_ev_sysexit {
151 	pid_t		pl_pid;
152 };
153 
154 struct pmclog_ev_threadcreate {
155 	pid_t		pl_tid;
156 	pid_t		pl_pid;
157 	uint32_t	pl_flags;
158 	char		pl_tdname[MAXCOMLEN+1];
159 };
160 
161 struct pmclog_ev_threadexit {
162 	pid_t		pl_tid;
163 };
164 
165 struct pmclog_ev_userdata {
166 	uint32_t	pl_userdata;
167 };
168 
169 struct pmclog_ev {
170 	enum pmclog_state pl_state;	/* state after 'get_event()' */
171 	off_t		  pl_offset;	/* byte offset in stream */
172 	size_t		  pl_count;	/* count of records so far */
173 	struct timespec   pl_ts;	/* log entry timestamp */
174 	enum pmclog_type  pl_type;	/* type of log entry */
175 	void		 *pl_data;
176 	int		  pl_len;
177 	union { 			/* log entry data */
178 		struct pmclog_ev_callchain	pl_cc;
179 		struct pmclog_ev_closelog	pl_cl;
180 		struct pmclog_ev_dropnotify	pl_dn;
181 		struct pmclog_ev_initialize	pl_i;
182 		struct pmclog_ev_map_in		pl_mi;
183 		struct pmclog_ev_map_out	pl_mo;
184 		struct pmclog_ev_pmcallocate	pl_a;
185 		struct pmclog_ev_pmcallocatedyn	pl_ad;
186 		struct pmclog_ev_pmcattach	pl_t;
187 		struct pmclog_ev_pmcdetach	pl_d;
188 		struct pmclog_ev_proccsw	pl_c;
189 		struct pmclog_ev_proccreate	pl_pc;
190 		struct pmclog_ev_procexec	pl_x;
191 		struct pmclog_ev_procexit	pl_e;
192 		struct pmclog_ev_procfork	pl_f;
193 		struct pmclog_ev_sysexit	pl_se;
194 		struct pmclog_ev_threadcreate	pl_tc;
195 		struct pmclog_ev_threadexit	pl_te;
196 		struct pmclog_ev_userdata	pl_u;
197 	} pl_u;
198 };
199 
200 enum pmclog_parser_state {
201 	PL_STATE_NEW_RECORD,		/* in-between records */
202 	PL_STATE_EXPECTING_HEADER,	/* header being read */
203 	PL_STATE_PARTIAL_RECORD,	/* header present but not the record */
204 	PL_STATE_ERROR			/* parsing error encountered */
205 };
206 
207 struct pmclog_parse_state {
208 	enum pmclog_parser_state ps_state;
209 	enum pmc_cputype	ps_arch;	/* log file architecture */
210 	uint32_t		ps_version;	/* hwpmc version */
211 	int			ps_initialized;	/* whether initialized */
212 	int			ps_count;	/* count of records processed */
213 	off_t			ps_offset;	/* stream byte offset */
214 	union pmclog_entry	ps_saved;	/* saved partial log entry */
215 	int			ps_svcount;	/* #bytes saved */
216 	int			ps_fd;		/* active fd or -1 */
217 	char			*ps_buffer;	/* scratch buffer if fd != -1 */
218 	char			*ps_data;	/* current parse pointer */
219 	char			*ps_cpuid;	/* log cpuid */
220 	size_t			ps_len;		/* length of buffered data */
221 };
222 
223 #define	PMCLOG_FD_NONE				(-1)
224 
225 __BEGIN_DECLS
226 void	*pmclog_open(int _fd);
227 int	pmclog_feed(void *_cookie, char *_data, int _len);
228 int	pmclog_read(void *_cookie, struct pmclog_ev *_ev);
229 void	pmclog_close(void *_cookie);
230 __END_DECLS
231 
232 #endif
233 
234