1/*-
2 * Copyright (c) 2005-2007, Joseph Koshy
3 * Copyright (c) 2007 The FreeBSD Foundation
4 * All rights reserved.
5 *
6 * Portions of this software were developed by A. Joseph Koshy under
7 * sponsorship from the FreeBSD Foundation and Google, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * $FreeBSD$
31 */
32
33#ifndef	_PMCSTAT_H_
34#define	_PMCSTAT_H_
35
36#include <sys/_cpuset.h>
37
38#define	FLAG_HAS_TARGET			0x00000001	/* process target */
39#define	FLAG_HAS_WAIT_INTERVAL		0x00000002	/* -w secs */
40#define	FLAG_HAS_OUTPUT_LOGFILE		0x00000004	/* -O file or pipe */
41#define	FLAG_HAS_COMMANDLINE		0x00000008	/* command */
42#define	FLAG_HAS_SAMPLING_PMCS		0x00000010	/* -S or -P */
43#define	FLAG_HAS_COUNTING_PMCS		0x00000020	/* -s or -p */
44#define	FLAG_HAS_PROCESS_PMCS		0x00000040	/* -P or -p */
45#define	FLAG_HAS_SYSTEM_PMCS		0x00000080	/* -S or -s */
46#define	FLAG_HAS_PIPE			0x00000100	/* implicit log */
47#define	FLAG_READ_LOGFILE		0x00000200	/* -R file */
48#define	FLAG_DO_GPROF			0x00000400	/* -g */
49#define	FLAG_HAS_SAMPLESDIR		0x00000800	/* -D dir */
50#define	FLAG_HAS_KERNELPATH		0x00001000	/* -k kernel */
51#define	FLAG_DO_PRINT			0x00002000	/* -o */
52#define	FLAG_DO_CALLGRAPHS		0x00004000	/* -G or -F */
53#define	FLAG_DO_ANNOTATE		0x00008000	/* -m */
54#define	FLAG_DO_TOP			0x00010000	/* -T */
55#define	FLAG_DO_ANALYSIS		0x00020000	/* -g or -G or -m or -T */
56#define	FLAGS_HAS_CPUMASK		0x00040000	/* -c */
57#define	FLAG_HAS_DURATION		0x00080000	/* -l secs */
58
59#define	DEFAULT_SAMPLE_COUNT		65536
60#define	DEFAULT_WAIT_INTERVAL		5.0
61#define	DEFAULT_DISPLAY_HEIGHT		256		/* file virtual height */
62#define	DEFAULT_DISPLAY_WIDTH		1024		/* file virtual width */
63#define	DEFAULT_BUFFER_SIZE		4096
64#define	DEFAULT_CALLGRAPH_DEPTH		16
65
66#define	PRINT_HEADER_PREFIX		"# "
67#define	READPIPEFD			0
68#define	WRITEPIPEFD			1
69#define	NPIPEFD				2
70
71#define	NSOCKPAIRFD			2
72#define	PARENTSOCKET			0
73#define	CHILDSOCKET			1
74
75#define	PMCSTAT_OPEN_FOR_READ		0
76#define	PMCSTAT_OPEN_FOR_WRITE		1
77#define	PMCSTAT_DEFAULT_NW_HOST		"localhost"
78#define	PMCSTAT_DEFAULT_NW_PORT		"9000"
79#define	PMCSTAT_NHASH			256
80#define	PMCSTAT_HASH_MASK		0xFF
81
82#define	PMCSTAT_LDD_COMMAND		"/usr/bin/ldd"
83
84#define	PMCSTAT_PRINT_ENTRY(T,...) do {					\
85		(void) fprintf(args.pa_printfile, "%-9s", T);		\
86		(void) fprintf(args.pa_printfile, " "  __VA_ARGS__);	\
87		(void) fprintf(args.pa_printfile, "\n");		\
88	} while (0)
89
90#define PMCSTAT_PL_NONE		0
91#define PMCSTAT_PL_CALLGRAPH	1
92#define PMCSTAT_PL_GPROF	2
93#define PMCSTAT_PL_ANNOTATE	3
94#define PMCSTAT_PL_CALLTREE	4
95#define PMCSTAT_PL_ANNOTATE_CG	5
96
97#define PMCSTAT_TOP_DELTA 	0
98#define PMCSTAT_TOP_ACCUM	1
99
100#define	min(A,B)		((A) < (B) ? (A) : (B))
101#define	max(A,B)		((A) > (B) ? (A) : (B))
102
103enum pmcstat_state {
104	PMCSTAT_FINISHED = 0,
105	PMCSTAT_EXITING  = 1,
106	PMCSTAT_RUNNING  = 2
107};
108
109struct pmcstat_ev {
110	STAILQ_ENTRY(pmcstat_ev) ev_next;
111	int		ev_count; /* associated count if in sampling mode */
112	uint32_t	ev_cpu;	  /* cpus for this event */
113	int		ev_cumulative;  /* show cumulative counts */
114	int		ev_flags; /* PMC_F_* */
115	int		ev_fieldskip;   /* #leading spaces */
116	int		ev_fieldwidth;  /* print width */
117	enum pmc_mode	ev_mode;  /* desired mode */
118	char	       *ev_name;  /* (derived) event name */
119	pmc_id_t	ev_pmcid; /* allocated ID */
120	pmc_value_t	ev_saved; /* for incremental counts */
121	char	       *ev_spec;  /* event specification */
122};
123
124struct pmcstat_target {
125	SLIST_ENTRY(pmcstat_target) pt_next;
126	pid_t		pt_pid;
127};
128
129struct pmcstat_args {
130	int	pa_flags;		/* argument flags */
131	int	pa_required;		/* required features */
132	int	pa_pplugin;		/* pre-processing plugin */
133	int	pa_plugin;		/* analysis plugin */
134	int	pa_verbosity;		/* verbosity level */
135	FILE	*pa_printfile;		/* where to send printed output */
136	int	pa_logfd;		/* output log file */
137	char	*pa_inputpath;		/* path to input log */
138	char	*pa_outputpath;		/* path to output log */
139	void	*pa_logparser;		/* log file parser */
140	const char	*pa_fsroot;	/* FS root where executables reside */
141	char	*pa_kernel;		/* pathname of the kernel */
142	const char	*pa_samplesdir;	/* directory for profile files */
143	const char	*pa_mapfilename;/* mapfile name */
144	FILE	*pa_graphfile;		/* where to send the callgraph */
145	int	pa_graphdepth;		/* print depth for callgraphs */
146	double	pa_interval;		/* printing interval in seconds */
147	cpuset_t	pa_cpumask;	/* filter for CPUs analysed */
148	int	pa_ctdumpinstr;		/* dump instructions with calltree */
149	int	pa_topmode;		/* delta or accumulative */
150	int	pa_toptty;		/* output to tty or file */
151	int	pa_topcolor;		/* terminal support color */
152	int	pa_mergepmc;		/* merge PMC with same name */
153	double	pa_duration;		/* time duration */
154	int	pa_argc;
155	char	**pa_argv;
156	STAILQ_HEAD(, pmcstat_ev) pa_events;
157	SLIST_HEAD(, pmcstat_target) pa_targets;
158};
159
160extern int pmcstat_displayheight;	/* current terminal height */
161extern int pmcstat_displaywidth;	/* current terminal width */
162extern struct pmcstat_args args;	/* command line args */
163
164/* Function prototypes */
165void	pmcstat_attach_pmcs(void);
166void	pmcstat_cleanup(void);
167int	pmcstat_close_log(void);
168void	pmcstat_create_process(void);
169void	pmcstat_find_targets(const char *_arg);
170void	pmcstat_initialize_logging(void);
171void	pmcstat_kill_process(void);
172int	pmcstat_open_log(const char *_p, int _mode);
173void	pmcstat_print_counters(void);
174void	pmcstat_print_headers(void);
175void	pmcstat_print_pmcs(void);
176void	pmcstat_show_usage(void);
177void	pmcstat_shutdown_logging(void);
178void	pmcstat_start_pmcs(void);
179void	pmcstat_start_process(void);
180int	pmcstat_process_log(void);
181int	pmcstat_keypress_log(void);
182void	pmcstat_display_log(void);
183void	pmcstat_pluginconfigure_log(char *_opt);
184void    pmcstat_topexit(void);
185
186#endif	/* _PMCSTAT_H_ */
187