2 * Copyright (c) 2005-2007 Joseph Koshy
3 * Copyright (c) 2007 The FreeBSD Foundation
6 * Portions of this software were developed by A. Joseph Koshy under
7 * sponsorship from the FreeBSD Foundation and Google, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
36 #include <sys/pmclog.h>
48 #include <machine/pmc_mdep.h>
50 #define PMCLOG_BUFFER_SIZE 4096
55 * The pmclog(3) API is oriented towards parsing an event stream in
56 * "realtime", i.e., from an data source that may or may not preserve
57 * record boundaries -- for example when the data source is elsewhere
58 * on a network. The API allows data to be fed into the parser zero
59 * or more bytes at a time.
61 * The state for a log file parser is maintained in a 'struct
62 * pmclog_parse_state'. Parser invocations are done by calling
63 * 'pmclog_read()'; this function will inform the caller when a
64 * complete event is parsed.
66 * The parser first assembles a complete log file event in an internal
67 * work area (see "ps_saved" below). Once a complete log file event
68 * is read, the parser then parses it and converts it to an event
69 * descriptor usable by the client. We could possibly avoid this two
70 * step process by directly parsing the input log to set fields in the
71 * event record. However the parser's state machine would get
72 * insanely complicated, and this code is unlikely to be used in
73 * performance critical paths.
76 enum pmclog_parser_state {
77 PL_STATE_NEW_RECORD, /* in-between records */
78 PL_STATE_EXPECTING_HEADER, /* header being read */
79 PL_STATE_PARTIAL_RECORD, /* header present but not the record */
80 PL_STATE_ERROR /* parsing error encountered */
83 struct pmclog_parse_state {
84 enum pmclog_parser_state ps_state;
85 enum pmc_cputype ps_arch; /* log file architecture */
86 uint32_t ps_version; /* hwpmc version */
87 int ps_initialized; /* whether initialized */
88 int ps_count; /* count of records processed */
89 off_t ps_offset; /* stream byte offset */
90 union pmclog_entry ps_saved; /* saved partial log entry */
91 int ps_svcount; /* #bytes saved */
92 int ps_fd; /* active fd or -1 */
93 char *ps_buffer; /* scratch buffer if fd != -1 */
94 char *ps_data; /* current parse pointer */
95 size_t ps_len; /* length of buffered data */
98 #define PMCLOG_HEADER_FROM_SAVED_STATE(PS) \
99 (* ((uint32_t *) &(PS)->ps_saved))
101 #define PMCLOG_INITIALIZE_READER(LE,A) LE = (uint32_t *) &(A)
102 #define PMCLOG_READ32(LE,V) do { \
105 #define PMCLOG_READ64(LE,V) do { \
107 _v = (uint64_t) *(LE)++; \
108 _v |= ((uint64_t) *(LE)++) << 32; \
112 #define PMCLOG_READSTRING(LE,DST,LEN) strlcpy((DST), (char *) (LE), (LEN))
115 * Assemble a log record from '*len' octets starting from address '*data'.
116 * Update 'data' and 'len' to reflect the number of bytes consumed.
118 * '*data' is potentially an unaligned address and '*len' octets may
119 * not be enough to complete a event record.
122 static enum pmclog_parser_state
123 pmclog_get_record(struct pmclog_parse_state *ps, char **data, ssize_t *len)
125 int avail, copylen, recordsize, used;
127 const int HEADERSIZE = sizeof(uint32_t);
130 if ((avail = *len) <= 0)
131 return (ps->ps_state = PL_STATE_ERROR);
136 if (ps->ps_state == PL_STATE_NEW_RECORD)
139 dst = (char *) &ps->ps_saved + ps->ps_svcount;
141 switch (ps->ps_state) {
142 case PL_STATE_NEW_RECORD:
147 * Case A: avail < headersize
148 * -> 'expecting header'
150 * Case B: avail >= headersize
151 * B.1: avail < recordsize
152 * -> 'partial record'
153 * B.2: avail >= recordsize
157 copylen = avail < HEADERSIZE ? avail : HEADERSIZE;
158 bcopy(src, dst, copylen);
159 ps->ps_svcount = used = copylen;
161 if (copylen < HEADERSIZE) {
162 ps->ps_state = PL_STATE_EXPECTING_HEADER;
169 h = PMCLOG_HEADER_FROM_SAVED_STATE(ps);
170 recordsize = PMCLOG_HEADER_TO_LENGTH(h);
175 if (recordsize <= avail) { /* full record available */
176 bcopy(src, dst, recordsize - copylen);
177 ps->ps_svcount = used = recordsize;
181 /* header + a partial record is available */
182 bcopy(src, dst, avail - copylen);
183 ps->ps_svcount = used = avail;
184 ps->ps_state = PL_STATE_PARTIAL_RECORD;
188 case PL_STATE_EXPECTING_HEADER:
193 * Case C: avail+saved < headersize
194 * -> 'expecting header'
196 * Case D: avail+saved >= headersize
197 * D.1: avail+saved < recordsize
198 * -> 'partial record'
199 * D.2: avail+saved >= recordsize
201 * (see PARTIAL_RECORD handling below)
204 if (avail + ps->ps_svcount < HEADERSIZE) {
205 bcopy(src, dst, avail);
206 ps->ps_svcount += avail;
211 used = copylen = HEADERSIZE - ps->ps_svcount;
212 bcopy(src, dst, copylen);
216 ps->ps_svcount += copylen;
220 case PL_STATE_PARTIAL_RECORD:
225 * Case E: avail+saved < recordsize
226 * -> 'partial record'
228 * Case F: avail+saved >= recordsize
232 h = PMCLOG_HEADER_FROM_SAVED_STATE(ps);
233 recordsize = PMCLOG_HEADER_TO_LENGTH(h);
238 if (avail + ps->ps_svcount < recordsize) {
240 ps->ps_state = PL_STATE_PARTIAL_RECORD;
242 copylen = recordsize - ps->ps_svcount;
243 ps->ps_state = PL_STATE_NEW_RECORD;
246 bcopy(src, dst, copylen);
247 ps->ps_svcount += copylen;
261 ps->ps_state = PL_STATE_ERROR;
266 * Get an event from the stream pointed to by '*data'. '*len'
267 * indicates the number of bytes available to parse. Arguments
268 * '*data' and '*len' are updated to indicate the number of bytes
273 pmclog_get_event(void *cookie, char **data, ssize_t *len,
274 struct pmclog_ev *ev)
277 uint32_t h, *le, npc;
278 enum pmclog_parser_state e;
279 struct pmclog_parse_state *ps;
281 ps = (struct pmclog_parse_state *) cookie;
283 assert(ps->ps_state != PL_STATE_ERROR);
285 if ((e = pmclog_get_record(ps,data,len)) == PL_STATE_ERROR) {
286 ev->pl_state = PMCLOG_ERROR;
290 if (e != PL_STATE_NEW_RECORD) {
291 ev->pl_state = PMCLOG_REQUIRE_DATA;
295 PMCLOG_INITIALIZE_READER(le, ps->ps_saved);
299 if (!PMCLOG_HEADER_CHECK_MAGIC(h)) {
300 ps->ps_state = PL_STATE_ERROR;
301 ev->pl_state = PMCLOG_ERROR;
305 /* copy out the time stamp */
306 PMCLOG_READ32(le,ev->pl_ts.tv_sec);
307 PMCLOG_READ32(le,ev->pl_ts.tv_nsec);
309 evlen = PMCLOG_HEADER_TO_LENGTH(h);
311 #define PMCLOG_GET_PATHLEN(P,E,TYPE) do { \
312 (P) = (E) - offsetof(struct TYPE, pl_pathname); \
313 if ((P) > PATH_MAX || (P) < 0) \
317 #define PMCLOG_GET_CALLCHAIN_SIZE(SZ,E) do { \
318 (SZ) = ((E) - offsetof(struct pmclog_callchain, pl_pc)) \
319 / sizeof(uintfptr_t); \
322 switch (ev->pl_type = PMCLOG_HEADER_TO_TYPE(h)) {
323 case PMCLOG_TYPE_CALLCHAIN:
324 PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_pid);
325 PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_pmcid);
326 PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_cpuflags);
327 PMCLOG_GET_CALLCHAIN_SIZE(ev->pl_u.pl_cc.pl_npc,evlen);
328 for (npc = 0; npc < ev->pl_u.pl_cc.pl_npc; npc++)
329 PMCLOG_READADDR(le,ev->pl_u.pl_cc.pl_pc[npc]);
330 for (;npc < PMC_CALLCHAIN_DEPTH_MAX; npc++)
331 ev->pl_u.pl_cc.pl_pc[npc] = (uintfptr_t) 0;
333 case PMCLOG_TYPE_CLOSELOG:
334 case PMCLOG_TYPE_DROPNOTIFY:
337 case PMCLOG_TYPE_INITIALIZE:
338 PMCLOG_READ32(le,ev->pl_u.pl_i.pl_version);
339 PMCLOG_READ32(le,ev->pl_u.pl_i.pl_arch);
340 ps->ps_version = ev->pl_u.pl_i.pl_version;
341 ps->ps_arch = ev->pl_u.pl_i.pl_arch;
342 ps->ps_initialized = 1;
344 case PMCLOG_TYPE_MAP_IN:
345 PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_map_in);
346 PMCLOG_READ32(le,ev->pl_u.pl_mi.pl_pid);
347 PMCLOG_READADDR(le,ev->pl_u.pl_mi.pl_start);
348 PMCLOG_READSTRING(le, ev->pl_u.pl_mi.pl_pathname, pathlen);
350 case PMCLOG_TYPE_MAP_OUT:
351 PMCLOG_READ32(le,ev->pl_u.pl_mo.pl_pid);
352 PMCLOG_READADDR(le,ev->pl_u.pl_mo.pl_start);
353 PMCLOG_READADDR(le,ev->pl_u.pl_mo.pl_end);
355 case PMCLOG_TYPE_PCSAMPLE:
356 PMCLOG_READ32(le,ev->pl_u.pl_s.pl_pid);
357 PMCLOG_READADDR(le,ev->pl_u.pl_s.pl_pc);
358 PMCLOG_READ32(le,ev->pl_u.pl_s.pl_pmcid);
359 PMCLOG_READ32(le,ev->pl_u.pl_s.pl_usermode);
361 case PMCLOG_TYPE_PMCALLOCATE:
362 PMCLOG_READ32(le,ev->pl_u.pl_a.pl_pmcid);
363 PMCLOG_READ32(le,ev->pl_u.pl_a.pl_event);
364 PMCLOG_READ32(le,ev->pl_u.pl_a.pl_flags);
365 if ((ev->pl_u.pl_a.pl_evname =
366 pmc_name_of_event(ev->pl_u.pl_a.pl_event)) == NULL)
369 case PMCLOG_TYPE_PMCATTACH:
370 PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_pmcattach);
371 PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pmcid);
372 PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pid);
373 PMCLOG_READSTRING(le,ev->pl_u.pl_t.pl_pathname,pathlen);
375 case PMCLOG_TYPE_PMCDETACH:
376 PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pmcid);
377 PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pid);
379 case PMCLOG_TYPE_PROCCSW:
380 PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pmcid);
381 PMCLOG_READ64(le,ev->pl_u.pl_c.pl_value);
382 PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pid);
384 case PMCLOG_TYPE_PROCEXEC:
385 PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_procexec);
386 PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pid);
387 PMCLOG_READADDR(le,ev->pl_u.pl_x.pl_entryaddr);
388 PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pmcid);
389 PMCLOG_READSTRING(le,ev->pl_u.pl_x.pl_pathname,pathlen);
391 case PMCLOG_TYPE_PROCEXIT:
392 PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pmcid);
393 PMCLOG_READ64(le,ev->pl_u.pl_e.pl_value);
394 PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pid);
396 case PMCLOG_TYPE_PROCFORK:
397 PMCLOG_READ32(le,ev->pl_u.pl_f.pl_oldpid);
398 PMCLOG_READ32(le,ev->pl_u.pl_f.pl_newpid);
400 case PMCLOG_TYPE_SYSEXIT:
401 PMCLOG_READ32(le,ev->pl_u.pl_se.pl_pid);
403 case PMCLOG_TYPE_USERDATA:
404 PMCLOG_READ32(le,ev->pl_u.pl_u.pl_userdata);
406 default: /* unknown record type */
407 ps->ps_state = PL_STATE_ERROR;
408 ev->pl_state = PMCLOG_ERROR;
412 ev->pl_offset = (ps->ps_offset += evlen);
413 ev->pl_count = (ps->ps_count += 1);
414 ev->pl_state = PMCLOG_OK;
418 ev->pl_state = PMCLOG_ERROR;
419 ps->ps_state = PL_STATE_ERROR;
424 * Extract and return the next event from the byte stream.
426 * Returns 0 and sets the event's state to PMCLOG_OK in case an event
427 * was successfully parsed. Otherwise this function returns -1 and
428 * sets the event's state to one of PMCLOG_REQUIRE_DATA (if more data
429 * is needed) or PMCLOG_EOF (if an EOF was seen) or PMCLOG_ERROR if
430 * a parse error was encountered.
434 pmclog_read(void *cookie, struct pmclog_ev *ev)
438 struct pmclog_parse_state *ps;
440 ps = (struct pmclog_parse_state *) cookie;
442 if (ps->ps_state == PL_STATE_ERROR) {
443 ev->pl_state = PMCLOG_ERROR;
448 * If there isn't enough data left for a new event try and get
451 if (ps->ps_len == 0) {
452 ev->pl_state = PMCLOG_REQUIRE_DATA;
455 * If we have a valid file descriptor to read from, attempt
456 * to read from that. This read may return with an error,
457 * (which may be EAGAIN or other recoverable error), or
460 if (ps->ps_fd != PMCLOG_FD_NONE) {
462 nread = read(ps->ps_fd, ps->ps_buffer,
467 ev->pl_state = PMCLOG_EOF;
468 else if (errno != EAGAIN) /* not restartable */
469 ev->pl_state = PMCLOG_ERROR;
474 ps->ps_data = ps->ps_buffer;
479 assert(ps->ps_len > 0);
482 /* Retrieve one event from the byte stream. */
483 retval = pmclog_get_event(ps, &ps->ps_data, &ps->ps_len, ev);
486 * If we need more data and we have a configured fd, try read
489 if (retval < 0 && ev->pl_state == PMCLOG_REQUIRE_DATA &&
491 assert(ps->ps_len == 0);
499 * Feed data to a memory based parser.
501 * The memory area pointed to by 'data' needs to be valid till the
502 * next error return from pmclog_next_event().
506 pmclog_feed(void *cookie, char *data, int len)
508 struct pmclog_parse_state *ps;
510 ps = (struct pmclog_parse_state *) cookie;
512 if (len < 0 || /* invalid length */
513 ps->ps_buffer || /* called for a file parser */
514 ps->ps_len != 0) /* unnecessary call */
524 * Allocate and initialize parser state.
530 struct pmclog_parse_state *ps;
532 if ((ps = (struct pmclog_parse_state *) malloc(sizeof(*ps))) == NULL)
535 ps->ps_state = PL_STATE_NEW_RECORD;
537 ps->ps_initialized = 0;
539 ps->ps_offset = (off_t) 0;
540 bzero(&ps->ps_saved, sizeof(ps->ps_saved));
544 ps->ps_buffer = NULL;
547 /* allocate space for a work area */
548 if (ps->ps_fd != PMCLOG_FD_NONE) {
549 if ((ps->ps_buffer = malloc(PMCLOG_BUFFER_SIZE)) == NULL)
558 * Free up parser state.
562 pmclog_close(void *cookie)
564 struct pmclog_parse_state *ps;
566 ps = (struct pmclog_parse_state *) cookie;