Page MenuHomeFreeBSD

D58924.1795555950.diff
No OneTemporary

Authored By
Unknown
Size
10 KB
Referenced Files
None
Subscribers
None

D58924.1795555950.diff

diff --git a/usr.sbin/pmc/Makefile b/usr.sbin/pmc/Makefile
--- a/usr.sbin/pmc/Makefile
+++ b/usr.sbin/pmc/Makefile
@@ -13,6 +13,7 @@
cmd_pmc_list.c cmd_pmc_filter.cc \
cmd_pmc_summary.cc \
cmd_pmc_frontend.cc cmd_pmc_record.cc cmd_pmc_info.cc \
+ cmd_pmc_program.cc \
cmd_pmc_system.cc \
display.cc util.cc view.cc
diff --git a/usr.sbin/pmc/cmd_pmc.h b/usr.sbin/pmc/cmd_pmc.h
--- a/usr.sbin/pmc/cmd_pmc.h
+++ b/usr.sbin/pmc/cmd_pmc.h
@@ -45,6 +45,7 @@
int cmd_pmc_filter(int, char **);
int cmd_pmc_frontend(int, char **);
int cmd_pmc_list_events(int, char **);
+ int cmd_pmc_program(int, char **);
int cmd_pmc_record(int, char **);
int cmd_pmc_stat(int, char **);
int cmd_pmc_stat_system(int, char **);
diff --git a/usr.sbin/pmc/cmd_pmc.h b/usr.sbin/pmc/cmd_pmc_program.hh
copy from usr.sbin/pmc/cmd_pmc.h
copy to usr.sbin/pmc/cmd_pmc_program.hh
--- a/usr.sbin/pmc/cmd_pmc.h
+++ b/usr.sbin/pmc/cmd_pmc_program.hh
@@ -1,7 +1,10 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
- * Copyright (c) 2018, Matthew Macy
+ * Copyright (c) 2026, Netflix, Inc.
+ *
+ * This software was developed by Ali Mashtizadeh under the sponsorship from
+ * Netflix, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -25,38 +28,28 @@
* SUCH DAMAGE.
*
*/
-#ifndef _CMD_PMC_H_
-#define _CMD_PMC_H_
-
-#define DEFAULT_DISPLAY_HEIGHT 256 /* file virtual height */
-#define DEFAULT_DISPLAY_WIDTH 1024 /* file virtual width */
-extern int pmc_displayheight;
-extern int pmc_displaywidth;
-extern int pmc_kq;
-extern struct pmcstat_args pmc_args;
-
-typedef int (*cmd_disp_t)(int, char **);
+struct funcinfo
+{
+ std::string binary;
+ std::string func;
+ std::unordered_map<uint64_t, uint64_t> ucounts;
+ std::unordered_map<uint64_t, uint64_t> scounts;
+ std::unordered_map<uint64_t, uint64_t> counts;
+};
-#if defined(__cplusplus)
-extern "C" {
-#endif
- int cmd_pmc_info(int, char **);
- int cmd_pmc_filter(int, char **);
- int cmd_pmc_frontend(int, char **);
- int cmd_pmc_list_events(int, char **);
- int cmd_pmc_record(int, char **);
- int cmd_pmc_stat(int, char **);
- int cmd_pmc_stat_system(int, char **);
- int cmd_pmc_summary(int, char **);
- int cmd_pmc_system(int, char **);
-#if defined(__cplusplus)
+class program_view : public pmcview
+{
+public:
+ program_view();
+ ~program_view();
+ void addsample(const syminfo &sym, uint64_t eventid, bool user);
+ funcinfo lookup(const std::string &key);
+ virtual void callchain(struct pmclog_ev_callchain &p, uintfptr_t *cc, __unused int len);
+ virtual void selectrow(std::vector<field> r);
+ virtual void print(bool interactive);
+protected:
+ uint64_t total = 0;
+ std::unordered_map<std::string, funcinfo> samples = std::unordered_map<std::string, funcinfo>();
};
-#endif
-int pmc_util_get_pid(struct pmcstat_args *);
-void pmc_util_start_pmcs(struct pmcstat_args *);
-void pmc_util_cleanup(struct pmcstat_args *);
-void pmc_util_shutdown_logging(struct pmcstat_args *args);
-void pmc_util_kill_process(struct pmcstat_args *args);
-#endif
diff --git a/usr.sbin/pmc/cmd_pmc_program.cc b/usr.sbin/pmc/cmd_pmc_program.cc
new file mode 100644
--- /dev/null
+++ b/usr.sbin/pmc/cmd_pmc_program.cc
@@ -0,0 +1,275 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026, Netflix, Inc.
+ *
+ * This software was developed by Ali Mashtizadeh under the sponsorship from
+ * Netflix, Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ */
+
+#include <sys/param.h>
+#include <sys/cpuset.h>
+#include <sys/event.h>
+#include <sys/queue.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <sys/time.h>
+#include <sys/ttycom.h>
+#include <sys/user.h>
+#include <sys/wait.h>
+
+#include <assert.h>
+#include <curses.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <kvm.h>
+#include <libgen.h>
+#include <limits.h>
+#include <locale.h>
+#include <math.h>
+#include <pmc.h>
+#include <pmclog.h>
+#include <regex.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <string.h>
+#include <sysexits.h>
+#include <unistd.h>
+
+#include "cmd_pmc.h"
+
+#include <iostream>
+#include <filesystem>
+#include <map>
+#include <set>
+#include <string>
+#include <unordered_map>
+#include <unordered_set>
+#include <vector>
+
+#include "display.hh"
+#include "view.hh"
+#include "cmd_pmc_annotate.hh"
+#include "cmd_pmc_program.hh"
+
+static int sortcol = 2;
+
+program_view::program_view() : total(), samples()
+{
+}
+
+program_view::~program_view()
+{
+}
+
+void
+program_view::addsample(const syminfo &sym, uint64_t eventid, bool user)
+{
+ /*
+ * Here we use the basename of the binary as our key so we can easily
+ * lookup rows from the ncurses view.
+ */
+ std::string key = basename(sym.binary) + "!!" + sym.name;
+
+ pmcinfo[eventid].count++;
+
+ auto e = samples.find(key);
+ if (e == samples.end()) {
+ samples[key] = funcinfo({ sym.binary, sym.name, {}, {}, {} });
+ e = samples.find(key);
+ }
+
+ if (user)
+ e->second.ucounts[eventid]++;
+ else
+ e->second.scounts[eventid]++;
+
+ e->second.counts[eventid]++;
+}
+
+funcinfo
+program_view::lookup(const std::string &key)
+{
+ auto e = samples.find(key);
+ if (e == samples.end())
+ return funcinfo();
+ else
+ return e->second;
+}
+
+void
+program_view::callchain(struct pmclog_ev_callchain &p, uintfptr_t *cc, __unused int len)
+{
+ uint64_t eventid = pmcidtoeventid(p.pl_pmcid);
+ int usermode = (PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(p.pl_cpuflags) != 0);
+
+ syminfo sym = addrtosymbol(usermode ? p.pl_pid : 0, cc[0]);
+ if (sym.binary != "") {
+ addsample(sym, eventid, usermode != 0);
+ }
+}
+
+void
+program_view::selectrow(std::vector<field> r)
+{
+ funcinfo info = lookup(r[0].to_string() + "!!" + r[1].to_string());
+
+ if (info.binary == "") {
+ message_box("Unknown binary!", "Alert");
+ return;
+ }
+ if (!std::filesystem::exists(info.binary)) {
+ message_box("File not found!", "Alert");
+ return;
+ }
+
+ annotate_view s = annotate_view(info.binary, info.func);
+
+ // Add the selected program to the current filter
+ s.setfilter(filter);
+
+ lseek(logfd, 0, SEEK_SET);
+ s.process(logfd);
+
+ s.print(/* interactive */true);
+}
+
+void
+program_view::print(bool interactive)
+{
+ // Print header
+ table t = table();
+ t.addcolumn("Image", true);
+ t.addcolumn("Function", true);
+ for (auto &p : pmcinfo)
+ t.addcolumn(p.second.name);
+
+ for (auto &kv : samples) {
+ bool nonzero = false;
+ std::vector<field> r;
+
+ r.emplace_back(basename(kv.second.binary));
+ r.emplace_back(kv.second.func);
+ for (const auto &e : pmcinfo) {
+ if (kv.second.counts[e.second.event] != 0)
+ nonzero = true;
+ if (filter.kernelonly || filter.useronly) {
+ r.emplace_back(
+ kv.second.counts[e.second.event] * e.second.rate,
+ e.second.count * e.second.rate);
+ } else {
+ r.emplace_back(
+ kv.second.ucounts[e.second.event] * e.second.rate,
+ e.second.count * e.second.rate,
+ kv.second.scounts[e.second.event] * e.second.rate,
+ e.second.count * e.second.rate);
+ }
+ }
+ if (nonzero)
+ t.addrow(r);
+ }
+
+ t.sort(sortcol);
+ if (interactive)
+ t.display("Program View",
+ [this](std::vector<field> r){ this->selectrow(r); });
+ else
+ t.print();
+}
+
+static struct option longopts[] = {
+ PMCFILTER_LOPTS,
+ { "format", required_argument, NULL, 'F' },
+ { "sort", required_argument, NULL, 's' },
+ { NULL, 0, NULL, 0 }
+};
+
+static void
+usage(void)
+{
+ printf("Usage: pmc program [options] [pmclog]\n\n");
+ printf("Display a per-function summary\n\n");
+ printf("Options:\n");
+ printf("\t-F,--format Output format (console, tui)\n");
+ printf("\t-s,--sort Sort by event\n\n");
+ PMCFILTER_PRINTOPTS();
+}
+
+int
+cmd_pmc_program(int argc, char **argv)
+{
+ struct pmcfilter filter = pmcfilter();
+ const char *logfile = "default.log";
+ int option, logfd;
+ std::string format;
+
+ while ((option = getopt_long(argc, argv, "F:s:" PMCFILTER_SOPTS, longopts, NULL)) != -1) {
+ switch (option) {
+ PMCFILTER_CASE(filter);
+ case 'F':
+ format = optarg;
+ break;
+ case 's':
+ sortcol = atoi(optarg);
+ break;
+ case '?':
+ default:
+ usage();
+ exit(EX_USAGE);
+ }
+ }
+ argc -= optind;
+ argv += optind;
+ if (argc > 1) {
+ usage();
+ exit(EX_USAGE);
+ }
+ if (argc == 1)
+ logfile = argv[0];
+
+ bool interactive = display_getmode(format, {"console", "tui"}) == "tui";
+ setup_screen(interactive);
+
+ if ((logfd = open(logfile, O_RDONLY)) < 0) {
+ errx(EX_OSERR, "ERROR: Cannot open \"%s\" for reading: %s.", argv[0],
+ strerror(errno));
+ }
+
+ program_view v = program_view();
+ v.setfilter(filter);
+ v.process(logfd);
+ v.print(interactive);
+
+ close(logfd);
+
+ return (EX_OK);
+}
diff --git a/usr.sbin/pmc/pmc.c b/usr.sbin/pmc/pmc.c
--- a/usr.sbin/pmc/pmc.c
+++ b/usr.sbin/pmc/pmc.c
@@ -65,6 +65,7 @@
{ "frontend", cmd_pmc_frontend, "Analyze front-end stalls" },
{ "info", cmd_pmc_info, "Display high level information about a log" },
{ "list-events", cmd_pmc_list_events, "List available PMC events" },
+ { "program", cmd_pmc_program, "Show a summary of all functions" },
{ "record", cmd_pmc_record, "Record a performance sample" },
{ "stat", cmd_pmc_stat, NULL },
{ "stat-system", cmd_pmc_stat_system, NULL },

File Metadata

Mime Type
text/plain
Expires
Tue, Nov 24, 9:32 PM (6 h, 13 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41964388
Default Alt Text
D58924.1795555950.diff (10 KB)

Event Timeline