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 */ 57147708Sjkoshy 58147708Sjkoshy#define DEFAULT_SAMPLE_COUNT 65536 59147708Sjkoshy#define DEFAULT_WAIT_INTERVAL 5.0 60203790Sfabient#define DEFAULT_DISPLAY_HEIGHT 256 /* file virtual height */ 61203790Sfabient#define DEFAULT_DISPLAY_WIDTH 1024 /* file virtual width */ 62147708Sjkoshy#define DEFAULT_BUFFER_SIZE 4096 63174396Sjkoshy#define DEFAULT_CALLGRAPH_DEPTH 4 64147708Sjkoshy 65147708Sjkoshy#define PRINT_HEADER_PREFIX "# " 66147708Sjkoshy#define READPIPEFD 0 67147708Sjkoshy#define WRITEPIPEFD 1 68147708Sjkoshy#define NPIPEFD 2 69147708Sjkoshy 70168949Sjkoshy#define NSOCKPAIRFD 2 71168949Sjkoshy#define PARENTSOCKET 0 72168949Sjkoshy#define CHILDSOCKET 1 73168949Sjkoshy 74147708Sjkoshy#define PMCSTAT_OPEN_FOR_READ 0 75147708Sjkoshy#define PMCSTAT_OPEN_FOR_WRITE 1 76147708Sjkoshy#define PMCSTAT_DEFAULT_NW_HOST "localhost" 77147708Sjkoshy#define PMCSTAT_DEFAULT_NW_PORT "9000" 78147708Sjkoshy#define PMCSTAT_NHASH 256 79147708Sjkoshy#define PMCSTAT_HASH_MASK 0xFF 80147708Sjkoshy 81147708Sjkoshy#define PMCSTAT_LDD_COMMAND "/usr/bin/ldd" 82147708Sjkoshy 83203790Sfabient#define PMCSTAT_PRINT_ENTRY(T,...) do { \ 84203790Sfabient (void) fprintf(args.pa_printfile, "%-9s", T); \ 85203790Sfabient (void) fprintf(args.pa_printfile, " " __VA_ARGS__); \ 86203790Sfabient (void) fprintf(args.pa_printfile, "\n"); \ 87147708Sjkoshy } while (0) 88147708Sjkoshy 89203790Sfabient#define PMCSTAT_PL_NONE 0 90203790Sfabient#define PMCSTAT_PL_CALLGRAPH 1 91203790Sfabient#define PMCSTAT_PL_GPROF 2 92203790Sfabient#define PMCSTAT_PL_ANNOTATE 3 93203790Sfabient#define PMCSTAT_PL_CALLTREE 4 94203790Sfabient 95203790Sfabient#define PMCSTAT_TOP_DELTA 0 96203790Sfabient#define PMCSTAT_TOP_ACCUM 1 97203790Sfabient 98203790Sfabient#define min(A,B) ((A) < (B) ? (A) : (B)) 99203790Sfabient#define max(A,B) ((A) > (B) ? (A) : (B)) 100203790Sfabient 101147708Sjkoshyenum pmcstat_state { 102147708Sjkoshy PMCSTAT_FINISHED = 0, 103147708Sjkoshy PMCSTAT_EXITING = 1, 104147708Sjkoshy PMCSTAT_RUNNING = 2 105147708Sjkoshy}; 106147708Sjkoshy 107147708Sjkoshystruct pmcstat_ev { 108147708Sjkoshy STAILQ_ENTRY(pmcstat_ev) ev_next; 109147708Sjkoshy int ev_count; /* associated count if in sampling mode */ 110168949Sjkoshy uint32_t ev_cpu; /* cpus for this event */ 111168949Sjkoshy int ev_cumulative; /* show cumulative counts */ 112147708Sjkoshy int ev_flags; /* PMC_F_* */ 113168949Sjkoshy int ev_fieldskip; /* #leading spaces */ 114147708Sjkoshy int ev_fieldwidth; /* print width */ 115168949Sjkoshy enum pmc_mode ev_mode; /* desired mode */ 116168949Sjkoshy char *ev_name; /* (derived) event name */ 117147708Sjkoshy pmc_id_t ev_pmcid; /* allocated ID */ 118168949Sjkoshy pmc_value_t ev_saved; /* for incremental counts */ 119168949Sjkoshy char *ev_spec; /* event specification */ 120147708Sjkoshy}; 121147708Sjkoshy 122169069Sjkoshystruct pmcstat_target { 123169069Sjkoshy SLIST_ENTRY(pmcstat_target) pt_next; 124169069Sjkoshy pid_t pt_pid; 125169069Sjkoshy}; 126169069Sjkoshy 127147708Sjkoshystruct pmcstat_args { 128147708Sjkoshy int pa_flags; /* argument flags */ 129147708Sjkoshy int pa_required; /* required features */ 130203790Sfabient int pa_pplugin; /* pre-processing plugin */ 131203790Sfabient int pa_plugin; /* analysis plugin */ 132157144Sjkoshy int pa_verbosity; /* verbosity level */ 133147708Sjkoshy FILE *pa_printfile; /* where to send printed output */ 134147708Sjkoshy int pa_logfd; /* output log file */ 135147708Sjkoshy char *pa_inputpath; /* path to input log */ 136147708Sjkoshy char *pa_outputpath; /* path to output log */ 137147708Sjkoshy void *pa_logparser; /* log file parser */ 138157144Sjkoshy const char *pa_fsroot; /* FS root where executables reside */ 139157144Sjkoshy char *pa_kernel; /* pathname of the kernel */ 140147708Sjkoshy const char *pa_samplesdir; /* directory for profile files */ 141157144Sjkoshy const char *pa_mapfilename;/* mapfile name */ 142174396Sjkoshy FILE *pa_graphfile; /* where to send the callgraph */ 143174396Sjkoshy int pa_graphdepth; /* print depth for callgraphs */ 144147708Sjkoshy double pa_interval; /* printing interval in seconds */ 145224698Sattilio cpuset_t pa_cpumask; /* filter for CPUs analysed */ 146203790Sfabient int pa_ctdumpinstr; /* dump instructions with calltree */ 147203790Sfabient int pa_topmode; /* delta or accumulative */ 148203790Sfabient int pa_toptty; /* output to tty or file */ 149203790Sfabient int pa_topcolor; /* terminal support color */ 150203790Sfabient int pa_mergepmc; /* merge PMC with same name */ 151147708Sjkoshy int pa_argc; 152147708Sjkoshy char **pa_argv; 153169069Sjkoshy STAILQ_HEAD(, pmcstat_ev) pa_events; 154169069Sjkoshy SLIST_HEAD(, pmcstat_target) pa_targets; 155203790Sfabient}; 156147708Sjkoshy 157203790Sfabientextern int pmcstat_displayheight; /* current terminal height */ 158203790Sfabientextern int pmcstat_displaywidth; /* current terminal width */ 159203790Sfabientextern struct pmcstat_args args; /* command line args */ 160203790Sfabient 161147708Sjkoshy/* Function prototypes */ 162203790Sfabientvoid pmcstat_attach_pmcs(void); 163203790Sfabientvoid pmcstat_cleanup(void); 164203790Sfabientint pmcstat_close_log(void); 165203790Sfabientvoid pmcstat_create_process(void); 166203790Sfabientvoid pmcstat_find_targets(const char *_arg); 167203790Sfabientvoid pmcstat_initialize_logging(void); 168203790Sfabientvoid pmcstat_kill_process(void); 169157144Sjkoshyint pmcstat_open_log(const char *_p, int _mode); 170203790Sfabientvoid pmcstat_print_counters(void); 171203790Sfabientvoid pmcstat_print_headers(void); 172203790Sfabientvoid pmcstat_print_pmcs(void); 173147708Sjkoshyvoid pmcstat_show_usage(void); 174203790Sfabientvoid pmcstat_shutdown_logging(void); 175203790Sfabientvoid pmcstat_start_pmcs(void); 176168949Sjkoshyvoid pmcstat_start_process(void); 177203790Sfabientint pmcstat_process_log(void); 178203790Sfabientint pmcstat_keypress_log(void); 179203790Sfabientvoid pmcstat_display_log(void); 180203790Sfabientvoid pmcstat_pluginconfigure_log(char *_opt); 181203790Sfabientvoid pmcstat_topexit(void); 182147708Sjkoshy 183147708Sjkoshy#endif /* _PMCSTAT_H_ */ 184