xref: /freebsd/lib/libpmc/pmclog.3 (revision 641a6cfb86023499caafe26a4d821a0b885cf00b)
1.\" Copyright (c) 2005-2006 Joseph Koshy.  All rights reserved.
2.\"
3.\" Redistribution and use in source and binary forms, with or without
4.\" modification, are permitted provided that the following conditions
5.\" are met:
6.\" 1. Redistributions of source code must retain the above copyright
7.\"    notice, this list of conditions and the following disclaimer.
8.\" 2. Redistributions in binary form must reproduce the above copyright
9.\"    notice, this list of conditions and the following disclaimer in the
10.\"    documentation and/or other materials provided with the distribution.
11.\"
12.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
16.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22.\" SUCH DAMAGE.
23.\"
24.\" $FreeBSD$
25.\"
26.Dd March 26, 2006
27.Dt PMCLOG 3
28.Os
29.Sh NAME
30.Nm pmclog_open ,
31.Nm pmclog_close ,
32.Nm pmclog_read ,
33.Nm pmclog_feed
34.Nd parse event log data generated by
35.Xr hwpmc 4
36.Sh LIBRARY
37.Lb libpmc
38.Sh SYNOPSIS
39.In pmclog.h
40.Ft "void *"
41.Fn pmclog_open "int fd"
42.Ft void
43.Fn pmclog_close "void *cookie"
44.Ft int
45.Fn pmclog_read "void *cookie" "struct pmclog_ev *ev"
46.Ft int
47.Fn pmclog_feed "void *cookie" "char *data" "int len"
48.Sh DESCRIPTION
49These functions provide a way for application programs to extract
50events from an event stream generated by
51.Xr hwpmc 4 .
52.Pp
53A new event log parser is allocated using
54.Fn pmclog_open .
55Argument
56.Fa fd
57may be a file descriptor opened for reading if the event stream is
58present in a file, or the constant
59.Dv PMCLOG_FD_NONE
60for an event stream present in memory.
61This function returns a cookie that is passed into the other functions
62in this API set.
63.Pp
64Function
65.Fn pmclog_read
66returns the next available event in the event stream associated with
67argument
68.Fa cookie .
69Argument
70.Fa ev
71points to an event descriptor that which will contain the result of a
72successfully parsed event.
73.Pp
74An event descriptor returned by
75.Fn pmclog_read
76has the following structure:
77.Bd -literal
78struct pmclog_ev {
79       enum pmclog_state pl_state;	/* parser state after 'get_event()' */
80       off_t             pl_offset;	/* byte offset in stream */
81       size_t            pl_count;	/* count of records so far */
82       struct timespec   pl_ts;		/* log entry timestamp */
83       enum pmclog_type  pl_type;	/* log entry kind */
84       union {				/* log entry data */
85		struct pmclog_ev_closelog    pl_cl;
86		struct pmclog_ev_dropnotify  pl_d;
87		struct pmclog_ev_initialize  pl_i;
88		struct pmclog_ev_map_in      pl_mi;
89		struct pmclog_ev_map_out     pl_mo;
90		struct pmclog_ev_pcsample    pl_s;
91		struct pmclog_ev_pmcallocate pl_a;
92		struct pmclog_ev_pmcattach   pl_t;
93		struct pmclog_ev_pmcdetach   pl_d;
94		struct pmclog_ev_proccsw     pl_c;
95		struct pmclog_ev_procexec    pl_x;
96		struct pmclog_ev_procexit    pl_e;
97		struct pmclog_ev_procfork    pl_f;
98		struct pmclog_ev_sysexit     pl_e;
99		struct pmclog_ev_userdata    pl_u;
100       } pl_u;
101};
102.Ed
103.Pp
104The current state of the parser is recorded in
105.Va pl_state .
106This field can take on the following values:
107.Bl -tag -width ".Dv PMCLOG_REQUIRE_DATA"
108.It Dv PMCLOG_EOF
109(For file based parsers only)
110An end-of-file condition was encountered on the configured file
111descriptor.
112.It Dv PMCLOG_ERROR
113An error occurred during parsing.
114.It Dv PMCLOG_OK
115A complete event record was read into
116.Fa *ev .
117.It Dv PMCLOG_REQUIRE_DATA
118There was insufficient data in the event stream to assemble a complete
119event record.
120For memory based parsers, more data can be fed to the
121parser using function
122.Fn pmclog_feed .
123For file based parsers, function
124.Fn pmclog_read
125may be retried when data is available on the configured file
126descriptor.
127.El
128.Pp
129The rest of the event structure is valid only if field
130.Va pl_state
131contains
132.Dv PMCLOG_OK .
133Field
134.Va pl_offset
135contains the offset of the current record in the byte stream.
136Field
137.Va pl_count
138contains the serial number of this event.
139Field
140.Va pl_ts
141contains a timestamp with the system time when the event occurred.
142Field
143.Va pl_type
144denotes the kind of the event returned in argument
145.Fa *ev
146and is one of the following:
147.Bl -tag -width ".Dv PMCLOG_TYPE_PMCALLOCATE"
148.It Dv PMCLOG_TYPE_CLOSELOG
149A marker indicating a successful close of a log file.
150This record will be the last record of a log file.
151.It Dv PMCLOG_TYPE_DROPNOTIFY
152A marker indicating that
153.Xr hwpmc 4
154had to drop data due to a resource constraint.
155.It Dv PMCLOG_TYPE_INITIALIZE
156An initialization record.
157This is the first record in a log file.
158.It Dv PMCLOG_TYPE_MAP_IN
159A record describing the introduction of a mapping to an executable
160object by a
161.Xr kldload 2
162or
163.Xr mmap 2
164system call.
165.It Dv PMCLOG_TYPE_MAP_OUT
166A record describing the removal of a mapping to an executable
167object by a
168.Xr kldunload 2
169or
170.Xr munmap 2
171system call.
172.It Dv PMCLOG_TYPE_PCSAMPLE
173A record containing an instruction pointer sample.
174.It Dv PMCLOG_TYPE_PMCALLOCATE
175A record describing a PMC allocation operation.
176.It Dv PMCLOG_TYPE_PMCATTACH
177A record describing a PMC attach operation.
178.It Dv PMCLOG_TYPE_PMCDETACH
179A record describing a PMC detach operation.
180.It Dv PMCLOG_TYPE_PROCCSW
181A record describing a PMC reading at the time of a process context switch.
182.It Dv PMCLOG_TYPE_PROCEXEC
183A record describing an
184.Xr execve 2
185by a target process.
186.It Dv PMCLOG_TYPE_PROCEXIT
187A record describing the accumulated PMC reading for a process at the
188time of
189.Xr _exit 2 .
190.It Dv PMCLOG_TYPE_PROCFORK
191A record describing a
192.Xr fork 2
193by a target process.
194.It Dv PMCLOG_TYPE_SYSEXIT
195A record describing a process exit, sent to processes
196owning system-wide sampling PMCs.
197.It Dv PMCLOG_TYPE_USERDATA
198A record containing user data.
199.El
200.Pp
201Function
202.Fn pmclog_feed
203is used with parsers configured to parse memory based event streams.
204It is intended to be called when function
205.Fn pmclog_read
206indicates the need for more data by a returning
207.Dv PMCLOG_REQUIRE_DATA
208in field
209.Va pl_state
210of its event structure argument.
211Argument
212.Fa data
213points to the start of a memory buffer containing fresh event data.
214Argument
215.Fa len
216indicates the number of data bytes available.
217The memory range
218.Bq Fa data , Fa data No + Fa len
219must remain valid till the next time
220.Fn pmclog_read
221returns an error.
222It is an error to use
223.Fn pmclog_feed
224on a parser configured to parse file data.
225.Pp
226Function
227.Fn pmclog_close
228releases the internal state allocated by a prior call
229to
230.Fn pmclog_open .
231.Sh RETURN VALUES
232Function
233.Fn pmclog_open
234will return a
235.No non- Ns Dv NULL
236value if successful or
237.Dv NULL
238otherwise.
239.Pp
240Function
241.Fn pmclog_read
242will return 0 in case a complete event record was successfully read,
243or will return \-1 and will set the
244.Va pl_state
245field of the event record to the appropriate code in case of an error.
246.Pp
247Function
248.Fn pmclog_feed
249will return 0 on success or \-1 in case of failure.
250.Sh EXAMPLES
251A template for using the log file parsing API is shown below in pseudocode:
252.Bd -literal
253void *parser;			/* cookie */
254struct pmclog_ev ev;		/* parsed event */
255int fd;				/* file descriptor */
256
257fd = open(filename, O_RDONLY);	/* open log file */
258parser = pmclog_open(fd);	/* initialize parser */
259if (parser == NULL)
260	--handle an out of memory error--;
261
262/* read and parse data */
263while (pmclog_read(parser, &ev) == 0) {
264	assert(ev.pl_state == PMCLOG_OK);
265	/* process the event */
266	switch (ev.pl_type) {
267	case PMCLOG_TYPE_ALLOCATE:
268		--process a pmc allocation record--
269		break;
270	case PMCLOG_TYPE_PROCCSW:
271		--process a thread context switch record--
272		break;
273	case PMCLOG_TYPE_PCSAMPLE:
274		--process a PC sample--
275		break;
276	--and so on--
277	}
278}
279
280/* examine parser state */
281switch (ev.pl_state) {
282case PMCLOG_EOF:
283	--normal termination--
284	break;
285case PMCLOG_ERROR:
286	--look at errno here--
287	break;
288case PMCLOG_REQUIRE_DATA:
289	--arrange for more data to be available for parsing--
290	break;
291default:
292	assert(0);
293	/*NOTREACHED*/
294}
295
296pmclog_close(parser);		/* cleanup */
297.Ed
298.Sh ERRORS
299A call to
300.Fn pmclog_init_parser
301may fail with any of the errors returned by
302.Xr malloc 3 .
303.Pp
304A call to
305.Fn pmclog_read
306for a file based parser may fail with any of the errors returned by
307.Xr read 2 .
308.Sh SEE ALSO
309.Xr read 2 ,
310.Xr malloc 3 ,
311.Xr pmc 3 ,
312.Xr hwpmc 4 ,
313.Xr pmcstat 8
314.Sh HISTORY
315The
316.Nm pmclog
317API
318.Ud
319It first appeared in
320.Fx 6.0 .
321