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.
24 * Copyright (c) 2018 Datto Inc.
27 #include <sys/dataset_kstats.h>
28 #include <sys/dmu_objset.h>
29 #include <sys/dsl_dataset.h>
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
},
43 dataset_kstats_update(kstat_t
*ksp
, int rw
)
45 dataset_kstats_t
*dk
= ksp
->ks_private
;
46 ASSERT3P(dk
->dk_kstats
->ks_data
, ==, ksp
->ks_data
);
48 if (rw
== KSTAT_WRITE
)
51 dataset_kstat_values_t
*dkv
= dk
->dk_kstats
->ks_data
;
52 dkv
->dkv_writes
.value
.ui64
=
53 wmsum_value(&dk
->dk_sums
.dss_writes
);
54 dkv
->dkv_nwritten
.value
.ui64
=
55 wmsum_value(&dk
->dk_sums
.dss_nwritten
);
56 dkv
->dkv_reads
.value
.ui64
=
57 wmsum_value(&dk
->dk_sums
.dss_reads
);
58 dkv
->dkv_nread
.value
.ui64
=
59 wmsum_value(&dk
->dk_sums
.dss_nread
);
60 dkv
->dkv_nunlinks
.value
.ui64
=
61 wmsum_value(&dk
->dk_sums
.dss_nunlinks
);
62 dkv
->dkv_nunlinked
.value
.ui64
=
63 wmsum_value(&dk
->dk_sums
.dss_nunlinked
);
69 dataset_kstats_create(dataset_kstats_t
*dk
, objset_t
*objset
)
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.
77 if (dmu_objset_is_snapshot(objset
))
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.
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
)));
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
);
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
),
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
));
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
);
115 ASSERT3U(n
, <, KSTAT_STRLEN
);
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
),
124 dataset_kstat_values_t
*dk_kstats
=
125 kmem_alloc(sizeof (empty_dataset_kstats
), KM_SLEEP
);
126 memcpy(dk_kstats
, &empty_dataset_kstats
,
127 sizeof (empty_dataset_kstats
));
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
;
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
;
140 kstat_install(kstat
);
141 dk
->dk_kstats
= kstat
;
143 wmsum_init(&dk
->dk_sums
.dss_writes
, 0);
144 wmsum_init(&dk
->dk_sums
.dss_nwritten
, 0);
145 wmsum_init(&dk
->dk_sums
.dss_reads
, 0);
146 wmsum_init(&dk
->dk_sums
.dss_nread
, 0);
147 wmsum_init(&dk
->dk_sums
.dss_nunlinks
, 0);
148 wmsum_init(&dk
->dk_sums
.dss_nunlinked
, 0);
152 dataset_kstats_destroy(dataset_kstats_t
*dk
)
154 if (dk
->dk_kstats
== NULL
)
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
));
162 kstat_delete(dk
->dk_kstats
);
163 dk
->dk_kstats
= NULL
;
165 wmsum_fini(&dk
->dk_sums
.dss_writes
);
166 wmsum_fini(&dk
->dk_sums
.dss_nwritten
);
167 wmsum_fini(&dk
->dk_sums
.dss_reads
);
168 wmsum_fini(&dk
->dk_sums
.dss_nread
);
169 wmsum_fini(&dk
->dk_sums
.dss_nunlinks
);
170 wmsum_fini(&dk
->dk_sums
.dss_nunlinked
);
174 dataset_kstats_update_write_kstats(dataset_kstats_t
*dk
,
177 ASSERT3S(nwritten
, >=, 0);
179 if (dk
->dk_kstats
== NULL
)
182 wmsum_add(&dk
->dk_sums
.dss_writes
, 1);
183 wmsum_add(&dk
->dk_sums
.dss_nwritten
, nwritten
);
187 dataset_kstats_update_read_kstats(dataset_kstats_t
*dk
,
190 ASSERT3S(nread
, >=, 0);
192 if (dk
->dk_kstats
== NULL
)
195 wmsum_add(&dk
->dk_sums
.dss_reads
, 1);
196 wmsum_add(&dk
->dk_sums
.dss_nread
, nread
);
200 dataset_kstats_update_nunlinks_kstat(dataset_kstats_t
*dk
, int64_t delta
)
202 if (dk
->dk_kstats
== NULL
)
205 wmsum_add(&dk
->dk_sums
.dss_nunlinks
, delta
);
209 dataset_kstats_update_nunlinked_kstat(dataset_kstats_t
*dk
, int64_t delta
)
211 if (dk
->dk_kstats
== NULL
)
214 wmsum_add(&dk
->dk_sums
.dss_nunlinked
, delta
);