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) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2012, 2016 by Delphix. All rights reserved. 25 * Copyright (c) 2022 by Pawel Jakub Dawidek 26 * Copyright (c) 2023, Klara Inc. 27 */ 28 29 #include <sys/zfs_context.h> 30 #include <sys/spa.h> 31 #include <sys/spa_impl.h> 32 #include <sys/ddt.h> 33 #include <sys/ddt_impl.h> 34 35 static void 36 ddt_stat_generate(ddt_t *ddt, ddt_entry_t *dde, ddt_stat_t *dds) 37 { 38 spa_t *spa = ddt->ddt_spa; 39 ddt_phys_t *ddp = dde->dde_phys; 40 ddt_key_t *ddk = &dde->dde_key; 41 uint64_t lsize = DDK_GET_LSIZE(ddk); 42 uint64_t psize = DDK_GET_PSIZE(ddk); 43 44 memset(dds, 0, sizeof (*dds)); 45 46 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 47 uint64_t dsize = 0; 48 uint64_t refcnt = ddp->ddp_refcnt; 49 50 if (ddp->ddp_phys_birth == 0) 51 continue; 52 53 int ndvas = DDK_GET_CRYPT(&dde->dde_key) ? 54 SPA_DVAS_PER_BP - 1 : SPA_DVAS_PER_BP; 55 for (int d = 0; d < ndvas; d++) 56 dsize += dva_get_dsize_sync(spa, &ddp->ddp_dva[d]); 57 58 dds->dds_blocks += 1; 59 dds->dds_lsize += lsize; 60 dds->dds_psize += psize; 61 dds->dds_dsize += dsize; 62 63 dds->dds_ref_blocks += refcnt; 64 dds->dds_ref_lsize += lsize * refcnt; 65 dds->dds_ref_psize += psize * refcnt; 66 dds->dds_ref_dsize += dsize * refcnt; 67 } 68 } 69 70 void 71 ddt_stat_add(ddt_stat_t *dst, const ddt_stat_t *src, uint64_t neg) 72 { 73 const uint64_t *s = (const uint64_t *)src; 74 uint64_t *d = (uint64_t *)dst; 75 uint64_t *d_end = (uint64_t *)(dst + 1); 76 77 ASSERT(neg == 0 || neg == -1ULL); /* add or subtract */ 78 79 for (int i = 0; i < d_end - d; i++) 80 d[i] += (s[i] ^ neg) - neg; 81 } 82 83 void 84 ddt_stat_update(ddt_t *ddt, ddt_entry_t *dde, uint64_t neg) 85 { 86 ddt_stat_t dds; 87 ddt_histogram_t *ddh; 88 int bucket; 89 90 ddt_stat_generate(ddt, dde, &dds); 91 92 bucket = highbit64(dds.dds_ref_blocks) - 1; 93 ASSERT3U(bucket, >=, 0); 94 95 ddh = &ddt->ddt_histogram[dde->dde_type][dde->dde_class]; 96 97 ddt_stat_add(&ddh->ddh_stat[bucket], &dds, neg); 98 } 99 100 void 101 ddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src) 102 { 103 for (int h = 0; h < 64; h++) 104 ddt_stat_add(&dst->ddh_stat[h], &src->ddh_stat[h], 0); 105 } 106 107 void 108 ddt_histogram_stat(ddt_stat_t *dds, const ddt_histogram_t *ddh) 109 { 110 memset(dds, 0, sizeof (*dds)); 111 112 for (int h = 0; h < 64; h++) 113 ddt_stat_add(dds, &ddh->ddh_stat[h], 0); 114 } 115 116 boolean_t 117 ddt_histogram_empty(const ddt_histogram_t *ddh) 118 { 119 const uint64_t *s = (const uint64_t *)ddh; 120 const uint64_t *s_end = (const uint64_t *)(ddh + 1); 121 122 while (s < s_end) 123 if (*s++ != 0) 124 return (B_FALSE); 125 126 return (B_TRUE); 127 } 128 129 void 130 ddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo_total) 131 { 132 memset(ddo_total, 0, sizeof (*ddo_total)); 133 134 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { 135 ddt_t *ddt = spa->spa_ddt[c]; 136 if (!ddt) 137 continue; 138 139 for (ddt_type_t type = 0; type < DDT_TYPES; type++) { 140 for (ddt_class_t class = 0; class < DDT_CLASSES; 141 class++) { 142 dmu_object_info_t doi; 143 uint64_t cnt; 144 int err; 145 146 /* 147 * These stats were originally calculated 148 * during ddt_object_load(). 149 */ 150 151 err = ddt_object_info(ddt, type, class, &doi); 152 if (err != 0) 153 continue; 154 155 err = ddt_object_count(ddt, type, class, &cnt); 156 if (err != 0) 157 continue; 158 159 ddt_object_t *ddo = 160 &ddt->ddt_object_stats[type][class]; 161 162 ddo->ddo_count = cnt; 163 ddo->ddo_dspace = 164 doi.doi_physical_blocks_512 << 9; 165 ddo->ddo_mspace = doi.doi_fill_count * 166 doi.doi_data_block_size; 167 168 ddo_total->ddo_count += ddo->ddo_count; 169 ddo_total->ddo_dspace += ddo->ddo_dspace; 170 ddo_total->ddo_mspace += ddo->ddo_mspace; 171 } 172 } 173 } 174 175 /* 176 * This returns raw counts (not averages). One of the consumers, 177 * print_dedup_stats(), historically has expected raw counts. 178 */ 179 180 spa->spa_dedup_dsize = ddo_total->ddo_dspace; 181 } 182 183 uint64_t 184 ddt_get_ddt_dsize(spa_t *spa) 185 { 186 ddt_object_t ddo_total; 187 188 /* recalculate after each txg sync */ 189 if (spa->spa_dedup_dsize == ~0ULL) 190 ddt_get_dedup_object_stats(spa, &ddo_total); 191 192 return (spa->spa_dedup_dsize); 193 } 194 195 void 196 ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh) 197 { 198 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { 199 ddt_t *ddt = spa->spa_ddt[c]; 200 if (!ddt) 201 continue; 202 203 for (ddt_type_t type = 0; type < DDT_TYPES; type++) { 204 for (ddt_class_t class = 0; class < DDT_CLASSES; 205 class++) { 206 ddt_histogram_add(ddh, 207 &ddt->ddt_histogram_cache[type][class]); 208 } 209 } 210 } 211 } 212 213 void 214 ddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total) 215 { 216 ddt_histogram_t *ddh_total; 217 218 ddh_total = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP); 219 ddt_get_dedup_histogram(spa, ddh_total); 220 ddt_histogram_stat(dds_total, ddh_total); 221 kmem_free(ddh_total, sizeof (ddt_histogram_t)); 222 } 223 224 uint64_t 225 ddt_get_dedup_dspace(spa_t *spa) 226 { 227 ddt_stat_t dds_total; 228 229 if (spa->spa_dedup_dspace != ~0ULL) 230 return (spa->spa_dedup_dspace); 231 232 memset(&dds_total, 0, sizeof (ddt_stat_t)); 233 234 /* Calculate and cache the stats */ 235 ddt_get_dedup_stats(spa, &dds_total); 236 spa->spa_dedup_dspace = dds_total.dds_ref_dsize - dds_total.dds_dsize; 237 return (spa->spa_dedup_dspace); 238 } 239 240 uint64_t 241 ddt_get_pool_dedup_ratio(spa_t *spa) 242 { 243 ddt_stat_t dds_total = { 0 }; 244 245 ddt_get_dedup_stats(spa, &dds_total); 246 if (dds_total.dds_dsize == 0) 247 return (100); 248 249 return (dds_total.dds_ref_dsize * 100 / dds_total.dds_dsize); 250 } 251 252 int 253 ddt_get_pool_dedup_cached(spa_t *spa, uint64_t *psize) 254 { 255 uint64_t l1sz, l1tot, l2sz, l2tot; 256 int err = 0; 257 258 l1tot = l2tot = 0; 259 *psize = 0; 260 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { 261 ddt_t *ddt = spa->spa_ddt[c]; 262 if (ddt == NULL) 263 continue; 264 for (ddt_type_t type = 0; type < DDT_TYPES; type++) { 265 for (ddt_class_t class = 0; class < DDT_CLASSES; 266 class++) { 267 err = dmu_object_cached_size(ddt->ddt_os, 268 ddt->ddt_object[type][class], &l1sz, &l2sz); 269 if (err != 0) 270 return (err); 271 l1tot += l1sz; 272 l2tot += l2sz; 273 } 274 } 275 } 276 277 *psize = l1tot + l2tot; 278 return (err); 279 } 280