Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/tprof add "top" subcommand to tprof(8)



details:   https://anonhg.NetBSD.org/src/rev/768dd7a3a89d
branches:  trunk
changeset: 372481:768dd7a3a89d
user:      ryo <ryo%NetBSD.org@localhost>
date:      Thu Dec 01 00:43:27 2022 +0000

description:
add "top" subcommand to tprof(8)

diffstat:

 usr.sbin/tprof/Makefile        |    6 +-
 usr.sbin/tprof/ksyms.c         |   20 +-
 usr.sbin/tprof/ksyms.h         |    9 +-
 usr.sbin/tprof/tprof.8         |   35 +-
 usr.sbin/tprof/tprof.c         |   25 +-
 usr.sbin/tprof/tprof.h         |    8 +-
 usr.sbin/tprof/tprof_analyze.c |   10 +-
 usr.sbin/tprof/tprof_top.c     |  739 +++++++++++++++++++++++++++++++++++++++++
 8 files changed, 821 insertions(+), 31 deletions(-)

diffs (truncated from 1066 to 300 lines):

diff -r 35f3ac118c2a -r 768dd7a3a89d usr.sbin/tprof/Makefile
--- a/usr.sbin/tprof/Makefile   Thu Dec 01 00:41:10 2022 +0000
+++ b/usr.sbin/tprof/Makefile   Thu Dec 01 00:43:27 2022 +0000
@@ -1,10 +1,10 @@
-#      $NetBSD: Makefile,v 1.11 2022/12/01 00:41:10 ryo Exp $
+#      $NetBSD: Makefile,v 1.12 2022/12/01 00:43:27 ryo Exp $
 
 .PATH: ${.CURDIR}/arch
 
 PROG=  tprof
 MAN=   tprof.8
-SRCS=  tprof.c tprof_analyze.c ksyms.c
+SRCS=  tprof.c tprof_analyze.c tprof_top.c ksyms.c
 
 .if    ${MACHINE_ARCH} == "i386" || ${MACHINE_ARCH} == "x86_64"
 SRCS+= tprof_x86.c
@@ -19,9 +19,11 @@
 CPPFLAGS+= -I${NETBSDSRCDIR}/sys/
 
 LDADD+= -lpthread
+LDADD+= -lm
 LDADD+= -lelf
 LDADD+= -lutil
 DPADD+= ${LIBPTHREAD}
+DPADD+= ${LIBM}
 DPADD+= ${LIBELF}
 DPADD+= ${LIBUTIL}
 
diff -r 35f3ac118c2a -r 768dd7a3a89d usr.sbin/tprof/ksyms.c
--- a/usr.sbin/tprof/ksyms.c    Thu Dec 01 00:41:10 2022 +0000
+++ b/usr.sbin/tprof/ksyms.c    Thu Dec 01 00:43:27 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ksyms.c,v 1.1 2022/12/01 00:41:10 ryo Exp $    */
+/*     $NetBSD: ksyms.c,v 1.2 2022/12/01 00:43:27 ryo Exp $    */
 
 /*
  * Copyright (c) 2010,2011,2012 YAMAMOTO Takashi,
@@ -28,7 +28,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: ksyms.c,v 1.1 2022/12/01 00:41:10 ryo Exp $");
+__RCSID("$NetBSD: ksyms.c,v 1.2 2022/12/01 00:43:27 ryo Exp $");
 #endif /* not lint */
 
 #include <assert.h>
@@ -43,8 +43,8 @@
 #include <util.h>
 #include "ksyms.h"
 
-struct sym **syms = NULL;
-size_t nsyms = 0;
+static struct sym **syms = NULL;
+static size_t nsyms = 0;
 
 static int
 compare_value(const void *p1, const void *p2)
@@ -69,8 +69,8 @@
        return strcmp(s1->name, s2->name);
 }
 
-void
-ksymload(void)
+struct sym **
+ksymload(size_t *nsymp)
 {
        Elf *e;
        Elf_Scn *s;
@@ -132,13 +132,15 @@
        elf_end(e);
        close(fd);
        qsort(syms, nsyms, sizeof(*syms), compare_value);
-       return;
+       if (nsymp != NULL)
+               *nsymp = nsyms;
+       return syms;
 elffail:
        errx(EXIT_FAILURE, "libelf: %s", elf_errmsg(elf_errno()));
 }
 
 const char *
