Use the default counter if -e argument is not specified. monitor command: The default counter is selected if -e argument is not specified. list command: Print the name of the default counter for monitor and top command.diff -r1.26 -r1.27 src/usr.sbin/tprof/tprof.8
(msaitoh)
--- src/usr.sbin/tprof/tprof.8 2023/04/17 07:13:35 1.26
+++ src/usr.sbin/tprof/tprof.8 2023/04/17 08:37:24 1.27
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | .\" $NetBSD: tprof.8,v 1.26 2023/04/17 07:13:35 msaitoh Exp $ | 1 | .\" $NetBSD: tprof.8,v 1.27 2023/04/17 08:37:24 msaitoh Exp $ | |
2 | .\" | 2 | .\" | |
3 | .\" Copyright (c)2011 YAMAMOTO Takashi, | 3 | .\" Copyright (c)2011 YAMAMOTO Takashi, | |
4 | .\" All rights reserved. | 4 | .\" All rights reserved. | |
5 | .\" | 5 | .\" | |
6 | .\" Redistribution and use in source and binary forms, with or without | 6 | .\" Redistribution and use in source and binary forms, with or without | |
7 | .\" modification, are permitted provided that the following conditions | 7 | .\" modification, are permitted provided that the following conditions | |
8 | .\" are met: | 8 | .\" are met: | |
9 | .\" 1. Redistributions of source code must retain the above copyright | 9 | .\" 1. Redistributions of source code must retain the above copyright | |
10 | .\" notice, this list of conditions and the following disclaimer. | 10 | .\" notice, this list of conditions and the following disclaimer. | |
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | |
12 | .\" notice, this list of conditions and the following disclaimer in the | 12 | .\" notice, this list of conditions and the following disclaimer in the | |
13 | .\" documentation and/or other materials provided with the distribution. | 13 | .\" documentation and/or other materials provided with the distribution. | |
14 | .\" | 14 | .\" | |
@@ -59,48 +59,52 @@ The | @@ -59,48 +59,52 @@ The | |||
59 | utility accepts the following options. | 59 | utility accepts the following options. | |
60 | The first argument, | 60 | The first argument, | |
61 | .Ar op , | 61 | .Ar op , | |
62 | specifies the action to take. | 62 | specifies the action to take. | |
63 | Valid actions are: | 63 | Valid actions are: | |
64 | .Bl -tag -width offline -offset indent | 64 | .Bl -tag -width offline -offset indent | |
65 | .It list | 65 | .It list | |
66 | Display the following information: | 66 | Display the following information: | |
67 | .Bl -bullet -compact | 67 | .Bl -bullet -compact | |
68 | .It | 68 | .It | |
69 | a list of performance counter events available on the system | 69 | a list of performance counter events available on the system | |
70 | .It | 70 | .It | |
71 | the maximum number of counters that can be used simultaneously | 71 | the maximum number of counters that can be used simultaneously | |
72 | .It | |||
73 | the default counter for monitor and top command | |||
72 | .El | 74 | .El | |
73 | .It monitor Xo | 75 | .It monitor Xo | |
74 | .Fl e | 76 | .Op Fl e Ar name[:option][,scale] | |
75 | .Ar name[:option][,scale] | |||
76 | .Op Fl e Ar ... | 77 | .Op Fl e Ar ... | |
77 | .Op Fl o Ar outfile | 78 | .Op Fl o Ar outfile | |
78 | .Ar command | 79 | .Ar command | |
79 | .Xc | 80 | .Xc | |
80 | Monitor the execution of command | 81 | Monitor the execution of command | |
81 | .Ar command . | 82 | .Ar command . | |
82 | .Ar name | 83 | .Ar name | |
83 | specifies the name of the event to count; it must be taken from the list of | 84 | specifies the name of the event to count; it must be taken from the list of | |
84 | available events. | 85 | available events. | |
85 | .Ar option | 86 | .Ar option | |
86 | specifies the source of the event; it must be a combination of | 87 | specifies the source of the event; it must be a combination of | |
87 | .Ar u | 88 | .Ar u | |
88 | (userland) and | 89 | (userland) and | |
89 | .Ar k | 90 | .Ar k | |
90 | (kernel). If omitted, it is assumed that both are specified. | 91 | (kernel). If omitted, it is assumed that both are specified. | |
91 | Multiple | 92 | Multiple | |
92 | .Fl e | 93 | .Fl e | |
93 | arguments can be specified. | 94 | arguments can be specified. | |
95 | If none of the | |||
96 | .Fl e | |||
97 | arguments are speficied, the CPU's default counter is used. | |||
94 | The collected samples are written into the file | 98 | The collected samples are written into the file | |
95 | .Ar scale | 99 | .Ar scale | |
96 | specifies the ratio of the speed to the cycle counter, or the counter until | 100 | specifies the ratio of the speed to the cycle counter, or the counter until | |
97 | overflow. | 101 | overflow. | |
98 | The counter reset value on overflow used for profiling is calculated from the | 102 | The counter reset value on overflow used for profiling is calculated from the | |
99 | speed of the cycle counter by default, but for some events this value may be | 103 | speed of the cycle counter by default, but for some events this value may be | |
100 | too large (counter increasing too slowly) to be sufficient for profiling. | 104 | too large (counter increasing too slowly) to be sufficient for profiling. | |
101 | For example, to specify an event that increases about 1000 times slower than | 105 | For example, to specify an event that increases about 1000 times slower than | |
102 | the cycle counter, specify | 106 | the cycle counter, specify | |
103 | .Dq Pa -e event,1000 . | 107 | .Dq Pa -e event,1000 . | |
104 | Also, if | 108 | Also, if | |
105 | .Dq Pa -e event,=200 | 109 | .Dq Pa -e event,=200 | |
106 | is specified, profiling is performed every time the counter is increased by 200. | 110 | is specified, profiling is performed every time the counter is increased by 200. |
--- src/usr.sbin/tprof/tprof.c 2022/12/26 08:00:13 1.20
+++ src/usr.sbin/tprof/tprof.c 2023/04/17 08:37:24 1.21
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: tprof.c,v 1.20 2022/12/26 08:00:13 ryo Exp $ */ | 1 | /* $NetBSD: tprof.c,v 1.21 2023/04/17 08:37:24 msaitoh Exp $ */ | |
2 | 2 | |||
3 | /* | 3 | /* | |
4 | * Copyright (c) 2018 The NetBSD Foundation, Inc. | 4 | * Copyright (c) 2018 The NetBSD Foundation, Inc. | |
5 | * All rights reserved. | 5 | * All rights reserved. | |
6 | * | 6 | * | |
7 | * This code is derived from software contributed to The NetBSD Foundation | 7 | * This code is derived from software contributed to The NetBSD Foundation | |
8 | * by Maxime Villard. | 8 | * by Maxime Villard. | |
9 | * | 9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | 10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | 11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | 12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | 13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | 14 | * notice, this list of conditions and the following disclaimer. | |
@@ -47,27 +47,27 @@ | @@ -47,27 +47,27 @@ | |||
47 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 47 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
48 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | 48 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
49 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 49 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
50 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 50 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
51 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 51 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
52 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 52 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
53 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 53 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
54 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 54 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
55 | * SUCH DAMAGE. | 55 | * SUCH DAMAGE. | |
56 | */ | 56 | */ | |
57 | 57 | |||
58 | #include <sys/cdefs.h> | 58 | #include <sys/cdefs.h> | |
59 | #ifndef lint | 59 | #ifndef lint | |
60 | __RCSID("$NetBSD: tprof.c,v 1.20 2022/12/26 08:00:13 ryo Exp $"); | 60 | __RCSID("$NetBSD: tprof.c,v 1.21 2023/04/17 08:37:24 msaitoh Exp $"); | |
61 | #endif /* not lint */ | 61 | #endif /* not lint */ | |
62 | 62 | |||
63 | #include <sys/atomic.h> | 63 | #include <sys/atomic.h> | |
64 | #include <sys/ioctl.h> | 64 | #include <sys/ioctl.h> | |
65 | #include <sys/sysctl.h> | 65 | #include <sys/sysctl.h> | |
66 | #include <sys/wait.h> | 66 | #include <sys/wait.h> | |
67 | 67 | |||
68 | #include <dev/tprof/tprof_ioctl.h> | 68 | #include <dev/tprof/tprof_ioctl.h> | |
69 | 69 | |||
70 | #include <err.h> | 70 | #include <err.h> | |
71 | #include <errno.h> | 71 | #include <errno.h> | |
72 | #include <fcntl.h> | 72 | #include <fcntl.h> | |
73 | #include <inttypes.h> | 73 | #include <inttypes.h> | |
@@ -250,27 +250,32 @@ process_stat(void *arg) | @@ -250,27 +250,32 @@ process_stat(void *arg) | |||
250 | 250 | |||
251 | while (atomic_add_int_nv(done, 0) == 0) { | 251 | while (atomic_add_int_nv(done, 0) == 0) { | |
252 | show_counters(); | 252 | show_counters(); | |
253 | nanosleep(&ts, NULL); | 253 | nanosleep(&ts, NULL); | |
254 | if (errno == EINTR) /* interrupted by SIGINFO? */ | 254 | if (errno == EINTR) /* interrupted by SIGINFO? */ | |
255 | output_delay(); | 255 | output_delay(); | |
256 | } | 256 | } | |
257 | return NULL; | 257 | return NULL; | |
258 | } | 258 | } | |
259 | 259 | |||
260 | static void | 260 | static void | |
261 | tprof_list(int argc, char **argv) | 261 | tprof_list(int argc, char **argv) | |
262 | { | 262 | { | |
263 | printf("%u events can be counted at the same time\n", ncounters); | 263 | const char *defaultevent = tprof_cycle_event_name(); | |
264 | ||||
265 | printf("%u events can be counted at the same time.\n", ncounters); | |||
266 | if (defaultevent != NULL) | |||
267 | printf("The default counter for monitor and top command is " | |||
268 | "\"%s\".\n", defaultevent); | |||
264 | tprof_event_list(); | 269 | tprof_event_list(); | |
265 | } | 270 | } | |
266 | 271 | |||
267 | int | 272 | int | |
268 | tprof_parse_event(tprof_param_t *param, const char *str, uint32_t flags, | 273 | tprof_parse_event(tprof_param_t *param, const char *str, uint32_t flags, | |
269 | const char **eventnamep, char **errmsgp) | 274 | const char **eventnamep, char **errmsgp) | |
270 | { | 275 | { | |
271 | double d; | 276 | double d; | |
272 | uint64_t n; | 277 | uint64_t n; | |
273 | int error = 0; | 278 | int error = 0; | |
274 | char *p, *event = NULL, *opt = NULL, *scale = NULL; | 279 | char *p, *event = NULL, *opt = NULL, *scale = NULL; | |
275 | bool allow_option, allow_scale; | 280 | bool allow_option, allow_scale; | |
276 | static char errmsgbuf[128]; | 281 | static char errmsgbuf[128]; | |
@@ -346,26 +351,49 @@ tprof_parse_event(tprof_param_t *param, | @@ -346,26 +351,49 @@ tprof_parse_event(tprof_param_t *param, | |||
346 | "invalid scale: %s", scale); | 351 | "invalid scale: %s", scale); | |
347 | goto done; | 352 | goto done; | |
348 | } | 353 | } | |
349 | } | 354 | } | |
350 | 355 | |||
351 | done: | 356 | done: | |
352 | if (eventnamep != NULL) | 357 | if (eventnamep != NULL) | |
353 | *eventnamep = event; | 358 | *eventnamep = event; | |
354 | if (error != 0 && errmsgp != NULL) | 359 | if (error != 0 && errmsgp != NULL) | |
355 | *errmsgp = errmsgbuf; | 360 | *errmsgp = errmsgbuf; | |
356 | return error; | 361 | return error; | |
357 | } | 362 | } | |
358 | 363 | |||
364 | const char * | |||
365 | tprof_cycle_event_name(void) | |||
366 | { | |||
367 | const char *cycleevent; | |||
368 | ||||
369 | switch (tprof_info.ti_ident) { | |||
370 | case TPROF_IDENT_INTEL_GENERIC: | |||
371 | cycleevent = "unhalted-core-cycles"; | |||
372 | break; | |||
373 | case TPROF_IDENT_AMD_GENERIC: | |||
374 | cycleevent = "LsNotHaltedCyc"; | |||
375 | break; | |||
376 | case TPROF_IDENT_ARMV8_GENERIC: | |||
377 | case TPROF_IDENT_ARMV7_GENERIC: | |||
378 | cycleevent = "CPU_CYCLES"; | |||
379 | break; | |||
380 | default: | |||
381 | cycleevent = NULL; | |||
382 | break; | |||
383 | } | |||
384 | return cycleevent; | |||
385 | } | |||
386 | ||||
359 | static void | 387 | static void | |
360 | tprof_monitor_common(bool do_profile, int argc, char **argv) | 388 | tprof_monitor_common(bool do_profile, int argc, char **argv) | |
361 | { | 389 | { | |
362 | const char *outfile = "tprof.out"; | 390 | const char *outfile = "tprof.out"; | |
363 | struct tprof_stat ts; | 391 | struct tprof_stat ts; | |
364 | tprof_param_t params[TPROF_MAXCOUNTERS]; | 392 | tprof_param_t params[TPROF_MAXCOUNTERS]; | |
365 | pid_t pid; | 393 | pid_t pid; | |
366 | pthread_t pt; | 394 | pthread_t pt; | |
367 | int ret, ch, i; | 395 | int ret, ch, i; | |
368 | char *p, *errmsg; | 396 | char *p, *errmsg; | |
369 | tprof_countermask_t mask = TPROF_COUNTERMASK_ALL; | 397 | tprof_countermask_t mask = TPROF_COUNTERMASK_ALL; | |
370 | 398 | |||
371 | memset(params, 0, sizeof(params)); | 399 | memset(params, 0, sizeof(params)); | |
@@ -394,28 +422,37 @@ tprof_monitor_common(bool do_profile, in | @@ -394,28 +422,37 @@ tprof_monitor_common(bool do_profile, in | |||
394 | nevent++; | 422 | nevent++; | |
395 | if (nevent > __arraycount(params) || | 423 | if (nevent > __arraycount(params) || | |
396 | nevent > ncounters) | 424 | nevent > ncounters) | |
397 | errx(EXIT_FAILURE, "Too many events. Only a" | 425 | errx(EXIT_FAILURE, "Too many events. Only a" | |
398 | " maximum of %d counters can be used.", | 426 | " maximum of %d counters can be used.", | |
399 | ncounters); | 427 | ncounters); | |
400 | break; | 428 | break; | |
401 | default: | 429 | default: | |
402 | usage(); | 430 | usage(); | |
403 | } | 431 | } | |
404 | } | 432 | } | |
405 | argc -= optind; | 433 | argc -= optind; | |
406 | argv += optind; | 434 | argv += optind; | |
407 | if (argc == 0 || nevent == 0) { | 435 | if (argc == 0) | |
408 | usage(); | 436 | usage(); | |
437 | if (nevent == 0) { | |||
438 | const char *defaultevent = tprof_cycle_event_name(); | |||
439 | if (defaultevent == NULL) | |||
440 | errx(EXIT_FAILURE, "cpu not supported"); | |||
441 | ||||
442 | tprof_event_lookup(defaultevent, ¶ms[nevent]); | |||
443 | eventname[nevent] = defaultevent; | |||
444 | params[nevent].p_flags |= TPROF_PARAM_KERN; | |||
445 | nevent++; | |||
409 | } | 446 | } | |
410 | 447 | |||
411 | if (do_profile) { | 448 | if (do_profile) { | |
412 | outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666); | 449 | outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666); | |
413 | if (outfd == -1) { | 450 | if (outfd == -1) { | |
414 | err(EXIT_FAILURE, "%s", outfile); | 451 | err(EXIT_FAILURE, "%s", outfile); | |
415 | } | 452 | } | |
416 | } | 453 | } | |
417 | 454 | |||
418 | for (i = 0; i < (int)nevent; i++) { | 455 | for (i = 0; i < (int)nevent; i++) { | |
419 | params[i].p_counter = i; | 456 | params[i].p_counter = i; | |
420 | if (do_profile) | 457 | if (do_profile) | |
421 | params[i].p_flags |= TPROF_PARAM_PROFILE; | 458 | params[i].p_flags |= TPROF_PARAM_PROFILE; |
--- src/usr.sbin/tprof/tprof.h 2022/12/16 08:02:04 1.4
+++ src/usr.sbin/tprof/tprof.h 2023/04/17 08:37:24 1.5
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: tprof.h,v 1.4 2022/12/16 08:02:04 ryo Exp $ */ | 1 | /* $NetBSD: tprof.h,v 1.5 2023/04/17 08:37:24 msaitoh Exp $ */ | |
2 | 2 | |||
3 | /* | 3 | /* | |
4 | * Copyright (c) 2018 The NetBSD Foundation, Inc. | 4 | * Copyright (c) 2018 The NetBSD Foundation, Inc. | |
5 | * All rights reserved. | 5 | * All rights reserved. | |
6 | * | 6 | * | |
7 | * This code is derived from software contributed to The NetBSD Foundation | 7 | * This code is derived from software contributed to The NetBSD Foundation | |
8 | * by Maxime Villard. | 8 | * by Maxime Villard. | |
9 | * | 9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | 10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | 11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | 12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | 13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | 14 | * notice, this list of conditions and the following disclaimer. | |
@@ -24,24 +24,25 @@ | @@ -24,24 +24,25 @@ | |||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
29 | * POSSIBILITY OF SUCH DAMAGE. | 29 | * POSSIBILITY OF SUCH DAMAGE. | |
30 | */ | 30 | */ | |
31 | 31 | |||
32 | extern struct tprof_info tprof_info; | 32 | extern struct tprof_info tprof_info; | |
33 | extern int ncpu; | 33 | extern int ncpu; | |
34 | extern int devfd; | 34 | extern int devfd; | |
35 | extern u_int ncounters; | 35 | extern u_int ncounters; | |
36 | 36 | |||
37 | const char *tprof_cycle_event_name(void); | |||
37 | int tprof_event_init(uint32_t); | 38 | int tprof_event_init(uint32_t); | |
38 | void tprof_event_list(void); | 39 | void tprof_event_list(void); | |
39 | void tprof_event_lookup(const char *, struct tprof_param *); | 40 | void tprof_event_lookup(const char *, struct tprof_param *); | |
40 | 41 | |||
41 | int tprof_parse_event(tprof_param_t *, const char *, uint32_t, const char **, | 42 | int tprof_parse_event(tprof_param_t *, const char *, uint32_t, const char **, | |
42 | char **); | 43 | char **); | |
43 | #define TPROF_PARSE_EVENT_F_ALLOWOPTION 0x00000001 | 44 | #define TPROF_PARSE_EVENT_F_ALLOWOPTION 0x00000001 | |
44 | #define TPROF_PARSE_EVENT_F_ALLOWSCALE 0x00000002 | 45 | #define TPROF_PARSE_EVENT_F_ALLOWSCALE 0x00000002 | |
45 | 46 | |||
46 | void tprof_analyze(int, char **); | 47 | void tprof_analyze(int, char **); | |
47 | void tprof_top(int, char **); | 48 | void tprof_top(int, char **); |
--- src/usr.sbin/tprof/tprof_top.c 2022/12/23 19:37:06 1.8
+++ src/usr.sbin/tprof/tprof_top.c 2023/04/17 08:37:24 1.9
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: tprof_top.c,v 1.8 2022/12/23 19:37:06 christos Exp $ */ | 1 | /* $NetBSD: tprof_top.c,v 1.9 2023/04/17 08:37:24 msaitoh Exp $ */ | |
2 | 2 | |||
3 | /*- | 3 | /*- | |
4 | * Copyright (c) 2022 Ryo Shimizu <ryo@nerv.org> | 4 | * Copyright (c) 2022 Ryo Shimizu <ryo@nerv.org> | |
5 | * All rights reserved. | 5 | * All rights reserved. | |
6 | * | 6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | 7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | 8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | 9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | 10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | 11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | 13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | 14 | * documentation and/or other materials provided with the distribution. | |
@@ -18,27 +18,27 @@ | @@ -18,27 +18,27 @@ | |||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | |
20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | 23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
24 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | 24 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
25 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 25 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | 26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | 27 | */ | |
28 | 28 | |||
29 | #include <sys/cdefs.h> | 29 | #include <sys/cdefs.h> | |
30 | #ifndef lint | 30 | #ifndef lint | |
31 | __RCSID("$NetBSD: tprof_top.c,v 1.8 2022/12/23 19:37:06 christos Exp $"); | 31 | __RCSID("$NetBSD: tprof_top.c,v 1.9 2023/04/17 08:37:24 msaitoh Exp $"); | |
32 | #endif /* not lint */ | 32 | #endif /* not lint */ | |
33 | 33 | |||
34 | #include <sys/param.h> | 34 | #include <sys/param.h> | |
35 | #include <sys/types.h> | 35 | #include <sys/types.h> | |
36 | #include <sys/ioctl.h> | 36 | #include <sys/ioctl.h> | |
37 | #include <sys/rbtree.h> | 37 | #include <sys/rbtree.h> | |
38 | #include <sys/select.h> | 38 | #include <sys/select.h> | |
39 | #include <sys/time.h> | 39 | #include <sys/time.h> | |
40 | 40 | |||
41 | #include <assert.h> | 41 | #include <assert.h> | |
42 | #include <err.h> | 42 | #include <err.h> | |
43 | #include <errno.h> | 43 | #include <errno.h> | |
44 | #include <fcntl.h> | 44 | #include <fcntl.h> | |
@@ -108,49 +108,26 @@ struct ptrarray sample_list[SAMPLE_MODE_ | @@ -108,49 +108,26 @@ struct ptrarray sample_list[SAMPLE_MODE_ | |||
108 | static u_int sample_n_kern[SAMPLE_MODE_NUM]; | 108 | static u_int sample_n_kern[SAMPLE_MODE_NUM]; | |
109 | static u_int sample_n_user[SAMPLE_MODE_NUM]; | 109 | static u_int sample_n_user[SAMPLE_MODE_NUM]; | |
110 | static u_int sample_event_width = 7; | 110 | static u_int sample_event_width = 7; | |
111 | static u_int *sample_cpu_width; /* [ncpu] */ | 111 | static u_int *sample_cpu_width; /* [ncpu] */ | |
112 | static uint32_t *sample_n_kern_per_cpu[SAMPLE_MODE_NUM]; /* [ncpu] */ | 112 | static uint32_t *sample_n_kern_per_cpu[SAMPLE_MODE_NUM]; /* [ncpu] */ | |
113 | static uint32_t *sample_n_user_per_cpu[SAMPLE_MODE_NUM]; /* [ncpu] */ | 113 | static uint32_t *sample_n_user_per_cpu[SAMPLE_MODE_NUM]; /* [ncpu] */ | |
114 | static uint64_t *sample_n_per_event[SAMPLE_MODE_NUM]; /* [nevent] */ | 114 | static uint64_t *sample_n_per_event[SAMPLE_MODE_NUM]; /* [nevent] */ | |
115 | static uint64_t *sample_n_per_event_cpu[SAMPLE_MODE_NUM]; /* [ncpu] */ | 115 | static uint64_t *sample_n_per_event_cpu[SAMPLE_MODE_NUM]; /* [ncpu] */ | |
116 | 116 | |||
117 | /* raw event counter */ | 117 | /* raw event counter */ | |
118 | static uint64_t *counters; /* counters[2][ncpu][nevent] */ | 118 | static uint64_t *counters; /* counters[2][ncpu][nevent] */ | |
119 | static u_int counters_i; | 119 | static u_int counters_i; | |
120 | 120 | |||
121 | static const char * | |||
122 | cycle_event_name(void) | |||
123 | { | |||
124 | const char *cycleevent; | |||
125 | ||||
126 | switch (tprof_info.ti_ident) { | |||
127 | case TPROF_IDENT_INTEL_GENERIC: | |||
128 | cycleevent = "unhalted-core-cycles"; | |||
129 | break; | |||
130 | case TPROF_IDENT_AMD_GENERIC: | |||
131 | cycleevent = "LsNotHaltedCyc"; | |||
132 | break; | |||
133 | case TPROF_IDENT_ARMV8_GENERIC: | |||
134 | case TPROF_IDENT_ARMV7_GENERIC: | |||
135 | cycleevent = "CPU_CYCLES"; | |||
136 | break; | |||
137 | default: | |||
138 | cycleevent = NULL; | |||
139 | break; | |||
140 | } | |||
141 | return cycleevent; | |||
142 | } | |||
143 | ||||
144 | static void | 121 | static void | |
145 | reset_cursor_pos(void) | 122 | reset_cursor_pos(void) | |
146 | { | 123 | { | |
147 | int i; | 124 | int i; | |
148 | char *p; | 125 | char *p; | |
149 | 126 | |||
150 | if (nontty || term == NULL) | 127 | if (nontty || term == NULL) | |
151 | return; | 128 | return; | |
152 | 129 | |||
153 | printf("\r"); | 130 | printf("\r"); | |
154 | 131 | |||
155 | /* cursor_up * n */ | 132 | /* cursor_up * n */ | |
156 | if ((p = tigetstr("cuu")) != NULL) { | 133 | if ((p = tigetstr("cuu")) != NULL) { | |
@@ -968,27 +945,27 @@ tprof_top(int argc, char **argv) | @@ -968,27 +945,27 @@ tprof_top(int argc, char **argv) | |||
968 | opt_userland = 1; | 945 | opt_userland = 1; | |
969 | break; | 946 | break; | |
970 | default: | 947 | default: | |
971 | tprof_top_usage(); | 948 | tprof_top_usage(); | |
972 | } | 949 | } | |
973 | } | 950 | } | |
974 | argc -= optind; | 951 | argc -= optind; | |
975 | argv += optind; | 952 | argv += optind; | |
976 | 953 | |||
977 | if (argc != 0) | 954 | if (argc != 0) | |
978 | tprof_top_usage(); | 955 | tprof_top_usage(); | |
979 | 956 | |||
980 | if (nevent == 0) { | 957 | if (nevent == 0) { | |
981 | const char *defaultevent = cycle_event_name(); | 958 | const char *defaultevent = tprof_cycle_event_name(); | |
982 | if (defaultevent == NULL) | 959 | if (defaultevent == NULL) | |
983 | die_errc(EXIT_FAILURE, 0, "cpu not supported"); | 960 | die_errc(EXIT_FAILURE, 0, "cpu not supported"); | |
984 | 961 | |||
985 | tprof_event_lookup(defaultevent, ¶ms[nevent]); | 962 | tprof_event_lookup(defaultevent, ¶ms[nevent]); | |
986 | eventname[nevent] = defaultevent; | 963 | eventname[nevent] = defaultevent; | |
987 | nevent++; | 964 | nevent++; | |
988 | } | 965 | } | |
989 | 966 | |||
990 | sample_init(); | 967 | sample_init(); | |
991 | show_counters_alloc(); | 968 | show_counters_alloc(); | |
992 | 969 | |||
993 | for (i = 0; i < nevent; i++) { | 970 | for (i = 0; i < nevent; i++) { | |
994 | params[i].p_counter = i; | 971 | params[i].p_counter = i; |