match.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Block matching used by the file-transfer code.
  3. *
  4. * Copyright (C) 1996 Andrew Tridgell
  5. * Copyright (C) 1996 Paul Mackerras
  6. * Copyright (C) 2003-2009 Wayne Davison
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, visit the http://fsf.org website.
  20. */
  21. #include "rsync.h"
  22. extern int verbose;
  23. extern int do_progress;
  24. extern int checksum_seed;
  25. extern int append_mode;
  26. int updating_basis_file;
  27. static int false_alarms;
  28. static int hash_hits;
  29. static int matches;
  30. static int64 data_transfer;
  31. static int total_false_alarms;
  32. static int total_hash_hits;
  33. static int total_matches;
  34. extern struct stats stats;
  35. #define TRADITIONAL_TABLESIZE (1<<16)
  36. static uint32 tablesize;
  37. static int32 *hash_table;
  38. #define SUM2HASH2(s1,s2) (((s1) + (s2)) & 0xFFFF)
  39. #define SUM2HASH(sum) SUM2HASH2((sum)&0xFFFF,(sum)>>16)
  40. #define BIG_SUM2HASH(sum) ((sum)%tablesize)
  41. static void build_hash_table(struct sum_struct *s)
  42. {
  43. static uint32 alloc_size;
  44. int32 i;
  45. /* Dynamically calculate the hash table size so that the hash load
  46. * for big files is about 80%. A number greater than the traditional
  47. * size must be odd or s2 will not be able to span the entire set. */
  48. tablesize = (uint32)(s->count/8) * 10 + 11;
  49. if (tablesize < TRADITIONAL_TABLESIZE)
  50. tablesize = TRADITIONAL_TABLESIZE;
  51. if (tablesize > alloc_size || tablesize < alloc_size - 16*1024) {
  52. if (hash_table)
  53. free(hash_table);
  54. hash_table = new_array(int32, tablesize);
  55. if (!hash_table)
  56. out_of_memory("build_hash_table");
  57. alloc_size = tablesize;
  58. }
  59. memset(hash_table, 0xFF, tablesize * sizeof hash_table[0]);
  60. if (tablesize == TRADITIONAL_TABLESIZE) {
  61. for (i = 0; i < s->count; i++) {
  62. uint32 t = SUM2HASH(s->sums[i].sum1);
  63. s->sums[i].chain = hash_table[t];
  64. hash_table[t] = i;
  65. }
  66. } else {
  67. for (i = 0; i < s->count; i++) {
  68. uint32 t = BIG_SUM2HASH(s->sums[i].sum1);
  69. s->sums[i].chain = hash_table[t];
  70. hash_table[t] = i;
  71. }
  72. }
  73. }
  74. static OFF_T last_match;
  75. /* Transmit a literal and/or match token.
  76. *
  77. * This delightfully-named function is called either when we find a
  78. * match and need to transmit all the unmatched data leading up to it,
  79. * or when we get bored of accumulating literal data and just need to
  80. * transmit it. As a result of this second case, it is called even if
  81. * we have not matched at all!
  82. *
  83. * If i >= 0, the number of a matched token. If < 0, indicates we have
  84. * only literal data. A -1 will send a 0-token-int too, and a -2 sends
  85. * only literal data, w/o any token-int. */
  86. static void matched(int f, struct sum_struct *s, struct map_struct *buf,
  87. OFF_T offset, int32 i)
  88. {
  89. int32 n = (int32)(offset - last_match); /* max value: block_size (int32) */
  90. int32 j;
  91. if (verbose > 2 && i >= 0) {
  92. rprintf(FINFO,
  93. "match at %.0f last_match=%.0f j=%d len=%ld n=%ld\n",
  94. (double)offset, (double)last_match, i,
  95. (long)s->sums[i].len, (long)n);
  96. }
  97. send_token(f, i, buf, last_match, n, i < 0 ? 0 : s->sums[i].len);
  98. data_transfer += n;
  99. if (i >= 0) {
  100. stats.matched_data += s->sums[i].len;
  101. n += s->sums[i].len;
  102. }
  103. for (j = 0; j < n; j += CHUNK_SIZE) {
  104. int32 n1 = MIN(CHUNK_SIZE, n - j);
  105. sum_update(map_ptr(buf, last_match + j, n1), n1);
  106. }
  107. if (i >= 0)
  108. last_match = offset + s->sums[i].len;
  109. else
  110. last_match = offset;
  111. if (buf && do_progress)
  112. show_progress(last_match, buf->file_size);
  113. }
  114. static void hash_search(int f,struct sum_struct *s,
  115. struct map_struct *buf, OFF_T len)
  116. {
  117. OFF_T offset, aligned_offset, end;
  118. int32 k, want_i, aligned_i, backup;
  119. char sum2[SUM_LENGTH];
  120. uint32 s1, s2, sum;
  121. int more;
  122. schar *map;
  123. /* want_i is used to encourage adjacent matches, allowing the RLL
  124. * coding of the output to work more efficiently. */
  125. want_i = 0;
  126. if (verbose > 2) {
  127. rprintf(FINFO, "hash search b=%ld len=%.0f\n",
  128. (long)s->blength, (double)len);
  129. }
  130. k = (int32)MIN(len, (OFF_T)s->blength);
  131. map = (schar *)map_ptr(buf, 0, k);
  132. sum = get_checksum1((char *)map, k);
  133. s1 = sum & 0xFFFF;
  134. s2 = sum >> 16;
  135. if (verbose > 3)
  136. rprintf(FINFO, "sum=%.8x k=%ld\n", sum, (long)k);
  137. offset = aligned_offset = aligned_i = 0;
  138. end = len + 1 - s->sums[s->count-1].len;
  139. if (verbose > 3) {
  140. rprintf(FINFO, "hash search s->blength=%ld len=%.0f count=%.0f\n",
  141. (long)s->blength, (double)len, (double)s->count);
  142. }
  143. do {
  144. int done_csum2 = 0;
  145. int32 i;
  146. if (verbose > 4) {
  147. rprintf(FINFO, "offset=%.0f sum=%04x%04x\n",
  148. (double)offset, s2 & 0xFFFF, s1 & 0xFFFF);
  149. }
  150. if (tablesize == TRADITIONAL_TABLESIZE) {
  151. if ((i = hash_table[SUM2HASH2(s1,s2)]) < 0)
  152. goto null_hash;
  153. sum = (s1 & 0xffff) | (s2 << 16);
  154. } else {
  155. sum = (s1 & 0xffff) | (s2 << 16);
  156. if ((i = hash_table[BIG_SUM2HASH(sum)]) < 0)
  157. goto null_hash;
  158. }
  159. hash_hits++;
  160. do {
  161. int32 l;
  162. if (sum != s->sums[i].sum1)
  163. continue;
  164. /* also make sure the two blocks are the same length */
  165. l = (int32)MIN((OFF_T)s->blength, len-offset);
  166. if (l != s->sums[i].len)
  167. continue;
  168. /* in-place: ensure chunk's offset is either >= our
  169. * offset or that the data didn't move. */
  170. if (updating_basis_file && s->sums[i].offset < offset
  171. && !(s->sums[i].flags & SUMFLG_SAME_OFFSET))
  172. continue;
  173. if (verbose > 3) {
  174. rprintf(FINFO,
  175. "potential match at %.0f i=%ld sum=%08x\n",
  176. (double)offset, (long)i, sum);
  177. }
  178. if (!done_csum2) {
  179. map = (schar *)map_ptr(buf,offset,l);
  180. get_checksum2((char *)map,l,sum2);
  181. done_csum2 = 1;
  182. }
  183. if (memcmp(sum2,s->sums[i].sum2,s->s2length) != 0) {
  184. false_alarms++;
  185. continue;
  186. }
  187. /* When updating in-place, the best possible match is
  188. * one with an identical offset, so we prefer that over
  189. * the adjacent want_i optimization. */
  190. if (updating_basis_file) {
  191. /* All the generator's chunks start at blength boundaries. */
  192. while (aligned_offset < offset) {
  193. aligned_offset += s->blength;
  194. aligned_i++;
  195. }
  196. if (offset == aligned_offset && aligned_i < s->count) {
  197. if (i != aligned_i) {
  198. if (sum != s->sums[aligned_i].sum1
  199. || l != s->sums[aligned_i].len
  200. || memcmp(sum2, s->sums[aligned_i].sum2, s->s2length) != 0)
  201. goto check_want_i;
  202. i = aligned_i;
  203. }
  204. /* This identical chunk is in the same spot in the old and new file. */
  205. s->sums[i].flags |= SUMFLG_SAME_OFFSET;
  206. want_i = i;
  207. }
  208. }
  209. check_want_i:
  210. /* we've found a match, but now check to see
  211. * if want_i can hint at a better match. */
  212. if (i != want_i && want_i < s->count
  213. && (!updating_basis_file || s->sums[want_i].offset >= offset
  214. || s->sums[want_i].flags & SUMFLG_SAME_OFFSET)
  215. && sum == s->sums[want_i].sum1
  216. && memcmp(sum2, s->sums[want_i].sum2, s->s2length) == 0) {
  217. /* we've found an adjacent match - the RLL coder
  218. * will be happy */
  219. i = want_i;
  220. }
  221. want_i = i + 1;
  222. matched(f,s,buf,offset,i);
  223. offset += s->sums[i].len - 1;
  224. k = (int32)MIN((OFF_T)s->blength, len-offset);
  225. map = (schar *)map_ptr(buf, offset, k);
  226. sum = get_checksum1((char *)map, k);
  227. s1 = sum & 0xFFFF;
  228. s2 = sum >> 16;
  229. matches++;
  230. break;
  231. } while ((i = s->sums[i].chain) >= 0);
  232. null_hash:
  233. backup = (int32)(offset - last_match);
  234. /* We sometimes read 1 byte prior to last_match... */
  235. if (backup < 0)
  236. backup = 0;
  237. /* Trim off the first byte from the checksum */
  238. more = offset + k < len;
  239. map = (schar *)map_ptr(buf, offset - backup, k + more + backup)
  240. + backup;
  241. s1 -= map[0] + CHAR_OFFSET;
  242. s2 -= k * (map[0]+CHAR_OFFSET);
  243. /* Add on the next byte (if there is one) to the checksum */
  244. if (more) {
  245. s1 += map[k] + CHAR_OFFSET;
  246. s2 += s1;
  247. } else
  248. --k;
  249. /* By matching early we avoid re-reading the
  250. data 3 times in the case where a token
  251. match comes a long way after last
  252. match. The 3 reads are caused by the
  253. running match, the checksum update and the
  254. literal send. */
  255. if (backup >= s->blength+CHUNK_SIZE && end-offset > CHUNK_SIZE)
  256. matched(f, s, buf, offset - s->blength, -2);
  257. } while (++offset < end);
  258. matched(f, s, buf, len, -1);
  259. map_ptr(buf, len-1, 1);
  260. }
  261. /**
  262. * Scan through a origin file, looking for sections that match
  263. * checksums from the generator, and transmit either literal or token
  264. * data.
  265. *
  266. * Also calculates the MD4 checksum of the whole file, using the md
  267. * accumulator. This is transmitted with the file as protection
  268. * against corruption on the wire.
  269. *
  270. * @param s Checksums received from the generator. If <tt>s->count ==
  271. * 0</tt>, then there are actually no checksums for this file.
  272. *
  273. * @param len Length of the file to send.
  274. **/
  275. void match_sums(int f, struct sum_struct *s, struct map_struct *buf, OFF_T len)
  276. {
  277. char file_sum[MAX_DIGEST_LEN];
  278. int sum_len;
  279. last_match = 0;
  280. false_alarms = 0;
  281. hash_hits = 0;
  282. matches = 0;
  283. data_transfer = 0;
  284. sum_init(checksum_seed);
  285. if (append_mode > 0) {
  286. if (append_mode == 2) {
  287. OFF_T j = 0;
  288. for (j = CHUNK_SIZE; j < s->flength; j += CHUNK_SIZE) {
  289. if (buf && do_progress)
  290. show_progress(last_match, buf->file_size);
  291. sum_update(map_ptr(buf, last_match, CHUNK_SIZE),
  292. CHUNK_SIZE);
  293. last_match = j;
  294. }
  295. if (last_match < s->flength) {
  296. int32 n = (int32)(s->flength - last_match);
  297. if (buf && do_progress)
  298. show_progress(last_match, buf->file_size);
  299. sum_update(map_ptr(buf, last_match, n), n);
  300. }
  301. }
  302. last_match = s->flength;
  303. s->count = 0;
  304. }
  305. if (len > 0 && s->count > 0) {
  306. build_hash_table(s);
  307. if (verbose > 2)
  308. rprintf(FINFO,"built hash table\n");
  309. hash_search(f, s, buf, len);
  310. if (verbose > 2)
  311. rprintf(FINFO,"done hash search\n");
  312. } else {
  313. OFF_T j;
  314. /* by doing this in pieces we avoid too many seeks */
  315. for (j = last_match + CHUNK_SIZE; j < len; j += CHUNK_SIZE)
  316. matched(f, s, buf, j, -2);
  317. matched(f, s, buf, len, -1);
  318. }
  319. sum_len = sum_end(file_sum);
  320. /* If we had a read error, send a bad checksum. */
  321. if (buf && buf->status != 0)
  322. file_sum[0]++;
  323. if (verbose > 2)
  324. rprintf(FINFO,"sending file_sum\n");
  325. write_buf(f, file_sum, sum_len);
  326. if (verbose > 2)
  327. rprintf(FINFO, "false_alarms=%d hash_hits=%d matches=%d\n",
  328. false_alarms, hash_hits, matches);
  329. total_hash_hits += hash_hits;
  330. total_false_alarms += false_alarms;
  331. total_matches += matches;
  332. stats.literal_data += data_transfer;
  333. }
  334. void match_report(void)
  335. {
  336. if (verbose <= 1)
  337. return;
  338. rprintf(FINFO,
  339. "total: matches=%d hash_hits=%d false_alarms=%d data=%.0f\n",
  340. total_matches, total_hash_hits, total_false_alarms,
  341. (double)stats.literal_data);
  342. }