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