xref: /freebsd/sys/contrib/openzfs/module/zfs/ddt_stats.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
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 	/* Sum the statistics we cached in ddt_object_sync(). */
133 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
134 		ddt_t *ddt = spa->spa_ddt[c];
135 		if (!ddt)
136 			continue;
137 
138 		for (ddt_type_t type = 0; type < DDT_TYPES; type++) {
139 			for (ddt_class_t class = 0; class < DDT_CLASSES;
140 			    class++) {
141 				ddt_object_t *ddo =
142 				    &ddt->ddt_object_stats[type][class];
143 				ddo_total->ddo_count += ddo->ddo_count;
144 				ddo_total->ddo_dspace += ddo->ddo_dspace;
145 				ddo_total->ddo_mspace += ddo->ddo_mspace;
146 			}
147 		}
148 	}
149 
150 	/* ... and compute the averages. */
151 	if (ddo_total->ddo_count != 0) {
152 		ddo_total->ddo_dspace /= ddo_total->ddo_count;
153 		ddo_total->ddo_mspace /= ddo_total->ddo_count;
154 	}
155 }
156 
157 void
158 ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh)
159 {
160 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
161 		ddt_t *ddt = spa->spa_ddt[c];
162 		if (!ddt)
163 			continue;
164 
165 		for (ddt_type_t type = 0; type < DDT_TYPES; type++) {
166 			for (ddt_class_t class = 0; class < DDT_CLASSES;
167 			    class++) {
168 				ddt_histogram_add(ddh,
169 				    &ddt->ddt_histogram_cache[type][class]);
170 			}
171 		}
172 	}
173 }
174 
175 void
176 ddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total)
177 {
178 	ddt_histogram_t *ddh_total;
179 
180 	ddh_total = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
181 	ddt_get_dedup_histogram(spa, ddh_total);
182 	ddt_histogram_stat(dds_total, ddh_total);
183 	kmem_free(ddh_total, sizeof (ddt_histogram_t));
184 }
185 
186 uint64_t
187 ddt_get_dedup_dspace(spa_t *spa)
188 {
189 	ddt_stat_t dds_total;
190 
191 	if (spa->spa_dedup_dspace != ~0ULL)
192 		return (spa->spa_dedup_dspace);
193 
194 	memset(&dds_total, 0, sizeof (ddt_stat_t));
195 
196 	/* Calculate and cache the stats */
197 	ddt_get_dedup_stats(spa, &dds_total);
198 	spa->spa_dedup_dspace = dds_total.dds_ref_dsize - dds_total.dds_dsize;
199 	return (spa->spa_dedup_dspace);
200 }
201 
202 uint64_t
203 ddt_get_pool_dedup_ratio(spa_t *spa)
204 {
205 	ddt_stat_t dds_total = { 0 };
206 
207 	ddt_get_dedup_stats(spa, &dds_total);
208 	if (dds_total.dds_dsize == 0)
209 		return (100);
210 
211 	return (dds_total.dds_ref_dsize * 100 / dds_total.dds_dsize);
212 }
213