xref: /freebsd/sys/contrib/openzfs/module/zfs/dataset_kstats.c (revision 9e5787d2284e187abb5b654d924394a65772e004)
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 http://www.opensolaris.org/os/licensing.
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 
42 static int
43 dataset_kstats_update(kstat_t *ksp, int rw)
44 {
45 	dataset_kstats_t *dk = ksp->ks_private;
46 	ASSERT3P(dk->dk_kstats->ks_data, ==, ksp->ks_data);
47 
48 	if (rw == KSTAT_WRITE)
49 		return (EACCES);
50 
51 	dataset_kstat_values_t *dkv = dk->dk_kstats->ks_data;
52 	dkv->dkv_writes.value.ui64 =
53 	    aggsum_value(&dk->dk_aggsums.das_writes);
54 	dkv->dkv_nwritten.value.ui64 =
55 	    aggsum_value(&dk->dk_aggsums.das_nwritten);
56 	dkv->dkv_reads.value.ui64 =
57 	    aggsum_value(&dk->dk_aggsums.das_reads);
58 	dkv->dkv_nread.value.ui64 =
59 	    aggsum_value(&dk->dk_aggsums.das_nread);
60 	dkv->dkv_nunlinks.value.ui64 =
61 	    aggsum_value(&dk->dk_aggsums.das_nunlinks);
62 	dkv->dkv_nunlinked.value.ui64 =
63 	    aggsum_value(&dk->dk_aggsums.das_nunlinked);
64 
65 	return (0);
66 }
67 
68 void
69 dataset_kstats_create(dataset_kstats_t *dk, objset_t *objset)
70 {
71 	/*
72 	 * There should not be anything wrong with having kstats for
73 	 * snapshots. Since we are not sure how useful they would be
74 	 * though nor how much their memory overhead would matter in
75 	 * a filesystem with many snapshots, we skip them for now.
76 	 */
77 	if (dmu_objset_is_snapshot(objset))
78 		return;
79 
80 	/*
81 	 * At the time of this writing, KSTAT_STRLEN is 255 in Linux,
82 	 * and the spa_name can theoretically be up to 256 characters.
83 	 * In reality though the spa_name can be 240 characters max
84 	 * [see origin directory name check in pool_namecheck()]. Thus,
85 	 * the naming scheme for the module name below should not cause
86 	 * any truncations. In the event that a truncation does happen
87 	 * though, due to some future change, we silently skip creating
88 	 * the kstat and log the event.
89 	 */
90 	char kstat_module_name[KSTAT_STRLEN];
91 	int n = snprintf(kstat_module_name, sizeof (kstat_module_name),
92 	    "zfs/%s", spa_name(dmu_objset_spa(objset)));
93 	if (n < 0) {
94 		zfs_dbgmsg("failed to create dataset kstat for objset %lld: "
95 		    " snprintf() for kstat module name returned %d",
96 		    (unsigned long long)dmu_objset_id(objset), n);
97 		return;
98 	} else if (n >= KSTAT_STRLEN) {
99 		zfs_dbgmsg("failed to create dataset kstat for objset %lld: "
100 		    "kstat module name length (%d) exceeds limit (%d)",
101 		    (unsigned long long)dmu_objset_id(objset),
102 		    n, KSTAT_STRLEN);
103 		return;
104 	}
105 
106 	char kstat_name[KSTAT_STRLEN];
107 	n = snprintf(kstat_name, sizeof (kstat_name), "objset-0x%llx",
108 	    (unsigned long long)dmu_objset_id(objset));
109 	if (n < 0) {
110 		zfs_dbgmsg("failed to create dataset kstat for objset %lld: "
111 		    " snprintf() for kstat name returned %d",
112 		    (unsigned long long)dmu_objset_id(objset), n);
113 		return;
114 	}
115 	ASSERT3U(n, <, KSTAT_STRLEN);
116 
117 	kstat_t *kstat = kstat_create(kstat_module_name, 0, kstat_name,
118 	    "dataset", KSTAT_TYPE_NAMED,
119 	    sizeof (empty_dataset_kstats) / sizeof (kstat_named_t),
120 	    KSTAT_FLAG_VIRTUAL);
121 	if (kstat == NULL)
122 		return;
123 
124 	dataset_kstat_values_t *dk_kstats =
125 	    kmem_alloc(sizeof (empty_dataset_kstats), KM_SLEEP);
126 	bcopy(&empty_dataset_kstats, dk_kstats,
127 	    sizeof (empty_dataset_kstats));
128 
129 	char *ds_name = kmem_zalloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
130 	dsl_dataset_name(objset->os_dsl_dataset, ds_name);
131 	KSTAT_NAMED_STR_PTR(&dk_kstats->dkv_ds_name) = ds_name;
132 	KSTAT_NAMED_STR_BUFLEN(&dk_kstats->dkv_ds_name) =
133 	    ZFS_MAX_DATASET_NAME_LEN;
134 
135 	kstat->ks_data = dk_kstats;
136 	kstat->ks_update = dataset_kstats_update;
137 	kstat->ks_private = dk;
138 	kstat->ks_data_size += ZFS_MAX_DATASET_NAME_LEN;
139 
140 	kstat_install(kstat);
141 	dk->dk_kstats = kstat;
142 
143 	aggsum_init(&dk->dk_aggsums.das_writes, 0);
144 	aggsum_init(&dk->dk_aggsums.das_nwritten, 0);
145 	aggsum_init(&dk->dk_aggsums.das_reads, 0);
146 	aggsum_init(&dk->dk_aggsums.das_nread, 0);
147 	aggsum_init(&dk->dk_aggsums.das_nunlinks, 0);
148 	aggsum_init(&dk->dk_aggsums.das_nunlinked, 0);
149 }
150 
151 void
152 dataset_kstats_destroy(dataset_kstats_t *dk)
153 {
154 	if (dk->dk_kstats == NULL)
155 		return;
156 
157 	dataset_kstat_values_t *dkv = dk->dk_kstats->ks_data;
158 	kmem_free(KSTAT_NAMED_STR_PTR(&dkv->dkv_ds_name),
159 	    KSTAT_NAMED_STR_BUFLEN(&dkv->dkv_ds_name));
160 	kmem_free(dkv, sizeof (empty_dataset_kstats));
161 
162 	kstat_delete(dk->dk_kstats);
163 	dk->dk_kstats = NULL;
164 
165 	aggsum_fini(&dk->dk_aggsums.das_writes);
166 	aggsum_fini(&dk->dk_aggsums.das_nwritten);
167 	aggsum_fini(&dk->dk_aggsums.das_reads);
168 	aggsum_fini(&dk->dk_aggsums.das_nread);
169 	aggsum_fini(&dk->dk_aggsums.das_nunlinks);
170 	aggsum_fini(&dk->dk_aggsums.das_nunlinked);
171 }
172 
173 void
174 dataset_kstats_update_write_kstats(dataset_kstats_t *dk,
175     int64_t nwritten)
176 {
177 	ASSERT3S(nwritten, >=, 0);
178 
179 	if (dk->dk_kstats == NULL)
180 		return;
181 
182 	aggsum_add(&dk->dk_aggsums.das_writes, 1);
183 	aggsum_add(&dk->dk_aggsums.das_nwritten, nwritten);
184 }
185 
186 void
187 dataset_kstats_update_read_kstats(dataset_kstats_t *dk,
188     int64_t nread)
189 {
190 	ASSERT3S(nread, >=, 0);
191 
192 	if (dk->dk_kstats == NULL)
193 		return;
194 
195 	aggsum_add(&dk->dk_aggsums.das_reads, 1);
196 	aggsum_add(&dk->dk_aggsums.das_nread, nread);
197 }
198 
199 void
200 dataset_kstats_update_nunlinks_kstat(dataset_kstats_t *dk, int64_t delta)
201 {
202 	if (dk->dk_kstats == NULL)
203 		return;
204 
205 	aggsum_add(&dk->dk_aggsums.das_nunlinks, delta);
206 }
207 
208 void
209 dataset_kstats_update_nunlinked_kstat(dataset_kstats_t *dk, int64_t delta)
210 {
211 	if (dk->dk_kstats == NULL)
212 		return;
213 
214 	aggsum_add(&dk->dk_aggsums.das_nunlinked, delta);
215 }
216