2 * Copyright (c) 2005-2006 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/pmclog.h>
44 #include <machine/pmc_mdep.h>
46 #define PMCLOG_BUFFER_SIZE 4096
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.
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.
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.
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 */
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 */
94 #define PMCLOG_HEADER_FROM_SAVED_STATE(PS) \
95 (* ((uint32_t *) &(PS)->ps_saved))
97 #define PMCLOG_INITIALIZE_READER(LE,A) LE = (uint32_t *) &(A)
98 #define PMCLOG_READ32(LE,V) do { \
101 #define PMCLOG_READ64(LE,V) do { \
103 _v = (uint64_t) *(LE)++; \
104 _v |= ((uint64_t) *(LE)++) << 32; \
108 #define PMCLOG_READSTRING(LE,DST,LEN) strlcpy((DST), (char *) (LE), (LEN))
111 * Assemble a log record from '*len' octets starting from address '*data'.
112 * Update 'data' and 'len' to reflect the number of bytes consumed.
114 * '*data' is potentially an unaligned address and '*len' octets may
115 * not be enough to complete a event record.
118 static enum pmclog_parser_state
119 pmclog_get_record(struct pmclog_parse_state *ps, char **data, ssize_t *len)
121 int avail, copylen, recordsize, used;
123 const int HEADERSIZE = sizeof(uint32_t);
126 if ((avail = *len) <= 0)
127 return (ps->ps_state = PL_STATE_ERROR);
132 if (ps->ps_state == PL_STATE_NEW_RECORD)
135 dst = (char *) &ps->ps_saved + ps->ps_svcount;
137 switch (ps->ps_state) {
138 case PL_STATE_NEW_RECORD:
143 * Case A: avail < headersize
144 * -> 'expecting header'
146 * Case B: avail >= headersize
147 * B.1: avail < recordsize
148 * -> 'partial record'
149 * B.2: avail >= recordsize
153 copylen = avail < HEADERSIZE ? avail : HEADERSIZE;
154 bcopy(src, dst, copylen);
155 ps->ps_svcount = used = copylen;
157 if (copylen < HEADERSIZE) {
158 ps->ps_state = PL_STATE_EXPECTING_HEADER;
165 h = PMCLOG_HEADER_FROM_SAVED_STATE(ps);
166 recordsize = PMCLOG_HEADER_TO_LENGTH(h);
171 if (recordsize <= avail) { /* full record available */
172 bcopy(src, dst, recordsize - copylen);
173 ps->ps_svcount = used = recordsize;
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;
184 case PL_STATE_EXPECTING_HEADER:
189 * Case C: avail+saved < headersize
190 * -> 'expecting header'
192 * Case D: avail+saved >= headersize
193 * D.1: avail+saved < recordsize
194 * -> 'partial record'
195 * D.2: avail+saved >= recordsize
197 * (see PARTIAL_RECORD handling below)
200 if (avail + ps->ps_svcount < HEADERSIZE) {
201 bcopy(src, dst, avail);
202 ps->ps_svcount += avail;
207 used = copylen = HEADERSIZE - ps->ps_svcount;
208 bcopy(src, dst, copylen);
212 ps->ps_svcount += copylen;
216 case PL_STATE_PARTIAL_RECORD:
221 * Case E: avail+saved < recordsize
222 * -> 'partial record'
224 * Case F: avail+saved >= recordsize
228 h = PMCLOG_HEADER_FROM_SAVED_STATE(ps);
229 recordsize = PMCLOG_HEADER_TO_LENGTH(h);
234 if (avail + ps->ps_svcount < recordsize) {
236 ps->ps_state = PL_STATE_PARTIAL_RECORD;
238 copylen = recordsize - ps->ps_svcount;
239 ps->ps_state = PL_STATE_NEW_RECORD;
242 bcopy(src, dst, copylen);
243 ps->ps_svcount += copylen;
257 ps->ps_state = PL_STATE_ERROR;
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
269 pmclog_get_event(void *cookie, char **data, ssize_t *len,
270 struct pmclog_ev *ev)
274 enum pmclog_parser_state e;
275 struct pmclog_parse_state *ps;
277 ps = (struct pmclog_parse_state *) cookie;
279 assert(ps->ps_state != PL_STATE_ERROR);
281 if ((e = pmclog_get_record(ps,data,len)) == PL_STATE_ERROR) {
282 ev->pl_state = PMCLOG_ERROR;
286 if (e != PL_STATE_NEW_RECORD) {
287 ev->pl_state = PMCLOG_REQUIRE_DATA;
291 PMCLOG_INITIALIZE_READER(le, ps->ps_saved);
295 if (!PMCLOG_HEADER_CHECK_MAGIC(h)) {
296 ps->ps_state = PL_STATE_ERROR;
297 ev->pl_state = PMCLOG_ERROR;
301 /* copy out the time stamp */
302 PMCLOG_READ32(le,ev->pl_ts.tv_sec);
303 PMCLOG_READ32(le,ev->pl_ts.tv_nsec);
305 evlen = PMCLOG_HEADER_TO_LENGTH(h);
307 #define PMCLOG_GET_PATHLEN(P,E,TYPE) do { \
308 (P) = (E) - offsetof(struct TYPE, pl_pathname); \
309 if ((P) > PATH_MAX || (P) < 0) \
313 switch (ev->pl_type = PMCLOG_HEADER_TO_TYPE(h)) {
314 case PMCLOG_TYPE_CLOSELOG:
315 case PMCLOG_TYPE_DROPNOTIFY:
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;
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);
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);
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);
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)
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);
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);
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);
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);
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);
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);
381 case PMCLOG_TYPE_SYSEXIT:
382 PMCLOG_READ32(le,ev->pl_u.pl_se.pl_pid);
384 case PMCLOG_TYPE_USERDATA:
385 PMCLOG_READ32(le,ev->pl_u.pl_u.pl_userdata);
387 default: /* unknown record type */
388 ps->ps_state = PL_STATE_ERROR;
389 ev->pl_state = PMCLOG_ERROR;
393 ev->pl_offset = (ps->ps_offset += evlen);
394 ev->pl_count = (ps->ps_count += 1);
395 ev->pl_state = PMCLOG_OK;
399 ev->pl_state = PMCLOG_ERROR;
400 ps->ps_state = PL_STATE_ERROR;
405 * Extract and return the next event from the byte stream.
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.
415 pmclog_read(void *cookie, struct pmclog_ev *ev)
419 struct pmclog_parse_state *ps;
421 ps = (struct pmclog_parse_state *) cookie;
423 if (ps->ps_state == PL_STATE_ERROR) {
424 ev->pl_state = PMCLOG_ERROR;
429 * If there isn't enough data left for a new event try and get
432 if (ps->ps_len == 0) {
433 ev->pl_state = PMCLOG_REQUIRE_DATA;
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
441 if (ps->ps_fd != PMCLOG_FD_NONE) {
443 nread = read(ps->ps_fd, ps->ps_buffer,
448 ev->pl_state = PMCLOG_EOF;
449 else if (errno != EAGAIN) /* not restartable */
450 ev->pl_state = PMCLOG_ERROR;
455 ps->ps_data = ps->ps_buffer;
460 assert(ps->ps_len > 0);
463 /* Retrieve one event from the byte stream. */
464 retval = pmclog_get_event(ps, &ps->ps_data, &ps->ps_len, ev);
467 * If we need more data and we have a configured fd, try read
470 if (retval < 0 && ev->pl_state == PMCLOG_REQUIRE_DATA &&
472 assert(ps->ps_len == 0);
480 * Feed data to a memory based parser.
482 * The memory area pointed to by 'data' needs to be valid till the
483 * next error return from pmclog_next_event().
487 pmclog_feed(void *cookie, char *data, int len)
489 struct pmclog_parse_state *ps;
491 ps = (struct pmclog_parse_state *) cookie;
493 if (len < 0 || /* invalid length */
494 ps->ps_buffer || /* called for a file parser */
495 ps->ps_len != 0) /* unnecessary call */
505 * Allocate and initialize parser state.
511 struct pmclog_parse_state *ps;
513 if ((ps = (struct pmclog_parse_state *) malloc(sizeof(*ps))) == NULL)
516 ps->ps_state = PL_STATE_NEW_RECORD;
518 ps->ps_initialized = 0;
520 ps->ps_offset = (off_t) 0;
521 bzero(&ps->ps_saved, sizeof(ps->ps_saved));
525 ps->ps_buffer = NULL;
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)
539 * Free up parser state.
543 pmclog_close(void *cookie)
545 struct pmclog_parse_state *ps;
547 ps = (struct pmclog_parse_state *) cookie;