]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/ddt_stats.c
ddt: remove DDE_GET_NDVAS macro
[mirror_zfs.git] / module / zfs / ddt_stats.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 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
34 static void
35 ddt_stat_generate(ddt_t *ddt, ddt_entry_t *dde, ddt_stat_t *dds)
36 {
37 spa_t *spa = ddt->ddt_spa;
38 ddt_phys_t *ddp = dde->dde_phys;
39 ddt_key_t *ddk = &dde->dde_key;
40 uint64_t lsize = DDK_GET_LSIZE(ddk);
41 uint64_t psize = DDK_GET_PSIZE(ddk);
42
43 memset(dds, 0, sizeof (*dds));
44
45 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
46 uint64_t dsize = 0;
47 uint64_t refcnt = ddp->ddp_refcnt;
48
49 if (ddp->ddp_phys_birth == 0)
50 continue;
51
52 int ndvas = DDK_GET_CRYPT(&dde->dde_key) ?
53 SPA_DVAS_PER_BP - 1 : SPA_DVAS_PER_BP;
54 for (int d = 0; d < ndvas; d++)
55 dsize += dva_get_dsize_sync(spa, &ddp->ddp_dva[d]);
56
57 dds->dds_blocks += 1;
58 dds->dds_lsize += lsize;
59 dds->dds_psize += psize;
60 dds->dds_dsize += dsize;
61
62 dds->dds_ref_blocks += refcnt;
63 dds->dds_ref_lsize += lsize * refcnt;
64 dds->dds_ref_psize += psize * refcnt;
65 dds->dds_ref_dsize += dsize * refcnt;
66 }
67 }
68
69 void
70 ddt_stat_add(ddt_stat_t *dst, const ddt_stat_t *src, uint64_t neg)
71 {
72 const uint64_t *s = (const uint64_t *)src;
73 uint64_t *d = (uint64_t *)dst;
74 uint64_t *d_end = (uint64_t *)(dst + 1);
75
76 ASSERT(neg == 0 || neg == -1ULL); /* add or subtract */
77
78 for (int i = 0; i < d_end - d; i++)
79 d[i] += (s[i] ^ neg) - neg;
80 }
81
82 void
83 ddt_stat_update(ddt_t *ddt, ddt_entry_t *dde, uint64_t neg)
84 {
85 ddt_stat_t dds;
86 ddt_histogram_t *ddh;
87 int bucket;
88
89 ddt_stat_generate(ddt, dde, &dds);
90
91 bucket = highbit64(dds.dds_ref_blocks) - 1;
92 ASSERT3U(bucket, >=, 0);
93
94 ddh = &ddt->ddt_histogram[dde->dde_type][dde->dde_class];
95
96 ddt_stat_add(&ddh->ddh_stat[bucket], &dds, neg);
97 }
98
99 void
100 ddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src)
101 {
102 for (int h = 0; h < 64; h++)
103 ddt_stat_add(&dst->ddh_stat[h], &src->ddh_stat[h], 0);
104 }
105
106 void
107 ddt_histogram_stat(ddt_stat_t *dds, const ddt_histogram_t *ddh)
108 {
109 memset(dds, 0, sizeof (*dds));
110
111 for (int h = 0; h < 64; h++)
112 ddt_stat_add(dds, &ddh->ddh_stat[h], 0);
113 }
114
115 boolean_t
116 ddt_histogram_empty(const ddt_histogram_t *ddh)
117 {
118 const uint64_t *s = (const uint64_t *)ddh;
119 const uint64_t *s_end = (const uint64_t *)(ddh + 1);
120
121 while (s < s_end)
122 if (*s++ != 0)
123 return (B_FALSE);
124
125 return (B_TRUE);
126 }
127
128 void
129 ddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo_total)
130 {
131 /* Sum the statistics we cached in ddt_object_sync(). */
132 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
133 ddt_t *ddt = spa->spa_ddt[c];
134 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
135 for (enum ddt_class class = 0; class < DDT_CLASSES;
136 class++) {
137 ddt_object_t *ddo =
138 &ddt->ddt_object_stats[type][class];
139 ddo_total->ddo_count += ddo->ddo_count;
140 ddo_total->ddo_dspace += ddo->ddo_dspace;
141 ddo_total->ddo_mspace += ddo->ddo_mspace;
142 }
143 }
144 }
145
146 /* ... and compute the averages. */
147 if (ddo_total->ddo_count != 0) {
148 ddo_total->ddo_dspace /= ddo_total->ddo_count;
149 ddo_total->ddo_mspace /= ddo_total->ddo_count;
150 }
151 }
152
153 void
154 ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh)
155 {
156 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
157 ddt_t *ddt = spa->spa_ddt[c];
158 for (enum ddt_type type = 0; type < DDT_TYPES && ddt; type++) {
159 for (enum ddt_class class = 0; class < DDT_CLASSES;
160 class++) {
161 ddt_histogram_add(ddh,
162 &ddt->ddt_histogram_cache[type][class]);
163 }
164 }
165 }
166 }
167
168 void
169 ddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total)
170 {
171 ddt_histogram_t *ddh_total;
172
173 ddh_total = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
174 ddt_get_dedup_histogram(spa, ddh_total);
175 ddt_histogram_stat(dds_total, ddh_total);
176 kmem_free(ddh_total, sizeof (ddt_histogram_t));
177 }
178
179 uint64_t
180 ddt_get_dedup_dspace(spa_t *spa)
181 {
182 ddt_stat_t dds_total;
183
184 if (spa->spa_dedup_dspace != ~0ULL)
185 return (spa->spa_dedup_dspace);
186
187 memset(&dds_total, 0, sizeof (ddt_stat_t));
188
189 /* Calculate and cache the stats */
190 ddt_get_dedup_stats(spa, &dds_total);
191 spa->spa_dedup_dspace = dds_total.dds_ref_dsize - dds_total.dds_dsize;
192 return (spa->spa_dedup_dspace);
193 }
194
195 uint64_t
196 ddt_get_pool_dedup_ratio(spa_t *spa)
197 {
198 ddt_stat_t dds_total = { 0 };
199
200 ddt_get_dedup_stats(spa, &dds_total);
201 if (dds_total.dds_dsize == 0)
202 return (100);
203
204 return (dds_total.dds_ref_dsize * 100 / dds_total.dds_dsize);
205 }