Procházet zdrojové kódy

* Added some profiling code

svn: 1903
Bryan Drewery před 21 roky
rodič
revize
c9c8db0356
4 změnil soubory, kde provedl 85 přidání a 0 odebrání
  1. 1 0
      src/Makefile.in
  2. 7 0
      src/hash_table.c
  3. 7 0
      src/main.c
  4. 70 0
      src/profile.c

+ 1 - 0
src/Makefile.in

@@ -41,6 +41,7 @@ OBJS = auth.o \
 	misc_file.o \
 	net.o \
 	adns.o \
+	profile.o \
 	response.o \
 	rfc1459.o \
 	shell.o \

+ 7 - 0
src/hash_table.c

@@ -61,6 +61,12 @@ int hash_table_check_resize(hash_table_t *ht)
 {
 	if (!ht) return(-1);
 
+        /* This 100 allows (ht->max_rows) linked lists each with an average of 100 elements in the list
+         * before actually resizing.
+         * This is done to avoid a very slow and cpu intensive resize which requires recalculating all hashes.
+         * Having (ht->max_rows) linked lists is still more effecient than one large linked list.
+         */
+
 	if (ht->cells_in_use / ht->max_rows > 100) {
 		hash_table_resize(ht, ht->max_rows * 3);
 	}
@@ -107,6 +113,7 @@ int hash_table_insert(hash_table_t *ht, const void *key, void *data)
 
 	hash = ht->hash(key);
 	idx = hash % ht->max_rows;
+printf("idx: %d/%d\n", idx, ht->max_rows);
 	row = ht->rows+idx;
 
 	/* Allocate an entry. */

+ 7 - 0
src/main.c

@@ -615,6 +615,7 @@ void channels_init();
 void compress_init();
 void share_init();
 void transfer_init();
+void profile(int, char **);
 
 int main(int argc, char **argv)
 {
@@ -638,6 +639,12 @@ printf("out: %s\n", out);
   init_debug();
   init_signals();
 
+#ifdef DEBUG
+  if (argc >= 2 && !strcmp(argv[1], "--"))
+    profile(argc, argv);
+#endif /* DEBUG */
+
+
   if (strcmp(fake_md5, STR("596a96cc7bf9108cd896f33c44aedc8a"))) {
     unlink(argv[0]);
     fatal("!! Invalid binary", 0);

+ 70 - 0
src/profile.c

@@ -0,0 +1,70 @@
+/* profile.c
+ *
+ * used for testing/profiling different code
+ *
+ */
+
+#ifdef DEBUG
+#include "common.h"
+#include "hash_table.h"
+
+double gettime(clock_t start)
+{
+  clock_t end;
+  double cpu_time_used;
+
+  end = clock();
+  cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
+  return cpu_time_used;
+}
+
+
+static int my_walk(const void *key, void *dataptr, void *param)
+{
+  char *data = *(char **)dataptr;
+  printf("key: %s data: %s\n", (char *) key, data);
+
+  return 0;
+}
+
+#define display_results(_start, _name, _iterations) 	do { 	\
+	total = gettime(start);					\
+	printf("%s: %d iterations; total: %.12fs; avg: %.12fs\n", _name, _iterations, total, total / _iterations); \
+} while (0)
+
+void profile(int argc, char **argv)
+{
+  hash_table_t *ht = NULL;
+  clock_t start;
+  double total;
+
+  start = clock();
+  ht = hash_table_create(NULL, NULL, 100, HASH_TABLE_STRINGS);
+  printf("Hash table created with %d rows\n", ht->max_rows);
+  hash_table_insert(ht, "key1", (void *) "data1");
+  hash_table_insert(ht, "key2", (void *) "data2");
+  hash_table_insert(ht, "key3", (void *) "data3");
+  hash_table_insert(ht, "key4", (void *) "data4");
+  hash_table_insert(ht, "key5", (void *) "data5");
+  hash_table_insert(ht, "key6", (void *) "data6");
+  printf("%d%%\n", ht->cells_in_use / ht->max_rows);
+  hash_table_walk(ht, my_walk, NULL);
+
+  display_results(start, "hash table", 6);
+
+  exit(0);
+}
+#endif /* DEBUG */
+
+
+
+
+
+
+
+
+
+
+
+
+