xref: /freebsd/sys/contrib/openzfs/module/zfs/dataset_kstats.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or https://opensource.org/licenses/CDDL-1.0.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2018 by Delphix. All rights reserved.
24  * Copyright (c) 2018 Datto Inc.
25  */
26 
27 #include <sys/dataset_kstats.h>
28 #include <sys/dmu_objset.h>
29 #include <sys/dsl_dataset.h>
30 #include <sys/spa.h>
31 
32 static dataset_kstat_values_t empty_dataset_kstats = {
33 	{ "dataset_name",	KSTAT_DATA_STRING },
34 	{ "writes",	KSTAT_DATA_UINT64 },
35 	{ "nwritten",	KSTAT_DATA_UINT64 },
36 	{ "reads",	KSTAT_DATA_UINT64 },
37 	{ "nread",	KSTAT_DATA_UINT64 },
38 	{ "nunlinks",	KSTAT_DATA_UINT64 },
39 	{ "nunlinked",	KSTAT_DATA_UINT64 },
40 	{
41 	{ "zil_commit_count",			KSTAT_DATA_UINT64 },
42 	{ "zil_commit_writer_count",		KSTAT_DATA_UINT64 },
43 	{ "zil_itx_count",			KSTAT_DATA_UINT64 },
44 	{ "zil_itx_indirect_count",		KSTAT_DATA_UINT64 },
45 	{ "zil_itx_indirect_bytes",		KSTAT_DATA_UINT64 },
46 	{ "zil_itx_copied_count",		KSTAT_DATA_UINT64 },
47 	{ "zil_itx_copied_bytes",		KSTAT_DATA_UINT64 },
48 	{ "zil_itx_needcopy_count",		KSTAT_DATA_UINT64 },
49 	{ "zil_itx_needcopy_bytes",		KSTAT_DATA_UINT64 },
50 	{ "zil_itx_metaslab_normal_count",	KSTAT_DATA_UINT64 },
51 	{ "zil_itx_metaslab_normal_bytes",	KSTAT_DATA_UINT64 },
52 	{ "zil_itx_metaslab_normal_write",	KSTAT_DATA_UINT64 },
53 	{ "zil_itx_metaslab_normal_alloc",	KSTAT_DATA_UINT64 },
54 	{ "zil_itx_metaslab_slog_count",	KSTAT_DATA_UINT64 },
55 	{ "zil_itx_metaslab_slog_bytes",	KSTAT_DATA_UINT64 },
56 	{ "zil_itx_metaslab_slog_write",	KSTAT_DATA_UINT64 },
57 	{ "zil_itx_metaslab_slog_alloc",	KSTAT_DATA_UINT64 }
58 	}
59 };
60 
61 static int
62 dataset_kstats_update(kstat_t *ksp, int rw)
63 {
64 	dataset_kstats_t *dk = ksp->ks_private;
65 	dataset_kstat_values_t *dkv = ksp->ks_data;
66 	ASSERT3P(dk->dk_kstats->ks_data, ==, dkv);
67 
68 	if (rw == KSTAT_WRITE)
69 		return (EACCES);
70 
71 	dkv->dkv_writes.value.ui64 =
72 	    wmsum_value(&dk->dk_sums.dss_writes);
73 	dkv->dkv_nwritten.value.ui64 =
74 	    wmsum_value(&dk->dk_sums.dss_nwritten);
75 	dkv->dkv_reads.value.ui64 =
76 	    wmsum_value(&dk->dk_sums.dss_reads);
77 	dkv->dkv_nread.value.ui64 =
78 	    wmsum_value(&dk->dk_sums.dss_nread);
79 	dkv->dkv_nunlinks.value.ui64 =
80 	    wmsum_value(&dk->dk_sums.dss_nunlinks);
81 	dkv->dkv_nunlinked.value.ui64 =
82 	    wmsum_value(&dk->dk_sums.dss_nunlinked);
83 
84 	zil_kstat_values_update(&dkv->dkv_zil_stats, &dk->dk_zil_sums);
85 
86 	return (0);
87 }
88 
89 int
90 dataset_kstats_create(dataset_kstats_t *dk, objset_t *objset)
91 {
92 	/*
93 	 * There should not be anything wrong with having kstats for
94 	 * snapshots. Since we are not sure how useful they would be
95 	 * though nor how much their memory overhead would matter in
96 	 * a filesystem with many snapshots, we skip them for now.
97 	 */
98 	if (dmu_objset_is_snapshot(objset))
99 		return (0);
100 
101 	/*
102 	 * At the time of this writing, KSTAT_STRLEN is 255 in Linux,
103 	 * and the spa_name can theoretically be up to 256 characters.
104 	 * In reality though the spa_name can be 240 characters max
105 	 * [see origin directory name check in pool_namecheck()]. Thus,
106 	 * the naming scheme for the module name below should not cause
107 	 * any truncations. In the event that a truncation does happen
108 	 * though, due to some future change, we silently skip creating
109 	 * the kstat and log the event.
110 	 */
111 	char kstat_module_name[KSTAT_STRLEN];
112 	int n = snprintf(kstat_module_name, sizeof (kstat_module_name),
113 	    "zfs/%s", spa_name(dmu_objset_spa(objset)));
114 	if (n < 0) {
115 		zfs_dbgmsg("failed to create dataset kstat for objset %lld: "
116 		    " snprintf() for kstat module name returned %d",
117 		    (unsigned long long)dmu_objset_id(objset), n);
118 		return (SET_ERROR(EINVAL));
119 	} else if (n >= KSTAT_STRLEN) {
120 		zfs_dbgmsg("failed to create dataset kstat for objset %lld: "
121 		    "kstat module name length (%d) exceeds limit (%d)",
122 		    (unsigned long long)dmu_objset_id(objset),
123 		    n, KSTAT_STRLEN);
124 		return (SET_ERROR(ENAMETOOLONG));
125 	}
126 
127 	char kstat_name[KSTAT_STRLEN];
128 	n = snprintf(kstat_name, sizeof (kstat_name), "objset-0x%llx",
129 	    (unsigned long long)dmu_objset_id(objset));
130 	if (n < 0) {
131 		zfs_dbgmsg("failed to create dataset kstat for objset %lld: "
132 		    " snprintf() for kstat name returned %d",
133 		    (unsigned long long)dmu_objset_id(objset), n);
134 		return (SET_ERROR(EINVAL));
135 	} else if (n >= KSTAT_STRLEN) {
136 		zfs_dbgmsg("failed to create dataset kstat for objset %lld: "
137 		    "kstat name length (%d) exceeds limit (%d)",
138 		    (unsigned long long)dmu_objset_id(objset),
139 		    n, KSTAT_STRLEN);
140 		return (SET_ERROR(ENAMETOOLONG));
141 	}
142 
143 	kstat_t *kstat = kstat_create(kstat_module_name, 0, kstat_name,
144 	    "dataset", KSTAT_TYPE_NAMED,
145 	    sizeof (empty_dataset_kstats) / sizeof (kstat_named_t),
146 	    KSTAT_FLAG_VIRTUAL);
147 	if (kstat == NULL)
148 		return (SET_ERROR(ENOMEM));
149 
150 	dataset_kstat_values_t *dk_kstats =
151 	    kmem_alloc(sizeof (empty_dataset_kstats), KM_SLEEP);
152 	memcpy(dk_kstats, &empty_dataset_kstats,
153 	    sizeof (empty_dataset_kstats));
154 
155 	char *ds_name = kmem_zalloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
156 	dsl_dataset_name(objset->os_dsl_dataset, ds_name);
157 	KSTAT_NAMED_STR_PTR(&dk_kstats->dkv_ds_name) = ds_name;
158 	KSTAT_NAMED_STR_BUFLEN(&dk_kstats->dkv_ds_name) =
159 	    ZFS_MAX_DATASET_NAME_LEN;
160 
161 	kstat->ks_data = dk_kstats;
162 	kstat->ks_update = dataset_kstats_update;
163 	kstat->ks_private = dk;
164 	kstat->ks_data_size += ZFS_MAX_DATASET_NAME_LEN;
165 
166 	wmsum_init(&dk->dk_sums.dss_writes, 0);
167 	wmsum_init(&dk->dk_sums.dss_nwritten, 0);
168 	wmsum_init(&dk->dk_sums.dss_reads, 0);
169 	wmsum_init(&dk->dk_sums.dss_nread, 0);
170 	wmsum_init(&dk->dk_sums.dss_nunlinks, 0);
171 	wmsum_init(&dk->dk_sums.dss_nunlinked, 0);
172 	zil_sums_init(&dk->dk_zil_sums);
173 
174 	dk->dk_kstats = kstat;
175 	kstat_install(kstat);
176 	return (0);
177 }
178 
179 void
180 dataset_kstats_destroy(dataset_kstats_t *dk)
181 {
182 	if (dk->dk_kstats == NULL)
183 		return;
184 
185 	dataset_kstat_values_t *dkv = dk->dk_kstats->ks_data;
186 	kstat_delete(dk->dk_kstats);
187 	dk->dk_kstats = NULL;
188 	kmem_free(KSTAT_NAMED_STR_PTR(&dkv->dkv_ds_name),
189 	    KSTAT_NAMED_STR_BUFLEN(&dkv->dkv_ds_name));
190 	kmem_free(dkv, sizeof (empty_dataset_kstats));
191 
192 	wmsum_fini(&dk->dk_sums.dss_writes);
193 	wmsum_fini(&dk->dk_sums.dss_nwritten);
194 	wmsum_fini(&dk->dk_sums.dss_reads);
195 	wmsum_fini(&dk->dk_sums.dss_nread);
196 	wmsum_fini(&dk->dk_sums.dss_nunlinks);
197 	wmsum_fini(&dk->dk_sums.dss_nunlinked);
198 	zil_sums_fini(&dk->dk_zil_sums);
199 }
200 
201 void
202 dataset_kstats_rename(dataset_kstats_t *dk, const char *name)
203 {
204 	dataset_kstat_values_t *dkv = dk->dk_kstats->ks_data;
205 	char *ds_name;
206 
207 	ds_name = KSTAT_NAMED_STR_PTR(&dkv->dkv_ds_name);
208 	ASSERT3S(ds_name, !=, NULL);
209 	(void) strlcpy(ds_name, name,
210 	    KSTAT_NAMED_STR_BUFLEN(&dkv->dkv_ds_name));
211 }
212 
213 void
214 dataset_kstats_update_write_kstats(dataset_kstats_t *dk,
215     int64_t nwritten)
216 {
217 	ASSERT3S(nwritten, >=, 0);
218 
219 	if (dk->dk_kstats == NULL)
220 		return;
221 
222 	wmsum_add(&dk->dk_sums.dss_writes, 1);
223 	wmsum_add(&dk->dk_sums.dss_nwritten, nwritten);
224 }
225 
226 void
227 dataset_kstats_update_read_kstats(dataset_kstats_t *dk,
228     int64_t nread)
229 {
230 	ASSERT3S(nread, >=, 0);
231 
232 	if (dk->dk_kstats == NULL)
233 		return;
234 
235 	wmsum_add(&dk->dk_sums.dss_reads, 1);
236 	wmsum_add(&dk->dk_sums.dss_nread, nread);
237 }
238 
239 void
240 dataset_kstats_update_nunlinks_kstat(dataset_kstats_t *dk, int64_t delta)
241 {
242 	if (dk->dk_kstats == NULL)
243 		return;
244 
245 	wmsum_add(&dk->dk_sums.dss_nunlinks, delta);
246 }
247 
248 void
249 dataset_kstats_update_nunlinked_kstat(dataset_kstats_t *dk, int64_t delta)
250 {
251 	if (dk->dk_kstats == NULL)
252 		return;
253 
254 	wmsum_add(&dk->dk_sums.dss_nunlinked, delta);
255 }
256