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 #define P_KPROC 0x00004 /* Kernel process. */ 85 86 static void __dead2 87 usage(void) 88 { 89 errx(EX_USAGE, 90 "\t summarize log file\n" 91 "\t -k <k>, --topk <k> show topk processes for each counter\n" 92 ); 93 } 94 95 static int 96 pmc_summary_handler(int logfd, int k, bool do_full) 97 { 98 struct pmclog_parse_state *ps; 99 struct pmclog_ev ev; 100 idmap pidmap, tidmap, eventnamemap; 101 strintmap tideventmap, pideventmap; 102 intmap eventmap, pmcidmap, ratemap; 103 intmap kerntidmap, kernpidmap; 104 eventcountmap countmap; 105 106 ps = static_cast<struct pmclog_parse_state*>(pmclog_open(logfd)); 107 if (ps == NULL) 108 errx(EX_OSERR, "ERROR: Cannot allocate pmclog parse state: %s\n", 109 strerror(errno)); 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; 115 } 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(); 121 } 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(); 127 } 128 if (ev.pl_type == PMCLOG_TYPE_CALLCHAIN) { 129 auto event = pmcidmap[ev.pl_u.pl_cc.pl_pmcid]; 130 131 if (event == 0) 132 continue; 133 eventmap[event]++; 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]; 138 teventmap[event]++; 139 } 140 if (pidname != pidmap.end()) { 141 auto &peventmap = pideventmap[pidname->second]; 142 peventmap[event]++; 143 } 144 } 145 } 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); 150 } 151 for (auto &kv : countmap) 152 std::sort(kv.second.begin(), kv.second.end(), [](auto &a, auto &b) {return (a.first < b.first);}); 153 if (do_full) { 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; 162 } 163 } 164 return (0); 165 } 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; 174 } 175 } 176 return (0); 177 } 178 179 static struct option longopts[] = { 180 {"full", no_argument, NULL, 'f'}, 181 {"topk", required_argument, NULL, 'k'}, 182 {NULL, 0, NULL, 0} 183 }; 184 185 int 186 cmd_pmc_summary(int argc, char **argv) 187 { 188 int option, logfd, k; 189 bool do_full; 190 191 do_full = false; 192 k = 5; 193 while ((option = getopt_long(argc, argv, "k:f", longopts, NULL)) != -1) { 194 switch (option) { 195 case 'f': 196 do_full = 1; 197 break; 198 case 'k': 199 k = atoi(optarg); 200 break; 201 case '?': 202 default: 203 usage(); 204 } 205 } 206 argc -= optind; 207 argv += optind; 208 if (argc != 1) { 209 printf("argc: %d\n", argc); 210 for (int i = 0; i < argc; i++) 211 printf("%s\n", argv[i]); 212 usage(); 213 } 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], 217 strerror(errno)); 218 219 return (pmc_summary_handler(logfd, k, do_full)); 220 } 221