xref: /titanic_51/usr/src/cmd/latencytop/common/latencytop.h (revision 62b885670cb2373723bdad8e9fc26c05f8dabfb6)
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 /*
32  * GLib header file, including TRUE and FALSE definitions
33  */
34 #include <glib.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 /*
41  * Lint seems to be confused by glib header file.
42  */
43 #ifdef __lint
44 #undef g_assert
45 #define	g_assert(x)	((void)(x))
46 #undef TRUE
47 #define	TRUE		1
48 #endif
49 
50 /*
51  * To avoid compiler warning, we define our own convertion.
52  */
53 #define	LT_POINTER_TO_INT(a)	((int)(long)(a))
54 #define	LT_INT_TO_POINTER(a)	((void *)(unsigned long)(a))
55 
56 #define	TITLE			"LatencyTOP for OpenSolaris, version 1.0"
57 #define	COPYRIGHT		"Copyright (c) 2008-2009, Intel Corporation."
58 #define	DEFAULT_KLOG_FILE	"/var/log/latencytop.log"
59 
60 #define	INVALID_PID		(~0)
61 #define	INVALID_TID		(~0)
62 #define	PID_SYS_GLOBAL		INVALID_PID
63 #define	INVALID_CAUSE		0
64 #define	HIGHER_PRIORITY(a, b)	((a) > (b))
65 
66 #ifdef EMBED_CONFIGS
67 /*
68  * LatencyTOP configurations embedded in the binary.
69  * Array will be generated by /usr/bin/xxd.
70  */
71 extern unsigned char latencytop_d[];
72 extern unsigned int latencytop_d_len;
73 extern unsigned char latencytop_trans[];
74 extern unsigned int latencytop_trans_len;
75 #else
76 /*
77  * LatencyTOP configurations is externally. This is easy for debugging.
78  */
79 #define	DEFAULT_CONFIG_NAME	"./latencytop.trans"
80 #define	DEFAULT_D_SCRIPT_NAME   "./latencytop.d"
81 #endif
82 
83 typedef enum {
84 	LT_STAT_COUNT,
85 	LT_STAT_MAX,
86 	LT_STAT_SUM,
87 } lt_stat_type_t;
88 
89 #define	LT_KLOG_LEVEL_NONE	0	/* Log nothing */
90 #define	LT_KLOG_LEVEL_UNMAPPED	1	/* Log only stacks not mapped */
91 #define	LT_KLOG_LEVEL_MAPPED	2	/* Log only stacks mapped */
92 #define	LT_KLOG_LEVEL_ALL	3	/* Log all stacks, mapped or not */
93 
94 typedef enum {
95 	LT_LEVEL_GLOBAL,	/* System wide statistics */
96 	LT_LEVEL_PROCESS,	/* Per-process statistics */
97 	LT_LEVEL_THREAD,	/* Per-thread statistics */
98 } lt_stat_level_t;
99 
100 typedef enum {
101 	LT_SORT_TOTAL,
102 	LT_SORT_MAX,
103 	LT_SORT_AVG,
104 	LT_SORT_COUNT,
105 } lt_sort_t;
106 
107 typedef enum {
108 	LT_FIELD_FNAME,
109 	LT_FIELD_PSARGS,
110 } lt_field_t;
111 
112 typedef enum {
113 	LT_LIST_CAUSE,		/* Lists latency by the causes (default) */
114 	LT_LIST_SPECIALS,	/* Lists only "special" causes. */
115 	LT_LIST_SOBJ		/* Lists synchronization objects. */
116 } lt_list_type_t;
117 
118 /*
119  * Data entry which contains the statistics.
120  */
121 typedef struct	{
122 	uint64_t count;
123 	uint64_t total;
124 	uint64_t max;
125 } lt_stat_data_t;
126 
127 /*
128  * Data entry stored along with its name.
129  */
130 typedef struct {
131 	enum	{
132 		STAT_CAUSE,
133 		STAT_SOBJ
134 	} type;
135 	const char *string;
136 	lt_stat_data_t data;
137 	union	{
138 		struct	{
139 			int id;
140 			int flags;
141 		} cause;
142 		struct	{
143 			int id;
144 		} sobj;
145 	} type_data;
146 } lt_stat_entry_t;
147 
148 typedef struct {
149 	int enable_filter;
150 	int trace_sched;
151 	int trace_syncobj;
152 	int low_overhead_mode;
153 	int snap_interval;
154 	char *config_name;
155 } lt_config_t;
156 
157 extern lt_config_t g_config;	/* The global settings */
158 
159 /*
160  * Causes can be disabled in the configuration file.
161  * Once disabled, DTrace script will still capture such causes,
162  * but they will not be counted in LatencyTOP.
163  */
164 #define	CAUSE_FLAG_DISABLED		1
165 /*
166  * Causes with this flag will not show and count as part of summary in
167  * "kstack window".
168  */
169 #define	CAUSE_FLAG_HIDE_IN_SUMMARY	2
170 /*
171  * This is generated from D script (named cause), which is "special".
172  */
173 #define	CAUSE_FLAG_SPECIAL		4
174 #define	CAUSE_ALL_FLAGS			0xffffffff
175 
176 /*
177  * These functions collect data using DTrace.
178  */
179 extern int lt_dtrace_init(void);
180 extern int lt_dtrace_work(int);
181 extern int lt_dtrace_collect(void);
182 extern void lt_dtrace_deinit(void);
183 
184 /*
185  * These functions maintain configuration, e.g. symbol to cause mapping.
186  */
187 extern int lt_table_init(void);
188 extern int lt_table_lookup_cause(const char *, int *, int *);
189 extern const char *lt_table_get_cause_name(int);
190 extern int lt_table_get_cause_flag(int, int);
191 extern int lt_table_lookup_named_cause(char *, int);
192 extern void lt_table_deinit(void);
193 
194 /*
195  * These functions update tatistic data of all causes collected from DTrace.
196  */
197 extern void lt_stat_update(pid_t, id_t, char *, lt_stat_type_t, uint64_t);
198 extern void lt_stat_update_cause(pid_t, id_t, int, lt_stat_type_t, uint64_t);
199 extern void lt_stat_update_sobj(pid_t, id_t, int, unsigned long long,
200     lt_stat_type_t, uint64_t);
201 extern void lt_stat_clear_all(void);
202 extern void lt_stat_free_all(void);
203 
204 /*
205  * These functions produce lists for display part.
206  * Note: after a call to lt_stat_update_*, the old lists will be invalid.
207  */
208 extern void *lt_stat_list_create(lt_list_type_t, lt_stat_level_t,
209     pid_t, id_t, int, lt_sort_t);
210 extern int lt_stat_list_has_item(void *, int);
211 extern const char *lt_stat_list_get_reason(void *, int);
212 extern uint64_t lt_stat_list_get_max(void *, int);
213 extern uint64_t lt_stat_list_get_sum(void *, int);
214 extern uint64_t lt_stat_list_get_count(void *, int);
215 extern uint64_t lt_stat_list_get_gtotal(void *);
216 extern void lt_stat_list_free(void *);
217 
218 /*
219  * These functions produce process and thread list.
220  */
221 extern int lt_stat_proc_list_create(pid_t **, id_t **);
222 extern void lt_stat_proc_list_free(pid_t *, id_t *);
223 extern const char *lt_stat_proc_get_name(pid_t);
224 extern int lt_stat_proc_get_nthreads(pid_t);
225 
226 /*
227  * Console based display functions using ncurses.
228  */
229 extern void lt_display_init(void);
230 extern int lt_display_loop(int);
231 extern void lt_display_error(const char *, ...);
232 extern void lt_display_deinit(void);
233 
234 /*
235  * Write statistics to a log file.
236  * Useful for debug and offline analysis.
237  */
238 extern void lt_klog_init(void);
239 extern void lt_klog_deinit(void);
240 extern int lt_klog_set_log_file(const char *);
241 extern int lt_klog_set_log_level(int);
242 extern void lt_klog_write(void);
243 extern void lt_klog_log(int, pid_t, char *, lt_stat_type_t,
244     uint64_t);
245 
246 /*
247  * Utility functions.
248  */
249 extern uint64_t lt_millisecond(void);
250 extern const char *lt_text(const char *);
251 extern void *lt_malloc(size_t);
252 extern void *lt_zalloc(size_t);
253 extern char *lt_strdup(const char *);
254 extern void lt_check_null(void *);
255 extern void lt_time_str(char *, int);
256 extern char *lt_get_proc_field(pid_t, lt_field_t);
257 extern void lt_update_stat_value(lt_stat_data_t *, lt_stat_type_t, uint64_t);
258 extern int lt_sort_by_total_desc(lt_stat_entry_t *, lt_stat_entry_t *);
259 extern int lt_sort_by_max_desc(lt_stat_entry_t *, lt_stat_entry_t *);
260 extern int lt_sort_by_count_desc(lt_stat_entry_t *, lt_stat_entry_t *);
261 extern int lt_sort_by_avg_desc(lt_stat_entry_t *, lt_stat_entry_t *);
262 extern void lt_gpipe_init(void);
263 extern void lt_gpipe_deinit(void);
264 extern void lt_gpipe_break(const char *);
265 extern int lt_gpipe_readfd(void);
266 extern int lt_file_exist(const char *);
267 
268 #ifdef	__cplusplus
269 }
270 #endif
271 
272 #endif	/* _LATENCYTOP_H */
273