]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/dataset_kstats.c
ac0ad84ed63f4b8e499a6cdf833e277b4dac5f9a
[mirror_zfs.git] / module / zfs / dataset_kstats.c
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 */
25
26 #include <sys/dataset_kstats.h>
27 #include <sys/dmu_objset.h>
28 #include <sys/dsl_dataset.h>
29 #include <sys/spa.h>
30
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 },
37 };
38
39 static int
40 dataset_kstats_update(kstat_t *ksp, int rw)
41 {
42 dataset_kstats_t *dk = ksp->ks_private;
43 ASSERT3P(dk->dk_kstats->ks_data, ==, ksp->ks_data);
44
45 if (rw == KSTAT_WRITE)
46 return (EACCES);
47
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);
57
58 return (0);
59 }
60
61 void
62 dataset_kstats_create(dataset_kstats_t *dk, objset_t *objset)
63 {
64 /*
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.
69 */
70 if (dmu_objset_is_snapshot(objset))
71 return;
72
73 /*
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.
82 */
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)));
86 if (n < 0) {
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);
90 return;
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),
95 n, KSTAT_STRLEN);
96 return;
97 }
98
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));
102 if (n < 0) {
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);
106 return;
107 }
108 ASSERT3U(n, <, KSTAT_STRLEN);
109
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),
113 KSTAT_FLAG_VIRTUAL);
114 if (kstat == NULL)
115 return;
116
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));
121
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;
127
128 kstat->ks_data = dk_kstats;
129 kstat->ks_update = dataset_kstats_update;
130 kstat->ks_private = dk;
131
132 kstat_install(kstat);
133 dk->dk_kstats = kstat;
134
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);
139 }
140
141 void
142 dataset_kstats_destroy(dataset_kstats_t *dk)
143 {
144 if (dk->dk_kstats == NULL)
145 return;
146
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));
151
152 kstat_delete(dk->dk_kstats);
153 dk->dk_kstats = NULL;
154
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);
159 }
160
161 void
162 dataset_kstats_update_write_kstats(dataset_kstats_t *dk,
163 int64_t nwritten)
164 {
165 ASSERT3S(nwritten, >=, 0);
166
167 if (dk->dk_kstats == NULL)
168 return;
169
170 aggsum_add(&dk->dk_aggsums.das_writes, 1);
171 aggsum_add(&dk->dk_aggsums.das_nwritten, nwritten);
172 }
173
174 void
175 dataset_kstats_update_read_kstats(dataset_kstats_t *dk,
176 int64_t nread)
177 {
178 ASSERT3S(nread, >=, 0);
179
180 if (dk->dk_kstats == NULL)
181 return;
182
183 aggsum_add(&dk->dk_aggsums.das_reads, 1);
184 aggsum_add(&dk->dk_aggsums.das_nread, nread);
185 }