Home | History | Annotate | Line # | Download | only in tprof
      1 /*	$NetBSD: tprof_analyze.c,v 1.9 2024/11/03 10:43:27 rillig 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.9 2024/11/03 10:43:27 rillig 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 	FILE *f;
    146 
    147 	while ((ch = getopt(argc, argv, "CkLPp:s")) != -1) {
    148 		uintmax_t val;
    149 		char *ep;
    150 
    151 		switch (ch) {
    152 		case 'C':	/* don't distinguish cpus */
    153 			distinguish_cpus = false;
    154 			break;
    155 		case 'k':	/* kernel only */
    156 			kernel_only = true;
    157 			break;
    158 		case 'L':	/* don't distinguish lwps */
    159 			distinguish_lwps = false;
    160 			break;
    161 		case 'p':	/* only for the process for the given pid */
    162 			errno = 0;
    163 			val = strtoumax(optarg, &ep, 10);
    164 			if (optarg[0] == 0 || *ep != 0 ||
    165 			    val > INT32_MAX) {
    166 				errx(EXIT_FAILURE, "invalid p option");
    167 			}
    168 			target_pid = val;
    169 			filter_by_pid = true;
    170 			break;
    171 		case 'P':	/* don't distinguish processes */
    172 			distinguish_processes = false;
    173 			break;
    174 		case 's':	/* per symbol */
    175 			per_symbol = true;
    176 			break;
    177 		default:
    178 			exit(EXIT_FAILURE);
    179 		}
    180 	}
    181 	argc -= optind;
    182 	argv += optind;
    183 
    184 	if (argc == 0) {
    185 		errx(EXIT_FAILURE, "missing file name");
    186 	}
    187 
    188 	f = fopen(argv[0], "rb");
    189 	if (f == NULL) {
    190 		errx(EXIT_FAILURE, "fopen");
    191 	}
    192 
    193 	ksymload(NULL);
    194 	rb_tree_init(&addrtree, &addrtree_ops);
    195 
    196 	/*
    197 	 * read and count samples.
    198 	 */
    199 
    200 	naddrs = 0;
    201 	nsamples = 0;
    202 	while (/*CONSTCOND*/true) {
    203 		struct addr *o;
    204 		tprof_sample_t sample;
    205 		size_t n = fread(&sample, sizeof(sample), 1, f);
    206 		bool in_kernel;
    207 
    208 		if (n == 0) {
    209 			if (feof(f)) {
    210 				break;
    211 			}
    212 			if (ferror(f)) {
    213 				err(EXIT_FAILURE, "fread");
    214 			}
    215 		}
    216 		if (filter_by_pid && (pid_t)sample.s_pid != target_pid) {
    217 			continue;
    218 		}
    219 		in_kernel = (sample.s_flags & TPROF_SAMPLE_INKERNEL) != 0;
    220 		if (kernel_only && !in_kernel) {
    221 			continue;
    222 		}
    223 		a = emalloc(sizeof(*a));
    224 		memset(a, 0, sizeof(*a));
    225 		a->addr = (uint64_t)sample.s_pc;
    226 		if (distinguish_processes) {
    227 			a->pid = sample.s_pid;
    228 		} else {
    229 			a->pid = 0;
    230 		}
    231 		if (distinguish_lwps) {
    232 			a->lwpid = sample.s_lwpid;
    233 		} else {
    234 			a->lwpid = 0;
    235 		}
    236 		if (distinguish_cpus) {
    237 			a->cpuid = sample.s_cpuid;
    238 		} else {
    239 			a->cpuid = 0;
    240 		}
    241 		a->in_kernel = in_kernel;
    242 		if (per_symbol) {
    243 			const char *name;
    244 			uint64_t offset;
    245 
    246 			name = ksymlookup(a->addr, &offset, NULL);
    247 			if (name != NULL) {
    248 				a->addr -= offset;
    249 			}
    250 		}
    251 		c = __SHIFTOUT(sample.s_flags, TPROF_SAMPLE_COUNTER_MASK);
    252 		assert(c < TPROF_MAXCOUNTERS);
    253 		if (maxevent < c)
    254 			maxevent = c;
    255 
    256 		a->nsamples = 1;
    257 		a->ncount[c] = 1;
    258 		o = rb_tree_insert_node(&addrtree, a);
    259 		if (o != a) {
    260 			assert(a->addr == o->addr);
    261 			assert(a->pid == o->pid);
    262 			assert(a->lwpid == o->lwpid);
    263 			assert(a->cpuid == o->cpuid);
    264 			assert(a->in_kernel == o->in_kernel);
    265 			free(a);
    266 
    267 			o->nsamples++;
    268 			o->ncount[c]++;
    269 		} else {
    270 			naddrs++;
    271 		}
    272 		nsamples++;
    273 	}
    274 
    275 	/*
    276 	 * sort samples by addresses.
    277 	 */
    278 
    279 	l = emalloc(naddrs * sizeof(*l));
    280 	p = l;
    281 	RB_TREE_FOREACH(a, &addrtree) {
    282 		*p++ = a;
    283 	}
    284 	assert(l + naddrs == p);
    285 	qsort(l, naddrs, sizeof(*l), compare_nsamples);
    286 
    287 	/*
    288 	 * print addresses and number of samples, preferably with
    289 	 * resolved symbol names.
    290 	 */
    291 	printf("File: %s\n", argv[0]);
    292 	printf("Number of samples: %zu\n\n", nsamples);
    293 
    294 	printf("percentage   nsamples ");
    295 	for (c = 0; c <= maxevent; c++)
    296 		printf("event#%02u ", c);
    297 	printf("pid    lwp    cpu  k address          symbol\n");
    298 
    299 	printf("------------ -------- ");
    300 	for (c = 0; c <= maxevent; c++)
    301 		printf("-------- ");
    302 
    303 	printf("------ ------ ---- - ---------------- ------\n");
    304 	for (i = 0; i < naddrs; i++) {
    305 		const char *name;
    306 		char buf[100];
    307 		uint64_t offset;
    308 
    309 		a = l[i];
    310 		if (a->in_kernel) {
    311 			name = ksymlookup(a->addr, &offset, NULL);
    312 		} else {
    313 			name = NULL;
    314 		}
    315 		if (name == NULL) {
    316 			(void)snprintf(buf, sizeof(buf), "<%016" PRIx64 ">",
    317 			    a->addr);
    318 			name = buf;
    319 		} else if (offset != 0) {
    320 			(void)snprintf(buf, sizeof(buf), "%s+0x%" PRIx64, name,
    321 			    offset);
    322 			name = buf;
    323 		}
    324 
    325 		perc = ((float)a->nsamples / (float)nsamples) * 100.0;
    326 
    327 		printf("%11f%% %8u", perc, a->nsamples);
    328 
    329 		for (c = 0; c <= maxevent; c++)
    330 			printf(" %8u", a->ncount[c]);
    331 
    332 		printf(" %6" PRIu32 " %6" PRIu32 " %4" PRIu32 " %u %016"
    333 		    PRIx64" %s",
    334 		    a->pid, a->lwpid, a->cpuid, a->in_kernel, a->addr, name);
    335 
    336 
    337 		printf("\n");
    338 	}
    339 
    340 	fclose(f);
    341 }
    342