4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2018 by Delphix. All rights reserved.
26 #include <sys/dataset_kstats.h>
27 #include <sys/dmu_objset.h>
28 #include <sys/dsl_dataset.h>
31 static dataset_kstat_values_t empty_dataset_kstats = {
32 { "dataset_name", KSTAT_DATA_STRING },
33 { "writes", KSTAT_DATA_UINT64 },
34 { "nwritten", KSTAT_DATA_UINT64 },
35 { "reads", KSTAT_DATA_UINT64 },
36 { "nread", KSTAT_DATA_UINT64 },
40 dataset_kstats_update(kstat_t *ksp, int rw)
42 dataset_kstats_t *dk = ksp->ks_private;
43 ASSERT3P(dk->dk_kstats->ks_data, ==, ksp->ks_data);
45 if (rw == KSTAT_WRITE)
48 dataset_kstat_values_t *dkv = dk->dk_kstats->ks_data;
49 dkv->dkv_writes.value.ui64 =
50 aggsum_value(&dk->dk_aggsums.das_writes);
51 dkv->dkv_nwritten.value.ui64 =
52 aggsum_value(&dk->dk_aggsums.das_nwritten);
53 dkv->dkv_reads.value.ui64 =
54 aggsum_value(&dk->dk_aggsums.das_reads);
55 dkv->dkv_nread.value.ui64 =
56 aggsum_value(&dk->dk_aggsums.das_nread);
62 dataset_kstats_create(dataset_kstats_t *dk, objset_t *objset)
65 * There should not be anything wrong with having kstats for
66 * snapshots. Since we are not sure how useful they would be
67 * though nor how much their memory overhead would matter in
68 * a filesystem with many snapshots, we skip them for now.
70 if (dmu_objset_is_snapshot(objset))
74 * At the time of this writing, KSTAT_STRLEN is 255 in Linux,
75 * and the spa_name can theoretically be up to 256 characters.
76 * In reality though the spa_name can be 240 characters max
77 * [see origin directory name check in pool_namecheck()]. Thus,
78 * the naming scheme for the module name below should not cause
79 * any truncations. In the event that a truncation does happen
80 * though, due to some future change, we silently skip creating
81 * the kstat and log the event.
83 char kstat_module_name[KSTAT_STRLEN];
84 int n = snprintf(kstat_module_name, sizeof (kstat_module_name),
85 "zfs/%s", spa_name(dmu_objset_spa(objset)));
87 zfs_dbgmsg("failed to create dataset kstat for objset %lld: "
88 " snprintf() for kstat module name returned %d",
89 (unsigned long long)dmu_objset_id(objset), n);
91 } else if (n >= KSTAT_STRLEN) {
92 zfs_dbgmsg("failed to create dataset kstat for objset %lld: "
93 "kstat module name length (%d) exceeds limit (%d)",
94 (unsigned long long)dmu_objset_id(objset),
99 char kstat_name[KSTAT_STRLEN];
100 n = snprintf(kstat_name, sizeof (kstat_name), "objset-0x%llx",
101 (unsigned long long)dmu_objset_id(objset));
103 zfs_dbgmsg("failed to create dataset kstat for objset %lld: "
104 " snprintf() for kstat name returned %d",
105 (unsigned long long)dmu_objset_id(objset), n);
108 ASSERT3U(n, <, KSTAT_STRLEN);
110 kstat_t *kstat = kstat_create(kstat_module_name, 0, kstat_name,
111 "dataset", KSTAT_TYPE_NAMED,
112 sizeof (empty_dataset_kstats) / sizeof (kstat_named_t),
117 dataset_kstat_values_t *dk_kstats =
118 kmem_alloc(sizeof (empty_dataset_kstats), KM_SLEEP);
119 bcopy(&empty_dataset_kstats, dk_kstats,
120 sizeof (empty_dataset_kstats));
122 char *ds_name = kmem_zalloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
123 dsl_dataset_name(objset->os_dsl_dataset, ds_name);
124 KSTAT_NAMED_STR_PTR(&dk_kstats->dkv_ds_name) = ds_name;
125 KSTAT_NAMED_STR_BUFLEN(&dk_kstats->dkv_ds_name) =
126 ZFS_MAX_DATASET_NAME_LEN;
128 kstat->ks_data = dk_kstats;
129 kstat->ks_update = dataset_kstats_update;
130 kstat->ks_private = dk;
132 kstat_install(kstat);
133 dk->dk_kstats = kstat;
135 aggsum_init(&dk->dk_aggsums.das_writes, 0);
136 aggsum_init(&dk->dk_aggsums.das_nwritten, 0);
137 aggsum_init(&dk->dk_aggsums.das_reads, 0);
138 aggsum_init(&dk->dk_aggsums.das_nread, 0);
142 dataset_kstats_destroy(dataset_kstats_t *dk)
144 if (dk->dk_kstats == NULL)
147 dataset_kstat_values_t *dkv = dk->dk_kstats->ks_data;
148 kmem_free(KSTAT_NAMED_STR_PTR(&dkv->dkv_ds_name),
149 KSTAT_NAMED_STR_BUFLEN(&dkv->dkv_ds_name));
150 kmem_free(dkv, sizeof (empty_dataset_kstats));
152 kstat_delete(dk->dk_kstats);
153 dk->dk_kstats = NULL;
155 aggsum_fini(&dk->dk_aggsums.das_writes);
156 aggsum_fini(&dk->dk_aggsums.das_nwritten);
157 aggsum_fini(&dk->dk_aggsums.das_reads);
158 aggsum_fini(&dk->dk_aggsums.das_nread);
162 dataset_kstats_update_write_kstats(dataset_kstats_t *dk,
165 ASSERT3S(nwritten, >=, 0);
167 if (dk->dk_kstats == NULL)
170 aggsum_add(&dk->dk_aggsums.das_writes, 1);
171 aggsum_add(&dk->dk_aggsums.das_nwritten, nwritten);
175 dataset_kstats_update_read_kstats(dataset_kstats_t *dk,
178 ASSERT3S(nread, >=, 0);
180 if (dk->dk_kstats == NULL)
183 aggsum_add(&dk->dk_aggsums.das_reads, 1);
184 aggsum_add(&dk->dk_aggsums.das_nread, nread);