Mon Apr 17 08:37:24 2023 UTC ()
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.


(msaitoh)
diff -r1.26 -r1.27 src/usr.sbin/tprof/tprof.8
diff -r1.20 -r1.21 src/usr.sbin/tprof/tprof.c
diff -r1.4 -r1.5 src/usr.sbin/tprof/tprof.h
diff -r1.8 -r1.9 src/usr.sbin/tprof/tprof_top.c

cvs diff -r1.26 -r1.27 src/usr.sbin/tprof/tprof.8 (expand / switch to unified diff)

--- 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
59utility accepts the following options. 59utility accepts the following options.
60The first argument, 60The first argument,
61.Ar op , 61.Ar op ,
62specifies the action to take. 62specifies the action to take.
63Valid actions are: 63Valid actions are:
64.Bl -tag -width offline -offset indent 64.Bl -tag -width offline -offset indent
65.It list 65.It list
66Display the following information: 66Display the following information:
67.Bl -bullet -compact 67.Bl -bullet -compact
68.It 68.It
69a list of performance counter events available on the system 69a list of performance counter events available on the system
70.It 70.It
71the maximum number of counters that can be used simultaneously 71the maximum number of counters that can be used simultaneously
 72.It
 73the 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
80Monitor the execution of command 81Monitor the execution of command
81.Ar command . 82.Ar command .
82.Ar name 83.Ar name
83specifies the name of the event to count; it must be taken from the list of 84specifies the name of the event to count; it must be taken from the list of
84available events. 85available events.
85.Ar option 86.Ar option
86specifies the source of the event; it must be a combination of 87specifies 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.
91Multiple 92Multiple
92.Fl e 93.Fl e
93arguments can be specified. 94arguments can be specified.
 95If none of the
 96.Fl e
 97arguments are speficied, the CPU's default counter is used.
94The collected samples are written into the file 98The collected samples are written into the file
95.Ar scale 99.Ar scale
96specifies the ratio of the speed to the cycle counter, or the counter until 100specifies the ratio of the speed to the cycle counter, or the counter until
97overflow. 101overflow.
98The counter reset value on overflow used for profiling is calculated from the 102The counter reset value on overflow used for profiling is calculated from the
99speed of the cycle counter by default, but for some events this value may be 103speed of the cycle counter by default, but for some events this value may be
100too large (counter increasing too slowly) to be sufficient for profiling. 104too large (counter increasing too slowly) to be sufficient for profiling.
101For example, to specify an event that increases about 1000 times slower than 105For example, to specify an event that increases about 1000 times slower than
102the cycle counter, specify 106the cycle counter, specify
103.Dq Pa -e event,1000 . 107.Dq Pa -e event,1000 .
104Also, if 108Also, if
105.Dq Pa -e event,=200 109.Dq Pa -e event,=200
106is specified, profiling is performed every time the counter is increased by 200. 110is specified, profiling is performed every time the counter is increased by 200.

cvs diff -r1.20 -r1.21 src/usr.sbin/tprof/tprof.c (expand / switch to unified diff)

--- 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
260static void 260static void
261tprof_list(int argc, char **argv) 261tprof_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
267int 272int
268tprof_parse_event(tprof_param_t *param, const char *str, uint32_t flags, 273tprof_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
 364const char *
 365tprof_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
359static void 387static void
360tprof_monitor_common(bool do_profile, int argc, char **argv) 388tprof_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, &params[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;

cvs diff -r1.4 -r1.5 src/usr.sbin/tprof/tprof.h (expand / switch to unified diff)

--- 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
32extern struct tprof_info tprof_info; 32extern struct tprof_info tprof_info;
33extern int ncpu; 33extern int ncpu;
34extern int devfd; 34extern int devfd;
35extern u_int ncounters; 35extern u_int ncounters;
36 36
 37const char *tprof_cycle_event_name(void);
37int tprof_event_init(uint32_t); 38int tprof_event_init(uint32_t);
38void tprof_event_list(void); 39void tprof_event_list(void);
39void tprof_event_lookup(const char *, struct tprof_param *); 40void tprof_event_lookup(const char *, struct tprof_param *);
40 41
41int tprof_parse_event(tprof_param_t *, const char *, uint32_t, const char **, 42int 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
46void tprof_analyze(int, char **); 47void tprof_analyze(int, char **);
47void tprof_top(int, char **); 48void tprof_top(int, char **);

cvs diff -r1.8 -r1.9 src/usr.sbin/tprof/tprof_top.c (expand / switch to unified diff)

--- 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_
108static u_int sample_n_kern[SAMPLE_MODE_NUM]; 108static u_int sample_n_kern[SAMPLE_MODE_NUM];
109static u_int sample_n_user[SAMPLE_MODE_NUM]; 109static u_int sample_n_user[SAMPLE_MODE_NUM];
110static u_int sample_event_width = 7; 110static u_int sample_event_width = 7;
111static u_int *sample_cpu_width; /* [ncpu] */ 111static u_int *sample_cpu_width; /* [ncpu] */
112static uint32_t *sample_n_kern_per_cpu[SAMPLE_MODE_NUM]; /* [ncpu] */ 112static uint32_t *sample_n_kern_per_cpu[SAMPLE_MODE_NUM]; /* [ncpu] */
113static uint32_t *sample_n_user_per_cpu[SAMPLE_MODE_NUM]; /* [ncpu] */ 113static uint32_t *sample_n_user_per_cpu[SAMPLE_MODE_NUM]; /* [ncpu] */
114static uint64_t *sample_n_per_event[SAMPLE_MODE_NUM]; /* [nevent] */ 114static uint64_t *sample_n_per_event[SAMPLE_MODE_NUM]; /* [nevent] */
115static uint64_t *sample_n_per_event_cpu[SAMPLE_MODE_NUM]; /* [ncpu] */ 115static uint64_t *sample_n_per_event_cpu[SAMPLE_MODE_NUM]; /* [ncpu] */
116 116
117/* raw event counter */ 117/* raw event counter */
118static uint64_t *counters; /* counters[2][ncpu][nevent] */ 118static uint64_t *counters; /* counters[2][ncpu][nevent] */
119static u_int counters_i; 119static u_int counters_i;
120 120
121static const char * 
122cycle_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 
144static void 121static void
145reset_cursor_pos(void) 122reset_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, &params[nevent]); 962 tprof_event_lookup(defaultevent, &params[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;