2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018, Matthew Macy
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/cpuset.h>
34 #include <sys/event.h>
35 #include <sys/queue.h>
36 #include <sys/socket.h>
38 #include <sys/sysctl.h>
40 #include <sys/ttycom.h>
68 #include <libpmcstat.h>
72 #include <unordered_map>
74 #include <pmcformat.h>
77 using std::unordered_map;
78 typedef unordered_map < int ,string > idmap;
79 typedef pair < int ,string > identry;
82 static struct option longopts[] = {
83 {"lwps", required_argument, NULL, 't'},
84 {"pids", required_argument, NULL, 'p'},
85 {"threads", required_argument, NULL, 'T'},
86 {"processes", required_argument, NULL, 'P'},
87 {"events", required_argument, NULL, 'e'},
95 "\t filter log file\n"
96 "\t -e <events>, --events <events> -- comma-delimited list of events to filter on\n"
97 "\t -p <pids>, --pids <pids> -- comma-delimited list of pids to filter on\n"
98 "\t -P <processes>, --processes <processes> -- comma-delimited list of process names to filter on\n"
99 "\t -t <lwps>, --lwps <lwps> -- comma-delimited list of lwps to filter on\n"
100 "\t -T <threads>, --threads <threads> -- comma-delimited list of thread names to filter on\n"
101 "\t -x -- toggle inclusive filtering\n"
107 parse_intlist(char *strlist, uint32_t *intlist, int *pcount, int (*fn) (const char *))
113 while ((token = strsep(&strlist, ",")) != NULL &&
115 if ((tokenval = fn(token)) < 0)
116 errx(EX_USAGE, "ERROR: %s not usable value", token);
117 intlist[count++] = tokenval;
123 parse_events(char *strlist, uint32_t intlist[LIST_MAX], int *pcount, char *cpuid)
129 while ((token = strsep(&strlist, ",")) != NULL &&
131 if ((tokenval = pmc_pmu_idx_get_by_event(cpuid, token)) < 0)
132 errx(EX_USAGE, "ERROR: %s not usable value", token);
133 intlist[count++] = tokenval;
139 parse_names(char *strlist, char *namelist[LIST_MAX], int *pcount)
145 while ((token = strsep(&strlist, ",")) != NULL &&
147 namelist[count++] = token;
157 #define _PMCLOG_TO_HEADER(T,L) \
158 ((PMCLOG_HEADER_MAGIC << 24) | \
159 (PMCLOG_TYPE_ ## T << 16) | \
163 pmc_find_name(idmap & map, uint32_t id, char *list[LIST_MAX], int count)
167 auto kvpair = map.find(id);
168 if (kvpair == map.end()) {
169 printf("unknown id: %d\n", id);
173 for (i = 0; i < count; i++, p++) {
174 if (strstr(kvpair->second.c_str(), *p) != NULL)
181 pmc_log_event(int fd, struct pmclog_ev *ev, bool json)
188 ret = event_to_json(ev);
195 if (write(fd, buf, len) != (ssize_t)len)
196 errx(EX_OSERR, "ERROR: failed output write");
200 pmc_filter_handler(uint32_t *lwplist, int lwpcount, uint32_t *pidlist, int pidcount,
201 char *events, char *processes, char *threads, bool exclusive, bool json, int infd,
205 struct pmclog_parse_state *ps;
206 struct pmcid_ent *pe;
207 uint32_t eventlist[LIST_MAX];
208 char cpuid[PMC_CPUID_LEN];
209 char *proclist[LIST_MAX];
210 char *threadlist[LIST_MAX];
211 int i, pmccount, copies, eventcount;
212 int proccount, threadcount;
214 idmap pidmap, tidmap;
216 if ((ps = static_cast < struct pmclog_parse_state *>(pmclog_open(infd)))== NULL)
217 errx(EX_OSERR, "ERROR: Cannot allocate pmclog parse state: %s\n", strerror(errno));
219 threadcount = proccount = eventcount = pmccount = 0;
221 parse_names(processes, proclist, &proccount);
223 parse_names(threads, threadlist, &threadcount);
224 while (pmclog_read(ps, &ev) == 0) {
225 if (ev.pl_type == PMCLOG_TYPE_INITIALIZE)
226 memcpy(cpuid, ev.pl_u.pl_i.pl_cpuid, PMC_CPUID_LEN);
227 if (ev.pl_type == PMCLOG_TYPE_PMCALLOCATE)
231 parse_events(events, eventlist, &eventcount, cpuid);
232 lseek(infd, 0, SEEK_SET);
234 if ((ps = static_cast < struct pmclog_parse_state *>(pmclog_open(infd)))== NULL)
235 errx(EX_OSERR, "ERROR: Cannot allocate pmclog parse state: %s\n", strerror(errno));
236 if ((pe = (struct pmcid_ent *) malloc(sizeof(*pe) * pmccount)) == NULL)
237 errx(EX_OSERR, "ERROR: failed to allocate pmcid map");
239 while (pmclog_read(ps, &ev) == 0 && i < pmccount) {
240 if (ev.pl_type == PMCLOG_TYPE_PMCALLOCATE) {
241 pe[i].pe_pmcid = ev.pl_u.pl_a.pl_pmcid;
242 pe[i].pe_idx = ev.pl_u.pl_a.pl_event;
246 lseek(infd, 0, SEEK_SET);
248 if ((ps = static_cast < struct pmclog_parse_state *>(pmclog_open(infd)))== NULL)
249 errx(EX_OSERR, "ERROR: Cannot allocate pmclog parse state: %s\n", strerror(errno));
251 while (pmclog_read(ps, &ev) == 0) {
252 if (ev.pl_type == PMCLOG_TYPE_THR_CREATE)
253 tidmap[ev.pl_u.pl_tc.pl_tid] = ev.pl_u.pl_tc.pl_tdname;
254 if (ev.pl_type == PMCLOG_TYPE_PROC_CREATE)
255 pidmap[ev.pl_u.pl_pc.pl_pid] = ev.pl_u.pl_pc.pl_pcomm;
256 if (ev.pl_type != PMCLOG_TYPE_CALLCHAIN) {
257 pmc_log_event(outfd, &ev, json);
261 for (i = 0; i < pidcount; i++)
262 if (pidlist[i] == ev.pl_u.pl_cc.pl_pid)
264 if ((i == pidcount) == exclusive)
268 for (i = 0; i < lwpcount; i++)
269 if (lwplist[i] == ev.pl_u.pl_cc.pl_tid)
271 if ((i == lwpcount) == exclusive)
275 for (i = 0; i < pmccount; i++) {
276 if (pe[i].pe_pmcid == ev.pl_u.pl_cc.pl_pmcid)
280 errx(EX_USAGE, "ERROR: unallocated pmcid: %d\n",
281 ev.pl_u.pl_cc.pl_pmcid);
284 for (i = 0; i < eventcount; i++) {
285 if (idx == eventlist[i])
288 if ((i == eventcount) == exclusive)
292 pmc_find_name(pidmap, ev.pl_u.pl_cc.pl_pid, proclist, proccount) == exclusive)
295 pmc_find_name(tidmap, ev.pl_u.pl_cc.pl_tid, threadlist, threadcount) == exclusive)
297 pmc_log_event(outfd, &ev, json);
302 cmd_pmc_filter(int argc, char **argv)
304 char *lwps, *pids, *events, *processes, *threads;
305 uint32_t lwplist[LIST_MAX];
306 uint32_t pidlist[LIST_MAX];
307 int option, lwpcount, pidcount;
308 int prelogfd, postlogfd;
309 bool exclusive, json;
311 threads = processes = lwps = pids = events = NULL;
312 lwpcount = pidcount = 0;
313 json = exclusive = false;
314 while ((option = getopt_long(argc, argv, "e:jp:t:xP:T:", longopts, NULL)) != -1) {
317 events = strdup(optarg);
323 pids = strdup(optarg);
326 processes = strdup(optarg);
329 lwps = strdup(optarg);
332 threads = strdup(optarg);
335 exclusive = !exclusive;
348 parse_intlist(lwps, lwplist, &lwpcount, atoi);
350 parse_intlist(pids, pidlist, &pidcount, atoi);
351 if ((prelogfd = open(argv[0], O_RDONLY,
352 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
353 errx(EX_OSERR, "ERROR: Cannot open \"%s\" for reading: %s.", argv[0],
355 if ((postlogfd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC,
356 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
357 errx(EX_OSERR, "ERROR: Cannot open \"%s\" for writing: %s.", argv[1],
360 pmc_filter_handler(lwplist, lwpcount, pidlist, pidcount, events,
361 processes, threads, exclusive, json, prelogfd, postlogfd);