2012-06-21 14:57:05 +00:00
|
|
|
/*
|
2012-07-19 16:11:07 +00:00
|
|
|
* The rabin polynomial computation is derived from:
|
|
|
|
* http://code.google.com/p/rabin-fingerprint-c/
|
2012-06-21 14:57:05 +00:00
|
|
|
*
|
2012-07-19 16:11:07 +00:00
|
|
|
* originally created by Joel Lawrence Tucci on 09-March-2011.
|
|
|
|
*
|
|
|
|
* Rabin polynomial portions Copyright (c) 2011 Joel Lawrence Tucci
|
2012-06-21 14:57:05 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* Neither the name of the project's author nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-06-21 15:10:43 +00:00
|
|
|
/*
|
|
|
|
* This file is a part of Pcompress, a chunked parallel multi-
|
|
|
|
* algorithm lossless compression and decompression program.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Moinak Ghosh. All rights reserved.
|
|
|
|
* Use is subject to license terms.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2012-07-07 16:48:29 +00:00
|
|
|
* version 3 of the License, or (at your option) any later version.
|
2012-06-21 15:10:43 +00:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* moinakg@belenix.org, http://moinakg.wordpress.com/
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-12-27 17:36:48 +00:00
|
|
|
#ifndef __STDC_FORMAT_MACROS
|
|
|
|
#define __STDC_FORMAT_MACROS 1
|
|
|
|
#endif
|
|
|
|
|
2012-06-21 14:57:05 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <allocator.h>
|
|
|
|
#include <utils.h>
|
2012-08-15 14:43:40 +00:00
|
|
|
#include <pthread.h>
|
2012-09-11 14:56:36 +00:00
|
|
|
#include <heapq.h>
|
2012-12-27 17:36:48 +00:00
|
|
|
#include <xxhash.h>
|
2012-06-21 14:57:05 +00:00
|
|
|
|
2012-09-02 15:10:32 +00:00
|
|
|
#include "rabin_dedup.h"
|
2012-06-21 14:57:05 +00:00
|
|
|
|
2012-09-26 14:17:32 +00:00
|
|
|
#define FORTY_PCNT(x) ((x)/5 << 1)
|
|
|
|
#define FIFTY_PCNT(x) ((x) >> 1)
|
2012-09-24 16:50:27 +00:00
|
|
|
#define SIXTY_PCNT(x) (((x) >> 1) + ((x) >> 3))
|
2012-09-11 14:56:36 +00:00
|
|
|
|
2012-12-09 04:45:06 +00:00
|
|
|
extern int lzma_init(void **data, int *level, int nthreads, int64_t chunksize,
|
2012-11-22 15:32:50 +00:00
|
|
|
int file_version, compress_op_t op);
|
2012-12-09 04:45:06 +00:00
|
|
|
extern int lzma_compress(void *src, uint64_t srclen, void *dst,
|
|
|
|
uint64_t *destlen, int level, uchar_t chdr, void *data);
|
|
|
|
extern int lzma_decompress(void *src, uint64_t srclen, void *dst,
|
|
|
|
uint64_t *dstlen, int level, uchar_t chdr, void *data);
|
2012-07-01 16:14:02 +00:00
|
|
|
extern int lzma_deinit(void **data);
|
2012-12-27 17:36:48 +00:00
|
|
|
extern int bsdiff(u_char *oldbuf, bsize_t oldsize, u_char *newbuf, bsize_t newsize,
|
2012-07-19 16:11:07 +00:00
|
|
|
u_char *diff, u_char *scratch, bsize_t scratchsize);
|
|
|
|
extern bsize_t get_bsdiff_sz(u_char *pbuf);
|
2012-12-27 17:36:48 +00:00
|
|
|
extern int bspatch(u_char *pbuf, u_char *oldbuf, bsize_t oldsize, u_char *newbuf,
|
2012-07-19 16:11:07 +00:00
|
|
|
bsize_t *_newsize);
|
2012-07-01 16:14:02 +00:00
|
|
|
|
2012-08-15 14:43:40 +00:00
|
|
|
static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
|
2012-09-28 18:39:49 +00:00
|
|
|
uint64_t ir[256], out[256];
|
2012-08-15 14:43:40 +00:00
|
|
|
static int inited = 0;
|
|
|
|
|
2012-08-23 17:28:44 +00:00
|
|
|
static uint32_t
|
2012-09-19 14:59:44 +00:00
|
|
|
dedupe_min_blksz(int rab_blk_sz)
|
2012-08-23 17:28:44 +00:00
|
|
|
{
|
|
|
|
uint32_t min_blk;
|
|
|
|
|
2012-11-08 14:11:33 +00:00
|
|
|
min_blk = (1 << (rab_blk_sz + RAB_BLK_MIN_BITS)) - 1024;
|
2012-08-23 17:28:44 +00:00
|
|
|
return (min_blk);
|
|
|
|
}
|
|
|
|
|
2012-07-27 16:33:24 +00:00
|
|
|
uint32_t
|
2012-09-16 05:42:58 +00:00
|
|
|
dedupe_buf_extra(uint64_t chunksize, int rab_blk_sz, const char *algo, int delta_flag)
|
2012-07-27 16:33:24 +00:00
|
|
|
{
|
2012-08-23 17:28:44 +00:00
|
|
|
if (rab_blk_sz < 1 || rab_blk_sz > 5)
|
|
|
|
rab_blk_sz = RAB_BLK_DEFAULT;
|
|
|
|
|
2012-09-19 14:59:44 +00:00
|
|
|
return ((chunksize / dedupe_min_blksz(rab_blk_sz)) * sizeof (uint32_t));
|
2012-07-27 16:33:24 +00:00
|
|
|
}
|
|
|
|
|
2012-06-21 14:57:05 +00:00
|
|
|
/*
|
2012-06-29 12:53:55 +00:00
|
|
|
* Initialize the algorithm with the default params.
|
2012-06-21 14:57:05 +00:00
|
|
|
*/
|
2012-09-16 05:42:58 +00:00
|
|
|
dedupe_context_t *
|
|
|
|
create_dedupe_context(uint64_t chunksize, uint64_t real_chunksize, int rab_blk_sz,
|
2012-11-22 15:32:50 +00:00
|
|
|
const char *algo, int delta_flag, int fixed_flag, int file_version, compress_op_t op) {
|
2012-09-16 05:42:58 +00:00
|
|
|
dedupe_context_t *ctx;
|
2012-08-17 05:33:02 +00:00
|
|
|
uint32_t i;
|
2012-06-29 12:53:55 +00:00
|
|
|
|
2012-08-23 17:28:44 +00:00
|
|
|
if (rab_blk_sz < 1 || rab_blk_sz > 5)
|
|
|
|
rab_blk_sz = RAB_BLK_DEFAULT;
|
|
|
|
|
2012-09-15 05:44:58 +00:00
|
|
|
if (fixed_flag) {
|
|
|
|
delta_flag = 0;
|
|
|
|
inited = 1;
|
|
|
|
}
|
|
|
|
|
2012-08-15 14:43:40 +00:00
|
|
|
/*
|
|
|
|
* Pre-compute a table of irreducible polynomial evaluations for each
|
|
|
|
* possible byte value.
|
|
|
|
*/
|
|
|
|
pthread_mutex_lock(&init_lock);
|
|
|
|
if (!inited) {
|
2012-10-01 18:50:44 +00:00
|
|
|
int term, pow, j;
|
2012-09-28 18:39:49 +00:00
|
|
|
uint64_t val, poly_pow;
|
|
|
|
|
|
|
|
poly_pow = 1;
|
|
|
|
for (j = 0; j < RAB_POLYNOMIAL_WIN_SIZE; j++) {
|
|
|
|
poly_pow = (poly_pow * RAB_POLYNOMIAL_CONST) & POLY_MASK;
|
|
|
|
}
|
2012-08-15 14:43:40 +00:00
|
|
|
|
2012-08-17 05:33:02 +00:00
|
|
|
for (j = 0; j < 256; j++) {
|
2012-08-15 14:43:40 +00:00
|
|
|
term = 1;
|
2012-10-01 18:50:44 +00:00
|
|
|
pow = 1;
|
2012-09-28 18:39:49 +00:00
|
|
|
val = 1;
|
|
|
|
out[j] = (j * poly_pow) & POLY_MASK;
|
2012-08-15 14:43:40 +00:00
|
|
|
for (i=0; i<RAB_POLYNOMIAL_WIN_SIZE; i++) {
|
|
|
|
if (term & FP_POLY) {
|
2012-10-01 18:50:44 +00:00
|
|
|
val += ((pow * j) & POLY_MASK);
|
2012-08-15 14:43:40 +00:00
|
|
|
}
|
2012-10-01 18:50:44 +00:00
|
|
|
pow = (pow * RAB_POLYNOMIAL_CONST) & POLY_MASK;
|
|
|
|
term <<= 1;
|
2012-08-15 14:43:40 +00:00
|
|
|
}
|
2012-08-17 05:33:02 +00:00
|
|
|
ir[j] = val;
|
2012-08-15 14:43:40 +00:00
|
|
|
}
|
|
|
|
inited = 1;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&init_lock);
|
|
|
|
|
2012-07-10 14:44:23 +00:00
|
|
|
/*
|
|
|
|
* Rabin window size must be power of 2 for optimization.
|
|
|
|
*/
|
|
|
|
if (!ISP2(RAB_POLYNOMIAL_WIN_SIZE)) {
|
|
|
|
fprintf(stderr, "Rabin window size must be a power of 2 in range 4 <= x <= 64\n");
|
|
|
|
return (NULL);
|
|
|
|
}
|
2012-07-19 16:11:07 +00:00
|
|
|
|
|
|
|
if (chunksize < RAB_MIN_CHUNK_SIZE) {
|
2012-10-21 15:33:07 +00:00
|
|
|
fprintf(stderr, "Minimum chunk size for Dedup must be %" PRIu64 " bytes\n",
|
2012-07-19 16:11:07 +00:00
|
|
|
RAB_MIN_CHUNK_SIZE);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2012-07-03 17:17:24 +00:00
|
|
|
/*
|
2012-07-19 16:11:07 +00:00
|
|
|
* For LZMA with chunksize <= LZMA Window size and/or Delta enabled we
|
|
|
|
* use 4K minimum Rabin block size. For everything else it is 2K based
|
|
|
|
* on experimentation.
|
2012-07-03 17:17:24 +00:00
|
|
|
*/
|
2012-09-16 05:42:58 +00:00
|
|
|
ctx = (dedupe_context_t *)slab_alloc(NULL, sizeof (dedupe_context_t));
|
2012-07-03 17:17:24 +00:00
|
|
|
ctx->rabin_poly_max_block_size = RAB_POLYNOMIAL_MAX_BLOCK_SIZE;
|
2012-07-23 16:13:12 +00:00
|
|
|
|
2012-11-24 18:23:07 +00:00
|
|
|
ctx->current_window_data = NULL;
|
2012-09-15 05:44:58 +00:00
|
|
|
ctx->fixed_flag = fixed_flag;
|
2012-08-08 17:10:58 +00:00
|
|
|
ctx->rabin_break_patt = 0;
|
2012-11-18 14:50:16 +00:00
|
|
|
ctx->rabin_poly_avg_block_size = RAB_BLK_AVG_SZ(rab_blk_sz);
|
2012-11-11 09:28:39 +00:00
|
|
|
ctx->rabin_avg_block_mask = RAB_BLK_MASK;
|
2012-09-19 14:59:44 +00:00
|
|
|
ctx->rabin_poly_min_block_size = dedupe_min_blksz(rab_blk_sz);
|
2012-09-26 14:17:32 +00:00
|
|
|
ctx->delta_flag = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scale down similarity percentage based on avg block size unless user specified
|
|
|
|
* argument '-EE' in which case fixed 40% match is used for Delta compression.
|
|
|
|
*/
|
|
|
|
if (delta_flag == DELTA_NORMAL) {
|
|
|
|
if (ctx->rabin_poly_avg_block_size < (1 << 14)) {
|
|
|
|
ctx->delta_flag = 1;
|
|
|
|
} else if (ctx->rabin_poly_avg_block_size < (1 << 16)) {
|
|
|
|
ctx->delta_flag = 2;
|
|
|
|
} else {
|
|
|
|
ctx->delta_flag = 3;
|
|
|
|
}
|
|
|
|
} else if (delta_flag == DELTA_EXTRA) {
|
|
|
|
ctx->delta_flag = 1;
|
|
|
|
}
|
2012-09-15 05:44:58 +00:00
|
|
|
|
|
|
|
if (!fixed_flag)
|
|
|
|
ctx->blknum = chunksize / ctx->rabin_poly_min_block_size;
|
|
|
|
else
|
|
|
|
ctx->blknum = chunksize / ctx->rabin_poly_avg_block_size;
|
2012-08-23 17:28:44 +00:00
|
|
|
|
2012-07-03 17:17:24 +00:00
|
|
|
if (chunksize % ctx->rabin_poly_min_block_size)
|
2012-08-17 05:33:02 +00:00
|
|
|
ctx->blknum++;
|
2012-06-21 14:57:05 +00:00
|
|
|
|
2012-08-17 05:33:02 +00:00
|
|
|
if (ctx->blknum > RABIN_MAX_BLOCKS) {
|
2012-07-02 16:38:03 +00:00
|
|
|
fprintf(stderr, "Chunk size too large for dedup.\n");
|
2012-09-16 05:42:58 +00:00
|
|
|
destroy_dedupe_context(ctx);
|
2012-07-02 16:38:03 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
2012-12-27 17:36:48 +00:00
|
|
|
ctx->current_window_data = (uchar_t *)slab_alloc(NULL, RAB_POLYNOMIAL_WIN_SIZE);
|
2012-07-08 16:14:08 +00:00
|
|
|
ctx->blocks = NULL;
|
|
|
|
if (real_chunksize > 0) {
|
2012-08-17 05:33:02 +00:00
|
|
|
ctx->blocks = (rabin_blockentry_t **)slab_calloc(NULL,
|
|
|
|
ctx->blknum, sizeof (rabin_blockentry_t *));
|
2012-07-08 16:14:08 +00:00
|
|
|
}
|
2012-11-22 15:32:50 +00:00
|
|
|
if(ctx == NULL || ctx->current_window_data == NULL ||
|
|
|
|
(ctx->blocks == NULL && real_chunksize > 0)) {
|
2012-06-21 14:57:05 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Could not allocate rabin polynomial context, out of memory\n");
|
2012-09-16 05:42:58 +00:00
|
|
|
destroy_dedupe_context(ctx);
|
2012-06-21 14:57:05 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2012-07-08 16:14:08 +00:00
|
|
|
ctx->lzma_data = NULL;
|
2012-07-19 16:11:07 +00:00
|
|
|
ctx->level = 14;
|
2012-07-08 16:14:08 +00:00
|
|
|
if (real_chunksize > 0) {
|
2012-11-22 15:32:50 +00:00
|
|
|
lzma_init(&(ctx->lzma_data), &(ctx->level), 1, chunksize, file_version, op);
|
|
|
|
|
|
|
|
// The lzma_data member is not needed during decompression
|
|
|
|
if (!(ctx->lzma_data) && op == COMPRESS) {
|
2012-07-08 16:14:08 +00:00
|
|
|
fprintf(stderr,
|
2012-09-15 05:44:58 +00:00
|
|
|
"Could not initialize LZMA data for dedupe index, out of memory\n");
|
2012-09-16 05:42:58 +00:00
|
|
|
destroy_dedupe_context(ctx);
|
2012-07-08 16:14:08 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
2012-07-01 16:14:02 +00:00
|
|
|
}
|
2012-06-21 14:57:05 +00:00
|
|
|
|
2012-09-05 17:13:54 +00:00
|
|
|
slab_cache_add(sizeof (rabin_blockentry_t));
|
2012-07-05 18:46:02 +00:00
|
|
|
ctx->real_chunksize = real_chunksize;
|
2012-09-16 05:42:58 +00:00
|
|
|
reset_dedupe_context(ctx);
|
2012-06-29 12:53:55 +00:00
|
|
|
return (ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-09-16 05:42:58 +00:00
|
|
|
reset_dedupe_context(dedupe_context_t *ctx)
|
2012-06-29 12:53:55 +00:00
|
|
|
{
|
|
|
|
memset(ctx->current_window_data, 0, RAB_POLYNOMIAL_WIN_SIZE);
|
2012-06-21 14:57:05 +00:00
|
|
|
ctx->window_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-09-16 05:42:58 +00:00
|
|
|
destroy_dedupe_context(dedupe_context_t *ctx)
|
2012-06-21 14:57:05 +00:00
|
|
|
{
|
2012-07-01 16:14:02 +00:00
|
|
|
if (ctx) {
|
2012-08-17 05:33:02 +00:00
|
|
|
uint32_t i;
|
2012-07-01 16:14:02 +00:00
|
|
|
if (ctx->current_window_data) slab_free(NULL, ctx->current_window_data);
|
2012-08-17 05:33:02 +00:00
|
|
|
if (ctx->blocks) {
|
|
|
|
for (i=0; i<ctx->blknum && ctx->blocks[i] != NULL; i++) {
|
|
|
|
slab_free(NULL, ctx->blocks[i]);
|
|
|
|
}
|
|
|
|
slab_free(NULL, ctx->blocks);
|
|
|
|
}
|
2012-07-01 16:14:02 +00:00
|
|
|
if (ctx->lzma_data) lzma_deinit(&(ctx->lzma_data));
|
|
|
|
slab_free(NULL, ctx);
|
|
|
|
}
|
2012-06-21 14:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-09-16 05:42:58 +00:00
|
|
|
* Perform Deduplication.
|
|
|
|
* Both Semi-Rabin fingerprinting based and Fixed Block Deduplication are supported.
|
|
|
|
* A 16-byte window is used for the rolling checksum and dedup blocks can vary in size
|
|
|
|
* from 4K-128K.
|
2012-06-21 14:57:05 +00:00
|
|
|
*/
|
2012-07-03 17:17:24 +00:00
|
|
|
uint32_t
|
2012-12-27 17:36:48 +00:00
|
|
|
dedupe_compress(dedupe_context_t *ctx, uchar_t *buf, uint64_t *size, uint64_t offset, uint64_t *rabin_pos)
|
2012-06-21 14:57:05 +00:00
|
|
|
{
|
2012-12-27 17:36:48 +00:00
|
|
|
uint64_t i, last_offset, j, ary_sz;
|
2012-07-03 17:17:24 +00:00
|
|
|
uint32_t blknum;
|
2012-12-27 17:36:48 +00:00
|
|
|
uchar_t *buf1 = (uchar_t *)buf;
|
2012-07-03 17:17:24 +00:00
|
|
|
uint32_t length;
|
2012-09-19 14:59:44 +00:00
|
|
|
uint64_t cur_roll_checksum, cur_pos_checksum;
|
2012-09-11 14:56:36 +00:00
|
|
|
uint32_t *fplist;
|
2012-09-23 09:27:09 +00:00
|
|
|
rabin_blockentry_t **htab;
|
2012-09-11 14:56:36 +00:00
|
|
|
heap_t heap;
|
2012-11-07 17:25:14 +00:00
|
|
|
DEBUG_STAT_EN(uint32_t max_count);
|
|
|
|
DEBUG_STAT_EN(max_count = 0);
|
2012-12-29 18:13:41 +00:00
|
|
|
DEBUG_STAT_EN(double strt, en_1, en);
|
2012-06-21 14:57:05 +00:00
|
|
|
|
2012-09-16 05:42:58 +00:00
|
|
|
length = offset;
|
|
|
|
last_offset = 0;
|
|
|
|
blknum = 0;
|
|
|
|
ctx->valid = 0;
|
|
|
|
cur_roll_checksum = 0;
|
2012-12-27 17:36:48 +00:00
|
|
|
if (*size < ctx->rabin_poly_avg_block_size) return (0);
|
2012-12-22 19:14:56 +00:00
|
|
|
DEBUG_STAT_EN(strt = get_wtime_millis());
|
2012-09-16 05:42:58 +00:00
|
|
|
|
|
|
|
if (ctx->fixed_flag) {
|
|
|
|
blknum = *size / ctx->rabin_poly_avg_block_size;
|
|
|
|
j = *size % ctx->rabin_poly_avg_block_size;
|
2012-10-01 18:50:44 +00:00
|
|
|
if (j)
|
|
|
|
blknum++;
|
|
|
|
else
|
|
|
|
j = ctx->rabin_poly_avg_block_size;
|
2012-09-16 05:42:58 +00:00
|
|
|
|
|
|
|
last_offset = 0;
|
|
|
|
length = ctx->rabin_poly_avg_block_size;
|
|
|
|
for (i=0; i<blknum; i++) {
|
|
|
|
if (i == blknum-1) {
|
|
|
|
length = j;
|
|
|
|
}
|
|
|
|
if (ctx->blocks[i] == 0) {
|
|
|
|
ctx->blocks[i] = (rabin_blockentry_t *)slab_alloc(NULL,
|
|
|
|
sizeof (rabin_blockentry_t));
|
|
|
|
}
|
|
|
|
ctx->blocks[i]->offset = last_offset;
|
|
|
|
ctx->blocks[i]->index = i; // Need to store for sorting
|
|
|
|
ctx->blocks[i]->length = length;
|
|
|
|
ctx->blocks[i]->similar = 0;
|
2012-12-31 06:23:47 +00:00
|
|
|
ctx->blocks[i]->hash = XXH32(buf1+last_offset, length, 0);
|
2012-09-23 09:27:09 +00:00
|
|
|
ctx->blocks[i]->similarity_hash = ctx->blocks[i]->hash;
|
2012-09-16 05:42:58 +00:00
|
|
|
last_offset += length;
|
|
|
|
}
|
|
|
|
goto process_blocks;
|
|
|
|
}
|
|
|
|
|
2012-07-19 16:11:07 +00:00
|
|
|
if (rabin_pos == NULL) {
|
|
|
|
/*
|
|
|
|
* Initialize arrays for sketch computation. We re-use memory allocated
|
|
|
|
* for the compressed chunk temporarily.
|
|
|
|
*/
|
2012-09-23 09:27:09 +00:00
|
|
|
ary_sz = 4 * ctx->rabin_poly_max_block_size;
|
|
|
|
fplist = (uint32_t *)(ctx->cbuf + ctx->real_chunksize - ary_sz);
|
2012-12-29 18:13:41 +00:00
|
|
|
if (ctx->delta_flag) memset(fplist, 0, ary_sz);
|
2012-07-19 16:11:07 +00:00
|
|
|
}
|
2012-09-19 14:59:44 +00:00
|
|
|
memset(ctx->current_window_data, 0, RAB_POLYNOMIAL_WIN_SIZE);
|
2012-06-21 14:57:05 +00:00
|
|
|
|
2012-07-08 16:14:08 +00:00
|
|
|
/*
|
|
|
|
* If rabin_pos is non-zero then we are being asked to scan for the last rabin boundary
|
|
|
|
* in the chunk. We start scanning at chunk end - max rabin block size. We avoid doing
|
|
|
|
* a full chunk scan.
|
2012-07-22 15:57:44 +00:00
|
|
|
*
|
|
|
|
* !!!NOTE!!!: Code duplication below for performance.
|
2012-07-08 16:14:08 +00:00
|
|
|
*/
|
|
|
|
if (rabin_pos) {
|
2012-08-17 05:33:02 +00:00
|
|
|
offset = *size - ctx->rabin_poly_max_block_size;
|
|
|
|
length = 0;
|
2012-07-22 15:57:44 +00:00
|
|
|
for (i=offset; i<*size; i++) {
|
2012-08-12 07:36:49 +00:00
|
|
|
uchar_t cur_byte = buf1[i];
|
2012-07-22 15:57:44 +00:00
|
|
|
uint64_t pushed_out = ctx->current_window_data[ctx->window_pos];
|
|
|
|
ctx->current_window_data[ctx->window_pos] = cur_byte;
|
2012-09-28 18:39:49 +00:00
|
|
|
|
|
|
|
cur_roll_checksum = (cur_roll_checksum * RAB_POLYNOMIAL_CONST) & POLY_MASK;
|
|
|
|
cur_roll_checksum += cur_byte;
|
|
|
|
cur_roll_checksum -= out[pushed_out];
|
2012-08-15 14:43:40 +00:00
|
|
|
cur_pos_checksum = cur_roll_checksum ^ ir[pushed_out];
|
2012-07-22 15:57:44 +00:00
|
|
|
|
|
|
|
ctx->window_pos = (ctx->window_pos + 1) & (RAB_POLYNOMIAL_WIN_SIZE-1);
|
|
|
|
length++;
|
|
|
|
if (length < ctx->rabin_poly_min_block_size) continue;
|
|
|
|
|
2012-08-17 05:33:02 +00:00
|
|
|
// If we hit our special value update block offset
|
|
|
|
if ((cur_pos_checksum & ctx->rabin_avg_block_mask) == ctx->rabin_break_patt) {
|
|
|
|
last_offset = i;
|
2012-07-22 15:57:44 +00:00
|
|
|
length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_offset < *size) {
|
|
|
|
*rabin_pos = last_offset;
|
|
|
|
}
|
|
|
|
return (0);
|
2012-07-08 16:14:08 +00:00
|
|
|
}
|
2012-08-08 17:10:58 +00:00
|
|
|
|
2012-09-11 14:56:36 +00:00
|
|
|
j = 0;
|
|
|
|
|
2012-06-29 12:53:55 +00:00
|
|
|
for (i=offset; i<*size; i++) {
|
2012-12-27 17:36:48 +00:00
|
|
|
uint64_t pc[4];
|
2012-08-08 17:10:58 +00:00
|
|
|
uchar_t cur_byte = buf1[i];
|
2012-06-21 14:57:05 +00:00
|
|
|
uint64_t pushed_out = ctx->current_window_data[ctx->window_pos];
|
|
|
|
ctx->current_window_data[ctx->window_pos] = cur_byte;
|
2012-09-28 18:39:49 +00:00
|
|
|
|
|
|
|
cur_roll_checksum = (cur_roll_checksum * RAB_POLYNOMIAL_CONST) & POLY_MASK;
|
|
|
|
cur_roll_checksum += cur_byte;
|
|
|
|
cur_roll_checksum -= out[pushed_out];
|
2012-08-15 14:43:40 +00:00
|
|
|
cur_pos_checksum = cur_roll_checksum ^ ir[pushed_out];
|
2012-06-21 14:57:05 +00:00
|
|
|
|
2012-07-19 16:11:07 +00:00
|
|
|
/*
|
2012-09-11 14:56:36 +00:00
|
|
|
* Retain a list of all fingerprints in the block. We then compute
|
|
|
|
* the K min values sketch from that list and generate a super sketch
|
|
|
|
* by hashing over the K min values sketch. We only store the least
|
|
|
|
* significant 32 bits of the fingerprint. This uses less memory,
|
|
|
|
* requires smaller memset() calls and generates a sufficiently large
|
|
|
|
* number of similarity matches without false positives - determined
|
|
|
|
* by experimentation.
|
2012-07-19 16:11:07 +00:00
|
|
|
*
|
2012-09-11 14:56:36 +00:00
|
|
|
* This is called minhashing and is used widely, for example in various
|
|
|
|
* search engines to detect similar documents.
|
2012-07-19 16:11:07 +00:00
|
|
|
*/
|
2012-12-29 18:13:41 +00:00
|
|
|
if (ctx->delta_flag) {
|
|
|
|
fplist[j] = cur_pos_checksum & 0xFFFFFFFFUL;
|
|
|
|
j++;
|
|
|
|
}
|
2012-08-12 07:36:49 +00:00
|
|
|
|
2012-07-13 16:36:55 +00:00
|
|
|
/*
|
|
|
|
* Window pos has to rotate from 0 .. RAB_POLYNOMIAL_WIN_SIZE-1
|
|
|
|
* We avoid a branch here by masking. This requires RAB_POLYNOMIAL_WIN_SIZE
|
|
|
|
* to be power of 2
|
|
|
|
*/
|
2012-07-10 14:44:23 +00:00
|
|
|
ctx->window_pos = (ctx->window_pos + 1) & (RAB_POLYNOMIAL_WIN_SIZE-1);
|
|
|
|
length++;
|
2012-07-03 17:17:24 +00:00
|
|
|
if (length < ctx->rabin_poly_min_block_size) continue;
|
2012-06-29 12:53:55 +00:00
|
|
|
|
2012-06-21 15:10:43 +00:00
|
|
|
// If we hit our special value or reached the max block size update block offset
|
2012-08-15 14:43:40 +00:00
|
|
|
if ((cur_pos_checksum & ctx->rabin_avg_block_mask) == ctx->rabin_break_patt ||
|
2012-08-17 05:33:02 +00:00
|
|
|
length >= ctx->rabin_poly_max_block_size) {
|
|
|
|
if (ctx->blocks[blknum] == 0)
|
2012-09-15 05:44:58 +00:00
|
|
|
ctx->blocks[blknum] = (rabin_blockentry_t *)slab_alloc(NULL,
|
|
|
|
sizeof (rabin_blockentry_t));
|
2012-08-17 05:33:02 +00:00
|
|
|
ctx->blocks[blknum]->offset = last_offset;
|
|
|
|
ctx->blocks[blknum]->index = blknum; // Need to store for sorting
|
|
|
|
ctx->blocks[blknum]->length = length;
|
2012-08-12 07:36:49 +00:00
|
|
|
|
2012-11-07 17:25:14 +00:00
|
|
|
DEBUG_STAT_EN(if (length >= ctx->rabin_poly_max_block_size) max_count++);
|
2012-09-24 16:50:27 +00:00
|
|
|
/*
|
|
|
|
* Reset the heap structure and find the K min values if Delta Compression
|
|
|
|
* is enabled. We use a min heap mechanism taken from the heap based priority
|
|
|
|
* queue implementation in Python.
|
|
|
|
* Here K = 60% or 40%. We are aiming to detect either 60% (default) or 40%
|
|
|
|
* similarity on average.
|
|
|
|
*/
|
2012-09-05 17:13:54 +00:00
|
|
|
if (ctx->delta_flag) {
|
2012-09-24 16:50:27 +00:00
|
|
|
pc[1] = SIXTY_PCNT(j);
|
2012-09-26 14:17:32 +00:00
|
|
|
pc[2] = FIFTY_PCNT(j);
|
|
|
|
pc[3] = FORTY_PCNT(j);
|
2012-09-24 16:50:27 +00:00
|
|
|
|
|
|
|
reset_heap(&heap, pc[ctx->delta_flag]);
|
2012-12-27 17:36:48 +00:00
|
|
|
ksmallest((int32_t *)fplist, j, &heap);
|
2012-09-23 09:27:09 +00:00
|
|
|
ctx->blocks[blknum]->similarity_hash =
|
2012-12-31 06:23:47 +00:00
|
|
|
XXH32((const uchar_t *)fplist, pc[ctx->delta_flag]*4, 0);
|
2012-09-23 09:27:09 +00:00
|
|
|
memset(fplist, 0, ary_sz);
|
2012-09-05 17:13:54 +00:00
|
|
|
}
|
2012-07-22 15:57:44 +00:00
|
|
|
blknum++;
|
2012-06-21 14:57:05 +00:00
|
|
|
last_offset = i+1;
|
|
|
|
length = 0;
|
2012-07-13 16:36:55 +00:00
|
|
|
j = 0;
|
2012-06-21 14:57:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-23 09:27:09 +00:00
|
|
|
// Insert the last left-over trailing bytes, if any, into a block.
|
|
|
|
if (last_offset < *size) {
|
|
|
|
if (ctx->blocks[blknum] == 0)
|
|
|
|
ctx->blocks[blknum] = (rabin_blockentry_t *)slab_alloc(NULL,
|
|
|
|
sizeof (rabin_blockentry_t));
|
|
|
|
ctx->blocks[blknum]->offset = last_offset;
|
|
|
|
ctx->blocks[blknum]->index = blknum;
|
|
|
|
ctx->blocks[blknum]->length = *size - last_offset;
|
|
|
|
|
|
|
|
if (ctx->delta_flag) {
|
|
|
|
uint64_t cur_sketch;
|
2012-12-27 17:36:48 +00:00
|
|
|
uint64_t pc[3];
|
2012-09-24 16:50:27 +00:00
|
|
|
|
2012-09-23 09:27:09 +00:00
|
|
|
if (j > 1) {
|
2012-09-24 16:50:27 +00:00
|
|
|
pc[1] = SIXTY_PCNT(j);
|
2012-09-26 14:17:32 +00:00
|
|
|
pc[2] = FIFTY_PCNT(j);
|
|
|
|
pc[3] = FORTY_PCNT(j);
|
2012-09-24 16:50:27 +00:00
|
|
|
reset_heap(&heap, pc[ctx->delta_flag]);
|
2012-12-27 17:36:48 +00:00
|
|
|
ksmallest((int32_t *)fplist, j, &heap);
|
2012-09-23 09:27:09 +00:00
|
|
|
cur_sketch =
|
2012-12-31 06:23:47 +00:00
|
|
|
XXH32((const uchar_t *)fplist, pc[ctx->delta_flag]*4, 0);
|
2012-09-23 09:27:09 +00:00
|
|
|
} else {
|
2012-09-24 16:50:27 +00:00
|
|
|
if (j == 0) j = 1;
|
2012-09-23 09:27:09 +00:00
|
|
|
cur_sketch =
|
2012-12-31 06:23:47 +00:00
|
|
|
XXH32((const uchar_t *)fplist, (j*4)/2, 0);
|
2012-09-23 09:27:09 +00:00
|
|
|
}
|
|
|
|
ctx->blocks[blknum]->similarity_hash = cur_sketch;
|
2012-09-19 14:59:44 +00:00
|
|
|
}
|
2012-09-23 09:27:09 +00:00
|
|
|
blknum++;
|
|
|
|
last_offset = *size;
|
2012-09-19 14:59:44 +00:00
|
|
|
}
|
|
|
|
|
2012-09-16 05:42:58 +00:00
|
|
|
process_blocks:
|
2012-06-29 12:53:55 +00:00
|
|
|
// If we found at least a few chunks, perform dedup.
|
2012-12-29 18:13:41 +00:00
|
|
|
DEBUG_STAT_EN(en_1 = get_wtime_millis());
|
2012-12-22 19:14:56 +00:00
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "Original size: %" PRId64 ", blknum: %u\n", *size, blknum));
|
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "Number of maxlen blocks: %u\n", max_count));
|
2012-06-29 12:53:55 +00:00
|
|
|
if (blknum > 2) {
|
2012-12-27 17:36:48 +00:00
|
|
|
uint64_t pos, matchlen, pos1;
|
2012-06-29 12:53:55 +00:00
|
|
|
int valid = 1;
|
2012-09-23 09:27:09 +00:00
|
|
|
uint32_t *dedupe_index;
|
2012-12-27 17:36:48 +00:00
|
|
|
uint64_t dedupe_index_sz;
|
2012-09-23 09:27:09 +00:00
|
|
|
rabin_blockentry_t *be;
|
|
|
|
DEBUG_STAT_EN(uint32_t delta_calls, delta_fails, merge_count, hash_collisions);
|
2012-08-15 14:43:40 +00:00
|
|
|
DEBUG_STAT_EN(delta_calls = 0);
|
|
|
|
DEBUG_STAT_EN(delta_fails = 0);
|
2012-09-23 09:27:09 +00:00
|
|
|
DEBUG_STAT_EN(hash_collisions = 0);
|
2012-06-29 12:53:55 +00:00
|
|
|
|
2012-09-23 09:27:09 +00:00
|
|
|
ary_sz = blknum * sizeof (rabin_blockentry_t *);
|
|
|
|
htab = (rabin_blockentry_t **)(ctx->cbuf + ctx->real_chunksize - ary_sz);
|
|
|
|
memset(htab, 0, ary_sz);
|
2012-06-29 12:53:55 +00:00
|
|
|
|
|
|
|
/*
|
2012-09-23 09:27:09 +00:00
|
|
|
* Compute hash signature for each block. We do this in a separate loop to
|
|
|
|
* have a fast linear scan through the buffer.
|
2012-06-29 12:53:55 +00:00
|
|
|
*/
|
2012-09-23 09:27:09 +00:00
|
|
|
if (ctx->delta_flag) {
|
|
|
|
for (i=0; i<blknum; i++) {
|
2012-12-31 06:23:47 +00:00
|
|
|
ctx->blocks[i]->hash = XXH32(buf1+ctx->blocks[i]->offset,
|
2012-09-23 09:27:09 +00:00
|
|
|
ctx->blocks[i]->length, 0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i=0; i<blknum; i++) {
|
2012-12-31 06:23:47 +00:00
|
|
|
ctx->blocks[i]->hash = XXH32(buf1+ctx->blocks[i]->offset,
|
2012-09-23 09:27:09 +00:00
|
|
|
ctx->blocks[i]->length, 0);
|
|
|
|
ctx->blocks[i]->similarity_hash = ctx->blocks[i]->hash;
|
|
|
|
}
|
|
|
|
}
|
2012-07-05 18:46:02 +00:00
|
|
|
|
|
|
|
/*
|
2012-09-23 09:27:09 +00:00
|
|
|
* Perform hash-matching of blocks and use a bucket-chained hashtable to match
|
|
|
|
* for duplicates and similar blocks. Unique blocks are inserted and duplicates
|
|
|
|
* and similar ones are marked in the block array.
|
|
|
|
*
|
|
|
|
* Hashtable memory is not allocated. We just use available space in the
|
|
|
|
* target buffer.
|
2012-07-05 18:46:02 +00:00
|
|
|
*/
|
2012-07-03 17:17:24 +00:00
|
|
|
matchlen = 0;
|
2012-09-23 09:27:09 +00:00
|
|
|
for (i=0; i<blknum; i++) {
|
|
|
|
uint64_t ck;
|
2012-06-29 12:53:55 +00:00
|
|
|
|
2012-09-23 09:27:09 +00:00
|
|
|
/*
|
2012-09-24 16:50:27 +00:00
|
|
|
* Bias hash with length for fewer collisions. If Delta Compression is
|
2012-09-23 09:27:09 +00:00
|
|
|
* not enabled then value of similarity_hash == hash.
|
|
|
|
*/
|
|
|
|
ck = ctx->blocks[i]->similarity_hash;
|
2012-09-24 16:50:27 +00:00
|
|
|
ck += (ck / ctx->blocks[i]->length);
|
2012-09-23 09:27:09 +00:00
|
|
|
j = ck % blknum;
|
2012-07-19 16:11:07 +00:00
|
|
|
|
2012-09-23 09:27:09 +00:00
|
|
|
if (htab[j] == 0) {
|
2012-09-24 16:50:27 +00:00
|
|
|
/*
|
|
|
|
* Hash bucket empty. So add block into table.
|
|
|
|
*/
|
|
|
|
htab[j] = ctx->blocks[i];
|
|
|
|
ctx->blocks[i]->other = 0;
|
|
|
|
ctx->blocks[i]->next = 0;
|
|
|
|
ctx->blocks[i]->similar = 0;
|
2012-09-23 09:27:09 +00:00
|
|
|
} else {
|
|
|
|
be = htab[j];
|
|
|
|
length = 0;
|
2012-07-19 16:11:07 +00:00
|
|
|
|
2012-08-09 18:27:24 +00:00
|
|
|
/*
|
2012-09-23 09:27:09 +00:00
|
|
|
* Look for exact duplicates. Same cksum, length and memcmp()\
|
2012-08-09 18:27:24 +00:00
|
|
|
*/
|
2012-09-23 09:27:09 +00:00
|
|
|
while (1) {
|
|
|
|
if (be->hash == ctx->blocks[i]->hash &&
|
|
|
|
be->length == ctx->blocks[i]->length &&
|
|
|
|
memcmp(buf1 + be->offset, buf1 + ctx->blocks[i]->offset,
|
|
|
|
be->length) == 0) {
|
|
|
|
ctx->blocks[i]->similar = SIMILAR_EXACT;
|
|
|
|
ctx->blocks[i]->other = be;
|
|
|
|
be->similar = SIMILAR_REF;
|
|
|
|
matchlen += be->length;
|
|
|
|
length = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (be->next)
|
|
|
|
be = be->next;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-12-29 18:13:41 +00:00
|
|
|
if (ctx->delta_flag && !length) {
|
2012-09-23 09:27:09 +00:00
|
|
|
/*
|
|
|
|
* Look for similar blocks.
|
|
|
|
*/
|
|
|
|
be = htab[j];
|
|
|
|
while (1) {
|
|
|
|
if (be->similarity_hash == ctx->blocks[i]->similarity_hash &&
|
|
|
|
be->length == ctx->blocks[i]->length) {
|
|
|
|
ctx->blocks[i]->similar = SIMILAR_PARTIAL;
|
|
|
|
ctx->blocks[i]->other = be;
|
|
|
|
be->similar = SIMILAR_REF;
|
|
|
|
matchlen += (be->length>>1);
|
|
|
|
length = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (be->next)
|
|
|
|
be = be->next;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-09-24 16:50:27 +00:00
|
|
|
/*
|
|
|
|
* No duplicate in table for this block. So add it to
|
|
|
|
* the bucket chain.
|
|
|
|
*/
|
2012-09-23 09:27:09 +00:00
|
|
|
if (!length) {
|
2012-09-24 16:50:27 +00:00
|
|
|
ctx->blocks[i]->other = 0;
|
|
|
|
ctx->blocks[i]->next = 0;
|
|
|
|
ctx->blocks[i]->similar = 0;
|
2012-09-23 09:27:09 +00:00
|
|
|
be->next = ctx->blocks[i];
|
|
|
|
DEBUG_STAT_EN(hash_collisions++);
|
2012-07-19 16:11:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-22 19:14:56 +00:00
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "Total Hashtable bucket collisions: %u\n", hash_collisions));
|
2012-09-23 09:27:09 +00:00
|
|
|
|
2012-12-27 17:36:48 +00:00
|
|
|
dedupe_index_sz = (uint64_t)blknum * RABIN_ENTRY_SIZE;
|
2012-09-15 05:44:58 +00:00
|
|
|
if (matchlen < dedupe_index_sz) {
|
2012-12-31 06:23:47 +00:00
|
|
|
DEBUG_STAT_EN(en = get_wtime_millis());
|
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "Chunking speed %.3f MB/s, Overall Dedupe speed %.3f MB/s\n",
|
|
|
|
get_mb_s(*size, strt, en_1), get_mb_s(*size, strt, en)));
|
2012-12-22 19:14:56 +00:00
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "No Dedupe possible.\n"));
|
2012-06-29 12:53:55 +00:00
|
|
|
ctx->valid = 0;
|
2012-12-27 17:36:48 +00:00
|
|
|
return (0);
|
2012-06-29 12:53:55 +00:00
|
|
|
}
|
2012-07-19 16:11:07 +00:00
|
|
|
|
2012-09-23 09:27:09 +00:00
|
|
|
dedupe_index = (uint32_t *)(ctx->cbuf + RABIN_HDR_SIZE);
|
2012-07-05 18:46:02 +00:00
|
|
|
pos = 0;
|
2012-09-23 09:27:09 +00:00
|
|
|
DEBUG_STAT_EN(merge_count = 0);
|
2012-06-29 12:53:55 +00:00
|
|
|
|
2012-09-23 09:27:09 +00:00
|
|
|
/*
|
|
|
|
* Merge runs of unique blocks into a single block entry to reduce
|
|
|
|
* dedupe index size.
|
|
|
|
*/
|
|
|
|
for (i=0; i<blknum;) {
|
|
|
|
dedupe_index[pos] = i;
|
|
|
|
ctx->blocks[i]->index = pos;
|
|
|
|
pos++;
|
|
|
|
length = 0;
|
|
|
|
j = i;
|
|
|
|
if (ctx->blocks[i]->similar == 0) {
|
|
|
|
while (i< blknum && ctx->blocks[i]->similar == 0 &&
|
|
|
|
length < RABIN_MAX_BLOCK_SIZE) {
|
|
|
|
length += ctx->blocks[i]->length;
|
|
|
|
i++;
|
|
|
|
DEBUG_STAT_EN(merge_count++);
|
2012-06-29 18:15:06 +00:00
|
|
|
}
|
2012-09-23 09:27:09 +00:00
|
|
|
ctx->blocks[j]->length = length;
|
2012-06-29 12:53:55 +00:00
|
|
|
} else {
|
2012-09-23 09:27:09 +00:00
|
|
|
i++;
|
2012-07-05 18:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-22 19:14:56 +00:00
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "Merge count: %u\n", merge_count));
|
2012-07-05 18:46:02 +00:00
|
|
|
|
|
|
|
/*
|
2012-09-23 09:27:09 +00:00
|
|
|
* Final pass update dedupe index and copy data.
|
2012-07-05 18:46:02 +00:00
|
|
|
*/
|
|
|
|
blknum = pos;
|
2012-12-27 17:36:48 +00:00
|
|
|
dedupe_index_sz = (uint64_t)blknum * RABIN_ENTRY_SIZE;
|
2012-09-15 05:44:58 +00:00
|
|
|
pos1 = dedupe_index_sz + RABIN_HDR_SIZE;
|
2012-09-23 09:27:09 +00:00
|
|
|
matchlen = ctx->real_chunksize - *size;
|
|
|
|
for (i=0; i<blknum; i++) {
|
|
|
|
be = ctx->blocks[dedupe_index[i]];
|
|
|
|
if (be->similar == 0 || be->similar == SIMILAR_REF) {
|
|
|
|
/* Just copy. */
|
|
|
|
dedupe_index[i] = htonl(be->length);
|
|
|
|
memcpy(ctx->cbuf + pos1, buf1 + be->offset, be->length);
|
|
|
|
pos1 += be->length;
|
|
|
|
} else {
|
|
|
|
if (be->similar == SIMILAR_EXACT) {
|
|
|
|
dedupe_index[i] = htonl((be->other->index | RABIN_INDEX_FLAG) &
|
|
|
|
CLEAR_SIMILARITY_FLAG);
|
|
|
|
} else {
|
2012-12-27 17:36:48 +00:00
|
|
|
uchar_t *oldbuf, *newbuf;
|
2012-09-23 09:27:09 +00:00
|
|
|
int32_t bsz;
|
|
|
|
/*
|
|
|
|
* Perform bsdiff.
|
|
|
|
*/
|
2012-12-27 17:36:48 +00:00
|
|
|
oldbuf = buf1 + be->other->offset;
|
|
|
|
newbuf = buf1 + be->offset;
|
2012-08-15 14:43:40 +00:00
|
|
|
DEBUG_STAT_EN(delta_calls++);
|
2012-07-20 15:23:46 +00:00
|
|
|
|
2012-12-27 17:36:48 +00:00
|
|
|
bsz = bsdiff(oldbuf, be->other->length, newbuf, be->length,
|
2012-09-23 09:27:09 +00:00
|
|
|
ctx->cbuf + pos1, buf1 + *size, matchlen);
|
2012-07-19 16:11:07 +00:00
|
|
|
if (bsz == 0) {
|
2012-08-15 14:43:40 +00:00
|
|
|
DEBUG_STAT_EN(delta_fails++);
|
2012-12-27 17:36:48 +00:00
|
|
|
memcpy(ctx->cbuf + pos1, newbuf, be->length);
|
2012-09-23 09:27:09 +00:00
|
|
|
dedupe_index[i] = htonl(be->length);
|
|
|
|
pos1 += be->length;
|
2012-07-19 16:11:07 +00:00
|
|
|
} else {
|
2012-09-23 09:27:09 +00:00
|
|
|
dedupe_index[i] = htonl(be->other->index |
|
2012-07-19 16:11:07 +00:00
|
|
|
RABIN_INDEX_FLAG | SET_SIMILARITY_FLAG);
|
|
|
|
pos1 += bsz;
|
|
|
|
}
|
2012-07-05 18:46:02 +00:00
|
|
|
}
|
2012-06-29 12:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-27 17:36:48 +00:00
|
|
|
|
2012-06-29 12:53:55 +00:00
|
|
|
if (valid) {
|
2012-07-01 16:14:02 +00:00
|
|
|
uchar_t *cbuf = ctx->cbuf;
|
2012-12-27 17:36:48 +00:00
|
|
|
uint64_t *entries;
|
|
|
|
DEBUG_STAT_EN(uint64_t sz);
|
2012-07-01 16:14:02 +00:00
|
|
|
|
2012-12-22 19:14:56 +00:00
|
|
|
DEBUG_STAT_EN(sz = *size);
|
2012-07-03 17:17:24 +00:00
|
|
|
*((uint32_t *)cbuf) = htonl(blknum);
|
|
|
|
cbuf += sizeof (uint32_t);
|
2012-12-27 17:36:48 +00:00
|
|
|
entries = (uint64_t *)cbuf;
|
2012-07-01 16:14:02 +00:00
|
|
|
entries[0] = htonll(*size);
|
|
|
|
entries[1] = 0;
|
2012-09-15 05:44:58 +00:00
|
|
|
entries[2] = htonll(pos1 - dedupe_index_sz - RABIN_HDR_SIZE);
|
2012-07-05 18:46:02 +00:00
|
|
|
*size = pos1;
|
2012-06-29 12:53:55 +00:00
|
|
|
ctx->valid = 1;
|
2012-12-22 19:14:56 +00:00
|
|
|
DEBUG_STAT_EN(en = get_wtime_millis());
|
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "Deduped size: %" PRId64 ", blknum: %u, delta_calls: %u, delta_fails: %u\n",
|
2012-08-15 14:43:40 +00:00
|
|
|
*size, blknum, delta_calls, delta_fails));
|
2012-12-29 18:13:41 +00:00
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "Chunking speed %.3f MB/s, Overall Dedupe speed %.3f MB/s\n",
|
|
|
|
get_mb_s(sz, strt, en_1), get_mb_s(sz, strt, en)));
|
2012-07-01 16:14:02 +00:00
|
|
|
/*
|
|
|
|
* Remaining header entries: size of compressed index and size of
|
|
|
|
* compressed data are inserted later via rabin_update_hdr, after actual compression!
|
|
|
|
*/
|
2012-09-15 05:44:58 +00:00
|
|
|
return (dedupe_index_sz);
|
2012-06-29 12:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-01 16:14:02 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-27 17:36:48 +00:00
|
|
|
update_dedupe_hdr(uchar_t *buf, uint64_t dedupe_index_sz_cmp, uint64_t dedupe_data_sz_cmp)
|
2012-07-01 16:14:02 +00:00
|
|
|
{
|
2012-12-27 17:36:48 +00:00
|
|
|
uint64_t *entries;
|
2012-07-01 16:14:02 +00:00
|
|
|
|
2012-07-03 17:17:24 +00:00
|
|
|
buf += sizeof (uint32_t);
|
2012-12-27 17:36:48 +00:00
|
|
|
entries = (uint64_t *)buf;
|
2012-09-15 05:44:58 +00:00
|
|
|
entries[1] = htonll(dedupe_index_sz_cmp);
|
2012-09-27 16:59:08 +00:00
|
|
|
entries[3] = htonll(dedupe_data_sz_cmp);
|
2012-07-01 16:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-27 17:36:48 +00:00
|
|
|
parse_dedupe_hdr(uchar_t *buf, uint32_t *blknum, uint64_t *dedupe_index_sz,
|
|
|
|
uint64_t *dedupe_data_sz, uint64_t *dedupe_index_sz_cmp,
|
|
|
|
uint64_t *dedupe_data_sz_cmp, uint64_t *deduped_size)
|
2012-07-01 16:14:02 +00:00
|
|
|
{
|
2012-12-27 17:36:48 +00:00
|
|
|
uint64_t *entries;
|
2012-07-01 16:14:02 +00:00
|
|
|
|
2012-07-03 17:17:24 +00:00
|
|
|
*blknum = ntohl(*((uint32_t *)(buf)));
|
|
|
|
buf += sizeof (uint32_t);
|
2012-07-01 16:14:02 +00:00
|
|
|
|
2012-12-27 17:36:48 +00:00
|
|
|
entries = (uint64_t *)buf;
|
2012-09-27 16:59:08 +00:00
|
|
|
*dedupe_data_sz = ntohll(entries[0]);
|
2012-12-27 17:36:48 +00:00
|
|
|
*dedupe_index_sz = (uint64_t)(*blknum) * RABIN_ENTRY_SIZE;
|
2012-09-15 05:44:58 +00:00
|
|
|
*dedupe_index_sz_cmp = ntohll(entries[1]);
|
2012-09-27 16:59:08 +00:00
|
|
|
*deduped_size = ntohll(entries[2]);
|
|
|
|
*dedupe_data_sz_cmp = ntohll(entries[3]);
|
2012-06-29 12:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-27 17:36:48 +00:00
|
|
|
dedupe_decompress(dedupe_context_t *ctx, uchar_t *buf, uint64_t *size)
|
2012-06-29 12:53:55 +00:00
|
|
|
{
|
2012-07-03 17:17:24 +00:00
|
|
|
uint32_t blknum, blk, oblk, len;
|
2012-09-15 05:44:58 +00:00
|
|
|
uint32_t *dedupe_index;
|
2012-12-27 17:36:48 +00:00
|
|
|
uint64_t data_sz, sz, indx_cmp, data_sz_cmp, deduped_sz;
|
|
|
|
uint64_t dedupe_index_sz, pos1;
|
2012-06-29 12:53:55 +00:00
|
|
|
uchar_t *pos2;
|
|
|
|
|
2012-09-16 05:42:58 +00:00
|
|
|
parse_dedupe_hdr(buf, &blknum, &dedupe_index_sz, &data_sz, &indx_cmp, &data_sz_cmp, &deduped_sz);
|
2012-09-15 05:44:58 +00:00
|
|
|
dedupe_index = (uint32_t *)(buf + RABIN_HDR_SIZE);
|
|
|
|
pos1 = dedupe_index_sz + RABIN_HDR_SIZE;
|
2012-06-29 12:53:55 +00:00
|
|
|
pos2 = ctx->cbuf;
|
|
|
|
sz = 0;
|
|
|
|
ctx->valid = 1;
|
|
|
|
|
2012-09-05 17:13:54 +00:00
|
|
|
slab_cache_add(sizeof (rabin_blockentry_t));
|
2012-06-29 12:53:55 +00:00
|
|
|
for (blk = 0; blk < blknum; blk++) {
|
2012-08-17 05:33:02 +00:00
|
|
|
if (ctx->blocks[blk] == 0)
|
|
|
|
ctx->blocks[blk] = (rabin_blockentry_t *)slab_alloc(NULL, sizeof (rabin_blockentry_t));
|
2012-09-15 05:44:58 +00:00
|
|
|
len = ntohl(dedupe_index[blk]);
|
2012-09-23 09:27:09 +00:00
|
|
|
ctx->blocks[blk]->hash = 0;
|
2012-06-29 18:15:06 +00:00
|
|
|
if (len == 0) {
|
2012-09-23 09:27:09 +00:00
|
|
|
ctx->blocks[blk]->hash = 1;
|
2012-06-29 18:15:06 +00:00
|
|
|
|
2012-07-02 16:38:03 +00:00
|
|
|
} else if (!(len & RABIN_INDEX_FLAG)) {
|
2012-08-17 05:33:02 +00:00
|
|
|
ctx->blocks[blk]->length = len;
|
|
|
|
ctx->blocks[blk]->offset = pos1;
|
2012-06-29 12:53:55 +00:00
|
|
|
pos1 += len;
|
|
|
|
} else {
|
2012-07-19 16:11:07 +00:00
|
|
|
bsize_t blen;
|
|
|
|
|
2012-08-17 05:33:02 +00:00
|
|
|
ctx->blocks[blk]->length = 0;
|
2012-07-19 16:11:07 +00:00
|
|
|
if (len & GET_SIMILARITY_FLAG) {
|
2012-08-17 05:33:02 +00:00
|
|
|
ctx->blocks[blk]->offset = pos1;
|
|
|
|
ctx->blocks[blk]->index = (len & RABIN_INDEX_VALUE) | SET_SIMILARITY_FLAG;
|
2012-07-19 16:11:07 +00:00
|
|
|
blen = get_bsdiff_sz(buf + pos1);
|
|
|
|
pos1 += blen;
|
|
|
|
} else {
|
2012-08-17 05:33:02 +00:00
|
|
|
ctx->blocks[blk]->index = len & RABIN_INDEX_VALUE;
|
2012-07-19 16:11:07 +00:00
|
|
|
}
|
2012-06-29 12:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-19 16:11:07 +00:00
|
|
|
|
2012-06-29 12:53:55 +00:00
|
|
|
for (blk = 0; blk < blknum; blk++) {
|
2012-07-19 16:11:07 +00:00
|
|
|
int rv;
|
|
|
|
bsize_t newsz;
|
|
|
|
|
2012-09-23 09:27:09 +00:00
|
|
|
if (ctx->blocks[blk]->hash == 1) continue;
|
2012-08-17 05:33:02 +00:00
|
|
|
if (ctx->blocks[blk]->length > 0) {
|
|
|
|
len = ctx->blocks[blk]->length;
|
|
|
|
pos1 = ctx->blocks[blk]->offset;
|
2012-06-29 12:53:55 +00:00
|
|
|
} else {
|
2012-08-17 05:33:02 +00:00
|
|
|
oblk = ctx->blocks[blk]->index;
|
2012-07-19 16:11:07 +00:00
|
|
|
|
|
|
|
if (oblk & GET_SIMILARITY_FLAG) {
|
|
|
|
oblk = oblk & CLEAR_SIMILARITY_FLAG;
|
2012-08-17 05:33:02 +00:00
|
|
|
len = ctx->blocks[oblk]->length;
|
|
|
|
pos1 = ctx->blocks[oblk]->offset;
|
2012-07-19 16:11:07 +00:00
|
|
|
newsz = data_sz - sz;
|
2012-08-17 05:33:02 +00:00
|
|
|
rv = bspatch(buf + ctx->blocks[blk]->offset, buf + pos1, len, pos2, &newsz);
|
2012-07-19 16:11:07 +00:00
|
|
|
if (rv == 0) {
|
|
|
|
fprintf(stderr, "Failed to bspatch block.\n");
|
|
|
|
ctx->valid = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos2 += newsz;
|
|
|
|
sz += newsz;
|
|
|
|
if (sz > data_sz) {
|
|
|
|
fprintf(stderr, "Dedup data overflows chunk.\n");
|
|
|
|
ctx->valid = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else {
|
2012-08-17 05:33:02 +00:00
|
|
|
len = ctx->blocks[oblk]->length;
|
|
|
|
pos1 = ctx->blocks[oblk]->offset;
|
2012-07-19 16:11:07 +00:00
|
|
|
}
|
2012-06-29 12:53:55 +00:00
|
|
|
}
|
|
|
|
memcpy(pos2, buf + pos1, len);
|
|
|
|
pos2 += len;
|
|
|
|
sz += len;
|
2012-07-01 16:14:02 +00:00
|
|
|
if (sz > data_sz) {
|
2012-07-19 16:11:07 +00:00
|
|
|
fprintf(stderr, "Dedup data overflows chunk.\n");
|
2012-06-29 12:53:55 +00:00
|
|
|
ctx->valid = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-07-01 16:14:02 +00:00
|
|
|
if (ctx->valid && sz < data_sz) {
|
2012-07-19 16:11:07 +00:00
|
|
|
fprintf(stderr, "Too little dedup data processed.\n");
|
2012-06-29 12:53:55 +00:00
|
|
|
ctx->valid = 0;
|
|
|
|
}
|
2012-07-01 16:14:02 +00:00
|
|
|
*size = data_sz;
|
2012-06-21 14:57:05 +00:00
|
|
|
}
|
|
|
|
|
2012-07-01 16:14:02 +00:00
|
|
|
/*
|
|
|
|
* TODO: Consolidate rabin dedup and compression/decompression in functions here rather than
|
|
|
|
* messy code in main program.
|
|
|
|
int
|
2012-12-27 17:36:48 +00:00
|
|
|
rabin_compress(dedupe_context_t *ctx, uchar_t *from, uint64_t fromlen, uchar_t *to, uint64_t *tolen,
|
2012-07-01 16:14:02 +00:00
|
|
|
int level, char chdr, void *data, compress_func_ptr cmp)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
*/
|