| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "rsync.h" |
| 23 |
#include "inums.h" |
| 24 |
|
| 25 |
extern int checksum_seed; |
| 26 |
extern int append_mode; |
| 27 |
|
| 28 |
extern struct name_num_item *xfer_sum_nni; |
| 29 |
extern int xfer_sum_len; |
| 30 |
|
| 31 |
int updating_basis_file; |
| 32 |
char sender_file_sum[MAX_DIGEST_LEN]; |
| 33 |
|
| 34 |
static int false_alarms; |
| 35 |
static int hash_hits; |
| 36 |
static int matches; |
| 37 |
static int64 data_transfer; |
| 38 |
|
| 39 |
static int total_false_alarms; |
| 40 |
static int total_hash_hits; |
| 41 |
static int total_matches; |
| 42 |
|
| 43 |
extern struct stats stats; |
| 44 |
|
| 45 |
#define TRADITIONAL_TABLESIZE (1<<16) |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
#ifndef MAX_CHAIN_LEN |
| 67 |
#define MAX_CHAIN_LEN 1024 |
| 68 |
#endif |
| 69 |
|
| 70 |
static uint32 tablesize; |
| 71 |
static int32 *hash_table; |
| 72 |
|
| 73 |
#define SUM2HASH2(s1,s2) (((s1) + (s2)) & 0xFFFF) |
| 74 |
#define SUM2HASH(sum) SUM2HASH2((sum)&0xFFFF,(sum)>>16) |
| 75 |
|
| 76 |
#define BIG_SUM2HASH(sum) ((sum)%tablesize) |
| 77 |
|
| 78 |
static void build_hash_table(struct sum_struct *s) |
| 79 |
{ |
| 80 |
static uint32 alloc_size; |
| 81 |
int32 i; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
tablesize = (uint32)(s->count/8) * 10 + 11; |
| 87 |
if (tablesize < TRADITIONAL_TABLESIZE) |
| 88 |
tablesize = TRADITIONAL_TABLESIZE; |
| 89 |
if (tablesize > alloc_size || tablesize < alloc_size - 16*1024) { |
| 90 |
if (hash_table) |
| 91 |
free(hash_table); |
| 92 |
hash_table = new_array(int32, tablesize); |
| 93 |
alloc_size = tablesize; |
| 94 |
} |
| 95 |
|
| 96 |
memset(hash_table, 0xFF, tablesize * sizeof hash_table[0]); |
| 97 |
|
| 98 |
if (tablesize == TRADITIONAL_TABLESIZE) { |
| 99 |
for (i = 0; i < s->count; i++) { |
| 100 |
uint32 t = SUM2HASH(s->sums[i].sum1); |
| 101 |
s->sums[i].chain = hash_table[t]; |
| 102 |
hash_table[t] = i; |
| 103 |
} |
| 104 |
} else { |
| 105 |
for (i = 0; i < s->count; i++) { |
| 106 |
uint32 t = BIG_SUM2HASH(s->sums[i].sum1); |
| 107 |
s->sums[i].chain = hash_table[t]; |
| 108 |
hash_table[t] = i; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
static OFF_T last_match; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
static void matched(int f, struct sum_struct *s, struct map_struct *buf, OFF_T offset, int32 i) |
| 129 |
{ |
| 130 |
int32 n = (int32)(offset - last_match); |
| 131 |
int32 j; |
| 132 |
|
| 133 |
if (DEBUG_GTE(DELTASUM, 2) && i >= 0) { |
| 134 |
rprintf(FINFO, |
| 135 |
"match at %s last_match=%s j=%d len=%ld n=%ld\n", |
| 136 |
big_num(offset), big_num(last_match), i, |
| 137 |
(long)s->sums[i].len, (long)n); |
| 138 |
} |
| 139 |
|
| 140 |
send_token(f, i, buf, last_match, n, i < 0 ? 0 : s->sums[i].len); |
| 141 |
data_transfer += n; |
| 142 |
|
| 143 |
if (i >= 0) { |
| 144 |
stats.matched_data += s->sums[i].len; |
| 145 |
n += s->sums[i].len; |
| 146 |
} |
| 147 |
|
| 148 |
for (j = 0; j < n; j += CHUNK_SIZE) { |
| 149 |
int32 n1 = MIN(CHUNK_SIZE, n - j); |
| 150 |
sum_update(map_ptr(buf, last_match + j, n1), n1); |
| 151 |
} |
| 152 |
|
| 153 |
if (i >= 0) |
| 154 |
last_match = offset + s->sums[i].len; |
| 155 |
else |
| 156 |
last_match = offset; |
| 157 |
|
| 158 |
if (buf && INFO_GTE(PROGRESS, 1)) |
| 159 |
show_progress(last_match, buf->file_size); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
static void hash_search(int f,struct sum_struct *s, |
| 164 |
struct map_struct *buf, OFF_T len) |
| 165 |
{ |
| 166 |
OFF_T offset, aligned_offset, end; |
| 167 |
int32 k, want_i, aligned_i, backup; |
| 168 |
char sum2[MAX_DIGEST_LEN]; |
| 169 |
uint32 s1, s2, sum; |
| 170 |
int more; |
| 171 |
schar *map; |
| 172 |
|
| 173 |
|
| 174 |
memset(sum2, 0, sizeof sum2); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
want_i = 0; |
| 179 |
|
| 180 |
if (DEBUG_GTE(DELTASUM, 2)) { |
| 181 |
rprintf(FINFO, "hash search b=%ld len=%s\n", |
| 182 |
(long)s->blength, big_num(len)); |
| 183 |
} |
| 184 |
|
| 185 |
k = (int32)MIN(len, (OFF_T)s->blength); |
| 186 |
|
| 187 |
map = (schar *)map_ptr(buf, 0, k); |
| 188 |
|
| 189 |
sum = get_checksum1((char *)map, k); |
| 190 |
s1 = sum & 0xFFFF; |
| 191 |
s2 = sum >> 16; |
| 192 |
if (DEBUG_GTE(DELTASUM, 3)) |
| 193 |
rprintf(FINFO, "sum=%.8x k=%ld\n", sum, (long)k); |
| 194 |
|
| 195 |
offset = aligned_offset = aligned_i = 0; |
| 196 |
|
| 197 |
end = len + 1 - s->sums[s->count-1].len; |
| 198 |
|
| 199 |
if (DEBUG_GTE(DELTASUM, 3)) { |
| 200 |
rprintf(FINFO, "hash search s->blength=%ld len=%s count=%s\n", |
| 201 |
(long)s->blength, big_num(len), big_num(s->count)); |
| 202 |
} |
| 203 |
|
| 204 |
do { |
| 205 |
int done_csum2 = 0; |
| 206 |
uint32 hash_entry; |
| 207 |
int32 i, *prev; |
| 208 |
int32 chain_len = 0; |
| 209 |
|
| 210 |
if (DEBUG_GTE(DELTASUM, 4)) { |
| 211 |
rprintf(FINFO, "offset=%s sum=%04x%04x\n", |
| 212 |
big_num(offset), s2 & 0xFFFF, s1 & 0xFFFF); |
| 213 |
} |
| 214 |
|
| 215 |
if (tablesize == TRADITIONAL_TABLESIZE) { |
| 216 |
hash_entry = SUM2HASH2(s1,s2); |
| 217 |
if ((i = hash_table[hash_entry]) < 0) |
| 218 |
goto null_hash; |
| 219 |
sum = (s1 & 0xffff) | (s2 << 16); |
| 220 |
} else { |
| 221 |
sum = (s1 & 0xffff) | (s2 << 16); |
| 222 |
hash_entry = BIG_SUM2HASH(sum); |
| 223 |
if ((i = hash_table[hash_entry]) < 0) |
| 224 |
goto null_hash; |
| 225 |
} |
| 226 |
prev = &hash_table[hash_entry]; |
| 227 |
|
| 228 |
hash_hits++; |
| 229 |
do { |
| 230 |
int32 l; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
if (updating_basis_file && s->sums[i].offset < offset |
| 236 |
&& !(s->sums[i].flags & SUMFLG_SAME_OFFSET)) { |
| 237 |
*prev = s->sums[i].chain; |
| 238 |
continue; |
| 239 |
} |
| 240 |
prev = &s->sums[i].chain; |
| 241 |
|
| 242 |
if (sum != s->sums[i].sum1) |
| 243 |
continue; |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
if (++chain_len > MAX_CHAIN_LEN) |
| 251 |
break; |
| 252 |
|
| 253 |
|
| 254 |
l = (int32)MIN((OFF_T)s->blength, len-offset); |
| 255 |
if (l != s->sums[i].len) |
| 256 |
continue; |
| 257 |
|
| 258 |
if (DEBUG_GTE(DELTASUM, 3)) { |
| 259 |
rprintf(FINFO, |
| 260 |
"potential match at %s i=%ld sum=%08x\n", |
| 261 |
big_num(offset), (long)i, sum); |
| 262 |
} |
| 263 |
|
| 264 |
if (!done_csum2) { |
| 265 |
map = (schar *)map_ptr(buf,offset,l); |
| 266 |
get_checksum2((char *)map,l,sum2); |
| 267 |
done_csum2 = 1; |
| 268 |
} |
| 269 |
|
| 270 |
if (memcmp(sum2, sum2_at(s, i), s->s2length) != 0) { |
| 271 |
false_alarms++; |
| 272 |
continue; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
if (updating_basis_file) { |
| 279 |
|
| 280 |
while (aligned_offset < offset) { |
| 281 |
aligned_offset += s->blength; |
| 282 |
aligned_i++; |
| 283 |
} |
| 284 |
if ((offset == aligned_offset |
| 285 |
|| (sum == 0 && l == s->blength && aligned_offset + l <= len)) |
| 286 |
&& aligned_i < s->count) { |
| 287 |
if (i != aligned_i) { |
| 288 |
if (sum != s->sums[aligned_i].sum1 |
| 289 |
|| l != s->sums[aligned_i].len |
| 290 |
|| memcmp(sum2, sum2_at(s, aligned_i), s->s2length) != 0) |
| 291 |
goto check_want_i; |
| 292 |
i = aligned_i; |
| 293 |
} |
| 294 |
if (offset != aligned_offset) { |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
backup = (int32)(aligned_offset - last_match); |
| 301 |
if (backup < 0) |
| 302 |
backup = 0; |
| 303 |
map = (schar *)map_ptr(buf, aligned_offset - backup, l + backup) |
| 304 |
+ backup; |
| 305 |
sum = get_checksum1((char *)map, l); |
| 306 |
if (sum != s->sums[i].sum1) |
| 307 |
goto check_want_i; |
| 308 |
get_checksum2((char *)map, l, sum2); |
| 309 |
if (memcmp(sum2, sum2_at(s, i), s->s2length) != 0) |
| 310 |
goto check_want_i; |
| 311 |
|
| 312 |
|
| 313 |
offset = aligned_offset; |
| 314 |
} |
| 315 |
|
| 316 |
s->sums[i].flags |= SUMFLG_SAME_OFFSET; |
| 317 |
want_i = i; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
check_want_i: |
| 322 |
|
| 323 |
|
| 324 |
if (i != want_i && want_i < s->count |
| 325 |
&& (!updating_basis_file || s->sums[want_i].offset >= offset |
| 326 |
|| s->sums[want_i].flags & SUMFLG_SAME_OFFSET) |
| 327 |
&& sum == s->sums[want_i].sum1 |
| 328 |
&& l == s->sums[want_i].len |
| 329 |
&& memcmp(sum2, sum2_at(s, want_i), s->s2length) == 0) { |
| 330 |
|
| 331 |
|
| 332 |
i = want_i; |
| 333 |
} |
| 334 |
want_i = i + 1; |
| 335 |
|
| 336 |
matched(f,s,buf,offset,i); |
| 337 |
offset += s->sums[i].len - 1; |
| 338 |
k = (int32)MIN((OFF_T)s->blength, len-offset); |
| 339 |
map = (schar *)map_ptr(buf, offset, k); |
| 340 |
sum = get_checksum1((char *)map, k); |
| 341 |
s1 = sum & 0xFFFF; |
| 342 |
s2 = sum >> 16; |
| 343 |
matches++; |
| 344 |
break; |
| 345 |
} while ((i = s->sums[i].chain) >= 0); |
| 346 |
|
| 347 |
null_hash: |
| 348 |
backup = (int32)(offset - last_match); |
| 349 |
|
| 350 |
if (backup < 0) |
| 351 |
backup = 0; |
| 352 |
|
| 353 |
|
| 354 |
more = offset + k < len; |
| 355 |
map = (schar *)map_ptr(buf, offset - backup, k + more + backup) + backup; |
| 356 |
s1 -= map[0] + CHAR_OFFSET; |
| 357 |
s2 -= k * (map[0]+CHAR_OFFSET); |
| 358 |
|
| 359 |
|
| 360 |
if (more) { |
| 361 |
s1 += map[k] + CHAR_OFFSET; |
| 362 |
s2 += s1; |
| 363 |
} else |
| 364 |
--k; |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
if (backup >= s->blength+CHUNK_SIZE && end-offset > CHUNK_SIZE) |
| 373 |
matched(f, s, buf, offset - s->blength, -2); |
| 374 |
} while (++offset < end); |
| 375 |
|
| 376 |
matched(f, s, buf, len, -1); |
| 377 |
map_ptr(buf, len-1, 1); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
void match_sums(int f, struct sum_struct *s, struct map_struct *buf, OFF_T len) |
| 396 |
{ |
| 397 |
last_match = 0; |
| 398 |
false_alarms = 0; |
| 399 |
hash_hits = 0; |
| 400 |
matches = 0; |
| 401 |
data_transfer = 0; |
| 402 |
|
| 403 |
sum_init(xfer_sum_nni, checksum_seed); |
| 404 |
|
| 405 |
if (append_mode > 0) { |
| 406 |
if (s->flength > len) { |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
s->flength = len; |
| 413 |
} |
| 414 |
if (append_mode == 2) { |
| 415 |
OFF_T j = 0; |
| 416 |
for (j = CHUNK_SIZE; j < s->flength; j += CHUNK_SIZE) { |
| 417 |
if (buf && INFO_GTE(PROGRESS, 1)) |
| 418 |
show_progress(last_match, buf->file_size); |
| 419 |
sum_update(map_ptr(buf, last_match, CHUNK_SIZE), |
| 420 |
CHUNK_SIZE); |
| 421 |
last_match = j; |
| 422 |
} |
| 423 |
if (last_match < s->flength) { |
| 424 |
int32 n = (int32)(s->flength - last_match); |
| 425 |
if (buf && INFO_GTE(PROGRESS, 1)) |
| 426 |
show_progress(last_match, buf->file_size); |
| 427 |
sum_update(map_ptr(buf, last_match, n), n); |
| 428 |
} |
| 429 |
} |
| 430 |
last_match = s->flength; |
| 431 |
s->count = 0; |
| 432 |
} |
| 433 |
|
| 434 |
if (len > 0 && s->count > 0) { |
| 435 |
build_hash_table(s); |
| 436 |
|
| 437 |
if (DEBUG_GTE(DELTASUM, 2)) |
| 438 |
rprintf(FINFO,"built hash table\n"); |
| 439 |
|
| 440 |
hash_search(f, s, buf, len); |
| 441 |
|
| 442 |
if (DEBUG_GTE(DELTASUM, 2)) |
| 443 |
rprintf(FINFO,"done hash search\n"); |
| 444 |
} else { |
| 445 |
OFF_T j; |
| 446 |
|
| 447 |
for (j = last_match + CHUNK_SIZE; j < len; j += CHUNK_SIZE) |
| 448 |
matched(f, s, buf, j, -2); |
| 449 |
matched(f, s, buf, len, -1); |
| 450 |
} |
| 451 |
|
| 452 |
sum_end(sender_file_sum); |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
if (buf && buf->status != 0) { |
| 458 |
int i; |
| 459 |
for (i = 0; i < xfer_sum_len && sender_file_sum[i] == 0; i++) {} |
| 460 |
memset(sender_file_sum, 0, xfer_sum_len); |
| 461 |
if (i == xfer_sum_len) |
| 462 |
sender_file_sum[i-1]++; |
| 463 |
} |
| 464 |
|
| 465 |
if (DEBUG_GTE(DELTASUM, 2)) |
| 466 |
rprintf(FINFO,"sending file_sum\n"); |
| 467 |
write_buf(f, sender_file_sum, xfer_sum_len); |
| 468 |
|
| 469 |
if (DEBUG_GTE(DELTASUM, 2)) { |
| 470 |
rprintf(FINFO, "false_alarms=%d hash_hits=%d matches=%d\n", |
| 471 |
false_alarms, hash_hits, matches); |
| 472 |
} |
| 473 |
|
| 474 |
total_hash_hits += hash_hits; |
| 475 |
total_false_alarms += false_alarms; |
| 476 |
total_matches += matches; |
| 477 |
stats.literal_data += data_transfer; |
| 478 |
} |
| 479 |
|
| 480 |
void match_report(void) |
| 481 |
{ |
| 482 |
if (!DEBUG_GTE(DELTASUM, 1)) |
| 483 |
return; |
| 484 |
|
| 485 |
rprintf(FINFO, |
| 486 |
"total: matches=%d hash_hits=%d false_alarms=%d data=%s\n", |
| 487 |
total_matches, total_hash_hits, total_false_alarms, |
| 488 |
big_num(stats.literal_data)); |
| 489 |
} |