Browse code

bug_fix: hash function's distribution flatened and singular value 0 no more used (caused troubles when generating branch)

Jiri Kuthan authored on 28/08/2002 13:48:50
Showing 3 changed files
... ...
@@ -20,6 +20,7 @@ extern unsigned short int crc_16_tab[];
20 20
 #include "crc.h"
21 21
 #include "ut.h"
22 22
 
23
+#ifdef _OBSOLETED
23 24
 int old_hash( str  call_id, str cseq_nr )
24 25
 {
25 26
    int  hash_code = 0;
... ...
@@ -69,9 +70,14 @@ int old_hash( str  call_id, str cseq_nr )
69 70
     if ( cseq_nr.len>0 )
70 71
       for( i=0 ; i<cseq_nr.len ; hash_code+=cseq_nr.s[i++] );
71 72
 #endif
73
+	/* this is a buggy line, see bellow hot to fix it -- as this
74
+	   code is obsoleted I dont care anymore
75
+	*/
72 76
    return hash_code &= (TABLE_ENTRIES-1); /* TABLE_ENTRIES = 2^k */
73 77
 }
74 78
 
79
+#endif
80
+
75 81
 int new_hash( str call_id, str cseq_nr )
76 82
 {
77 83
 	int hash_code = 0;
... ...
@@ -105,7 +111,11 @@ int new_hash( str call_id, str cseq_nr )
105 111
 		//hash_code+=crc_32_tab[(cseq_nr.s[i]+hash_code)%243];
106 112
 		hash_code+=ccitt_tab[*(cs+i)+123];
107 113
 
114
+#ifdef _BUG
108 115
 	hash_code &= (TABLE_ENTRIES-1); /* TABLE_ENTRIES = 2^k */
116
+#endif
117
+	/* hash_code conditioning */
118
+	hash_code=hash_code%(TABLE_ENTRIES-1)+1;
109 119
    	return hash_code;
110 120
 }
111 121
 
... ...
@@ -128,12 +138,23 @@ void hashtest_cycle( int hits[TABLE_ENTRIES], char *ip )
128 138
 					call_id.len=sprintf( buf1, "%d-%d-%d@%s",(int)i,(int)j,
129 139
 						(int)k, ip );
130 140
 					cseq.len=sprintf( buf2, "%d", (int)l );
131
-					printf("%s\t%s\n", buf1, buf2 );
141
+					/* printf("%s\t%s\n", buf1, buf2 ); */
132 142
 					hashv=hash( call_id, cseq );
133 143
 					hits[ hashv ]++;
134 144
 				}
135 145
 }
136 146
 
147
+int init_hash()
148
+{
149
+	if (TABLE_ENTRIES != (1<<10)) {
150
+		LOG(L_WARN, "WARNING: hash function optimized for %d entries\n",
151
+			1<<10);
152
+		LOG(L_WARN, "WARNING: use of %d entries may lead "
153
+			"to unflat distribution\n", TABLE_ENTRIES );
154
+	}
155
+	return 1;
156
+}
157
+
137 158
 void hashtest()
138 159
 {
139 160
 	int hits[TABLE_ENTRIES];
... ...
@@ -9,12 +9,14 @@
9 9
 #include "str.h"
10 10
 
11 11
 /* always use a power of 2 for hash table size */
12
-#define T_TABLE_POWER    12
12
+#define T_TABLE_POWER    10
13 13
 #define TABLE_ENTRIES    (1 << (T_TABLE_POWER))
14 14
 
15 15
 int new_hash( str  call_id, str cseq_nr );
16 16
 int old_hash( str  call_id, str cseq_nr );
17 17
 
18
+int init_hash();
19
+
18 20
 #define hash( cid, cseq) new_hash( cid, cseq )
19 21
 
20 22
 #endif
... ...
@@ -38,6 +38,8 @@
38 38
 #include "parser/digest/digest_parser.h"
39 39
 #include "fifo_server.h"
40 40
 #include "name_alias.h"
41
+#include "hash_func.h"
42
+#include "hash_func.h"
41 43
 
42 44
 
43 45
 #include "stats.h"
... ...
@@ -873,6 +875,12 @@ int main(int argc, char** argv)
873 875
         init_hfname_parser();
874 876
 	init_digest_parser();
875 877
 
878
+	/* init hash fucntion */
879
+	if (init_hash()<0) {
880
+		LOG(L_ERR, "ERROR: init_hash failed\n");
881
+		goto error;
882
+	}
883
+
876 884
 	/*init mallocs (before parsing cfg !)*/
877 885
 	if (init_mallocs()==-1)
878 886
 		goto error;