Переглянути джерело

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

Jiri Kuthan 23 роки тому
батько
коміт
9df1213c46
3 змінених файлів з 33 додано та 2 видалено
  1. 22 1
      hash_func.c
  2. 3 1
      hash_func.h
  3. 8 0
      main.c

+ 22 - 1
hash_func.c

@@ -20,6 +20,7 @@ extern unsigned short int crc_16_tab[];
 #include "crc.h"
 #include "ut.h"
 
+#ifdef _OBSOLETED
 int old_hash( str  call_id, str cseq_nr )
 {
    int  hash_code = 0;
@@ -69,9 +70,14 @@ int old_hash( str  call_id, str cseq_nr )
     if ( cseq_nr.len>0 )
       for( i=0 ; i<cseq_nr.len ; hash_code+=cseq_nr.s[i++] );
 #endif
+	/* this is a buggy line, see bellow hot to fix it -- as this
+	   code is obsoleted I dont care anymore
+	*/
    return hash_code &= (TABLE_ENTRIES-1); /* TABLE_ENTRIES = 2^k */
 }
 
+#endif
+
 int new_hash( str call_id, str cseq_nr )
 {
 	int hash_code = 0;
@@ -105,7 +111,11 @@ int new_hash( str call_id, str cseq_nr )
 		//hash_code+=crc_32_tab[(cseq_nr.s[i]+hash_code)%243];
 		hash_code+=ccitt_tab[*(cs+i)+123];
 
+#ifdef _BUG
 	hash_code &= (TABLE_ENTRIES-1); /* TABLE_ENTRIES = 2^k */
+#endif
+	/* hash_code conditioning */
+	hash_code=hash_code%(TABLE_ENTRIES-1)+1;
    	return hash_code;
 }
 
@@ -128,12 +138,23 @@ void hashtest_cycle( int hits[TABLE_ENTRIES], char *ip )
 					call_id.len=sprintf( buf1, "%d-%d-%d@%s",(int)i,(int)j,
 						(int)k, ip );
 					cseq.len=sprintf( buf2, "%d", (int)l );
-					printf("%s\t%s\n", buf1, buf2 );
+					/* printf("%s\t%s\n", buf1, buf2 ); */
 					hashv=hash( call_id, cseq );
 					hits[ hashv ]++;
 				}
 }
 
+int init_hash()
+{
+	if (TABLE_ENTRIES != (1<<10)) {
+		LOG(L_WARN, "WARNING: hash function optimized for %d entries\n",
+			1<<10);
+		LOG(L_WARN, "WARNING: use of %d entries may lead "
+			"to unflat distribution\n", TABLE_ENTRIES );
+	}
+	return 1;
+}
+
 void hashtest()
 {
 	int hits[TABLE_ENTRIES];

+ 3 - 1
hash_func.h

@@ -9,12 +9,14 @@
 #include "str.h"
 
 /* always use a power of 2 for hash table size */
-#define T_TABLE_POWER    12
+#define T_TABLE_POWER    10
 #define TABLE_ENTRIES    (1 << (T_TABLE_POWER))
 
 int new_hash( str  call_id, str cseq_nr );
 int old_hash( str  call_id, str cseq_nr );
 
+int init_hash();
+
 #define hash( cid, cseq) new_hash( cid, cseq )
 
 #endif

+ 8 - 0
main.c

@@ -38,6 +38,8 @@
 #include "parser/digest/digest_parser.h"
 #include "fifo_server.h"
 #include "name_alias.h"
+#include "hash_func.h"
+#include "hash_func.h"
 
 
 #include "stats.h"
@@ -873,6 +875,12 @@ int main(int argc, char** argv)
         init_hfname_parser();
 	init_digest_parser();
 
+	/* init hash fucntion */
+	if (init_hash()<0) {
+		LOG(L_ERR, "ERROR: init_hash failed\n");
+		goto error;
+	}
+
 	/*init mallocs (before parsing cfg !)*/
 	if (init_mallocs()==-1)
 		goto error;