1 /*
2  *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3  *  Copyright (C) 2007 The Regents of the University of California.
4  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5  *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6  *  UCRL-CODE-235197
7  *
8  *  This file is part of the SPL, Solaris Porting Layer.
9  *
10  *  The SPL is free software; you can redistribute it and/or modify it
11  *  under the terms of the GNU General Public License as published by the
12  *  Free Software Foundation; either version 2 of the License, or (at your
13  *  option) any later version.
14  *
15  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
16  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18  *  for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef _SPL_KSTAT_H
25 #define	_SPL_KSTAT_H
26 
27 #include <sys/types.h>
28 #ifndef _STANDALONE
29 #include <sys/sysctl.h>
30 #endif
31 struct list_head {};
32 #include <sys/mutex.h>
33 #include <sys/proc.h>
34 
35 #define	KSTAT_STRLEN		255
36 #define	KSTAT_RAW_MAX		(128*1024)
37 
38 /*
39  * For reference valid classes are:
40  * disk, tape, net, controller, vm, kvm, hat, streams, kstat, misc
41  */
42 
43 #define	KSTAT_TYPE_RAW		0 /* can be anything; ks_ndata >= 1 */
44 #define	KSTAT_TYPE_NAMED	1 /* name/value pair; ks_ndata >= 1 */
45 #define	KSTAT_TYPE_INTR		2 /* interrupt stats; ks_ndata == 1 */
46 #define	KSTAT_TYPE_IO		3 /* I/O stats; ks_ndata == 1 */
47 #define	KSTAT_TYPE_TIMER	4 /* event timer; ks_ndata >= 1 */
48 #define	KSTAT_NUM_TYPES		5
49 
50 #define	KSTAT_DATA_CHAR		0
51 #define	KSTAT_DATA_INT32	1
52 #define	KSTAT_DATA_UINT32	2
53 #define	KSTAT_DATA_INT64	3
54 #define	KSTAT_DATA_UINT64	4
55 #define	KSTAT_DATA_LONG		5
56 #define	KSTAT_DATA_ULONG	6
57 #define	KSTAT_DATA_STRING	7
58 #define	KSTAT_NUM_DATAS		8
59 
60 #define	KSTAT_INTR_HARD		0
61 #define	KSTAT_INTR_SOFT		1
62 #define	KSTAT_INTR_WATCHDOG	2
63 #define	KSTAT_INTR_SPURIOUS	3
64 #define	KSTAT_INTR_MULTSVC	4
65 #define	KSTAT_NUM_INTRS		5
66 
67 #define	KSTAT_FLAG_VIRTUAL	0x01
68 #define	KSTAT_FLAG_VAR_SIZE	0x02
69 #define	KSTAT_FLAG_WRITABLE	0x04
70 #define	KSTAT_FLAG_PERSISTENT	0x08
71 #define	KSTAT_FLAG_DORMANT	0x10
72 #define	KSTAT_FLAG_INVALID	0x20
73 #define	KSTAT_FLAG_LONGSTRINGS	0x40
74 #define	KSTAT_FLAG_NO_HEADERS	0x80
75 
76 #define	KS_MAGIC		0x9d9d9d9d
77 
78 /* Dynamic updates */
79 #define	KSTAT_READ		0
80 #define	KSTAT_WRITE		1
81 
82 struct kstat_s;
83 typedef struct kstat_s kstat_t;
84 
85 typedef int kid_t;				/* unique kstat id */
86 typedef int kstat_update_t(struct kstat_s *, int); /* dynamic update cb */
87 
88 struct seq_file {
89 	char *sf_buf;
90 	size_t sf_size;
91 };
92 
93 void seq_printf(struct seq_file *m, const char *fmt, ...);
94 
95 
96 typedef struct kstat_module {
97 	char ksm_name[KSTAT_STRLEN];		/* module name */
98 	struct list_head ksm_module_list;	/* module linkage */
99 	struct list_head ksm_kstat_list;	/* list of kstat entries */
100 	struct proc_dir_entry *ksm_proc;	/* proc entry */
101 } kstat_module_t;
102 
103 typedef struct kstat_raw_ops {
104 	int (*headers)(char *buf, size_t size);
105 	int (*seq_headers)(struct seq_file *);
106 	int (*data)(char *buf, size_t size, void *data);
107 	void *(*addr)(kstat_t *ksp, loff_t index);
108 } kstat_raw_ops_t;
109 
110 struct kstat_s {
111 	int		ks_magic;		/* magic value */
112 	kid_t		ks_kid;			/* unique kstat ID */
113 	hrtime_t	ks_crtime;		/* creation time */
114 	hrtime_t	ks_snaptime;		/* last access time */
115 	char		ks_module[KSTAT_STRLEN]; /* provider module name */
116 	int		ks_instance;		/* provider module instance */
117 	char		ks_name[KSTAT_STRLEN]; /* kstat name */
118 	char		ks_class[KSTAT_STRLEN]; /* kstat class */
119 	uchar_t		ks_type;		/* kstat data type */
120 	uchar_t		ks_flags;		/* kstat flags */
121 	void		*ks_data;		/* kstat type-specific data */
122 	uint_t		ks_ndata;		/* # of data records */
123 	size_t		ks_data_size;		/* size of kstat data section */
124 	kstat_update_t	*ks_update;		/* dynamic updates */
125 	void		*ks_private;		/* private data */
126 	void		*ks_private1;		/* private data */
127 	kmutex_t	ks_private_lock;	/* kstat private data lock */
128 	kmutex_t	*ks_lock;		/* kstat data lock */
129 	struct list_head ks_list;		/* kstat linkage */
130 	kstat_module_t	*ks_owner;		/* kstat module linkage */
131 	kstat_raw_ops_t	ks_raw_ops;		/* ops table for raw type */
132 	char		*ks_raw_buf;		/* buf used for raw ops */
133 	size_t		ks_raw_bufsize;		/* size of raw ops buffer */
134 #ifndef _STANDALONE
135 	struct sysctl_ctx_list ks_sysctl_ctx;
136 	struct sysctl_oid *ks_sysctl_root;
137 #endif /* _STANDALONE */
138 };
139 
140 typedef struct kstat_named_s {
141 	char	name[KSTAT_STRLEN];	/* name of counter */
142 	uchar_t	data_type;		/* data type */
143 	union {
144 		char c[16];	/* 128-bit int */
145 		int32_t	i32;	/* 32-bit signed int */
146 		uint32_t ui32;	/* 32-bit unsigned int */
147 		int64_t i64;	/* 64-bit signed int */
148 		uint64_t ui64;	/* 64-bit unsigned int */
149 		long l;		/* native signed long */
150 		ulong_t ul;	/* native unsigned long */
151 		struct {
152 			union {
153 				char *ptr;	/* NULL-term string */
154 				char __pad[8];	/* 64-bit padding */
155 			} addr;
156 			uint32_t len;		/* # bytes for strlen + '\0' */
157 		} string;
158 	} value;
159 } kstat_named_t;
160 
161 #define	KSTAT_NAMED_STR_PTR(knptr) ((knptr)->value.string.addr.ptr)
162 #define	KSTAT_NAMED_STR_BUFLEN(knptr) ((knptr)->value.string.len)
163 
164 typedef struct kstat_intr {
165 	uint_t intrs[KSTAT_NUM_INTRS];
166 } kstat_intr_t;
167 
168 typedef struct kstat_io {
169 	u_longlong_t	nread;		/* number of bytes read */
170 	u_longlong_t	nwritten;	/* number of bytes written */
171 	uint_t		reads;		/* number of read operations */
172 	uint_t		writes;		/* number of write operations */
173 	hrtime_t	wtime;		/* cumulative wait (pre-service) time */
174 	hrtime_t	wlentime;	/* cumulative wait len*time product */
175 	hrtime_t	wlastupdate;	/* last time wait queue changed */
176 	hrtime_t	rtime;		/* cumulative run (service) time */
177 	hrtime_t	rlentime;	/* cumulative run length*time product */
178 	hrtime_t	rlastupdate;	/* last time run queue changed */
179 	uint_t		wcnt;		/* count of elements in wait state */
180 	uint_t		rcnt;		/* count of elements in run state */
181 } kstat_io_t;
182 
183 typedef struct kstat_timer {
184 	char		name[KSTAT_STRLEN]; /* event name */
185 	u_longlong_t	num_events;	 /* number of events */
186 	hrtime_t	elapsed_time;	 /* cumulative elapsed time */
187 	hrtime_t	min_time;	 /* shortest event duration */
188 	hrtime_t	max_time;	 /* longest event duration */
189 	hrtime_t	start_time;	 /* previous event start time */
190 	hrtime_t	stop_time;	 /* previous event stop time */
191 } kstat_timer_t;
192 
193 int spl_kstat_init(void);
194 void spl_kstat_fini(void);
195 
196 extern void __kstat_set_raw_ops(kstat_t *ksp,
197     int (*headers)(char *buf, size_t size),
198     int (*data)(char *buf, size_t size, void *data),
199     void* (*addr)(kstat_t *ksp, loff_t index));
200 
201 extern void __kstat_set_seq_raw_ops(kstat_t *ksp,
202     int (*headers)(struct seq_file *),
203     int (*data)(char *buf, size_t size, void *data),
204     void* (*addr)(kstat_t *ksp, loff_t index));
205 
206 
207 extern kstat_t *__kstat_create(const char *ks_module, int ks_instance,
208     const char *ks_name, const char *ks_class, uchar_t ks_type,
209     uint_t ks_ndata, uchar_t ks_flags);
210 
211 extern void __kstat_install(kstat_t *ksp);
212 extern void __kstat_delete(kstat_t *ksp);
213 
214 #define	kstat_set_seq_raw_ops(k, h, d, a) \
215     __kstat_set_seq_raw_ops(k, h, d, a)
216 #define	kstat_set_raw_ops(k, h, d, a) \
217     __kstat_set_raw_ops(k, h, d, a)
218 #ifndef _STANDALONE
219 #define	kstat_create(m, i, n, c, t, s, f) \
220     __kstat_create(m, i, n, c, t, s, f)
221 
222 #define	kstat_install(k)		__kstat_install(k)
223 #define	kstat_delete(k)			__kstat_delete(k)
224 #else
225 #define	kstat_create(m, i, n, c, t, s, f)	((kstat_t *)0)
226 #define	kstat_install(k)
227 #define	kstat_delete(k)
228 #endif
229 
230 #endif  /* _SPL_KSTAT_H */
231