Page MenuHomeFreeBSD

D58925.1796009744.diff
No OneTemporary

Authored By
Unknown
Size
7 KB
Referenced Files
None
Subscribers
None

D58925.1796009744.diff

diff --git a/usr.sbin/pmc/Makefile b/usr.sbin/pmc/Makefile
--- a/usr.sbin/pmc/Makefile
+++ b/usr.sbin/pmc/Makefile
@@ -12,6 +12,7 @@
SRCS= pmc.c pmc_util.c cmd_pmc_stat.c \
cmd_pmc_list.c cmd_pmc_filter.cc \
cmd_pmc_summary.cc \
+ cmd_pmc_branch.cc \
cmd_pmc_frontend.cc cmd_pmc_record.cc cmd_pmc_info.cc \
cmd_pmc_program.cc \
cmd_pmc_system.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
@@ -41,6 +41,7 @@
#if defined(__cplusplus)
extern "C" {
#endif
+ int cmd_pmc_branch(int, char **);
int cmd_pmc_info(int, char **);
int cmd_pmc_filter(int, char **);
int cmd_pmc_frontend(int, char **);
diff --git a/usr.sbin/pmc/cmd_pmc_branch.cc b/usr.sbin/pmc/cmd_pmc_branch.cc
new file mode 100644
--- /dev/null
+++ b/usr.sbin/pmc/cmd_pmc_branch.cc
@@ -0,0 +1,266 @@
+/*-
+ * 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 <libpmcstat.h>
+#include "cmd_pmc.h"
+
+#include <iostream>
+#include <map>
+#include <set>
+#include <string>
+#include <unordered_map>
+#include <unordered_set>
+#include <vector>
+
+#include <dev/hwpmc/hwpmc_ibs.h>
+#include "display.hh"
+#include "view.hh"
+
+struct branch {
+ syminfo func;
+ uintfptr_t source;
+ uintfptr_t targets[16];
+ int64_t retired;
+ int64_t taken;
+ int64_t mispredict;
+ int64_t misptaken;
+};
+
+static int sortcol = 2;
+
+class branch_view : public pmcview
+{
+public:
+ branch_view() : pmcview(), branches(), tagtoret(), tagtonret() { }
+ ~branch_view() { }
+
+ virtual void
+ callchain(struct pmclog_ev_callchain &p,
+ ibsopinfo &o, __unused uintfptr_t *cc, __unused int len)
+ {
+ int usermode = PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(p.pl_cpuflags);
+
+ /* We only care about retired branches */
+ if ((o.data & IBS_OP_DATA_BRANCHRETIRED) == 0)
+ return;
+
+ /*
+ * In my testing it is rare to see an invalid RIP so we just
+ * discard this silently.
+ */
+ if ((o.data & IBS_OP_DATA_RIPINVALID) != 0)
+ return;
+
+ auto br = branches.find(o.rip);
+ if (br == branches.end()) {
+ syminfo sym = addrtosymbol(usermode ? p.pl_pid : 0, o.rip);
+ if (sym.binary == "")
+ return;
+
+ branches[o.rip] = branch();
+ branches[o.rip].func = sym;
+ }
+
+ /*
+ * Breakdown the IBS Op branch statistics
+ */
+ if (o.data & IBS_OP_DATA_BRANCHRETIRED)
+ branches[o.rip].retired += 1;
+ if (o.data & IBS_OP_DATA_BRANCHTAKEN)
+ branches[o.rip].taken += 1;
+ if (o.data & IBS_OP_DATA_BRANCHMISPREDICTED)
+ branches[o.rip].mispredict += 1;
+
+ if (o.data & IBS_OP_DATA_BRANCHRETIRED)
+ tagtoret.addsample(IBS_OP_DATA_TO_TAGTORET(o.data));
+ if (o.data & IBS_OP_DATA_BRANCHMISPREDICTED)
+ tagtonret.addsample(IBS_OP_DATA_TO_TAGTORET(o.data));
+
+ }
+
+ virtual void
+ print(bool interactive)
+ {
+ title("IBS Branch Analysis");
+
+ // Print header
+ table t = table();
+ t.addcolumn("Image", true);
+ t.addcolumn("Function", true);
+ t.addcolumn("Misp.");
+ t.addcolumn("Retired");
+ t.addcolumn("% Misp.");
+ t.addcolumn("% Taken");
+
+ for (auto &kv : branches) {
+ std::vector<field> r;
+
+ if (kv.second.mispredict < 5)
+ continue;
+
+ r.emplace_back(basename(kv.second.func.binary));
+ r.emplace_back(kv.second.func.to_string());
+ r.emplace_back(kv.second.mispredict);
+ r.emplace_back(kv.second.retired);
+ r.emplace_back(kv.second.mispredict, kv.second.retired, true);
+ r.emplace_back(kv.second.taken, kv.second.retired, true);
+
+ t.addrow(r);
+ }
+
+ t.sort(sortcol);
+ if (interactive) {
+ t.display("Branch Analysis");
+ } else {
+ t.print();
+
+ // Experiment
+ header("Tag to Retire (Retired Branches)");
+ tagtoret.setxlabel("Latency (Cycles)");
+ tagtoret.setylabel("Frequency Log");
+ tagtoret.print();
+
+ header("Tag to Retire (Misp Branches)");
+ tagtonret.setxlabel("Latency (Cycles)");
+ tagtonret.setylabel("Frequency Log");
+ tagtonret.print();
+ }
+ }
+private:
+ std::unordered_map<uint64_t, branch> branches;
+ histogram tagtoret;
+ histogram tagtonret;
+};
+
+
+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 branch [options] [pmclog]\n\n");
+ printf("Analyze branch mispredictions\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_branch(int argc, char **argv)
+{
+ 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));
+ }
+
+ branch_view v = branch_view();
+ v.setfilter(filter);
+ v.process(logfd);
+ v.print(interactive);
+
+ close(logfd);
+
+ return (0);
+}
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
@@ -61,6 +61,7 @@
};
static struct cmd_handler disp_table[] = {
+ { "branch", cmd_pmc_branch, "Analyze branch misprediction" },
{ "filter", cmd_pmc_filter, NULL },
{ "frontend", cmd_pmc_frontend, "Analyze front-end stalls" },
{ "info", cmd_pmc_info, "Display high level information about a log" },

File Metadata

Mime Type
text/plain
Expires
Mon, Nov 30, 3:35 AM (22 h, 1 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42099937
Default Alt Text
D58925.1796009744.diff (7 KB)

Event Timeline