2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2005-2007 Joseph Koshy
5 * Copyright (c) 2007 The FreeBSD Foundation
8 * Portions of this software were developed by A. Joseph Koshy under
9 * sponsorship from the FreeBSD Foundation and Google, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 #include <sys/param.h>
36 #include <sys/pmclog.h>
49 #include <machine/pmc_mdep.h>
51 #include "libpmcinternal.h"
53 #define PMCLOG_BUFFER_SIZE 512*1024
58 * The pmclog(3) API is oriented towards parsing an event stream in
59 * "realtime", i.e., from an data source that may or may not preserve
60 * record boundaries -- for example when the data source is elsewhere
61 * on a network. The API allows data to be fed into the parser zero
62 * or more bytes at a time.
64 * The state for a log file parser is maintained in a 'struct
65 * pmclog_parse_state'. Parser invocations are done by calling
66 * 'pmclog_read()'; this function will inform the caller when a
67 * complete event is parsed.
69 * The parser first assembles a complete log file event in an internal
70 * work area (see "ps_saved" below). Once a complete log file event
71 * is read, the parser then parses it and converts it to an event
72 * descriptor usable by the client. We could possibly avoid this two
73 * step process by directly parsing the input log to set fields in the
74 * event record. However the parser's state machine would get
75 * insanely complicated, and this code is unlikely to be used in
76 * performance critical paths.
79 #define PMCLOG_HEADER_FROM_SAVED_STATE(PS) \
80 (* ((uint32_t *) &(PS)->ps_saved))
82 #define PMCLOG_INITIALIZE_READER(LE,A) LE = (uint32_t *) &(A)
83 #define PMCLOG_READ32(LE,V) do { \
86 #define PMCLOG_READ64(LE,V) do { \
88 _v = (uint64_t) *(LE)++; \
89 _v |= ((uint64_t) *(LE)++) << 32; \
93 #define PMCLOG_READSTRING(LE,DST,LEN) strlcpy((DST), (char *) (LE), (LEN))
96 * Assemble a log record from '*len' octets starting from address '*data'.
97 * Update 'data' and 'len' to reflect the number of bytes consumed.
99 * '*data' is potentially an unaligned address and '*len' octets may
100 * not be enough to complete a event record.
103 static enum pmclog_parser_state
104 pmclog_get_record(struct pmclog_parse_state *ps, char **data, ssize_t *len)
106 int avail, copylen, recordsize, used;
108 const int HEADERSIZE = sizeof(uint32_t);
111 if ((avail = *len) <= 0)
112 return (ps->ps_state = PL_STATE_ERROR);
117 if (ps->ps_state == PL_STATE_NEW_RECORD)
120 dst = (char *) &ps->ps_saved + ps->ps_svcount;
122 switch (ps->ps_state) {
123 case PL_STATE_NEW_RECORD:
128 * Case A: avail < headersize
129 * -> 'expecting header'
131 * Case B: avail >= headersize
132 * B.1: avail < recordsize
133 * -> 'partial record'
134 * B.2: avail >= recordsize
138 copylen = avail < HEADERSIZE ? avail : HEADERSIZE;
139 bcopy(src, dst, copylen);
140 ps->ps_svcount = used = copylen;
142 if (copylen < HEADERSIZE) {
143 ps->ps_state = PL_STATE_EXPECTING_HEADER;
150 h = PMCLOG_HEADER_FROM_SAVED_STATE(ps);
151 recordsize = PMCLOG_HEADER_TO_LENGTH(h);
156 if (recordsize <= avail) { /* full record available */
157 bcopy(src, dst, recordsize - copylen);
158 ps->ps_svcount = used = recordsize;
162 /* header + a partial record is available */
163 bcopy(src, dst, avail - copylen);
164 ps->ps_svcount = used = avail;
165 ps->ps_state = PL_STATE_PARTIAL_RECORD;
169 case PL_STATE_EXPECTING_HEADER:
174 * Case C: avail+saved < headersize
175 * -> 'expecting header'
177 * Case D: avail+saved >= headersize
178 * D.1: avail+saved < recordsize
179 * -> 'partial record'
180 * D.2: avail+saved >= recordsize
182 * (see PARTIAL_RECORD handling below)
185 if (avail + ps->ps_svcount < HEADERSIZE) {
186 bcopy(src, dst, avail);
187 ps->ps_svcount += avail;
192 used = copylen = HEADERSIZE - ps->ps_svcount;
193 bcopy(src, dst, copylen);
197 ps->ps_svcount += copylen;
201 case PL_STATE_PARTIAL_RECORD:
206 * Case E: avail+saved < recordsize
207 * -> 'partial record'
209 * Case F: avail+saved >= recordsize
213 h = PMCLOG_HEADER_FROM_SAVED_STATE(ps);
214 recordsize = PMCLOG_HEADER_TO_LENGTH(h);
219 if (avail + ps->ps_svcount < recordsize) {
221 ps->ps_state = PL_STATE_PARTIAL_RECORD;
223 copylen = recordsize - ps->ps_svcount;
224 ps->ps_state = PL_STATE_NEW_RECORD;
227 bcopy(src, dst, copylen);
228 ps->ps_svcount += copylen;
242 ps->ps_state = PL_STATE_ERROR;
247 * Get an event from the stream pointed to by '*data'. '*len'
248 * indicates the number of bytes available to parse. Arguments
249 * '*data' and '*len' are updated to indicate the number of bytes
254 pmclog_get_event(void *cookie, char **data, ssize_t *len,
255 struct pmclog_ev *ev)
258 uint32_t h, *le, npc, noop;
259 enum pmclog_parser_state e;
260 struct pmclog_parse_state *ps;
261 struct pmclog_header *ph;
263 ps = (struct pmclog_parse_state *) cookie;
265 assert(ps->ps_state != PL_STATE_ERROR);
267 if ((e = pmclog_get_record(ps,data,len)) == PL_STATE_ERROR) {
268 ev->pl_state = PMCLOG_ERROR;
269 printf("state error\n");
273 if (e != PL_STATE_NEW_RECORD) {
274 ev->pl_state = PMCLOG_REQUIRE_DATA;
278 PMCLOG_INITIALIZE_READER(le, ps->ps_saved);
280 ph = (struct pmclog_header *)(uintptr_t)le;
283 if (!PMCLOG_HEADER_CHECK_MAGIC(h)) {
284 printf("bad magic\n");
285 ps->ps_state = PL_STATE_ERROR;
286 ev->pl_state = PMCLOG_ERROR;
290 /* copy out the time stamp */
291 ev->pl_ts.tv_sec = ph->pl_tsc;
294 evlen = PMCLOG_HEADER_TO_LENGTH(h);
296 #define PMCLOG_GET_PATHLEN(P,E,TYPE) do { \
297 (P) = (E) - offsetof(struct TYPE, pl_pathname); \
298 if ((P) > PATH_MAX || (P) < 0) \
302 #define PMCLOG_GET_CALLCHAIN_SIZE(SZ,E) do { \
303 (SZ) = ((E) - offsetof(struct pmclog_callchain, pl_pc)) \
304 / sizeof(uintfptr_t); \
307 switch (ev->pl_type = PMCLOG_HEADER_TO_TYPE(h)) {
308 case PMCLOG_TYPE_CALLCHAIN:
309 PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_pid);
310 PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_tid);
311 PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_pmcid);
312 PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_cpuflags);
313 PMCLOG_GET_CALLCHAIN_SIZE(ev->pl_u.pl_cc.pl_npc,evlen);
314 for (npc = 0; npc < ev->pl_u.pl_cc.pl_npc; npc++)
315 PMCLOG_READADDR(le,ev->pl_u.pl_cc.pl_pc[npc]);
316 for (;npc < PMC_CALLCHAIN_DEPTH_MAX; npc++)
317 ev->pl_u.pl_cc.pl_pc[npc] = (uintfptr_t) 0;
319 case PMCLOG_TYPE_CLOSELOG:
320 ev->pl_state = PMCLOG_EOF;
322 case PMCLOG_TYPE_DROPNOTIFY:
325 case PMCLOG_TYPE_INITIALIZE:
326 PMCLOG_READ32(le,ev->pl_u.pl_i.pl_version);
327 PMCLOG_READ32(le,ev->pl_u.pl_i.pl_arch);
328 PMCLOG_READ64(le,ev->pl_u.pl_i.pl_tsc_freq);
329 memcpy(&ev->pl_u.pl_i.pl_ts, le, sizeof(struct timespec));
330 le += sizeof(struct timespec)/4;
331 PMCLOG_READSTRING(le, ev->pl_u.pl_i.pl_cpuid, PMC_CPUID_LEN);
332 memcpy(ev->pl_u.pl_i.pl_cpuid, le, PMC_CPUID_LEN);
333 ps->ps_cpuid = strdup(ev->pl_u.pl_i.pl_cpuid);
334 ps->ps_version = ev->pl_u.pl_i.pl_version;
335 ps->ps_arch = ev->pl_u.pl_i.pl_arch;
336 ps->ps_initialized = 1;
338 case PMCLOG_TYPE_MAP_IN:
339 PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_map_in);
340 PMCLOG_READ32(le,ev->pl_u.pl_mi.pl_pid);
341 PMCLOG_READ32(le,noop);
342 PMCLOG_READADDR(le,ev->pl_u.pl_mi.pl_start);
343 PMCLOG_READSTRING(le, ev->pl_u.pl_mi.pl_pathname, pathlen);
345 case PMCLOG_TYPE_MAP_OUT:
346 PMCLOG_READ32(le,ev->pl_u.pl_mo.pl_pid);
347 PMCLOG_READ32(le,noop);
348 PMCLOG_READADDR(le,ev->pl_u.pl_mo.pl_start);
349 PMCLOG_READADDR(le,ev->pl_u.pl_mo.pl_end);
351 case PMCLOG_TYPE_PMCALLOCATE:
352 PMCLOG_READ32(le,ev->pl_u.pl_a.pl_pmcid);
353 PMCLOG_READ32(le,ev->pl_u.pl_a.pl_event);
354 PMCLOG_READ32(le,ev->pl_u.pl_a.pl_flags);
355 PMCLOG_READ32(le,noop);
356 PMCLOG_READ64(le,ev->pl_u.pl_a.pl_rate);
359 * Could be either a PMC event code or a PMU event index;
360 * assume that their encodings don't overlap (i.e. no PMU event
361 * table is more than 0x1000 entries) to distinguish them here.
362 * Otherwise pmc_pmu_event_get_by_idx will go out of bounds if
363 * given a PMC event code when it knows about that CPU.
365 * XXX: Ideally we'd have user flags to give us that context.
367 if (ev->pl_u.pl_a.pl_event < PMC_EVENT_FIRST)
368 ev->pl_u.pl_a.pl_evname =
369 pmc_pmu_event_get_by_idx(ps->ps_cpuid,
370 ev->pl_u.pl_a.pl_event);
371 else if (ev->pl_u.pl_a.pl_event <= PMC_EVENT_LAST)
372 ev->pl_u.pl_a.pl_evname =
373 _pmc_name_of_event(ev->pl_u.pl_a.pl_event,
376 ev->pl_u.pl_a.pl_evname = NULL;
377 if (ev->pl_u.pl_a.pl_evname == NULL) {
378 printf("unknown event\n");
382 case PMCLOG_TYPE_PMCALLOCATEDYN:
383 PMCLOG_READ32(le,ev->pl_u.pl_ad.pl_pmcid);
384 PMCLOG_READ32(le,ev->pl_u.pl_ad.pl_event);
385 PMCLOG_READ32(le,ev->pl_u.pl_ad.pl_flags);
386 PMCLOG_READ32(le,noop);
387 PMCLOG_READSTRING(le,ev->pl_u.pl_ad.pl_evname,PMC_NAME_MAX);
389 case PMCLOG_TYPE_PMCATTACH:
390 PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_pmcattach);
391 PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pmcid);
392 PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pid);
393 PMCLOG_READSTRING(le,ev->pl_u.pl_t.pl_pathname,pathlen);
395 case PMCLOG_TYPE_PMCDETACH:
396 PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pmcid);
397 PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pid);
399 case PMCLOG_TYPE_PROCCSW:
400 PMCLOG_READ64(le,ev->pl_u.pl_c.pl_value);
401 PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pmcid);
402 PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pid);
403 PMCLOG_READ32(le,ev->pl_u.pl_c.pl_tid);
405 case PMCLOG_TYPE_PROCEXEC:
406 PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_procexec);
407 PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pid);
408 PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pmcid);
409 PMCLOG_READADDR(le,ev->pl_u.pl_x.pl_entryaddr);
410 PMCLOG_READSTRING(le,ev->pl_u.pl_x.pl_pathname,pathlen);
412 case PMCLOG_TYPE_PROCEXIT:
413 PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pmcid);
414 PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pid);
415 PMCLOG_READ64(le,ev->pl_u.pl_e.pl_value);
417 case PMCLOG_TYPE_PROCFORK:
418 PMCLOG_READ32(le,ev->pl_u.pl_f.pl_oldpid);
419 PMCLOG_READ32(le,ev->pl_u.pl_f.pl_newpid);
421 case PMCLOG_TYPE_SYSEXIT:
422 PMCLOG_READ32(le,ev->pl_u.pl_se.pl_pid);
424 case PMCLOG_TYPE_USERDATA:
425 PMCLOG_READ32(le,ev->pl_u.pl_u.pl_userdata);
427 case PMCLOG_TYPE_THR_CREATE:
428 PMCLOG_READ32(le,ev->pl_u.pl_tc.pl_tid);
429 PMCLOG_READ32(le,ev->pl_u.pl_tc.pl_pid);
430 PMCLOG_READ32(le,ev->pl_u.pl_tc.pl_flags);
431 PMCLOG_READ32(le,noop);
432 memcpy(ev->pl_u.pl_tc.pl_tdname, le, MAXCOMLEN+1);
434 case PMCLOG_TYPE_THR_EXIT:
435 PMCLOG_READ32(le,ev->pl_u.pl_te.pl_tid);
437 case PMCLOG_TYPE_PROC_CREATE:
438 PMCLOG_READ32(le,ev->pl_u.pl_pc.pl_pid);
439 PMCLOG_READ32(le,ev->pl_u.pl_pc.pl_flags);
440 memcpy(ev->pl_u.pl_pc.pl_pcomm, le, MAXCOMLEN+1);
442 default: /* unknown record type */
443 ps->ps_state = PL_STATE_ERROR;
444 ev->pl_state = PMCLOG_ERROR;
448 ev->pl_offset = (ps->ps_offset += evlen);
449 ev->pl_count = (ps->ps_count += 1);
451 ev->pl_state = PMCLOG_OK;
455 ev->pl_state = PMCLOG_ERROR;
456 ps->ps_state = PL_STATE_ERROR;
461 * Extract and return the next event from the byte stream.
463 * Returns 0 and sets the event's state to PMCLOG_OK in case an event
464 * was successfully parsed. Otherwise this function returns -1 and
465 * sets the event's state to one of PMCLOG_REQUIRE_DATA (if more data
466 * is needed) or PMCLOG_EOF (if an EOF was seen) or PMCLOG_ERROR if
467 * a parse error was encountered.
471 pmclog_read(void *cookie, struct pmclog_ev *ev)
475 struct pmclog_parse_state *ps;
477 ps = (struct pmclog_parse_state *) cookie;
479 if (ps->ps_state == PL_STATE_ERROR) {
480 ev->pl_state = PMCLOG_ERROR;
485 * If there isn't enough data left for a new event try and get
488 if (ps->ps_len == 0) {
489 ev->pl_state = PMCLOG_REQUIRE_DATA;
492 * If we have a valid file descriptor to read from, attempt
493 * to read from that. This read may return with an error,
494 * (which may be EAGAIN or other recoverable error), or
497 if (ps->ps_fd != PMCLOG_FD_NONE) {
499 nread = read(ps->ps_fd, ps->ps_buffer,
504 ev->pl_state = PMCLOG_EOF;
505 else if (errno != EAGAIN) /* not restartable */
506 ev->pl_state = PMCLOG_ERROR;
511 ps->ps_data = ps->ps_buffer;
517 assert(ps->ps_len > 0);
520 /* Retrieve one event from the byte stream. */
521 retval = pmclog_get_event(ps, &ps->ps_data, &ps->ps_len, ev);
523 * If we need more data and we have a configured fd, try read
526 if (retval < 0 && ev->pl_state == PMCLOG_REQUIRE_DATA &&
528 assert(ps->ps_len == 0);
536 * Feed data to a memory based parser.
538 * The memory area pointed to by 'data' needs to be valid till the
539 * next error return from pmclog_next_event().
543 pmclog_feed(void *cookie, char *data, int len)
545 struct pmclog_parse_state *ps;
547 ps = (struct pmclog_parse_state *) cookie;
549 if (len < 0 || /* invalid length */
550 ps->ps_buffer || /* called for a file parser */
551 ps->ps_len != 0) /* unnecessary call */
561 * Allocate and initialize parser state.
567 struct pmclog_parse_state *ps;
569 if ((ps = (struct pmclog_parse_state *) malloc(sizeof(*ps))) == NULL)
572 ps->ps_state = PL_STATE_NEW_RECORD;
574 ps->ps_initialized = 0;
576 ps->ps_offset = (off_t) 0;
577 bzero(&ps->ps_saved, sizeof(ps->ps_saved));
582 ps->ps_buffer = NULL;
585 /* allocate space for a work area */
586 if (ps->ps_fd != PMCLOG_FD_NONE) {
587 if ((ps->ps_buffer = malloc(PMCLOG_BUFFER_SIZE)) == NULL) {
598 * Free up parser state.
602 pmclog_close(void *cookie)
604 struct pmclog_parse_state *ps;
606 ps = (struct pmclog_parse_state *) cookie;