2 * Copyright (c) 2005-2007, Joseph Koshy
3 * Copyright (c) 2007 The FreeBSD Foundation
6 * Portions of this software were developed by A. Joseph Koshy under
7 * sponsorship from the FreeBSD Foundation and Google, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
32 * Transform a hwpmc(4) log into human readable form, and into
33 * gprof(1) compatible profiles.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/endian.h>
42 #include <sys/imgact_aout.h>
43 #include <sys/imgact_elf.h>
46 #include <sys/queue.h>
47 #include <sys/socket.h>
51 #include <netinet/in.h>
72 #include "pmcstat_log.h"
73 #include "pmcstat_top.h"
75 #define PMCSTAT_ALLOCATE 1
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
87 * IMPLEMENTATION NOTES
89 * We correlate each 'callchain' or 'sample' entry seen in the event
90 * log back to an executable object in the system. Executable objects
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.
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.
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.
110 * The sample log could have samples from multiple PMCs; we
111 * generate one 'gmon.out' profile per PMC.
113 * IMPLEMENTATION OF GMON OUTPUT
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
121 * IMPLEMENTATION OF SYSTEM-WIDE CALLGRAPH OUTPUT
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.
131 struct pmcstat_pmcs pmcstat_pmcs = LIST_HEAD_INITIALIZER(pmcstat_pmcs);
134 * All image descriptors are kept in a hash table.
136 struct pmcstat_image_hash_list pmcstat_image_hash[PMCSTAT_NHASH];
139 * All process descriptors are kept in a hash table.
141 struct pmcstat_process_hash_list pmcstat_process_hash[PMCSTAT_NHASH];
143 struct pmcstat_stats pmcstat_stats; /* statistics */
145 struct pmcstat_process *pmcstat_kernproc; /* kernel 'process' */
147 #include "pmcpl_gprof.h"
148 #include "pmcpl_callgraph.h"
149 #include "pmcpl_annotate.h"
150 #include "pmcpl_calltree.h"
153 const char *pl_name; /* name */
156 int (*pl_configure)(char *opt);
158 /* init and shutdown */
159 int (*pl_init)(void);
160 void (*pl_shutdown)(FILE *mf);
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);
168 void (*pl_initimage)(struct pmcstat_image *pi);
169 void (*pl_shutdownimage)(struct pmcstat_image *pi);
172 void (*pl_newpmc)(pmcstat_interned_string ps,
173 struct pmcstat_pmcrecord *pr);
176 void (*pl_topdisplay)(void);
179 int (*pl_topkeypress)(int c, WINDOW *w);
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
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
202 .pl_name = "annotate",
203 .pl_process = pmcpl_annotate_process
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
219 int pmcstat_mergepmc;
221 int pmcstat_pmcinfilter = 0; /* PMC filter for top mode. */
222 float pmcstat_threshold = 0.5; /* Cost filter for top mode. */
228 static struct pmcstat_image *pmcstat_image_from_path(pmcstat_interned_string
229 _path, int _iskernelmodule);
230 static void pmcstat_image_get_aout_params(struct pmcstat_image *_image);
231 static void pmcstat_image_get_elf_params(struct pmcstat_image *_image);
232 static void pmcstat_image_link(struct pmcstat_process *_pp,
233 struct pmcstat_image *_i, uintfptr_t _lpc);
235 static void pmcstat_pmcid_add(pmc_id_t _pmcid,
236 pmcstat_interned_string _name);
238 static void pmcstat_process_aout_exec(struct pmcstat_process *_pp,
239 struct pmcstat_image *_image, uintfptr_t _entryaddr);
240 static void pmcstat_process_elf_exec(struct pmcstat_process *_pp,
241 struct pmcstat_image *_image, uintfptr_t _entryaddr);
242 static void pmcstat_process_exec(struct pmcstat_process *_pp,
243 pmcstat_interned_string _path, uintfptr_t _entryaddr);
244 static struct pmcstat_process *pmcstat_process_lookup(pid_t _pid,
246 static int pmcstat_string_compute_hash(const char *_string);
247 static void pmcstat_string_initialize(void);
248 static int pmcstat_string_lookup_hash(pmcstat_interned_string _is);
249 static void pmcstat_string_shutdown(void);
250 static void pmcstat_stats_reset(void);
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.
259 struct pmcstat_string {
260 LIST_ENTRY(pmcstat_string) ps_next; /* hash link */
266 static LIST_HEAD(,pmcstat_string) pmcstat_string_hash[PMCSTAT_NHASH];
274 * PMC Top mode pause state.
279 pmcstat_stats_reset(void)
281 struct pmcstat_pmcrecord *pr;
283 /* Flush PMCs stats. */
284 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) {
286 pr->pr_dubious_frames = 0;
289 /* Flush global stats. */
290 bzero(&pmcstat_stats, sizeof(struct pmcstat_stats));
294 * Compute a 'hash' value for a string.
298 pmcstat_string_compute_hash(const char *s)
302 for (hash = 0; *s; s++)
305 return (hash & PMCSTAT_HASH_MASK);
309 * Intern a copy of string 's', and return a pointer to the
310 * interned structure.
313 pmcstat_interned_string
314 pmcstat_string_intern(const char *s)
316 struct pmcstat_string *ps;
317 const struct pmcstat_string *cps;
320 if ((cps = pmcstat_string_lookup(s)) != NULL)
323 hash = pmcstat_string_compute_hash(s);
326 if ((ps = malloc(sizeof(*ps))) == NULL)
327 err(EX_OSERR, "ERROR: Could not intern string");
330 ps->ps_string = strdup(s);
331 LIST_INSERT_HEAD(&pmcstat_string_hash[hash], ps, ps_next);
332 return ((pmcstat_interned_string) ps);
336 pmcstat_string_unintern(pmcstat_interned_string str)
340 s = ((const struct pmcstat_string *) str)->ps_string;
344 pmcstat_interned_string
345 pmcstat_string_lookup(const char *s)
347 struct pmcstat_string *ps;
350 hash = pmcstat_string_compute_hash(s);
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)
361 pmcstat_string_lookup_hash(pmcstat_interned_string s)
363 const struct pmcstat_string *ps;
365 ps = (const struct pmcstat_string *) s;
366 return (ps->ps_hash);
370 * Initialize the string interning facility.
374 pmcstat_string_initialize(void)
378 for (i = 0; i < PMCSTAT_NHASH; i++)
379 LIST_INIT(&pmcstat_string_hash[i]);
383 * Destroy the string table, free'ing up space.
387 pmcstat_string_shutdown(void)
390 struct pmcstat_string *ps, *pstmp;
392 for (i = 0; i < PMCSTAT_NHASH; i++)
393 LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next,
395 LIST_REMOVE(ps, ps_next);
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.
408 pmcstat_image_get_aout_params(struct pmcstat_image *image)
414 char buffer[PATH_MAX];
416 path = pmcstat_string_unintern(image->pi_execpath);
417 assert(path != NULL);
419 if (image->pi_iskernelmodule)
420 errx(EX_SOFTWARE, "ERROR: a.out kernel modules are "
421 "unsupported \"%s\"", path);
423 (void) snprintf(buffer, sizeof(buffer), "%s%s",
424 args.pa_fsroot, path);
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;
437 if ((unsigned) nbytes != sizeof(ex) ||
441 image->pi_type = PMCSTAT_IMAGE_AOUT;
443 /* TODO: the rest of a.out processing */
453 pmcstat_symbol_compare(const void *a, const void *b)
455 const struct pmcstat_symbol *sym1, *sym2;
457 sym1 = (const struct pmcstat_symbol *) a;
458 sym2 = (const struct pmcstat_symbol *) b;
460 if (sym1->ps_end <= sym2->ps_start)
462 if (sym1->ps_start >= sym2->ps_end)
468 * Map an address to a symbol in an image.
471 struct pmcstat_symbol *
472 pmcstat_symbol_search(struct pmcstat_image *image, uintfptr_t addr)
474 struct pmcstat_symbol sym;
476 if (image->pi_symbols == NULL)
481 sym.ps_end = addr + 1;
483 return (bsearch((void *) &sym, image->pi_symbols,
484 image->pi_symcount, sizeof(struct pmcstat_symbol),
485 pmcstat_symbol_compare));
489 * Add the list of symbols in the given section to the list associated
493 pmcstat_image_add_symbols(struct pmcstat_image *image, Elf *e,
494 Elf_Scn *scn, GElf_Shdr *sh)
497 size_t n, newsyms, nshsyms, nfuncsyms;
498 struct pmcstat_symbol *symptr;
503 if ((data = elf_getdata(scn, NULL)) == NULL)
507 * Determine the number of functions named in this
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)
515 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC)
523 * Allocate space for the new entries.
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. */
530 image->pi_symbols = symptr;
533 * Append new symbols to the end of the current table.
535 symptr += image->pi_symcount;
537 for (n = newsyms = 0; n < nshsyms; n++) {
538 if (gelf_getsym(data, (int) n, &sym) != &sym)
540 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC)
542 if (sym.st_shndx == STN_UNDEF)
545 if (!firsttime && pmcstat_symbol_search(image, sym.st_value))
546 continue; /* We've seen this symbol already. */
548 if ((fnname = elf_strptr(e, sh->sh_link, sym.st_name))
552 symptr->ps_name = pmcstat_string_intern(fnname);
553 symptr->ps_start = sym.st_value - image->pi_vaddr;
554 symptr->ps_end = symptr->ps_start + sym.st_size;
560 image->pi_symcount += newsyms;
562 assert(newsyms <= nfuncsyms);
565 * Return space to the system if there were duplicates.
567 if (newsyms < nfuncsyms)
568 image->pi_symbols = realloc(image->pi_symbols,
569 sizeof(*symptr) * image->pi_symcount);
572 * Keep the list of symbols sorted.
574 qsort(image->pi_symbols, image->pi_symcount, sizeof(*symptr),
575 pmcstat_symbol_compare);
578 * Deal with function symbols that have a size of 'zero' by
579 * making them extend to the next higher address. These
580 * symbols are usually defined in assembly code.
582 for (symptr = image->pi_symbols;
583 symptr < image->pi_symbols + (image->pi_symcount - 1);
585 if (symptr->ps_start == symptr->ps_end)
586 symptr->ps_end = (symptr+1)->ps_start;
590 * Examine an ELF file to determine the size of its text segment.
591 * Sets image->pi_type if anything conclusive can be determined about
596 pmcstat_image_get_elf_params(struct pmcstat_image *image)
600 const char *path, *elfbase;
602 uintfptr_t minva, maxva;
608 enum pmcstat_image_type image_type;
609 char buffer[PATH_MAX];
611 assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN);
613 image->pi_start = minva = ~(uintfptr_t) 0;
614 image->pi_end = maxva = (uintfptr_t) 0;
615 image->pi_type = image_type = PMCSTAT_IMAGE_INDETERMINABLE;
616 image->pi_isdynamic = 0;
617 image->pi_dynlinkerpath = NULL;
620 path = pmcstat_string_unintern(image->pi_execpath);
621 assert(path != NULL);
624 * Look for kernel modules under FSROOT/KERNELPATH/NAME,
625 * and user mode executable objects under FSROOT/PATHNAME.
627 if (image->pi_iskernelmodule)
628 (void) snprintf(buffer, sizeof(buffer), "%s%s/%s",
629 args.pa_fsroot, args.pa_kernel, path);
631 (void) snprintf(buffer, sizeof(buffer), "%s%s",
632 args.pa_fsroot, path);
635 if ((fd = open(buffer, O_RDONLY, 0)) < 0 ||
636 (e = elf_begin(fd, ELF_C_READ, NULL)) == NULL ||
637 (elf_kind(e) != ELF_K_ELF)) {
638 warnx("WARNING: Cannot determine the type of \"%s\".",
643 if (gelf_getehdr(e, &eh) != &eh) {
644 warnx("WARNING: Cannot retrieve the ELF Header for "
645 "\"%s\": %s.", buffer, elf_errmsg(-1));
649 if (eh.e_type != ET_EXEC && eh.e_type != ET_DYN &&
650 !(image->pi_iskernelmodule && eh.e_type == ET_REL)) {
651 warnx("WARNING: \"%s\" is of an unsupported ELF type.",
656 image_type = eh.e_ident[EI_CLASS] == ELFCLASS32 ?
657 PMCSTAT_IMAGE_ELF32 : PMCSTAT_IMAGE_ELF64;
660 * Determine the virtual address where an executable would be
661 * loaded. Additionally, for dynamically linked executables,
662 * save the pathname to the runtime linker.
664 if (eh.e_type == ET_EXEC) {
665 if (elf_getphnum(e, &nph) == 0) {
666 warnx("WARNING: Could not determine the number of "
667 "program headers in \"%s\": %s.", buffer,
671 for (i = 0; i < eh.e_phnum; i++) {
672 if (gelf_getphdr(e, i, &ph) != &ph) {
673 warnx("WARNING: Retrieval of PHDR entry #%ju "
674 "in \"%s\" failed: %s.", (uintmax_t) i,
675 buffer, elf_errmsg(-1));
680 image->pi_isdynamic = 1;
683 if ((elfbase = elf_rawfile(e, NULL)) == NULL) {
684 warnx("WARNING: Cannot retrieve the "
685 "interpreter for \"%s\": %s.",
686 buffer, elf_errmsg(-1));
689 image->pi_dynlinkerpath =
690 pmcstat_string_intern(elfbase +
694 if (ph.p_offset == 0)
695 image->pi_vaddr = ph.p_vaddr;
702 * Get the min and max VA associated with this ELF object.
704 if (elf_getshnum(e, &nsh) == 0) {
705 warnx("WARNING: Could not determine the number of sections "
706 "for \"%s\": %s.", buffer, elf_errmsg(-1));
710 for (i = 0; i < nsh; i++) {
711 if ((scn = elf_getscn(e, i)) == NULL ||
712 gelf_getshdr(scn, &sh) != &sh) {
713 warnx("WARNING: Could not retrieve section header "
714 "#%ju in \"%s\": %s.", (uintmax_t) i, buffer,
718 if (sh.sh_flags & SHF_EXECINSTR) {
719 minva = min(minva, sh.sh_addr);
720 maxva = max(maxva, sh.sh_addr + sh.sh_size);
722 if (sh.sh_type == SHT_SYMTAB || sh.sh_type == SHT_DYNSYM)
723 pmcstat_image_add_symbols(image, e, scn, &sh);
726 image->pi_start = minva;
727 image->pi_end = maxva;
728 image->pi_type = image_type;
729 image->pi_fullpath = pmcstat_string_intern(buffer);
731 /* Build display name
734 for (p = buffer; *p; p++)
737 image->pi_name = pmcstat_string_intern(endp);
747 * Given an image descriptor, determine whether it is an ELF, or AOUT.
748 * If no handler claims the image, set its type to 'INDETERMINABLE'.
752 pmcstat_image_determine_type(struct pmcstat_image *image)
754 assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN);
756 /* Try each kind of handler in turn */
757 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
758 pmcstat_image_get_elf_params(image);
759 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
760 pmcstat_image_get_aout_params(image);
763 * Otherwise, remember that we tried to determine
764 * the object's type and had failed.
766 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
767 image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE;
771 * Locate an image descriptor given an interned path, adding a fresh
772 * descriptor to the cache if necessary. This function also finds a
773 * suitable name for this image's sample file.
775 * We defer filling in the file format specific parts of the image
776 * structure till the time we actually see a sample that would fall
780 static struct pmcstat_image *
781 pmcstat_image_from_path(pmcstat_interned_string internedpath,
785 struct pmcstat_image *pi;
787 hash = pmcstat_string_lookup_hash(internedpath);
789 /* First, look for an existing entry. */
790 LIST_FOREACH(pi, &pmcstat_image_hash[hash], pi_next)
791 if (pi->pi_execpath == internedpath &&
792 pi->pi_iskernelmodule == iskernelmodule)
796 * Allocate a new entry and place it at the head of the hash
799 pi = malloc(sizeof(*pi));
803 pi->pi_type = PMCSTAT_IMAGE_UNKNOWN;
804 pi->pi_execpath = internedpath;
809 pi->pi_isdynamic = 0;
810 pi->pi_iskernelmodule = iskernelmodule;
811 pi->pi_dynlinkerpath = NULL;
812 pi->pi_symbols = NULL;
814 pi->pi_addr2line = NULL;
816 if (plugins[args.pa_pplugin].pl_initimage != NULL)
817 plugins[args.pa_pplugin].pl_initimage(pi);
818 if (plugins[args.pa_plugin].pl_initimage != NULL)
819 plugins[args.pa_plugin].pl_initimage(pi);
821 LIST_INSERT_HEAD(&pmcstat_image_hash[hash], pi, pi_next);
827 * Record the fact that PC values from 'start' to 'end' come from
832 pmcstat_image_link(struct pmcstat_process *pp, struct pmcstat_image *image,
835 struct pmcstat_pcmap *pcm, *pcmnew;
838 assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN &&
839 image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE);
841 if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL)
842 err(EX_OSERR, "ERROR: Cannot create a map entry");
845 * Adjust the map entry to only cover the text portion
849 offset = start - image->pi_vaddr;
850 pcmnew->ppm_lowpc = image->pi_start + offset;
851 pcmnew->ppm_highpc = image->pi_end + offset;
852 pcmnew->ppm_image = image;
854 assert(pcmnew->ppm_lowpc < pcmnew->ppm_highpc);
856 /* Overlapped mmap()'s are assumed to never occur. */
857 TAILQ_FOREACH(pcm, &pp->pp_map, ppm_next)
858 if (pcm->ppm_lowpc >= pcmnew->ppm_highpc)
862 TAILQ_INSERT_TAIL(&pp->pp_map, pcmnew, ppm_next);
864 TAILQ_INSERT_BEFORE(pcm, pcmnew, ppm_next);
868 * Unmap images in the range [start..end) associated with process
873 pmcstat_image_unmap(struct pmcstat_process *pp, uintfptr_t start,
876 struct pmcstat_pcmap *pcm, *pcmtmp, *pcmnew;
883 * - we could have the range completely in the middle of an
884 * existing pcmap; in this case we have to split the pcmap
885 * structure into two (i.e., generate a 'hole').
886 * - we could have the range covering multiple pcmaps; these
887 * will have to be removed.
888 * - we could have either 'start' or 'end' falling in the
889 * middle of a pcmap; in this case shorten the entry.
891 TAILQ_FOREACH_SAFE(pcm, &pp->pp_map, ppm_next, pcmtmp) {
892 assert(pcm->ppm_lowpc < pcm->ppm_highpc);
893 if (pcm->ppm_highpc <= start)
895 if (pcm->ppm_lowpc >= end)
897 if (pcm->ppm_lowpc >= start && pcm->ppm_highpc <= end) {
899 * The current pcmap is completely inside the
900 * unmapped range: remove it entirely.
902 TAILQ_REMOVE(&pp->pp_map, pcm, ppm_next);
904 } else if (pcm->ppm_lowpc < start && pcm->ppm_highpc > end) {
906 * Split this pcmap into two; curtail the
907 * current map to end at [start-1], and start
908 * the new one at [end].
910 if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL)
911 err(EX_OSERR, "ERROR: Cannot split a map "
914 pcmnew->ppm_image = pcm->ppm_image;
916 pcmnew->ppm_lowpc = end;
917 pcmnew->ppm_highpc = pcm->ppm_highpc;
919 pcm->ppm_highpc = start;
921 TAILQ_INSERT_AFTER(&pp->pp_map, pcm, pcmnew, ppm_next);
924 } else if (pcm->ppm_lowpc < start && pcm->ppm_highpc <= end)
925 pcm->ppm_highpc = start;
926 else if (pcm->ppm_lowpc >= start && pcm->ppm_highpc > end)
927 pcm->ppm_lowpc = end;
934 * Resolve file name and line number for the given address.
937 pmcstat_image_addr2line(struct pmcstat_image *image, uintfptr_t addr,
938 char *sourcefile, size_t sourcefile_len, unsigned *sourceline,
939 char *funcname, size_t funcname_len)
941 static int addr2line_warn = 0;
943 char *sep, cmdline[PATH_MAX], imagepath[PATH_MAX];
946 if (image->pi_addr2line == NULL) {
947 snprintf(imagepath, sizeof(imagepath), "%s.symbols",
948 pmcstat_string_unintern(image->pi_fullpath));
949 fd = open(imagepath, O_RDONLY);
951 snprintf(imagepath, sizeof(imagepath), "%s",
952 pmcstat_string_unintern(image->pi_fullpath));
955 snprintf(cmdline, sizeof(cmdline), "addr2line -Cfe \"%s\"",
957 image->pi_addr2line = popen(cmdline, "r+");
958 if (image->pi_addr2line == NULL) {
959 if (!addr2line_warn) {
961 warnx("WARNING: addr2line is needed"
962 "for source code information.");
968 if (feof(image->pi_addr2line) || ferror(image->pi_addr2line)) {
969 warnx("WARNING: addr2line pipe error");
970 pclose(image->pi_addr2line);
971 image->pi_addr2line = NULL;
975 fprintf(image->pi_addr2line, "%p\n", (void *)addr);
977 if (fgets(funcname, funcname_len, image->pi_addr2line) == NULL) {
978 warnx("WARNING: addr2line function name read error");
981 sep = strchr(funcname, '\n');
985 if (fgets(sourcefile, sourcefile_len, image->pi_addr2line) == NULL) {
986 warnx("WARNING: addr2line source file read error");
989 sep = strchr(sourcefile, ':');
991 warnx("WARNING: addr2line source line separator missing");
995 *sourceline = atoi(sep+1);
996 if (*sourceline == 0)
1003 * Add a {pmcid,name} mapping.
1007 pmcstat_pmcid_add(pmc_id_t pmcid, pmcstat_interned_string ps)
1009 struct pmcstat_pmcrecord *pr, *prm;
1011 /* Replace an existing name for the PMC. */
1013 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
1014 if (pr->pr_pmcid == pmcid) {
1015 pr->pr_pmcname = ps;
1017 } else if (pr->pr_pmcname == ps)
1021 * Otherwise, allocate a new descriptor and call the
1024 if ((pr = malloc(sizeof(*pr))) == NULL)
1025 err(EX_OSERR, "ERROR: Cannot allocate pmc record");
1027 pr->pr_pmcid = pmcid;
1028 pr->pr_pmcname = ps;
1029 pr->pr_pmcin = pmcstat_npmcs++;
1031 pr->pr_dubious_frames = 0;
1032 pr->pr_merge = prm == NULL ? pr : prm;
1034 LIST_INSERT_HEAD(&pmcstat_pmcs, pr, pr_next);
1036 if (plugins[args.pa_pplugin].pl_newpmc != NULL)
1037 plugins[args.pa_pplugin].pl_newpmc(ps, pr);
1038 if (plugins[args.pa_plugin].pl_newpmc != NULL)
1039 plugins[args.pa_plugin].pl_newpmc(ps, pr);
1043 * Given a pmcid in use, find its human-readable name.
1047 pmcstat_pmcid_to_name(pmc_id_t pmcid)
1049 struct pmcstat_pmcrecord *pr;
1051 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
1052 if (pr->pr_pmcid == pmcid)
1053 return (pmcstat_string_unintern(pr->pr_pmcname));
1055 err(EX_SOFTWARE, "ERROR: cannot find pmcid");
1060 * Convert PMC index to name.
1064 pmcstat_pmcindex_to_name(int pmcin)
1066 struct pmcstat_pmcrecord *pr;
1068 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
1069 if (pr->pr_pmcin == pmcin)
1070 return pmcstat_string_unintern(pr->pr_pmcname);
1076 * Return PMC record with given index.
1079 struct pmcstat_pmcrecord *
1080 pmcstat_pmcindex_to_pmcr(int pmcin)
1082 struct pmcstat_pmcrecord *pr;
1084 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
1085 if (pr->pr_pmcin == pmcin)
1088 err(EX_SOFTWARE, "ERROR: invalid pmcindex");
1093 * Get PMC record by id, apply merge policy.
1096 static struct pmcstat_pmcrecord *
1097 pmcstat_lookup_pmcid(pmc_id_t pmcid)
1099 struct pmcstat_pmcrecord *pr;
1101 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) {
1102 if (pr->pr_pmcid == pmcid) {
1103 if (pmcstat_mergepmc)
1104 return pr->pr_merge;
1113 * Associate an AOUT image with a process.
1117 pmcstat_process_aout_exec(struct pmcstat_process *pp,
1118 struct pmcstat_image *image, uintfptr_t entryaddr)
1123 /* TODO Implement a.out handling */
1127 * Associate an ELF image with a process.
1131 pmcstat_process_elf_exec(struct pmcstat_process *pp,
1132 struct pmcstat_image *image, uintfptr_t entryaddr)
1135 struct pmcstat_image *rtldimage;
1137 assert(image->pi_type == PMCSTAT_IMAGE_ELF32 ||
1138 image->pi_type == PMCSTAT_IMAGE_ELF64);
1140 /* Create a map entry for the base executable. */
1141 pmcstat_image_link(pp, image, image->pi_vaddr);
1144 * For dynamically linked executables we need to determine
1145 * where the dynamic linker was mapped to for this process,
1146 * Subsequent executable objects that are mapped in by the
1147 * dynamic linker will be tracked by log events of type
1148 * PMCLOG_TYPE_MAP_IN.
1151 if (image->pi_isdynamic) {
1154 * The runtime loader gets loaded just after the maximum
1155 * possible heap address. Like so:
1157 * [ TEXT DATA BSS HEAP -->*RTLD SHLIBS <--STACK]
1159 * 0 VM_MAXUSER_ADDRESS
1162 * The exact address where the loader gets mapped in
1163 * will vary according to the size of the executable
1164 * and the limits on the size of the process'es data
1165 * segment at the time of exec(). The entry address
1166 * recorded at process exec time corresponds to the
1167 * 'start' address inside the dynamic linker. From
1168 * this we can figure out the address where the
1169 * runtime loader's file object had been mapped to.
1171 rtldimage = pmcstat_image_from_path(image->pi_dynlinkerpath, 0);
1172 if (rtldimage == NULL) {
1173 warnx("WARNING: Cannot find image for \"%s\".",
1174 pmcstat_string_unintern(image->pi_dynlinkerpath));
1175 pmcstat_stats.ps_exec_errors++;
1179 if (rtldimage->pi_type == PMCSTAT_IMAGE_UNKNOWN)
1180 pmcstat_image_get_elf_params(rtldimage);
1182 if (rtldimage->pi_type != PMCSTAT_IMAGE_ELF32 &&
1183 rtldimage->pi_type != PMCSTAT_IMAGE_ELF64) {
1184 warnx("WARNING: rtld not an ELF object \"%s\".",
1185 pmcstat_string_unintern(image->pi_dynlinkerpath));
1189 libstart = entryaddr - rtldimage->pi_entry;
1190 pmcstat_image_link(pp, rtldimage, libstart);
1195 * Find the process descriptor corresponding to a PID. If 'allocate'
1196 * is zero, we return a NULL if a pid descriptor could not be found or
1197 * a process descriptor process. If 'allocate' is non-zero, then we
1198 * will attempt to allocate a fresh process descriptor. Zombie
1199 * process descriptors are only removed if a fresh allocation for the
1200 * same PID is requested.
1203 static struct pmcstat_process *
1204 pmcstat_process_lookup(pid_t pid, int allocate)
1207 struct pmcstat_pcmap *ppm, *ppmtmp;
1208 struct pmcstat_process *pp, *pptmp;
1210 hash = (uint32_t) pid & PMCSTAT_HASH_MASK; /* simplicity wins */
1212 LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[hash], pp_next, pptmp)
1213 if (pp->pp_pid == pid) {
1214 /* Found a descriptor, check and process zombies */
1215 if (allocate && pp->pp_isactive == 0) {
1217 TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next,
1219 TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
1222 /* remove process entry */
1223 LIST_REMOVE(pp, pp_next);
1233 if ((pp = malloc(sizeof(*pp))) == NULL)
1234 err(EX_OSERR, "ERROR: Cannot allocate pid descriptor");
1237 pp->pp_isactive = 1;
1239 TAILQ_INIT(&pp->pp_map);
1241 LIST_INSERT_HEAD(&pmcstat_process_hash[hash], pp, pp_next);
1246 * Associate an image and a process.
1250 pmcstat_process_exec(struct pmcstat_process *pp,
1251 pmcstat_interned_string path, uintfptr_t entryaddr)
1253 struct pmcstat_image *image;
1255 if ((image = pmcstat_image_from_path(path, 0)) == NULL) {
1256 pmcstat_stats.ps_exec_errors++;
1260 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
1261 pmcstat_image_determine_type(image);
1263 assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN);
1265 switch (image->pi_type) {
1266 case PMCSTAT_IMAGE_ELF32:
1267 case PMCSTAT_IMAGE_ELF64:
1268 pmcstat_stats.ps_exec_elf++;
1269 pmcstat_process_elf_exec(pp, image, entryaddr);
1272 case PMCSTAT_IMAGE_AOUT:
1273 pmcstat_stats.ps_exec_aout++;
1274 pmcstat_process_aout_exec(pp, image, entryaddr);
1277 case PMCSTAT_IMAGE_INDETERMINABLE:
1278 pmcstat_stats.ps_exec_indeterminable++;
1282 err(EX_SOFTWARE, "ERROR: Unsupported executable type for "
1283 "\"%s\"", pmcstat_string_unintern(path));
1289 * Find the map entry associated with process 'p' at PC value 'pc'.
1292 struct pmcstat_pcmap *
1293 pmcstat_process_find_map(struct pmcstat_process *p, uintfptr_t pc)
1295 struct pmcstat_pcmap *ppm;
1297 TAILQ_FOREACH(ppm, &p->pp_map, ppm_next) {
1298 if (pc >= ppm->ppm_lowpc && pc < ppm->ppm_highpc)
1300 if (pc < ppm->ppm_lowpc)
1308 * Convert a hwpmc(4) log to profile information. A system-wide
1309 * callgraph is generated if FLAG_DO_CALLGRAPHS is set. gmon.out
1310 * files usable by gprof(1) are created if FLAG_DO_GPROF is set.
1313 pmcstat_analyze_log(void)
1315 uint32_t cpu, cpuflags;
1318 struct pmcstat_image *image;
1319 struct pmcstat_process *pp, *ppnew;
1320 struct pmcstat_pcmap *ppm, *ppmtmp;
1321 struct pmclog_ev ev;
1322 struct pmcstat_pmcrecord *pmcr;
1323 pmcstat_interned_string image_path;
1325 assert(args.pa_flags & FLAG_DO_ANALYSIS);
1327 if (elf_version(EV_CURRENT) == EV_NONE)
1328 err(EX_UNAVAILABLE, "Elf library intialization failed");
1330 while (pmclog_read(args.pa_logparser, &ev) == 0) {
1331 assert(ev.pl_state == PMCLOG_OK);
1333 switch (ev.pl_type) {
1334 case PMCLOG_TYPE_INITIALIZE:
1335 if ((ev.pl_u.pl_i.pl_version & 0xFF000000) !=
1336 PMC_VERSION_MAJOR << 24 && args.pa_verbosity > 0)
1337 warnx("WARNING: Log version 0x%x does not "
1338 "match compiled version 0x%x.",
1339 ev.pl_u.pl_i.pl_version,
1343 case PMCLOG_TYPE_MAP_IN:
1345 * Introduce an address range mapping for a
1346 * userland process or the kernel (pid == -1).
1348 * We always allocate a process descriptor so
1349 * that subsequent samples seen for this
1350 * address range are mapped to the current
1351 * object being mapped in.
1353 pid = ev.pl_u.pl_mi.pl_pid;
1355 pp = pmcstat_kernproc;
1357 pp = pmcstat_process_lookup(pid,
1362 image_path = pmcstat_string_intern(ev.pl_u.pl_mi.
1364 image = pmcstat_image_from_path(image_path, pid == -1);
1365 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
1366 pmcstat_image_determine_type(image);
1367 if (image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE)
1368 pmcstat_image_link(pp, image,
1369 ev.pl_u.pl_mi.pl_start);
1372 case PMCLOG_TYPE_MAP_OUT:
1374 * Remove an address map.
1376 pid = ev.pl_u.pl_mo.pl_pid;
1378 pp = pmcstat_kernproc;
1380 pp = pmcstat_process_lookup(pid, 0);
1382 if (pp == NULL) /* unknown process */
1385 pmcstat_image_unmap(pp, ev.pl_u.pl_mo.pl_start,
1386 ev.pl_u.pl_mo.pl_end);
1389 case PMCLOG_TYPE_PCSAMPLE:
1391 * Note: the `PCSAMPLE' log entry is not
1392 * generated by hpwmc(4) after version 2.
1396 * We bring in the gmon file for the image
1397 * currently associated with the PMC & pid
1398 * pair and increment the appropriate entry
1401 pmcstat_stats.ps_samples_total++;
1403 pc = ev.pl_u.pl_s.pl_pc;
1404 pp = pmcstat_process_lookup(ev.pl_u.pl_s.pl_pid,
1407 /* Get PMC record. */
1408 pmcr = pmcstat_lookup_pmcid(ev.pl_u.pl_s.pl_pmcid);
1409 assert(pmcr != NULL);
1413 * Call the plugins processing
1414 * TODO: move pmcstat_process_find_map inside plugins
1417 if (plugins[args.pa_pplugin].pl_process != NULL)
1418 plugins[args.pa_pplugin].pl_process(
1420 pmcstat_process_find_map(pp, pc) != NULL, 0);
1421 plugins[args.pa_plugin].pl_process(
1423 pmcstat_process_find_map(pp, pc) != NULL, 0);
1426 case PMCLOG_TYPE_CALLCHAIN:
1427 pmcstat_stats.ps_samples_total++;
1429 cpuflags = ev.pl_u.pl_cc.pl_cpuflags;
1430 cpu = PMC_CALLCHAIN_CPUFLAGS_TO_CPU(cpuflags);
1432 /* Filter on the CPU id. */
1433 if ((args.pa_cpumask & (1 << cpu)) == 0) {
1434 pmcstat_stats.ps_samples_skipped++;
1438 pp = pmcstat_process_lookup(ev.pl_u.pl_cc.pl_pid,
1441 /* Get PMC record. */
1442 pmcr = pmcstat_lookup_pmcid(ev.pl_u.pl_cc.pl_pmcid);
1443 assert(pmcr != NULL);
1447 * Call the plugins processing
1450 if (plugins[args.pa_pplugin].pl_process != NULL)
1451 plugins[args.pa_pplugin].pl_process(
1453 ev.pl_u.pl_cc.pl_npc,
1454 ev.pl_u.pl_cc.pl_pc,
1455 PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(cpuflags),
1457 plugins[args.pa_plugin].pl_process(
1459 ev.pl_u.pl_cc.pl_npc,
1460 ev.pl_u.pl_cc.pl_pc,
1461 PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(cpuflags),
1465 case PMCLOG_TYPE_PMCALLOCATE:
1467 * Record the association pmc id between this
1470 pmcstat_pmcid_add(ev.pl_u.pl_a.pl_pmcid,
1471 pmcstat_string_intern(ev.pl_u.pl_a.pl_evname));
1474 case PMCLOG_TYPE_PROCEXEC:
1477 * Change the executable image associated with
1480 pp = pmcstat_process_lookup(ev.pl_u.pl_x.pl_pid,
1483 /* delete the current process map */
1484 TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) {
1485 TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
1489 /* associate this process image */
1490 image_path = pmcstat_string_intern(
1491 ev.pl_u.pl_x.pl_pathname);
1492 assert(image_path != NULL);
1493 pmcstat_process_exec(pp, image_path,
1494 ev.pl_u.pl_x.pl_entryaddr);
1497 case PMCLOG_TYPE_PROCEXIT:
1500 * Due to the way the log is generated, the
1501 * last few samples corresponding to a process
1502 * may appear in the log after the process
1503 * exit event is recorded. Thus we keep the
1504 * process' descriptor and associated data
1505 * structures around, but mark the process as
1508 pp = pmcstat_process_lookup(ev.pl_u.pl_e.pl_pid, 0);
1511 pp->pp_isactive = 0; /* mark as a zombie */
1514 case PMCLOG_TYPE_SYSEXIT:
1515 pp = pmcstat_process_lookup(ev.pl_u.pl_se.pl_pid, 0);
1518 pp->pp_isactive = 0; /* make a zombie */
1521 case PMCLOG_TYPE_PROCFORK:
1524 * Allocate a process descriptor for the new
1528 pmcstat_process_lookup(ev.pl_u.pl_f.pl_newpid,
1532 * If we had been tracking the parent, clone
1535 pp = pmcstat_process_lookup(ev.pl_u.pl_f.pl_oldpid, 0);
1538 TAILQ_FOREACH(ppm, &pp->pp_map, ppm_next)
1539 pmcstat_image_link(ppnew, ppm->ppm_image,
1543 default: /* other types of entries are not relevant */
1548 if (ev.pl_state == PMCLOG_EOF)
1549 return (PMCSTAT_FINISHED);
1550 else if (ev.pl_state == PMCLOG_REQUIRE_DATA)
1551 return (PMCSTAT_RUNNING);
1553 err(EX_DATAERR, "ERROR: event parsing failed (record %jd, "
1554 "offset 0x%jx)", (uintmax_t) ev.pl_count + 1, ev.pl_offset);
1558 * Print log entries as text.
1562 pmcstat_print_log(void)
1564 struct pmclog_ev ev;
1567 while (pmclog_read(args.pa_logparser, &ev) == 0) {
1568 assert(ev.pl_state == PMCLOG_OK);
1569 switch (ev.pl_type) {
1570 case PMCLOG_TYPE_CALLCHAIN:
1571 PMCSTAT_PRINT_ENTRY("callchain",
1572 "%d 0x%x %d %d %c", ev.pl_u.pl_cc.pl_pid,
1573 ev.pl_u.pl_cc.pl_pmcid,
1574 PMC_CALLCHAIN_CPUFLAGS_TO_CPU(ev.pl_u.pl_cc. \
1575 pl_cpuflags), ev.pl_u.pl_cc.pl_npc,
1576 PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(ev.pl_u.pl_cc.\
1577 pl_cpuflags) ? 'u' : 's');
1578 for (npc = 0; npc < ev.pl_u.pl_cc.pl_npc; npc++)
1579 PMCSTAT_PRINT_ENTRY("...", "%p",
1580 (void *) ev.pl_u.pl_cc.pl_pc[npc]);
1582 case PMCLOG_TYPE_CLOSELOG:
1583 PMCSTAT_PRINT_ENTRY("closelog",);
1585 case PMCLOG_TYPE_DROPNOTIFY:
1586 PMCSTAT_PRINT_ENTRY("drop",);
1588 case PMCLOG_TYPE_INITIALIZE:
1589 PMCSTAT_PRINT_ENTRY("initlog","0x%x \"%s\"",
1590 ev.pl_u.pl_i.pl_version,
1591 pmc_name_of_cputype(ev.pl_u.pl_i.pl_arch));
1592 if ((ev.pl_u.pl_i.pl_version & 0xFF000000) !=
1593 PMC_VERSION_MAJOR << 24 && args.pa_verbosity > 0)
1594 warnx("WARNING: Log version 0x%x != expected "
1595 "version 0x%x.", ev.pl_u.pl_i.pl_version,
1598 case PMCLOG_TYPE_MAP_IN:
1599 PMCSTAT_PRINT_ENTRY("map-in","%d %p \"%s\"",
1600 ev.pl_u.pl_mi.pl_pid,
1601 (void *) ev.pl_u.pl_mi.pl_start,
1602 ev.pl_u.pl_mi.pl_pathname);
1604 case PMCLOG_TYPE_MAP_OUT:
1605 PMCSTAT_PRINT_ENTRY("map-out","%d %p %p",
1606 ev.pl_u.pl_mo.pl_pid,
1607 (void *) ev.pl_u.pl_mo.pl_start,
1608 (void *) ev.pl_u.pl_mo.pl_end);
1610 case PMCLOG_TYPE_PCSAMPLE:
1611 PMCSTAT_PRINT_ENTRY("sample","0x%x %d %p %c",
1612 ev.pl_u.pl_s.pl_pmcid,
1613 ev.pl_u.pl_s.pl_pid,
1614 (void *) ev.pl_u.pl_s.pl_pc,
1615 ev.pl_u.pl_s.pl_usermode ? 'u' : 's');
1617 case PMCLOG_TYPE_PMCALLOCATE:
1618 PMCSTAT_PRINT_ENTRY("allocate","0x%x \"%s\" 0x%x",
1619 ev.pl_u.pl_a.pl_pmcid,
1620 ev.pl_u.pl_a.pl_evname,
1621 ev.pl_u.pl_a.pl_flags);
1623 case PMCLOG_TYPE_PMCATTACH:
1624 PMCSTAT_PRINT_ENTRY("attach","0x%x %d \"%s\"",
1625 ev.pl_u.pl_t.pl_pmcid,
1626 ev.pl_u.pl_t.pl_pid,
1627 ev.pl_u.pl_t.pl_pathname);
1629 case PMCLOG_TYPE_PMCDETACH:
1630 PMCSTAT_PRINT_ENTRY("detach","0x%x %d",
1631 ev.pl_u.pl_d.pl_pmcid,
1632 ev.pl_u.pl_d.pl_pid);
1634 case PMCLOG_TYPE_PROCCSW:
1635 PMCSTAT_PRINT_ENTRY("cswval","0x%x %d %jd",
1636 ev.pl_u.pl_c.pl_pmcid,
1637 ev.pl_u.pl_c.pl_pid,
1638 ev.pl_u.pl_c.pl_value);
1640 case PMCLOG_TYPE_PROCEXEC:
1641 PMCSTAT_PRINT_ENTRY("exec","0x%x %d %p \"%s\"",
1642 ev.pl_u.pl_x.pl_pmcid,
1643 ev.pl_u.pl_x.pl_pid,
1644 (void *) ev.pl_u.pl_x.pl_entryaddr,
1645 ev.pl_u.pl_x.pl_pathname);
1647 case PMCLOG_TYPE_PROCEXIT:
1648 PMCSTAT_PRINT_ENTRY("exitval","0x%x %d %jd",
1649 ev.pl_u.pl_e.pl_pmcid,
1650 ev.pl_u.pl_e.pl_pid,
1651 ev.pl_u.pl_e.pl_value);
1653 case PMCLOG_TYPE_PROCFORK:
1654 PMCSTAT_PRINT_ENTRY("fork","%d %d",
1655 ev.pl_u.pl_f.pl_oldpid,
1656 ev.pl_u.pl_f.pl_newpid);
1658 case PMCLOG_TYPE_USERDATA:
1659 PMCSTAT_PRINT_ENTRY("userdata","0x%x",
1660 ev.pl_u.pl_u.pl_userdata);
1662 case PMCLOG_TYPE_SYSEXIT:
1663 PMCSTAT_PRINT_ENTRY("exit","%d",
1664 ev.pl_u.pl_se.pl_pid);
1667 fprintf(args.pa_printfile, "unknown event (type %d).\n",
1672 if (ev.pl_state == PMCLOG_EOF)
1673 return (PMCSTAT_FINISHED);
1674 else if (ev.pl_state == PMCLOG_REQUIRE_DATA)
1675 return (PMCSTAT_RUNNING);
1677 errx(EX_DATAERR, "ERROR: event parsing failed "
1678 "(record %jd, offset 0x%jx).",
1679 (uintmax_t) ev.pl_count + 1, ev.pl_offset);
1684 * Public Interfaces.
1688 * Close a logfile, after first flushing all in-module queued data.
1692 pmcstat_close_log(void)
1694 if (pmc_flush_logfile() < 0)
1695 err(EX_OSERR, "ERROR: logging failed");
1696 return (args.pa_flags & FLAG_HAS_PIPE ? PMCSTAT_EXITING :
1703 * Open a log file, for reading or writing.
1705 * The function returns the fd of a successfully opened log or -1 in
1710 pmcstat_open_log(const char *path, int mode)
1714 const char *p, *errstr;
1715 struct addrinfo hints, *res, *res0;
1716 char hostname[MAXHOSTNAMELEN];
1722 * If 'path' is "-" then open one of stdin or stdout depending
1723 * on the value of 'mode'.
1725 * If 'path' contains a ':' and does not start with a '/' or '.',
1726 * and is being opened for writing, treat it as a "host:port"
1727 * specification and open a network socket.
1729 * Otherwise, treat 'path' as a file name and open that.
1731 if (path[0] == '-' && path[1] == '\0')
1732 fd = (mode == PMCSTAT_OPEN_FOR_READ) ? 0 : 1;
1733 else if (mode == PMCSTAT_OPEN_FOR_WRITE && path[0] != '/' &&
1734 path[0] != '.' && strchr(path, ':') != NULL) {
1736 p = strrchr(path, ':');
1738 if (p == path || hlen >= sizeof(hostname)) {
1739 errstr = strerror(EINVAL);
1743 assert(hlen < sizeof(hostname));
1744 (void) strncpy(hostname, path, hlen);
1745 hostname[hlen] = '\0';
1747 (void) memset(&hints, 0, sizeof(hints));
1748 hints.ai_family = AF_UNSPEC;
1749 hints.ai_socktype = SOCK_STREAM;
1750 if ((error = getaddrinfo(hostname, p+1, &hints, &res0)) != 0) {
1751 errstr = gai_strerror(error);
1756 for (res = res0; res; res = res->ai_next) {
1757 if ((fd = socket(res->ai_family, res->ai_socktype,
1758 res->ai_protocol)) < 0) {
1759 errstr = strerror(errno);
1762 if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
1763 errstr = strerror(errno);
1773 } else if ((fd = open(path, mode == PMCSTAT_OPEN_FOR_READ ?
1774 O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC),
1775 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
1776 errstr = strerror(errno);
1780 errx(EX_OSERR, "ERROR: Cannot open \"%s\" for %s: %s.", path,
1781 (mode == PMCSTAT_OPEN_FOR_READ ? "reading" : "writing"),
1788 * Process a log file in offline analysis mode.
1792 pmcstat_process_log(void)
1796 * If analysis has not been asked for, just print the log to
1797 * the current output file.
1799 if (args.pa_flags & FLAG_DO_PRINT)
1800 return (pmcstat_print_log());
1802 return (pmcstat_analyze_log());
1806 * Refresh top display.
1810 pmcstat_refresh_top(void)
1815 struct pmcstat_pmcrecord *pmcpr;
1817 /* If in pause mode do not refresh display. */
1821 /* Wait until PMC pop in the log. */
1822 pmcpr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
1826 /* Format PMC name. */
1827 if (pmcstat_mergepmc)
1828 snprintf(pmcname, sizeof(pmcname), "[%s]",
1829 pmcstat_string_unintern(pmcpr->pr_pmcname));
1831 snprintf(pmcname, sizeof(pmcname), "%s.%d",
1832 pmcstat_string_unintern(pmcpr->pr_pmcname),
1833 pmcstat_pmcinfilter);
1835 /* Format samples count. */
1836 if (pmcstat_stats.ps_samples_total > 0)
1837 v = (pmcpr->pr_samples * 100.0) /
1838 pmcstat_stats.ps_samples_total;
1841 v_attrs = PMCSTAT_ATTRPERCENT(v);
1843 PMCSTAT_PRINTBEGIN();
1844 PMCSTAT_PRINTW("PMC: %s Samples: %u ",
1847 PMCSTAT_ATTRON(v_attrs);
1848 PMCSTAT_PRINTW("(%.1f%%) ", v);
1849 PMCSTAT_ATTROFF(v_attrs);
1850 PMCSTAT_PRINTW(", %u unresolved\n\n",
1851 pmcpr->pr_dubious_frames);
1852 if (plugins[args.pa_plugin].pl_topdisplay != NULL)
1853 plugins[args.pa_plugin].pl_topdisplay();
1858 * Find the next pmc index to display.
1862 pmcstat_changefilter(void)
1865 struct pmcstat_pmcrecord *pmcr;
1868 * Find the next merge target.
1870 if (pmcstat_mergepmc) {
1871 pmcin = pmcstat_pmcinfilter;
1874 pmcr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
1875 if (pmcr == pmcr->pr_merge)
1878 pmcstat_pmcinfilter++;
1879 if (pmcstat_pmcinfilter >= pmcstat_npmcs)
1880 pmcstat_pmcinfilter = 0;
1882 } while (pmcstat_pmcinfilter != pmcin);
1887 * Top mode keypress.
1891 pmcstat_keypress_log(void)
1896 w = newwin(1, 0, 1, 0);
1898 wprintw(w, "Key: %c => ", c);
1901 wprintw(w, "enter mode 'd' or 'a' => ");
1904 args.pa_topmode = PMCSTAT_TOP_DELTA;
1905 wprintw(w, "switching to delta mode");
1907 args.pa_topmode = PMCSTAT_TOP_ACCUM;
1908 wprintw(w, "switching to accumulation mode");
1912 pmcstat_mergepmc = !pmcstat_mergepmc;
1914 * Changing merge state require data reset.
1916 if (plugins[args.pa_plugin].pl_shutdown != NULL)
1917 plugins[args.pa_plugin].pl_shutdown(NULL);
1918 pmcstat_stats_reset();
1919 if (plugins[args.pa_plugin].pl_init != NULL)
1920 plugins[args.pa_plugin].pl_init();
1922 /* Update filter to be on a merge target. */
1923 pmcstat_changefilter();
1924 wprintw(w, "merge PMC %s", pmcstat_mergepmc ? "on" : "off");
1927 /* Close current plugin. */
1928 if (plugins[args.pa_plugin].pl_shutdown != NULL)
1929 plugins[args.pa_plugin].pl_shutdown(NULL);
1931 /* Find next top display available. */
1934 if (plugins[args.pa_plugin].pl_name == NULL)
1936 } while (plugins[args.pa_plugin].pl_topdisplay == NULL);
1938 /* Open new plugin. */
1939 pmcstat_stats_reset();
1940 if (plugins[args.pa_plugin].pl_init != NULL)
1941 plugins[args.pa_plugin].pl_init();
1942 wprintw(w, "switching to plugin %s",
1943 plugins[args.pa_plugin].pl_name);
1946 pmcstat_pmcinfilter++;
1947 if (pmcstat_pmcinfilter >= pmcstat_npmcs)
1948 pmcstat_pmcinfilter = 0;
1949 pmcstat_changefilter();
1950 wprintw(w, "switching to PMC %s.%d",
1951 pmcstat_pmcindex_to_name(pmcstat_pmcinfilter),
1952 pmcstat_pmcinfilter);
1955 pmcstat_pause = !pmcstat_pause;
1957 wprintw(w, "pause => press space again to continue");
1960 wprintw(w, "exiting...");
1963 if (plugins[args.pa_plugin].pl_topkeypress != NULL)
1964 if (plugins[args.pa_plugin].pl_topkeypress(c, w))
1979 pmcstat_display_log(void)
1982 pmcstat_refresh_top();
1984 /* Reset everythings if delta mode. */
1985 if (args.pa_topmode == PMCSTAT_TOP_DELTA) {
1986 if (plugins[args.pa_plugin].pl_shutdown != NULL)
1987 plugins[args.pa_plugin].pl_shutdown(NULL);
1988 pmcstat_stats_reset();
1989 if (plugins[args.pa_plugin].pl_init != NULL)
1990 plugins[args.pa_plugin].pl_init();
1996 * Configure a plugins.
2000 pmcstat_pluginconfigure_log(char *opt)
2003 if (strncmp(opt, "threshold=", 10) == 0) {
2004 pmcstat_threshold = atof(opt+10);
2006 if (plugins[args.pa_plugin].pl_configure != NULL) {
2007 if (!plugins[args.pa_plugin].pl_configure(opt))
2009 "ERROR: unknown option <%s>.", opt);
2015 * Initialize module.
2019 pmcstat_initialize_logging(void)
2023 /* use a convenient format for 'ldd' output */
2024 if (setenv("LD_TRACE_LOADED_OBJECTS_FMT1","%o \"%p\" %x\n",1) != 0)
2025 err(EX_OSERR, "ERROR: Cannot setenv");
2027 /* Initialize hash tables */
2028 pmcstat_string_initialize();
2029 for (i = 0; i < PMCSTAT_NHASH; i++) {
2030 LIST_INIT(&pmcstat_image_hash[i]);
2031 LIST_INIT(&pmcstat_process_hash[i]);
2035 * Create a fake 'process' entry for the kernel with pid -1.
2036 * hwpmc(4) will subsequently inform us about where the kernel
2037 * and any loaded kernel modules are mapped.
2039 if ((pmcstat_kernproc = pmcstat_process_lookup((pid_t) -1,
2040 PMCSTAT_ALLOCATE)) == NULL)
2041 err(EX_OSERR, "ERROR: Cannot initialize logging");
2046 /* Merge PMC with same name. */
2047 pmcstat_mergepmc = args.pa_mergepmc;
2050 * Initialize plugins
2053 if (plugins[args.pa_pplugin].pl_init != NULL)
2054 plugins[args.pa_pplugin].pl_init();
2055 if (plugins[args.pa_plugin].pl_init != NULL)
2056 plugins[args.pa_plugin].pl_init();
2064 pmcstat_shutdown_logging(void)
2068 struct pmcstat_image *pi, *pitmp;
2069 struct pmcstat_process *pp, *pptmp;
2070 struct pmcstat_pcmap *ppm, *ppmtmp;
2072 /* determine where to send the map file */
2074 if (args.pa_mapfilename != NULL)
2075 mf = (strcmp(args.pa_mapfilename, "-") == 0) ?
2076 args.pa_printfile : fopen(args.pa_mapfilename, "w");
2078 if (mf == NULL && args.pa_flags & FLAG_DO_GPROF &&
2079 args.pa_verbosity >= 2)
2080 mf = args.pa_printfile;
2083 (void) fprintf(mf, "MAP:\n");
2086 * Shutdown the plugins
2089 if (plugins[args.pa_plugin].pl_shutdown != NULL)
2090 plugins[args.pa_plugin].pl_shutdown(mf);
2091 if (plugins[args.pa_pplugin].pl_shutdown != NULL)
2092 plugins[args.pa_pplugin].pl_shutdown(mf);
2094 for (i = 0; i < PMCSTAT_NHASH; i++) {
2095 LIST_FOREACH_SAFE(pi, &pmcstat_image_hash[i], pi_next,
2097 if (plugins[args.pa_plugin].pl_shutdownimage != NULL)
2098 plugins[args.pa_plugin].pl_shutdownimage(pi);
2099 if (plugins[args.pa_pplugin].pl_shutdownimage != NULL)
2100 plugins[args.pa_pplugin].pl_shutdownimage(pi);
2102 free(pi->pi_symbols);
2103 if (pi->pi_addr2line != NULL)
2104 pclose(pi->pi_addr2line);
2105 LIST_REMOVE(pi, pi_next);
2109 LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[i], pp_next,
2111 TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) {
2112 TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
2115 LIST_REMOVE(pp, pp_next);
2120 pmcstat_string_shutdown();
2123 * Print errors unless -q was specified. Print all statistics
2126 #define PRINT(N,V) do { \
2127 if (pmcstat_stats.ps_##V || args.pa_verbosity >= 2) \
2128 (void) fprintf(args.pa_printfile, " %-40s %d\n",\
2129 N, pmcstat_stats.ps_##V); \
2132 if (args.pa_verbosity >= 1 && (args.pa_flags & FLAG_DO_ANALYSIS) &&
2133 (args.pa_flags & FLAG_DO_TOP) == 0) {
2134 (void) fprintf(args.pa_printfile, "CONVERSION STATISTICS:\n");
2135 PRINT("#exec/a.out", exec_aout);
2136 PRINT("#exec/elf", exec_elf);
2137 PRINT("#exec/unknown", exec_indeterminable);
2138 PRINT("#exec handling errors", exec_errors);
2139 PRINT("#samples/total", samples_total);
2140 PRINT("#samples/unclaimed", samples_unknown_offset);
2141 PRINT("#samples/unknown-object", samples_indeterminable);
2142 PRINT("#callchain/dubious-frames", callchain_dubious_frames);