xref: /netbsd-src/usr.sbin/tprof/tprof_analyze.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1 /*	$NetBSD: tprof_analyze.c,v 1.8 2022/12/01 00:43:27 ryo Exp $	*/
2 
3 /*
4  * Copyright (c) 2010,2011,2012 YAMAMOTO Takashi,
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __RCSID("$NetBSD: tprof_analyze.c,v 1.8 2022/12/01 00:43:27 ryo Exp $");
32 #endif /* not lint */
33 
34 #include <assert.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <gelf.h>
39 #include <inttypes.h>
40 #include <libelf.h>
41 #include <stdbool.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <util.h>
47 #include <dev/tprof/tprof_ioctl.h>
48 #include "tprof.h"
49 #include "ksyms.h"
50 
51 #include <sys/rbtree.h>
52 
53 static bool filter_by_pid;
54 static pid_t target_pid;
55 static bool per_symbol;
56 
57 struct addr {
58 	struct rb_node node;
59 	uint64_t addr;		/* address */
60 	uint32_t pid;		/* process id */
61 	uint32_t lwpid;		/* lwp id */
62 	uint32_t cpuid;		/* cpu id */
63 	bool in_kernel;		/* if addr is in the kernel address space */
64 	unsigned int nsamples;	/* number of samples taken for the address */
65 	unsigned int ncount[TPROF_MAXCOUNTERS];	/* count per event */
66 };
67 
68 static rb_tree_t addrtree;
69 
70 static signed int
71 addrtree_compare_key(void *ctx, const void *n1, const void *keyp)
72 {
73 	const struct addr *a1 = n1;
74 	const struct addr *a2 = (const struct addr *)keyp;
75 
76 	if (a1->addr > a2->addr) {
77 		return 1;
78 	} else if (a1->addr < a2->addr) {
79 		return -1;
80 	}
81 	if (a1->pid > a2->pid) {
82 		return -1;
83 	} else if (a1->pid < a2->pid) {
84 		return 1;
85 	}
86 	if (a1->lwpid > a2->lwpid) {
87 		return -1;
88 	} else if (a1->lwpid < a2->lwpid) {
89 		return 1;
90 	}
91 	if (a1->cpuid > a2->cpuid) {
92 		return -1;
93 	} else if (a1->cpuid < a2->cpuid) {
94 		return 1;
95 	}
96 	if (a1->in_kernel > a2->in_kernel) {
97 		return -1;
98 	} else if (a1->in_kernel < a2->in_kernel) {
99 		return 1;
100 	}
101 	return 0;
102 }
103 
104 static signed int
105 addrtree_compare_nodes(void *ctx, const void *n1, const void *n2)
106 {
107 	const struct addr *a2 = n2;
108 
109 	return addrtree_compare_key(ctx, n1, a2);
110 }
111 
112 static const rb_tree_ops_t addrtree_ops = {
113 	.rbto_compare_nodes = addrtree_compare_nodes,
114 	.rbto_compare_key = addrtree_compare_key,
115 };
116 
117 static int
118 compare_nsamples(const void *p1, const void *p2)
119 {
120 	const struct addr *a1 = *(const struct addr * const *)p1;
121 	const struct addr *a2 = *(const struct addr * const *)p2;
122 
123 	if (a1->nsamples > a2->nsamples) {
124 		return -1;
125 	} else if (a1->nsamples < a2->nsamples) {
126 		return 1;
127 	}
128 	return 0;
129 }
130 
131 void
132 tprof_analyze(int argc, char **argv)
133 {
134 	struct addr *a;
135 	struct addr **l;
136 	struct addr **p;
137 	size_t naddrs, nsamples, i;
138 	float perc;
139 	int ch;
140 	u_int c, maxevent = 0;
141 	bool distinguish_processes = true;
142 	bool distinguish_cpus = true;
143 	bool distinguish_lwps = true;
144 	bool kernel_only = false;
145 	extern char *optarg;
146 	extern int optind;
147 	FILE *f;
148 
149 	while ((ch = getopt(argc, argv, "CkLPp:s")) != -1) {
150 		uintmax_t val;
151 		char *ep;
152 
153 		switch (ch) {
154 		case 'C':	/* don't distinguish cpus */
155 			distinguish_cpus = false;
156 			break;
157 		case 'k':	/* kernel only */
158 			kernel_only = true;
159 			break;
160 		case 'L':	/* don't distinguish lwps */
161 			distinguish_lwps = false;
162 			break;
163 		case 'p':	/* only for the process for the given pid */
164 			errno = 0;
165 			val = strtoumax(optarg, &ep, 10);
166 			if (optarg[0] == 0 || *ep != 0 ||
167 			    val > INT32_MAX) {
168 				errx(EXIT_FAILURE, "invalid p option");
169 			}
170 			target_pid = val;
171 			filter_by_pid = true;
172 			break;
173 		case 'P':	/* don't distinguish processes */
174 			distinguish_processes = false;
175 			break;
176 		case 's':	/* per symbol */
177 			per_symbol = true;
178 			break;
179 		default:
180 			exit(EXIT_FAILURE);
181 		}
182 	}
183 	argc -= optind;
184 	argv += optind;
185 
186 	if (argc == 0) {
187 		errx(EXIT_FAILURE, "missing file name");
188 	}
189 
190 	f = fopen(argv[0], "rb");
191 	if (f == NULL) {
192 		errx(EXIT_FAILURE, "fopen");
193 	}
194 
195 	ksymload(NULL);
196 	rb_tree_init(&addrtree, &addrtree_ops);
197 
198 	/*
199 	 * read and count samples.
200 	 */
201 
202 	naddrs = 0;
203 	nsamples = 0;
204 	while (/*CONSTCOND*/true) {
205 		struct addr *o;
206 		tprof_sample_t sample;
207 		size_t n = fread(&sample, sizeof(sample), 1, f);
208 		bool in_kernel;
209 
210 		if (n == 0) {
211 			if (feof(f)) {
212 				break;
213 			}
214 			if (ferror(f)) {
215 				err(EXIT_FAILURE, "fread");
216 			}
217 		}
218 		if (filter_by_pid && (pid_t)sample.s_pid != target_pid) {
219 			continue;
220 		}
221 		in_kernel = (sample.s_flags & TPROF_SAMPLE_INKERNEL) != 0;
222 		if (kernel_only && !in_kernel) {
223 			continue;
224 		}
225 		a = emalloc(sizeof(*a));
226 		memset(a, 0, sizeof(*a));
227 		a->addr = (uint64_t)sample.s_pc;
228 		if (distinguish_processes) {
229 			a->pid = sample.s_pid;
230 		} else {
231 			a->pid = 0;
232 		}
233 		if (distinguish_lwps) {
234 			a->lwpid = sample.s_lwpid;
235 		} else {
236 			a->lwpid = 0;
237 		}
238 		if (distinguish_cpus) {
239 			a->cpuid = sample.s_cpuid;
240 		} else {
241 			a->cpuid = 0;
242 		}
243 		a->in_kernel = in_kernel;
244 		if (per_symbol) {
245 			const char *name;
246 			uint64_t offset;
247 
248 			name = ksymlookup(a->addr, &offset, NULL);
249 			if (name != NULL) {
250 				a->addr -= offset;
251 			}
252 		}
253 		c = __SHIFTOUT(sample.s_flags, TPROF_SAMPLE_COUNTER_MASK);
254 		assert(c < TPROF_MAXCOUNTERS);
255 		if (maxevent < c)
256 			maxevent = c;
257 
258 		a->nsamples = 1;
259 		a->ncount[c] = 1;
260 		o = rb_tree_insert_node(&addrtree, a);
261 		if (o != a) {
262 			assert(a->addr == o->addr);
263 			assert(a->pid == o->pid);
264 			assert(a->lwpid == o->lwpid);
265 			assert(a->cpuid == o->cpuid);
266 			assert(a->in_kernel == o->in_kernel);
267 			free(a);
268 
269 			o->nsamples++;
270 			o->ncount[c]++;
271 		} else {
272 			naddrs++;
273 		}
274 		nsamples++;
275 	}
276 
277 	/*
278 	 * sort samples by addresses.
279 	 */
280 
281 	l = emalloc(naddrs * sizeof(*l));
282 	p = l;
283 	RB_TREE_FOREACH(a, &addrtree) {
284 		*p++ = a;
285 	}
286 	assert(l + naddrs == p);
287 	qsort(l, naddrs, sizeof(*l), compare_nsamples);
288 
289 	/*
290 	 * print addresses and number of samples, preferably with
291 	 * resolved symbol names.
292 	 */
293 	printf("File: %s\n", argv[0]);
294 	printf("Number of samples: %zu\n\n", nsamples);
295 
296 	printf("percentage   nsamples ");
297 	for (c = 0; c <= maxevent; c++)
298 		printf("event#%02u ", c);
299 	printf("pid    lwp    cpu  k address          symbol\n");
300 
301 	printf("------------ -------- ");
302 	for (c = 0; c <= maxevent; c++)
303 		printf("-------- ");
304 
305 	printf("------ ------ ---- - ---------------- ------\n");
306 	for (i = 0; i < naddrs; i++) {
307 		const char *name;
308 		char buf[100];
309 		uint64_t offset;
310 
311 		a = l[i];
312 		if (a->in_kernel) {
313 			name = ksymlookup(a->addr, &offset, NULL);
314 		} else {
315 			name = NULL;
316 		}
317 		if (name == NULL) {
318 			(void)snprintf(buf, sizeof(buf), "<%016" PRIx64 ">",
319 			    a->addr);
320 			name = buf;
321 		} else if (offset != 0) {
322 			(void)snprintf(buf, sizeof(buf), "%s+0x%" PRIx64, name,
323 			    offset);
324 			name = buf;
325 		}
326 
327 		perc = ((float)a->nsamples / (float)nsamples) * 100.0;
328 
329 		printf("%11f%% %8u", perc, a->nsamples);
330 
331 		for (c = 0; c <= maxevent; c++)
332 			printf(" %8u", a->ncount[c]);
333 
334 		printf(" %6" PRIu32 " %6" PRIu32 " %4" PRIu32 " %u %016"
335 		    PRIx64" %s",
336 		    a->pid, a->lwpid, a->cpuid, a->in_kernel, a->addr, name);
337 
338 
339 		printf("\n");
340 	}
341 
342 	fclose(f);
343 }
344