-ksymlookup(uint64_t value, uint64_t *offset)
+ksymlookup(uint64_t value, uint64_t *offset, size_t *n)
 {
        size_t hi;
        size_t lo;
@@ -171,6 +173,8 @@
                if (sym->value <= value &&
                    (sym->size == 0 || value - sym->value <= sym->size )) {
                        *offset = value - sym->value;
+                       if (n != NULL)
+                               *n = i;
                        return sym->name;
                }
                if (sym->size != 0 && sym->value + sym->size < value) {
diff -r 35f3ac118c2a -r 768dd7a3a89d usr.sbin/tprof/ksyms.h
--- a/usr.sbin/tprof/ksyms.h    Thu Dec 01 00:41:10 2022 +0000
+++ b/usr.sbin/tprof/ksyms.h    Thu Dec 01 00:43:27 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ksyms.h,v 1.1 2022/12/01 00:41:10 ryo Exp $    */
+/*     $NetBSD: ksyms.h,v 1.2 2022/12/01 00:43:27 ryo Exp $    */
 
 /*
  * Copyright (c) 2010,2011,2012 YAMAMOTO Takashi,
@@ -35,10 +35,7 @@
        uint64_t size;
 };
 
-extern struct sym **syms;
-extern size_t nsyms;
-
-void ksymload(void);
-const char *ksymlookup(uint64_t, uint64_t *);
+struct sym **ksymload(size_t *);
+const char *ksymlookup(uint64_t, uint64_t *, size_t *);
 
 #endif /* _KSYMS_H_ */
diff -r 35f3ac118c2a -r 768dd7a3a89d usr.sbin/tprof/tprof.8
--- a/usr.sbin/tprof/tprof.8    Thu Dec 01 00:41:10 2022 +0000
+++ b/usr.sbin/tprof/tprof.8    Thu Dec 01 00:43:27 2022 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: tprof.8,v 1.18 2022/12/01 00:40:05 ryo Exp $
+.\"    $NetBSD: tprof.8,v 1.19 2022/12/01 00:43:27 ryo Exp $
 .\"
 .\" Copyright (c)2011 YAMAMOTO Takashi,
 .\" All rights reserved.
@@ -130,6 +130,39 @@
 .It Fl s
 Per symbol.
 .El
+.It top Xo
+.Oo
+.Fl e
+.Ar name[,value]
+.Op Fl e Ar ...
+.Oc
+.Op Fl i Ar interval
+.Op Fl c
+.Op Fl u
+.Xc
+Displays profiling results in real-time.
+.Ar name
+specifies the name of the event to count.
+.Ar value
+specifies the ratio of the speed to the cycle counter, or the counter until
+overflow.
+The counter reset value on overflow used for profiling is calculated from the
+speed of the cycle counter by default, but for some events this value may be
+too large (counter increasing too slowly) to be sufficient for profiling.
+For example, to specify an event that increases about 1000 times slower than
+the cycle counter, specify
+.Dq Pa -e event,1000 .
+Also, if 
+.Dq Pa -e event,=200
+is specified, profiling is performed every time the counter is increased by 200.
+.Bl -tag -width XXintervalX -offset indent
+.It Fl i Ar interval
+set the update interval in seconds. The default value is 1.
+.It Fl c
+show the delta of the event counters.
+.It Fl u
+Userland processes are also included in the profiling.
+.El
 .El
 .Sh EXAMPLES
 The following command profiles the system during 20 seconds and writes the
diff -r 35f3ac118c2a -r 768dd7a3a89d usr.sbin/tprof/tprof.c
--- a/usr.sbin/tprof/tprof.c    Thu Dec 01 00:41:10 2022 +0000
+++ b/usr.sbin/tprof/tprof.c    Thu Dec 01 00:43:27 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tprof.c,v 1.15 2022/12/01 00:40:05 ryo Exp $   */
+/*     $NetBSD: tprof.c,v 1.16 2022/12/01 00:43:27 ryo Exp $   */
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -57,7 +57,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: tprof.c,v 1.15 2022/12/01 00:40:05 ryo Exp $");
+__RCSID("$NetBSD: tprof.c,v 1.16 2022/12/01 00:43:27 ryo Exp $");
 #endif /* not lint */
 
 #include <sys/atomic.h>
@@ -111,6 +111,7 @@
        { "monitor",    true,  false, tprof_monitor },
        { "count",      true,  false, tprof_count },
        { "analyze",    true,  true,  tprof_analyze },
+       { "top",        true,  true,  tprof_top },
        { NULL,         false, false, NULL },
 };
 
@@ -122,7 +123,8 @@
        fprintf(stderr, "\n");
        fprintf(stderr, "\tlist\n");
        fprintf(stderr, "\t\tList the available events.\n");
-       fprintf(stderr, "\tmonitor -e name[:option] [-e ...] [-o outfile] command\n");
+       fprintf(stderr, "\tmonitor -e name[:option] [-e ...] [-o outfile]"
+           " command\n");
        fprintf(stderr, "\t\tMonitor the event 'name' with option 'option'\n"
            "\t\tcounted during the execution of 'command'.\n");
        fprintf(stderr, "\tcount -e name[:option] [-e ...] [-i interval]"
@@ -131,7 +133,8 @@
            " only outputs a counter.\n");
        fprintf(stderr, "\tanalyze [-CkLPs] [-p pid] file\n");
        fprintf(stderr, "\t\tAnalyze the samples of the file 'file'.\n");
-
+       fprintf(stderr, "\ttop [-e name [-e ...]] [-i interval] [-u]\n");
+       fprintf(stderr, "\t\tDisplay profiling results in real-time.\n");
        exit(EXIT_FAILURE);
 }
 
