2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file set/gnunet-set-ibf-profiler.c
21 * @brief tool for profiling the invertible bloom filter implementation
22 * @author Florian Dold
26 #include "gnunet_util_lib.h"
30 static unsigned int asize = 10;
31 static unsigned int bsize = 10;
32 static unsigned int csize = 10;
33 static unsigned int hash_num = 4;
34 static unsigned int ibf_size = 80;
36 /* FIXME: add parameter for this */
37 static enum GNUNET_CRYPTO_Quality random_quality = GNUNET_CRYPTO_QUALITY_WEAK;
39 static struct GNUNET_CONTAINER_MultiHashMap *set_a;
40 static struct GNUNET_CONTAINER_MultiHashMap *set_b;
41 /* common elements in a and b */
42 static struct GNUNET_CONTAINER_MultiHashMap *set_c;
44 static struct GNUNET_CONTAINER_MultiHashMap *key_to_hashcode;
46 static struct InvertibleBloomFilter *ibf_a;
47 static struct InvertibleBloomFilter *ibf_b;
51 register_hashcode (struct GNUNET_HashCode *hash)
53 struct GNUNET_HashCode replicated;
55 key = ibf_key_from_hashcode (hash);
56 ibf_hashcode_from_key (key, &replicated);
57 (void) GNUNET_CONTAINER_multihashmap_put (key_to_hashcode,
59 GNUNET_memdup (hash, sizeof *hash),
60 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
65 iter_hashcodes (struct IBF_Key key,
66 GNUNET_CONTAINER_HashMapIterator iter,
69 struct GNUNET_HashCode replicated;
71 ibf_hashcode_from_key (key, &replicated);
72 GNUNET_CONTAINER_multihashmap_get_multiple (key_to_hashcode,
79 insert_iterator (void *cls,
80 const struct GNUNET_HashCode *key,
83 struct InvertibleBloomFilter *ibf = cls;
85 ibf_insert (ibf, ibf_key_from_hashcode (key));
91 remove_iterator (void *cls,
92 const struct GNUNET_HashCode *key,
95 struct GNUNET_CONTAINER_MultiHashMap *hashmap = cls;
96 /* if remove fails, there just was a collision with another key */
97 (void) GNUNET_CONTAINER_multihashmap_remove (hashmap, value, NULL);
106 const struct GNUNET_CONFIGURATION_Handle *cfg)
108 struct GNUNET_HashCode id;
109 struct IBF_Key ibf_key;
113 struct GNUNET_TIME_Absolute start_time;
114 struct GNUNET_TIME_Relative delta_time;
116 set_a = GNUNET_CONTAINER_multihashmap_create (((asize == 0) ? 1 : (asize + csize)),
118 set_b = GNUNET_CONTAINER_multihashmap_create (((bsize == 0) ? 1 : (bsize + csize)),
120 set_c = GNUNET_CONTAINER_multihashmap_create (((csize == 0) ? 1 : csize),
123 key_to_hashcode = GNUNET_CONTAINER_multihashmap_create (((asize+bsize+csize == 0) ? 1 : (asize+bsize+csize)),
126 printf ("hash-num=%u, size=%u, #(A-B)=%u, #(B-A)=%u, #(A&B)=%u\n",
127 hash_num, ibf_size, asize, bsize, csize);
132 GNUNET_CRYPTO_hash_create_random (random_quality, &id);
133 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id))
135 GNUNET_break (GNUNET_OK ==
136 GNUNET_CONTAINER_multihashmap_put (set_a, &id, NULL,
137 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
138 register_hashcode (&id);
144 GNUNET_CRYPTO_hash_create_random (random_quality, &id);
145 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id))
147 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_b, &id))
149 GNUNET_break (GNUNET_OK ==
150 GNUNET_CONTAINER_multihashmap_put (set_b, &id, NULL,
151 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
152 register_hashcode (&id);
158 GNUNET_CRYPTO_hash_create_random (random_quality, &id);
159 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id))
161 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_b, &id))
163 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_c, &id))
165 GNUNET_break (GNUNET_OK ==
166 GNUNET_CONTAINER_multihashmap_put (set_c, &id, NULL,
167 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
168 register_hashcode (&id);
172 ibf_a = ibf_create (ibf_size, hash_num);
173 ibf_b = ibf_create (ibf_size, hash_num);
174 if ( (NULL == ibf_a) ||
177 /* insufficient memory */
179 GNUNET_SCHEDULER_shutdown ();
184 printf ("generated sets\n");
186 start_time = GNUNET_TIME_absolute_get ();
188 GNUNET_CONTAINER_multihashmap_iterate (set_a, &insert_iterator, ibf_a);
189 GNUNET_CONTAINER_multihashmap_iterate (set_b, &insert_iterator, ibf_b);
190 GNUNET_CONTAINER_multihashmap_iterate (set_c, &insert_iterator, ibf_a);
191 GNUNET_CONTAINER_multihashmap_iterate (set_c, &insert_iterator, ibf_b);
193 delta_time = GNUNET_TIME_absolute_get_duration (start_time);
195 printf ("encoded in: %s\n",
196 GNUNET_STRINGS_relative_time_to_string (delta_time,
199 ibf_subtract (ibf_a, ibf_b);
202 start_time = GNUNET_TIME_absolute_get ();
204 for (i = 0; i <= asize + bsize; i++)
206 res = ibf_decode (ibf_a, &side, &ibf_key);
207 if (GNUNET_SYSERR == res)
209 printf ("decode failed, %u/%u elements left\n",
210 GNUNET_CONTAINER_multihashmap_size (set_a) + GNUNET_CONTAINER_multihashmap_size (set_b),
214 if (GNUNET_NO == res)
216 if ((0 == GNUNET_CONTAINER_multihashmap_size (set_b)) &&
217 (0 == GNUNET_CONTAINER_multihashmap_size (set_a)))
219 delta_time = GNUNET_TIME_absolute_get_duration (start_time);
220 printf ("decoded successfully in: %s\n",
221 GNUNET_STRINGS_relative_time_to_string (delta_time,
226 printf ("decode missed elements (should never happen)\n");
232 iter_hashcodes (ibf_key, remove_iterator, set_a);
234 iter_hashcodes (ibf_key, remove_iterator, set_b);
236 printf("cyclic IBF, %u/%u elements left\n",
237 GNUNET_CONTAINER_multihashmap_size (set_a) + GNUNET_CONTAINER_multihashmap_size (set_b),
243 main (int argc, char **argv)
245 struct GNUNET_GETOPT_CommandLineOption options[] = {
247 GNUNET_GETOPT_option_uint ('A',
250 gettext_noop ("number of element in set A-B"),
253 GNUNET_GETOPT_option_uint ('B',
256 gettext_noop ("number of element in set B-A"),
259 GNUNET_GETOPT_option_uint ('C',
262 gettext_noop ("number of common elements in A and B"),
265 GNUNET_GETOPT_option_uint ('k',
268 gettext_noop ("hash num"),
271 GNUNET_GETOPT_option_uint ('s',
274 gettext_noop ("ibf size"),
277 GNUNET_GETOPT_OPTION_END
280 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-ibf",
282 options, &run, NULL, GNUNET_YES);