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