@@ -257,6 +260,7 @@
 static void
 tprof_list(int argc, char **argv)
 {
+       printf("%u events can be counted at the same time\n", ncounters);
        tprof_event_list();
 }
 
@@ -309,7 +313,9 @@
                        nevent++;
                        if (nevent > __arraycount(params) ||
                            nevent > ncounters)
-                               errx(EXIT_FAILURE, "Too many events");
+                               errx(EXIT_FAILURE, "Too many events. Only a"
+                                   " maximum of %d counters can be used.",
+                                   ncounters);
                        break;
                default:
                        usage();
@@ -333,8 +339,10 @@
                if (do_profile)
                        params[i].p_flags |= TPROF_PARAM_PROFILE;
                ret = ioctl(devfd, TPROF_IOC_CONFIGURE_EVENT, &params[i]);
-               if (ret == -1)
-                       err(EXIT_FAILURE, "TPROF_IOC_CONFIGURE_EVENT");
+               if (ret == -1) {
+                       err(EXIT_FAILURE, "TPROF_IOC_CONFIGURE_EVENT: %s",
+                           eventname[i]);
+               }
        }
 
        ret = ioctl(devfd, TPROF_IOC_START, &mask);
@@ -404,7 +412,8 @@
                fprintf(stderr, "\tbuf %" PRIu64 "\n", ts.ts_buf);
                fprintf(stderr, "\temptybuf %" PRIu64 "\n", ts.ts_emptybuf);
                fprintf(stderr, "\tdropbuf %" PRIu64 "\n", ts.ts_dropbuf);
-               fprintf(stderr, "\tdropbuf_sample %" PRIu64 "\n", ts.ts_dropbuf_sample);
+               fprintf(stderr, "\tdropbuf_sample %" PRIu64 "\n",
+                   ts.ts_dropbuf_sample);
 
                fprintf(stderr, "\n");
        }
diff -r 35f3ac118c2a -r 768dd7a3a89d usr.sbin/tprof/tprof.h
--- a/usr.sbin/tprof/tprof.h    Thu Dec 01 00:41:10 2022 +0000
+++ b/usr.sbin/tprof/tprof.h    Thu Dec 01 00:43:27 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tprof.h,v 1.2 2018/07/13 11:03:36 maxv Exp $   */
+/*     $NetBSD: tprof.h,v 1.3 2022/12/01 00:43:27 ryo Exp $    */
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -29,8 +29,14 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+extern struct tprof_info tprof_info;
+extern int ncpu;
+extern int devfd;
+extern u_int ncounters;
+
 int tprof_event_init(uint32_t);
 void tprof_event_list(void);
 void tprof_event_lookup(const char *, struct tprof_param *);
 
 void tprof_analyze(int, char **);
+void tprof_top(int, char **);
diff -r 35f3ac118c2a -r 768dd7a3a89d usr.sbin/tprof/tprof_analyze.c
--- a/usr.sbin/tprof/tprof_analyze.c    Thu Dec 01 00:41:10 2022 +0000
+++ b/usr.sbin/tprof/tprof_analyze.c    Thu Dec 01 00:43:27 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tprof_analyze.c,v 1.7 2022/12/01 00:41:10 ryo Exp $    */
+/*     $NetBSD: tprof_analyze.c,v 1.8 2022/12/01 00:43:27 ryo Exp $    */
 
 /*
  * Copyright (c) 2010,2011,2012 YAMAMOTO Takashi,
@@ -28,7 +28,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: tprof_analyze.c,v 1.7 2022/12/01 00:41:10 ryo Exp $");
+__RCSID("$NetBSD: tprof_analyze.c,v 1.8 2022/12/01 00:43:27 ryo Exp $");
 #endif /* not lint */
 
 #include <assert.h>
@@ -192,7 +192,7 @@
                errx(EXIT_FAILURE, "fopen");
        }
 



Home | Main Index | Thread Index | Old Index