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