Rsync中使用了一种滚动检验(Rolling Checksum)算法,用于快速计算数据块的检验值。它是一种弱校验算法,采用的是Mark Adler的adler-32校验,它的定义如下:
a(k, l) = (∑Xi) mod M
b(k, l) = (∑(l - i +1)Xi) mod M
s(k, l) = a(k, l) + 216 b(k, l)
上面公式中,s(k, l)表示数据块Xk, ..., Xl的滚动校验值,为了简化以及计算速度考虑,M取值为216。这种校验计算公式具有一个非常关键的特性,那就是后续校验值可以通过递推关系高效地计算获得。
a(k+1, l+1) = (a(k, l) - Xk + Xl+1)) mod M
b(k+1, l+1) = (b(k, l) - (l - k +1)Xk + a(k+1, l+1)) mod M
s(k+1, l+1) = a(k+1, l+1) + 216 b(k+1, l+1)
因此,给定X1, ..., Xn的校验值,X1以及Xn+1,我们就可以快速地计算出X2, ..., Xn+1校验值。这样,利用这种性质我们就可以高效地计算数据块连续校验值,大幅减少checksum计算量。dedup util中,我在CDC和Sliding-block文件分块处理中使用了该校验值算法,性能得到大幅提升。
附Adler32_checksum算法实现:
-
/*
-
* a simple 32 bit checksum that can be upadted from either end
-
* (inspired by Mark Adler's Adler-32 checksum)
-
*/
-
unsigned int adler32_checksum(char *buf, int len)
-
{
-
int i;
-
unsigned int s1, s2;
-
s1 = s2 = 0;
-
for (i = 0; i < (len - 4); i += 4) {
-
s2 += 4 * (s1 + buf[i]) + 3 * buf[i+1] + 2 * buf[i+2] + buf[i+3] +
-
10 * CHAR_OFFSET;
-
s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4 * CHAR_OFFSET);
-
}
-
for (; i < len; i++) {
-
s1 += (buf[i]+CHAR_OFFSET);
-
s2 += s1;
-
}
-
return (s1 & 0xffff) + (s2 << 16);
-
}
-
/*
-
* adler32_checksum(X0, ..., Xn), X0, Xn+1 ----> adler32_checksum(X1, ..., Xn+1)
-
* where csum is adler32_checksum(X0, ..., Xn), c1 is X0, c2 is Xn+1
-
*/
-
unsigned int adler32_rolling_checksum(unsigned int csum, int len, char c1, char c2)
-
{
-
unsigned int s1, s2, s11, s22;
-
s1 = csum & 0xffff;
-
s2 = csum >> 16;
-
s1 -= (c1 - c2);
-
s2 -= (len * c1 - s1);
-
return (s1 & 0xffff) + (s2 << 16);
-
}
阅读(1110) | 评论(0) | 转发(0) |