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 */
144 int ps_samples_period; /* samples count between top refresh. */
146 struct pmcstat_process *pmcstat_kernproc; /* kernel 'process' */
148 #include "pmcpl_gprof.h"
149 #include "pmcpl_callgraph.h"
150 #include "pmcpl_annotate.h"
151 #include "pmcpl_calltree.h"
154 const char *pl_name; /* name */
157 int (*pl_configure)(char *opt);
159 /* init and shutdown */
160 int (*pl_init)(void);
161 void (*pl_shutdown)(FILE *mf);
163 /* sample processing */
164 void (*pl_process)(struct pmcstat_process *pp,
165 struct pmcstat_pmcrecord *pmcr, uint32_t nsamples,
166 uintfptr_t *cc, int usermode, uint32_t cpu);
169 void (*pl_initimage)(struct pmcstat_image *pi);
170 void (*pl_shutdownimage)(struct pmcstat_image *pi);
173 void (*pl_newpmc)(pmcstat_interned_string ps,
174 struct pmcstat_pmcrecord *pr);
177 void (*pl_topdisplay)(void);
180 int (*pl_topkeypress)(int c, WINDOW *w);
187 .pl_name = "callgraph",
188 .pl_init = pmcpl_cg_init,
189 .pl_shutdown = pmcpl_cg_shutdown,
190 .pl_process = pmcpl_cg_process,
191 .pl_topkeypress = pmcpl_cg_topkeypress,
192 .pl_topdisplay = pmcpl_cg_topdisplay
196 .pl_shutdown = pmcpl_gmon_shutdown,
197 .pl_process = pmcpl_gmon_process,
198 .pl_initimage = pmcpl_gmon_initimage,
199 .pl_shutdownimage = pmcpl_gmon_shutdownimage,
200 .pl_newpmc = pmcpl_gmon_newpmc
203 .pl_name = "annotate",
204 .pl_process = pmcpl_annotate_process
207 .pl_name = "calltree",
208 .pl_configure = pmcpl_ct_configure,
209 .pl_init = pmcpl_ct_init,
210 .pl_shutdown = pmcpl_ct_shutdown,
211 .pl_process = pmcpl_ct_process,
212 .pl_topkeypress = pmcpl_ct_topkeypress,
213 .pl_topdisplay = pmcpl_ct_topdisplay
220 int pmcstat_mergepmc;
222 int pmcstat_pmcinfilter = 0; /* PMC filter for top mode. */
223 float pmcstat_threshold = 0.5; /* Cost filter for top mode. */
229 static struct pmcstat_image *pmcstat_image_from_path(pmcstat_interned_string
230 _path, int _iskernelmodule);
231 static void pmcstat_image_get_aout_params(struct pmcstat_image *_image);
232 static void pmcstat_image_get_elf_params(struct pmcstat_image *_image);
233 static void pmcstat_image_link(struct pmcstat_process *_pp,
234 struct pmcstat_image *_i, uintfptr_t _lpc);
236 static void pmcstat_pmcid_add(pmc_id_t _pmcid,
237 pmcstat_interned_string _name);
239 static void pmcstat_process_aout_exec(struct pmcstat_process *_pp,
240 struct pmcstat_image *_image, uintfptr_t _entryaddr);
241 static void pmcstat_process_elf_exec(struct pmcstat_process *_pp,
242 struct pmcstat_image *_image, uintfptr_t _entryaddr);
243 static void pmcstat_process_exec(struct pmcstat_process *_pp,
244 pmcstat_interned_string _path, uintfptr_t _entryaddr);
245 static struct pmcstat_process *pmcstat_process_lookup(pid_t _pid,
247 static int pmcstat_string_compute_hash(const char *_string);
248 static void pmcstat_string_initialize(void);
249 static int pmcstat_string_lookup_hash(pmcstat_interned_string _is);
250 static void pmcstat_string_shutdown(void);
251 static void pmcstat_stats_reset(int _reset_global);
254 * A simple implementation of interned strings. Each interned string
255 * is assigned a unique address, so that subsequent string compares
256 * can be done by a simple pointer comparision instead of using
257 * strcmp(). This speeds up hash table lookups and saves memory if
258 * duplicate strings are the norm.
260 struct pmcstat_string {
261 LIST_ENTRY(pmcstat_string) ps_next; /* hash link */
267 static LIST_HEAD(,pmcstat_string) pmcstat_string_hash[PMCSTAT_NHASH];
275 * PMC Top mode pause state.
280 pmcstat_stats_reset(int reset_global)
282 struct pmcstat_pmcrecord *pr;
284 /* Flush PMCs stats. */
285 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) {
287 pr->pr_dubious_frames = 0;
289 ps_samples_period = 0;
291 /* Flush global stats. */
293 bzero(&pmcstat_stats, sizeof(struct pmcstat_stats));
297 * Compute a 'hash' value for a string.
301 pmcstat_string_compute_hash(const char *s)
305 for (hash = 0; *s; s++)
308 return (hash & PMCSTAT_HASH_MASK);
312 * Intern a copy of string 's', and return a pointer to the
313 * interned structure.
316 pmcstat_interned_string
317 pmcstat_string_intern(const char *s)
319 struct pmcstat_string *ps;
320 const struct pmcstat_string *cps;
323 if ((cps = pmcstat_string_lookup(s)) != NULL)
326 hash = pmcstat_string_compute_hash(s);
329 if ((ps = malloc(sizeof(*ps))) == NULL)
330 err(EX_OSERR, "ERROR: Could not intern string");
333 ps->ps_string = strdup(s);
334 LIST_INSERT_HEAD(&pmcstat_string_hash[hash], ps, ps_next);
335 return ((pmcstat_interned_string) ps);
339 pmcstat_string_unintern(pmcstat_interned_string str)
343 s = ((const struct pmcstat_string *) str)->ps_string;
347 pmcstat_interned_string
348 pmcstat_string_lookup(const char *s)
350 struct pmcstat_string *ps;
353 hash = pmcstat_string_compute_hash(s);
356 LIST_FOREACH(ps, &pmcstat_string_hash[hash], ps_next)
357 if (ps->ps_len == len && ps->ps_hash == hash &&
358 strcmp(ps->ps_string, s) == 0)
364 pmcstat_string_lookup_hash(pmcstat_interned_string s)
366 const struct pmcstat_string *ps;
368 ps = (const struct pmcstat_string *) s;
369 return (ps->ps_hash);
373 * Initialize the string interning facility.
377 pmcstat_string_initialize(void)
381 for (i = 0; i < PMCSTAT_NHASH; i++)
382 LIST_INIT(&pmcstat_string_hash[i]);
386 * Destroy the string table, free'ing up space.
390 pmcstat_string_shutdown(void)
393 struct pmcstat_string *ps, *pstmp;
395 for (i = 0; i < PMCSTAT_NHASH; i++)
396 LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next,
398 LIST_REMOVE(ps, ps_next);
405 * Determine whether a given executable image is an A.OUT object, and
406 * if so, fill in its parameters from the text file.
407 * Sets image->pi_type.
411 pmcstat_image_get_aout_params(struct pmcstat_image *image)
417 char buffer[PATH_MAX];
419 path = pmcstat_string_unintern(image->pi_execpath);
420 assert(path != NULL);
422 if (image->pi_iskernelmodule)
423 errx(EX_SOFTWARE, "ERROR: a.out kernel modules are "
424 "unsupported \"%s\"", path);
426 (void) snprintf(buffer, sizeof(buffer), "%s%s",
427 args.pa_fsroot, path);
429 if ((fd = open(buffer, O_RDONLY, 0)) < 0 ||
430 (nbytes = read(fd, &ex, sizeof(ex))) < 0) {
431 warn("WARNING: Cannot determine type of \"%s\"", path);
432 image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE;
440 if ((unsigned) nbytes != sizeof(ex) ||
444 image->pi_type = PMCSTAT_IMAGE_AOUT;
446 /* TODO: the rest of a.out processing */
456 pmcstat_symbol_compare(const void *a, const void *b)
458 const struct pmcstat_symbol *sym1, *sym2;
460 sym1 = (const struct pmcstat_symbol *) a;
461 sym2 = (const struct pmcstat_symbol *) b;
463 if (sym1->ps_end <= sym2->ps_start)
465 if (sym1->ps_start >= sym2->ps_end)
471 * Map an address to a symbol in an image.
474 struct pmcstat_symbol *
475 pmcstat_symbol_search(struct pmcstat_image *image, uintfptr_t addr)
477 struct pmcstat_symbol sym;
479 if (image->pi_symbols == NULL)
484 sym.ps_end = addr + 1;
486 return (bsearch((void *) &sym, image->pi_symbols,
487 image->pi_symcount, sizeof(struct pmcstat_symbol),
488 pmcstat_symbol_compare));
492 * Add the list of symbols in the given section to the list associated
496 pmcstat_image_add_symbols(struct pmcstat_image *image, Elf *e,
497 Elf_Scn *scn, GElf_Shdr *sh)
500 size_t n, newsyms, nshsyms, nfuncsyms;
501 struct pmcstat_symbol *symptr;
506 if ((data = elf_getdata(scn, NULL)) == NULL)
510 * Determine the number of functions named in this
514 nshsyms = sh->sh_size / sh->sh_entsize;
515 for (n = nfuncsyms = 0; n < nshsyms; n++) {
516 if (gelf_getsym(data, (int) n, &sym) != &sym)
518 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC)
526 * Allocate space for the new entries.
528 firsttime = image->pi_symbols == NULL;
529 symptr = realloc(image->pi_symbols,
530 sizeof(*symptr) * (image->pi_symcount + nfuncsyms));
531 if (symptr == image->pi_symbols) /* realloc() failed. */
533 image->pi_symbols = symptr;
536 * Append new symbols to the end of the current table.
538 symptr += image->pi_symcount;
540 for (n = newsyms = 0; n < nshsyms; n++) {
541 if (gelf_getsym(data, (int) n, &sym) != &sym)
543 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC)
545 if (sym.st_shndx == STN_UNDEF)
548 if (!firsttime && pmcstat_symbol_search(image, sym.st_value))
549 continue; /* We've seen this symbol already. */
551 if ((fnname = elf_strptr(e, sh->sh_link, sym.st_name))
555 symptr->ps_name = pmcstat_string_intern(fnname);
556 symptr->ps_start = sym.st_value - image->pi_vaddr;
557 symptr->ps_end = symptr->ps_start + sym.st_size;
563 image->pi_symcount += newsyms;
565 assert(newsyms <= nfuncsyms);
568 * Return space to the system if there were duplicates.
570 if (newsyms < nfuncsyms)
571 image->pi_symbols = realloc(image->pi_symbols,
572 sizeof(*symptr) * image->pi_symcount);
575 * Keep the list of symbols sorted.
577 qsort(image->pi_symbols, image->pi_symcount, sizeof(*symptr),
578 pmcstat_symbol_compare);
581 * Deal with function symbols that have a size of 'zero' by
582 * making them extend to the next higher address. These
583 * symbols are usually defined in assembly code.
585 for (symptr = image->pi_symbols;
586 symptr < image->pi_symbols + (image->pi_symcount - 1);
588 if (symptr->ps_start == symptr->ps_end)
589 symptr->ps_end = (symptr+1)->ps_start;
593 * Examine an ELF file to determine the size of its text segment.
594 * Sets image->pi_type if anything conclusive can be determined about
599 pmcstat_image_get_elf_params(struct pmcstat_image *image)
603 const char *path, *elfbase;
605 uintfptr_t minva, maxva;
611 enum pmcstat_image_type image_type;
612 char buffer[PATH_MAX];
614 assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN);
616 image->pi_start = minva = ~(uintfptr_t) 0;
617 image->pi_end = maxva = (uintfptr_t) 0;
618 image->pi_type = image_type = PMCSTAT_IMAGE_INDETERMINABLE;
619 image->pi_isdynamic = 0;
620 image->pi_dynlinkerpath = NULL;
623 path = pmcstat_string_unintern(image->pi_execpath);
624 assert(path != NULL);
627 * Look for kernel modules under FSROOT/KERNELPATH/NAME,
628 * and user mode executable objects under FSROOT/PATHNAME.
630 if (image->pi_iskernelmodule)
631 (void) snprintf(buffer, sizeof(buffer), "%s%s/%s",
632 args.pa_fsroot, args.pa_kernel, path);
634 (void) snprintf(buffer, sizeof(buffer), "%s%s",
635 args.pa_fsroot, path);
638 if ((fd = open(buffer, O_RDONLY, 0)) < 0 ||
639 (e = elf_begin(fd, ELF_C_READ, NULL)) == NULL ||
640 (elf_kind(e) != ELF_K_ELF)) {
641 warnx("WARNING: Cannot determine the type of \"%s\".",
646 if (gelf_getehdr(e, &eh) != &eh) {
647 warnx("WARNING: Cannot retrieve the ELF Header for "
648 "\"%s\": %s.", buffer, elf_errmsg(-1));
652 if (eh.e_type != ET_EXEC && eh.e_type != ET_DYN &&
653 !(image->pi_iskernelmodule && eh.e_type == ET_REL)) {
654 warnx("WARNING: \"%s\" is of an unsupported ELF type.",
659 image_type = eh.e_ident[EI_CLASS] == ELFCLASS32 ?
660 PMCSTAT_IMAGE_ELF32 : PMCSTAT_IMAGE_ELF64;
663 * Determine the virtual address where an executable would be
664 * loaded. Additionally, for dynamically linked executables,
665 * save the pathname to the runtime linker.
667 if (eh.e_type == ET_EXEC) {
668 if (elf_getphnum(e, &nph) == 0) {
669 warnx("WARNING: Could not determine the number of "
670 "program headers in \"%s\": %s.", buffer,
674 for (i = 0; i < eh.e_phnum; i++) {
675 if (gelf_getphdr(e, i, &ph) != &ph) {
676 warnx("WARNING: Retrieval of PHDR entry #%ju "
677 "in \"%s\" failed: %s.", (uintmax_t) i,
678 buffer, elf_errmsg(-1));
683 image->pi_isdynamic = 1;
686 if ((elfbase = elf_rawfile(e, NULL)) == NULL) {
687 warnx("WARNING: Cannot retrieve the "
688 "interpreter for \"%s\": %s.",
689 buffer, elf_errmsg(-1));
692 image->pi_dynlinkerpath =
693 pmcstat_string_intern(elfbase +
697 if (ph.p_offset == 0)
698 image->pi_vaddr = ph.p_vaddr;
705 * Get the min and max VA associated with this ELF object.
707 if (elf_getshnum(e, &nsh) == 0) {
708 warnx("WARNING: Could not determine the number of sections "
709 "for \"%s\": %s.", buffer, elf_errmsg(-1));
713 for (i = 0; i < nsh; i++) {
714 if ((scn = elf_getscn(e, i)) == NULL ||
715 gelf_getshdr(scn, &sh) != &sh) {
716 warnx("WARNING: Could not retrieve section header "
717 "#%ju in \"%s\": %s.", (uintmax_t) i, buffer,
721 if (sh.sh_flags & SHF_EXECINSTR) {
722 minva = min(minva, sh.sh_addr);
723 maxva = max(maxva, sh.sh_addr + sh.sh_size);
725 if (sh.sh_type == SHT_SYMTAB || sh.sh_type == SHT_DYNSYM)
726 pmcstat_image_add_symbols(image, e, scn, &sh);
729 image->pi_start = minva;
730 image->pi_end = maxva;
731 image->pi_type = image_type;
732 image->pi_fullpath = pmcstat_string_intern(buffer);
734 /* Build display name
737 for (p = buffer; *p; p++)
740 image->pi_name = pmcstat_string_intern(endp);
750 * Given an image descriptor, determine whether it is an ELF, or AOUT.
751 * If no handler claims the image, set its type to 'INDETERMINABLE'.
755 pmcstat_image_determine_type(struct pmcstat_image *image)
757 assert(image->pi_type == PMCSTAT_IMAGE_UNKNOWN);
759 /* Try each kind of handler in turn */
760 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
761 pmcstat_image_get_elf_params(image);
762 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
763 pmcstat_image_get_aout_params(image);
766 * Otherwise, remember that we tried to determine
767 * the object's type and had failed.
769 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
770 image->pi_type = PMCSTAT_IMAGE_INDETERMINABLE;
774 * Locate an image descriptor given an interned path, adding a fresh
775 * descriptor to the cache if necessary. This function also finds a
776 * suitable name for this image's sample file.
778 * We defer filling in the file format specific parts of the image
779 * structure till the time we actually see a sample that would fall
783 static struct pmcstat_image *
784 pmcstat_image_from_path(pmcstat_interned_string internedpath,
788 struct pmcstat_image *pi;
790 hash = pmcstat_string_lookup_hash(internedpath);
792 /* First, look for an existing entry. */
793 LIST_FOREACH(pi, &pmcstat_image_hash[hash], pi_next)
794 if (pi->pi_execpath == internedpath &&
795 pi->pi_iskernelmodule == iskernelmodule)
799 * Allocate a new entry and place it at the head of the hash
802 pi = malloc(sizeof(*pi));
806 pi->pi_type = PMCSTAT_IMAGE_UNKNOWN;
807 pi->pi_execpath = internedpath;
812 pi->pi_isdynamic = 0;
813 pi->pi_iskernelmodule = iskernelmodule;
814 pi->pi_dynlinkerpath = NULL;
815 pi->pi_symbols = NULL;
817 pi->pi_addr2line = NULL;
819 if (plugins[args.pa_pplugin].pl_initimage != NULL)
820 plugins[args.pa_pplugin].pl_initimage(pi);
821 if (plugins[args.pa_plugin].pl_initimage != NULL)
822 plugins[args.pa_plugin].pl_initimage(pi);
824 LIST_INSERT_HEAD(&pmcstat_image_hash[hash], pi, pi_next);
830 * Record the fact that PC values from 'start' to 'end' come from
835 pmcstat_image_link(struct pmcstat_process *pp, struct pmcstat_image *image,
838 struct pmcstat_pcmap *pcm, *pcmnew;
841 assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN &&
842 image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE);
844 if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL)
845 err(EX_OSERR, "ERROR: Cannot create a map entry");
848 * Adjust the map entry to only cover the text portion
852 offset = start - image->pi_vaddr;
853 pcmnew->ppm_lowpc = image->pi_start + offset;
854 pcmnew->ppm_highpc = image->pi_end + offset;
855 pcmnew->ppm_image = image;
857 assert(pcmnew->ppm_lowpc < pcmnew->ppm_highpc);
859 /* Overlapped mmap()'s are assumed to never occur. */
860 TAILQ_FOREACH(pcm, &pp->pp_map, ppm_next)
861 if (pcm->ppm_lowpc >= pcmnew->ppm_highpc)
865 TAILQ_INSERT_TAIL(&pp->pp_map, pcmnew, ppm_next);
867 TAILQ_INSERT_BEFORE(pcm, pcmnew, ppm_next);
871 * Unmap images in the range [start..end) associated with process
876 pmcstat_image_unmap(struct pmcstat_process *pp, uintfptr_t start,
879 struct pmcstat_pcmap *pcm, *pcmtmp, *pcmnew;
886 * - we could have the range completely in the middle of an
887 * existing pcmap; in this case we have to split the pcmap
888 * structure into two (i.e., generate a 'hole').
889 * - we could have the range covering multiple pcmaps; these
890 * will have to be removed.
891 * - we could have either 'start' or 'end' falling in the
892 * middle of a pcmap; in this case shorten the entry.
894 TAILQ_FOREACH_SAFE(pcm, &pp->pp_map, ppm_next, pcmtmp) {
895 assert(pcm->ppm_lowpc < pcm->ppm_highpc);
896 if (pcm->ppm_highpc <= start)
898 if (pcm->ppm_lowpc >= end)
900 if (pcm->ppm_lowpc >= start && pcm->ppm_highpc <= end) {
902 * The current pcmap is completely inside the
903 * unmapped range: remove it entirely.
905 TAILQ_REMOVE(&pp->pp_map, pcm, ppm_next);
907 } else if (pcm->ppm_lowpc < start && pcm->ppm_highpc > end) {
909 * Split this pcmap into two; curtail the
910 * current map to end at [start-1], and start
911 * the new one at [end].
913 if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL)
914 err(EX_OSERR, "ERROR: Cannot split a map "
917 pcmnew->ppm_image = pcm->ppm_image;
919 pcmnew->ppm_lowpc = end;
920 pcmnew->ppm_highpc = pcm->ppm_highpc;
922 pcm->ppm_highpc = start;
924 TAILQ_INSERT_AFTER(&pp->pp_map, pcm, pcmnew, ppm_next);
927 } else if (pcm->ppm_lowpc < start && pcm->ppm_highpc <= end)
928 pcm->ppm_highpc = start;
929 else if (pcm->ppm_lowpc >= start && pcm->ppm_highpc > end)
930 pcm->ppm_lowpc = end;
937 * Resolve file name and line number for the given address.
940 pmcstat_image_addr2line(struct pmcstat_image *image, uintfptr_t addr,
941 char *sourcefile, size_t sourcefile_len, unsigned *sourceline,
942 char *funcname, size_t funcname_len)
944 static int addr2line_warn = 0;
946 char *sep, cmdline[PATH_MAX], imagepath[PATH_MAX];
949 if (image->pi_addr2line == NULL) {
950 snprintf(imagepath, sizeof(imagepath), "%s%s.symbols",
952 pmcstat_string_unintern(image->pi_fullpath));
953 fd = open(imagepath, O_RDONLY);
955 snprintf(imagepath, sizeof(imagepath), "%s%s",
957 pmcstat_string_unintern(image->pi_fullpath));
960 snprintf(cmdline, sizeof(cmdline), "addr2line -Cfe \"%s\"",
962 image->pi_addr2line = popen(cmdline, "r+");
963 if (image->pi_addr2line == NULL) {
964 if (!addr2line_warn) {
966 warnx("WARNING: addr2line is needed"
967 "for source code information.");
973 if (feof(image->pi_addr2line) || ferror(image->pi_addr2line)) {
974 warnx("WARNING: addr2line pipe error");
975 pclose(image->pi_addr2line);
976 image->pi_addr2line = NULL;
980 fprintf(image->pi_addr2line, "%p\n", (void *)addr);
982 if (fgets(funcname, funcname_len, image->pi_addr2line) == NULL) {
983 warnx("WARNING: addr2line function name read error");
986 sep = strchr(funcname, '\n');
990 if (fgets(sourcefile, sourcefile_len, image->pi_addr2line) == NULL) {
991 warnx("WARNING: addr2line source file read error");
994 sep = strchr(sourcefile, ':');
996 warnx("WARNING: addr2line source line separator missing");
1000 *sourceline = atoi(sep+1);
1001 if (*sourceline == 0)
1008 * Add a {pmcid,name} mapping.
1012 pmcstat_pmcid_add(pmc_id_t pmcid, pmcstat_interned_string ps)
1014 struct pmcstat_pmcrecord *pr, *prm;
1016 /* Replace an existing name for the PMC. */
1018 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
1019 if (pr->pr_pmcid == pmcid) {
1020 pr->pr_pmcname = ps;
1022 } else if (pr->pr_pmcname == ps)
1026 * Otherwise, allocate a new descriptor and call the
1029 if ((pr = malloc(sizeof(*pr))) == NULL)
1030 err(EX_OSERR, "ERROR: Cannot allocate pmc record");
1032 pr->pr_pmcid = pmcid;
1033 pr->pr_pmcname = ps;
1034 pr->pr_pmcin = pmcstat_npmcs++;
1036 pr->pr_dubious_frames = 0;
1037 pr->pr_merge = prm == NULL ? pr : prm;
1039 LIST_INSERT_HEAD(&pmcstat_pmcs, pr, pr_next);
1041 if (plugins[args.pa_pplugin].pl_newpmc != NULL)
1042 plugins[args.pa_pplugin].pl_newpmc(ps, pr);
1043 if (plugins[args.pa_plugin].pl_newpmc != NULL)
1044 plugins[args.pa_plugin].pl_newpmc(ps, pr);
1048 * Given a pmcid in use, find its human-readable name.
1052 pmcstat_pmcid_to_name(pmc_id_t pmcid)
1054 struct pmcstat_pmcrecord *pr;
1056 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
1057 if (pr->pr_pmcid == pmcid)
1058 return (pmcstat_string_unintern(pr->pr_pmcname));
1060 err(EX_SOFTWARE, "ERROR: cannot find pmcid");
1065 * Convert PMC index to name.
1069 pmcstat_pmcindex_to_name(int pmcin)
1071 struct pmcstat_pmcrecord *pr;
1073 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
1074 if (pr->pr_pmcin == pmcin)
1075 return pmcstat_string_unintern(pr->pr_pmcname);
1081 * Return PMC record with given index.
1084 struct pmcstat_pmcrecord *
1085 pmcstat_pmcindex_to_pmcr(int pmcin)
1087 struct pmcstat_pmcrecord *pr;
1089 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
1090 if (pr->pr_pmcin == pmcin)
1093 err(EX_SOFTWARE, "ERROR: invalid pmcindex");
1098 * Get PMC record by id, apply merge policy.
1101 static struct pmcstat_pmcrecord *
1102 pmcstat_lookup_pmcid(pmc_id_t pmcid)
1104 struct pmcstat_pmcrecord *pr;
1106 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) {
1107 if (pr->pr_pmcid == pmcid) {
1108 if (pmcstat_mergepmc)
1109 return pr->pr_merge;
1118 * Associate an AOUT image with a process.
1122 pmcstat_process_aout_exec(struct pmcstat_process *pp,
1123 struct pmcstat_image *image, uintfptr_t entryaddr)
1128 /* TODO Implement a.out handling */
1132 * Associate an ELF image with a process.
1136 pmcstat_process_elf_exec(struct pmcstat_process *pp,
1137 struct pmcstat_image *image, uintfptr_t entryaddr)
1140 struct pmcstat_image *rtldimage;
1142 assert(image->pi_type == PMCSTAT_IMAGE_ELF32 ||
1143 image->pi_type == PMCSTAT_IMAGE_ELF64);
1145 /* Create a map entry for the base executable. */
1146 pmcstat_image_link(pp, image, image->pi_vaddr);
1149 * For dynamically linked executables we need to determine
1150 * where the dynamic linker was mapped to for this process,
1151 * Subsequent executable objects that are mapped in by the
1152 * dynamic linker will be tracked by log events of type
1153 * PMCLOG_TYPE_MAP_IN.
1156 if (image->pi_isdynamic) {
1159 * The runtime loader gets loaded just after the maximum
1160 * possible heap address. Like so:
1162 * [ TEXT DATA BSS HEAP -->*RTLD SHLIBS <--STACK]
1164 * 0 VM_MAXUSER_ADDRESS
1167 * The exact address where the loader gets mapped in
1168 * will vary according to the size of the executable
1169 * and the limits on the size of the process'es data
1170 * segment at the time of exec(). The entry address
1171 * recorded at process exec time corresponds to the
1172 * 'start' address inside the dynamic linker. From
1173 * this we can figure out the address where the
1174 * runtime loader's file object had been mapped to.
1176 rtldimage = pmcstat_image_from_path(image->pi_dynlinkerpath, 0);
1177 if (rtldimage == NULL) {
1178 warnx("WARNING: Cannot find image for \"%s\".",
1179 pmcstat_string_unintern(image->pi_dynlinkerpath));
1180 pmcstat_stats.ps_exec_errors++;
1184 if (rtldimage->pi_type == PMCSTAT_IMAGE_UNKNOWN)
1185 pmcstat_image_get_elf_params(rtldimage);
1187 if (rtldimage->pi_type != PMCSTAT_IMAGE_ELF32 &&
1188 rtldimage->pi_type != PMCSTAT_IMAGE_ELF64) {
1189 warnx("WARNING: rtld not an ELF object \"%s\".",
1190 pmcstat_string_unintern(image->pi_dynlinkerpath));
1194 libstart = entryaddr - rtldimage->pi_entry;
1195 pmcstat_image_link(pp, rtldimage, libstart);
1200 * Find the process descriptor corresponding to a PID. If 'allocate'
1201 * is zero, we return a NULL if a pid descriptor could not be found or
1202 * a process descriptor process. If 'allocate' is non-zero, then we
1203 * will attempt to allocate a fresh process descriptor. Zombie
1204 * process descriptors are only removed if a fresh allocation for the
1205 * same PID is requested.
1208 static struct pmcstat_process *
1209 pmcstat_process_lookup(pid_t pid, int allocate)
1212 struct pmcstat_pcmap *ppm, *ppmtmp;
1213 struct pmcstat_process *pp, *pptmp;
1215 hash = (uint32_t) pid & PMCSTAT_HASH_MASK; /* simplicity wins */
1217 LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[hash], pp_next, pptmp)
1218 if (pp->pp_pid == pid) {
1219 /* Found a descriptor, check and process zombies */
1220 if (allocate && pp->pp_isactive == 0) {
1222 TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next,
1224 TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
1227 /* remove process entry */
1228 LIST_REMOVE(pp, pp_next);
1238 if ((pp = malloc(sizeof(*pp))) == NULL)
1239 err(EX_OSERR, "ERROR: Cannot allocate pid descriptor");
1242 pp->pp_isactive = 1;
1244 TAILQ_INIT(&pp->pp_map);
1246 LIST_INSERT_HEAD(&pmcstat_process_hash[hash], pp, pp_next);
1251 * Associate an image and a process.
1255 pmcstat_process_exec(struct pmcstat_process *pp,
1256 pmcstat_interned_string path, uintfptr_t entryaddr)
1258 struct pmcstat_image *image;
1260 if ((image = pmcstat_image_from_path(path, 0)) == NULL) {
1261 pmcstat_stats.ps_exec_errors++;
1265 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
1266 pmcstat_image_determine_type(image);
1268 assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN);
1270 switch (image->pi_type) {
1271 case PMCSTAT_IMAGE_ELF32:
1272 case PMCSTAT_IMAGE_ELF64:
1273 pmcstat_stats.ps_exec_elf++;
1274 pmcstat_process_elf_exec(pp, image, entryaddr);
1277 case PMCSTAT_IMAGE_AOUT:
1278 pmcstat_stats.ps_exec_aout++;
1279 pmcstat_process_aout_exec(pp, image, entryaddr);
1282 case PMCSTAT_IMAGE_INDETERMINABLE:
1283 pmcstat_stats.ps_exec_indeterminable++;
1287 err(EX_SOFTWARE, "ERROR: Unsupported executable type for "
1288 "\"%s\"", pmcstat_string_unintern(path));
1294 * Find the map entry associated with process 'p' at PC value 'pc'.
1297 struct pmcstat_pcmap *
1298 pmcstat_process_find_map(struct pmcstat_process *p, uintfptr_t pc)
1300 struct pmcstat_pcmap *ppm;
1302 TAILQ_FOREACH(ppm, &p->pp_map, ppm_next) {
1303 if (pc >= ppm->ppm_lowpc && pc < ppm->ppm_highpc)
1305 if (pc < ppm->ppm_lowpc)
1313 * Convert a hwpmc(4) log to profile information. A system-wide
1314 * callgraph is generated if FLAG_DO_CALLGRAPHS is set. gmon.out
1315 * files usable by gprof(1) are created if FLAG_DO_GPROF is set.
1318 pmcstat_analyze_log(void)
1320 uint32_t cpu, cpuflags;
1323 struct pmcstat_image *image;
1324 struct pmcstat_process *pp, *ppnew;
1325 struct pmcstat_pcmap *ppm, *ppmtmp;
1326 struct pmclog_ev ev;
1327 struct pmcstat_pmcrecord *pmcr;
1328 pmcstat_interned_string image_path;
1330 assert(args.pa_flags & FLAG_DO_ANALYSIS);
1332 if (elf_version(EV_CURRENT) == EV_NONE)
1333 err(EX_UNAVAILABLE, "Elf library intialization failed");
1335 while (pmclog_read(args.pa_logparser, &ev) == 0) {
1336 assert(ev.pl_state == PMCLOG_OK);
1338 switch (ev.pl_type) {
1339 case PMCLOG_TYPE_INITIALIZE:
1340 if ((ev.pl_u.pl_i.pl_version & 0xFF000000) !=
1341 PMC_VERSION_MAJOR << 24 && args.pa_verbosity > 0)
1342 warnx("WARNING: Log version 0x%x does not "
1343 "match compiled version 0x%x.",
1344 ev.pl_u.pl_i.pl_version,
1348 case PMCLOG_TYPE_MAP_IN:
1350 * Introduce an address range mapping for a
1351 * userland process or the kernel (pid == -1).
1353 * We always allocate a process descriptor so
1354 * that subsequent samples seen for this
1355 * address range are mapped to the current
1356 * object being mapped in.
1358 pid = ev.pl_u.pl_mi.pl_pid;
1360 pp = pmcstat_kernproc;
1362 pp = pmcstat_process_lookup(pid,
1367 image_path = pmcstat_string_intern(ev.pl_u.pl_mi.
1369 image = pmcstat_image_from_path(image_path, pid == -1);
1370 if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
1371 pmcstat_image_determine_type(image);
1372 if (image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE)
1373 pmcstat_image_link(pp, image,
1374 ev.pl_u.pl_mi.pl_start);
1377 case PMCLOG_TYPE_MAP_OUT:
1379 * Remove an address map.
1381 pid = ev.pl_u.pl_mo.pl_pid;
1383 pp = pmcstat_kernproc;
1385 pp = pmcstat_process_lookup(pid, 0);
1387 if (pp == NULL) /* unknown process */
1390 pmcstat_image_unmap(pp, ev.pl_u.pl_mo.pl_start,
1391 ev.pl_u.pl_mo.pl_end);
1394 case PMCLOG_TYPE_PCSAMPLE:
1396 * Note: the `PCSAMPLE' log entry is not
1397 * generated by hpwmc(4) after version 2.
1401 * We bring in the gmon file for the image
1402 * currently associated with the PMC & pid
1403 * pair and increment the appropriate entry
1406 pmcstat_stats.ps_samples_total++;
1407 ps_samples_period++;
1409 pc = ev.pl_u.pl_s.pl_pc;
1410 pp = pmcstat_process_lookup(ev.pl_u.pl_s.pl_pid,
1413 /* Get PMC record. */
1414 pmcr = pmcstat_lookup_pmcid(ev.pl_u.pl_s.pl_pmcid);
1415 assert(pmcr != NULL);
1419 * Call the plugins processing
1420 * TODO: move pmcstat_process_find_map inside plugins
1423 if (plugins[args.pa_pplugin].pl_process != NULL)
1424 plugins[args.pa_pplugin].pl_process(
1426 pmcstat_process_find_map(pp, pc) != NULL, 0);
1427 plugins[args.pa_plugin].pl_process(
1429 pmcstat_process_find_map(pp, pc) != NULL, 0);
1432 case PMCLOG_TYPE_CALLCHAIN:
1433 pmcstat_stats.ps_samples_total++;
1434 ps_samples_period++;
1436 cpuflags = ev.pl_u.pl_cc.pl_cpuflags;
1437 cpu = PMC_CALLCHAIN_CPUFLAGS_TO_CPU(cpuflags);
1439 /* Filter on the CPU id. */
1440 if ((args.pa_cpumask & (1 << cpu)) == 0) {
1441 pmcstat_stats.ps_samples_skipped++;
1445 pp = pmcstat_process_lookup(ev.pl_u.pl_cc.pl_pid,
1448 /* Get PMC record. */
1449 pmcr = pmcstat_lookup_pmcid(ev.pl_u.pl_cc.pl_pmcid);
1450 assert(pmcr != NULL);
1454 * Call the plugins processing
1457 if (plugins[args.pa_pplugin].pl_process != NULL)
1458 plugins[args.pa_pplugin].pl_process(
1460 ev.pl_u.pl_cc.pl_npc,
1461 ev.pl_u.pl_cc.pl_pc,
1462 PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(cpuflags),
1464 plugins[args.pa_plugin].pl_process(
1466 ev.pl_u.pl_cc.pl_npc,
1467 ev.pl_u.pl_cc.pl_pc,
1468 PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(cpuflags),
1472 case PMCLOG_TYPE_PMCALLOCATE:
1474 * Record the association pmc id between this
1477 pmcstat_pmcid_add(ev.pl_u.pl_a.pl_pmcid,
1478 pmcstat_string_intern(ev.pl_u.pl_a.pl_evname));
1481 case PMCLOG_TYPE_PROCEXEC:
1484 * Change the executable image associated with
1487 pp = pmcstat_process_lookup(ev.pl_u.pl_x.pl_pid,
1490 /* delete the current process map */
1491 TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) {
1492 TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
1496 /* associate this process image */
1497 image_path = pmcstat_string_intern(
1498 ev.pl_u.pl_x.pl_pathname);
1499 assert(image_path != NULL);
1500 pmcstat_process_exec(pp, image_path,
1501 ev.pl_u.pl_x.pl_entryaddr);
1504 case PMCLOG_TYPE_PROCEXIT:
1507 * Due to the way the log is generated, the
1508 * last few samples corresponding to a process
1509 * may appear in the log after the process
1510 * exit event is recorded. Thus we keep the
1511 * process' descriptor and associated data
1512 * structures around, but mark the process as
1515 pp = pmcstat_process_lookup(ev.pl_u.pl_e.pl_pid, 0);
1518 pp->pp_isactive = 0; /* mark as a zombie */
1521 case PMCLOG_TYPE_SYSEXIT:
1522 pp = pmcstat_process_lookup(ev.pl_u.pl_se.pl_pid, 0);
1525 pp->pp_isactive = 0; /* make a zombie */
1528 case PMCLOG_TYPE_PROCFORK:
1531 * Allocate a process descriptor for the new
1535 pmcstat_process_lookup(ev.pl_u.pl_f.pl_newpid,
1539 * If we had been tracking the parent, clone
1542 pp = pmcstat_process_lookup(ev.pl_u.pl_f.pl_oldpid, 0);
1545 TAILQ_FOREACH(ppm, &pp->pp_map, ppm_next)
1546 pmcstat_image_link(ppnew, ppm->ppm_image,
1550 default: /* other types of entries are not relevant */
1555 if (ev.pl_state == PMCLOG_EOF)
1556 return (PMCSTAT_FINISHED);
1557 else if (ev.pl_state == PMCLOG_REQUIRE_DATA)
1558 return (PMCSTAT_RUNNING);
1560 err(EX_DATAERR, "ERROR: event parsing failed (record %jd, "
1561 "offset 0x%jx)", (uintmax_t) ev.pl_count + 1, ev.pl_offset);
1565 * Print log entries as text.
1569 pmcstat_print_log(void)
1571 struct pmclog_ev ev;
1574 while (pmclog_read(args.pa_logparser, &ev) == 0) {
1575 assert(ev.pl_state == PMCLOG_OK);
1576 switch (ev.pl_type) {
1577 case PMCLOG_TYPE_CALLCHAIN:
1578 PMCSTAT_PRINT_ENTRY("callchain",
1579 "%d 0x%x %d %d %c", ev.pl_u.pl_cc.pl_pid,
1580 ev.pl_u.pl_cc.pl_pmcid,
1581 PMC_CALLCHAIN_CPUFLAGS_TO_CPU(ev.pl_u.pl_cc. \
1582 pl_cpuflags), ev.pl_u.pl_cc.pl_npc,
1583 PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(ev.pl_u.pl_cc.\
1584 pl_cpuflags) ? 'u' : 's');
1585 for (npc = 0; npc < ev.pl_u.pl_cc.pl_npc; npc++)
1586 PMCSTAT_PRINT_ENTRY("...", "%p",
1587 (void *) ev.pl_u.pl_cc.pl_pc[npc]);
1589 case PMCLOG_TYPE_CLOSELOG:
1590 PMCSTAT_PRINT_ENTRY("closelog",);
1592 case PMCLOG_TYPE_DROPNOTIFY:
1593 PMCSTAT_PRINT_ENTRY("drop",);
1595 case PMCLOG_TYPE_INITIALIZE:
1596 PMCSTAT_PRINT_ENTRY("initlog","0x%x \"%s\"",
1597 ev.pl_u.pl_i.pl_version,
1598 pmc_name_of_cputype(ev.pl_u.pl_i.pl_arch));
1599 if ((ev.pl_u.pl_i.pl_version & 0xFF000000) !=
1600 PMC_VERSION_MAJOR << 24 && args.pa_verbosity > 0)
1601 warnx("WARNING: Log version 0x%x != expected "
1602 "version 0x%x.", ev.pl_u.pl_i.pl_version,
1605 case PMCLOG_TYPE_MAP_IN:
1606 PMCSTAT_PRINT_ENTRY("map-in","%d %p \"%s\"",
1607 ev.pl_u.pl_mi.pl_pid,
1608 (void *) ev.pl_u.pl_mi.pl_start,
1609 ev.pl_u.pl_mi.pl_pathname);
1611 case PMCLOG_TYPE_MAP_OUT:
1612 PMCSTAT_PRINT_ENTRY("map-out","%d %p %p",
1613 ev.pl_u.pl_mo.pl_pid,
1614 (void *) ev.pl_u.pl_mo.pl_start,
1615 (void *) ev.pl_u.pl_mo.pl_end);
1617 case PMCLOG_TYPE_PCSAMPLE:
1618 PMCSTAT_PRINT_ENTRY("sample","0x%x %d %p %c",
1619 ev.pl_u.pl_s.pl_pmcid,
1620 ev.pl_u.pl_s.pl_pid,
1621 (void *) ev.pl_u.pl_s.pl_pc,
1622 ev.pl_u.pl_s.pl_usermode ? 'u' : 's');
1624 case PMCLOG_TYPE_PMCALLOCATE:
1625 PMCSTAT_PRINT_ENTRY("allocate","0x%x \"%s\" 0x%x",
1626 ev.pl_u.pl_a.pl_pmcid,
1627 ev.pl_u.pl_a.pl_evname,
1628 ev.pl_u.pl_a.pl_flags);
1630 case PMCLOG_TYPE_PMCATTACH:
1631 PMCSTAT_PRINT_ENTRY("attach","0x%x %d \"%s\"",
1632 ev.pl_u.pl_t.pl_pmcid,
1633 ev.pl_u.pl_t.pl_pid,
1634 ev.pl_u.pl_t.pl_pathname);
1636 case PMCLOG_TYPE_PMCDETACH:
1637 PMCSTAT_PRINT_ENTRY("detach","0x%x %d",
1638 ev.pl_u.pl_d.pl_pmcid,
1639 ev.pl_u.pl_d.pl_pid);
1641 case PMCLOG_TYPE_PROCCSW:
1642 PMCSTAT_PRINT_ENTRY("cswval","0x%x %d %jd",
1643 ev.pl_u.pl_c.pl_pmcid,
1644 ev.pl_u.pl_c.pl_pid,
1645 ev.pl_u.pl_c.pl_value);
1647 case PMCLOG_TYPE_PROCEXEC:
1648 PMCSTAT_PRINT_ENTRY("exec","0x%x %d %p \"%s\"",
1649 ev.pl_u.pl_x.pl_pmcid,
1650 ev.pl_u.pl_x.pl_pid,
1651 (void *) ev.pl_u.pl_x.pl_entryaddr,
1652 ev.pl_u.pl_x.pl_pathname);
1654 case PMCLOG_TYPE_PROCEXIT:
1655 PMCSTAT_PRINT_ENTRY("exitval","0x%x %d %jd",
1656 ev.pl_u.pl_e.pl_pmcid,
1657 ev.pl_u.pl_e.pl_pid,
1658 ev.pl_u.pl_e.pl_value);
1660 case PMCLOG_TYPE_PROCFORK:
1661 PMCSTAT_PRINT_ENTRY("fork","%d %d",
1662 ev.pl_u.pl_f.pl_oldpid,
1663 ev.pl_u.pl_f.pl_newpid);
1665 case PMCLOG_TYPE_USERDATA:
1666 PMCSTAT_PRINT_ENTRY("userdata","0x%x",
1667 ev.pl_u.pl_u.pl_userdata);
1669 case PMCLOG_TYPE_SYSEXIT:
1670 PMCSTAT_PRINT_ENTRY("exit","%d",
1671 ev.pl_u.pl_se.pl_pid);
1674 fprintf(args.pa_printfile, "unknown event (type %d).\n",
1679 if (ev.pl_state == PMCLOG_EOF)
1680 return (PMCSTAT_FINISHED);
1681 else if (ev.pl_state == PMCLOG_REQUIRE_DATA)
1682 return (PMCSTAT_RUNNING);
1684 errx(EX_DATAERR, "ERROR: event parsing failed "
1685 "(record %jd, offset 0x%jx).",
1686 (uintmax_t) ev.pl_count + 1, ev.pl_offset);
1691 * Public Interfaces.
1695 * Close a logfile, after first flushing all in-module queued data.
1699 pmcstat_close_log(void)
1701 /* If a local logfile is configured ask the kernel to stop
1702 * and flush data. Kernel will close the file when data is flushed
1703 * so keep the status to EXITING.
1705 if (args.pa_logfd != -1) {
1706 if (pmc_flush_logfile() < 0)
1707 err(EX_OSERR, "ERROR: logging failed");
1710 return (args.pa_flags & FLAG_HAS_PIPE ? PMCSTAT_EXITING :
1717 * Open a log file, for reading or writing.
1719 * The function returns the fd of a successfully opened log or -1 in
1724 pmcstat_open_log(const char *path, int mode)
1728 const char *p, *errstr;
1729 struct addrinfo hints, *res, *res0;
1730 char hostname[MAXHOSTNAMELEN];
1736 * If 'path' is "-" then open one of stdin or stdout depending
1737 * on the value of 'mode'.
1739 * If 'path' contains a ':' and does not start with a '/' or '.',
1740 * and is being opened for writing, treat it as a "host:port"
1741 * specification and open a network socket.
1743 * Otherwise, treat 'path' as a file name and open that.
1745 if (path[0] == '-' && path[1] == '\0')
1746 fd = (mode == PMCSTAT_OPEN_FOR_READ) ? 0 : 1;
1747 else if (path[0] != '/' &&
1748 path[0] != '.' && strchr(path, ':') != NULL) {
1750 p = strrchr(path, ':');
1752 if (p == path || hlen >= sizeof(hostname)) {
1753 errstr = strerror(EINVAL);
1757 assert(hlen < sizeof(hostname));
1758 (void) strncpy(hostname, path, hlen);
1759 hostname[hlen] = '\0';
1761 (void) memset(&hints, 0, sizeof(hints));
1762 hints.ai_family = AF_UNSPEC;
1763 hints.ai_socktype = SOCK_STREAM;
1764 if ((error = getaddrinfo(hostname, p+1, &hints, &res0)) != 0) {
1765 errstr = gai_strerror(error);
1770 for (res = res0; res; res = res->ai_next) {
1771 if ((fd = socket(res->ai_family, res->ai_socktype,
1772 res->ai_protocol)) < 0) {
1773 errstr = strerror(errno);
1776 if (mode == PMCSTAT_OPEN_FOR_READ) {
1777 if (bind(fd, res->ai_addr, res->ai_addrlen) < 0) {
1778 errstr = strerror(errno);
1784 cfd = accept(fd, NULL, NULL);
1787 errstr = strerror(errno);
1793 if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
1794 errstr = strerror(errno);
1805 } else if ((fd = open(path, mode == PMCSTAT_OPEN_FOR_READ ?
1806 O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC),
1807 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
1808 errstr = strerror(errno);
1812 errx(EX_OSERR, "ERROR: Cannot open \"%s\" for %s: %s.", path,
1813 (mode == PMCSTAT_OPEN_FOR_READ ? "reading" : "writing"),
1820 * Process a log file in offline analysis mode.
1824 pmcstat_process_log(void)
1828 * If analysis has not been asked for, just print the log to
1829 * the current output file.
1831 if (args.pa_flags & FLAG_DO_PRINT)
1832 return (pmcstat_print_log());
1834 return (pmcstat_analyze_log());
1838 * Refresh top display.
1842 pmcstat_refresh_top(void)
1847 struct pmcstat_pmcrecord *pmcpr;
1849 /* If in pause mode do not refresh display. */
1853 /* Wait until PMC pop in the log. */
1854 pmcpr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
1858 /* Format PMC name. */
1859 if (pmcstat_mergepmc)
1860 snprintf(pmcname, sizeof(pmcname), "[%s]",
1861 pmcstat_string_unintern(pmcpr->pr_pmcname));
1863 snprintf(pmcname, sizeof(pmcname), "%s.%d",
1864 pmcstat_string_unintern(pmcpr->pr_pmcname),
1865 pmcstat_pmcinfilter);
1867 /* Format samples count. */
1868 if (ps_samples_period > 0)
1869 v = (pmcpr->pr_samples * 100.0) / ps_samples_period;
1872 v_attrs = PMCSTAT_ATTRPERCENT(v);
1874 PMCSTAT_PRINTBEGIN();
1875 PMCSTAT_PRINTW("PMC: %s Samples: %u ",
1878 PMCSTAT_ATTRON(v_attrs);
1879 PMCSTAT_PRINTW("(%.1f%%) ", v);
1880 PMCSTAT_ATTROFF(v_attrs);
1881 PMCSTAT_PRINTW(", %u unresolved\n\n",
1882 pmcpr->pr_dubious_frames);
1883 if (plugins[args.pa_plugin].pl_topdisplay != NULL)
1884 plugins[args.pa_plugin].pl_topdisplay();
1889 * Find the next pmc index to display.
1893 pmcstat_changefilter(void)
1896 struct pmcstat_pmcrecord *pmcr;
1899 * Find the next merge target.
1901 if (pmcstat_mergepmc) {
1902 pmcin = pmcstat_pmcinfilter;
1905 pmcr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
1906 if (pmcr == NULL || pmcr == pmcr->pr_merge)
1909 pmcstat_pmcinfilter++;
1910 if (pmcstat_pmcinfilter >= pmcstat_npmcs)
1911 pmcstat_pmcinfilter = 0;
1913 } while (pmcstat_pmcinfilter != pmcin);
1918 * Top mode keypress.
1922 pmcstat_keypress_log(void)
1927 w = newwin(1, 0, 1, 0);
1929 wprintw(w, "Key: %c => ", c);
1932 wprintw(w, "enter mode 'd' or 'a' => ");
1935 args.pa_topmode = PMCSTAT_TOP_DELTA;
1936 wprintw(w, "switching to delta mode");
1938 args.pa_topmode = PMCSTAT_TOP_ACCUM;
1939 wprintw(w, "switching to accumulation mode");
1943 pmcstat_mergepmc = !pmcstat_mergepmc;
1945 * Changing merge state require data reset.
1947 if (plugins[args.pa_plugin].pl_shutdown != NULL)
1948 plugins[args.pa_plugin].pl_shutdown(NULL);
1949 pmcstat_stats_reset(0);
1950 if (plugins[args.pa_plugin].pl_init != NULL)
1951 plugins[args.pa_plugin].pl_init();
1953 /* Update filter to be on a merge target. */
1954 pmcstat_changefilter();
1955 wprintw(w, "merge PMC %s", pmcstat_mergepmc ? "on" : "off");
1958 /* Close current plugin. */
1959 if (plugins[args.pa_plugin].pl_shutdown != NULL)
1960 plugins[args.pa_plugin].pl_shutdown(NULL);
1962 /* Find next top display available. */
1965 if (plugins[args.pa_plugin].pl_name == NULL)
1967 } while (plugins[args.pa_plugin].pl_topdisplay == NULL);
1969 /* Open new plugin. */
1970 pmcstat_stats_reset(0);
1971 if (plugins[args.pa_plugin].pl_init != NULL)
1972 plugins[args.pa_plugin].pl_init();
1973 wprintw(w, "switching to plugin %s",
1974 plugins[args.pa_plugin].pl_name);
1977 pmcstat_pmcinfilter++;
1978 if (pmcstat_pmcinfilter >= pmcstat_npmcs)
1979 pmcstat_pmcinfilter = 0;
1980 pmcstat_changefilter();
1981 wprintw(w, "switching to PMC %s.%d",
1982 pmcstat_pmcindex_to_name(pmcstat_pmcinfilter),
1983 pmcstat_pmcinfilter);
1986 pmcstat_pause = !pmcstat_pause;
1988 wprintw(w, "pause => press space again to continue");
1991 wprintw(w, "exiting...");
1995 if (plugins[args.pa_plugin].pl_topkeypress != NULL)
1996 if (plugins[args.pa_plugin].pl_topkeypress(c, w))
2011 pmcstat_display_log(void)
2014 pmcstat_refresh_top();
2016 /* Reset everythings if delta mode. */
2017 if (args.pa_topmode == PMCSTAT_TOP_DELTA) {
2018 if (plugins[args.pa_plugin].pl_shutdown != NULL)
2019 plugins[args.pa_plugin].pl_shutdown(NULL);
2020 pmcstat_stats_reset(0);
2021 if (plugins[args.pa_plugin].pl_init != NULL)
2022 plugins[args.pa_plugin].pl_init();
2028 * Configure a plugins.
2032 pmcstat_pluginconfigure_log(char *opt)
2035 if (strncmp(opt, "threshold=", 10) == 0) {
2036 pmcstat_threshold = atof(opt+10);
2038 if (plugins[args.pa_plugin].pl_configure != NULL) {
2039 if (!plugins[args.pa_plugin].pl_configure(opt))
2041 "ERROR: unknown option <%s>.", opt);
2047 * Initialize module.
2051 pmcstat_initialize_logging(void)
2055 /* use a convenient format for 'ldd' output */
2056 if (setenv("LD_TRACE_LOADED_OBJECTS_FMT1","%o \"%p\" %x\n",1) != 0)
2057 err(EX_OSERR, "ERROR: Cannot setenv");
2059 /* Initialize hash tables */
2060 pmcstat_string_initialize();
2061 for (i = 0; i < PMCSTAT_NHASH; i++) {
2062 LIST_INIT(&pmcstat_image_hash[i]);
2063 LIST_INIT(&pmcstat_process_hash[i]);
2067 * Create a fake 'process' entry for the kernel with pid -1.
2068 * hwpmc(4) will subsequently inform us about where the kernel
2069 * and any loaded kernel modules are mapped.
2071 if ((pmcstat_kernproc = pmcstat_process_lookup((pid_t) -1,
2072 PMCSTAT_ALLOCATE)) == NULL)
2073 err(EX_OSERR, "ERROR: Cannot initialize logging");
2078 /* Merge PMC with same name. */
2079 pmcstat_mergepmc = args.pa_mergepmc;
2082 * Initialize plugins
2085 if (plugins[args.pa_pplugin].pl_init != NULL)
2086 plugins[args.pa_pplugin].pl_init();
2087 if (plugins[args.pa_plugin].pl_init != NULL)
2088 plugins[args.pa_plugin].pl_init();
2096 pmcstat_shutdown_logging(void)
2100 struct pmcstat_image *pi, *pitmp;
2101 struct pmcstat_process *pp, *pptmp;
2102 struct pmcstat_pcmap *ppm, *ppmtmp;
2104 /* determine where to send the map file */
2106 if (args.pa_mapfilename != NULL)
2107 mf = (strcmp(args.pa_mapfilename, "-") == 0) ?
2108 args.pa_printfile : fopen(args.pa_mapfilename, "w");
2110 if (mf == NULL && args.pa_flags & FLAG_DO_GPROF &&
2111 args.pa_verbosity >= 2)
2112 mf = args.pa_printfile;
2115 (void) fprintf(mf, "MAP:\n");
2118 * Shutdown the plugins
2121 if (plugins[args.pa_plugin].pl_shutdown != NULL)
2122 plugins[args.pa_plugin].pl_shutdown(mf);
2123 if (plugins[args.pa_pplugin].pl_shutdown != NULL)
2124 plugins[args.pa_pplugin].pl_shutdown(mf);
2126 for (i = 0; i < PMCSTAT_NHASH; i++) {
2127 LIST_FOREACH_SAFE(pi, &pmcstat_image_hash[i], pi_next,
2129 if (plugins[args.pa_plugin].pl_shutdownimage != NULL)
2130 plugins[args.pa_plugin].pl_shutdownimage(pi);
2131 if (plugins[args.pa_pplugin].pl_shutdownimage != NULL)
2132 plugins[args.pa_pplugin].pl_shutdownimage(pi);
2134 free(pi->pi_symbols);
2135 if (pi->pi_addr2line != NULL)
2136 pclose(pi->pi_addr2line);
2137 LIST_REMOVE(pi, pi_next);
2141 LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[i], pp_next,
2143 TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) {
2144 TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
2147 LIST_REMOVE(pp, pp_next);
2152 pmcstat_string_shutdown();
2155 * Print errors unless -q was specified. Print all statistics
2158 #define PRINT(N,V) do { \
2159 if (pmcstat_stats.ps_##V || args.pa_verbosity >= 2) \
2160 (void) fprintf(args.pa_printfile, " %-40s %d\n",\
2161 N, pmcstat_stats.ps_##V); \
2164 if (args.pa_verbosity >= 1 && (args.pa_flags & FLAG_DO_ANALYSIS)) {
2165 (void) fprintf(args.pa_printfile, "CONVERSION STATISTICS:\n");
2166 PRINT("#exec/a.out", exec_aout);
2167 PRINT("#exec/elf", exec_elf);
2168 PRINT("#exec/unknown", exec_indeterminable);
2169 PRINT("#exec handling errors", exec_errors);
2170 PRINT("#samples/total", samples_total);
2171 PRINT("#samples/unclaimed", samples_unknown_offset);
2172 PRINT("#samples/unknown-object", samples_indeterminable);
2173 PRINT("#samples/unknown-function", samples_unknown_function);
2174 PRINT("#callchain/dubious-frames", callchain_dubious_frames);