Page MenuHomeFreeBSD

D58923.id.diff
No OneTemporary

D58923.id.diff

diff --git a/usr.sbin/pmc/cmd_pmc_system.cc b/usr.sbin/pmc/cmd_pmc_system.cc
new file mode 100644
--- /dev/null
+++ b/usr.sbin/pmc/cmd_pmc_system.cc
@@ -0,0 +1,235 @@
+/*-
+ * 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 <map>
+#include <set>
+#include <string>
+#include <unordered_map>
+#include <unordered_set>
+#include <vector>
+
+#include "display.hh"
+#include "view.hh"
+
+static int sortcol = 1;
+
+struct exprocinfo {
+ exprocinfo() : pid(-1), name(), ucounts(), scounts(),
+ counts() { }
+ exprocinfo(int p, std::string n) : pid(p), name(n),
+ ucounts(), scounts(), counts() { }
+ ~exprocinfo() { }
+ int pid;
+ std::string name;
+ std::unordered_map<uint64_t, uint64_t> ucounts;
+ std::unordered_map<uint64_t, uint64_t> scounts;
+ std::unordered_map<uint64_t, uint64_t> counts;
+};
+
+class system_view : public pmcview
+{
+public:
+ virtual void
+ proccreate(pid_t pid)
+ {
+ std::string pname = pidtoname(pid);
+
+ if (eventmap.find(pname) == eventmap.end())
+ eventmap[pname] = exprocinfo(pid, pname);
+ }
+
+ virtual void
+ procexec(pid_t pid)
+ {
+ std::string pname = pidtoname(pid);
+
+ if (eventmap.find(pname) == eventmap.end())
+ eventmap[pname] = exprocinfo(pid, pname);
+ }
+
+ virtual void
+ callchain(struct pmclog_ev_callchain &p)
+ {
+ uint64_t eventid = pmcidtoeventid(p.pl_pmcid);
+ int usermode = (PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(p.pl_cpuflags) != 0);
+
+ pmcinfo[eventid].count++;
+
+ std::string pname = pidtoname(p.pl_pid);
+ auto &exinfo = eventmap[pname];
+
+ if (usermode)
+ exinfo.ucounts[eventid]++;
+ else
+ exinfo.scounts[eventid]++;
+
+ exinfo.counts[eventid]++;
+ }
+
+ virtual void
+ print()
+ {
+ table t = table();
+ t.addcolumn("Program", true);
+ for (auto &p : pmcinfo)
+ t.addcolumn(p.second.name);
+
+ for (auto &kv : eventmap) {
+ bool nonzero = false;
+ std::vector<field> r;
+
+ r.emplace_back(kv.second.name);
+ 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);
+ t.print();
+ }
+
+protected:
+ std::unordered_map<std::string, exprocinfo> eventmap;
+};
+
+static struct option longopts[] = {
+ PMCFILTER_LOPTS,
+ { "sort", required_argument, NULL, 's' },
+ { NULL, 0, NULL, 0 }
+};
+
+static void
+usage(void)
+{
+ printf("Usage: pmc system [options] [pmclog]\n\n");
+ printf("Display a per-process summary\n\n");
+ printf("Options:\n");
+ printf("\t--sort Sort by event\n\n");
+ PMCFILTER_PRINTOPTS();
+}
+
+int
+cmd_pmc_system(int argc, char **argv)
+{
+ struct pmcfilter filter = pmcfilter();
+ const char *logfile = "default.log";
+ int option, logfd;
+
+ while ((option = getopt_long(argc, argv, PMCFILTER_SOPTS "s:", longopts, NULL)) != -1) {
+ switch (option) {
+ case 's':
+ sortcol = atoi(optarg);
+ break;
+ PMCFILTER_CASE(filter);
+ case '?':
+ default:
+ usage();
+ }
+ }
+ argc -= optind;
+ argv += optind;
+ if (argc > 1) {
+ usage();
+ exit(EX_USAGE);
+ }
+ if (argc == 1)
+ logfile = argv[0];
+
+ setup_screen();
+
+ if ((logfd = open(logfile, O_RDONLY)) < 0) {
+ errx(EX_OSERR, "ERROR: Cannot open \"%s\" for reading: %s.", argv[0],
+ strerror(errno));
+ return (EX_NOINPUT);
+ }
+
+ system_view s = system_view();
+ s.setfilter(filter);
+ s.process(logfd);
+ s.print();
+
+ close(logfd);
+
+ return (EX_OK);
+}

File Metadata

Mime Type
text/plain
Expires
Fri, Sep 11, 12:27 AM (21 h, 10 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
36963752
Default Alt Text
D58923.id.diff (5 KB)

Event Timeline