xref: /freebsd/usr.sbin/pmc/cmd_pmc_filter.cc (revision 190cef3d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018, Matthew Macy
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
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>
37 #include <sys/stat.h>
38 #include <sys/sysctl.h>
39 #include <sys/time.h>
40 #include <sys/ttycom.h>
41 #include <sys/user.h>
42 #include <sys/wait.h>
43 
44 #include <assert.h>
45 #include <curses.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <getopt.h>
50 #include <kvm.h>
51 #include <libgen.h>
52 #include <limits.h>
53 #include <locale.h>
54 #include <math.h>
55 #include <pmc.h>
56 #include <pmclog.h>
57 #include <regex.h>
58 #include <signal.h>
59 #include <stdarg.h>
60 #include <stdint.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <stddef.h>
64 #include <string.h>
65 #include <sysexits.h>
66 #include <unistd.h>
67 
68 #include <libpmcstat.h>
69 #include "cmd_pmc.h"
70 
71 #include <string>
72 #include <unordered_map>
73 
74 #include <pmcformat.h>
75 
76 using	namespace std;
77 using	std::unordered_map;
78 typedef unordered_map < int ,string > idmap;
79 typedef pair < int ,string > identry;
80 
81 #define LIST_MAX 64
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'},
88 	{NULL, 0, NULL, 0}
89 };
90 
91 static void __dead2
92 usage(void)
93 {
94 	errx(EX_USAGE,
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"
102 	    );
103 }
104 
105 
106 static void
107 parse_intlist(char *strlist, uint32_t *intlist, int *pcount, int (*fn) (const char *))
108 {
109 	char *token;
110 	int count, tokenval;
111 
112 	count = 0;
113 	while ((token = strsep(&strlist, ",")) != NULL &&
114 	    count < LIST_MAX) {
115 		if ((tokenval = fn(token)) < 0)
116 			errx(EX_USAGE, "ERROR: %s not usable value", token);
117 		intlist[count++] = tokenval;
118 	}
119 	*pcount = count;
120 }
121 
122 static void
123 parse_events(char *strlist, uint32_t intlist[LIST_MAX], int *pcount, char *cpuid)
124 {
125 	char *token;
126 	int count, tokenval;
127 
128 	count = 0;
129 	while ((token = strsep(&strlist, ",")) != NULL &&
130 	    count < LIST_MAX) {
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;
134 	}
135 	*pcount = count;
136 }
137 
138 static void
139 parse_names(char *strlist, char *namelist[LIST_MAX], int *pcount)
140 {
141 	char *token;
142 	int count;
143 
144 	count = 0;
145 	while ((token = strsep(&strlist, ",")) != NULL &&
146 	    count < LIST_MAX) {
147 		namelist[count++] = token;
148 	}
149 	*pcount = count;
150 }
151 
152 
153 struct pmcid_ent {
154 	uint32_t pe_pmcid;
155 	uint32_t pe_idx;
156 };
157 #define	_PMCLOG_TO_HEADER(T,L)						\
158 	((PMCLOG_HEADER_MAGIC << 24) |					\
159 	 (PMCLOG_TYPE_ ## T << 16)   |					\
160 	 ((L) & 0xFFFF))
161 
162 static bool
163 pmc_find_name(idmap & map, uint32_t id, char *list[LIST_MAX], int count)
164 {
165 	int i;
166 
167 	auto kvpair = map.find(id);
168 	if (kvpair == map.end()) {
169 		printf("unknown id: %d\n", id);
170 		return (false);
171 	}
172 	auto p = list;
173 	for (i = 0; i < count; i++, p++) {
174 		if (strstr(kvpair->second.c_str(), *p) != NULL)
175 			return (true);
176 	}
177 	return (false);
178 }
179 
180 static void
181 pmc_log_event(int fd, struct pmclog_ev *ev, bool json)
182 {
183 	int len;
184 	const void *buf;
185 
186 	if (json) {
187 		string ret = event_to_json(ev);
188 		buf = ret.c_str();
189 		len = ret.size();
190 	} else {
191 		len = ev->pl_len;
192 		buf = ev->pl_data;
193 	}
194 	if (write(fd, buf, len) != (ssize_t)len)
195 		errx(EX_OSERR, "ERROR: failed output write");
196 }
197 
198 static void
199 pmc_filter_handler(uint32_t *lwplist, int lwpcount, uint32_t *pidlist, int pidcount,
200     char *events, char *processes, char *threads, bool exclusive, bool json, int infd,
201     int outfd)
202 {
203 	struct pmclog_ev ev;
204 	struct pmclog_parse_state *ps;
205 	struct pmcid_ent *pe;
206 	uint32_t eventlist[LIST_MAX];
207 	char cpuid[PMC_CPUID_LEN];
208 	char *proclist[LIST_MAX];
209 	char *threadlist[LIST_MAX];
210 	int i, pmccount, copies, eventcount;
211 	int proccount, threadcount;
212 	uint32_t idx;
213 	idmap pidmap, tidmap;
214 
215 	if ((ps = static_cast < struct pmclog_parse_state *>(pmclog_open(infd)))== NULL)
216 		errx(EX_OSERR, "ERROR: Cannot allocate pmclog parse state: %s\n", strerror(errno));
217 
218 	threadcount = proccount = eventcount = pmccount = 0;
219 	if (processes)
220 		parse_names(processes, proclist, &proccount);
221 	if (threads)
222 		parse_names(threads, threadlist, &threadcount);
223 	while (pmclog_read(ps, &ev) == 0) {
224 		if (ev.pl_type == PMCLOG_TYPE_INITIALIZE)
225 			memcpy(cpuid, ev.pl_u.pl_i.pl_cpuid, PMC_CPUID_LEN);
226 		if (ev.pl_type == PMCLOG_TYPE_PMCALLOCATE)
227 			pmccount++;
228 	}
229 	if (events)
230 		parse_events(events, eventlist, &eventcount, cpuid);
231 	lseek(infd, 0, SEEK_SET);
232 	pmclog_close(ps);
233 	if ((ps = static_cast < struct pmclog_parse_state *>(pmclog_open(infd)))== NULL)
234 		errx(EX_OSERR, "ERROR: Cannot allocate pmclog parse state: %s\n", strerror(errno));
235 	if ((pe = (struct pmcid_ent *) malloc(sizeof(*pe) * pmccount)) == NULL)
236 		errx(EX_OSERR, "ERROR: failed to allocate pmcid map");
237 	i = 0;
238 	while (pmclog_read(ps, &ev) == 0 && i < pmccount) {
239 		if (ev.pl_type == PMCLOG_TYPE_PMCALLOCATE) {
240 			pe[i].pe_pmcid = ev.pl_u.pl_a.pl_pmcid;
241 			pe[i].pe_idx = ev.pl_u.pl_a.pl_event;
242 			i++;
243 		}
244 	}
245 	lseek(infd, 0, SEEK_SET);
246 	pmclog_close(ps);
247 	if ((ps = static_cast < struct pmclog_parse_state *>(pmclog_open(infd)))== NULL)
248 		errx(EX_OSERR, "ERROR: Cannot allocate pmclog parse state: %s\n", strerror(errno));
249 	copies = 0;
250 	while (pmclog_read(ps, &ev) == 0) {
251 		if (ev.pl_type == PMCLOG_TYPE_THR_CREATE)
252 			tidmap[ev.pl_u.pl_tc.pl_tid] = ev.pl_u.pl_tc.pl_tdname;
253 		if (ev.pl_type == PMCLOG_TYPE_PROC_CREATE)
254 			pidmap[ev.pl_u.pl_pc.pl_pid] = ev.pl_u.pl_pc.pl_pcomm;
255 		if (ev.pl_type != PMCLOG_TYPE_CALLCHAIN) {
256 			pmc_log_event(outfd, &ev, json);
257 			continue;
258 		}
259 		if (pidcount) {
260 			for (i = 0; i < pidcount; i++)
261 				if (pidlist[i] == ev.pl_u.pl_cc.pl_pid)
262 					break;
263 			if ((i == pidcount) == exclusive)
264 				continue;
265 		}
266 		if (lwpcount) {
267 			for (i = 0; i < lwpcount; i++)
268 				if (lwplist[i] == ev.pl_u.pl_cc.pl_tid)
269 					break;
270 			if ((i == lwpcount) == exclusive)
271 				continue;
272 		}
273 		if (eventcount) {
274 			for (i = 0; i < pmccount; i++) {
275 				if (pe[i].pe_pmcid == ev.pl_u.pl_cc.pl_pmcid)
276 					break;
277 			}
278 			if (i == pmccount)
279 				errx(EX_USAGE, "ERROR: unallocated pmcid: %d\n",
280 				    ev.pl_u.pl_cc.pl_pmcid);
281 
282 			idx = pe[i].pe_idx;
283 			for (i = 0; i < eventcount; i++) {
284 				if (idx == eventlist[i])
285 					break;
286 			}
287 			if ((i == eventcount) == exclusive)
288 				continue;
289 		}
290 		if (proccount &&
291 		    pmc_find_name(pidmap, ev.pl_u.pl_cc.pl_pid, proclist, proccount) == exclusive)
292 			continue;
293 		if (threadcount &&
294 		    pmc_find_name(tidmap, ev.pl_u.pl_cc.pl_tid, threadlist, threadcount) == exclusive)
295 			continue;
296 		pmc_log_event(outfd, &ev, json);
297 	}
298 }
299 
300 int
301 cmd_pmc_filter(int argc, char **argv)
302 {
303 	char *lwps, *pids, *events, *processes, *threads;
304 	uint32_t lwplist[LIST_MAX];
305 	uint32_t pidlist[LIST_MAX];
306 	int option, lwpcount, pidcount;
307 	int prelogfd, postlogfd;
308 	bool exclusive, json;
309 
310 	threads = processes = lwps = pids = events = NULL;
311 	lwpcount = pidcount = 0;
312 	json = exclusive = false;
313 	while ((option = getopt_long(argc, argv, "e:jp:t:xP:T:", longopts, NULL)) != -1) {
314 		switch (option) {
315 		case 'e':
316 			events = strdup(optarg);
317 			break;
318 		case 'j':
319 			json = true;
320 			break;
321 		case 'p':
322 			pids = strdup(optarg);
323 			break;
324 		case 'P':
325 			processes = strdup(optarg);
326 			break;
327 		case 't':
328 			lwps = strdup(optarg);
329 			break;
330 		case 'T':
331 			threads = strdup(optarg);
332 			break;
333 		case 'x':
334 			exclusive = !exclusive;
335 			break;
336 		case '?':
337 		default:
338 			usage();
339 		}
340 	}
341 	argc -= optind;
342 	argv += optind;
343 	if (argc != 2)
344 		usage();
345 
346 	if (lwps)
347 		parse_intlist(lwps, lwplist, &lwpcount, atoi);
348 	if (pids)
349 		parse_intlist(pids, pidlist, &pidcount, atoi);
350 	if ((prelogfd = open(argv[0], O_RDONLY,
351 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
352 		errx(EX_OSERR, "ERROR: Cannot open \"%s\" for reading: %s.", argv[0],
353 		    strerror(errno));
354 	if ((postlogfd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC,
355 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
356 		errx(EX_OSERR, "ERROR: Cannot open \"%s\" for writing: %s.", argv[1],
357 		    strerror(errno));
358 
359 	pmc_filter_handler(lwplist, lwpcount, pidlist, pidcount, events,
360 	    processes, threads, exclusive, json, prelogfd, postlogfd);
361 	return (0);
362 }
363