went to full non-blocking writes for the send_files() process
[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 am_server;
24
25 typedef unsigned short tag;
26
27 #define TABLESIZE (1<<16)
28 #define NULL_TAG ((tag)-1)
29
30 static int false_alarms;
31 static int tag_hits;
32 static int matches;
33 static int data_transfer;
34
35 static int total_false_alarms=0;
36 static int total_tag_hits=0;
37 static int total_matches=0;
38 static int total_data_transfer=0;
39
40
41 struct target {
42   tag t;
43   int i;
44 };
45
46 static struct target *targets=NULL;
47
48 static tag *tag_table = NULL;
49
50 #define gettag2(s1,s2) (((s1) + (s2)) & 0xFFFF)
51 #define gettag(sum) gettag2((sum)&0xFFFF,(sum)>>16)
52
53 static int compare_targets(struct target *t1,struct target *t2)
54 {
55   return(t1->t - t2->t);
56 }
57
58
59 static void build_hash_table(struct sum_struct *s)
60 {
61   int i;
62
63   if (!tag_table)
64     tag_table = (tag *)malloc(sizeof(tag)*TABLESIZE);
65
66   targets = (struct target *)malloc(sizeof(targets[0])*s->count);
67   if (!tag_table || !targets) 
68     out_of_memory("build_hash_table");
69
70   for (i=0;i<s->count;i++) {
71     targets[i].i = i;
72     targets[i].t = gettag(s->sums[i].sum1);
73   }
74
75   qsort(targets,s->count,sizeof(targets[0]),(int (*)())compare_targets);
76
77   for (i=0;i<TABLESIZE;i++)
78     tag_table[i] = NULL_TAG;
79
80   for (i=s->count-1;i>=0;i--) {    
81     tag_table[targets[i].t] = i;
82   }
83 }
84
85
86
87 static off_t last_match;
88
89
90 static void matched(int f,struct sum_struct *s,char *buf,off_t len,int offset,int i)
91 {
92   int n = offset - last_match;
93   
94   if (verbose > 2)
95     if (i != -1)
96       fprintf(stderr,"match at %d last_match=%d j=%d len=%d n=%d\n",
97               (int)offset,(int)last_match,i,(int)s->sums[i].len,n);
98
99   if (n > 0) {
100     write_int(f,n);
101     write_buf(f,buf+last_match,n);
102     data_transfer += n;
103   }
104   write_int(f,-(i+1));
105   if (i != -1)
106     last_match = offset + s->sums[i].len;
107   if (n > 0)
108     write_flush(f);
109 }
110
111
112 static void hash_search(int f,struct sum_struct *s,char *buf,off_t len)
113 {
114   int offset,j,k;
115   int end;
116   char sum2[SUM_LENGTH];
117   uint32 s1, s2, sum;
118
119   if (verbose > 2)
120     fprintf(stderr,"hash search b=%d len=%d\n",s->n,(int)len);
121
122   k = MIN(len, s->n);
123   sum = get_checksum1(buf, k);
124   s1 = sum & 0xFFFF;
125   s2 = sum >> 16;
126   if (verbose > 3)
127     fprintf(stderr, "sum=%.8x k=%d\n", sum, k);
128
129   offset = 0;
130
131   end = len + 1 - s->sums[s->count-1].len;
132
133   if (verbose > 3)
134     fprintf(stderr,"hash search s->n=%d len=%d count=%d\n",
135             s->n,(int)len,s->count);
136
137   do {
138     tag t = gettag2(s1,s2);
139     j = tag_table[t];
140     if (verbose > 4)
141       fprintf(stderr,"offset=%d sum=%08x\n",
142               offset,sum);
143
144     if (j != NULL_TAG) {
145       int done_csum2 = 0;
146
147       sum = (s1 & 0xffff) | (s2 << 16);
148       tag_hits++;
149       do {
150         int i = targets[j].i;
151
152         if (sum == s->sums[i].sum1) {
153           if (verbose > 3)
154             fprintf(stderr,"potential match at %d target=%d %d sum=%08x\n",
155                     offset,j,i,sum);
156
157           if (!done_csum2) {
158             get_checksum2(buf+offset,MIN(s->n,len-offset),sum2);
159             done_csum2 = 1;
160           }
161           if (memcmp(sum2,s->sums[i].sum2,SUM_LENGTH) == 0) {
162             matched(f,s,buf,len,offset,i);
163             offset += s->sums[i].len - 1;
164             k = MIN((len-offset), s->n);
165             sum = get_checksum1(buf+offset, k);
166             s1 = sum & 0xFFFF;
167             s2 = sum >> 16;
168             ++matches;
169             break;
170           } else {
171             false_alarms++;
172           }
173         }
174         j++;
175       } while (j<s->count && targets[j].t == t);
176     }
177
178     /* Trim off the first byte from the checksum */
179     s1 -= buf[offset];
180     s2 -= k * buf[offset];
181
182     /* Add on the next byte (if there is one) to the checksum */
183     if (k < (len-offset)) {
184       s1 += buf[offset+k];
185       s2 += s1;
186     } else {
187       --k;
188     }
189
190     if (verbose > 3) 
191       fprintf(stderr,"s2:s1 = %.4x%.4x sum=%.8x k=%d offset=%d took %x added %x\n",
192               s2&0xffff, s1&0xffff, get_checksum1(buf+offset+1,k),
193               k, (int)offset, buf[offset], buf[offset+k]);
194   } while (++offset < end);
195
196   matched(f,s,buf,len,len,-1);
197 }
198
199
200 void match_sums(int f,struct sum_struct *s,char *buf,off_t len)
201 {
202   last_match = 0;
203   false_alarms = 0;
204   tag_hits = 0;
205   matches=0;
206   data_transfer=0;
207
208   if (len > 0 && s->count>0) {
209     build_hash_table(s);
210
211     if (verbose > 2) 
212       fprintf(stderr,"built hash table\n");
213
214     hash_search(f,s,buf,len);
215
216     if (verbose > 2) 
217       fprintf(stderr,"done hash search\n");
218   } else {
219     matched(f,s,buf,len,len,-1);
220   }
221
222   if (targets) {
223     free(targets);
224     targets=NULL;
225   }
226
227   if (verbose > 2)
228     fprintf(stderr, "false_alarms=%d tag_hits=%d matches=%d\n",
229             false_alarms, tag_hits, matches);
230
231   total_tag_hits += tag_hits;
232   total_false_alarms += false_alarms;
233   total_matches += matches;
234   total_data_transfer += data_transfer;
235 }
236
237 void match_report(void)
238 {
239   if (verbose <= 1)
240     return;
241
242   fprintf(am_server?stderr:stdout,
243           "total: matches=%d  tag_hits=%d  false_alarms=%d  data=%d\n",
244           total_matches,total_tag_hits,
245           total_false_alarms,total_data_transfer);
246 }