xref: /freebsd/usr.sbin/pmc/cmd_pmc_summary.cc (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018, Matthew Macy
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
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>
37 #include <sys/stat.h>
38 #include <sys/sysctl.h>
39 #include <sys/time.h>
40 #include <sys/ttycom.h>
41 #include <sys/user.h>
42 #include <sys/wait.h>
43 
44 #include <assert.h>
45 #include <curses.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <getopt.h>
50 #include <kvm.h>
51 #include <libgen.h>
52 #include <limits.h>
53 #include <locale.h>
54 #include <math.h>
55 #include <pmc.h>
56 #include <pmclog.h>
57 #include <regex.h>
58 #include <signal.h>
59 #include <stdarg.h>
60 #include <stdint.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <stddef.h>
64 #include <string.h>
65 #include <sysexits.h>
66 #include <unistd.h>
67 
68 #include <libpmcstat.h>
69 #include "cmd_pmc.h"
70 
71 #include <iostream>
72 #include <string>
73 #include <vector>
74 #include <unordered_map>
75 
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;
83 
84 static void __dead2
85 usage(void)
86 {
87 	errx(EX_USAGE,
88 	    "\t summarize log file\n"
89 		 "\t -k <k>, --topk <k> show topk processes for each counter\n"
90 	    );
91 }
92 
93 static int
94 pmc_summary_handler(int logfd, int k, bool do_full)
95 {
96 	struct pmclog_parse_state *ps;
97 	struct pmclog_ev ev;
98 	idmap pidmap, tidmap, eventnamemap;
99 	strintmap tideventmap, pideventmap;
100 	intmap eventmap, pmcidmap, ratemap;
101 	intmap kerntidmap, kernpidmap;
102 	eventcountmap countmap;
103 
104 	ps = static_cast<struct pmclog_parse_state*>(pmclog_open(logfd));
105 	if (ps == NULL)
106 		errx(EX_OSERR, "ERROR: Cannot allocate pmclog parse state: %s\n",
107 			 strerror(errno));
108 	while (pmclog_read(ps, &ev) == 0) {
109 		if (ev.pl_type == PMCLOG_TYPE_PMCALLOCATE) {
110 			pmcidmap[ev.pl_u.pl_a.pl_pmcid] = ev.pl_u.pl_a.pl_event;
111 			ratemap[ev.pl_u.pl_a.pl_event] = ev.pl_u.pl_a.pl_rate;
112 			eventnamemap[ev.pl_u.pl_a.pl_event] = ev.pl_u.pl_a.pl_evname;
113 		}
114 		if (ev.pl_type == PMCLOG_TYPE_THR_CREATE) {
115 			tidmap[ev.pl_u.pl_tc.pl_tid] = ev.pl_u.pl_tc.pl_tdname;
116 			kerntidmap[ev.pl_u.pl_tc.pl_tid] = !!(ev.pl_u.pl_tc.pl_flags & P_KPROC);
117 			if (tideventmap.find(ev.pl_u.pl_tc.pl_tdname) == tideventmap.end())
118 				tideventmap[ev.pl_u.pl_tc.pl_tdname] = intmap();
119 		}
120 		if (ev.pl_type == PMCLOG_TYPE_PROC_CREATE) {
121 			pidmap[ev.pl_u.pl_pc.pl_pid] = ev.pl_u.pl_pc.pl_pcomm;
122 			kernpidmap[ev.pl_u.pl_pc.pl_pid] = !!(ev.pl_u.pl_pc.pl_flags & P_KPROC);
123 			if (pideventmap.find(ev.pl_u.pl_pc.pl_pcomm) == pideventmap.end())
124 				pideventmap[ev.pl_u.pl_pc.pl_pcomm] = intmap();
125 		}
126 		if (ev.pl_type == PMCLOG_TYPE_CALLCHAIN) {
127 			auto event = pmcidmap[ev.pl_u.pl_cc.pl_pmcid];
128 
129 			if (event == 0)
130 				continue;
131 			eventmap[event]++;
132 			auto tidname = tidmap.find(ev.pl_u.pl_cc.pl_tid);
133 			auto pidname = pidmap.find(ev.pl_u.pl_cc.pl_pid);
134 			if (tidname != tidmap.end()) {
135 				auto &teventmap = tideventmap[tidname->second];
136 				teventmap[event]++;
137 			}
138 			if (pidname != pidmap.end()) {
139 				auto &peventmap = pideventmap[pidname->second];
140 				peventmap[event]++;
141 			}
142 		}
143 	}
144 	for (auto &pkv : pideventmap)
145 		for (auto &ekv : pkv.second) {
146 			auto &samplevec = countmap[ekv.first];
147 			samplevec.emplace_back(ekv.second, pkv.first);
148 		}
149 	for (auto &kv : countmap)
150 		std::sort(kv.second.begin(), kv.second.end(), [](auto &a, auto &b) {return (a.first < b.first);});
151 	if (do_full) {
152 		for (auto &kv : countmap) {
153 			auto &name = eventnamemap[kv.first];
154 			auto rate = ratemap[kv.first];
155 			std::cout << "idx: " << kv.first << " name: " << name << " rate: " << rate << std::endl;
156 			while (!kv.second.empty()) {
157 				auto val = kv.second.back();
158 				kv.second.pop_back();
159 				std::cout << val.second << ": " << val.first << std::endl;
160 			}
161 		}
162 		return (0);
163 	}
164 	for (auto &kv : countmap) {
165 		auto &name = eventnamemap[kv.first];
166 		auto rate = ratemap[kv.first];
167 		std::cout << name << ":" << std::endl;
168 		for (auto i = 0; i < k; i++) {
169 			auto largest = kv.second.back();
170 			kv.second.pop_back();
171 			std::cout << "\t" << largest.second << ": " << largest.first*rate << std::endl;
172 		}
173 	}
174 	return (0);
175 }
176 
177 static struct option longopts[] = {
178 	{"full", no_argument, NULL, 'f'},
179 	{"topk", required_argument, NULL, 'k'},
180 	{NULL, 0, NULL, 0}
181 };
182 
183 int
184 cmd_pmc_summary(int argc, char **argv)
185 {
186 	int option, logfd, k;
187 	bool do_full;
188 
189 	do_full = false;
190 	k = 5;
191 	while ((option = getopt_long(argc, argv, "k:f", longopts, NULL)) != -1) {
192 		switch (option) {
193 		case 'f':
194 			do_full = 1;
195 			break;
196 		case 'k':
197 			k = atoi(optarg);
198 			break;
199 		case '?':
200 		default:
201 			usage();
202 		}
203 	}
204 	argc -= optind;
205 	argv += optind;
206 	if (argc != 1) {
207 		printf("argc: %d\n", argc);
208 		for (int i = 0; i < argc; i++)
209 			printf("%s\n", argv[i]);
210 		usage();
211 	}
212 	if ((logfd = open(argv[0], O_RDONLY,
213 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
214 		errx(EX_OSERR, "ERROR: Cannot open \"%s\" for reading: %s.", argv[0],
215 		    strerror(errno));
216 
217 	return (pmc_summary_handler(logfd, k, do_full));
218 }
219