match.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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-2008 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. /**
  76. * Transmit a literal and/or match token.
  77. *
  78. * This delightfully-named function is called either when we find a
  79. * match and need to transmit all the unmatched data leading up to it,
  80. * or when we get bored of accumulating literal data and just need to
  81. * transmit it. As a result of this second case, it is called even if
  82. * we have not matched at all!
  83. *
  84. * @param i If >0, the number of a matched token. If 0, indicates we
  85. * have only literal data.
  86. **/
  87. static void matched(int f, struct sum_struct *s, struct map_struct *buf,
  88. OFF_T offset, int32 i)
  89. {
  90. int32 n = (int32)(offset - last_match); /* max value: block_size (int32) */
  91. int32 j;
  92. if (verbose > 2 && i >= 0) {
  93. rprintf(FINFO,
  94. "match at %.0f last_match=%.0f j=%d len=%ld n=%ld\n",
  95. (double)offset, (double)last_match, i,
  96. (long)s->sums[i].len, (long)n);
  97. }
  98. send_token(f, i, buf, last_match, n, i < 0 ? 0 : s->sums[i].len);
  99. data_transfer += n;
  100. if (i >= 0) {
  101. stats.matched_data += s->sums[i].len;
  102. n += s->sums[i].len;
  103. }
  104. for (j = 0; j < n; j += CHUNK_SIZE) {
  105. int32 n1 = MIN(CHUNK_SIZE, n - j);
  106. sum_update(map_ptr(buf, last_match + j, n1), n1);
  107. }
  108. if (i >= 0)
  109. last_match = offset + s->sums[i].len;
  110. else
  111. last_match = offset;
  112. if (buf && do_progress)
  113. show_progress(last_match, buf->file_size);
  114. }
  115. static void hash_search(int f,struct sum_struct *s,
  116. struct map_struct *buf, OFF_T len)
  117. {
  118. OFF_T offset, end;
  119. int32 k, want_i, backup;
  120. char sum2[SUM_LENGTH];
  121. uint32 s1, s2, sum;
  122. int more;
  123. schar *map;
  124. /* want_i is used to encourage adjacent matches, allowing the RLL
  125. * coding of the output to work more efficiently. */
  126. want_i = 0;
  127. if (verbose > 2) {
  128. rprintf(FINFO, "hash search b=%ld len=%.0f\n",
  129. (long)s->blength, (double)len);
  130. }
  131. k = (int32)MIN(len, (OFF_T)s->blength);
  132. map = (schar *)map_ptr(buf, 0, k);
  133. sum = get_checksum1((char *)map, k);
  134. s1 = sum & 0xFFFF;
  135. s2 = sum >> 16;
  136. if (verbose > 3)
  137. rprintf(FINFO, "sum=%.8x k=%ld\n", sum, (long)k);
  138. offset = 0;
  139. end = len + 1 - s->sums[s->count-1].len;
  140. if (verbose > 3) {
  141. rprintf(FINFO, "hash search s->blength=%ld len=%.0f count=%.0f\n",
  142. (long)s->blength, (double)len, (double)s->count);
  143. }
  144. do {
  145. int done_csum2 = 0;
  146. int32 i;
  147. if (verbose > 4) {
  148. rprintf(FINFO, "offset=%.0f sum=%04x%04x\n",
  149. (double)offset, s2 & 0xFFFF, s1 & 0xFFFF);
  150. }
  151. if (tablesize == TRADITIONAL_TABLESIZE) {
  152. if ((i = hash_table[SUM2HASH2(s1,s2)]) < 0)
  153. goto null_hash;
  154. sum = (s1 & 0xffff) | (s2 << 16);
  155. } else {
  156. sum = (s1 & 0xffff) | (s2 << 16);
  157. if ((i = hash_table[BIG_SUM2HASH(sum)]) < 0)
  158. goto null_hash;
  159. }
  160. hash_hits++;
  161. do {
  162. int32 l;
  163. if (sum != s->sums[i].sum1)
  164. continue;
  165. /* also make sure the two blocks are the same length */
  166. l = (int32)MIN((OFF_T)s->blength, len-offset);
  167. if (l != s->sums[i].len)
  168. continue;
  169. /* in-place: ensure chunk's offset is either >= our
  170. * offset or that the data didn't move. */
  171. if (updating_basis_file && s->sums[i].offset < offset
  172. && !(s->sums[i].flags & SUMFLG_SAME_OFFSET))
  173. continue;
  174. if (verbose > 3) {
  175. rprintf(FINFO,
  176. "potential match at %.0f i=%ld sum=%08x\n",
  177. (double)offset, (long)i, sum);
  178. }
  179. if (!done_csum2) {
  180. map = (schar *)map_ptr(buf,offset,l);
  181. get_checksum2((char *)map,l,sum2);
  182. done_csum2 = 1;
  183. }
  184. if (memcmp(sum2,s->sums[i].sum2,s->s2length) != 0) {
  185. false_alarms++;
  186. continue;
  187. }
  188. /* When updating in-place, the best possible match is
  189. * one with an identical offset, so we prefer that over
  190. * the following want_i optimization. */
  191. if (updating_basis_file) {
  192. int32 i2;
  193. for (i2 = i; i2 >= 0; i2 = s->sums[i2].chain) {
  194. if (s->sums[i2].offset != offset)
  195. continue;
  196. if (i2 != i) {
  197. if (sum != s->sums[i2].sum1)
  198. break;
  199. if (memcmp(sum2, s->sums[i2].sum2,
  200. s->s2length) != 0)
  201. break;
  202. i = i2;
  203. }
  204. /* This chunk was at the same offset on
  205. * both the sender and the receiver. */
  206. s->sums[i].flags |= SUMFLG_SAME_OFFSET;
  207. goto set_want_i;
  208. }
  209. }
  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. set_want_i:
  222. want_i = i + 1;
  223. matched(f,s,buf,offset,i);
  224. offset += s->sums[i].len - 1;
  225. k = (int32)MIN((OFF_T)s->blength, len-offset);
  226. map = (schar *)map_ptr(buf, offset, k);
  227. sum = get_checksum1((char *)map, k);
  228. s1 = sum & 0xFFFF;
  229. s2 = sum >> 16;
  230. matches++;
  231. break;
  232. } while ((i = s->sums[i].chain) >= 0);
  233. null_hash:
  234. backup = (int32)(offset - last_match);
  235. /* We sometimes read 1 byte prior to last_match... */
  236. if (backup < 0)
  237. backup = 0;
  238. /* Trim off the first byte from the checksum */
  239. more = offset + k < len;
  240. map = (schar *)map_ptr(buf, offset - backup, k + more + backup)
  241. + backup;
  242. s1 -= map[0] + CHAR_OFFSET;
  243. s2 -= k * (map[0]+CHAR_OFFSET);
  244. /* Add on the next byte (if there is one) to the checksum */
  245. if (more) {
  246. s1 += map[k] + CHAR_OFFSET;
  247. s2 += s1;
  248. } else
  249. --k;
  250. /* By matching early we avoid re-reading the
  251. data 3 times in the case where a token
  252. match comes a long way after last
  253. match. The 3 reads are caused by the
  254. running match, the checksum update and the
  255. literal send. */
  256. if (backup >= s->blength+CHUNK_SIZE && end-offset > CHUNK_SIZE)
  257. matched(f, s, buf, offset - s->blength, -2);
  258. } while (++offset < end);
  259. matched(f, s, buf, len, -1);
  260. map_ptr(buf, len-1, 1);
  261. }
  262. /**
  263. * Scan through a origin file, looking for sections that match
  264. * checksums from the generator, and transmit either literal or token
  265. * data.
  266. *
  267. * Also calculates the MD4 checksum of the whole file, using the md
  268. * accumulator. This is transmitted with the file as protection
  269. * against corruption on the wire.
  270. *
  271. * @param s Checksums received from the generator. If <tt>s->count ==
  272. * 0</tt>, then there are actually no checksums for this file.
  273. *
  274. * @param len Length of the file to send.
  275. **/
  276. void match_sums(int f, struct sum_struct *s, struct map_struct *buf, OFF_T len)
  277. {
  278. char file_sum[MAX_DIGEST_LEN];
  279. int sum_len;
  280. last_match = 0;
  281. false_alarms = 0;
  282. hash_hits = 0;
  283. matches = 0;
  284. data_transfer = 0;
  285. sum_init(checksum_seed);
  286. if (append_mode > 0) {
  287. if (append_mode == 2) {
  288. OFF_T j = 0;
  289. for (j = CHUNK_SIZE; j < s->flength; j += CHUNK_SIZE) {
  290. if (buf && do_progress)
  291. show_progress(last_match, buf->file_size);
  292. sum_update(map_ptr(buf, last_match, CHUNK_SIZE),
  293. CHUNK_SIZE);
  294. last_match = j;
  295. }
  296. if (last_match < s->flength) {
  297. int32 n = (int32)(s->flength - last_match);
  298. if (buf && do_progress)
  299. show_progress(last_match, buf->file_size);
  300. sum_update(map_ptr(buf, last_match, n), n);
  301. }
  302. }
  303. last_match = s->flength;
  304. s->count = 0;
  305. }
  306. if (len > 0 && s->count > 0) {
  307. build_hash_table(s);
  308. if (verbose > 2)
  309. rprintf(FINFO,"built hash table\n");
  310. hash_search(f, s, buf, len);
  311. if (verbose > 2)
  312. rprintf(FINFO,"done hash search\n");
  313. } else {
  314. OFF_T j;
  315. /* by doing this in pieces we avoid too many seeks */
  316. for (j = last_match + CHUNK_SIZE; j < len; j += CHUNK_SIZE)
  317. matched(f, s, buf, j, -2);
  318. matched(f, s, buf, len, -1);
  319. }
  320. sum_len = sum_end(file_sum);
  321. /* If we had a read error, send a bad checksum. */
  322. if (buf && buf->status != 0)
  323. file_sum[0]++;
  324. if (verbose > 2)
  325. rprintf(FINFO,"sending file_sum\n");
  326. write_buf(f, file_sum, sum_len);
  327. if (verbose > 2)
  328. rprintf(FINFO, "false_alarms=%d hash_hits=%d matches=%d\n",
  329. false_alarms, hash_hits, matches);
  330. total_hash_hits += hash_hits;
  331. total_false_alarms += false_alarms;
  332. total_matches += matches;
  333. stats.literal_data += data_transfer;
  334. }
  335. void match_report(void)
  336. {
  337. if (verbose <= 1)
  338. return;
  339. rprintf(FINFO,
  340. "total: matches=%d hash_hits=%d false_alarms=%d data=%.0f\n",
  341. total_matches, total_hash_hits, total_false_alarms,
  342. (double)stats.literal_data);
  343. }