xref: /freebsd/lib/libpmc/pmclog.c (revision 2b743a9e9ddc6736208dc8ca1ce06ce64ad20a19)
1 /*-
2  * Copyright (c) 2005-2006 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_MAP_IN:
326 		PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_map_in);
327 		PMCLOG_READ32(le,ev->pl_u.pl_mi.pl_pid);
328 		PMCLOG_READADDR(le,ev->pl_u.pl_mi.pl_start);
329 		PMCLOG_READSTRING(le, ev->pl_u.pl_mi.pl_pathname, pathlen);
330 		break;
331 	case PMCLOG_TYPE_MAP_OUT:
332 		PMCLOG_READ32(le,ev->pl_u.pl_mo.pl_pid);
333 		PMCLOG_READADDR(le,ev->pl_u.pl_mo.pl_start);
334 		PMCLOG_READADDR(le,ev->pl_u.pl_mo.pl_end);
335 		break;
336 	case PMCLOG_TYPE_PCSAMPLE:
337 		PMCLOG_READ32(le,ev->pl_u.pl_s.pl_pid);
338 		PMCLOG_READADDR(le,ev->pl_u.pl_s.pl_pc);
339 		PMCLOG_READ32(le,ev->pl_u.pl_s.pl_pmcid);
340 		PMCLOG_READ32(le,ev->pl_u.pl_s.pl_usermode);
341 		break;
342 	case PMCLOG_TYPE_PMCALLOCATE:
343 		PMCLOG_READ32(le,ev->pl_u.pl_a.pl_pmcid);
344 		PMCLOG_READ32(le,ev->pl_u.pl_a.pl_event);
345 		PMCLOG_READ32(le,ev->pl_u.pl_a.pl_flags);
346 		if ((ev->pl_u.pl_a.pl_evname =
347 		    pmc_name_of_event(ev->pl_u.pl_a.pl_event)) == NULL)
348 			goto error;
349 		break;
350 	case PMCLOG_TYPE_PMCATTACH:
351 		PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_pmcattach);
352 		PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pmcid);
353 		PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pid);
354 		PMCLOG_READSTRING(le,ev->pl_u.pl_t.pl_pathname,pathlen);
355 		break;
356 	case PMCLOG_TYPE_PMCDETACH:
357 		PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pmcid);
358 		PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pid);
359 		break;
360 	case PMCLOG_TYPE_PROCCSW:
361 		PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pmcid);
362 		PMCLOG_READ64(le,ev->pl_u.pl_c.pl_value);
363 		PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pid);
364 		break;
365 	case PMCLOG_TYPE_PROCEXEC:
366 		PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_procexec);
367 		PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pid);
368 		PMCLOG_READADDR(le,ev->pl_u.pl_x.pl_entryaddr);
369 		PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pmcid);
370 		PMCLOG_READSTRING(le,ev->pl_u.pl_x.pl_pathname,pathlen);
371 		break;
372 	case PMCLOG_TYPE_PROCEXIT:
373 		PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pmcid);
374 		PMCLOG_READ64(le,ev->pl_u.pl_e.pl_value);
375 		PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pid);
376 		break;
377 	case PMCLOG_TYPE_PROCFORK:
378 		PMCLOG_READ32(le,ev->pl_u.pl_f.pl_oldpid);
379 		PMCLOG_READ32(le,ev->pl_u.pl_f.pl_newpid);
380 		break;
381 	case PMCLOG_TYPE_SYSEXIT:
382 		PMCLOG_READ32(le,ev->pl_u.pl_se.pl_pid);
383 		break;
384 	case PMCLOG_TYPE_USERDATA:
385 		PMCLOG_READ32(le,ev->pl_u.pl_u.pl_userdata);
386 		break;
387 	default:	/* unknown record type */
388 		ps->ps_state = PL_STATE_ERROR;
389 		ev->pl_state = PMCLOG_ERROR;
390 		return -1;
391 	}
392 
393 	ev->pl_offset = (ps->ps_offset += evlen);
394 	ev->pl_count  = (ps->ps_count += 1);
395 	ev->pl_state = PMCLOG_OK;
396 	return 0;
397 
398  error:
399 	ev->pl_state = PMCLOG_ERROR;
400 	ps->ps_state = PL_STATE_ERROR;
401 	return -1;
402 }
403 
404 /*
405  * Extract and return the next event from the byte stream.
406  *
407  * Returns 0 and sets the event's state to PMCLOG_OK in case an event
408  * was successfully parsed.  Otherwise this function returns -1 and
409  * sets the event's state to one of PMCLOG_REQUIRE_DATA (if more data
410  * is needed) or PMCLOG_EOF (if an EOF was seen) or PMCLOG_ERROR if
411  * a parse error was encountered.
412  */
413 
414 int
415 pmclog_read(void *cookie, struct pmclog_ev *ev)
416 {
417 	int retval;
418 	ssize_t nread;
419 	struct pmclog_parse_state *ps;
420 
421 	ps = (struct pmclog_parse_state *) cookie;
422 
423 	if (ps->ps_state == PL_STATE_ERROR) {
424 		ev->pl_state = PMCLOG_ERROR;
425 		return -1;
426 	}
427 
428 	/*
429 	 * If there isn't enough data left for a new event try and get
430 	 * more data.
431 	 */
432 	if (ps->ps_len == 0) {
433 		ev->pl_state = PMCLOG_REQUIRE_DATA;
434 
435 		/*
436 		 * If we have a valid file descriptor to read from, attempt
437 		 * to read from that.  This read may return with an error,
438 		 * (which may be EAGAIN or other recoverable error), or
439 		 * can return EOF.
440 		 */
441 		if (ps->ps_fd != PMCLOG_FD_NONE) {
442 		refill:
443 			nread = read(ps->ps_fd, ps->ps_buffer,
444 			    PMCLOG_BUFFER_SIZE);
445 
446 			if (nread <= 0) {
447 				if (nread == 0)
448 					ev->pl_state = PMCLOG_EOF;
449 				else if (errno != EAGAIN) /* not restartable */
450 					ev->pl_state = PMCLOG_ERROR;
451 				return -1;
452 			}
453 
454 			ps->ps_len = nread;
455 			ps->ps_data = ps->ps_buffer;
456 		} else
457 			return -1;
458 	}
459 
460 	assert(ps->ps_len > 0);
461 
462 
463 	 /* Retrieve one event from the byte stream. */
464 	retval = pmclog_get_event(ps, &ps->ps_data, &ps->ps_len, ev);
465 
466 	/*
467 	 * If we need more data and we have a configured fd, try read
468 	 * from it.
469 	 */
470 	if (retval < 0 && ev->pl_state == PMCLOG_REQUIRE_DATA &&
471 	    ps->ps_fd != -1) {
472 		assert(ps->ps_len == 0);
473 		goto refill;
474 	}
475 
476 	return retval;
477 }
478 
479 /*
480  * Feed data to a memory based parser.
481  *
482  * The memory area pointed to by 'data' needs to be valid till the
483  * next error return from pmclog_next_event().
484  */
485 
486 int
487 pmclog_feed(void *cookie, char *data, int len)
488 {
489 	struct pmclog_parse_state *ps;
490 
491 	ps = (struct pmclog_parse_state *) cookie;
492 
493 	if (len < 0 ||		/* invalid length */
494 	    ps->ps_buffer ||	/* called for a file parser */
495 	    ps->ps_len != 0)	/* unnecessary call */
496 		return -1;
497 
498 	ps->ps_data = data;
499 	ps->ps_len  = len;
500 
501 	return 0;
502 }
503 
504 /*
505  * Allocate and initialize parser state.
506  */
507 
508 void *
509 pmclog_open(int fd)
510 {
511 	struct pmclog_parse_state *ps;
512 
513 	if ((ps = (struct pmclog_parse_state *) malloc(sizeof(*ps))) == NULL)
514 		return NULL;
515 
516 	ps->ps_state = PL_STATE_NEW_RECORD;
517 	ps->ps_arch = -1;
518 	ps->ps_initialized = 0;
519 	ps->ps_count = 0;
520 	ps->ps_offset = (off_t) 0;
521 	bzero(&ps->ps_saved, sizeof(ps->ps_saved));
522 	ps->ps_svcount = 0;
523 	ps->ps_fd    = fd;
524 	ps->ps_data  = NULL;
525 	ps->ps_buffer = NULL;
526 	ps->ps_len   = 0;
527 
528 	/* allocate space for a work area */
529 	if (ps->ps_fd != PMCLOG_FD_NONE) {
530 		if ((ps->ps_buffer = malloc(PMCLOG_BUFFER_SIZE)) == NULL)
531 			return NULL;
532 	}
533 
534 	return ps;
535 }
536 
537 
538 /*
539  * Free up parser state.
540  */
541 
542 void
543 pmclog_close(void *cookie)
544 {
545 	struct pmclog_parse_state *ps;
546 
547 	ps = (struct pmclog_parse_state *) cookie;
548 
549 	if (ps->ps_buffer)
550 		free(ps->ps_buffer);
551 
552 	free(ps);
553 }
554