xref: /titanic_41/usr/src/cmd/latencytop/common/latencytop.h (revision d92fc07239af943d62e4e67791879707ec89e7d3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2008-2009, Intel Corporation.
23  * All Rights Reserved.
24  */
25 
26 #ifndef _LATENCYTOP_H
27 #define	_LATENCYTOP_H
28 
29 #include <sys/types.h>
30 
31 #include <glib.h>
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 /*
38  * Without this lint seems to be confused by glib header file.
39  */
40 #ifdef __lint
41 #undef g_assert
42 #define	g_assert(x)	((void)(x))
43 #undef TRUE
44 #define	TRUE		1
45 #endif
46 
47 /*
48  * We define our own conversions in order to avoid compiler warnings.
49  */
50 #define	LT_INT_TO_POINTER(a)	((void *)(unsigned long)(a))
51 
52 #define	TITLE			"LatencyTOP for OpenSolaris, version 1.0"
53 #define	COPYRIGHT		"Copyright (c) 2008-2009, Intel Corporation."
54 #define	DEFAULT_KLOG_FILE	"/var/log/latencytop.log"
55 
56 #define	INVALID_PID		(~0)
57 #define	INVALID_TID		(~0)
58 #define	PID_SYS_GLOBAL		INVALID_PID
59 #define	INVALID_CAUSE		0
60 #define	HIGHER_PRIORITY(a, b)	((a) > (b))
61 
62 #ifdef EMBED_CONFIGS
63 /*
64  * LatencyTOP configuration is embedded in the binary.
65  * Array will be generated by elfwrap.
66  */
67 extern char latencytop_d_start;
68 extern char latencytop_d_end;
69 extern char latencytop_trans_start;
70 extern char latencytop_trans_end;
71 #else
72 /*
73  * LatencyTOP configuration is provided externally by user.
74  */
75 #define	DEFAULT_CONFIG_NAME	"./latencytop.trans"
76 #define	DEFAULT_D_SCRIPT_NAME   "./latencytop.d"
77 #endif
78 
79 typedef enum {
80 	LT_STAT_COUNT,
81 	LT_STAT_MAX,
82 	LT_STAT_SUM,
83 } lt_stat_type_t;
84 
85 #define	LT_KLOG_LEVEL_NONE	0	/* Log nothing */
86 #define	LT_KLOG_LEVEL_UNMAPPED	1	/* Log only stacks not mapped */
87 #define	LT_KLOG_LEVEL_MAPPED	2	/* Log only stacks mapped */
88 #define	LT_KLOG_LEVEL_ALL	3	/* Log all stacks, mapped or not */
89 
90 typedef enum {
91 	LT_LEVEL_GLOBAL,	/* System wide statistics */
92 	LT_LEVEL_PROCESS,	/* Per-process statistics */
93 	LT_LEVEL_THREAD,	/* Per-thread statistics */
94 } lt_stat_level_t;
95 
96 typedef enum {
97 	LT_SORT_TOTAL,
98 	LT_SORT_MAX,
99 	LT_SORT_AVG,
100 	LT_SORT_COUNT,
101 } lt_sort_t;
102 
103 typedef enum {
104 	LT_FIELD_FNAME,
105 	LT_FIELD_PSARGS,
106 } lt_field_t;
107 
108 typedef enum {
109 	LT_LIST_CAUSE,		/* List latency by causes (default) */
110 	LT_LIST_SPECIALS,	/* List only "special" causes */
111 	LT_LIST_SOBJ		/* List synchronization objects */
112 } lt_list_type_t;
113 
114 /*
115  * Data structure which contains statistics.
116  */
117 typedef struct {
118 	uint64_t lt_s_count;
119 	uint64_t lt_s_total;
120 	uint64_t lt_s_max;
121 } lt_stat_data_t;
122 
123 /*
124  * Data structure that stores statistics along with the name.
125  */
126 typedef struct {
127 	enum {
128 		STAT_CAUSE,
129 		STAT_SOBJ
130 	} lt_se_type;
131 	const char *lt_se_string;
132 	lt_stat_data_t lt_se_data;
133 	union {
134 		struct {
135 			int lt_se_c_id;
136 			int lt_se_c_flags;
137 		} lt_se_t_cause;
138 		struct {
139 			int lt_se_s_id;
140 		} lt_se_t_sobj;
141 	} lt_se_tsdata;	/* type specific data */
142 } lt_stat_entry_t;
143 
144 typedef struct {
145 	int lt_cfg_enable_filter;
146 	int lt_cfg_trace_sched;
147 	int lt_cfg_trace_syncobj;
148 	int lt_cfg_low_overhead_mode;
149 	int lt_cfg_snap_interval;
150 	char *lt_cfg_config_name;
151 } lt_config_t;
152 
153 extern lt_config_t g_config;	/* The global settings */
154 
155 /*
156  * Causes can be disabled through the configuration file.
157  * When disabled, though D script will continue to capture causes, they will
158  * not be counted by LatencyTOP.
159  */
160 #define	CAUSE_FLAG_DISABLED		1
161 /*
162  * This flag will not show and count causes as part of summary in
163  * "kstack window".
164  */
165 #define	CAUSE_FLAG_HIDE_IN_SUMMARY	2
166 /*
167  * This is generated from D script (named cause), and is "special".
168  */
169 #define	CAUSE_FLAG_SPECIAL		4
170 #define	CAUSE_ALL_FLAGS			0xffffffff
171 
172 /*
173  * These functions collect statistics using DTrace.
174  */
175 extern int lt_dtrace_init(void);
176 extern int lt_dtrace_work(int);
177 extern int lt_dtrace_collect(void);
178 extern void lt_dtrace_deinit(void);
179 
180 /*
181  * These functions maintain configuration, e.g. symbol to cause mapping.
182  */
183 extern int lt_table_init(void);
184 extern int lt_table_cause_from_stack(const char *, int *, int *);
185 extern const char *lt_table_get_cause_name(int);
186 extern int lt_table_get_cause_flag(int, int);
187 extern int lt_table_cause_from_name(char *, int, int);
188 extern int lt_table_append_trans(FILE *fp);
189 extern void lt_table_deinit(void);
190 
191 /*
192  * These functions update statistic of all causes of latency, collected
193  * from DTrace.
194  */
195 extern void lt_stat_update(pid_t, id_t, char *, char *, unsigned int,
196     lt_stat_type_t, uint64_t);
197 extern void lt_stat_update_cause(pid_t, id_t, int, lt_stat_type_t, uint64_t);
198 extern void lt_stat_update_sobj(pid_t, id_t, int, unsigned long long,
199     lt_stat_type_t, uint64_t);
200 extern void lt_stat_clear_all(void);
201 extern void lt_stat_free_all(void);
202 
203 /*
204  * These functions produce lists for display panes.
205  * Note: after a call to lt_stat_update_*, the old lists will become invalid.
206  */
207 extern void *lt_stat_list_create(lt_list_type_t, lt_stat_level_t,
208     pid_t, id_t, int, lt_sort_t);
209 extern int lt_stat_list_has_item(void *, int);
210 extern const char *lt_stat_list_get_reason(void *, int);
211 extern uint64_t lt_stat_list_get_max(void *, int);
212 extern uint64_t lt_stat_list_get_sum(void *, int);
213 extern uint64_t lt_stat_list_get_count(void *, int);
214 extern uint64_t lt_stat_list_get_gtotal(void *);
215 extern void lt_stat_list_free(void *);
216 
217 /*
218  * These functions produce the process list and the thread list.
219  */
220 extern int lt_stat_proc_list_create(pid_t **, id_t **);
221 extern void lt_stat_proc_list_free(pid_t *, id_t *);
222 extern const char *lt_stat_proc_get_name(pid_t);
223 extern int lt_stat_proc_get_nthreads(pid_t);
224 
225 /*
226  * These functions use ncurses to create console-based display.
227  */
228 extern void lt_display_init(void);
229 extern int lt_display_loop(int);
230 extern void lt_display_error(const char *, ...);
231 extern void lt_display_deinit(void);
232 
233 /*
234  * Write statistics to log file - useful for debugging and offline analysis.
235  */
236 extern void lt_klog_init(void);
237 extern void lt_klog_deinit(void);
238 extern int lt_klog_set_log_file(const char *);
239 extern int lt_klog_set_log_level(int);
240 extern void lt_klog_write(void);
241 extern void lt_klog_log(int, pid_t, char *, lt_stat_type_t,
242     uint64_t);
243 
244 /*
245  * Utility functions.
246  */
247 extern uint64_t lt_millisecond(void);
248 extern void *lt_malloc(size_t);
249 extern void *lt_zalloc(size_t);
250 extern char *lt_strdup(const char *);
251 extern void lt_check_null(void *);
252 extern void lt_time_str(char *, int);
253 extern char *lt_get_proc_field(pid_t, lt_field_t);
254 extern void lt_update_stat_value(lt_stat_data_t *, lt_stat_type_t, uint64_t);
255 extern int lt_sort_by_total_desc(lt_stat_entry_t *, lt_stat_entry_t *);
256 extern int lt_sort_by_max_desc(lt_stat_entry_t *, lt_stat_entry_t *);
257 extern int lt_sort_by_count_desc(lt_stat_entry_t *, lt_stat_entry_t *);
258 extern int lt_sort_by_avg_desc(lt_stat_entry_t *, lt_stat_entry_t *);
259 extern void lt_gpipe_init(void);
260 extern void lt_gpipe_deinit(void);
261 extern void lt_gpipe_break(const char *);
262 extern int lt_gpipe_readfd(void);
263 extern int lt_file_exist(const char *);
264 
265 #ifdef	__cplusplus
266 }
267 #endif
268 
269 #endif	/* _LATENCYTOP_H */
270