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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/sysctl.h>
35 #include <sys/kstat.h>
37 static MALLOC_DEFINE(M_KSTAT, "kstat_data", "Kernel statistics");
39 SYSCTL_NODE(, OID_AUTO, kstat, CTLFLAG_RW, 0, "Kernel statistics");
42 kstat_create(char *module, int instance, char *name, char *class, uchar_t type,
43 ulong_t ndata, uchar_t flags)
45 struct sysctl_oid *root;
48 KASSERT(instance == 0, ("instance=%d", instance));
49 KASSERT(type == KSTAT_TYPE_NAMED, ("type=%hhu", type));
50 KASSERT(flags == KSTAT_FLAG_VIRTUAL, ("flags=%02hhx", flags));
53 * Allocate the main structure. We don't need to copy module/class/name
54 * stuff in here, because it is only used for sysctl node creation
55 * done in this function.
57 ksp = malloc(sizeof(*ksp), M_KSTAT, M_WAITOK);
58 ksp->ks_ndata = ndata;
61 * Create sysctl tree for those statistics:
63 * kstat.<module>.<class>.<name>.
65 sysctl_ctx_init(&ksp->ks_sysctl_ctx);
66 root = SYSCTL_ADD_NODE(&ksp->ks_sysctl_ctx,
67 SYSCTL_STATIC_CHILDREN(_kstat), OID_AUTO, module, CTLFLAG_RW, 0,
70 printf("%s: Cannot create kstat.%s tree!\n", __func__, module);
71 sysctl_ctx_free(&ksp->ks_sysctl_ctx);
75 root = SYSCTL_ADD_NODE(&ksp->ks_sysctl_ctx, SYSCTL_CHILDREN(root),
76 OID_AUTO, class, CTLFLAG_RW, 0, "");
78 printf("%s: Cannot create kstat.%s.%s tree!\n", __func__,
80 sysctl_ctx_free(&ksp->ks_sysctl_ctx);
84 root = SYSCTL_ADD_NODE(&ksp->ks_sysctl_ctx, SYSCTL_CHILDREN(root),
85 OID_AUTO, name, CTLFLAG_RW, 0, "");
87 printf("%s: Cannot create kstat.%s.%s.%s tree!\n", __func__,
89 sysctl_ctx_free(&ksp->ks_sysctl_ctx);
93 ksp->ks_sysctl_root = root;
99 kstat_sysctl(SYSCTL_HANDLER_ARGS)
101 kstat_named_t *ksent = arg1;
104 val = ksent->value.ui64;
105 return sysctl_handle_quad(oidp, &val, 0, req);
109 kstat_install(kstat_t *ksp)
111 kstat_named_t *ksent;
114 ksent = ksp->ks_data;
115 for (i = 0; i < ksp->ks_ndata; i++, ksent++) {
116 KASSERT(ksent->data_type == KSTAT_DATA_UINT64,
117 ("data_type=%d", ksent->data_type));
118 SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
119 SYSCTL_CHILDREN(ksp->ks_sysctl_root), OID_AUTO, ksent->name,
120 CTLTYPE_QUAD | CTLFLAG_RD, ksent, sizeof(*ksent),
121 kstat_sysctl, "QU", "");
126 kstat_delete(kstat_t *ksp)
129 sysctl_ctx_free(&ksp->ks_sysctl_ctx);