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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
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.
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
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/>.
27 #include <sys/types.h>
28 #include <sys/sysctl.h>
30 #include <sys/mutex.h>
33 #define KSTAT_STRLEN 255
34 #define KSTAT_RAW_MAX (128*1024)
37 * For reference valid classes are:
38 * disk, tape, net, controller, vm, kvm, hat, streams, kstat, misc
41 #define KSTAT_TYPE_RAW 0 /* can be anything; ks_ndata >= 1 */
42 #define KSTAT_TYPE_NAMED 1 /* name/value pair; ks_ndata >= 1 */
43 #define KSTAT_TYPE_INTR 2 /* interrupt stats; ks_ndata == 1 */
44 #define KSTAT_TYPE_IO 3 /* I/O stats; ks_ndata == 1 */
45 #define KSTAT_TYPE_TIMER 4 /* event timer; ks_ndata >= 1 */
46 #define KSTAT_NUM_TYPES 5
48 #define KSTAT_DATA_CHAR 0
49 #define KSTAT_DATA_INT32 1
50 #define KSTAT_DATA_UINT32 2
51 #define KSTAT_DATA_INT64 3
52 #define KSTAT_DATA_UINT64 4
53 #define KSTAT_DATA_LONG 5
54 #define KSTAT_DATA_ULONG 6
55 #define KSTAT_DATA_STRING 7
56 #define KSTAT_NUM_DATAS 8
58 #define KSTAT_INTR_HARD 0
59 #define KSTAT_INTR_SOFT 1
60 #define KSTAT_INTR_WATCHDOG 2
61 #define KSTAT_INTR_SPURIOUS 3
62 #define KSTAT_INTR_MULTSVC 4
63 #define KSTAT_NUM_INTRS 5
65 #define KSTAT_FLAG_VIRTUAL 0x01
66 #define KSTAT_FLAG_VAR_SIZE 0x02
67 #define KSTAT_FLAG_WRITABLE 0x04
68 #define KSTAT_FLAG_PERSISTENT 0x08
69 #define KSTAT_FLAG_DORMANT 0x10
70 #define KSTAT_FLAG_INVALID 0x20
71 #define KSTAT_FLAG_LONGSTRINGS 0x40
72 #define KSTAT_FLAG_NO_HEADERS 0x80
74 #define KS_MAGIC 0x9d9d9d9d
81 typedef struct kstat_s kstat_t;
83 typedef int kid_t; /* unique kstat id */
84 typedef int kstat_update_t(struct kstat_s *, int); /* dynamic update cb */
86 typedef struct kstat_module {
87 char ksm_name[KSTAT_STRLEN+1]; /* module name */
88 struct list_head ksm_module_list; /* module linkage */
89 struct list_head ksm_kstat_list; /* list of kstat entries */
90 struct proc_dir_entry *ksm_proc; /* proc entry */
93 typedef struct kstat_raw_ops {
94 int (*headers)(char *buf, size_t size);
95 int (*data)(char *buf, size_t size, void *data);
96 void *(*addr)(kstat_t *ksp, loff_t index);
100 int ks_magic; /* magic value */
101 kid_t ks_kid; /* unique kstat ID */
102 hrtime_t ks_crtime; /* creation time */
103 hrtime_t ks_snaptime; /* last access time */
104 char ks_module[KSTAT_STRLEN+1]; /* provider module name */
105 int ks_instance; /* provider module instance */
106 char ks_name[KSTAT_STRLEN+1]; /* kstat name */
107 char ks_class[KSTAT_STRLEN+1]; /* kstat class */
108 uchar_t ks_type; /* kstat data type */
109 uchar_t ks_flags; /* kstat flags */
110 void *ks_data; /* kstat type-specific data */
111 uint_t ks_ndata; /* # of data records */
112 size_t ks_data_size; /* size of kstat data section */
113 kstat_update_t *ks_update; /* dynamic updates */
114 void *ks_private; /* private data */
115 kmutex_t ks_private_lock; /* kstat private data lock */
116 kmutex_t *ks_lock; /* kstat data lock */
117 struct list_head ks_list; /* kstat linkage */
118 kstat_module_t *ks_owner; /* kstat module linkage */
119 kstat_raw_ops_t ks_raw_ops; /* ops table for raw type */
120 char *ks_raw_buf; /* buf used for raw ops */
121 size_t ks_raw_bufsize; /* size of raw ops buffer */
122 struct sysctl_ctx_list ks_sysctl_ctx;
123 struct sysctl_oid *ks_sysctl_root;
127 typedef struct kstat_named_s {
128 char name[KSTAT_STRLEN]; /* name of counter */
129 uchar_t data_type; /* data type */
131 char c[16]; /* 128-bit int */
132 int32_t i32; /* 32-bit signed int */
133 uint32_t ui32; /* 32-bit unsigned int */
134 int64_t i64; /* 64-bit signed int */
135 uint64_t ui64; /* 64-bit unsigned int */
136 long l; /* native signed long */
137 ulong_t ul; /* native unsigned long */
140 char *ptr; /* NULL-term string */
141 char __pad[8]; /* 64-bit padding */
143 uint32_t len; /* # bytes for strlen + '\0' */
148 #define KSTAT_NAMED_STR_PTR(knptr) ((knptr)->value.string.addr.ptr)
149 #define KSTAT_NAMED_STR_BUFLEN(knptr) ((knptr)->value.string.len)
151 typedef struct kstat_intr {
152 uint_t intrs[KSTAT_NUM_INTRS];
155 typedef struct kstat_io {
156 u_longlong_t nread; /* number of bytes read */
157 u_longlong_t nwritten; /* number of bytes written */
158 uint_t reads; /* number of read operations */
159 uint_t writes; /* number of write operations */
160 hrtime_t wtime; /* cumulative wait (pre-service) time */
161 hrtime_t wlentime; /* cumulative wait len*time product */
162 hrtime_t wlastupdate; /* last time wait queue changed */
163 hrtime_t rtime; /* cumulative run (service) time */
164 hrtime_t rlentime; /* cumulative run length*time product */
165 hrtime_t rlastupdate; /* last time run queue changed */
166 uint_t wcnt; /* count of elements in wait state */
167 uint_t rcnt; /* count of elements in run state */
170 typedef struct kstat_timer {
171 char name[KSTAT_STRLEN+1]; /* event name */
172 u_longlong_t num_events; /* number of events */
173 hrtime_t elapsed_time; /* cumulative elapsed time */
174 hrtime_t min_time; /* shortest event duration */
175 hrtime_t max_time; /* longest event duration */
176 hrtime_t start_time; /* previous event start time */
177 hrtime_t stop_time; /* previous event stop time */
180 int spl_kstat_init(void);
181 void spl_kstat_fini(void);
183 extern void __kstat_set_raw_ops(kstat_t *ksp,
184 int (*headers)(char *buf, size_t size),
185 int (*data)(char *buf, size_t size, void *data),
186 void* (*addr)(kstat_t *ksp, loff_t index));
188 extern kstat_t *__kstat_create(const char *ks_module, int ks_instance,
189 const char *ks_name, const char *ks_class, uchar_t ks_type,
190 uint_t ks_ndata, uchar_t ks_flags);
192 extern void __kstat_install(kstat_t *ksp);
193 extern void __kstat_delete(kstat_t *ksp);
194 extern void kstat_waitq_enter(kstat_io_t *);
195 extern void kstat_waitq_exit(kstat_io_t *);
196 extern void kstat_runq_enter(kstat_io_t *);
197 extern void kstat_runq_exit(kstat_io_t *);
199 #define kstat_set_raw_ops(k, h, d, a) \
200 __kstat_set_raw_ops(k, h, d, a)
201 #define kstat_create(m, i, n, c, t, s, f) \
202 __kstat_create(m, i, n, c, t, s, f)
204 #define kstat_install(k) __kstat_install(k)
205 #define kstat_delete(k) __kstat_delete(k)
207 #endif /* _SPL_KSTAT_H */