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