Print this page
4185 New hash algorithm support
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/common/zfs/zfs_fletcher.c
+++ new/usr/src/common/zfs/zfs_fletcher.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 +/*
26 + * Copyright 2013 Saso Kiselkov. All rights reserved.
27 + */
25 28
26 29 /*
27 30 * Fletcher Checksums
28 31 * ------------------
29 32 *
30 33 * ZFS's 2nd and 4th order Fletcher checksums are defined by the following
31 34 * recurrence relations:
32 35 *
33 36 * a = a + f
34 37 * i i-1 i-1
35 38 *
36 39 * b = b + a
37 40 * i i-1 i
38 41 *
39 42 * c = c + b (fletcher-4 only)
40 43 * i i-1 i
41 44 *
42 45 * d = d + c (fletcher-4 only)
43 46 * i i-1 i
44 47 *
45 48 * Where
46 49 * a_0 = b_0 = c_0 = d_0 = 0
47 50 * and
48 51 * f_0 .. f_(n-1) are the input data.
49 52 *
50 53 * Using standard techniques, these translate into the following series:
51 54 *
52 55 * __n_ __n_
53 56 * \ | \ |
54 57 * a = > f b = > i * f
55 58 * n /___| n - i n /___| n - i
56 59 * i = 1 i = 1
57 60 *
58 61 *
59 62 * __n_ __n_
60 63 * \ | i*(i+1) \ | i*(i+1)*(i+2)
61 64 * c = > ------- f d = > ------------- f
62 65 * n /___| 2 n - i n /___| 6 n - i
63 66 * i = 1 i = 1
64 67 *
65 68 * For fletcher-2, the f_is are 64-bit, and [ab]_i are 64-bit accumulators.
66 69 * Since the additions are done mod (2^64), errors in the high bits may not
67 70 * be noticed. For this reason, fletcher-2 is deprecated.
68 71 *
69 72 * For fletcher-4, the f_is are 32-bit, and [abcd]_i are 64-bit accumulators.
70 73 * A conservative estimate of how big the buffer can get before we overflow
71 74 * can be estimated using f_i = 0xffffffff for all i:
72 75 *
73 76 * % bc
74 77 * f=2^32-1;d=0; for (i = 1; d<2^64; i++) { d += f*i*(i+1)*(i+2)/6 }; (i-1)*4
75 78 * 2264
76 79 * quit
77 80 * %
78 81 *
79 82 * So blocks of up to 2k will not overflow. Our largest block size is
80 83 * 128k, which has 32k 4-byte words, so we can compute the largest possible
81 84 * accumulators, then divide by 2^64 to figure the max amount of overflow:
82 85 *
83 86 * % bc
84 87 * a=b=c=d=0; f=2^32-1; for (i=1; i<=32*1024; i++) { a+=f; b+=a; c+=b; d+=c }
85 88 * a/2^64;b/2^64;c/2^64;d/2^64
86 89 * 0
87 90 * 0
88 91 * 1365
89 92 * 11186858
90 93 * quit
91 94 * %
92 95 *
93 96 * So a and b cannot overflow. To make sure each bit of input has some
94 97 * effect on the contents of c and d, we can look at what the factors of
95 98 * the coefficients in the equations for c_n and d_n are. The number of 2s
96 99 * in the factors determines the lowest set bit in the multiplier. Running
97 100 * through the cases for n*(n+1)/2 reveals that the highest power of 2 is
98 101 * 2^14, and for n*(n+1)*(n+2)/6 it is 2^15. So while some data may overflow
99 102 * the 64-bit accumulators, every bit of every f_i effects every accumulator,
100 103 * even for 128k blocks.
101 104 *
102 105 * If we wanted to make a stronger version of fletcher4 (fletcher4c?),
103 106 * we could do our calculations mod (2^32 - 1) by adding in the carries
104 107 * periodically, and store the number of carries in the top 32-bits.
105 108 *
106 109 * --------------------
107 110 * Checksum Performance
108 111 * --------------------
109 112 *
110 113 * There are two interesting components to checksum performance: cached and
111 114 * uncached performance. With cached data, fletcher-2 is about four times
112 115 * faster than fletcher-4. With uncached data, the performance difference is
113 116 * negligible, since the cost of a cache fill dominates the processing time.
114 117 * Even though fletcher-4 is slower than fletcher-2, it is still a pretty
115 118 * efficient pass over the data.
116 119 *
117 120 * In normal operation, the data which is being checksummed is in a buffer
118 121 * which has been filled either by:
119 122 *
120 123 * 1. a compression step, which will be mostly cached, or
121 124 * 2. a bcopy() or copyin(), which will be uncached (because the
122 125 * copy is cache-bypassing).
123 126 *
↓ open down ↓ |
89 lines elided |
↑ open up ↑ |
124 127 * For both cached and uncached data, both fletcher checksums are much faster
125 128 * than sha-256, and slower than 'off', which doesn't touch the data at all.
126 129 */
127 130
128 131 #include <sys/types.h>
129 132 #include <sys/sysmacros.h>
130 133 #include <sys/byteorder.h>
131 134 #include <sys/zio.h>
132 135 #include <sys/spa.h>
133 136
137 +/*ARGSUSED*/
134 138 void
135 -fletcher_2_native(const void *buf, uint64_t size, zio_cksum_t *zcp)
139 +fletcher_2_native(const void *buf, uint64_t size, const zio_cksum_salt_t *salt,
140 + const void *ctx_template, zio_cksum_t *zcp)
136 141 {
137 142 const uint64_t *ip = buf;
138 143 const uint64_t *ipend = ip + (size / sizeof (uint64_t));
139 144 uint64_t a0, b0, a1, b1;
140 145
141 146 for (a0 = b0 = a1 = b1 = 0; ip < ipend; ip += 2) {
142 147 a0 += ip[0];
143 148 a1 += ip[1];
144 149 b0 += a0;
145 150 b1 += a1;
146 151 }
147 152
148 153 ZIO_SET_CHECKSUM(zcp, a0, a1, b0, b1);
149 154 }
150 155
156 +/*ARGSUSED*/
151 157 void
152 -fletcher_2_byteswap(const void *buf, uint64_t size, zio_cksum_t *zcp)
158 +fletcher_2_byteswap(const void *buf, uint64_t size,
159 + const zio_cksum_salt_t *salt, const void *ctx_template, zio_cksum_t *zcp)
153 160 {
154 161 const uint64_t *ip = buf;
155 162 const uint64_t *ipend = ip + (size / sizeof (uint64_t));
156 163 uint64_t a0, b0, a1, b1;
157 164
158 165 for (a0 = b0 = a1 = b1 = 0; ip < ipend; ip += 2) {
159 166 a0 += BSWAP_64(ip[0]);
160 167 a1 += BSWAP_64(ip[1]);
161 168 b0 += a0;
162 169 b1 += a1;
163 170 }
164 171
165 172 ZIO_SET_CHECKSUM(zcp, a0, a1, b0, b1);
166 173 }
167 174
175 +/*ARGSUSED*/
168 176 void
169 -fletcher_4_native(const void *buf, uint64_t size, zio_cksum_t *zcp)
177 +fletcher_4_native(const void *buf, uint64_t size, const zio_cksum_salt_t *salt,
178 + const void *ctx_template, zio_cksum_t *zcp)
170 179 {
171 180 const uint32_t *ip = buf;
172 181 const uint32_t *ipend = ip + (size / sizeof (uint32_t));
173 182 uint64_t a, b, c, d;
174 183
175 184 for (a = b = c = d = 0; ip < ipend; ip++) {
176 185 a += ip[0];
177 186 b += a;
178 187 c += b;
179 188 d += c;
180 189 }
181 190
182 191 ZIO_SET_CHECKSUM(zcp, a, b, c, d);
183 192 }
184 193
194 +/*ARGSUSED*/
185 195 void
186 -fletcher_4_byteswap(const void *buf, uint64_t size, zio_cksum_t *zcp)
196 +fletcher_4_byteswap(const void *buf, uint64_t size,
197 + const zio_cksum_salt_t *salt, const void *ctx_template, zio_cksum_t *zcp)
187 198 {
188 199 const uint32_t *ip = buf;
189 200 const uint32_t *ipend = ip + (size / sizeof (uint32_t));
190 201 uint64_t a, b, c, d;
191 202
192 203 for (a = b = c = d = 0; ip < ipend; ip++) {
193 204 a += BSWAP_32(ip[0]);
194 205 b += a;
195 206 c += b;
196 207 d += c;
197 208 }
198 209
199 210 ZIO_SET_CHECKSUM(zcp, a, b, c, d);
200 211 }
201 212
202 213 void
203 214 fletcher_4_incremental_native(const void *buf, uint64_t size,
204 215 zio_cksum_t *zcp)
205 216 {
206 217 const uint32_t *ip = buf;
207 218 const uint32_t *ipend = ip + (size / sizeof (uint32_t));
208 219 uint64_t a, b, c, d;
209 220
210 221 a = zcp->zc_word[0];
211 222 b = zcp->zc_word[1];
212 223 c = zcp->zc_word[2];
213 224 d = zcp->zc_word[3];
214 225
215 226 for (; ip < ipend; ip++) {
216 227 a += ip[0];
217 228 b += a;
218 229 c += b;
219 230 d += c;
220 231 }
221 232
222 233 ZIO_SET_CHECKSUM(zcp, a, b, c, d);
223 234 }
224 235
225 236 void
226 237 fletcher_4_incremental_byteswap(const void *buf, uint64_t size,
227 238 zio_cksum_t *zcp)
228 239 {
229 240 const uint32_t *ip = buf;
230 241 const uint32_t *ipend = ip + (size / sizeof (uint32_t));
231 242 uint64_t a, b, c, d;
232 243
233 244 a = zcp->zc_word[0];
234 245 b = zcp->zc_word[1];
235 246 c = zcp->zc_word[2];
236 247 d = zcp->zc_word[3];
237 248
238 249 for (; ip < ipend; ip++) {
239 250 a += BSWAP_32(ip[0]);
240 251 b += a;
241 252 c += b;
242 253 d += c;
243 254 }
244 255
245 256 ZIO_SET_CHECKSUM(zcp, a, b, c, d);
246 257 }
↓ open down ↓ |
50 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX