xref: /freebsd/lib/libpmc/pmclog.3 (revision 110e1704d33d8632ce8febdd36a0143ca8b2ef0e)
1f263522aSJoseph Koshy.\" Copyright (c) 2005 Joseph Koshy.  All rights reserved.
2f263522aSJoseph Koshy.\"
3f263522aSJoseph Koshy.\" Redistribution and use in source and binary forms, with or without
4f263522aSJoseph Koshy.\" modification, are permitted provided that the following conditions
5f263522aSJoseph Koshy.\" are met:
6f263522aSJoseph Koshy.\" 1. Redistributions of source code must retain the above copyright
7f263522aSJoseph Koshy.\"    notice, this list of conditions and the following disclaimer.
8f263522aSJoseph Koshy.\" 2. Redistributions in binary form must reproduce the above copyright
9f263522aSJoseph Koshy.\"    notice, this list of conditions and the following disclaimer in the
10f263522aSJoseph Koshy.\"    documentation and/or other materials provided with the distribution.
11f263522aSJoseph Koshy.\"
12f263522aSJoseph Koshy.\" This software is provided by Joseph Koshy ``as is'' and
13f263522aSJoseph Koshy.\" any express or implied warranties, including, but not limited to, the
14f263522aSJoseph Koshy.\" implied warranties of merchantability and fitness for a particular purpose
15f263522aSJoseph Koshy.\" are disclaimed.  in no event shall Joseph Koshy be liable
16f263522aSJoseph Koshy.\" for any direct, indirect, incidental, special, exemplary, or consequential
17f263522aSJoseph Koshy.\" damages (including, but not limited to, procurement of substitute goods
18f263522aSJoseph Koshy.\" or services; loss of use, data, or profits; or business interruption)
19f263522aSJoseph Koshy.\" however caused and on any theory of liability, whether in contract, strict
20f263522aSJoseph Koshy.\" liability, or tort (including negligence or otherwise) arising in any way
21f263522aSJoseph Koshy.\" out of the use of this software, even if advised of the possibility of
22f263522aSJoseph Koshy.\" such damage.
23f263522aSJoseph Koshy.\"
24f263522aSJoseph Koshy.\" $FreeBSD$
25f263522aSJoseph Koshy.\"
26f711ce99SRuslan Ermilov.Dd June 1, 2005
27f263522aSJoseph Koshy.Os
28f263522aSJoseph Koshy.Dt PMCLOG 3
29f263522aSJoseph Koshy.Sh NAME
30f263522aSJoseph Koshy.Nm pmclog_open ,
31f263522aSJoseph Koshy.Nm pmclog_close ,
32f263522aSJoseph Koshy.Nm pmclog_read ,
33f263522aSJoseph Koshy.Nm pmclog_feed
34f263522aSJoseph Koshy.Nd parse event log data generated by
35f263522aSJoseph Koshy.Xr hwpmc 4
36f263522aSJoseph Koshy.Sh LIBRARY
37f263522aSJoseph Koshy.Lb libpmc
38f263522aSJoseph Koshy.Sh SYNOPSIS
39f263522aSJoseph Koshy.In pmclog.h
40f263522aSJoseph Koshy.Ft "void *"
41f263522aSJoseph Koshy.Fn pmclog_open "int fd"
42f263522aSJoseph Koshy.Ft void
43f263522aSJoseph Koshy.Fn pmclog_close "void *cookie"
44f263522aSJoseph Koshy.Ft int
45f263522aSJoseph Koshy.Fn pmclog_read "void *cookie" "struct pmclog_ev *ev"
46f263522aSJoseph Koshy.Ft int
47f263522aSJoseph Koshy.Fn pmclog_feed "void *cookie" "char *data" "int len"
48f263522aSJoseph Koshy.Sh DESCRIPTION
49f263522aSJoseph KoshyThese functions provide a way for application programs to extract
50f263522aSJoseph Koshyevents from an event stream generated by
51f263522aSJoseph Koshy.Xr hwpmc 4 .
52f263522aSJoseph Koshy.Pp
53f263522aSJoseph KoshyA new event log parser is allocated using
54f263522aSJoseph Koshy.Fn pmclog_open .
55f263522aSJoseph KoshyArgument
56f263522aSJoseph Koshy.Fa fd
57f263522aSJoseph Koshymay be a file descriptor opened for reading if the event stream is
58f263522aSJoseph Koshypresent in a file, or the constant
59f263522aSJoseph Koshy.Dv PMCLOG_FD_NONE
60f263522aSJoseph Koshyfor an event stream present in memory.
61f263522aSJoseph KoshyThis function returns a cookie that is passed into the other functions
62f263522aSJoseph Koshyin this API set.
63f263522aSJoseph Koshy.Pp
64f263522aSJoseph KoshyFunction
65f263522aSJoseph Koshy.Fn pmclog_read
66f263522aSJoseph Koshyreturns the next available event in the event stream associated with
67f263522aSJoseph Koshyargument
68f263522aSJoseph Koshy.Fa cookie .
69f263522aSJoseph KoshyArgument
70f263522aSJoseph Koshy.Fa ev
71f263522aSJoseph Koshypoints to an event descriptor that which will contain the result of a
72f263522aSJoseph Koshysuccessfully parsed event.
73f263522aSJoseph Koshy.Pp
74f263522aSJoseph KoshyAn event descriptor returned by
75f263522aSJoseph Koshy.Fn pmclog_read
76f263522aSJoseph Koshyhas the following structure:
77f263522aSJoseph Koshy.Bd -literal
78f263522aSJoseph Koshystruct pmclog_ev {
79f263522aSJoseph Koshy       enum pmclog_state pl_state;	/* parser state after 'get_event()' */
80f263522aSJoseph Koshy       off_t             pl_offset;	/* byte offset in stream */
81f263522aSJoseph Koshy       size_t            pl_count;	/* count of records so far */
82f263522aSJoseph Koshy       struct timespec   pl_ts;		/* log entry timestamp */
83f263522aSJoseph Koshy       enum pmclog_type  pl_type;	/* log entry kind */
84f263522aSJoseph Koshy       union {				/* log entry data */
85f263522aSJoseph Koshy		struct pmclog_ev_allocate   pl_a;
86f263522aSJoseph Koshy		struct pmclog_ev_proccsw    pl_c;
87f263522aSJoseph Koshy		struct pmclog_ev_dropnotify pl_d;
88f263522aSJoseph Koshy		struct pmclog_ev_procexit   pl_e;
89f263522aSJoseph Koshy		struct pmclog_ev_initialize pl_i;
90f263522aSJoseph Koshy		struct pmclog_ev_pcsample   pl_s;
91f263522aSJoseph Koshy		struct pmclog_ev_pmcattach  pl_t;
92f263522aSJoseph Koshy		struct pmclog_ev_userdata   pl_u;
93f263522aSJoseph Koshy		struct pmclog_ev_procexec   pl_x;
94f263522aSJoseph Koshy       } pl_u;
95f263522aSJoseph Koshy};
96f263522aSJoseph Koshy.Ed
97f263522aSJoseph Koshy.Pp
98f263522aSJoseph KoshyThe current state of the parser is recorded in
99f263522aSJoseph Koshy.Va pl_state .
100f263522aSJoseph KoshyThis field can take on the following values:
101f711ce99SRuslan Ermilov.Bl -tag -width ".Dv PMCLOG_REQUIRE_DATA"
102f263522aSJoseph Koshy.It Dv PMCLOG_EOF
103f711ce99SRuslan Ermilov(For file based parsers only)
104f263522aSJoseph KoshyAn end-of-file condition was encountered on the configured file
105f263522aSJoseph Koshydescriptor.
106f263522aSJoseph Koshy.It Dv PMCLOG_ERROR
107f263522aSJoseph KoshyAn error occurred during parsing.
108f263522aSJoseph Koshy.It Dv PMCLOG_OK
109f263522aSJoseph KoshyA complete event record was read into
110f711ce99SRuslan Ermilov.Fa *ev .
111f263522aSJoseph Koshy.It Dv PMCLOG_REQUIRE_DATA
112f263522aSJoseph KoshyThere was insufficient data in the event stream to assemble a complete
113f263522aSJoseph Koshyevent record.
114f263522aSJoseph KoshyFor memory based parsers, more data can be fed to the
115f263522aSJoseph Koshyparser using function
116f263522aSJoseph Koshy.Fn pmclog_feed .
117f263522aSJoseph KoshyFor file based parsers, function
118f263522aSJoseph Koshy.Fn pmclog_read
119f263522aSJoseph Koshymay be retried when data is available on the configured file
120f263522aSJoseph Koshydescriptor.
121f263522aSJoseph Koshy.El
122f263522aSJoseph Koshy.Pp
123f263522aSJoseph KoshyThe rest of the event structure is valid only if field
124f263522aSJoseph Koshy.Va pl_state
125f263522aSJoseph Koshycontains
126f263522aSJoseph Koshy.Dv PMCLOG_OK .
127f263522aSJoseph KoshyField
128f263522aSJoseph Koshy.Va pl_offset
129f263522aSJoseph Koshycontains the offset of the current record in the byte stream.
130f263522aSJoseph KoshyField
131f263522aSJoseph Koshy.Va pl_count
132f263522aSJoseph Koshycontains the serial number of this event.
133f263522aSJoseph KoshyField
134f263522aSJoseph Koshy.Va pl_ts
135f263522aSJoseph Koshycontains a timestamp with the system time when the event occurred.
136f263522aSJoseph KoshyField
137f263522aSJoseph Koshy.Va pl_type
138f263522aSJoseph Koshydenotes the kind of the event returned in argument
139f263522aSJoseph Koshy.Fa *ev
140f263522aSJoseph Koshyand is one of the following:
141f711ce99SRuslan Ermilov.Bl -tag -width ".Dv PMCLOG_TYPE_PMCALLOCATE"
14279b6b08fSJoseph Koshy.It Dv PMCLOG_TYPE_CLOSELOG
14379b6b08fSJoseph KoshyA marker indicating a successful close of a log file.
14479b6b08fSJoseph KoshyThis record will be the last record of a log file.
145f263522aSJoseph Koshy.It Dv PMCLOG_TYPE_DROPNOTIFY
14679b6b08fSJoseph KoshyA marker indicating that
147f263522aSJoseph Koshy.Xr hwpmc 4
148f263522aSJoseph Koshyhad to drop data due to a resource constraint.
149f263522aSJoseph Koshy.It Dv PMCLOG_TYPE_INITIALIZE
15079b6b08fSJoseph KoshyAn initialization record.
15179b6b08fSJoseph KoshyThis is the first record in a log file.
15279b6b08fSJoseph Koshy.It Dv PMCLOG_TYPE_MAPPINGCHANGE
15379b6b08fSJoseph KoshyA record describing an address space change for a process.
154f263522aSJoseph Koshy.It Dv PMCLOG_TYPE_PCSAMPLE
155f263522aSJoseph KoshyA record containing an instruction pointer sample.
156f263522aSJoseph Koshy.It Dv PMCLOG_TYPE_PMCALLOCATE
157f263522aSJoseph KoshyA record describing a PMC allocation operation.
158f263522aSJoseph Koshy.It Dv PMCLOG_TYPE_PMCATTACH
159f263522aSJoseph KoshyA record describing a PMC attach operation.
16079b6b08fSJoseph Koshy.It Dv PMCLOG_TYPE_PMCDETACH
16179b6b08fSJoseph KoshyA record describing a PMC detach operation.
162f263522aSJoseph Koshy.It Dv PMCLOG_TYPE_PROCCSW
163f263522aSJoseph KoshyA record describing a PMC reading at the time of a process context switch.
164f263522aSJoseph Koshy.It Dv PMCLOG_TYPE_PROCEXEC
165f263522aSJoseph KoshyA record describing an
166f263522aSJoseph Koshy.Xr execve 2
167f263522aSJoseph Koshyby a target process.
16879b6b08fSJoseph Koshy.It Dv PMCLOG_TYPE_PROCEXIT
16979b6b08fSJoseph KoshyA record describing the accumulated PMC reading for a process at the
17079b6b08fSJoseph Koshytime of
17179b6b08fSJoseph Koshy.Xr _exit 2 .
17279b6b08fSJoseph Koshy.It Dv PMCLOG_TYPE_PROCFORK
17379b6b08fSJoseph KoshyA record describing a
17479b6b08fSJoseph Koshy.Xr fork 2
17579b6b08fSJoseph Koshyby a target process.
17679b6b08fSJoseph Koshy.It Dv PMCLOG_TYPE_SYSEXIT
17779b6b08fSJoseph KoshyA record describing a process exit, sent to processes
17879b6b08fSJoseph Koshyowning system-wide sampling PMCs.
179f263522aSJoseph Koshy.It Dv PMCLOG_TYPE_USERDATA
180f263522aSJoseph KoshyA record containing user data.
181f263522aSJoseph Koshy.El
182f263522aSJoseph Koshy.Pp
183f263522aSJoseph KoshyFunction
184f263522aSJoseph Koshy.Fn pmclog_feed
185f263522aSJoseph Koshyis used with parsers configured to parse memory based event streams.
186f263522aSJoseph KoshyIt is intended to be called when function
187f263522aSJoseph Koshy.Fn pmclog_read
188f263522aSJoseph Koshyindicates the need for more data by a returning
189f263522aSJoseph Koshy.Dv PMCLOG_REQUIRE_DATA
190f263522aSJoseph Koshyin field
191f263522aSJoseph Koshy.Va pl_state
192f263522aSJoseph Koshyof its event structure argument.
193f263522aSJoseph KoshyArgument
194f263522aSJoseph Koshy.Fa data
195f263522aSJoseph Koshypoints to the start of a memory buffer containing fresh event data.
196f263522aSJoseph KoshyArgument
197f263522aSJoseph Koshy.Fa len
198f711ce99SRuslan Ermilovindicates the number of data bytes available.
199f263522aSJoseph KoshyThe memory range
200f711ce99SRuslan Ermilov.Bq Fa data , Fa data No + Fa len
201f263522aSJoseph Koshymust remain valid till the next time
202f263522aSJoseph Koshy.Fn pmclog_read
203f263522aSJoseph Koshyreturns an error.
204f263522aSJoseph KoshyIt is an error to use
205f263522aSJoseph Koshy.Fn pmclog_feed
206f263522aSJoseph Koshyon a parser configured to parse file data.
207f263522aSJoseph Koshy.Pp
208f263522aSJoseph KoshyFunction
209f263522aSJoseph Koshy.Fn pmclog_close
210f263522aSJoseph Koshyreleases the internal state allocated by a prior call
211f263522aSJoseph Koshyto
212f263522aSJoseph Koshy.Fn pmclog_open .
213f263522aSJoseph Koshy.Sh RETURN VALUES
214f263522aSJoseph KoshyFunction
215f263522aSJoseph Koshy.Fn pmclog_open
216f711ce99SRuslan Ermilovwill return a
217f711ce99SRuslan Ermilov.No non- Ns Dv NULL
218f711ce99SRuslan Ermilovvalue if successful or
219f711ce99SRuslan Ermilov.Dv NULL
220f711ce99SRuslan Ermilovotherwise.
221f263522aSJoseph Koshy.Pp
222f263522aSJoseph KoshyFunction
223f263522aSJoseph Koshy.Fn pmclog_read
224f263522aSJoseph Koshywill return 0 in case a complete event record was successfully read,
225f711ce99SRuslan Ermilovor will return \-1 and will set the
226f263522aSJoseph Koshy.Va pl_state
227f263522aSJoseph Koshyfield of the event record to the appropriate code in case of an error.
228f263522aSJoseph Koshy.Pp
229f263522aSJoseph KoshyFunction
230f263522aSJoseph Koshy.Fn pmclog_feed
231f711ce99SRuslan Ermilovwill return 0 on success or \-1 in case of failure.
232f263522aSJoseph Koshy.Sh EXAMPLES
233f711ce99SRuslan ErmilovA template for using the log file parsing API is shown below in pseudocode:
234f263522aSJoseph Koshy.Bd -literal
235f263522aSJoseph Koshyvoid *parser;			/* cookie */
236f263522aSJoseph Koshystruct pmclog_ev ev;		/* parsed event */
237f263522aSJoseph Koshyint fd;				/* file descriptor */
238f263522aSJoseph Koshy
239f263522aSJoseph Koshyfd = open(filename, O_RDONLY);	/* open log file */
240f263522aSJoseph Koshyparser = pmclog_open(fd);	/* initialize parser */
241f263522aSJoseph Koshyif (parser == NULL)
242f263522aSJoseph Koshy	--handle an out of memory error--;
243f263522aSJoseph Koshy
244f263522aSJoseph Koshy/* read and parse data */
245f263522aSJoseph Koshywhile (pmclog_read(parser, &ev) == 0) {
246f263522aSJoseph Koshy	assert(ev.pl_state == PMCLOG_OK);
247f263522aSJoseph Koshy	/* process the event */
248f263522aSJoseph Koshy	switch (ev.pl_type) {
249f263522aSJoseph Koshy	case PMCLOG_TYPE_ALLOCATE:
250f263522aSJoseph Koshy		--process a pmc allocation record--
251f263522aSJoseph Koshy		break;
252f263522aSJoseph Koshy	case PMCLOG_TYPE_PROCCSW:
253f263522aSJoseph Koshy		--process a thread context switch record--
254f263522aSJoseph Koshy		break;
255f263522aSJoseph Koshy	case PMCLOG_TYPE_PCSAMPLE:
256f263522aSJoseph Koshy		--process a PC sample--
257f263522aSJoseph Koshy		break;
258f263522aSJoseph Koshy	--and so on--
259f263522aSJoseph Koshy	}
260f263522aSJoseph Koshy}
261f263522aSJoseph Koshy
262f263522aSJoseph Koshy/* examine parser state */
263f263522aSJoseph Koshyswitch (ev.pl_state) {
264f263522aSJoseph Koshycase PMCLOG_EOF:
265f263522aSJoseph Koshy	--normal termination--
266f263522aSJoseph Koshy	break;
267f263522aSJoseph Koshycase PMCLOG_ERROR:
268f263522aSJoseph Koshy	--look at errno here--
269f263522aSJoseph Koshy	break;
270f263522aSJoseph Koshycase PMCLOG_REQUIRE_DATA:
271f263522aSJoseph Koshy	--arrange for more data to be available for parsing--
272f263522aSJoseph Koshy	break;
273f263522aSJoseph Koshydefault:
274f263522aSJoseph Koshy	assert(0);
275f263522aSJoseph Koshy	/*NOTREACHED*/
276f263522aSJoseph Koshy}
277f263522aSJoseph Koshy
278f263522aSJoseph Koshypmclog_close(parser);		/* cleanup */
279f263522aSJoseph Koshy.Ed
280f263522aSJoseph Koshy.Sh ERRORS
281f263522aSJoseph KoshyA call to
282f263522aSJoseph Koshy.Fn pmclog_init_parser
283f263522aSJoseph Koshymay fail with any of the errors returned by
284f263522aSJoseph Koshy.Xr malloc 3 .
285f263522aSJoseph Koshy.Pp
286f263522aSJoseph KoshyA call to
287f263522aSJoseph Koshy.Fn pmclog_read
288f263522aSJoseph Koshyfor a file based parser may fail with any of the errors returned by
289f263522aSJoseph Koshy.Xr read 2 .
290f263522aSJoseph Koshy.Sh SEE ALSO
291f263522aSJoseph Koshy.Xr read 2 ,
292f263522aSJoseph Koshy.Xr malloc 3 ,
293f263522aSJoseph Koshy.Xr pmc 3 ,
294f711ce99SRuslan Ermilov.Xr hwpmc 4 ,
295f711ce99SRuslan Ermilov.Xr pmcstat 8
29679b6b08fSJoseph Koshy.Sh HISTORY
29779b6b08fSJoseph KoshyThe
298110e1704SRuslan Ermilov.Nm pmclog
29979b6b08fSJoseph KoshyAPI
30079b6b08fSJoseph Koshy.Ud
30179b6b08fSJoseph KoshyIt first appeared in
30279b6b08fSJoseph Koshy.Fx 6.0 .
303