2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018, Matthew Macy
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/cpuset.h>
34 #include <sys/event.h>
35 #include <sys/queue.h>
36 #include <sys/socket.h>
38 #include <sys/sysctl.h>
40 #include <sys/ttycom.h>
68 #include <libpmcstat.h>
74 #include <unordered_map>
76 using std::unordered_map;
77 typedef unordered_map <int, std::string> idmap;
78 typedef unordered_map <uint32_t, uint64_t> intmap;
79 typedef unordered_map <std::string, intmap> strintmap;
80 typedef std::pair<uint64_t, uint32_t> sampleid;
81 typedef std::pair<uint64_t, std::string> samplename;
82 typedef unordered_map <uint32_t, std::vector<samplename>> eventcountmap;
84 #define P_KPROC 0x00004 /* Kernel process. */
90 "\t summarize log file\n"
91 "\t -k <k>, --topk <k> show topk processes for each counter\n"
96 pmc_summary_handler(int logfd, int k, bool do_full)
98 struct pmclog_parse_state *ps;
100 idmap pidmap, tidmap, eventnamemap;
101 strintmap tideventmap, pideventmap;
102 intmap eventmap, pmcidmap, ratemap;
103 intmap kerntidmap, kernpidmap;
104 eventcountmap countmap;
106 ps = static_cast<struct pmclog_parse_state*>(pmclog_open(logfd));
108 errx(EX_OSERR, "ERROR: Cannot allocate pmclog parse state: %s\n",
110 while (pmclog_read(ps, &ev) == 0) {
111 if (ev.pl_type == PMCLOG_TYPE_PMCALLOCATE) {
112 pmcidmap[ev.pl_u.pl_a.pl_pmcid] = ev.pl_u.pl_a.pl_event;
113 ratemap[ev.pl_u.pl_a.pl_event] = ev.pl_u.pl_a.pl_rate;
114 eventnamemap[ev.pl_u.pl_a.pl_event] = ev.pl_u.pl_a.pl_evname;
116 if (ev.pl_type == PMCLOG_TYPE_THR_CREATE) {
117 tidmap[ev.pl_u.pl_tc.pl_tid] = ev.pl_u.pl_tc.pl_tdname;
118 kerntidmap[ev.pl_u.pl_tc.pl_tid] = !!(ev.pl_u.pl_tc.pl_flags & P_KPROC);
119 if (tideventmap.find(ev.pl_u.pl_tc.pl_tdname) == tideventmap.end())
120 tideventmap[ev.pl_u.pl_tc.pl_tdname] = intmap();
122 if (ev.pl_type == PMCLOG_TYPE_PROC_CREATE) {
123 pidmap[ev.pl_u.pl_pc.pl_pid] = ev.pl_u.pl_pc.pl_pcomm;
124 kernpidmap[ev.pl_u.pl_pc.pl_pid] = !!(ev.pl_u.pl_pc.pl_flags & P_KPROC);
125 if (pideventmap.find(ev.pl_u.pl_pc.pl_pcomm) == pideventmap.end())
126 pideventmap[ev.pl_u.pl_pc.pl_pcomm] = intmap();
128 if (ev.pl_type == PMCLOG_TYPE_CALLCHAIN) {
129 auto event = pmcidmap[ev.pl_u.pl_cc.pl_pmcid];
134 auto tidname = tidmap.find(ev.pl_u.pl_cc.pl_tid);
135 auto pidname = pidmap.find(ev.pl_u.pl_cc.pl_pid);
136 if (tidname != tidmap.end()) {
137 auto &teventmap = tideventmap[tidname->second];
140 if (pidname != pidmap.end()) {
141 auto &peventmap = pideventmap[pidname->second];
146 for (auto &pkv : pideventmap)
147 for (auto &ekv : pkv.second) {
148 auto &samplevec = countmap[ekv.first];
149 samplevec.emplace_back(ekv.second, pkv.first);
151 for (auto &kv : countmap)
152 std::sort(kv.second.begin(), kv.second.end(), [](auto &a, auto &b) {return (a.first < b.first);});
154 for (auto &kv : countmap) {
155 auto &name = eventnamemap[kv.first];
156 auto rate = ratemap[kv.first];
157 std::cout << "idx: " << kv.first << " name: " << name << " rate: " << rate << std::endl;
158 while (!kv.second.empty()) {
159 auto &val = kv.second.back();
160 kv.second.pop_back();
161 std::cout << val.second << ": " << val.first << std::endl;
166 for (auto &kv : countmap) {
167 auto &name = eventnamemap[kv.first];
168 auto rate = ratemap[kv.first];
169 std::cout << name << ":" << std::endl;
170 for (auto i = 0; i < k; i++) {
171 auto largest = kv.second.back();
172 kv.second.pop_back();
173 std::cout << "\t" << largest.second << ": " << largest.first*rate << std::endl;
179 static struct option longopts[] = {
180 {"full", no_argument, NULL, 'f'},
181 {"topk", required_argument, NULL, 'k'},
186 cmd_pmc_summary(int argc, char **argv)
188 int option, logfd, k;
193 while ((option = getopt_long(argc, argv, "k:f", longopts, NULL)) != -1) {
209 printf("argc: %d\n", argc);
210 for (int i = 0; i < argc; i++)
211 printf("%s\n", argv[i]);
214 if ((logfd = open(argv[0], O_RDONLY,
215 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
216 errx(EX_OSERR, "ERROR: Cannot open \"%s\" for reading: %s.", argv[0],
219 return (pmc_summary_handler(logfd, k, do_full));