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 69
     if ( cseq_nr.len>0 )
70 70
       for( i=0 ; i<cseq_nr.len ; hash_code+=cseq_nr.s[i++] );
71 71
 #endif
72
+	/* this is a buggy line, see bellow hot to fix it -- as this
73
+	   code is obsoleted I dont care anymore
74
+	*/
72 75
    return hash_code &= (TABLE_ENTRIES-1); /* TABLE_ENTRIES = 2^k */
73 76
 }
74 77
 
78
+#endif
79
+
75 80
 int new_hash( str call_id, str cseq_nr )
76 81
 {
77 82
 	int hash_code = 0;
... ...
@@ -105,7 +111,11 @@ int new_hash( str call_id, str cseq_nr )
105 105
 		//hash_code+=crc_32_tab[(cseq_nr.s[i]+hash_code)%243];
106 106
 		hash_code+=ccitt_tab[*(cs+i)+123];
107 107
 
108
+#ifdef _BUG
108 109
 	hash_code &= (TABLE_ENTRIES-1); /* TABLE_ENTRIES = 2^k */
110
+#endif
111
+	/* hash_code conditioning */
112
+	hash_code=hash_code%(TABLE_ENTRIES-1)+1;
109 113
    	return hash_code;
110 114
 }
111 115
 
... ...
@@ -128,12 +138,23 @@ void hashtest_cycle( int hits[TABLE_ENTRIES], char *ip )
128 128
 					call_id.len=sprintf( buf1, "%d-%d-%d@%s",(int)i,(int)j,
129 129
 						(int)k, ip );
130 130
 					cseq.len=sprintf( buf2, "%d", (int)l );
131
-					printf("%s\t%s\n", buf1, buf2 );
131
+					/* printf("%s\t%s\n", buf1, buf2 ); */
132 132
 					hashv=hash( call_id, cseq );
133 133
 					hits[ hashv ]++;
134 134
 				}
135 135
 }
136 136
 
137
+int init_hash()
138
+{
139
+	if (TABLE_ENTRIES != (1<<10)) {
140
+		LOG(L_WARN, "WARNING: hash function optimized for %d entries\n",
141
+			1<<10);
142
+		LOG(L_WARN, "WARNING: use of %d entries may lead "
143
+			"to unflat distribution\n", TABLE_ENTRIES );
144
+	}
145
+	return 1;
146
+}
147
+
137 148
 void hashtest()
138 149
 {
139 150
 	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 873
         init_hfname_parser();
874 874
 	init_digest_parser();
875 875
 
876
+	/* init hash fucntion */
877
+	if (init_hash()<0) {
878
+		LOG(L_ERR, "ERROR: init_hash failed\n");
879
+		goto error;
880
+	}
881
+
876 882
 	/*init mallocs (before parsing cfg !)*/
877 883
 	if (init_mallocs()==-1)
878 884
 		goto error;