pmcstat_log.c revision 206090
1/*- 2 * Copyright (c) 2005-2007, Joseph Koshy 3 * Copyright (c) 2007 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by A. Joseph Koshy under 7 * sponsorship from the FreeBSD Foundation and Google, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31/* 32 * Transform a hwpmc(4) log into human readable form, and into 33 * gprof(1) compatible profiles. 34 */ 35 36#include <sys/cdefs.h> 37__FBSDID("$FreeBSD: head/usr.sbin/pmcstat/pmcstat_log.c 206090 2010-04-02 13:34:28Z fabient $"); 38 39#include <sys/param.h> 40#include <sys/endian.h> 41#include <sys/gmon.h> 42#include <sys/imgact_aout.h> 43#include <sys/imgact_elf.h> 44#include <sys/mman.h> 45#include <sys/pmc.h> 46#include <sys/queue.h> 47#include <sys/socket.h> 48#include <sys/stat.h> 49#include <sys/wait.h> 50 51#include <netinet/in.h> 52 53#include <assert.h> 54#include <curses.h> 55#include <err.h> 56#include <errno.h> 57#include <fcntl.h> 58#include <gelf.h> 59#include <libgen.h> 60#include <limits.h> 61#include <netdb.h> 62#include <pmc.h> 63#include <pmclog.h> 64#include <sysexits.h> 65#include <stdint.h> 66#include <stdio.h> 67#include <stdlib.h> 68#include <string.h> 69#include <unistd.h> 70 71#include "pmcstat.h" 72#include "pmcstat_log.h" 73#include "pmcstat_top.h" 74 75#define PMCSTAT_ALLOCATE 1 76 77/* 78 * PUBLIC INTERFACES 79 * 80 * pmcstat_initialize_logging() initialize this module, called first 81 * pmcstat_shutdown_logging() orderly shutdown, called last 82 * pmcstat_open_log() open an eventlog for processing 83 * pmcstat_process_log() print/convert an event log 84 * pmcstat_display_log() top mode display for the log 85 * pmcstat_close_log() finish processing an event log 86 * 87 * IMPLEMENTATION NOTES 88 * 89 * We correlate each 'callchain' or 'sample' entry seen in the event 90 * log back to an executable object in the system. Executable objects 91 * include: 92 * - program executables, 93 * - shared libraries loaded by the runtime loader, 94 * - dlopen()'ed objects loaded by the program, 95 * - the runtime loader itself, 96 * - the kernel and kernel modules. 97 * 98 * Each process that we know about is treated as a set of regions that 99 * map to executable objects. Processes are described by 100 * 'pmcstat_process' structures. Executable objects are tracked by 101 * 'pmcstat_image' structures. The kernel and kernel modules are 102 * common to all processes (they reside at the same virtual addresses 103 * for all processes). Individual processes can have their text 104 * segments and shared libraries loaded at process-specific locations. 105 * 106 * A given executable object can be in use by multiple processes 107 * (e.g., libc.so) and loaded at a different address in each. 108 * pmcstat_pcmap structures track per-image mappings. 109 * 110 * The sample log could have samples from multiple PMCs; we 111 * generate one 'gmon.out' profile per PMC. 112 * 113 * IMPLEMENTATION OF GMON OUTPUT 114 * 115 * Each executable object gets one 'gmon.out' profile, per PMC in 116 * use. Creation of 'gmon.out' profiles is done lazily. The 117 * 'gmon.out' profiles generated for a given sampling PMC are 118 * aggregates of all the samples for that particular executable 119 * object. 120 * 121 * IMPLEMENTATION OF SYSTEM-WIDE CALLGRAPH OUTPUT 122 * 123 * Each active pmcid has its own callgraph structure, described by a 124 * 'struct pmcstat_callgraph'. Given a process id and a list of pc 125 * values, we map each pc value to a tuple (image, symbol), where 126 * 'image' denotes an executable object and 'symbol' is the closest 127 * symbol that precedes the pc value. Each pc value in the list is 128 * also given a 'rank' that reflects its depth in the call stack. 129 */ 130 131struct pmcstat_pmcs pmcstat_pmcs = LIST_HEAD_INITIALIZER(pmcstat_pmcs); 132 133/* 134 * All image descriptors are kept in a hash table. 135 */ 136struct pmcstat_image_hash_list pmcstat_image_hash[PMCSTAT_NHASH]; 137 138/* 139 * All process descriptors are kept in a hash table. 140 */ 141struct pmcstat_process_hash_list pmcstat_process_hash[PMCSTAT_NHASH]; 142 143struct pmcstat_stats pmcstat_stats; /* statistics */ 144 145struct pmcstat_process *pmcstat_kernproc; /* kernel 'process' */ 146 147#include "pmcpl_gprof.h" 148#include "pmcpl_callgraph.h" 149#include "pmcpl_annotate.h" 150#include "pmcpl_calltree.h" 151 152struct pmc_plugins { 153 const char *pl_name; /* name */ 154 155 /* configure */ 156 int (*pl_configure)(char *opt); 157 158 /* init and shutdown */ 159 int (*pl_init)(void); 160 void (*pl_shutdown)(FILE *mf); 161 162 /* sample processing */ 163 void (*pl_process)(struct pmcstat_process *pp, 164 struct pmcstat_pmcrecord *pmcr, uint32_t nsamples, 165 uintfptr_t *cc, int usermode, uint32_t cpu); 166 167 /* image */ 168 void (*pl_initimage)(struct pmcstat_image *pi); 169 void (*pl_shutdownimage)(struct pmcstat_image *pi); 170 171 /* pmc */ 172 void (*pl_newpmc)(pmcstat_interned_string ps, 173 struct pmcstat_pmcrecord *pr); 174 175 /* top display */ 176 void (*pl_topdisplay)(void); 177 178 /* top keypress */ 179 int (*pl_topkeypress)(int c, WINDOW *w); 180 181} plugins[] = { 182 { 183 .pl_name = "none", 184 }, 185 { 186 .pl_name = "callgraph", 187 .pl_init = pmcpl_cg_init, 188 .pl_shutdown = pmcpl_cg_shutdown, 189 .pl_process = pmcpl_cg_process, 190 .pl_topkeypress = pmcpl_cg_topkeypress, 191 .pl_topdisplay = pmcpl_cg_topdisplay 192 }, 193 { 194 .pl_name = "gprof", 195 .pl_shutdown = pmcpl_gmon_shutdown, 196 .pl_process = pmcpl_gmon_process, 197 .pl_initimage = pmcpl_gmon_initimage, 198 .pl_shutdownimage = pmcpl_gmon_shutdownimage, 199 .pl_newpmc = pmcpl_gmon_newpmc 200 }, 201 { 202 .pl_name = "annotate", 203 .pl_process = pmcpl_annotate_process 204 }, 205 { 206 .pl_name = "calltree", 207 .pl_configure = pmcpl_ct_configure, 208 .pl_init = pmcpl_ct_init, 209 .pl_shutdown = pmcpl_ct_shutdown, 210 .pl_process = pmcpl_ct_process, 211 .pl_topkeypress = pmcpl_ct_topkeypress, 212 .pl_topdisplay = pmcpl_ct_topdisplay 213 }, 214 { 215 .pl_name = NULL 216 } 217}; 218 219int pmcstat_mergepmc; 220 221int pmcstat_pmcinfilter = 0; /* PMC filter for top mode. */ 222float pmcstat_threshold = 0.5; /* Cost filter for top mode. */ 223 224/* 225 * Prototypes 226 */ 227 228static struct pmcstat_image *pmcstat_image_from_path(pmcstat_interned_string 229 _path, int _iskernelmodule); 230static void pmcstat_image_get_aout_params(struct pmcstat_image *_image); 231static void pmcstat_image_get_elf_params(struct pmcstat_image *_image); 232static void pmcstat_image_link(struct pmcstat_process *_pp, 233 struct pmcstat_image *_i, uintfptr_t _lpc); 234 235static void pmcstat_pmcid_add(pmc_id_t _pmcid, 236 pmcstat_interned_string _name); 237 238static void pmcstat_process_aout_exec(struct pmcstat_process *_pp, 239 struct pmcstat_image *_image, uintfptr_t _entryaddr); 240static void pmcstat_process_elf_exec(struct pmcstat_process *_pp, 241 struct pmcstat_image *_image, uintfptr_t _entryaddr); 242static void pmcstat_process_exec(struct pmcstat_process *_pp, 243 pmcstat_interned_string _path, uintfptr_t _entryaddr); 244static struct pmcstat_process *pmcstat_process_lookup(pid_t _pid, 245 int _allocate); 246static int pmcstat_string_compute_hash(const char *_string); 247static void pmcstat_string_initialize(void); 248static int pmcstat_string_lookup_hash(pmcstat_interned_string _is); 249static void pmcstat_string_shutdown(void); 250static void pmcstat_stats_reset(void); 251 252/* 253 * A simple implementation of interned strings. Each interned string 254 * is assigned a unique address, so that subsequent string compares 255 * can be done by a simple pointer comparision instead of using 256 * strcmp(). This speeds up hash table lookups and saves memory if 257 * duplicate strings are the norm. 258 */ 259struct pmcstat_string { 260 LIST_ENTRY(pmcstat_string) ps_next; /* hash link */ 261 int ps_len; 262 int ps_hash; 263 char *ps_string; 264}; 265 266static LIST_HEAD(,pmcstat_string) pmcstat_string_hash[PMCSTAT_NHASH]; 267 268/* 269 * PMC count. 270 */ 271int pmcstat_npmcs; 272 273/* 274 * PMC Top mode pause state. 275 */ 276int pmcstat_pause; 277 278static void 279pmcstat_stats_reset(void) 280{ 281 struct pmcstat_pmcrecord *pr; 282 283 /* Flush PMCs stats. */ 284 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) { 285 pr->pr_samples = 0; 286 pr->pr_dubious_frames = 0; 287 } 288 289 /* Flush global stats. */ 290 bzero(&pmcstat_stats, sizeof(struct pmcstat_stats)); 291} 292 293/* 294 * Compute a 'hash' value for a string. 295 */ 296 297static int 298pmcstat_string_compute_hash(const char *s) 299{ 300 int hash; 301 302 for (hash = 0; *s; s++) 303 hash ^= *s; 304 305 return (hash & PMCSTAT_HASH_MASK); 306} 307 308/* 309 * Intern a copy of string 's', and return a pointer to the 310 * interned structure. 311 */ 312 313pmcstat_interned_string 314pmcstat_string_intern(const char *s) 315{ 316 struct pmcstat_string *ps; 317 const struct pmcstat_string *cps; 318 int hash, len; 319 320 if ((cps = pmcstat_string_lookup(s)) != NULL) 321 return (cps); 322 323 hash = pmcstat_string_compute_hash(s); 324 len = strlen(s); 325 326 if ((ps = malloc(sizeof(*ps))) == NULL) 327 err(EX_OSERR, "ERROR: Could not intern string"); 328 ps->ps_len = len; 329 ps->ps_hash = hash; 330 ps->ps_string = strdup(s); 331 LIST_INSERT_HEAD(&pmcstat_string_hash[hash], ps, ps_next); 332 return ((pmcstat_interned_string) ps); 333} 334 335const char * 336pmcstat_string_unintern(pmcstat_interned_string str) 337{ 338 const char *s; 339 340 s = ((const struct pmcstat_string *) str)->ps_string; 341 return (s); 342} 343 344pmcstat_interned_string 345pmcstat_string_lookup(const char *s) 346{ 347 struct pmcstat_string *ps; 348 int hash, len; 349 350 hash = pmcstat_string_compute_hash(s); 351 len = strlen(s); 352 353 LIST_FOREACH(ps, &pmcstat_string_hash[hash], ps_next) 354 if (ps->ps_len == len && ps->ps_hash == hash && 355 strcmp(ps->ps_string, s) == 0) 356 return (ps); 357 return (NULL); 358} 359 360static int 361pmcstat_string_lookup_hash(pmcstat_interned_string s) 362{ 363 const struct pmcstat_string *ps; 364 365 ps = (const struct pmcstat_string *) s; 366 return (ps->ps_hash); 367} 368 369/* 370 * Initialize the string interning facility. 371 */ 372 373static void 374pmcstat_string_initialize(void) 375{ 376 int i; 377 378 for (i = 0; i < PMCSTAT_NHASH; i++) 379 LIST_INIT(&pmcstat_string_hash[i]); 380} 381 382/* 383 * Destroy the string table, free'ing up space. 384 */ 385 386static void 387pmcstat_string_shutdown(void) 388{ 389 int i; 390 struct pmcstat_string *ps, *pstmp; 391 392 for (i = 0; i < PMCSTAT_NHASH; i++) 393 LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next, 394 pstmp) { 395 LIST_REMOVE(ps, ps_next); 396 free(ps->ps_string); 397 free(ps); 398 } 399} 400 401/* 402 * Determine whether a given executable image is an A.OUT object, and 403 * if so, fill in its parameters from the text file. 404 * Sets image->pi_type. 405 */ 406 407static void 408pmcstat_image_get_aout_params(struct pmcstat_image *image) 409{ 410 int fd; 411 ssize_t nbytes; 412 struct exec ex; 413 const char *path; 414 char buffer[PATH_MAX]; 415 416 path = pmcstat_string_unintern(image->pi_execpath); 417 assert(path != NULL); 418 419 if (image->pi_iskernelmodule) 420 errx(EX_SOFTWARE, "ERROR: a.out kernel modules are " 421 "unsupported \"%s\"", path); 422 423 (void) snprintf(buffer, sizeof(buffer), "%s%s", 424 args.pa_fsroot, path); 425 426 if ((fd = open(buffer, O_RDONLY, 0)) < 0 || 427 (nbytes = read(fd, &ex, sizeof(ex))) < 0) { 428 warn("WARNING: Cannot determine type of \"%s\"", path); 429 image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE; 430 if (fd != -1) 431 (void) close(fd); 432 return; 433 } 434 435 (void) close(fd); 436 437 if ((unsigned) nbytes != sizeof(ex) || 438 N_BADMAG(ex)) 439 return; 440 441 image->pi_type = PMCSTAT_IMAGE_AOUT; 442 443 /* TODO: the rest of a.out processing */ 444 445 return; 446} 447 448/* 449 * Helper function. 450 */ 451 452static int 453pmcstat_symbol_compare(const void *a, const void *b) 454{ 455 const struct pmcstat_symbol *sym1, *sym2; 456 457 sym1 = (const struct pmcstat_symbol *) a; 458 sym2 = (const struct pmcstat_symbol *) b; 459 460 if (sym1->ps_end <= sym2->ps_start) 461 return (-1); 462 if (sym1->ps_start >= sym2->ps_end) 463 return (1); 464 return (0); 465} 466 467/* 468 * Map an address to a symbol in an image. 469 */ 470 471struct pmcstat_symbol * 472pmcstat_symbol_search(struct pmcstat_image *image, uintfptr_t addr) 473{ 474 struct pmcstat_symbol sym; 475 476 if (image->pi_symbols == NULL) 477 return (NULL); 478 479 sym.ps_name = NULL; 480 sym.ps_start = addr; 481 sym.ps_end = addr + 1; 482 483 return (bsearch((void *) &sym, image->pi_symbols, 484 image->pi_symcount, sizeof(struct pmcstat_symbol), 485 pmcstat_symbol_compare)); 486} 487 488/* 489 * Add the list of symbols in the given section to the list associated 490 * with the object. 491 */ 492static void 493pmcstat_image_add_symbols(struct pmcstat_image *image, Elf *e, 494 Elf_Scn *scn, GElf_Shdr *sh) 495{ 496 int firsttime; 497 size_t n, newsyms, nshsyms, nfuncsyms; 498 struct pmcstat_symbol *symptr; 499 char *fnname; 500 GElf_Sym sym; 501 Elf_Data *data; 502 503 if ((data = elf_getdata(scn, NULL)) == NULL) 504 return; 505 506 /* 507 * Determine the number of functions named in this 508 * section. 509 */ 510 511 nshsyms = sh->sh_size / sh->sh_entsize; 512 for (n = nfuncsyms = 0; n < nshsyms; n++) { 513 if (gelf_getsym(data, (int) n, &sym) != &sym) 514 return; 515 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC) 516 nfuncsyms++; 517 } 518 519 if (nfuncsyms == 0) 520 return; 521 522 /* 523 * Allocate space for the new entries. 524 */ 525 firsttime = image->pi_symbols == NULL; 526 symptr = realloc(image->pi_symbols, 527 sizeof(*symptr) * (image->pi_symcount + nfuncsyms)); 528 if (symptr == image->pi_symbols) /* realloc() failed. */ 529 return; 530 image->pi_symbols = symptr; 531 532 /* 533 * Append new symbols to the end of the current table. 534 */ 535 symptr += image->pi_symcount; 536 537 for (n = newsyms = 0; n < nshsyms; n++) { 538 if (gelf_getsym(data, (int) n, &sym) != &sym) 539 return; 540 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC) 541 continue; 542 543 if (!firsttime && pmcstat_symbol_search(image, sym.st_value)) 544 continue; /* We've seen this symbol already. */ 545 546 if ((fnname = elf_strptr(e, sh->sh_link, sym.st_name)) 547 == NULL) 548 continue; 549 550 symptr->ps_name = pmcstat_string_intern(fnname); 551 symptr->ps_start = sym.st_value - image->pi_vaddr; 552 symptr->ps_end = symptr->ps_start + sym.st_size; 553 symptr++; 554 555 newsyms++; 556 } 557 558 image->pi_symcount += newsyms; 559 560 assert(newsyms <= nfuncsyms); 561 562 /* 563 * Return space to the system if there were duplicates. 564 */ 565 if (newsyms < nfuncsyms) 566 image->pi_symbols = realloc(image->pi_symbols, 567 sizeof(*symptr) * image->pi_symcount); 568 569 /* 570 * Keep the list of symbols sorted. 571 */ 572 qsort(image->pi_symbols, image->pi_symcount, sizeof(*symptr), 573 pmcstat_symbol_compare); 574 575 /* 576 * Deal with function symbols that have a size of 'zero' by 577 * making them extend to the next higher address. These 578 * symbols are usually defined in assembly code. 579 */ 580 for (symptr = image->pi_symbols; 581 symptr < image->pi_symbols + (image->pi_symcount - 1); 582 symptr++) 583 if (symptr->ps_start == symptr->ps_end) 584 symptr->ps_end = (symptr+1)->ps_start; 585} 586 587/* 588 * Examine an ELF file to determine the size of its text segment. 589 * Sets image->pi_type if anything conclusive can be determined about 590 * this image. 591 */ 592 593static void 594pmcstat_image_get_elf_params(struct pmcstat_image *image) 595{ 596 int fd; 597 size_t i, nph, nsh; 598 const char *path, *elfbase; 599 char *p, *endp; 600 uintfptr_t minva, maxva; 601 Elf *e; 602 Elf_Scn *scn; 603 GElf_Ehdr eh; 604 GElf_Phdr ph; 605 GElf_Shdr sh; 606 enum pmcstat_image_type image_type; 607 char buffer[PATH_MAX]; 608 609 assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN); 610 611 image->pi_start = minva = ~(uintfptr_t) 0; 612 image->pi_end = maxva = (uintfptr_t) 0; 613 image->pi_type = image_type = PMCSTAT_IMAGE_INDETERMINABLE; 614 image->pi_isdynamic = 0; 615 image->pi_dynlinkerpath = NULL; 616 image->pi_vaddr = 0; 617 618 path = pmcstat_string_unintern(image->pi_execpath); 619 assert(path != NULL); 620 621 /* 622 * Look for kernel modules under FSROOT/KERNELPATH/NAME, 623 * and user mode executable objects under FSROOT/PATHNAME. 624 */ 625 if (image->pi_iskernelmodule) 626 (void) snprintf(buffer, sizeof(buffer), "%s%s/%s", 627 args.pa_fsroot, args.pa_kernel, path); 628 else 629 (void) snprintf(buffer, sizeof(buffer), "%s%s", 630 args.pa_fsroot, path); 631 632 e = NULL; 633 if ((fd = open(buffer, O_RDONLY, 0)) < 0 || 634 (e = elf_begin(fd, ELF_C_READ, NULL)) == NULL || 635 (elf_kind(e) != ELF_K_ELF)) { 636 warnx("WARNING: Cannot determine the type of \"%s\".", 637 buffer); 638 goto done; 639 } 640 641 if (gelf_getehdr(e, &eh) != &eh) { 642 warnx("WARNING: Cannot retrieve the ELF Header for " 643 "\"%s\": %s.", buffer, elf_errmsg(-1)); 644 goto done; 645 } 646 647 if (eh.e_type != ET_EXEC && eh.e_type != ET_DYN && 648 !(image->pi_iskernelmodule && eh.e_type == ET_REL)) { 649 warnx("WARNING: \"%s\" is of an unsupported ELF type.", 650 buffer); 651 goto done; 652 } 653 654 image_type = eh.e_ident[EI_CLASS] == ELFCLASS32 ? 655 PMCSTAT_IMAGE_ELF32 : PMCSTAT_IMAGE_ELF64; 656 657 /* 658 * Determine the virtual address where an executable would be 659 * loaded. Additionally, for dynamically linked executables, 660 * save the pathname to the runtime linker. 661 */ 662 if (eh.e_type == ET_EXEC) { 663 if (elf_getphnum(e, &nph) == 0) { 664 warnx("WARNING: Could not determine the number of " 665 "program headers in \"%s\": %s.", buffer, 666 elf_errmsg(-1)); 667 goto done; 668 } 669 for (i = 0; i < eh.e_phnum; i++) { 670 if (gelf_getphdr(e, i, &ph) != &ph) { 671 warnx("WARNING: Retrieval of PHDR entry #%ju " 672 "in \"%s\" failed: %s.", (uintmax_t) i, 673 buffer, elf_errmsg(-1)); 674 goto done; 675 } 676 switch (ph.p_type) { 677 case PT_DYNAMIC: 678 image->pi_isdynamic = 1; 679 break; 680 case PT_INTERP: 681 if ((elfbase = elf_rawfile(e, NULL)) == NULL) { 682 warnx("WARNING: Cannot retrieve the " 683 "interpreter for \"%s\": %s.", 684 buffer, elf_errmsg(-1)); 685 goto done; 686 } 687 image->pi_dynlinkerpath = 688 pmcstat_string_intern(elfbase + 689 ph.p_offset); 690 break; 691 case PT_LOAD: 692 if (ph.p_offset == 0) 693 image->pi_vaddr = ph.p_vaddr; 694 break; 695 } 696 } 697 } 698 699 /* 700 * Get the min and max VA associated with this ELF object. 701 */ 702 if (elf_getshnum(e, &nsh) == 0) { 703 warnx("WARNING: Could not determine the number of sections " 704 "for \"%s\": %s.", buffer, elf_errmsg(-1)); 705 goto done; 706 } 707 708 for (i = 0; i < nsh; i++) { 709 if ((scn = elf_getscn(e, i)) == NULL || 710 gelf_getshdr(scn, &sh) != &sh) { 711 warnx("WARNING: Could not retrieve section header " 712 "#%ju in \"%s\": %s.", (uintmax_t) i, buffer, 713 elf_errmsg(-1)); 714 goto done; 715 } 716 if (sh.sh_flags & SHF_EXECINSTR) { 717 minva = min(minva, sh.sh_addr); 718 maxva = max(maxva, sh.sh_addr + sh.sh_size); 719 } 720 if (sh.sh_type == SHT_SYMTAB || sh.sh_type == SHT_DYNSYM) 721 pmcstat_image_add_symbols(image, e, scn, &sh); 722 } 723 724 image->pi_start = minva; 725 image->pi_end = maxva; 726 image->pi_type = image_type; 727 image->pi_fullpath = pmcstat_string_intern(buffer); 728 729 /* Build display name 730 */ 731 endp = buffer; 732 for (p = buffer; *p; p++) 733 if (*p == '/') 734 endp = p+1; 735 image->pi_name = pmcstat_string_intern(endp); 736 737 done: 738 (void) elf_end(e); 739 if (fd >= 0) 740 (void) close(fd); 741 return; 742} 743 744/* 745 * Given an image descriptor, determine whether it is an ELF, or AOUT. 746 * If no handler claims the image, set its type to 'INDETERMINABLE'. 747 */ 748 749void 750pmcstat_image_determine_type(struct pmcstat_image *image) 751{ 752 assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN); 753 754 /* Try each kind of handler in turn */ 755 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 756 pmcstat_image_get_elf_params(image); 757 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 758 pmcstat_image_get_aout_params(image); 759 760 /* 761 * Otherwise, remember that we tried to determine 762 * the object's type and had failed. 763 */ 764 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 765 image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE; 766} 767 768/* 769 * Locate an image descriptor given an interned path, adding a fresh 770 * descriptor to the cache if necessary. This function also finds a 771 * suitable name for this image's sample file. 772 * 773 * We defer filling in the file format specific parts of the image 774 * structure till the time we actually see a sample that would fall 775 * into this image. 776 */ 777 778static struct pmcstat_image * 779pmcstat_image_from_path(pmcstat_interned_string internedpath, 780 int iskernelmodule) 781{ 782 int hash; 783 struct pmcstat_image *pi; 784 785 hash = pmcstat_string_lookup_hash(internedpath); 786 787 /* First, look for an existing entry. */ 788 LIST_FOREACH(pi, &pmcstat_image_hash[hash], pi_next) 789 if (pi->pi_execpath == internedpath && 790 pi->pi_iskernelmodule == iskernelmodule) 791 return (pi); 792 793 /* 794 * Allocate a new entry and place it at the head of the hash 795 * and LRU lists. 796 */ 797 pi = malloc(sizeof(*pi)); 798 if (pi == NULL) 799 return (NULL); 800 801 pi->pi_type = PMCSTAT_IMAGE_UNKNOWN; 802 pi->pi_execpath = internedpath; 803 pi->pi_start = ~0; 804 pi->pi_end = 0; 805 pi->pi_entry = 0; 806 pi->pi_vaddr = 0; 807 pi->pi_isdynamic = 0; 808 pi->pi_iskernelmodule = iskernelmodule; 809 pi->pi_dynlinkerpath = NULL; 810 pi->pi_symbols = NULL; 811 pi->pi_symcount = 0; 812 pi->pi_addr2line = NULL; 813 814 if (plugins[args.pa_pplugin].pl_initimage != NULL) 815 plugins[args.pa_pplugin].pl_initimage(pi); 816 if (plugins[args.pa_plugin].pl_initimage != NULL) 817 plugins[args.pa_plugin].pl_initimage(pi); 818 819 LIST_INSERT_HEAD(&pmcstat_image_hash[hash], pi, pi_next); 820 821 return (pi); 822} 823 824/* 825 * Record the fact that PC values from 'start' to 'end' come from 826 * image 'image'. 827 */ 828 829static void 830pmcstat_image_link(struct pmcstat_process *pp, struct pmcstat_image *image, 831 uintfptr_t start) 832{ 833 struct pmcstat_pcmap *pcm, *pcmnew; 834 uintfptr_t offset; 835 836 assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN && 837 image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE); 838 839 if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL) 840 err(EX_OSERR, "ERROR: Cannot create a map entry"); 841 842 /* 843 * Adjust the map entry to only cover the text portion 844 * of the object. 845 */ 846 847 offset = start - image->pi_vaddr; 848 pcmnew->ppm_lowpc = image->pi_start + offset; 849 pcmnew->ppm_highpc = image->pi_end + offset; 850 pcmnew->ppm_image = image; 851 852 assert(pcmnew->ppm_lowpc < pcmnew->ppm_highpc); 853 854 /* Overlapped mmap()'s are assumed to never occur. */ 855 TAILQ_FOREACH(pcm, &pp->pp_map, ppm_next) 856 if (pcm->ppm_lowpc >= pcmnew->ppm_highpc) 857 break; 858 859 if (pcm == NULL) 860 TAILQ_INSERT_TAIL(&pp->pp_map, pcmnew, ppm_next); 861 else 862 TAILQ_INSERT_BEFORE(pcm, pcmnew, ppm_next); 863} 864 865/* 866 * Unmap images in the range [start..end) associated with process 867 * 'pp'. 868 */ 869 870static void 871pmcstat_image_unmap(struct pmcstat_process *pp, uintfptr_t start, 872 uintfptr_t end) 873{ 874 struct pmcstat_pcmap *pcm, *pcmtmp, *pcmnew; 875 876 assert(pp != NULL); 877 assert(start < end); 878 879 /* 880 * Cases: 881 * - we could have the range completely in the middle of an 882 * existing pcmap; in this case we have to split the pcmap 883 * structure into two (i.e., generate a 'hole'). 884 * - we could have the range covering multiple pcmaps; these 885 * will have to be removed. 886 * - we could have either 'start' or 'end' falling in the 887 * middle of a pcmap; in this case shorten the entry. 888 */ 889 TAILQ_FOREACH_SAFE(pcm, &pp->pp_map, ppm_next, pcmtmp) { 890 assert(pcm->ppm_lowpc < pcm->ppm_highpc); 891 if (pcm->ppm_highpc <= start) 892 continue; 893 if (pcm->ppm_lowpc >= end) 894 return; 895 if (pcm->ppm_lowpc >= start && pcm->ppm_highpc <= end) { 896 /* 897 * The current pcmap is completely inside the 898 * unmapped range: remove it entirely. 899 */ 900 TAILQ_REMOVE(&pp->pp_map, pcm, ppm_next); 901 free(pcm); 902 } else if (pcm->ppm_lowpc < start && pcm->ppm_highpc > end) { 903 /* 904 * Split this pcmap into two; curtail the 905 * current map to end at [start-1], and start 906 * the new one at [end]. 907 */ 908 if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL) 909 err(EX_OSERR, "ERROR: Cannot split a map " 910 "entry"); 911 912 pcmnew->ppm_image = pcm->ppm_image; 913 914 pcmnew->ppm_lowpc = end; 915 pcmnew->ppm_highpc = pcm->ppm_highpc; 916 917 pcm->ppm_highpc = start; 918 919 TAILQ_INSERT_AFTER(&pp->pp_map, pcm, pcmnew, ppm_next); 920 921 return; 922 } else if (pcm->ppm_lowpc < start && pcm->ppm_highpc <= end) 923 pcm->ppm_highpc = start; 924 else if (pcm->ppm_lowpc >= start && pcm->ppm_highpc > end) 925 pcm->ppm_lowpc = end; 926 else 927 assert(0); 928 } 929} 930 931/* 932 * Resolve file name and line number for the given address. 933 */ 934int 935pmcstat_image_addr2line(struct pmcstat_image *image, uintfptr_t addr, 936 char *sourcefile, size_t sourcefile_len, unsigned *sourceline, 937 char *funcname, size_t funcname_len) 938{ 939 static int addr2line_warn = 0; 940 941 char *sep, cmdline[PATH_MAX], imagepath[PATH_MAX]; 942 int fd; 943 944 if (image->pi_addr2line == NULL) { 945 snprintf(imagepath, sizeof(imagepath), "%s.symbols", 946 pmcstat_string_unintern(image->pi_fullpath)); 947 fd = open(imagepath, O_RDONLY); 948 if (fd < 0) { 949 snprintf(imagepath, sizeof(imagepath), "%s", 950 pmcstat_string_unintern(image->pi_fullpath)); 951 } else 952 close(fd); 953 snprintf(cmdline, sizeof(cmdline), "addr2line -Cfe \"%s\"", 954 imagepath); 955 image->pi_addr2line = popen(cmdline, "r+"); 956 if (image->pi_addr2line == NULL) { 957 if (!addr2line_warn) { 958 addr2line_warn = 1; 959 warnx("WARNING: addr2line is needed" 960 "for source code information."); 961 } 962 return (0); 963 } 964 } 965 966 if (feof(image->pi_addr2line) || ferror(image->pi_addr2line)) { 967 warnx("WARNING: addr2line pipe error"); 968 pclose(image->pi_addr2line); 969 image->pi_addr2line = NULL; 970 return (0); 971 } 972 973 fprintf(image->pi_addr2line, "%p\n", (void *)addr); 974 975 if (fgets(funcname, funcname_len, image->pi_addr2line) == NULL) { 976 warnx("WARNING: addr2line function name read error"); 977 return (0); 978 } 979 sep = strchr(funcname, '\n'); 980 if (sep != NULL) 981 *sep = '\0'; 982 983 if (fgets(sourcefile, sourcefile_len, image->pi_addr2line) == NULL) { 984 warnx("WARNING: addr2line source file read error"); 985 return (0); 986 } 987 sep = strchr(sourcefile, ':'); 988 if (sep == NULL) { 989 warnx("WARNING: addr2line source line separator missing"); 990 return (0); 991 } 992 *sep = '\0'; 993 *sourceline = atoi(sep+1); 994 if (*sourceline == 0) 995 return (0); 996 997 return (1); 998} 999 1000/* 1001 * Add a {pmcid,name} mapping. 1002 */ 1003 1004static void 1005pmcstat_pmcid_add(pmc_id_t pmcid, pmcstat_interned_string ps) 1006{ 1007 struct pmcstat_pmcrecord *pr, *prm; 1008 1009 /* Replace an existing name for the PMC. */ 1010 prm = NULL; 1011 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) 1012 if (pr->pr_pmcid == pmcid) { 1013 pr->pr_pmcname = ps; 1014 return; 1015 } else if (pr->pr_pmcname == ps) 1016 prm = pr; 1017 1018 /* 1019 * Otherwise, allocate a new descriptor and call the 1020 * plugins hook. 1021 */ 1022 if ((pr = malloc(sizeof(*pr))) == NULL) 1023 err(EX_OSERR, "ERROR: Cannot allocate pmc record"); 1024 1025 pr->pr_pmcid = pmcid; 1026 pr->pr_pmcname = ps; 1027 pr->pr_pmcin = pmcstat_npmcs++; 1028 pr->pr_samples = 0; 1029 pr->pr_dubious_frames = 0; 1030 pr->pr_merge = prm == NULL ? pr : prm; 1031 1032 LIST_INSERT_HEAD(&pmcstat_pmcs, pr, pr_next); 1033 1034 if (plugins[args.pa_pplugin].pl_newpmc != NULL) 1035 plugins[args.pa_pplugin].pl_newpmc(ps, pr); 1036 if (plugins[args.pa_plugin].pl_newpmc != NULL) 1037 plugins[args.pa_plugin].pl_newpmc(ps, pr); 1038} 1039 1040/* 1041 * Given a pmcid in use, find its human-readable name. 1042 */ 1043 1044const char * 1045pmcstat_pmcid_to_name(pmc_id_t pmcid) 1046{ 1047 struct pmcstat_pmcrecord *pr; 1048 1049 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) 1050 if (pr->pr_pmcid == pmcid) 1051 return (pmcstat_string_unintern(pr->pr_pmcname)); 1052 1053 err(EX_SOFTWARE, "ERROR: cannot find pmcid"); 1054 return NULL; 1055} 1056 1057/* 1058 * Convert PMC index to name. 1059 */ 1060 1061const char * 1062pmcstat_pmcindex_to_name(int pmcin) 1063{ 1064 struct pmcstat_pmcrecord *pr; 1065 1066 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) 1067 if (pr->pr_pmcin == pmcin) 1068 return pmcstat_string_unintern(pr->pr_pmcname); 1069 1070 return NULL; 1071} 1072 1073/* 1074 * Return PMC record with given index. 1075 */ 1076 1077struct pmcstat_pmcrecord * 1078pmcstat_pmcindex_to_pmcr(int pmcin) 1079{ 1080 struct pmcstat_pmcrecord *pr; 1081 1082 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) 1083 if (pr->pr_pmcin == pmcin) 1084 return pr; 1085 1086 err(EX_SOFTWARE, "ERROR: invalid pmcindex"); 1087 return NULL; 1088} 1089 1090/* 1091 * Get PMC record by id, apply merge policy. 1092 */ 1093 1094static struct pmcstat_pmcrecord * 1095pmcstat_lookup_pmcid(pmc_id_t pmcid) 1096{ 1097 struct pmcstat_pmcrecord *pr; 1098 1099 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) { 1100 if (pr->pr_pmcid == pmcid) { 1101 if (pmcstat_mergepmc) 1102 return pr->pr_merge; 1103 return pr; 1104 } 1105 } 1106 1107 return NULL; 1108} 1109 1110/* 1111 * Associate an AOUT image with a process. 1112 */ 1113 1114static void 1115pmcstat_process_aout_exec(struct pmcstat_process *pp, 1116 struct pmcstat_image *image, uintfptr_t entryaddr) 1117{ 1118 (void) pp; 1119 (void) image; 1120 (void) entryaddr; 1121 /* TODO Implement a.out handling */ 1122} 1123 1124/* 1125 * Associate an ELF image with a process. 1126 */ 1127 1128static void 1129pmcstat_process_elf_exec(struct pmcstat_process *pp, 1130 struct pmcstat_image *image, uintfptr_t entryaddr) 1131{ 1132 uintmax_t libstart; 1133 struct pmcstat_image *rtldimage; 1134 1135 assert(image->pi_type == PMCSTAT_IMAGE_ELF32 || 1136 image->pi_type == PMCSTAT_IMAGE_ELF64); 1137 1138 /* Create a map entry for the base executable. */ 1139 pmcstat_image_link(pp, image, image->pi_vaddr); 1140 1141 /* 1142 * For dynamically linked executables we need to determine 1143 * where the dynamic linker was mapped to for this process, 1144 * Subsequent executable objects that are mapped in by the 1145 * dynamic linker will be tracked by log events of type 1146 * PMCLOG_TYPE_MAP_IN. 1147 */ 1148 1149 if (image->pi_isdynamic) { 1150 1151 /* 1152 * The runtime loader gets loaded just after the maximum 1153 * possible heap address. Like so: 1154 * 1155 * [ TEXT DATA BSS HEAP -->*RTLD SHLIBS <--STACK] 1156 * ^ ^ 1157 * 0 VM_MAXUSER_ADDRESS 1158 1159 * 1160 * The exact address where the loader gets mapped in 1161 * will vary according to the size of the executable 1162 * and the limits on the size of the process'es data 1163 * segment at the time of exec(). The entry address 1164 * recorded at process exec time corresponds to the 1165 * 'start' address inside the dynamic linker. From 1166 * this we can figure out the address where the 1167 * runtime loader's file object had been mapped to. 1168 */ 1169 rtldimage = pmcstat_image_from_path(image->pi_dynlinkerpath, 0); 1170 if (rtldimage == NULL) { 1171 warnx("WARNING: Cannot find image for \"%s\".", 1172 pmcstat_string_unintern(image->pi_dynlinkerpath)); 1173 pmcstat_stats.ps_exec_errors++; 1174 return; 1175 } 1176 1177 if (rtldimage->pi_type == PMCSTAT_IMAGE_UNKNOWN) 1178 pmcstat_image_get_elf_params(rtldimage); 1179 1180 if (rtldimage->pi_type != PMCSTAT_IMAGE_ELF32 && 1181 rtldimage->pi_type != PMCSTAT_IMAGE_ELF64) { 1182 warnx("WARNING: rtld not an ELF object \"%s\".", 1183 pmcstat_string_unintern(image->pi_dynlinkerpath)); 1184 return; 1185 } 1186 1187 libstart = entryaddr - rtldimage->pi_entry; 1188 pmcstat_image_link(pp, rtldimage, libstart); 1189 } 1190} 1191 1192/* 1193 * Find the process descriptor corresponding to a PID. If 'allocate' 1194 * is zero, we return a NULL if a pid descriptor could not be found or 1195 * a process descriptor process. If 'allocate' is non-zero, then we 1196 * will attempt to allocate a fresh process descriptor. Zombie 1197 * process descriptors are only removed if a fresh allocation for the 1198 * same PID is requested. 1199 */ 1200 1201static struct pmcstat_process * 1202pmcstat_process_lookup(pid_t pid, int allocate) 1203{ 1204 uint32_t hash; 1205 struct pmcstat_pcmap *ppm, *ppmtmp; 1206 struct pmcstat_process *pp, *pptmp; 1207 1208 hash = (uint32_t) pid & PMCSTAT_HASH_MASK; /* simplicity wins */ 1209 1210 LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[hash], pp_next, pptmp) 1211 if (pp->pp_pid == pid) { 1212 /* Found a descriptor, check and process zombies */ 1213 if (allocate && pp->pp_isactive == 0) { 1214 /* remove maps */ 1215 TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, 1216 ppmtmp) { 1217 TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next); 1218 free(ppm); 1219 } 1220 /* remove process entry */ 1221 LIST_REMOVE(pp, pp_next); 1222 free(pp); 1223 break; 1224 } 1225 return (pp); 1226 } 1227 1228 if (!allocate) 1229 return (NULL); 1230 1231 if ((pp = malloc(sizeof(*pp))) == NULL) 1232 err(EX_OSERR, "ERROR: Cannot allocate pid descriptor"); 1233 1234 pp->pp_pid = pid; 1235 pp->pp_isactive = 1; 1236 1237 TAILQ_INIT(&pp->pp_map); 1238 1239 LIST_INSERT_HEAD(&pmcstat_process_hash[hash], pp, pp_next); 1240 return (pp); 1241} 1242 1243/* 1244 * Associate an image and a process. 1245 */ 1246 1247static void 1248pmcstat_process_exec(struct pmcstat_process *pp, 1249 pmcstat_interned_string path, uintfptr_t entryaddr) 1250{ 1251 struct pmcstat_image *image; 1252 1253 if ((image = pmcstat_image_from_path(path, 0)) == NULL) { 1254 pmcstat_stats.ps_exec_errors++; 1255 return; 1256 } 1257 1258 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 1259 pmcstat_image_determine_type(image); 1260 1261 assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN); 1262 1263 switch (image->pi_type) { 1264 case PMCSTAT_IMAGE_ELF32: 1265 case PMCSTAT_IMAGE_ELF64: 1266 pmcstat_stats.ps_exec_elf++; 1267 pmcstat_process_elf_exec(pp, image, entryaddr); 1268 break; 1269 1270 case PMCSTAT_IMAGE_AOUT: 1271 pmcstat_stats.ps_exec_aout++; 1272 pmcstat_process_aout_exec(pp, image, entryaddr); 1273 break; 1274 1275 case PMCSTAT_IMAGE_INDETERMINABLE: 1276 pmcstat_stats.ps_exec_indeterminable++; 1277 break; 1278 1279 default: 1280 err(EX_SOFTWARE, "ERROR: Unsupported executable type for " 1281 "\"%s\"", pmcstat_string_unintern(path)); 1282 } 1283} 1284 1285 1286/* 1287 * Find the map entry associated with process 'p' at PC value 'pc'. 1288 */ 1289 1290struct pmcstat_pcmap * 1291pmcstat_process_find_map(struct pmcstat_process *p, uintfptr_t pc) 1292{ 1293 struct pmcstat_pcmap *ppm; 1294 1295 TAILQ_FOREACH(ppm, &p->pp_map, ppm_next) { 1296 if (pc >= ppm->ppm_lowpc && pc < ppm->ppm_highpc) 1297 return (ppm); 1298 if (pc < ppm->ppm_lowpc) 1299 return (NULL); 1300 } 1301 1302 return (NULL); 1303} 1304 1305/* 1306 * Convert a hwpmc(4) log to profile information. A system-wide 1307 * callgraph is generated if FLAG_DO_CALLGRAPHS is set. gmon.out 1308 * files usable by gprof(1) are created if FLAG_DO_GPROF is set. 1309 */ 1310static int 1311pmcstat_analyze_log(void) 1312{ 1313 uint32_t cpu, cpuflags; 1314 uintfptr_t pc; 1315 pid_t pid; 1316 struct pmcstat_image *image; 1317 struct pmcstat_process *pp, *ppnew; 1318 struct pmcstat_pcmap *ppm, *ppmtmp; 1319 struct pmclog_ev ev; 1320 struct pmcstat_pmcrecord *pmcr; 1321 pmcstat_interned_string image_path; 1322 1323 assert(args.pa_flags & FLAG_DO_ANALYSIS); 1324 1325 if (elf_version(EV_CURRENT) == EV_NONE) 1326 err(EX_UNAVAILABLE, "Elf library intialization failed"); 1327 1328 while (pmclog_read(args.pa_logparser, &ev) == 0) { 1329 assert(ev.pl_state == PMCLOG_OK); 1330 1331 switch (ev.pl_type) { 1332 case PMCLOG_TYPE_INITIALIZE: 1333 if ((ev.pl_u.pl_i.pl_version & 0xFF000000) != 1334 PMC_VERSION_MAJOR << 24 && args.pa_verbosity > 0) 1335 warnx("WARNING: Log version 0x%x does not " 1336 "match compiled version 0x%x.", 1337 ev.pl_u.pl_i.pl_version, 1338 PMC_VERSION_MAJOR); 1339 break; 1340 1341 case PMCLOG_TYPE_MAP_IN: 1342 /* 1343 * Introduce an address range mapping for a 1344 * userland process or the kernel (pid == -1). 1345 * 1346 * We always allocate a process descriptor so 1347 * that subsequent samples seen for this 1348 * address range are mapped to the current 1349 * object being mapped in. 1350 */ 1351 pid = ev.pl_u.pl_mi.pl_pid; 1352 if (pid == -1) 1353 pp = pmcstat_kernproc; 1354 else 1355 pp = pmcstat_process_lookup(pid, 1356 PMCSTAT_ALLOCATE); 1357 1358 assert(pp != NULL); 1359 1360 image_path = pmcstat_string_intern(ev.pl_u.pl_mi. 1361 pl_pathname); 1362 image = pmcstat_image_from_path(image_path, pid == -1); 1363 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN) 1364 pmcstat_image_determine_type(image); 1365 if (image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE) 1366 pmcstat_image_link(pp, image, 1367 ev.pl_u.pl_mi.pl_start); 1368 break; 1369 1370 case PMCLOG_TYPE_MAP_OUT: 1371 /* 1372 * Remove an address map. 1373 */ 1374 pid = ev.pl_u.pl_mo.pl_pid; 1375 if (pid == -1) 1376 pp = pmcstat_kernproc; 1377 else 1378 pp = pmcstat_process_lookup(pid, 0); 1379 1380 if (pp == NULL) /* unknown process */ 1381 break; 1382 1383 pmcstat_image_unmap(pp, ev.pl_u.pl_mo.pl_start, 1384 ev.pl_u.pl_mo.pl_end); 1385 break; 1386 1387 case PMCLOG_TYPE_PCSAMPLE: 1388 /* 1389 * Note: the `PCSAMPLE' log entry is not 1390 * generated by hpwmc(4) after version 2. 1391 */ 1392 1393 /* 1394 * We bring in the gmon file for the image 1395 * currently associated with the PMC & pid 1396 * pair and increment the appropriate entry 1397 * bin inside this. 1398 */ 1399 pmcstat_stats.ps_samples_total++; 1400 1401 pc = ev.pl_u.pl_s.pl_pc; 1402 pp = pmcstat_process_lookup(ev.pl_u.pl_s.pl_pid, 1403 PMCSTAT_ALLOCATE); 1404 1405 /* Get PMC record. */ 1406 pmcr = pmcstat_lookup_pmcid(ev.pl_u.pl_s.pl_pmcid); 1407 assert(pmcr != NULL); 1408 pmcr->pr_samples++; 1409 1410 /* 1411 * Call the plugins processing 1412 * TODO: move pmcstat_process_find_map inside plugins 1413 */ 1414 1415 if (plugins[args.pa_pplugin].pl_process != NULL) 1416 plugins[args.pa_pplugin].pl_process( 1417 pp, pmcr, 1, &pc, 1418 pmcstat_process_find_map(pp, pc) != NULL, 0); 1419 plugins[args.pa_plugin].pl_process( 1420 pp, pmcr, 1, &pc, 1421 pmcstat_process_find_map(pp, pc) != NULL, 0); 1422 break; 1423 1424 case PMCLOG_TYPE_CALLCHAIN: 1425 pmcstat_stats.ps_samples_total++; 1426 1427 cpuflags = ev.pl_u.pl_cc.pl_cpuflags; 1428 cpu = PMC_CALLCHAIN_CPUFLAGS_TO_CPU(cpuflags); 1429 1430 /* Filter on the CPU id. */ 1431 if ((args.pa_cpumask & (1 << cpu)) == 0) { 1432 pmcstat_stats.ps_samples_skipped++; 1433 break; 1434 } 1435 1436 pp = pmcstat_process_lookup(ev.pl_u.pl_cc.pl_pid, 1437 PMCSTAT_ALLOCATE); 1438 1439 /* Get PMC record. */ 1440 pmcr = pmcstat_lookup_pmcid(ev.pl_u.pl_cc.pl_pmcid); 1441 assert(pmcr != NULL); 1442 pmcr->pr_samples++; 1443 1444 /* 1445 * Call the plugins processing 1446 */ 1447 1448 if (plugins[args.pa_pplugin].pl_process != NULL) 1449 plugins[args.pa_pplugin].pl_process( 1450 pp, pmcr, 1451 ev.pl_u.pl_cc.pl_npc, 1452 ev.pl_u.pl_cc.pl_pc, 1453 PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(cpuflags), 1454 cpu); 1455 plugins[args.pa_plugin].pl_process( 1456 pp, pmcr, 1457 ev.pl_u.pl_cc.pl_npc, 1458 ev.pl_u.pl_cc.pl_pc, 1459 PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(cpuflags), 1460 cpu); 1461 break; 1462 1463 case PMCLOG_TYPE_PMCALLOCATE: 1464 /* 1465 * Record the association pmc id between this 1466 * PMC and its name. 1467 */ 1468 pmcstat_pmcid_add(ev.pl_u.pl_a.pl_pmcid, 1469 pmcstat_string_intern(ev.pl_u.pl_a.pl_evname)); 1470 break; 1471 1472 case PMCLOG_TYPE_PROCEXEC: 1473 1474 /* 1475 * Change the executable image associated with 1476 * a process. 1477 */ 1478 pp = pmcstat_process_lookup(ev.pl_u.pl_x.pl_pid, 1479 PMCSTAT_ALLOCATE); 1480 1481 /* delete the current process map */ 1482 TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) { 1483 TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next); 1484 free(ppm); 1485 } 1486 1487 /* associate this process image */ 1488 image_path = pmcstat_string_intern( 1489 ev.pl_u.pl_x.pl_pathname); 1490 assert(image_path != NULL); 1491 pmcstat_process_exec(pp, image_path, 1492 ev.pl_u.pl_x.pl_entryaddr); 1493 break; 1494 1495 case PMCLOG_TYPE_PROCEXIT: 1496 1497 /* 1498 * Due to the way the log is generated, the 1499 * last few samples corresponding to a process 1500 * may appear in the log after the process 1501 * exit event is recorded. Thus we keep the 1502 * process' descriptor and associated data 1503 * structures around, but mark the process as 1504 * having exited. 1505 */ 1506 pp = pmcstat_process_lookup(ev.pl_u.pl_e.pl_pid, 0); 1507 if (pp == NULL) 1508 break; 1509 pp->pp_isactive = 0; /* mark as a zombie */ 1510 break; 1511 1512 case PMCLOG_TYPE_SYSEXIT: 1513 pp = pmcstat_process_lookup(ev.pl_u.pl_se.pl_pid, 0); 1514 if (pp == NULL) 1515 break; 1516 pp->pp_isactive = 0; /* make a zombie */ 1517 break; 1518 1519 case PMCLOG_TYPE_PROCFORK: 1520 1521 /* 1522 * Allocate a process descriptor for the new 1523 * (child) process. 1524 */ 1525 ppnew = 1526 pmcstat_process_lookup(ev.pl_u.pl_f.pl_newpid, 1527 PMCSTAT_ALLOCATE); 1528 1529 /* 1530 * If we had been tracking the parent, clone 1531 * its address maps. 1532 */ 1533 pp = pmcstat_process_lookup(ev.pl_u.pl_f.pl_oldpid, 0); 1534 if (pp == NULL) 1535 break; 1536 TAILQ_FOREACH(ppm, &pp->pp_map, ppm_next) 1537 pmcstat_image_link(ppnew, ppm->ppm_image, 1538 ppm->ppm_lowpc); 1539 break; 1540 1541 default: /* other types of entries are not relevant */ 1542 break; 1543 } 1544 } 1545 1546 if (ev.pl_state == PMCLOG_EOF) 1547 return (PMCSTAT_FINISHED); 1548 else if (ev.pl_state == PMCLOG_REQUIRE_DATA) 1549 return (PMCSTAT_RUNNING); 1550 1551 err(EX_DATAERR, "ERROR: event parsing failed (record %jd, " 1552 "offset 0x%jx)", (uintmax_t) ev.pl_count + 1, ev.pl_offset); 1553} 1554 1555/* 1556 * Print log entries as text. 1557 */ 1558 1559static int 1560pmcstat_print_log(void) 1561{ 1562 struct pmclog_ev ev; 1563 uint32_t npc; 1564 1565 while (pmclog_read(args.pa_logparser, &ev) == 0) { 1566 assert(ev.pl_state == PMCLOG_OK); 1567 switch (ev.pl_type) { 1568 case PMCLOG_TYPE_CALLCHAIN: 1569 PMCSTAT_PRINT_ENTRY("callchain", 1570 "%d 0x%x %d %d %c", ev.pl_u.pl_cc.pl_pid, 1571 ev.pl_u.pl_cc.pl_pmcid, 1572 PMC_CALLCHAIN_CPUFLAGS_TO_CPU(ev.pl_u.pl_cc. \ 1573 pl_cpuflags), ev.pl_u.pl_cc.pl_npc, 1574 PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(ev.pl_u.pl_cc.\ 1575 pl_cpuflags) ? 'u' : 's'); 1576 for (npc = 0; npc < ev.pl_u.pl_cc.pl_npc; npc++) 1577 PMCSTAT_PRINT_ENTRY("...", "%p", 1578 (void *) ev.pl_u.pl_cc.pl_pc[npc]); 1579 break; 1580 case PMCLOG_TYPE_CLOSELOG: 1581 PMCSTAT_PRINT_ENTRY("closelog",); 1582 break; 1583 case PMCLOG_TYPE_DROPNOTIFY: 1584 PMCSTAT_PRINT_ENTRY("drop",); 1585 break; 1586 case PMCLOG_TYPE_INITIALIZE: 1587 PMCSTAT_PRINT_ENTRY("initlog","0x%x \"%s\"", 1588 ev.pl_u.pl_i.pl_version, 1589 pmc_name_of_cputype(ev.pl_u.pl_i.pl_arch)); 1590 if ((ev.pl_u.pl_i.pl_version & 0xFF000000) != 1591 PMC_VERSION_MAJOR << 24 && args.pa_verbosity > 0) 1592 warnx("WARNING: Log version 0x%x != expected " 1593 "version 0x%x.", ev.pl_u.pl_i.pl_version, 1594 PMC_VERSION); 1595 break; 1596 case PMCLOG_TYPE_MAP_IN: 1597 PMCSTAT_PRINT_ENTRY("map-in","%d %p \"%s\"", 1598 ev.pl_u.pl_mi.pl_pid, 1599 (void *) ev.pl_u.pl_mi.pl_start, 1600 ev.pl_u.pl_mi.pl_pathname); 1601 break; 1602 case PMCLOG_TYPE_MAP_OUT: 1603 PMCSTAT_PRINT_ENTRY("map-out","%d %p %p", 1604 ev.pl_u.pl_mo.pl_pid, 1605 (void *) ev.pl_u.pl_mo.pl_start, 1606 (void *) ev.pl_u.pl_mo.pl_end); 1607 break; 1608 case PMCLOG_TYPE_PCSAMPLE: 1609 PMCSTAT_PRINT_ENTRY("sample","0x%x %d %p %c", 1610 ev.pl_u.pl_s.pl_pmcid, 1611 ev.pl_u.pl_s.pl_pid, 1612 (void *) ev.pl_u.pl_s.pl_pc, 1613 ev.pl_u.pl_s.pl_usermode ? 'u' : 's'); 1614 break; 1615 case PMCLOG_TYPE_PMCALLOCATE: 1616 PMCSTAT_PRINT_ENTRY("allocate","0x%x \"%s\" 0x%x", 1617 ev.pl_u.pl_a.pl_pmcid, 1618 ev.pl_u.pl_a.pl_evname, 1619 ev.pl_u.pl_a.pl_flags); 1620 break; 1621 case PMCLOG_TYPE_PMCATTACH: 1622 PMCSTAT_PRINT_ENTRY("attach","0x%x %d \"%s\"", 1623 ev.pl_u.pl_t.pl_pmcid, 1624 ev.pl_u.pl_t.pl_pid, 1625 ev.pl_u.pl_t.pl_pathname); 1626 break; 1627 case PMCLOG_TYPE_PMCDETACH: 1628 PMCSTAT_PRINT_ENTRY("detach","0x%x %d", 1629 ev.pl_u.pl_d.pl_pmcid, 1630 ev.pl_u.pl_d.pl_pid); 1631 break; 1632 case PMCLOG_TYPE_PROCCSW: 1633 PMCSTAT_PRINT_ENTRY("cswval","0x%x %d %jd", 1634 ev.pl_u.pl_c.pl_pmcid, 1635 ev.pl_u.pl_c.pl_pid, 1636 ev.pl_u.pl_c.pl_value); 1637 break; 1638 case PMCLOG_TYPE_PROCEXEC: 1639 PMCSTAT_PRINT_ENTRY("exec","0x%x %d %p \"%s\"", 1640 ev.pl_u.pl_x.pl_pmcid, 1641 ev.pl_u.pl_x.pl_pid, 1642 (void *) ev.pl_u.pl_x.pl_entryaddr, 1643 ev.pl_u.pl_x.pl_pathname); 1644 break; 1645 case PMCLOG_TYPE_PROCEXIT: 1646 PMCSTAT_PRINT_ENTRY("exitval","0x%x %d %jd", 1647 ev.pl_u.pl_e.pl_pmcid, 1648 ev.pl_u.pl_e.pl_pid, 1649 ev.pl_u.pl_e.pl_value); 1650 break; 1651 case PMCLOG_TYPE_PROCFORK: 1652 PMCSTAT_PRINT_ENTRY("fork","%d %d", 1653 ev.pl_u.pl_f.pl_oldpid, 1654 ev.pl_u.pl_f.pl_newpid); 1655 break; 1656 case PMCLOG_TYPE_USERDATA: 1657 PMCSTAT_PRINT_ENTRY("userdata","0x%x", 1658 ev.pl_u.pl_u.pl_userdata); 1659 break; 1660 case PMCLOG_TYPE_SYSEXIT: 1661 PMCSTAT_PRINT_ENTRY("exit","%d", 1662 ev.pl_u.pl_se.pl_pid); 1663 break; 1664 default: 1665 fprintf(args.pa_printfile, "unknown event (type %d).\n", 1666 ev.pl_type); 1667 } 1668 } 1669 1670 if (ev.pl_state == PMCLOG_EOF) 1671 return (PMCSTAT_FINISHED); 1672 else if (ev.pl_state == PMCLOG_REQUIRE_DATA) 1673 return (PMCSTAT_RUNNING); 1674 1675 errx(EX_DATAERR, "ERROR: event parsing failed " 1676 "(record %jd, offset 0x%jx).", 1677 (uintmax_t) ev.pl_count + 1, ev.pl_offset); 1678 /*NOTREACHED*/ 1679} 1680 1681/* 1682 * Public Interfaces. 1683 */ 1684 1685/* 1686 * Close a logfile, after first flushing all in-module queued data. 1687 */ 1688 1689int 1690pmcstat_close_log(void) 1691{ 1692 if (pmc_flush_logfile() < 0) 1693 err(EX_OSERR, "ERROR: logging failed"); 1694 return (args.pa_flags & FLAG_HAS_PIPE ? PMCSTAT_EXITING : 1695 PMCSTAT_FINISHED); 1696} 1697 1698 1699 1700/* 1701 * Open a log file, for reading or writing. 1702 * 1703 * The function returns the fd of a successfully opened log or -1 in 1704 * case of failure. 1705 */ 1706 1707int 1708pmcstat_open_log(const char *path, int mode) 1709{ 1710 int error, fd; 1711 size_t hlen; 1712 const char *p, *errstr; 1713 struct addrinfo hints, *res, *res0; 1714 char hostname[MAXHOSTNAMELEN]; 1715 1716 errstr = NULL; 1717 fd = -1; 1718 1719 /* 1720 * If 'path' is "-" then open one of stdin or stdout depending 1721 * on the value of 'mode'. 1722 * 1723 * If 'path' contains a ':' and does not start with a '/' or '.', 1724 * and is being opened for writing, treat it as a "host:port" 1725 * specification and open a network socket. 1726 * 1727 * Otherwise, treat 'path' as a file name and open that. 1728 */ 1729 if (path[0] == '-' && path[1] == '\0') 1730 fd = (mode == PMCSTAT_OPEN_FOR_READ) ? 0 : 1; 1731 else if (mode == PMCSTAT_OPEN_FOR_WRITE && path[0] != '/' && 1732 path[0] != '.' && strchr(path, ':') != NULL) { 1733 1734 p = strrchr(path, ':'); 1735 hlen = p - path; 1736 if (p == path || hlen >= sizeof(hostname)) { 1737 errstr = strerror(EINVAL); 1738 goto done; 1739 } 1740 1741 assert(hlen < sizeof(hostname)); 1742 (void) strncpy(hostname, path, hlen); 1743 hostname[hlen] = '\0'; 1744 1745 (void) memset(&hints, 0, sizeof(hints)); 1746 hints.ai_family = AF_UNSPEC; 1747 hints.ai_socktype = SOCK_STREAM; 1748 if ((error = getaddrinfo(hostname, p+1, &hints, &res0)) != 0) { 1749 errstr = gai_strerror(error); 1750 goto done; 1751 } 1752 1753 fd = -1; 1754 for (res = res0; res; res = res->ai_next) { 1755 if ((fd = socket(res->ai_family, res->ai_socktype, 1756 res->ai_protocol)) < 0) { 1757 errstr = strerror(errno); 1758 continue; 1759 } 1760 if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) { 1761 errstr = strerror(errno); 1762 (void) close(fd); 1763 fd = -1; 1764 continue; 1765 } 1766 errstr = NULL; 1767 break; 1768 } 1769 freeaddrinfo(res0); 1770 1771 } else if ((fd = open(path, mode == PMCSTAT_OPEN_FOR_READ ? 1772 O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC), 1773 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) 1774 errstr = strerror(errno); 1775 1776 done: 1777 if (errstr) 1778 errx(EX_OSERR, "ERROR: Cannot open \"%s\" for %s: %s.", path, 1779 (mode == PMCSTAT_OPEN_FOR_READ ? "reading" : "writing"), 1780 errstr); 1781 1782 return (fd); 1783} 1784 1785/* 1786 * Process a log file in offline analysis mode. 1787 */ 1788 1789int 1790pmcstat_process_log(void) 1791{ 1792 1793 /* 1794 * If analysis has not been asked for, just print the log to 1795 * the current output file. 1796 */ 1797 if (args.pa_flags & FLAG_DO_PRINT) 1798 return (pmcstat_print_log()); 1799 else 1800 return (pmcstat_analyze_log()); 1801} 1802 1803/* 1804 * Refresh top display. 1805 */ 1806 1807static void 1808pmcstat_refresh_top(void) 1809{ 1810 int v_attrs; 1811 float v; 1812 char pmcname[40]; 1813 struct pmcstat_pmcrecord *pmcpr; 1814 1815 /* If in pause mode do not refresh display. */ 1816 if (pmcstat_pause) 1817 return; 1818 1819 /* Wait until PMC pop in the log. */ 1820 pmcpr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter); 1821 if (pmcpr == NULL) 1822 return; 1823 1824 /* Format PMC name. */ 1825 if (pmcstat_mergepmc) 1826 snprintf(pmcname, sizeof(pmcname), "[%s]", 1827 pmcstat_string_unintern(pmcpr->pr_pmcname)); 1828 else 1829 snprintf(pmcname, sizeof(pmcname), "%s.%d", 1830 pmcstat_string_unintern(pmcpr->pr_pmcname), 1831 pmcstat_pmcinfilter); 1832 1833 /* Format samples count. */ 1834 if (pmcstat_stats.ps_samples_total > 0) 1835 v = (pmcpr->pr_samples * 100.0) / 1836 pmcstat_stats.ps_samples_total; 1837 else 1838 v = 0.; 1839 v_attrs = PMCSTAT_ATTRPERCENT(v); 1840 1841 PMCSTAT_PRINTBEGIN(); 1842 PMCSTAT_PRINTW("PMC: %s Samples: %u ", 1843 pmcname, 1844 pmcpr->pr_samples); 1845 PMCSTAT_ATTRON(v_attrs); 1846 PMCSTAT_PRINTW("(%.1f%%) ", v); 1847 PMCSTAT_ATTROFF(v_attrs); 1848 PMCSTAT_PRINTW(", %u unresolved\n\n", 1849 pmcpr->pr_dubious_frames); 1850 if (plugins[args.pa_plugin].pl_topdisplay != NULL) 1851 plugins[args.pa_plugin].pl_topdisplay(); 1852 PMCSTAT_PRINTEND(); 1853} 1854 1855/* 1856 * Find the next pmc index to display. 1857 */ 1858 1859static void 1860pmcstat_changefilter(void) 1861{ 1862 int pmcin; 1863 struct pmcstat_pmcrecord *pmcr; 1864 1865 /* 1866 * Find the next merge target. 1867 */ 1868 if (pmcstat_mergepmc) { 1869 pmcin = pmcstat_pmcinfilter; 1870 1871 do { 1872 pmcr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter); 1873 if (pmcr == pmcr->pr_merge) 1874 break; 1875 1876 pmcstat_pmcinfilter++; 1877 if (pmcstat_pmcinfilter >= pmcstat_npmcs) 1878 pmcstat_pmcinfilter = 0; 1879 1880 } while (pmcstat_pmcinfilter != pmcin); 1881 } 1882} 1883 1884/* 1885 * Top mode keypress. 1886 */ 1887 1888int 1889pmcstat_keypress_log(void) 1890{ 1891 int c, ret = 0; 1892 WINDOW *w; 1893 1894 w = newwin(1, 0, 1, 0); 1895 c = wgetch(w); 1896 wprintw(w, "Key: %c => ", c); 1897 switch (c) { 1898 case 'c': 1899 wprintw(w, "enter mode 'd' or 'a' => "); 1900 c = wgetch(w); 1901 if (c == 'd') { 1902 args.pa_topmode = PMCSTAT_TOP_DELTA; 1903 wprintw(w, "switching to delta mode"); 1904 } else { 1905 args.pa_topmode = PMCSTAT_TOP_ACCUM; 1906 wprintw(w, "switching to accumulation mode"); 1907 } 1908 break; 1909 case 'm': 1910 pmcstat_mergepmc = !pmcstat_mergepmc; 1911 /* 1912 * Changing merge state require data reset. 1913 */ 1914 if (plugins[args.pa_plugin].pl_shutdown != NULL) 1915 plugins[args.pa_plugin].pl_shutdown(NULL); 1916 pmcstat_stats_reset(); 1917 if (plugins[args.pa_plugin].pl_init != NULL) 1918 plugins[args.pa_plugin].pl_init(); 1919 1920 /* Update filter to be on a merge target. */ 1921 pmcstat_changefilter(); 1922 wprintw(w, "merge PMC %s", pmcstat_mergepmc ? "on" : "off"); 1923 break; 1924 case 'n': 1925 /* Close current plugin. */ 1926 if (plugins[args.pa_plugin].pl_shutdown != NULL) 1927 plugins[args.pa_plugin].pl_shutdown(NULL); 1928 1929 /* Find next top display available. */ 1930 do { 1931 args.pa_plugin++; 1932 if (plugins[args.pa_plugin].pl_name == NULL) 1933 args.pa_plugin = 0; 1934 } while (plugins[args.pa_plugin].pl_topdisplay == NULL); 1935 1936 /* Open new plugin. */ 1937 pmcstat_stats_reset(); 1938 if (plugins[args.pa_plugin].pl_init != NULL) 1939 plugins[args.pa_plugin].pl_init(); 1940 wprintw(w, "switching to plugin %s", 1941 plugins[args.pa_plugin].pl_name); 1942 break; 1943 case 'p': 1944 pmcstat_pmcinfilter++; 1945 if (pmcstat_pmcinfilter >= pmcstat_npmcs) 1946 pmcstat_pmcinfilter = 0; 1947 pmcstat_changefilter(); 1948 wprintw(w, "switching to PMC %s.%d", 1949 pmcstat_pmcindex_to_name(pmcstat_pmcinfilter), 1950 pmcstat_pmcinfilter); 1951 break; 1952 case ' ': 1953 pmcstat_pause = !pmcstat_pause; 1954 if (pmcstat_pause) 1955 wprintw(w, "pause => press space again to continue"); 1956 break; 1957 case 'q': 1958 wprintw(w, "exiting..."); 1959 ret = 1; 1960 default: 1961 if (plugins[args.pa_plugin].pl_topkeypress != NULL) 1962 if (plugins[args.pa_plugin].pl_topkeypress(c, w)) 1963 ret = 1; 1964 } 1965 1966 wrefresh(w); 1967 delwin(w); 1968 return ret; 1969} 1970 1971 1972/* 1973 * Top mode display. 1974 */ 1975 1976void 1977pmcstat_display_log(void) 1978{ 1979 1980 pmcstat_refresh_top(); 1981 1982 /* Reset everythings if delta mode. */ 1983 if (args.pa_topmode == PMCSTAT_TOP_DELTA) { 1984 if (plugins[args.pa_plugin].pl_shutdown != NULL) 1985 plugins[args.pa_plugin].pl_shutdown(NULL); 1986 pmcstat_stats_reset(); 1987 if (plugins[args.pa_plugin].pl_init != NULL) 1988 plugins[args.pa_plugin].pl_init(); 1989 } 1990 1991} 1992 1993/* 1994 * Configure a plugins. 1995 */ 1996 1997void 1998pmcstat_pluginconfigure_log(char *opt) 1999{ 2000 2001 if (strncmp(opt, "threshold=", 10) == 0) { 2002 pmcstat_threshold = atof(opt+10); 2003 } else { 2004 if (plugins[args.pa_plugin].pl_configure != NULL) { 2005 if (!plugins[args.pa_plugin].pl_configure(opt)) 2006 err(EX_USAGE, 2007 "ERROR: unknown option <%s>.", opt); 2008 } 2009 } 2010} 2011 2012/* 2013 * Initialize module. 2014 */ 2015 2016void 2017pmcstat_initialize_logging(void) 2018{ 2019 int i; 2020 2021 /* use a convenient format for 'ldd' output */ 2022 if (setenv("LD_TRACE_LOADED_OBJECTS_FMT1","%o \"%p\" %x\n",1) != 0) 2023 err(EX_OSERR, "ERROR: Cannot setenv"); 2024 2025 /* Initialize hash tables */ 2026 pmcstat_string_initialize(); 2027 for (i = 0; i < PMCSTAT_NHASH; i++) { 2028 LIST_INIT(&pmcstat_image_hash[i]); 2029 LIST_INIT(&pmcstat_process_hash[i]); 2030 } 2031 2032 /* 2033 * Create a fake 'process' entry for the kernel with pid -1. 2034 * hwpmc(4) will subsequently inform us about where the kernel 2035 * and any loaded kernel modules are mapped. 2036 */ 2037 if ((pmcstat_kernproc = pmcstat_process_lookup((pid_t) -1, 2038 PMCSTAT_ALLOCATE)) == NULL) 2039 err(EX_OSERR, "ERROR: Cannot initialize logging"); 2040 2041 /* PMC count. */ 2042 pmcstat_npmcs = 0; 2043 2044 /* Merge PMC with same name. */ 2045 pmcstat_mergepmc = args.pa_mergepmc; 2046 2047 /* 2048 * Initialize plugins 2049 */ 2050 2051 if (plugins[args.pa_pplugin].pl_init != NULL) 2052 plugins[args.pa_pplugin].pl_init(); 2053 if (plugins[args.pa_plugin].pl_init != NULL) 2054 plugins[args.pa_plugin].pl_init(); 2055} 2056 2057/* 2058 * Shutdown module. 2059 */ 2060 2061void 2062pmcstat_shutdown_logging(void) 2063{ 2064 int i; 2065 FILE *mf; 2066 struct pmcstat_image *pi, *pitmp; 2067 struct pmcstat_process *pp, *pptmp; 2068 struct pmcstat_pcmap *ppm, *ppmtmp; 2069 2070 /* determine where to send the map file */ 2071 mf = NULL; 2072 if (args.pa_mapfilename != NULL) 2073 mf = (strcmp(args.pa_mapfilename, "-") == 0) ? 2074 args.pa_printfile : fopen(args.pa_mapfilename, "w"); 2075 2076 if (mf == NULL && args.pa_flags & FLAG_DO_GPROF && 2077 args.pa_verbosity >= 2) 2078 mf = args.pa_printfile; 2079 2080 if (mf) 2081 (void) fprintf(mf, "MAP:\n"); 2082 2083 /* 2084 * Shutdown the plugins 2085 */ 2086 2087 if (plugins[args.pa_plugin].pl_shutdown != NULL) 2088 plugins[args.pa_plugin].pl_shutdown(mf); 2089 if (plugins[args.pa_pplugin].pl_shutdown != NULL) 2090 plugins[args.pa_pplugin].pl_shutdown(mf); 2091 2092 for (i = 0; i < PMCSTAT_NHASH; i++) { 2093 LIST_FOREACH_SAFE(pi, &pmcstat_image_hash[i], pi_next, 2094 pitmp) { 2095 if (plugins[args.pa_plugin].pl_shutdownimage != NULL) 2096 plugins[args.pa_plugin].pl_shutdownimage(pi); 2097 if (plugins[args.pa_pplugin].pl_shutdownimage != NULL) 2098 plugins[args.pa_pplugin].pl_shutdownimage(pi); 2099 2100 free(pi->pi_symbols); 2101 if (pi->pi_addr2line != NULL) 2102 pclose(pi->pi_addr2line); 2103 LIST_REMOVE(pi, pi_next); 2104 free(pi); 2105 } 2106 2107 LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[i], pp_next, 2108 pptmp) { 2109 TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) { 2110 TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next); 2111 free(ppm); 2112 } 2113 LIST_REMOVE(pp, pp_next); 2114 free(pp); 2115 } 2116 } 2117 2118 pmcstat_string_shutdown(); 2119 2120 /* 2121 * Print errors unless -q was specified. Print all statistics 2122 * if verbosity > 1. 2123 */ 2124#define PRINT(N,V) do { \ 2125 if (pmcstat_stats.ps_##V || args.pa_verbosity >= 2) \ 2126 (void) fprintf(args.pa_printfile, " %-40s %d\n",\ 2127 N, pmcstat_stats.ps_##V); \ 2128 } while (0) 2129 2130 if (args.pa_verbosity >= 1 && (args.pa_flags & FLAG_DO_ANALYSIS) && 2131 (args.pa_flags & FLAG_DO_TOP) == 0) { 2132 (void) fprintf(args.pa_printfile, "CONVERSION STATISTICS:\n"); 2133 PRINT("#exec/a.out", exec_aout); 2134 PRINT("#exec/elf", exec_elf); 2135 PRINT("#exec/unknown", exec_indeterminable); 2136 PRINT("#exec handling errors", exec_errors); 2137 PRINT("#samples/total", samples_total); 2138 PRINT("#samples/unclaimed", samples_unknown_offset); 2139 PRINT("#samples/unknown-object", samples_indeterminable); 2140 PRINT("#callchain/dubious-frames", callchain_dubious_frames); 2141 } 2142 2143 if (mf) 2144 (void) fclose(mf); 2145} 2146