Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178104435
D58929.1795974049.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
8 KB
Referenced Files
None
Subscribers
None
D58929.1795974049.diff
View Options
diff --git a/usr.sbin/pmc/Makefile b/usr.sbin/pmc/Makefile
--- a/usr.sbin/pmc/Makefile
+++ b/usr.sbin/pmc/Makefile
@@ -14,7 +14,7 @@
cmd_pmc_summary.cc \
cmd_pmc_annotate.cc cmd_pmc_branch.cc \
cmd_pmc_frontend.cc cmd_pmc_ibs.cc cmd_pmc_info.cc \
- cmd_pmc_program.cc cmd_pmc_record.cc \
+ cmd_pmc_memory.cc cmd_pmc_program.cc cmd_pmc_record.cc \
cmd_pmc_system.cc \
display.cc util.cc view.cc objdump.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
@@ -48,6 +48,7 @@
int cmd_pmc_filter(int, char **);
int cmd_pmc_frontend(int, char **);
int cmd_pmc_list_events(int, char **);
+ int cmd_pmc_memory(int, char **);
int cmd_pmc_program(int, char **);
int cmd_pmc_record(int, char **);
int cmd_pmc_stat(int, char **);
diff --git a/usr.sbin/pmc/cmd_pmc_memory.cc b/usr.sbin/pmc/cmd_pmc_memory.cc
new file mode 100644
--- /dev/null
+++ b/usr.sbin/pmc/cmd_pmc_memory.cc
@@ -0,0 +1,270 @@
+/*-
+ * 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"
+
+static int sortcol = 2;
+
+struct memory {
+ syminfo func;
+ int64_t cycles;
+ int64_t tlbcycles;
+ int64_t dccycles;
+ int64_t openmemreqs;
+ int64_t samples;
+};
+
+class memory_view : public pmcview
+{
+public:
+ memory_view() : pmcview(), samples(), tlbrefill(), dcmiss(), openreq() { }
+ ~memory_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 loads */
+ if ((o.data3 & IBS_OP_DATA3_LOAD) == 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 m = samples.find(o.rip);
+ if (m == samples.end()) {
+ syminfo sym = addrtosymbol(usermode ? p.pl_pid : 0, o.rip);
+ if (sym.binary == "")
+ return;
+
+ samples[o.rip] = memory();
+ samples[o.rip].func = sym;
+
+ m = samples.find(o.rip);
+ }
+
+ /*
+ * Breakdown the IBS memory statistics
+ */
+ m->second.cycles += IBS_OP_DATA_TO_TAGTORET(o.data);
+ m->second.tlbcycles += IBS_OP_DATA3_TO_TLBREFILLLAT(o.data3);
+ tlbrefill.addsample(IBS_OP_DATA3_TO_TLBREFILLLAT(o.data3));
+ m->second.dccycles += IBS_OP_DATA3_TO_DCLAT(o.data3);
+ dcmiss.addsample(IBS_OP_DATA3_TO_DCLAT(o.data3));
+ m->second.openmemreqs += IBS_OP_DATA3_TO_OPENMEMREQS(o.data3);
+ openreq.addsample(IBS_OP_DATA3_TO_OPENMEMREQS(o.data3));
+ m->second.samples += 1;
+ }
+
+ virtual void
+ print(bool interactive)
+ {
+ title("IBS Memory Analysis");
+
+ // Print header
+ table t = table();
+ t.addcolumn("Image", true);
+ t.addcolumn("Function", true);
+ t.addcolumn("Total Cycles");
+ t.addcolumn("Load Latency");
+ t.addcolumn("L1 TLB Latency");
+ t.addcolumn("DC Miss Latency");
+ t.addcolumn("Outstanding Reqs.");
+
+ for (auto &kv : samples) {
+ std::vector<field> r;
+
+ if (kv.second.dccycles / kv.second.samples < 5)
+ continue;
+
+ r.emplace_back(basename(kv.second.func.binary));
+ r.emplace_back(kv.second.func.to_string());
+ r.emplace_back(kv.second.cycles);
+ r.emplace_back(kv.second.cycles / kv.second.samples);
+ r.emplace_back(kv.second.dccycles / kv.second.samples);
+ r.emplace_back(kv.second.tlbcycles / kv.second.samples);
+ r.emplace_back(kv.second.openmemreqs / kv.second.samples);
+
+ t.addrow(r);
+ }
+
+ t.sort(sortcol);
+ if (interactive) {
+ t.display("Memory Access Analyzer");
+ } else {
+ t.print();
+
+ header("TLB Refill Latency");
+ tlbrefill.setxlabel("Latency (Cycles)");
+ tlbrefill.setylabel("Frequency Log");
+ tlbrefill.print();
+
+ header("DC Miss Latency");
+ dcmiss.setxlabel("Latency (Cycles)");
+ dcmiss.setylabel("Frequency Log");
+ dcmiss.print();
+
+ header("Open Memory Requests");
+ openreq.setxlabel("Open Requests");
+ openreq.setylabel("Frequency Log");
+ openreq.print();
+ }
+ }
+private:
+ std::unordered_map<uint64_t, memory> samples;
+ histogram tlbrefill;
+ histogram dcmiss;
+ histogram openreq;
+};
+
+
+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 memory [options] [pmclog]\n\n");
+ printf("Analyze memory operations\n\n");
+ printf("Options:\n");
+ printf("\t-F,--format Output format (console, tui)\n");
+ printf("\t-s Sort by column\n\n");
+ PMCFILTER_PRINTOPTS();
+}
+
+int
+cmd_pmc_memory(int argc, char **argv)
+{
+ pmcfilter filter = pmcfilter();
+ const char *logfile = "default.log";
+ int option, logfd;
+ std::string format;
+
+ while ((option = getopt_long(argc, argv, PMCFILTER_SOPTS"F:s:", 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));
+ }
+
+ memory_view v = memory_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
@@ -68,6 +68,7 @@
{ "ibs", cmd_pmc_ibs, "Breakdown IBS statistics" },
{ "info", cmd_pmc_info, "Display high level information about a log" },
{ "list-events", cmd_pmc_list_events, "List available PMC events" },
+ { "memory", cmd_pmc_memory, "Show most expensive memory operations" },
{ "program", cmd_pmc_program, "Show a summary of all functions" },
{ "record", cmd_pmc_record, "Record a performance sample" },
{ "stat", cmd_pmc_stat, NULL },
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Nov 29, 5:40 PM (15 h, 25 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42093631
Default Alt Text
D58929.1795974049.diff (8 KB)
Attached To
Mode
D58929: pmc: Analyze high latency memory accesses
Attached
Detach File
Event Timeline