xref: /freebsd/lib/libpmc/pmclog.c (revision d429ea332342fcb98d27a350d0c4944bf9aec3f9)
1 /*-
2  * Copyright (c) 2005 Joseph Koshy
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/pmc.h>
32 #include <sys/pmclog.h>
33 
34 #include <assert.h>
35 #include <errno.h>
36 #include <pmc.h>
37 #include <pmclog.h>
38 #include <stddef.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <strings.h>
42 #include <unistd.h>
43 
44 #include <machine/pmc_mdep.h>
45 
46 #define	PMCLOG_BUFFER_SIZE			4096
47 
48 /*
49  * API NOTES
50  *
51  * The pmclog(3) API is oriented towards parsing an event stream in
52  * "realtime", i.e., from an data source that may or may not preserve
53  * record boundaries -- for example when the data source is elsewhere
54  * on a network.  The API allows data to be fed into the parser zero
55  * or more bytes at a time.
56  *
57  * The state for a log file parser is maintained in a 'struct
58  * pmclog_parse_state'.  Parser invocations are done by calling
59  * 'pmclog_read()'; this function will inform the caller when a
60  * complete event is parsed.
61  *
62  * The parser first assembles a complete log file event in an internal
63  * work area (see "ps_saved" below).  Once a complete log file event
64  * is read, the parser then parses it and converts it to an event
65  * descriptor usable by the client.  We could possibly avoid this two
66  * step process by directly parsing the input log to set fields in the
67  * event record.  However the parser's state machine would get
68  * insanely complicated, and this code is unlikely to be used in
69  * performance critical paths.
70  */
71 
72 enum pmclog_parser_state {
73 	PL_STATE_NEW_RECORD,		/* in-between records */
74 	PL_STATE_EXPECTING_HEADER,	/* header being read */
75 	PL_STATE_PARTIAL_RECORD,	/* header present but not the record */
76 	PL_STATE_ERROR			/* parsing error encountered */
77 };
78 
79 struct pmclog_parse_state {
80 	enum pmclog_parser_state ps_state;
81 	enum pmc_cputype	ps_arch;	/* log file architecture */
82 	uint32_t		ps_version;	/* hwpmc version */
83 	int			ps_initialized;	/* whether initialized */
84 	int			ps_count;	/* count of records processed */
85 	off_t			ps_offset;	/* stream byte offset */
86 	union pmclog_entry	ps_saved;	/* saved partial log entry */
87 	int			ps_svcount;	/* #bytes saved */
88 	int			ps_fd;		/* active fd or -1 */
89 	char			*ps_buffer;	/* scratch buffer if fd != -1 */
90 	char			*ps_data;	/* current parse pointer */
91 	size_t			ps_len;		/* length of buffered data */
92 };
93 
94 #define	PMCLOG_HEADER_FROM_SAVED_STATE(PS)				\
95 	(* ((uint32_t *) &(PS)->ps_saved))
96 
97 #define	PMCLOG_INITIALIZE_READER(LE,A)	LE = (uint32_t *) &(A)
98 #define	PMCLOG_READ32(LE,V) 		do {				\
99 		(V)  = *(LE)++;						\
100 	} while (0)
101 #define	PMCLOG_READ64(LE,V)		do {				\
102 		uint64_t _v;						\
103 		_v  = (uint64_t) *(LE)++;				\
104 		_v |= ((uint64_t) *(LE)++) << 32;			\
105 		(V) = _v;						\
106 	} while (0)
107 
108 #define	PMCLOG_READSTRING(LE,DST,LEN)	strlcpy((DST), (char *) (LE), (LEN))
109 
110 /*
111  * Assemble a log record from '*len' octets starting from address '*data'.
112  * Update 'data' and 'len' to reflect the number of bytes consumed.
113  *
114  * '*data' is potentially an unaligned address and '*len' octets may
115  * not be enough to complete a event record.
116  */
117 
118 static enum pmclog_parser_state
119 pmclog_get_record(struct pmclog_parse_state *ps, char **data, ssize_t *len)
120 {
121 	int avail, copylen, recordsize, used;
122 	uint32_t h;
123 	const int HEADERSIZE = sizeof(uint32_t);
124 	char *src, *dst;
125 
126 	if ((avail = *len) <= 0)
127 		return (ps->ps_state = PL_STATE_ERROR);
128 
129 	src = *data;
130 	h = used = 0;
131 
132 	if (ps->ps_state == PL_STATE_NEW_RECORD)
133 		ps->ps_svcount = 0;
134 
135 	dst = (char *) &ps->ps_saved + ps->ps_svcount;
136 
137 	switch (ps->ps_state) {
138 	case PL_STATE_NEW_RECORD:
139 
140 		/*
141 		 * Transitions:
142 		 *
143 		 * Case A: avail < headersize
144 		 *	-> 'expecting header'
145 		 *
146 		 * Case B: avail >= headersize
147 		 *    B.1: avail < recordsize
148 		 *	   -> 'partial record'
149 		 *    B.2: avail >= recordsize
150 		 *         -> 'new record'
151 		 */
152 
153 		copylen = avail < HEADERSIZE ? avail : HEADERSIZE;
154 		bcopy(src, dst, copylen);
155 		ps->ps_svcount = used = copylen;
156 
157 		if (copylen < HEADERSIZE) {
158 			ps->ps_state = PL_STATE_EXPECTING_HEADER;
159 			goto done;
160 		}
161 
162 		src += copylen;
163 		dst += copylen;
164 
165 		h = PMCLOG_HEADER_FROM_SAVED_STATE(ps);
166 		recordsize = PMCLOG_HEADER_TO_LENGTH(h);
167 
168 		if (recordsize <= 0)
169 			goto error;
170 
171 		if (recordsize <= avail) { /* full record available */
172 			bcopy(src, dst, recordsize - copylen);
173 			ps->ps_svcount = used = recordsize;
174 			goto done;
175 		}
176 
177 		/* header + a partial record is available */
178 		bcopy(src, dst, avail - copylen);
179 		ps->ps_svcount = used = avail;
180 		ps->ps_state = PL_STATE_PARTIAL_RECORD;
181 
182 		break;
183 
184 	case PL_STATE_EXPECTING_HEADER:
185 
186 		/*
187 		 * Transitions:
188 		 *
189 		 * Case C: avail+saved < headersize
190 		 * 	-> 'expecting header'
191 		 *
192 		 * Case D: avail+saved >= headersize
193 		 *    D.1: avail+saved < recordsize
194 		 *    	-> 'partial record'
195 		 *    D.2: avail+saved >= recordsize
196 		 *    	-> 'new record'
197 		 *    (see PARTIAL_RECORD handling below)
198 		 */
199 
200 		if (avail + ps->ps_svcount < HEADERSIZE) {
201 			bcopy(src, dst, avail);
202 			ps->ps_svcount += avail;
203 			used = avail;
204 			break;
205 		}
206 
207 		used = copylen = HEADERSIZE - ps->ps_svcount;
208 		bcopy(src, dst, copylen);
209 		src += copylen;
210 		dst += copylen;
211 		avail -= copylen;
212 		ps->ps_svcount += copylen;
213 
214 		/*FALLTHROUGH*/
215 
216 	case PL_STATE_PARTIAL_RECORD:
217 
218 		/*
219 		 * Transitions:
220 		 *
221 		 * Case E: avail+saved < recordsize
222 		 * 	-> 'partial record'
223 		 *
224 		 * Case F: avail+saved >= recordsize
225 		 * 	-> 'new record'
226 		 */
227 
228 		h = PMCLOG_HEADER_FROM_SAVED_STATE(ps);
229 		recordsize = PMCLOG_HEADER_TO_LENGTH(h);
230 
231 		if (recordsize <= 0)
232 			goto error;
233 
234 		if (avail + ps->ps_svcount < recordsize) {
235 			copylen = avail;
236 			ps->ps_state = PL_STATE_PARTIAL_RECORD;
237 		} else {
238 			copylen = recordsize - ps->ps_svcount;
239 			ps->ps_state = PL_STATE_NEW_RECORD;
240 		}
241 
242 		bcopy(src, dst, copylen);
243 		ps->ps_svcount += copylen;
244 		used += copylen;
245 		break;
246 
247 	default:
248 		goto error;
249 	}
250 
251  done:
252 	*data += used;
253 	*len  -= used;
254 	return ps->ps_state;
255 
256  error:
257 	ps->ps_state = PL_STATE_ERROR;
258 	return ps->ps_state;
259 }
260 
261 /*
262  * Get an event from the stream pointed to by '*data'.  '*len'
263  * indicates the number of bytes available to parse.  Arguments
264  * '*data' and '*len' are updated to indicate the number of bytes
265  * consumed.
266  */
267 
268 static int
269 pmclog_get_event(void *cookie, char **data, ssize_t *len,
270     struct pmclog_ev *ev)
271 {
272 	int evlen, pathlen;
273 	uint32_t h, *le;
274 	enum pmclog_parser_state e;
275 	struct pmclog_parse_state *ps;
276 
277 	ps = (struct pmclog_parse_state *) cookie;
278 
279 	assert(ps->ps_state != PL_STATE_ERROR);
280 
281 	if ((e = pmclog_get_record(ps,data,len)) == PL_STATE_ERROR) {
282 		ev->pl_state = PMCLOG_ERROR;
283 		return -1;
284 	}
285 
286 	if (e != PL_STATE_NEW_RECORD) {
287 		ev->pl_state = PMCLOG_REQUIRE_DATA;
288 		return -1;
289 	}
290 
291 	PMCLOG_INITIALIZE_READER(le, ps->ps_saved);
292 
293 	PMCLOG_READ32(le,h);
294 
295 	if (!PMCLOG_HEADER_CHECK_MAGIC(h)) {
296 		ps->ps_state = PL_STATE_ERROR;
297 		ev->pl_state = PMCLOG_ERROR;
298 		return -1;
299 	}
300 
301 	/* copy out the time stamp */
302 	PMCLOG_READ32(le,ev->pl_ts.tv_sec);
303 	PMCLOG_READ32(le,ev->pl_ts.tv_nsec);
304 
305 	evlen = PMCLOG_HEADER_TO_LENGTH(h);
306 
307 #define	PMCLOG_GET_PATHLEN(P,E,TYPE) do {				\
308 		(P) = (E) - offsetof(struct TYPE, pl_pathname);		\
309 		if ((P) > PATH_MAX || (P) < 0)				\
310 			goto error;					\
311 	} while (0)
312 
313 	switch (ev->pl_type = PMCLOG_HEADER_TO_TYPE(h)) {
314 	case PMCLOG_TYPE_CLOSELOG:
315 	case PMCLOG_TYPE_DROPNOTIFY:
316 		/* nothing to do */
317 		break;
318 	case PMCLOG_TYPE_INITIALIZE:
319 		PMCLOG_READ32(le,ev->pl_u.pl_i.pl_version);
320 		PMCLOG_READ32(le,ev->pl_u.pl_i.pl_arch);
321 		ps->ps_version = ev->pl_u.pl_i.pl_version;
322 		ps->ps_arch = ev->pl_u.pl_i.pl_arch;
323 		ps->ps_initialized = 1;
324 		break;
325 	case PMCLOG_TYPE_MAPPINGCHANGE:
326 		PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_mappingchange);
327 		PMCLOG_READ32(le,ev->pl_u.pl_m.pl_type);
328 		PMCLOG_READADDR(le,ev->pl_u.pl_m.pl_start);
329 		PMCLOG_READADDR(le,ev->pl_u.pl_m.pl_end);
330 		PMCLOG_READ32(le,ev->pl_u.pl_m.pl_pid);
331 		PMCLOG_READSTRING(le, ev->pl_u.pl_m.pl_pathname, pathlen);
332 		break;
333 	case PMCLOG_TYPE_PCSAMPLE:
334 		PMCLOG_READ32(le,ev->pl_u.pl_s.pl_pid);
335 		PMCLOG_READADDR(le,ev->pl_u.pl_s.pl_pc);
336 		PMCLOG_READ32(le,ev->pl_u.pl_s.pl_pmcid);
337 		break;
338 	case PMCLOG_TYPE_PMCALLOCATE:
339 		PMCLOG_READ32(le,ev->pl_u.pl_a.pl_pmcid);
340 		PMCLOG_READ32(le,ev->pl_u.pl_a.pl_event);
341 		PMCLOG_READ32(le,ev->pl_u.pl_a.pl_flags);
342 		if ((ev->pl_u.pl_a.pl_evname =
343 		    pmc_name_of_event(ev->pl_u.pl_a.pl_event)) == NULL)
344 			goto error;
345 		break;
346 	case PMCLOG_TYPE_PMCATTACH:
347 		PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_pmcattach);
348 		PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pmcid);
349 		PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pid);
350 		PMCLOG_READSTRING(le,ev->pl_u.pl_t.pl_pathname,pathlen);
351 		break;
352 	case PMCLOG_TYPE_PMCDETACH:
353 		PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pmcid);
354 		PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pid);
355 		break;
356 	case PMCLOG_TYPE_PROCCSW:
357 		PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pmcid);
358 		PMCLOG_READ64(le,ev->pl_u.pl_c.pl_value);
359 		PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pid);
360 		break;
361 	case PMCLOG_TYPE_PROCEXEC:
362 		PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_procexec);
363 		PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pid);
364 		PMCLOG_READSTRING(le,ev->pl_u.pl_x.pl_pathname,pathlen);
365 		break;
366 	case PMCLOG_TYPE_PROCEXIT:
367 		PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pmcid);
368 		PMCLOG_READ64(le,ev->pl_u.pl_e.pl_value);
369 		PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pid);
370 		break;
371 	case PMCLOG_TYPE_PROCFORK:
372 		PMCLOG_READ32(le,ev->pl_u.pl_f.pl_oldpid);
373 		PMCLOG_READ32(le,ev->pl_u.pl_f.pl_newpid);
374 		break;
375 	case PMCLOG_TYPE_SYSEXIT:
376 		PMCLOG_READ32(le,ev->pl_u.pl_se.pl_pid);
377 		break;
378 	case PMCLOG_TYPE_USERDATA:
379 		PMCLOG_READ32(le,ev->pl_u.pl_u.pl_userdata);
380 		break;
381 	default:	/* unknown record type */
382 		ps->ps_state = PL_STATE_ERROR;
383 		ev->pl_state = PMCLOG_ERROR;
384 		return -1;
385 	}
386 
387 	ev->pl_offset = (ps->ps_offset += evlen);
388 	ev->pl_count  = (ps->ps_count += 1);
389 	ev->pl_state = PMCLOG_OK;
390 	return 0;
391 
392  error:
393 	ev->pl_state = PMCLOG_ERROR;
394 	ps->ps_state = PL_STATE_ERROR;
395 	return -1;
396 }
397 
398 /*
399  * Extract and return the next event from the byte stream.
400  *
401  * Returns 0 and sets the event's state to PMCLOG_OK in case an event
402  * was successfully parsed.  Otherwise this function returns -1 and
403  * sets the event's state to one of PMCLOG_REQUIRE_DATA (if more data
404  * is needed) or PMCLOG_EOF (if an EOF was seen) or PMCLOG_ERROR if
405  * a parse error was encountered.
406  */
407 
408 int
409 pmclog_read(void *cookie, struct pmclog_ev *ev)
410 {
411 	ssize_t nread;
412 	struct pmclog_parse_state *ps;
413 
414 	ps = (struct pmclog_parse_state *) cookie;
415 
416 	if (ps->ps_state == PL_STATE_ERROR) {
417 		ev->pl_state = PMCLOG_ERROR;
418 		return -1;
419 	}
420 
421 	/*
422 	 * If there isn't enough data left for a new event try and get
423 	 * more data.
424 	 */
425 	if (ps->ps_len == 0) {
426 		ev->pl_state = PMCLOG_REQUIRE_DATA;
427 
428 		/*
429 		 * If we have a valid file descriptor to read from, attempt
430 		 * to read from that.  This read may return with an error,
431 		 * (which may be EAGAIN or other recoverable error), or
432 		 * can return EOF.
433 		 */
434 		if (ps->ps_fd != PMCLOG_FD_NONE) {
435 			nread = read(ps->ps_fd, ps->ps_buffer,
436 			    PMCLOG_BUFFER_SIZE);
437 
438 			if (nread <= 0) {
439 				ev->pl_state = nread < 0 ? PMCLOG_ERROR :
440 				    PMCLOG_EOF;
441 				return -1;
442 			}
443 
444 			ps->ps_len = nread;
445 			ps->ps_data = ps->ps_buffer;
446 		} else
447 			return -1;
448 	}
449 
450 	assert(ps->ps_len > 0);
451 
452 	/*
453 	 * Retrieve one event from the byte stream.
454 	 */
455 	return pmclog_get_event(ps, &ps->ps_data, &ps->ps_len, ev);
456 }
457 
458 /*
459  * Feed data to a memory based parser.
460  *
461  * The memory area pointed to by 'data' needs to be valid till the
462  * next error return from pmclog_next_event().
463  */
464 
465 int
466 pmclog_feed(void *cookie, char *data, int len)
467 {
468 	struct pmclog_parse_state *ps;
469 
470 	ps = (struct pmclog_parse_state *) cookie;
471 
472 	if (len < 0 ||		/* invalid length */
473 	    ps->ps_buffer ||	/* called for a file parser */
474 	    ps->ps_len != 0)	/* unnecessary call */
475 		return -1;
476 
477 	ps->ps_data = data;
478 	ps->ps_len  = len;
479 
480 	return 0;
481 }
482 
483 /*
484  * Allocate and initialize parser state.
485  */
486 
487 void *
488 pmclog_open(int fd)
489 {
490 	struct pmclog_parse_state *ps;
491 
492 	if ((ps = (struct pmclog_parse_state *) malloc(sizeof(*ps))) == NULL)
493 		return NULL;
494 
495 	ps->ps_state = PL_STATE_NEW_RECORD;
496 	ps->ps_arch = -1;
497 	ps->ps_initialized = 0;
498 	ps->ps_count = 0;
499 	ps->ps_offset = (off_t) 0;
500 	bzero(&ps->ps_saved, sizeof(ps->ps_saved));
501 	ps->ps_svcount = 0;
502 	ps->ps_fd    = fd;
503 	ps->ps_data  = NULL;
504 	ps->ps_buffer = NULL;
505 	ps->ps_len   = 0;
506 
507 	/* allocate space for a work area */
508 	if (ps->ps_fd != PMCLOG_FD_NONE) {
509 		if ((ps->ps_buffer = malloc(PMCLOG_BUFFER_SIZE)) == NULL)
510 			return NULL;
511 	}
512 
513 	return ps;
514 }
515 
516 
517 /*
518  * Free up parser state.
519  */
520 
521 void
522 pmclog_close(void *cookie)
523 {
524 	struct pmclog_parse_state *ps;
525 
526 	ps = (struct pmclog_parse_state *) cookie;
527 
528 	if (ps->ps_buffer)
529 		free(ps->ps_buffer);
530 
531 	free(ps);
532 }
533