2 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * Links to Illumos.org for more information on kstat function:
27 * [1] https://illumos.org/man/1M/kstat
28 * [2] https://illumos.org/man/9f/kstat_create
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/sysctl.h>
40 #include <sys/kstat.h>
43 static MALLOC_DEFINE(M_KSTAT, "kstat_data", "Kernel statistics");
45 SYSCTL_ROOT_NODE(OID_AUTO, kstat, CTLFLAG_RW, 0, "Kernel statistics");
48 __kstat_set_raw_ops(kstat_t *ksp,
49 int (*headers)(char *buf, size_t size),
50 int (*data)(char *buf, size_t size, void *data),
51 void *(*addr)(kstat_t *ksp, loff_t index))
53 ksp->ks_raw_ops.headers = headers;
54 ksp->ks_raw_ops.data = data;
55 ksp->ks_raw_ops.addr = addr;
59 __kstat_set_seq_raw_ops(kstat_t *ksp,
60 int (*headers)(struct seq_file *f),
61 int (*data)(char *buf, size_t size, void *data),
62 void *(*addr)(kstat_t *ksp, loff_t index))
64 ksp->ks_raw_ops.seq_headers = headers;
65 ksp->ks_raw_ops.data = data;
66 ksp->ks_raw_ops.addr = addr;
70 kstat_default_update(kstat_t *ksp, int rw)
74 if (rw == KSTAT_WRITE)
81 kstat_resize_raw(kstat_t *ksp)
83 if (ksp->ks_raw_bufsize == KSTAT_RAW_MAX)
86 free(ksp->ks_raw_buf, M_TEMP);
87 ksp->ks_raw_bufsize = MIN(ksp->ks_raw_bufsize * 2, KSTAT_RAW_MAX);
88 ksp->ks_raw_buf = malloc(ksp->ks_raw_bufsize, M_TEMP, M_WAITOK);
94 kstat_raw_default_addr(kstat_t *ksp, loff_t n)
97 return (ksp->ks_data);
102 kstat_sysctl(SYSCTL_HANDLER_ARGS)
105 kstat_named_t *ksent;
108 ksent = ksp->ks_data;
109 /* Select the correct element */
111 /* Update the aggsums before reading */
112 (void) ksp->ks_update(ksp, KSTAT_READ);
113 val = ksent->value.ui64;
115 return (sysctl_handle_64(oidp, &val, 0, req));
119 kstat_sysctl_string(SYSCTL_HANDLER_ARGS)
122 kstat_named_t *ksent = ksp->ks_data;
126 /* Select the correct element */
128 /* Update the aggsums before reading */
129 (void) ksp->ks_update(ksp, KSTAT_READ);
130 val = KSTAT_NAMED_STR_PTR(ksent);
131 len = KSTAT_NAMED_STR_BUFLEN(ksent);
134 return (sysctl_handle_string(oidp, val, len, req));
138 kstat_sysctl_io(SYSCTL_HANDLER_ARGS)
142 kstat_io_t *kip = ksp->ks_data;
145 sb = sbuf_new_auto();
148 /* Update the aggsums before reading */
149 (void) ksp->ks_update(ksp, KSTAT_READ);
151 /* though wlentime & friends are signed, they will never be negative */
153 "%-8llu %-8llu %-8u %-8u %-8llu %-8llu "
154 "%-8llu %-8llu %-8llu %-8llu %-8u %-8u\n",
155 kip->nread, kip->nwritten,
156 kip->reads, kip->writes,
157 kip->wtime, kip->wlentime, kip->wlastupdate,
158 kip->rtime, kip->rlentime, kip->rlastupdate,
159 kip->wcnt, kip->rcnt);
160 rc = sbuf_finish(sb);
162 rc = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
168 kstat_sysctl_raw(SYSCTL_HANDLER_ARGS)
173 void *(*addr_op)(kstat_t *ksp, loff_t index);
174 int n, has_header, rc = 0;
176 sb = sbuf_new_auto();
180 if (ksp->ks_raw_ops.addr)
181 addr_op = ksp->ks_raw_ops.addr;
183 addr_op = kstat_raw_default_addr;
185 mutex_enter(ksp->ks_lock);
187 /* Update the aggsums before reading */
188 (void) ksp->ks_update(ksp, KSTAT_READ);
190 ksp->ks_raw_bufsize = PAGE_SIZE;
191 ksp->ks_raw_buf = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
194 has_header = (ksp->ks_raw_ops.headers ||
195 ksp->ks_raw_ops.seq_headers);
198 if (ksp->ks_raw_ops.headers) {
199 rc = ksp->ks_raw_ops.headers(
200 ksp->ks_raw_buf, ksp->ks_raw_bufsize);
201 } else if (ksp->ks_raw_ops.seq_headers) {
204 f.sf_buf = ksp->ks_raw_buf;
205 f.sf_size = ksp->ks_raw_bufsize;
206 rc = ksp->ks_raw_ops.seq_headers(&f);
209 if (rc == ENOMEM && !kstat_resize_raw(ksp))
210 goto restart_headers;
212 sbuf_printf(sb, "\n%s", ksp->ks_raw_buf);
215 while ((data = addr_op(ksp, n)) != NULL) {
217 if (ksp->ks_raw_ops.data) {
218 rc = ksp->ks_raw_ops.data(ksp->ks_raw_buf,
219 ksp->ks_raw_bufsize, data);
220 if (rc == ENOMEM && !kstat_resize_raw(ksp))
223 sbuf_printf(sb, "%s", ksp->ks_raw_buf);
226 ASSERT(ksp->ks_ndata == 1);
227 sbuf_hexdump(sb, ksp->ks_data,
228 ksp->ks_data_size, NULL, 0);
232 free(ksp->ks_raw_buf, M_TEMP);
233 mutex_exit(ksp->ks_lock);
234 rc = sbuf_finish(sb);
236 rc = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
242 __kstat_create(const char *module, int instance, const char *name,
243 const char *class, uchar_t ks_type, uint_t ks_ndata, uchar_t flags)
245 char buf[KSTAT_STRLEN];
246 struct sysctl_oid *root;
250 KASSERT(instance == 0, ("instance=%d", instance));
251 if ((ks_type == KSTAT_TYPE_INTR) || (ks_type == KSTAT_TYPE_IO))
252 ASSERT(ks_ndata == 1);
258 * Allocate the main structure. We don't need to keep a copy of
259 * module in here, because it is only used for sysctl node creation
260 * done in this function.
262 ksp = malloc(sizeof (*ksp), M_KSTAT, M_WAITOK|M_ZERO);
264 ksp->ks_crtime = gethrtime();
265 ksp->ks_snaptime = ksp->ks_crtime;
266 ksp->ks_instance = instance;
267 (void) strlcpy(ksp->ks_name, name, KSTAT_STRLEN);
268 (void) strlcpy(ksp->ks_class, class, KSTAT_STRLEN);
269 ksp->ks_type = ks_type;
270 ksp->ks_flags = flags;
271 ksp->ks_update = kstat_default_update;
273 mutex_init(&ksp->ks_private_lock, NULL, MUTEX_DEFAULT, NULL);
274 ksp->ks_lock = &ksp->ks_private_lock;
276 switch (ksp->ks_type) {
279 ksp->ks_data_size = ks_ndata;
281 case KSTAT_TYPE_NAMED:
282 ksp->ks_ndata = ks_ndata;
283 ksp->ks_data_size = ks_ndata * sizeof (kstat_named_t);
285 case KSTAT_TYPE_INTR:
286 ksp->ks_ndata = ks_ndata;
287 ksp->ks_data_size = ks_ndata * sizeof (kstat_intr_t);
290 ksp->ks_ndata = ks_ndata;
291 ksp->ks_data_size = ks_ndata * sizeof (kstat_io_t);
293 case KSTAT_TYPE_TIMER:
294 ksp->ks_ndata = ks_ndata;
295 ksp->ks_data_size = ks_ndata * sizeof (kstat_timer_t);
298 panic("Undefined kstat type %d\n", ksp->ks_type);
301 if (ksp->ks_flags & KSTAT_FLAG_VIRTUAL) {
304 ksp->ks_data = kmem_zalloc(ksp->ks_data_size, KM_SLEEP);
305 if (ksp->ks_data == NULL) {
306 kmem_free(ksp, sizeof (*ksp));
312 * Some kstats use a module name like "zfs/poolname" to distinguish a
313 * set of kstats belonging to a specific pool. Split on '/' to add an
314 * extra node for the pool name if needed.
316 (void) strlcpy(buf, module, KSTAT_STRLEN);
318 pool = strchr(module, '/');
323 * Create sysctl tree for those statistics:
325 * kstat.<module>[.<pool>].<class>.<name>
327 sysctl_ctx_init(&ksp->ks_sysctl_ctx);
328 root = SYSCTL_ADD_NODE(&ksp->ks_sysctl_ctx,
329 SYSCTL_STATIC_CHILDREN(_kstat), OID_AUTO, module, CTLFLAG_RW, 0,
332 printf("%s: Cannot create kstat.%s tree!\n", __func__, module);
333 sysctl_ctx_free(&ksp->ks_sysctl_ctx);
338 root = SYSCTL_ADD_NODE(&ksp->ks_sysctl_ctx,
339 SYSCTL_CHILDREN(root), OID_AUTO, pool, CTLFLAG_RW, 0, "");
341 printf("%s: Cannot create kstat.%s.%s tree!\n",
342 __func__, module, pool);
343 sysctl_ctx_free(&ksp->ks_sysctl_ctx);
348 root = SYSCTL_ADD_NODE(&ksp->ks_sysctl_ctx, SYSCTL_CHILDREN(root),
349 OID_AUTO, class, CTLFLAG_RW, 0, "");
352 printf("%s: Cannot create kstat.%s.%s.%s tree!\n",
353 __func__, module, pool, class);
355 printf("%s: Cannot create kstat.%s.%s tree!\n",
356 __func__, module, class);
357 sysctl_ctx_free(&ksp->ks_sysctl_ctx);
361 if (ksp->ks_type == KSTAT_TYPE_NAMED) {
362 root = SYSCTL_ADD_NODE(&ksp->ks_sysctl_ctx,
363 SYSCTL_CHILDREN(root),
364 OID_AUTO, name, CTLFLAG_RW, 0, "");
367 printf("%s: Cannot create kstat.%s.%s.%s.%s "
368 "tree!\n", __func__, module, pool, class,
371 printf("%s: Cannot create kstat.%s.%s.%s "
372 "tree!\n", __func__, module, class, name);
373 sysctl_ctx_free(&ksp->ks_sysctl_ctx);
379 ksp->ks_sysctl_root = root;
385 kstat_install_named(kstat_t *ksp)
387 kstat_named_t *ksent;
391 ksent = ksp->ks_data;
393 VERIFY((ksp->ks_flags & KSTAT_FLAG_VIRTUAL) || ksent != NULL);
398 for (int i = 0; i < ksp->ks_ndata; i++, ksent++) {
399 if (ksent->data_type != 0) {
400 typelast = ksent->data_type;
401 namelast = ksent->name;
404 case KSTAT_DATA_CHAR:
405 /* Not Implemented */
407 case KSTAT_DATA_INT32:
408 SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
409 SYSCTL_CHILDREN(ksp->ks_sysctl_root),
411 CTLTYPE_S32 | CTLFLAG_RD | CTLFLAG_MPSAFE,
412 ksp, i, kstat_sysctl, "I", namelast);
414 case KSTAT_DATA_UINT32:
415 SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
416 SYSCTL_CHILDREN(ksp->ks_sysctl_root),
418 CTLTYPE_U32 | CTLFLAG_RD | CTLFLAG_MPSAFE,
419 ksp, i, kstat_sysctl, "IU", namelast);
421 case KSTAT_DATA_INT64:
422 SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
423 SYSCTL_CHILDREN(ksp->ks_sysctl_root),
425 CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
426 ksp, i, kstat_sysctl, "Q", namelast);
428 case KSTAT_DATA_UINT64:
429 SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
430 SYSCTL_CHILDREN(ksp->ks_sysctl_root),
432 CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
433 ksp, i, kstat_sysctl, "QU", namelast);
435 case KSTAT_DATA_LONG:
436 SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
437 SYSCTL_CHILDREN(ksp->ks_sysctl_root),
439 CTLTYPE_LONG | CTLFLAG_RD | CTLFLAG_MPSAFE,
440 ksp, i, kstat_sysctl, "L", namelast);
442 case KSTAT_DATA_ULONG:
443 SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
444 SYSCTL_CHILDREN(ksp->ks_sysctl_root),
446 CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE,
447 ksp, i, kstat_sysctl, "LU", namelast);
449 case KSTAT_DATA_STRING:
450 SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
451 SYSCTL_CHILDREN(ksp->ks_sysctl_root),
453 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
454 ksp, i, kstat_sysctl_string, "A", namelast);
457 panic("unsupported type: %d", typelast);
463 kstat_install(kstat_t *ksp)
465 struct sysctl_oid *root;
467 if (ksp->ks_ndata == UINT32_MAX)
468 VERIFY(ksp->ks_type == KSTAT_TYPE_RAW);
470 switch (ksp->ks_type) {
471 case KSTAT_TYPE_NAMED:
472 return (kstat_install_named(ksp));
474 if (ksp->ks_raw_ops.data) {
475 root = SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
476 SYSCTL_CHILDREN(ksp->ks_sysctl_root),
477 OID_AUTO, ksp->ks_name,
478 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
479 ksp, 0, kstat_sysctl_raw, "A", ksp->ks_name);
481 root = SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
482 SYSCTL_CHILDREN(ksp->ks_sysctl_root),
483 OID_AUTO, ksp->ks_name,
484 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
485 ksp, 0, kstat_sysctl_raw, "", ksp->ks_name);
489 root = SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
490 SYSCTL_CHILDREN(ksp->ks_sysctl_root),
491 OID_AUTO, ksp->ks_name,
492 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
493 ksp, 0, kstat_sysctl_io, "A", ksp->ks_name);
495 case KSTAT_TYPE_TIMER:
496 case KSTAT_TYPE_INTR:
498 panic("unsupported kstat type %d\n", ksp->ks_type);
500 VERIFY(root != NULL);
501 ksp->ks_sysctl_root = root;
505 kstat_delete(kstat_t *ksp)
508 sysctl_ctx_free(&ksp->ks_sysctl_ctx);
510 mutex_destroy(&ksp->ks_private_lock);
515 kstat_waitq_enter(kstat_io_t *kiop)
521 delta = new - kiop->wlastupdate;
522 kiop->wlastupdate = new;
525 kiop->wlentime += delta * wcnt;
526 kiop->wtime += delta;
531 kstat_waitq_exit(kstat_io_t *kiop)
537 delta = new - kiop->wlastupdate;
538 kiop->wlastupdate = new;
540 ASSERT((int)wcnt > 0);
541 kiop->wlentime += delta * wcnt;
542 kiop->wtime += delta;
546 kstat_runq_enter(kstat_io_t *kiop)
552 delta = new - kiop->rlastupdate;
553 kiop->rlastupdate = new;
556 kiop->rlentime += delta * rcnt;
557 kiop->rtime += delta;
562 kstat_runq_exit(kstat_io_t *kiop)
568 delta = new - kiop->rlastupdate;
569 kiop->rlastupdate = new;
571 ASSERT((int)rcnt > 0);
572 kiop->rlentime += delta * rcnt;
573 kiop->rtime += delta;