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