xref: /freebsd/sys/contrib/openzfs/include/os/linux/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  * Copyright (c) 2024-2025, Klara, Inc.
26  * Copyright (c) 2024-2025, Syneto
27  */
28 
29 #ifndef _SPL_KSTAT_H
30 #define	_SPL_KSTAT_H
31 
32 #include <linux/module.h>
33 #include <sys/types.h>
34 #include <sys/time.h>
35 #include <sys/kmem.h>
36 #include <sys/mutex.h>
37 #include <sys/proc.h>
38 
39 #define	KSTAT_STRLEN		255
40 #define	KSTAT_RAW_MAX		(128*1024)
41 
42 /*
43  * For reference valid classes are:
44  * disk, tape, net, controller, vm, kvm, hat, streams, kstat, misc
45  */
46 
47 #define	KSTAT_TYPE_RAW		0 /* can be anything; ks_ndata >= 1 */
48 #define	KSTAT_TYPE_NAMED	1 /* name/value pair; ks_ndata >= 1 */
49 #define	KSTAT_TYPE_INTR		2 /* interrupt stats; ks_ndata == 1 */
50 #define	KSTAT_TYPE_IO		3 /* I/O stats; ks_ndata == 1 */
51 #define	KSTAT_TYPE_TIMER	4 /* event timer; ks_ndata >= 1 */
52 #define	KSTAT_NUM_TYPES		5
53 
54 #define	KSTAT_DATA_CHAR		0
55 #define	KSTAT_DATA_INT32	1
56 #define	KSTAT_DATA_UINT32	2
57 #define	KSTAT_DATA_INT64	3
58 #define	KSTAT_DATA_UINT64	4
59 #define	KSTAT_DATA_LONG		5
60 #define	KSTAT_DATA_ULONG	6
61 #define	KSTAT_DATA_STRING	7
62 #define	KSTAT_NUM_DATAS		8
63 
64 #define	KSTAT_INTR_HARD		0
65 #define	KSTAT_INTR_SOFT		1
66 #define	KSTAT_INTR_WATCHDOG	2
67 #define	KSTAT_INTR_SPURIOUS	3
68 #define	KSTAT_INTR_MULTSVC	4
69 #define	KSTAT_NUM_INTRS		5
70 
71 #define	KSTAT_FLAG_VIRTUAL	0x01
72 #define	KSTAT_FLAG_VAR_SIZE	0x02
73 #define	KSTAT_FLAG_WRITABLE	0x04
74 #define	KSTAT_FLAG_PERSISTENT	0x08
75 #define	KSTAT_FLAG_DORMANT	0x10
76 #define	KSTAT_FLAG_INVALID	0x20
77 #define	KSTAT_FLAG_LONGSTRINGS	0x40
78 #define	KSTAT_FLAG_NO_HEADERS	0x80
79 
80 #define	KS_MAGIC		0x9d9d9d9d
81 
82 /* Dynamic updates */
83 #define	KSTAT_READ		0
84 #define	KSTAT_WRITE		1
85 
86 struct kstat_s;
87 typedef struct kstat_s kstat_t;
88 
89 typedef int kid_t;				/* unique kstat id */
90 typedef int kstat_update_t(struct kstat_s *, int); /* dynamic update cb */
91 
92 typedef struct kstat_module {
93 	char ksm_name[KSTAT_STRLEN];		/* module name */
94 	struct list_head ksm_module_list;	/* module linkage */
95 	struct list_head ksm_kstat_list;	/* list of kstat entries */
96 	struct proc_dir_entry *ksm_proc;	/* proc entry */
97 	struct kstat_module *ksm_parent;	/* parent module in hierarchy */
98 	uint_t ksm_nchildren;			/* number of child modules */
99 } kstat_module_t;
100 
101 typedef struct kstat_raw_ops {
102 	int (*headers)(char *buf, size_t size);
103 	int (*data)(char *buf, size_t size, void *data);
104 	void *(*addr)(kstat_t *ksp, loff_t index);
105 } kstat_raw_ops_t;
106 
107 typedef struct kstat_proc_entry {
108 	char	kpe_name[KSTAT_STRLEN];		/* kstat name */
109 	char	kpe_module[KSTAT_STRLEN];	/* provider module name */
110 	kstat_module_t		*kpe_owner;	/* kstat module linkage */
111 	struct list_head	kpe_list;	/* kstat linkage */
112 	struct proc_dir_entry	*kpe_proc;	/* procfs entry */
113 } kstat_proc_entry_t;
114 
115 struct kstat_s {
116 	int		ks_magic;		/* magic value */
117 	kid_t		ks_kid;			/* unique kstat ID */
118 	hrtime_t	ks_crtime;		/* creation time */
119 	hrtime_t	ks_snaptime;		/* last access time */
120 	int		ks_instance;		/* provider module instance */
121 	char		ks_class[KSTAT_STRLEN]; /* kstat class */
122 	uchar_t		ks_type;		/* kstat data type */
123 	uchar_t		ks_flags;		/* kstat flags */
124 	void		*ks_data;		/* kstat type-specific data */
125 	uint_t		ks_ndata;		/* # of data records */
126 	size_t		ks_data_size;		/* size of kstat data section */
127 	kstat_update_t	*ks_update;		/* dynamic updates */
128 	void		*ks_private;		/* private data */
129 	kmutex_t	ks_private_lock;	/* kstat private data lock */
130 	kmutex_t	*ks_lock;		/* kstat data lock */
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 	kstat_proc_entry_t	ks_proc;	/* data for procfs entry */
135 };
136 
137 typedef struct kstat_named_s {
138 	char	name[KSTAT_STRLEN];	/* name of counter */
139 	uchar_t	data_type;		/* data type */
140 	union {
141 		char c[16];	/* 128-bit int */
142 		int32_t	i32;	/* 32-bit signed int */
143 		uint32_t ui32;	/* 32-bit unsigned int */
144 		int64_t i64;	/* 64-bit signed int */
145 		uint64_t ui64;	/* 64-bit unsigned int */
146 		long l;		/* native signed long */
147 		ulong_t ul;	/* native unsigned long */
148 		struct {
149 			union {
150 				char *ptr;	/* NULL-term string */
151 				char __pad[8];	/* 64-bit padding */
152 			} addr;
153 			uint32_t len;		/* # bytes for strlen + '\0' */
154 		} string;
155 	} value;
156 } kstat_named_t;
157 
158 #define	KSTAT_NAMED_STR_PTR(knptr) ((knptr)->value.string.addr.ptr)
159 #define	KSTAT_NAMED_STR_BUFLEN(knptr) ((knptr)->value.string.len)
160 
161 #ifdef HAVE_PROC_OPS_STRUCT
162 typedef struct proc_ops kstat_proc_op_t;
163 #else
164 typedef struct file_operations kstat_proc_op_t;
165 #endif
166 
167 typedef struct kstat_intr {
168 	uint_t intrs[KSTAT_NUM_INTRS];
169 } kstat_intr_t;
170 
171 typedef struct kstat_io {
172 	u_longlong_t	nread;		/* number of bytes read */
173 	u_longlong_t	nwritten;	/* number of bytes written */
174 	uint_t		reads;		/* number of read operations */
175 	uint_t		writes;		/* number of write operations */
176 	hrtime_t	wtime;		/* cumulative wait (pre-service) time */
177 	hrtime_t	wlentime;	/* cumulative wait len*time product */
178 	hrtime_t	wlastupdate;	/* last time wait queue changed */
179 	hrtime_t	rtime;		/* cumulative run (service) time */
180 	hrtime_t	rlentime;	/* cumulative run length*time product */
181 	hrtime_t	rlastupdate;	/* last time run queue changed */
182 	uint_t		wcnt;		/* count of elements in wait state */
183 	uint_t		rcnt;		/* count of elements in run state */
184 } kstat_io_t;
185 
186 typedef struct kstat_timer {
187 	char		name[KSTAT_STRLEN]; /* event name */
188 	u_longlong_t	num_events;	 /* number of events */
189 	hrtime_t	elapsed_time;	 /* cumulative elapsed time */
190 	hrtime_t	min_time;	 /* shortest event duration */
191 	hrtime_t	max_time;	 /* longest event duration */
192 	hrtime_t	start_time;	 /* previous event start time */
193 	hrtime_t	stop_time;	 /* previous event stop time */
194 } kstat_timer_t;
195 
196 int spl_kstat_init(void);
197 void spl_kstat_fini(void);
198 
199 extern void __kstat_set_raw_ops(kstat_t *ksp,
200     int (*headers)(char *buf, size_t size),
201     int (*data)(char *buf, size_t size, void *data),
202     void* (*addr)(kstat_t *ksp, loff_t index));
203 
204 extern kstat_t *__kstat_create(const char *ks_module, int ks_instance,
205     const char *ks_name, const char *ks_class, uchar_t ks_type,
206     uint_t ks_ndata, uchar_t ks_flags);
207 
208 extern void kstat_proc_entry_init(kstat_proc_entry_t *kpep,
209     const char *module, const char *name);
210 extern void kstat_proc_entry_delete(kstat_proc_entry_t *kpep);
211 extern void kstat_proc_entry_install(kstat_proc_entry_t *kpep, mode_t mode,
212     const kstat_proc_op_t *file_ops, void *data);
213 
214 extern void __kstat_install(kstat_t *ksp);
215 extern void __kstat_delete(kstat_t *ksp);
216 
217 #define	kstat_set_raw_ops(k, h, d, a) \
218     __kstat_set_raw_ops(k, h, d, a)
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 
225 #endif  /* _SPL_KSTAT_H */
226