gnunet-set-ibf-profiler.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012 GNUnet e.V.
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @file set/gnunet-set-ibf-profiler.c
  19. * @brief tool for profiling the invertible bloom filter implementation
  20. * @author Florian Dold
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "ibf.h"
  25. static unsigned int asize = 10;
  26. static unsigned int bsize = 10;
  27. static unsigned int csize = 10;
  28. static unsigned int hash_num = 4;
  29. static unsigned int ibf_size = 80;
  30. /* FIXME: add parameter for this */
  31. static enum GNUNET_CRYPTO_Quality random_quality = GNUNET_CRYPTO_QUALITY_WEAK;
  32. static struct GNUNET_CONTAINER_MultiHashMap *set_a;
  33. static struct GNUNET_CONTAINER_MultiHashMap *set_b;
  34. /* common elements in a and b */
  35. static struct GNUNET_CONTAINER_MultiHashMap *set_c;
  36. static struct GNUNET_CONTAINER_MultiHashMap *key_to_hashcode;
  37. static struct InvertibleBloomFilter *ibf_a;
  38. static struct InvertibleBloomFilter *ibf_b;
  39. static void
  40. register_hashcode (struct GNUNET_HashCode *hash)
  41. {
  42. struct GNUNET_HashCode replicated;
  43. struct IBF_Key key;
  44. key = ibf_key_from_hashcode (hash);
  45. ibf_hashcode_from_key (key, &replicated);
  46. (void) GNUNET_CONTAINER_multihashmap_put (key_to_hashcode,
  47. &replicated,
  48. GNUNET_memdup (hash, sizeof *hash),
  49. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  50. }
  51. static void
  52. iter_hashcodes (struct IBF_Key key,
  53. GNUNET_CONTAINER_HashMapIterator iter,
  54. void *cls)
  55. {
  56. struct GNUNET_HashCode replicated;
  57. ibf_hashcode_from_key (key, &replicated);
  58. GNUNET_CONTAINER_multihashmap_get_multiple (key_to_hashcode,
  59. &replicated,
  60. iter, cls);
  61. }
  62. static int
  63. insert_iterator (void *cls,
  64. const struct GNUNET_HashCode *key,
  65. void *value)
  66. {
  67. struct InvertibleBloomFilter *ibf = cls;
  68. ibf_insert (ibf, ibf_key_from_hashcode (key));
  69. return GNUNET_YES;
  70. }
  71. static int
  72. remove_iterator (void *cls,
  73. const struct GNUNET_HashCode *key,
  74. void *value)
  75. {
  76. struct GNUNET_CONTAINER_MultiHashMap *hashmap = cls;
  77. /* if remove fails, there just was a collision with another key */
  78. (void) GNUNET_CONTAINER_multihashmap_remove (hashmap, value, NULL);
  79. return GNUNET_YES;
  80. }
  81. static void
  82. run (void *cls,
  83. char *const *args,
  84. const char *cfgfile,
  85. const struct GNUNET_CONFIGURATION_Handle *cfg)
  86. {
  87. struct GNUNET_HashCode id;
  88. struct IBF_Key ibf_key;
  89. int i;
  90. int side;
  91. int res;
  92. struct GNUNET_TIME_Absolute start_time;
  93. struct GNUNET_TIME_Relative delta_time;
  94. set_a = GNUNET_CONTAINER_multihashmap_create (((asize == 0) ? 1 : (asize + csize)),
  95. GNUNET_NO);
  96. set_b = GNUNET_CONTAINER_multihashmap_create (((bsize == 0) ? 1 : (bsize + csize)),
  97. GNUNET_NO);
  98. set_c = GNUNET_CONTAINER_multihashmap_create (((csize == 0) ? 1 : csize),
  99. GNUNET_NO);
  100. key_to_hashcode = GNUNET_CONTAINER_multihashmap_create (((asize+bsize+csize == 0) ? 1 : (asize+bsize+csize)),
  101. GNUNET_NO);
  102. printf ("hash-num=%u, size=%u, #(A-B)=%u, #(B-A)=%u, #(A&B)=%u\n",
  103. hash_num, ibf_size, asize, bsize, csize);
  104. i = 0;
  105. while (i < asize)
  106. {
  107. GNUNET_CRYPTO_hash_create_random (random_quality, &id);
  108. if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id))
  109. continue;
  110. GNUNET_break (GNUNET_OK ==
  111. GNUNET_CONTAINER_multihashmap_put (set_a, &id, NULL,
  112. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  113. register_hashcode (&id);
  114. i++;
  115. }
  116. i = 0;
  117. while (i < bsize)
  118. {
  119. GNUNET_CRYPTO_hash_create_random (random_quality, &id);
  120. if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id))
  121. continue;
  122. if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_b, &id))
  123. continue;
  124. GNUNET_break (GNUNET_OK ==
  125. GNUNET_CONTAINER_multihashmap_put (set_b, &id, NULL,
  126. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  127. register_hashcode (&id);
  128. i++;
  129. }
  130. i = 0;
  131. while (i < csize)
  132. {
  133. GNUNET_CRYPTO_hash_create_random (random_quality, &id);
  134. if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id))
  135. continue;
  136. if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_b, &id))
  137. continue;
  138. if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_c, &id))
  139. continue;
  140. GNUNET_break (GNUNET_OK ==
  141. GNUNET_CONTAINER_multihashmap_put (set_c, &id, NULL,
  142. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  143. register_hashcode (&id);
  144. i++;
  145. }
  146. ibf_a = ibf_create (ibf_size, hash_num);
  147. ibf_b = ibf_create (ibf_size, hash_num);
  148. if ( (NULL == ibf_a) ||
  149. (NULL == ibf_b) )
  150. {
  151. /* insufficient memory */
  152. GNUNET_break (0);
  153. GNUNET_SCHEDULER_shutdown ();
  154. return;
  155. }
  156. printf ("generated sets\n");
  157. start_time = GNUNET_TIME_absolute_get ();
  158. GNUNET_CONTAINER_multihashmap_iterate (set_a, &insert_iterator, ibf_a);
  159. GNUNET_CONTAINER_multihashmap_iterate (set_b, &insert_iterator, ibf_b);
  160. GNUNET_CONTAINER_multihashmap_iterate (set_c, &insert_iterator, ibf_a);
  161. GNUNET_CONTAINER_multihashmap_iterate (set_c, &insert_iterator, ibf_b);
  162. delta_time = GNUNET_TIME_absolute_get_duration (start_time);
  163. printf ("encoded in: %s\n",
  164. GNUNET_STRINGS_relative_time_to_string (delta_time,
  165. GNUNET_NO));
  166. ibf_subtract (ibf_a, ibf_b);
  167. start_time = GNUNET_TIME_absolute_get ();
  168. for (i = 0; i <= asize + bsize; i++)
  169. {
  170. res = ibf_decode (ibf_a, &side, &ibf_key);
  171. if (GNUNET_SYSERR == res)
  172. {
  173. printf ("decode failed, %u/%u elements left\n",
  174. GNUNET_CONTAINER_multihashmap_size (set_a) + GNUNET_CONTAINER_multihashmap_size (set_b),
  175. asize + bsize);
  176. return;
  177. }
  178. if (GNUNET_NO == res)
  179. {
  180. if ((0 == GNUNET_CONTAINER_multihashmap_size (set_b)) &&
  181. (0 == GNUNET_CONTAINER_multihashmap_size (set_a)))
  182. {
  183. delta_time = GNUNET_TIME_absolute_get_duration (start_time);
  184. printf ("decoded successfully in: %s\n",
  185. GNUNET_STRINGS_relative_time_to_string (delta_time,
  186. GNUNET_NO));
  187. }
  188. else
  189. {
  190. printf ("decode missed elements (should never happen)\n");
  191. }
  192. return;
  193. }
  194. if (side == 1)
  195. iter_hashcodes (ibf_key, remove_iterator, set_a);
  196. if (side == -1)
  197. iter_hashcodes (ibf_key, remove_iterator, set_b);
  198. }
  199. printf("cyclic IBF, %u/%u elements left\n",
  200. GNUNET_CONTAINER_multihashmap_size (set_a) + GNUNET_CONTAINER_multihashmap_size (set_b),
  201. asize + bsize);
  202. }
  203. int
  204. main (int argc, char **argv)
  205. {
  206. struct GNUNET_GETOPT_CommandLineOption options[] = {
  207. GNUNET_GETOPT_option_uint ('A',
  208. "asize",
  209. NULL,
  210. gettext_noop ("number of element in set A-B"),
  211. &asize),
  212. GNUNET_GETOPT_option_uint ('B',
  213. "bsize",
  214. NULL,
  215. gettext_noop ("number of element in set B-A"),
  216. &bsize),
  217. GNUNET_GETOPT_option_uint ('C',
  218. "csize",
  219. NULL,
  220. gettext_noop ("number of common elements in A and B"),
  221. &csize),
  222. GNUNET_GETOPT_option_uint ('k',
  223. "hash-num",
  224. NULL,
  225. gettext_noop ("hash num"),
  226. &hash_num),
  227. GNUNET_GETOPT_option_uint ('s',
  228. "ibf-size",
  229. NULL,
  230. gettext_noop ("ibf size"),
  231. &ibf_size),
  232. GNUNET_GETOPT_OPTION_END
  233. };
  234. GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-ibf",
  235. "help",
  236. options, &run, NULL, GNUNET_YES);
  237. return 0;
  238. }