tprof_top.c revision 1.9 1 1.9 msaitoh /* $NetBSD: tprof_top.c,v 1.9 2023/04/17 08:37:24 msaitoh Exp $ */
2 1.1 ryo
3 1.1 ryo /*-
4 1.1 ryo * Copyright (c) 2022 Ryo Shimizu <ryo (at) nerv.org>
5 1.1 ryo * All rights reserved.
6 1.1 ryo *
7 1.1 ryo * Redistribution and use in source and binary forms, with or without
8 1.1 ryo * modification, are permitted provided that the following conditions
9 1.1 ryo * are met:
10 1.1 ryo * 1. Redistributions of source code must retain the above copyright
11 1.1 ryo * notice, this list of conditions and the following disclaimer.
12 1.1 ryo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 ryo * notice, this list of conditions and the following disclaimer in the
14 1.1 ryo * documentation and/or other materials provided with the distribution.
15 1.1 ryo *
16 1.1 ryo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 1.1 ryo * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 1.1 ryo * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 ryo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 1.1 ryo * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 1.1 ryo * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 1.1 ryo * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 ryo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 1.1 ryo * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 1.1 ryo * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 ryo * POSSIBILITY OF SUCH DAMAGE.
27 1.1 ryo */
28 1.1 ryo
29 1.1 ryo #include <sys/cdefs.h>
30 1.1 ryo #ifndef lint
31 1.9 msaitoh __RCSID("$NetBSD: tprof_top.c,v 1.9 2023/04/17 08:37:24 msaitoh Exp $");
32 1.1 ryo #endif /* not lint */
33 1.1 ryo
34 1.1 ryo #include <sys/param.h>
35 1.1 ryo #include <sys/types.h>
36 1.4 ryo #include <sys/ioctl.h>
37 1.1 ryo #include <sys/rbtree.h>
38 1.4 ryo #include <sys/select.h>
39 1.1 ryo #include <sys/time.h>
40 1.1 ryo
41 1.4 ryo #include <assert.h>
42 1.1 ryo #include <err.h>
43 1.1 ryo #include <errno.h>
44 1.1 ryo #include <fcntl.h>
45 1.1 ryo #include <inttypes.h>
46 1.1 ryo #include <math.h>
47 1.1 ryo #include <signal.h>
48 1.1 ryo #include <stdio.h>
49 1.1 ryo #include <stdlib.h>
50 1.1 ryo #include <string.h>
51 1.4 ryo #include <term.h>
52 1.4 ryo #include <termios.h>
53 1.1 ryo #include <unistd.h>
54 1.1 ryo #include <util.h>
55 1.1 ryo
56 1.1 ryo #include <dev/tprof/tprof_ioctl.h>
57 1.1 ryo #include "tprof.h"
58 1.1 ryo #include "ksyms.h"
59 1.1 ryo
60 1.3 ryo #define SAMPLE_MODE_ACCUMULATIVE 0
61 1.3 ryo #define SAMPLE_MODE_INSTANTANEOUS 1
62 1.3 ryo #define SAMPLE_MODE_NUM 2
63 1.3 ryo
64 1.4 ryo #define LINESTR "-------------------------------------------------------------"
65 1.4 ryo #define SYMBOL_LEN 32 /* symbol and event name */
66 1.4 ryo
67 1.3 ryo struct sample_elm {
68 1.3 ryo struct rb_node node;
69 1.3 ryo uint64_t addr;
70 1.3 ryo const char *name;
71 1.3 ryo uint32_t flags;
72 1.3 ryo #define SAMPLE_ELM_FLAGS_USER 0x00000001
73 1.3 ryo uint32_t num[SAMPLE_MODE_NUM];
74 1.3 ryo uint32_t num_cpu[]; /* [SAMPLE_MODE_NUM][ncpu] */
75 1.3 ryo #define SAMPLE_ELM_NUM_CPU(e, k) \
76 1.3 ryo ((e)->num_cpu + (k) * ncpu)
77 1.3 ryo };
78 1.3 ryo
79 1.3 ryo struct ptrarray {
80 1.3 ryo void **pa_ptrs;
81 1.3 ryo size_t pa_allocnum;
82 1.3 ryo size_t pa_inuse;
83 1.3 ryo };
84 1.3 ryo
85 1.3 ryo static int opt_mode = SAMPLE_MODE_INSTANTANEOUS;
86 1.3 ryo static int opt_userland = 0;
87 1.3 ryo static int opt_showcounter = 0;
88 1.3 ryo
89 1.3 ryo /* for display */
90 1.4 ryo static char *term;
91 1.3 ryo static struct winsize win;
92 1.3 ryo static int nontty;
93 1.4 ryo static struct termios termios_save;
94 1.4 ryo static bool termios_saved;
95 1.3 ryo static long top_interval = 1;
96 1.4 ryo static bool do_redraw;
97 1.4 ryo static u_int nshow;
98 1.3 ryo
99 1.3 ryo /* for profiling and counting samples */
100 1.3 ryo static sig_atomic_t sigalrm;
101 1.1 ryo static struct sym **ksyms;
102 1.1 ryo static size_t nksyms;
103 1.1 ryo static u_int nevent;
104 1.1 ryo static const char *eventname[TPROF_MAXCOUNTERS];
105 1.3 ryo static size_t sizeof_sample_elm;
106 1.3 ryo static rb_tree_t rb_tree_sample;
107 1.3 ryo struct ptrarray sample_list[SAMPLE_MODE_NUM];
108 1.3 ryo static u_int sample_n_kern[SAMPLE_MODE_NUM];
109 1.3 ryo static u_int sample_n_user[SAMPLE_MODE_NUM];
110 1.4 ryo static u_int sample_event_width = 7;
111 1.4 ryo static u_int *sample_cpu_width; /* [ncpu] */
112 1.3 ryo static uint32_t *sample_n_kern_per_cpu[SAMPLE_MODE_NUM]; /* [ncpu] */
113 1.3 ryo static uint32_t *sample_n_user_per_cpu[SAMPLE_MODE_NUM]; /* [ncpu] */
114 1.3 ryo static uint64_t *sample_n_per_event[SAMPLE_MODE_NUM]; /* [nevent] */
115 1.3 ryo static uint64_t *sample_n_per_event_cpu[SAMPLE_MODE_NUM]; /* [ncpu] */
116 1.3 ryo
117 1.3 ryo /* raw event counter */
118 1.3 ryo static uint64_t *counters; /* counters[2][ncpu][nevent] */
119 1.3 ryo static u_int counters_i;
120 1.3 ryo
121 1.1 ryo static void
122 1.4 ryo reset_cursor_pos(void)
123 1.1 ryo {
124 1.4 ryo int i;
125 1.4 ryo char *p;
126 1.4 ryo
127 1.4 ryo if (nontty || term == NULL)
128 1.1 ryo return;
129 1.4 ryo
130 1.4 ryo printf("\r");
131 1.4 ryo
132 1.4 ryo /* cursor_up * n */
133 1.4 ryo if ((p = tigetstr("cuu")) != NULL) {
134 1.6 ryo putp(tparm(p, win.ws_row - 1, 0, 0, 0, 0, 0, 0, 0, 0));
135 1.4 ryo } else if ((p = tigetstr("cuu1")) != NULL) {
136 1.4 ryo for (i = win.ws_row - 1; i > 0; i--)
137 1.6 ryo putp(p);
138 1.4 ryo }
139 1.1 ryo }
140 1.1 ryo
141 1.1 ryo static void
142 1.4 ryo clr_to_eol(void)
143 1.1 ryo {
144 1.4 ryo char *p;
145 1.4 ryo
146 1.4 ryo if (nontty || term == NULL)
147 1.1 ryo return;
148 1.4 ryo
149 1.4 ryo if ((p = tigetstr("el")) != NULL)
150 1.6 ryo putp(p);
151 1.1 ryo }
152 1.1 ryo
153 1.4 ryo /* newline, and clearing to end of line if needed */
154 1.1 ryo static void
155 1.4 ryo lim_newline(int *lim)
156 1.1 ryo {
157 1.4 ryo if (*lim >= 1)
158 1.4 ryo clr_to_eol();
159 1.4 ryo
160 1.4 ryo printf("\n");
161 1.4 ryo *lim = win.ws_col;
162 1.1 ryo }
163 1.1 ryo
164 1.4 ryo static int
165 1.4 ryo lim_printf(int *lim, const char *fmt, ...)
166 1.1 ryo {
167 1.4 ryo va_list ap;
168 1.4 ryo size_t written;
169 1.4 ryo char *p;
170 1.4 ryo
171 1.4 ryo if (*lim <= 0)
172 1.4 ryo return 0;
173 1.4 ryo
174 1.8 christos p = malloc(*lim + 1);
175 1.8 christos if (p == NULL)
176 1.8 christos return -1;
177 1.4 ryo
178 1.4 ryo va_start(ap, fmt);
179 1.4 ryo vsnprintf(p, *lim + 1, fmt, ap);
180 1.4 ryo va_end(ap);
181 1.4 ryo
182 1.4 ryo written = strlen(p);
183 1.4 ryo if (written == 0) {
184 1.8 christos free(p);
185 1.4 ryo *lim = 0;
186 1.4 ryo return 0;
187 1.4 ryo }
188 1.4 ryo
189 1.4 ryo fwrite(p, written, 1, stdout);
190 1.4 ryo *lim -= written;
191 1.4 ryo
192 1.8 christos free(p);
193 1.4 ryo return written;
194 1.1 ryo }
195 1.1 ryo
196 1.1 ryo static void
197 1.1 ryo sigwinch_handler(int signo)
198 1.1 ryo {
199 1.4 ryo char *p;
200 1.4 ryo
201 1.4 ryo win.ws_col = tigetnum("lines");
202 1.4 ryo win.ws_row = tigetnum("cols");
203 1.4 ryo
204 1.1 ryo nontty = ioctl(STDOUT_FILENO, TIOCGWINSZ, &win);
205 1.4 ryo if (nontty != 0) {
206 1.4 ryo nontty = !isatty(STDOUT_FILENO);
207 1.4 ryo win.ws_col = 65535;
208 1.4 ryo win.ws_row = 65535;
209 1.4 ryo }
210 1.4 ryo
211 1.4 ryo if ((p = getenv("LINES")) != NULL)
212 1.4 ryo win.ws_row = strtoul(p, NULL, 0);
213 1.4 ryo if ((p = getenv("COLUMNS")) != NULL)
214 1.4 ryo win.ws_col = strtoul(p, NULL, 0);
215 1.4 ryo
216 1.4 ryo do_redraw = true;
217 1.4 ryo }
218 1.4 ryo
219 1.4 ryo static void
220 1.4 ryo tty_setup(void)
221 1.4 ryo {
222 1.4 ryo struct termios termios;
223 1.4 ryo
224 1.4 ryo term = getenv("TERM");
225 1.4 ryo if (term != NULL)
226 1.4 ryo setupterm(term, 0, NULL);
227 1.4 ryo
228 1.4 ryo sigwinch_handler(0);
229 1.4 ryo
230 1.4 ryo if (tcgetattr(STDOUT_FILENO, &termios_save) == 0) {
231 1.4 ryo termios_saved = true;
232 1.4 ryo
233 1.4 ryo /* stty cbreak */
234 1.4 ryo termios = termios_save;
235 1.4 ryo termios.c_iflag |= BRKINT|IXON|IMAXBEL;
236 1.4 ryo termios.c_oflag |= OPOST;
237 1.4 ryo termios.c_lflag |= ISIG|IEXTEN;
238 1.4 ryo termios.c_lflag &= ~(ICANON|ECHO);
239 1.4 ryo tcsetattr(STDOUT_FILENO, TCSADRAIN, &termios);
240 1.4 ryo }
241 1.4 ryo }
242 1.4 ryo
243 1.4 ryo static void
244 1.4 ryo tty_restore(void)
245 1.4 ryo {
246 1.4 ryo if (termios_saved) {
247 1.4 ryo tcsetattr(STDOUT_FILENO, TCSADRAIN, &termios_save);
248 1.4 ryo termios_saved = false;
249 1.4 ryo }
250 1.4 ryo }
251 1.4 ryo
252 1.4 ryo static void
253 1.4 ryo sigtstp_handler(int signo)
254 1.4 ryo {
255 1.4 ryo tty_restore();
256 1.4 ryo
257 1.4 ryo signal(SIGWINCH, SIG_DFL);
258 1.4 ryo signal(SIGINT, SIG_DFL);
259 1.4 ryo signal(SIGQUIT, SIG_DFL);
260 1.4 ryo signal(SIGTERM, SIG_DFL);
261 1.4 ryo signal(SIGTSTP, SIG_DFL);
262 1.4 ryo kill(0, SIGTSTP);
263 1.4 ryo nshow = 0;
264 1.1 ryo }
265 1.1 ryo
266 1.1 ryo static void
267 1.1 ryo sigalrm_handler(int signo)
268 1.1 ryo {
269 1.1 ryo sigalrm = 1;
270 1.1 ryo }
271 1.1 ryo
272 1.6 ryo __dead static void
273 1.4 ryo die(int signo)
274 1.4 ryo {
275 1.4 ryo tty_restore();
276 1.4 ryo printf("\n");
277 1.4 ryo
278 1.4 ryo exit(EXIT_SUCCESS);
279 1.4 ryo }
280 1.4 ryo
281 1.6 ryo __dead static void
282 1.4 ryo die_errc(int status, int code, const char *fmt, ...)
283 1.4 ryo {
284 1.4 ryo va_list ap;
285 1.4 ryo
286 1.4 ryo tty_restore();
287 1.4 ryo
288 1.4 ryo va_start(ap, fmt);
289 1.4 ryo if (code == 0)
290 1.4 ryo verrx(status, fmt, ap);
291 1.4 ryo else
292 1.4 ryo verrc(status, code, fmt, ap);
293 1.4 ryo va_end(ap);
294 1.4 ryo }
295 1.4 ryo
296 1.4 ryo static void
297 1.3 ryo ptrarray_push(struct ptrarray *ptrarray, void *ptr)
298 1.3 ryo {
299 1.3 ryo int error;
300 1.3 ryo
301 1.3 ryo if (ptrarray->pa_inuse >= ptrarray->pa_allocnum) {
302 1.3 ryo /* increase buffer */
303 1.3 ryo ptrarray->pa_allocnum += 1024;
304 1.3 ryo error = reallocarr(&ptrarray->pa_ptrs, ptrarray->pa_allocnum,
305 1.3 ryo sizeof(*ptrarray->pa_ptrs));
306 1.3 ryo if (error != 0)
307 1.4 ryo die_errc(EXIT_FAILURE, error, "rellocarr failed");
308 1.3 ryo }
309 1.3 ryo ptrarray->pa_ptrs[ptrarray->pa_inuse++] = ptr;
310 1.3 ryo }
311 1.3 ryo
312 1.3 ryo static void
313 1.3 ryo ptrarray_iterate(struct ptrarray *ptrarray, void (*ifunc)(void *))
314 1.3 ryo {
315 1.3 ryo size_t i;
316 1.1 ryo
317 1.3 ryo for (i = 0; i < ptrarray->pa_inuse; i++) {
318 1.3 ryo (*ifunc)(ptrarray->pa_ptrs[i]);
319 1.3 ryo }
320 1.3 ryo }
321 1.1 ryo
322 1.3 ryo static void
323 1.3 ryo ptrarray_clear(struct ptrarray *ptrarray)
324 1.3 ryo {
325 1.3 ryo ptrarray->pa_inuse = 0;
326 1.3 ryo }
327 1.1 ryo
328 1.1 ryo static int
329 1.1 ryo sample_compare_key(void *ctx, const void *n1, const void *keyp)
330 1.1 ryo {
331 1.1 ryo const struct sample_elm *a1 = n1;
332 1.1 ryo const struct sample_elm *a2 = (const struct sample_elm *)keyp;
333 1.1 ryo return a1->addr - a2->addr;
334 1.1 ryo }
335 1.1 ryo
336 1.1 ryo static signed int
337 1.1 ryo sample_compare_nodes(void *ctx, const void *n1, const void *n2)
338 1.1 ryo {
339 1.1 ryo const struct addr *a2 = n2;
340 1.1 ryo return sample_compare_key(ctx, n1, a2);
341 1.1 ryo }
342 1.1 ryo
343 1.1 ryo static const rb_tree_ops_t sample_ops = {
344 1.1 ryo .rbto_compare_nodes = sample_compare_nodes,
345 1.1 ryo .rbto_compare_key = sample_compare_key
346 1.1 ryo };
347 1.1 ryo
348 1.3 ryo static u_int
349 1.3 ryo n_align(u_int n, u_int align)
350 1.3 ryo {
351 1.3 ryo return (n + align - 1) / align * align;
352 1.3 ryo }
353 1.3 ryo
354 1.1 ryo static void
355 1.1 ryo sample_init(void)
356 1.1 ryo {
357 1.3 ryo const struct sample_elm *e;
358 1.4 ryo int l, mode, n;
359 1.4 ryo u_int size;
360 1.4 ryo char buf[16];
361 1.3 ryo
362 1.4 ryo size = sizeof(struct sample_elm) +
363 1.3 ryo sizeof(e->num_cpu[0]) * SAMPLE_MODE_NUM * ncpu;
364 1.3 ryo sizeof_sample_elm = n_align(size, __alignof(struct sample_elm));
365 1.3 ryo
366 1.4 ryo sample_cpu_width = ecalloc(1, sizeof(*sample_cpu_width) * ncpu);
367 1.4 ryo for (n = 0; n < ncpu; n++) {
368 1.4 ryo sample_cpu_width[n] = 5;
369 1.4 ryo l = snprintf(buf, sizeof(buf), "CPU%d", n);
370 1.4 ryo if (sample_cpu_width[n] < (u_int)l)
371 1.4 ryo sample_cpu_width[n] = l;
372 1.4 ryo }
373 1.4 ryo
374 1.3 ryo for (mode = 0; mode < SAMPLE_MODE_NUM; mode++) {
375 1.3 ryo sample_n_kern_per_cpu[mode] = ecalloc(1,
376 1.3 ryo sizeof(typeof(*sample_n_kern_per_cpu[mode])) * ncpu);
377 1.3 ryo sample_n_user_per_cpu[mode] = ecalloc(1,
378 1.3 ryo sizeof(typeof(*sample_n_user_per_cpu[mode])) * ncpu);
379 1.3 ryo sample_n_per_event[mode] = ecalloc(1,
380 1.3 ryo sizeof(typeof(*sample_n_per_event[mode])) * nevent);
381 1.3 ryo sample_n_per_event_cpu[mode] = ecalloc(1,
382 1.3 ryo sizeof(typeof(*sample_n_per_event_cpu[mode])) *
383 1.3 ryo nevent * ncpu);
384 1.3 ryo }
385 1.1 ryo }
386 1.1 ryo
387 1.1 ryo static void
388 1.3 ryo sample_clear_instantaneous(void *arg)
389 1.1 ryo {
390 1.3 ryo struct sample_elm *e = (void *)arg;
391 1.1 ryo
392 1.3 ryo e->num[SAMPLE_MODE_INSTANTANEOUS] = 0;
393 1.3 ryo memset(SAMPLE_ELM_NUM_CPU(e, SAMPLE_MODE_INSTANTANEOUS),
394 1.3 ryo 0, sizeof(e->num_cpu[0]) * ncpu);
395 1.1 ryo }
396 1.1 ryo
397 1.3 ryo static void
398 1.3 ryo sample_reset(bool reset_accumulative)
399 1.1 ryo {
400 1.3 ryo int mode;
401 1.3 ryo
402 1.3 ryo for (mode = 0; mode < SAMPLE_MODE_NUM; mode++) {
403 1.3 ryo if (mode == SAMPLE_MODE_ACCUMULATIVE && !reset_accumulative)
404 1.3 ryo continue;
405 1.1 ryo
406 1.3 ryo sample_n_kern[mode] = 0;
407 1.3 ryo sample_n_user[mode] = 0;
408 1.3 ryo memset(sample_n_kern_per_cpu[mode], 0,
409 1.3 ryo sizeof(typeof(*sample_n_kern_per_cpu[mode])) * ncpu);
410 1.3 ryo memset(sample_n_user_per_cpu[mode], 0,
411 1.3 ryo sizeof(typeof(*sample_n_user_per_cpu[mode])) * ncpu);
412 1.3 ryo memset(sample_n_per_event[mode], 0,
413 1.3 ryo sizeof(typeof(*sample_n_per_event[mode])) * nevent);
414 1.3 ryo memset(sample_n_per_event_cpu[mode], 0,
415 1.3 ryo sizeof(typeof(*sample_n_per_event_cpu[mode])) *
416 1.3 ryo nevent * ncpu);
417 1.1 ryo }
418 1.1 ryo
419 1.3 ryo if (reset_accumulative) {
420 1.3 ryo rb_tree_init(&rb_tree_sample, &sample_ops);
421 1.3 ryo ptrarray_iterate(&sample_list[SAMPLE_MODE_ACCUMULATIVE], free);
422 1.3 ryo ptrarray_clear(&sample_list[SAMPLE_MODE_ACCUMULATIVE]);
423 1.3 ryo ptrarray_clear(&sample_list[SAMPLE_MODE_INSTANTANEOUS]);
424 1.3 ryo } else {
425 1.3 ryo ptrarray_iterate(&sample_list[SAMPLE_MODE_INSTANTANEOUS],
426 1.3 ryo sample_clear_instantaneous);
427 1.3 ryo ptrarray_clear(&sample_list[SAMPLE_MODE_INSTANTANEOUS]);
428 1.3 ryo }
429 1.1 ryo }
430 1.1 ryo
431 1.3 ryo static int __unused
432 1.3 ryo sample_sortfunc_accumulative(const void *a, const void *b)
433 1.1 ryo {
434 1.3 ryo struct sample_elm * const *ea = a;
435 1.3 ryo struct sample_elm * const *eb = b;
436 1.3 ryo return (*eb)->num[SAMPLE_MODE_ACCUMULATIVE] -
437 1.3 ryo (*ea)->num[SAMPLE_MODE_ACCUMULATIVE];
438 1.1 ryo }
439 1.1 ryo
440 1.1 ryo static int
441 1.3 ryo sample_sortfunc_instantaneous(const void *a, const void *b)
442 1.1 ryo {
443 1.3 ryo struct sample_elm * const *ea = a;
444 1.3 ryo struct sample_elm * const *eb = b;
445 1.3 ryo return (*eb)->num[SAMPLE_MODE_INSTANTANEOUS] -
446 1.3 ryo (*ea)->num[SAMPLE_MODE_INSTANTANEOUS];
447 1.1 ryo }
448 1.1 ryo
449 1.1 ryo static void
450 1.3 ryo sample_sort_accumulative(void)
451 1.1 ryo {
452 1.3 ryo qsort(sample_list[SAMPLE_MODE_ACCUMULATIVE].pa_ptrs,
453 1.3 ryo sample_list[SAMPLE_MODE_ACCUMULATIVE].pa_inuse,
454 1.3 ryo sizeof(struct sample_elm *), sample_sortfunc_accumulative);
455 1.3 ryo }
456 1.3 ryo
457 1.3 ryo static void
458 1.3 ryo sample_sort_instantaneous(void)
459 1.3 ryo {
460 1.3 ryo qsort(sample_list[SAMPLE_MODE_INSTANTANEOUS].pa_ptrs,
461 1.3 ryo sample_list[SAMPLE_MODE_INSTANTANEOUS].pa_inuse,
462 1.3 ryo sizeof(struct sample_elm *), sample_sortfunc_instantaneous);
463 1.1 ryo }
464 1.1 ryo
465 1.1 ryo static void
466 1.1 ryo sample_collect(tprof_sample_t *s)
467 1.1 ryo {
468 1.1 ryo struct sample_elm *e, *o;
469 1.1 ryo const char *name;
470 1.1 ryo size_t symid;
471 1.1 ryo uint64_t addr, offset;
472 1.1 ryo uint32_t flags = 0;
473 1.1 ryo uint32_t eventid, cpuid;
474 1.3 ryo int mode;
475 1.1 ryo
476 1.1 ryo eventid = __SHIFTOUT(s->s_flags, TPROF_SAMPLE_COUNTER_MASK);
477 1.1 ryo cpuid = s->s_cpuid;
478 1.1 ryo
479 1.3 ryo if (eventid >= nevent) /* unknown event from tprof? */
480 1.3 ryo return;
481 1.3 ryo
482 1.3 ryo for (mode = 0; mode < SAMPLE_MODE_NUM; mode++) {
483 1.3 ryo sample_n_per_event[mode][eventid]++;
484 1.3 ryo sample_n_per_event_cpu[mode][nevent * cpuid + eventid]++;
485 1.3 ryo }
486 1.1 ryo
487 1.1 ryo if ((s->s_flags & TPROF_SAMPLE_INKERNEL) == 0) {
488 1.3 ryo sample_n_user[SAMPLE_MODE_ACCUMULATIVE]++;
489 1.3 ryo sample_n_user[SAMPLE_MODE_INSTANTANEOUS]++;
490 1.3 ryo sample_n_user_per_cpu[SAMPLE_MODE_ACCUMULATIVE][cpuid]++;
491 1.3 ryo sample_n_user_per_cpu[SAMPLE_MODE_INSTANTANEOUS][cpuid]++;
492 1.1 ryo
493 1.1 ryo name = NULL;
494 1.1 ryo addr = s->s_pid; /* XXX */
495 1.1 ryo flags |= SAMPLE_ELM_FLAGS_USER;
496 1.1 ryo
497 1.1 ryo if (!opt_userland)
498 1.1 ryo return;
499 1.1 ryo } else {
500 1.3 ryo sample_n_kern[SAMPLE_MODE_ACCUMULATIVE]++;
501 1.3 ryo sample_n_kern[SAMPLE_MODE_INSTANTANEOUS]++;
502 1.3 ryo sample_n_kern_per_cpu[SAMPLE_MODE_ACCUMULATIVE][cpuid]++;
503 1.3 ryo sample_n_kern_per_cpu[SAMPLE_MODE_INSTANTANEOUS][cpuid]++;
504 1.1 ryo
505 1.1 ryo name = ksymlookup(s->s_pc, &offset, &symid);
506 1.1 ryo if (name != NULL) {
507 1.1 ryo addr = ksyms[symid]->value;
508 1.1 ryo } else {
509 1.1 ryo addr = s->s_pc;
510 1.1 ryo }
511 1.1 ryo }
512 1.1 ryo
513 1.3 ryo e = ecalloc(1, sizeof_sample_elm);
514 1.1 ryo e->addr = addr;
515 1.1 ryo e->name = name;
516 1.1 ryo e->flags = flags;
517 1.3 ryo e->num[SAMPLE_MODE_ACCUMULATIVE] = 1;
518 1.3 ryo e->num[SAMPLE_MODE_INSTANTANEOUS] = 1;
519 1.3 ryo SAMPLE_ELM_NUM_CPU(e, SAMPLE_MODE_ACCUMULATIVE)[cpuid] = 1;
520 1.3 ryo SAMPLE_ELM_NUM_CPU(e, SAMPLE_MODE_INSTANTANEOUS)[cpuid] = 1;
521 1.1 ryo o = rb_tree_insert_node(&rb_tree_sample, e);
522 1.3 ryo if (o == e) {
523 1.3 ryo /* new symbol. add to list for sort */
524 1.3 ryo ptrarray_push(&sample_list[SAMPLE_MODE_ACCUMULATIVE], o);
525 1.3 ryo ptrarray_push(&sample_list[SAMPLE_MODE_INSTANTANEOUS], o);
526 1.3 ryo } else {
527 1.1 ryo /* already exists */
528 1.3 ryo free(e);
529 1.3 ryo
530 1.3 ryo o->num[SAMPLE_MODE_ACCUMULATIVE]++;
531 1.3 ryo if (o->num[SAMPLE_MODE_INSTANTANEOUS]++ == 0) {
532 1.3 ryo /* new instantaneous symbols. add to list for sort */
533 1.3 ryo ptrarray_push(&sample_list[SAMPLE_MODE_INSTANTANEOUS],
534 1.3 ryo o);
535 1.3 ryo }
536 1.3 ryo SAMPLE_ELM_NUM_CPU(o, SAMPLE_MODE_ACCUMULATIVE)[cpuid]++;
537 1.3 ryo SAMPLE_ELM_NUM_CPU(o, SAMPLE_MODE_INSTANTANEOUS)[cpuid]++;
538 1.1 ryo }
539 1.1 ryo }
540 1.1 ryo
541 1.1 ryo static void
542 1.4 ryo show_tprof_stat(int *lim)
543 1.1 ryo {
544 1.1 ryo static struct tprof_stat tsbuf[2], *ts0, *ts;
545 1.1 ryo static u_int ts_i = 0;
546 1.4 ryo static int tprofstat_width[6];
547 1.4 ryo int ret, l;
548 1.4 ryo char tmpbuf[128];
549 1.1 ryo
550 1.1 ryo ts0 = &tsbuf[ts_i++ & 1];
551 1.1 ryo ts = &tsbuf[ts_i & 1];
552 1.1 ryo ret = ioctl(devfd, TPROF_IOC_GETSTAT, ts);
553 1.1 ryo if (ret == -1)
554 1.4 ryo die_errc(EXIT_FAILURE, errno, "TPROF_IOC_GETSTAT");
555 1.1 ryo
556 1.4 ryo #define TS_PRINT(idx, label, _m) \
557 1.4 ryo do { \
558 1.4 ryo __CTASSERT(idx < __arraycount(tprofstat_width)); \
559 1.4 ryo lim_printf(lim, "%s", label); \
560 1.4 ryo l = snprintf(tmpbuf, sizeof(tmpbuf), "%"PRIu64, ts->_m);\
561 1.4 ryo if (ts->_m != ts0->_m) \
562 1.4 ryo l += snprintf(tmpbuf + l, sizeof(tmpbuf) - l, \
563 1.4 ryo "(+%"PRIu64")", ts->_m - ts0->_m); \
564 1.4 ryo assert(l < (int)sizeof(tmpbuf)); \
565 1.4 ryo if (tprofstat_width[idx] < l) \
566 1.4 ryo tprofstat_width[idx] = l; \
567 1.4 ryo lim_printf(lim, "%-*.*s ", tprofstat_width[idx], \
568 1.4 ryo tprofstat_width[idx], tmpbuf); \
569 1.1 ryo } while (0)
570 1.4 ryo lim_printf(lim, "tprof ");
571 1.4 ryo TS_PRINT(0, "sample:", ts_sample);
572 1.4 ryo TS_PRINT(1, "overflow:", ts_overflow);
573 1.4 ryo TS_PRINT(2, "buf:", ts_buf);
574 1.4 ryo TS_PRINT(3, "emptybuf:", ts_emptybuf);
575 1.4 ryo TS_PRINT(4, "dropbuf:", ts_dropbuf);
576 1.4 ryo TS_PRINT(5, "dropbuf_sample:", ts_dropbuf_sample);
577 1.1 ryo }
578 1.1 ryo
579 1.1 ryo static void
580 1.1 ryo show_timestamp(void)
581 1.1 ryo {
582 1.1 ryo struct timeval tv;
583 1.1 ryo gettimeofday(&tv, NULL);
584 1.1 ryo printf("%-8.8s", &(ctime((time_t *)&tv.tv_sec)[11]));
585 1.1 ryo }
586 1.1 ryo
587 1.1 ryo static void
588 1.1 ryo show_counters_alloc(void)
589 1.1 ryo {
590 1.3 ryo size_t sz = 2 * ncpu * nevent * sizeof(*counters);
591 1.3 ryo counters = ecalloc(1, sz);
592 1.1 ryo }
593 1.1 ryo
594 1.1 ryo static void
595 1.4 ryo show_counters(int *lim)
596 1.1 ryo {
597 1.1 ryo tprof_counts_t countsbuf;
598 1.1 ryo uint64_t *cn[2], *c0, *c;
599 1.1 ryo u_int i;
600 1.1 ryo int n, ret;
601 1.1 ryo
602 1.1 ryo cn[0] = counters;
603 1.1 ryo cn[1] = counters + ncpu * nevent;
604 1.1 ryo c0 = cn[counters_i++ & 1];
605 1.1 ryo c = cn[counters_i & 1];
606 1.1 ryo
607 1.1 ryo for (n = 0; n < ncpu; n++) {
608 1.1 ryo countsbuf.c_cpu = n;
609 1.1 ryo ret = ioctl(devfd, TPROF_IOC_GETCOUNTS, &countsbuf);
610 1.1 ryo if (ret == -1)
611 1.4 ryo die_errc(EXIT_FAILURE, errno, "TPROF_IOC_GETCOUNTS");
612 1.1 ryo
613 1.1 ryo for (i = 0; i < nevent; i++)
614 1.1 ryo c[n * nevent + i] = countsbuf.c_count[i];
615 1.1 ryo }
616 1.1 ryo
617 1.4 ryo if (do_redraw) {
618 1.4 ryo lim_printf(lim, "%-22s", "Event counter (delta)");
619 1.4 ryo for (n = 0; n < ncpu; n++) {
620 1.4 ryo char cpuname[16];
621 1.4 ryo snprintf(cpuname, sizeof(cpuname), "CPU%u", n);
622 1.4 ryo lim_printf(lim, "%11s", cpuname);
623 1.4 ryo }
624 1.4 ryo lim_newline(lim);
625 1.4 ryo } else {
626 1.4 ryo printf("\n");
627 1.1 ryo }
628 1.1 ryo
629 1.1 ryo for (i = 0; i < nevent; i++) {
630 1.4 ryo lim_printf(lim, "%-22.22s", eventname[i]);
631 1.1 ryo for (n = 0; n < ncpu; n++) {
632 1.4 ryo lim_printf(lim, "%11"PRIu64,
633 1.1 ryo c[n * nevent + i] - c0[n * nevent + i]);
634 1.1 ryo }
635 1.4 ryo lim_newline(lim);
636 1.1 ryo }
637 1.4 ryo lim_newline(lim);
638 1.1 ryo }
639 1.1 ryo
640 1.1 ryo static void
641 1.4 ryo show_count_per_event(int *lim)
642 1.1 ryo {
643 1.1 ryo u_int i, nsample_total;
644 1.4 ryo int n, l;
645 1.4 ryo char buf[32];
646 1.1 ryo
647 1.3 ryo nsample_total = sample_n_kern[opt_mode] + sample_n_user[opt_mode];
648 1.4 ryo if (nsample_total == 0)
649 1.4 ryo nsample_total = 1;
650 1.4 ryo
651 1.4 ryo /* calc width in advance */
652 1.4 ryo for (i = 0; i < nevent; i++) {
653 1.4 ryo l = snprintf(buf, sizeof(buf), "%"PRIu64,
654 1.4 ryo sample_n_per_event[opt_mode][i]);
655 1.5 ryo if (sample_event_width < (u_int)l) {
656 1.4 ryo sample_event_width = l;
657 1.5 ryo do_redraw = true;
658 1.5 ryo }
659 1.4 ryo }
660 1.6 ryo for (n = 0; n < ncpu; n++) {
661 1.6 ryo uint64_t sum = 0;
662 1.6 ryo for (i = 0; i < nevent; i++)
663 1.6 ryo sum += sample_n_per_event_cpu[opt_mode][nevent * n + i];
664 1.6 ryo l = snprintf(buf, sizeof(buf), "%"PRIu64, sum);
665 1.6 ryo if (sample_cpu_width[n] < (u_int)l) {
666 1.6 ryo sample_cpu_width[n] = l;
667 1.6 ryo do_redraw = true;
668 1.4 ryo }
669 1.4 ryo }
670 1.4 ryo
671 1.4 ryo if (do_redraw) {
672 1.6 ryo lim_printf(lim, " Rate %*s %-*s",
673 1.6 ryo sample_event_width, "Sample#",
674 1.6 ryo SYMBOL_LEN, "Eventname");
675 1.4 ryo for (n = 0; n < ncpu; n++) {
676 1.4 ryo snprintf(buf, sizeof(buf), "CPU%d", n);
677 1.4 ryo lim_printf(lim, " %*s", sample_cpu_width[n], buf);
678 1.4 ryo }
679 1.4 ryo lim_newline(lim);
680 1.4 ryo
681 1.4 ryo lim_printf(lim, "------ %*.*s %*.*s",
682 1.4 ryo sample_event_width, sample_event_width, LINESTR,
683 1.4 ryo SYMBOL_LEN, SYMBOL_LEN, LINESTR);
684 1.4 ryo for (n = 0; n < ncpu; n++) {
685 1.4 ryo lim_printf(lim, " %*.*s",
686 1.4 ryo sample_cpu_width[n], sample_cpu_width[n], LINESTR);
687 1.4 ryo }
688 1.4 ryo lim_newline(lim);
689 1.4 ryo } else {
690 1.4 ryo printf("\n\n");
691 1.4 ryo }
692 1.1 ryo
693 1.1 ryo for (i = 0; i < nevent; i++) {
694 1.3 ryo if (sample_n_per_event[opt_mode][i] >= nsample_total) {
695 1.4 ryo lim_printf(lim, "%5.1f%%", 100.0 *
696 1.4 ryo sample_n_per_event[opt_mode][i] / nsample_total);
697 1.1 ryo } else {
698 1.4 ryo lim_printf(lim, "%5.2f%%", 100.0 *
699 1.4 ryo sample_n_per_event[opt_mode][i] / nsample_total);
700 1.1 ryo }
701 1.4 ryo lim_printf(lim, " %*"PRIu64" ", sample_event_width,
702 1.4 ryo sample_n_per_event[opt_mode][i]);
703 1.1 ryo
704 1.4 ryo lim_printf(lim, "%-32.32s", eventname[i]);
705 1.1 ryo for (n = 0; n < ncpu; n++) {
706 1.4 ryo lim_printf(lim, " %*"PRIu64, sample_cpu_width[n],
707 1.3 ryo sample_n_per_event_cpu[opt_mode][nevent * n + i]);
708 1.1 ryo }
709 1.4 ryo lim_newline(lim);
710 1.1 ryo }
711 1.1 ryo }
712 1.1 ryo
713 1.1 ryo static void
714 1.1 ryo sample_show(void)
715 1.1 ryo {
716 1.1 ryo struct sample_elm *e;
717 1.3 ryo struct ptrarray *samples;
718 1.1 ryo u_int nsample_total;
719 1.4 ryo int i, l, lim, n, ndisp;
720 1.1 ryo char namebuf[32];
721 1.1 ryo const char *name;
722 1.1 ryo
723 1.4 ryo if (nshow++ == 0) {
724 1.4 ryo printf("\n");
725 1.4 ryo if (!nontty) {
726 1.4 ryo signal(SIGWINCH, sigwinch_handler);
727 1.4 ryo signal(SIGINT, die);
728 1.4 ryo signal(SIGQUIT, die);
729 1.4 ryo signal(SIGTERM, die);
730 1.4 ryo signal(SIGTSTP, sigtstp_handler);
731 1.4 ryo
732 1.4 ryo tty_setup();
733 1.4 ryo }
734 1.4 ryo } else {
735 1.4 ryo reset_cursor_pos();
736 1.4 ryo }
737 1.4 ryo
738 1.1 ryo int margin_lines = 7;
739 1.1 ryo
740 1.1 ryo margin_lines += 3 + nevent; /* show_counter_per_event() */
741 1.1 ryo
742 1.3 ryo if (opt_mode == SAMPLE_MODE_INSTANTANEOUS)
743 1.3 ryo sample_sort_instantaneous();
744 1.3 ryo else
745 1.3 ryo sample_sort_accumulative();
746 1.3 ryo samples = &sample_list[opt_mode];
747 1.3 ryo
748 1.1 ryo if (opt_showcounter)
749 1.1 ryo margin_lines += 2 + nevent;
750 1.1 ryo if (opt_userland)
751 1.1 ryo margin_lines += 1;
752 1.1 ryo
753 1.3 ryo ndisp = samples->pa_inuse;
754 1.1 ryo if (!nontty && ndisp > (win.ws_row - margin_lines))
755 1.1 ryo ndisp = win.ws_row - margin_lines;
756 1.1 ryo
757 1.4 ryo lim = win.ws_col;
758 1.3 ryo if (opt_mode == SAMPLE_MODE_ACCUMULATIVE)
759 1.4 ryo lim_printf(&lim, "[Accumulative mode] ");
760 1.4 ryo show_tprof_stat(&lim);
761 1.3 ryo
762 1.4 ryo if (lim >= 16) {
763 1.4 ryo l = win.ws_col - lim;
764 1.4 ryo if (!nontty) {
765 1.4 ryo clr_to_eol();
766 1.4 ryo for (; l <= win.ws_col - 17; l = ((l + 8) & -8))
767 1.4 ryo printf("\t");
768 1.4 ryo }
769 1.4 ryo show_timestamp();
770 1.4 ryo }
771 1.4 ryo lim_newline(&lim);
772 1.4 ryo lim_newline(&lim);
773 1.1 ryo
774 1.4 ryo if (opt_showcounter)
775 1.4 ryo show_counters(&lim);
776 1.1 ryo
777 1.4 ryo show_count_per_event(&lim);
778 1.4 ryo lim_newline(&lim);
779 1.1 ryo
780 1.4 ryo if (do_redraw) {
781 1.6 ryo lim_printf(&lim, " Rate %*s %-*s",
782 1.6 ryo sample_event_width, "Sample#",
783 1.6 ryo SYMBOL_LEN, "Symbol");
784 1.4 ryo for (n = 0; n < ncpu; n++) {
785 1.1 ryo snprintf(namebuf, sizeof(namebuf), "CPU%d", n);
786 1.4 ryo lim_printf(&lim, " %*s", sample_cpu_width[n], namebuf);
787 1.1 ryo }
788 1.4 ryo lim_newline(&lim);
789 1.1 ryo
790 1.4 ryo lim_printf(&lim, "------ %*.*s %*.*s",
791 1.4 ryo sample_event_width, sample_event_width, LINESTR,
792 1.4 ryo SYMBOL_LEN, SYMBOL_LEN, LINESTR);
793 1.4 ryo for (n = 0; n < ncpu; n++) {
794 1.4 ryo lim_printf(&lim, " %*.*s", sample_cpu_width[n],
795 1.4 ryo sample_cpu_width[n], LINESTR);
796 1.1 ryo }
797 1.4 ryo lim_newline(&lim);
798 1.4 ryo } else {
799 1.4 ryo printf("\n\n");
800 1.1 ryo }
801 1.1 ryo
802 1.1 ryo for (i = 0; i < ndisp; i++) {
803 1.3 ryo e = (struct sample_elm *)samples->pa_ptrs[i];
804 1.1 ryo name = e->name;
805 1.1 ryo if (name == NULL) {
806 1.1 ryo if (e->flags & SAMPLE_ELM_FLAGS_USER) {
807 1.3 ryo snprintf(namebuf, sizeof(namebuf),
808 1.3 ryo "<PID:%"PRIu64">", e->addr);
809 1.1 ryo } else {
810 1.3 ryo snprintf(namebuf, sizeof(namebuf),
811 1.3 ryo "0x%016"PRIx64, e->addr);
812 1.1 ryo }
813 1.1 ryo name = namebuf;
814 1.1 ryo }
815 1.1 ryo
816 1.3 ryo nsample_total = sample_n_kern[opt_mode];
817 1.1 ryo if (opt_userland)
818 1.3 ryo nsample_total += sample_n_user[opt_mode];
819 1.1 ryo /*
820 1.1 ryo * even when only kernel mode events are configured,
821 1.1 ryo * interrupts may still occur in the user mode state.
822 1.1 ryo */
823 1.1 ryo if (nsample_total == 0)
824 1.1 ryo nsample_total = 1;
825 1.1 ryo
826 1.3 ryo if (e->num[opt_mode] >= nsample_total) {
827 1.4 ryo lim_printf(&lim, "%5.1f%%", 100.0 *
828 1.4 ryo e->num[opt_mode] / nsample_total);
829 1.1 ryo } else {
830 1.4 ryo lim_printf(&lim, "%5.2f%%", 100.0 *
831 1.4 ryo e->num[opt_mode] / nsample_total);
832 1.1 ryo }
833 1.4 ryo lim_printf(&lim, " %*u %-32.32s", sample_event_width,
834 1.4 ryo e->num[opt_mode], name);
835 1.1 ryo
836 1.1 ryo for (n = 0; n < ncpu; n++) {
837 1.3 ryo if (SAMPLE_ELM_NUM_CPU(e, opt_mode)[n] == 0) {
838 1.4 ryo lim_printf(&lim, " %*s", sample_cpu_width[n],
839 1.4 ryo ".");
840 1.3 ryo } else {
841 1.4 ryo lim_printf(&lim, " %*u", sample_cpu_width[n],
842 1.3 ryo SAMPLE_ELM_NUM_CPU(e, opt_mode)[n]);
843 1.3 ryo }
844 1.1 ryo }
845 1.4 ryo lim_newline(&lim);
846 1.1 ryo }
847 1.1 ryo
848 1.3 ryo if ((u_int)ndisp != samples->pa_inuse) {
849 1.4 ryo lim_printf(&lim, " : %*s (more %zu symbols omitted)",
850 1.4 ryo sample_event_width, ":", samples->pa_inuse - ndisp);
851 1.4 ryo lim_newline(&lim);
852 1.4 ryo } else if (!nontty) {
853 1.1 ryo for (i = ndisp; i <= win.ws_row - margin_lines; i++) {
854 1.1 ryo printf("~");
855 1.4 ryo lim_newline(&lim);
856 1.1 ryo }
857 1.1 ryo }
858 1.1 ryo
859 1.4 ryo if (do_redraw) {
860 1.4 ryo lim_printf(&lim, "------ %*.*s %*.*s",
861 1.4 ryo sample_event_width, sample_event_width, LINESTR,
862 1.4 ryo SYMBOL_LEN, SYMBOL_LEN, LINESTR);
863 1.4 ryo for (n = 0; n < ncpu; n++) {
864 1.4 ryo lim_printf(&lim, " %*.*s",
865 1.4 ryo sample_cpu_width[n], sample_cpu_width[n], LINESTR);
866 1.4 ryo }
867 1.4 ryo lim_newline(&lim);
868 1.4 ryo } else {
869 1.4 ryo printf("\n");
870 1.1 ryo }
871 1.1 ryo
872 1.4 ryo lim_printf(&lim, "Total %*u %-32.32s",
873 1.4 ryo sample_event_width, sample_n_kern[opt_mode], "in-kernel");
874 1.1 ryo for (n = 0; n < ncpu; n++) {
875 1.4 ryo lim_printf(&lim, " %*u", sample_cpu_width[n],
876 1.4 ryo sample_n_kern_per_cpu[opt_mode][n]);
877 1.1 ryo }
878 1.1 ryo
879 1.1 ryo if (opt_userland) {
880 1.4 ryo lim_newline(&lim);
881 1.4 ryo lim_printf(&lim, " %*u %-32.32s",
882 1.4 ryo sample_event_width, sample_n_user[opt_mode], "userland");
883 1.1 ryo for (n = 0; n < ncpu; n++) {
884 1.4 ryo lim_printf(&lim, " %*u", sample_cpu_width[n],
885 1.4 ryo sample_n_user_per_cpu[opt_mode][n]);
886 1.1 ryo }
887 1.1 ryo }
888 1.1 ryo
889 1.4 ryo if (nontty)
890 1.4 ryo printf("\n");
891 1.4 ryo else
892 1.4 ryo clr_to_eol();
893 1.1 ryo }
894 1.1 ryo
895 1.1 ryo __dead static void
896 1.1 ryo tprof_top_usage(void)
897 1.1 ryo {
898 1.3 ryo fprintf(stderr, "%s top [-acu] [-e name[,scale] [-e ...]]"
899 1.3 ryo " [-i interval]\n", getprogname());
900 1.1 ryo exit(EXIT_FAILURE);
901 1.1 ryo }
902 1.1 ryo
903 1.6 ryo __dead void
904 1.1 ryo tprof_top(int argc, char **argv)
905 1.1 ryo {
906 1.1 ryo tprof_param_t params[TPROF_MAXCOUNTERS];
907 1.1 ryo struct itimerval it;
908 1.4 ryo ssize_t tprof_bufsize, len;
909 1.1 ryo u_int i;
910 1.1 ryo int ch, ret;
911 1.7 ryo char *tprof_buf, *p, *errmsg;
912 1.4 ryo bool noinput = false;
913 1.1 ryo
914 1.1 ryo memset(params, 0, sizeof(params));
915 1.1 ryo nevent = 0;
916 1.1 ryo
917 1.4 ryo while ((ch = getopt(argc, argv, "ace:i:L:u")) != -1) {
918 1.1 ryo switch (ch) {
919 1.3 ryo case 'a':
920 1.3 ryo opt_mode = SAMPLE_MODE_ACCUMULATIVE;
921 1.3 ryo break;
922 1.1 ryo case 'c':
923 1.1 ryo opt_showcounter = 1;
924 1.1 ryo break;
925 1.1 ryo case 'e':
926 1.7 ryo if (tprof_parse_event(¶ms[nevent], optarg,
927 1.7 ryo TPROF_PARSE_EVENT_F_ALLOWSCALE,
928 1.7 ryo &eventname[nevent], &errmsg) != 0) {
929 1.7 ryo die_errc(EXIT_FAILURE, 0, "%s", errmsg);
930 1.1 ryo }
931 1.1 ryo nevent++;
932 1.1 ryo if (nevent > __arraycount(params) ||
933 1.1 ryo nevent > ncounters)
934 1.4 ryo die_errc(EXIT_FAILURE, 0,
935 1.4 ryo "Too many events. Only a maximum of %d "
936 1.4 ryo "counters can be used.", ncounters);
937 1.1 ryo break;
938 1.1 ryo case 'i':
939 1.1 ryo top_interval = strtol(optarg, &p, 10);
940 1.1 ryo if (*p != '\0' || top_interval <= 0)
941 1.4 ryo die_errc(EXIT_FAILURE, 0,
942 1.4 ryo "Bad/invalid interval: %s", optarg);
943 1.1 ryo break;
944 1.1 ryo case 'u':
945 1.1 ryo opt_userland = 1;
946 1.1 ryo break;
947 1.1 ryo default:
948 1.1 ryo tprof_top_usage();
949 1.1 ryo }
950 1.1 ryo }
951 1.1 ryo argc -= optind;
952 1.1 ryo argv += optind;
953 1.1 ryo
954 1.1 ryo if (argc != 0)
955 1.1 ryo tprof_top_usage();
956 1.1 ryo
957 1.1 ryo if (nevent == 0) {
958 1.9 msaitoh const char *defaultevent = tprof_cycle_event_name();
959 1.1 ryo if (defaultevent == NULL)
960 1.4 ryo die_errc(EXIT_FAILURE, 0, "cpu not supported");
961 1.1 ryo
962 1.1 ryo tprof_event_lookup(defaultevent, ¶ms[nevent]);
963 1.1 ryo eventname[nevent] = defaultevent;
964 1.1 ryo nevent++;
965 1.1 ryo }
966 1.1 ryo
967 1.3 ryo sample_init();
968 1.1 ryo show_counters_alloc();
969 1.1 ryo
970 1.1 ryo for (i = 0; i < nevent; i++) {
971 1.1 ryo params[i].p_counter = i;
972 1.1 ryo params[i].p_flags |= TPROF_PARAM_KERN | TPROF_PARAM_PROFILE;
973 1.1 ryo if (opt_userland)
974 1.1 ryo params[i].p_flags |= TPROF_PARAM_USER;
975 1.1 ryo ret = ioctl(devfd, TPROF_IOC_CONFIGURE_EVENT, ¶ms[i]);
976 1.1 ryo if (ret == -1)
977 1.4 ryo die_errc(EXIT_FAILURE, errno,
978 1.4 ryo "TPROF_IOC_CONFIGURE_EVENT: %s", eventname[i]);
979 1.1 ryo }
980 1.1 ryo
981 1.1 ryo tprof_countermask_t mask = TPROF_COUNTERMASK_ALL;
982 1.1 ryo ret = ioctl(devfd, TPROF_IOC_START, &mask);
983 1.1 ryo if (ret == -1)
984 1.4 ryo die_errc(EXIT_FAILURE, errno, "TPROF_IOC_START");
985 1.1 ryo
986 1.1 ryo ksyms = ksymload(&nksyms);
987 1.1 ryo
988 1.4 ryo signal(SIGALRM, sigalrm_handler);
989 1.1 ryo
990 1.1 ryo it.it_interval.tv_sec = it.it_value.tv_sec = top_interval;
991 1.1 ryo it.it_interval.tv_usec = it.it_value.tv_usec = 0;
992 1.1 ryo setitimer(ITIMER_REAL, &it, NULL);
993 1.1 ryo
994 1.3 ryo sample_reset(true);
995 1.1 ryo printf("collecting samples...");
996 1.1 ryo fflush(stdout);
997 1.1 ryo
998 1.6 ryo tprof_bufsize = sizeof(tprof_sample_t) * 1024 * 32;
999 1.3 ryo tprof_buf = emalloc(tprof_bufsize);
1000 1.1 ryo do {
1001 1.4 ryo bool force_update = false;
1002 1.4 ryo
1003 1.6 ryo while (sigalrm == 0 && !force_update) {
1004 1.4 ryo fd_set r;
1005 1.4 ryo int nfound;
1006 1.4 ryo char c;
1007 1.4 ryo
1008 1.4 ryo FD_ZERO(&r);
1009 1.4 ryo if (!noinput)
1010 1.4 ryo FD_SET(STDIN_FILENO, &r);
1011 1.4 ryo FD_SET(devfd, &r);
1012 1.4 ryo nfound = select(devfd + 1, &r, NULL, NULL, NULL);
1013 1.4 ryo if (nfound == -1) {
1014 1.4 ryo if (errno == EINTR)
1015 1.4 ryo break;
1016 1.4 ryo die_errc(EXIT_FAILURE, errno, "select");
1017 1.4 ryo }
1018 1.4 ryo
1019 1.4 ryo if (FD_ISSET(STDIN_FILENO, &r)) {
1020 1.4 ryo len = read(STDIN_FILENO, &c, 1);
1021 1.4 ryo if (len <= 0) {
1022 1.4 ryo noinput = true;
1023 1.4 ryo continue;
1024 1.4 ryo }
1025 1.4 ryo switch (c) {
1026 1.4 ryo case 0x0c: /* ^L */
1027 1.4 ryo do_redraw = true;
1028 1.4 ryo break;
1029 1.4 ryo case 'a':
1030 1.4 ryo /* toggle mode */
1031 1.4 ryo opt_mode = (opt_mode + 1) %
1032 1.4 ryo SAMPLE_MODE_NUM;
1033 1.6 ryo do_redraw = true;
1034 1.6 ryo break;
1035 1.6 ryo case 'c':
1036 1.6 ryo /* toggle mode */
1037 1.6 ryo opt_showcounter ^= 1;
1038 1.6 ryo do_redraw = true;
1039 1.4 ryo break;
1040 1.4 ryo case 'q':
1041 1.4 ryo goto done;
1042 1.4 ryo case 'z':
1043 1.4 ryo sample_reset(true);
1044 1.4 ryo break;
1045 1.4 ryo default:
1046 1.6 ryo continue;
1047 1.4 ryo }
1048 1.4 ryo force_update = true;
1049 1.4 ryo }
1050 1.4 ryo
1051 1.4 ryo if (FD_ISSET(devfd, &r)) {
1052 1.4 ryo len = read(devfd, tprof_buf, tprof_bufsize);
1053 1.4 ryo if (len == -1 && errno != EINTR)
1054 1.4 ryo die_errc(EXIT_FAILURE, errno, "read");
1055 1.4 ryo if (len > 0) {
1056 1.4 ryo tprof_sample_t *s =
1057 1.4 ryo (tprof_sample_t *)tprof_buf;
1058 1.4 ryo while (s <
1059 1.4 ryo (tprof_sample_t *)(tprof_buf + len))
1060 1.4 ryo sample_collect(s++);
1061 1.4 ryo }
1062 1.1 ryo }
1063 1.1 ryo }
1064 1.1 ryo sigalrm = 0;
1065 1.1 ryo
1066 1.1 ryo /* update screen */
1067 1.1 ryo sample_show();
1068 1.1 ryo fflush(stdout);
1069 1.4 ryo do_redraw = false;
1070 1.4 ryo if (force_update)
1071 1.4 ryo continue;
1072 1.1 ryo
1073 1.3 ryo sample_reset(false);
1074 1.4 ryo
1075 1.1 ryo } while (!nontty);
1076 1.1 ryo
1077 1.4 ryo done:
1078 1.4 ryo die(0);
1079 1.1 ryo }
1080