1147708Sjkoshy/*- 2169069Sjkoshy * Copyright (c) 2005-2007, Joseph Koshy 3174396Sjkoshy * Copyright (c) 2007 The FreeBSD Foundation 4147708Sjkoshy * All rights reserved. 5147708Sjkoshy * 6174396Sjkoshy * Portions of this software were developed by A. Joseph Koshy under 7174396Sjkoshy * sponsorship from the FreeBSD Foundation and Google, Inc. 8174396Sjkoshy * 9147708Sjkoshy * Redistribution and use in source and binary forms, with or without 10147708Sjkoshy * modification, are permitted provided that the following conditions 11147708Sjkoshy * are met: 12147708Sjkoshy * 1. Redistributions of source code must retain the above copyright 13147708Sjkoshy * notice, this list of conditions and the following disclaimer. 14147708Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright 15147708Sjkoshy * notice, this list of conditions and the following disclaimer in the 16147708Sjkoshy * documentation and/or other materials provided with the distribution. 17147708Sjkoshy * 18147708Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19147708Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20147708Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21147708Sjkoshy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22147708Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23147708Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24147708Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25147708Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26147708Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27147708Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28147708Sjkoshy * SUCH DAMAGE. 29147708Sjkoshy * 30147708Sjkoshy * $FreeBSD$ 31147708Sjkoshy */ 32147708Sjkoshy 33147708Sjkoshy#ifndef _PMCSTAT_H_ 34147708Sjkoshy#define _PMCSTAT_H_ 35147708Sjkoshy 36224698Sattilio#include <sys/_cpuset.h> 37224698Sattilio 38169069Sjkoshy#define FLAG_HAS_TARGET 0x00000001 /* process target */ 39147708Sjkoshy#define FLAG_HAS_WAIT_INTERVAL 0x00000002 /* -w secs */ 40147708Sjkoshy#define FLAG_HAS_OUTPUT_LOGFILE 0x00000004 /* -O file or pipe */ 41147708Sjkoshy#define FLAG_HAS_COMMANDLINE 0x00000008 /* command */ 42147708Sjkoshy#define FLAG_HAS_SAMPLING_PMCS 0x00000010 /* -S or -P */ 43147708Sjkoshy#define FLAG_HAS_COUNTING_PMCS 0x00000020 /* -s or -p */ 44147708Sjkoshy#define FLAG_HAS_PROCESS_PMCS 0x00000040 /* -P or -p */ 45147708Sjkoshy#define FLAG_HAS_SYSTEM_PMCS 0x00000080 /* -S or -s */ 46147708Sjkoshy#define FLAG_HAS_PIPE 0x00000100 /* implicit log */ 47147708Sjkoshy#define FLAG_READ_LOGFILE 0x00000200 /* -R file */ 48147708Sjkoshy#define FLAG_DO_GPROF 0x00000400 /* -g */ 49147708Sjkoshy#define FLAG_HAS_SAMPLESDIR 0x00000800 /* -D dir */ 50147708Sjkoshy#define FLAG_HAS_KERNELPATH 0x00001000 /* -k kernel */ 51147708Sjkoshy#define FLAG_DO_PRINT 0x00002000 /* -o */ 52203790Sfabient#define FLAG_DO_CALLGRAPHS 0x00004000 /* -G or -F */ 53203790Sfabient#define FLAG_DO_ANNOTATE 0x00008000 /* -m */ 54203790Sfabient#define FLAG_DO_TOP 0x00010000 /* -T */ 55203790Sfabient#define FLAG_DO_ANALYSIS 0x00020000 /* -g or -G or -m or -T */ 56210797Sfabient#define FLAGS_HAS_CPUMASK 0x00040000 /* -c */ 57266890Sgnn#define FLAG_HAS_DURATION 0x00080000 /* -l secs */ 58294046Sjtl#define FLAG_DO_WIDE_GPROF_HC 0x00100000 /* -e */ 59147708Sjkoshy 60147708Sjkoshy#define DEFAULT_SAMPLE_COUNT 65536 61147708Sjkoshy#define DEFAULT_WAIT_INTERVAL 5.0 62203790Sfabient#define DEFAULT_DISPLAY_HEIGHT 256 /* file virtual height */ 63203790Sfabient#define DEFAULT_DISPLAY_WIDTH 1024 /* file virtual width */ 64147708Sjkoshy#define DEFAULT_BUFFER_SIZE 4096 65267143Semaste#define DEFAULT_CALLGRAPH_DEPTH 16 66147708Sjkoshy 67147708Sjkoshy#define PRINT_HEADER_PREFIX "# " 68147708Sjkoshy#define READPIPEFD 0 69147708Sjkoshy#define WRITEPIPEFD 1 70147708Sjkoshy#define NPIPEFD 2 71147708Sjkoshy 72168949Sjkoshy#define NSOCKPAIRFD 2 73168949Sjkoshy#define PARENTSOCKET 0 74168949Sjkoshy#define CHILDSOCKET 1 75168949Sjkoshy 76147708Sjkoshy#define PMCSTAT_OPEN_FOR_READ 0 77147708Sjkoshy#define PMCSTAT_OPEN_FOR_WRITE 1 78147708Sjkoshy#define PMCSTAT_DEFAULT_NW_HOST "localhost" 79147708Sjkoshy#define PMCSTAT_DEFAULT_NW_PORT "9000" 80147708Sjkoshy#define PMCSTAT_NHASH 256 81147708Sjkoshy#define PMCSTAT_HASH_MASK 0xFF 82147708Sjkoshy 83147708Sjkoshy#define PMCSTAT_LDD_COMMAND "/usr/bin/ldd" 84147708Sjkoshy 85203790Sfabient#define PMCSTAT_PRINT_ENTRY(T,...) do { \ 86203790Sfabient (void) fprintf(args.pa_printfile, "%-9s", T); \ 87203790Sfabient (void) fprintf(args.pa_printfile, " " __VA_ARGS__); \ 88203790Sfabient (void) fprintf(args.pa_printfile, "\n"); \ 89147708Sjkoshy } while (0) 90147708Sjkoshy 91203790Sfabient#define PMCSTAT_PL_NONE 0 92203790Sfabient#define PMCSTAT_PL_CALLGRAPH 1 93203790Sfabient#define PMCSTAT_PL_GPROF 2 94203790Sfabient#define PMCSTAT_PL_ANNOTATE 3 95203790Sfabient#define PMCSTAT_PL_CALLTREE 4 96265604Sscottl#define PMCSTAT_PL_ANNOTATE_CG 5 97203790Sfabient 98203790Sfabient#define PMCSTAT_TOP_DELTA 0 99203790Sfabient#define PMCSTAT_TOP_ACCUM 1 100203790Sfabient 101203790Sfabient#define min(A,B) ((A) < (B) ? (A) : (B)) 102203790Sfabient#define max(A,B) ((A) > (B) ? (A) : (B)) 103203790Sfabient 104147708Sjkoshyenum pmcstat_state { 105147708Sjkoshy PMCSTAT_FINISHED = 0, 106147708Sjkoshy PMCSTAT_EXITING = 1, 107147708Sjkoshy PMCSTAT_RUNNING = 2 108147708Sjkoshy}; 109147708Sjkoshy 110147708Sjkoshystruct pmcstat_ev { 111147708Sjkoshy STAILQ_ENTRY(pmcstat_ev) ev_next; 112147708Sjkoshy int ev_count; /* associated count if in sampling mode */ 113168949Sjkoshy uint32_t ev_cpu; /* cpus for this event */ 114168949Sjkoshy int ev_cumulative; /* show cumulative counts */ 115147708Sjkoshy int ev_flags; /* PMC_F_* */ 116168949Sjkoshy int ev_fieldskip; /* #leading spaces */ 117147708Sjkoshy int ev_fieldwidth; /* print width */ 118168949Sjkoshy enum pmc_mode ev_mode; /* desired mode */ 119168949Sjkoshy char *ev_name; /* (derived) event name */ 120147708Sjkoshy pmc_id_t ev_pmcid; /* allocated ID */ 121168949Sjkoshy pmc_value_t ev_saved; /* for incremental counts */ 122168949Sjkoshy char *ev_spec; /* event specification */ 123147708Sjkoshy}; 124147708Sjkoshy 125169069Sjkoshystruct pmcstat_target { 126169069Sjkoshy SLIST_ENTRY(pmcstat_target) pt_next; 127169069Sjkoshy pid_t pt_pid; 128169069Sjkoshy}; 129169069Sjkoshy 130147708Sjkoshystruct pmcstat_args { 131147708Sjkoshy int pa_flags; /* argument flags */ 132147708Sjkoshy int pa_required; /* required features */ 133203790Sfabient int pa_pplugin; /* pre-processing plugin */ 134203790Sfabient int pa_plugin; /* analysis plugin */ 135157144Sjkoshy int pa_verbosity; /* verbosity level */ 136147708Sjkoshy FILE *pa_printfile; /* where to send printed output */ 137147708Sjkoshy int pa_logfd; /* output log file */ 138147708Sjkoshy char *pa_inputpath; /* path to input log */ 139147708Sjkoshy char *pa_outputpath; /* path to output log */ 140147708Sjkoshy void *pa_logparser; /* log file parser */ 141157144Sjkoshy const char *pa_fsroot; /* FS root where executables reside */ 142157144Sjkoshy char *pa_kernel; /* pathname of the kernel */ 143147708Sjkoshy const char *pa_samplesdir; /* directory for profile files */ 144157144Sjkoshy const char *pa_mapfilename;/* mapfile name */ 145174396Sjkoshy FILE *pa_graphfile; /* where to send the callgraph */ 146174396Sjkoshy int pa_graphdepth; /* print depth for callgraphs */ 147147708Sjkoshy double pa_interval; /* printing interval in seconds */ 148224698Sattilio cpuset_t pa_cpumask; /* filter for CPUs analysed */ 149203790Sfabient int pa_ctdumpinstr; /* dump instructions with calltree */ 150203790Sfabient int pa_topmode; /* delta or accumulative */ 151203790Sfabient int pa_toptty; /* output to tty or file */ 152203790Sfabient int pa_topcolor; /* terminal support color */ 153203790Sfabient int pa_mergepmc; /* merge PMC with same name */ 154266890Sgnn double pa_duration; /* time duration */ 155147708Sjkoshy int pa_argc; 156147708Sjkoshy char **pa_argv; 157169069Sjkoshy STAILQ_HEAD(, pmcstat_ev) pa_events; 158169069Sjkoshy SLIST_HEAD(, pmcstat_target) pa_targets; 159203790Sfabient}; 160147708Sjkoshy 161203790Sfabientextern int pmcstat_displayheight; /* current terminal height */ 162203790Sfabientextern int pmcstat_displaywidth; /* current terminal width */ 163203790Sfabientextern struct pmcstat_args args; /* command line args */ 164203790Sfabient 165147708Sjkoshy/* Function prototypes */ 166203790Sfabientvoid pmcstat_attach_pmcs(void); 167203790Sfabientvoid pmcstat_cleanup(void); 168203790Sfabientint pmcstat_close_log(void); 169203790Sfabientvoid pmcstat_create_process(void); 170203790Sfabientvoid pmcstat_find_targets(const char *_arg); 171203790Sfabientvoid pmcstat_initialize_logging(void); 172203790Sfabientvoid pmcstat_kill_process(void); 173157144Sjkoshyint pmcstat_open_log(const char *_p, int _mode); 174203790Sfabientvoid pmcstat_print_counters(void); 175203790Sfabientvoid pmcstat_print_headers(void); 176203790Sfabientvoid pmcstat_print_pmcs(void); 177147708Sjkoshyvoid pmcstat_show_usage(void); 178203790Sfabientvoid pmcstat_shutdown_logging(void); 179203790Sfabientvoid pmcstat_start_pmcs(void); 180168949Sjkoshyvoid pmcstat_start_process(void); 181203790Sfabientint pmcstat_process_log(void); 182203790Sfabientint pmcstat_keypress_log(void); 183203790Sfabientvoid pmcstat_display_log(void); 184203790Sfabientvoid pmcstat_pluginconfigure_log(char *_opt); 185203790Sfabientvoid pmcstat_topexit(void); 186147708Sjkoshy 187147708Sjkoshy#endif /* _PMCSTAT_H_ */ 188