aboutsummaryrefslogtreecommitdiff
path: root/src/set/gnunet-set-ibf-profiler.c
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2013-06-19 10:48:54 +0000
committerFlorian Dold <florian.dold@gmail.com>2013-06-19 10:48:54 +0000
commita900b29ddaa9ea46c731b054b5e3ef3e725b95a8 (patch)
tree52e1a9697b0abf4618cd5684359ec5f0a040898a /src/set/gnunet-set-ibf-profiler.c
parent17353bc0a47c89bda205f23e7995377c9bfe7769 (diff)
downloadgnunet-a900b29ddaa9ea46c731b054b5e3ef3e725b95a8.tar.gz
gnunet-a900b29ddaa9ea46c731b054b5e3ef3e725b95a8.zip
- opaque mq structs
- mq for mesh - faster hashing for IBFs - mesh replaces stream in set - new set profiler (work in progress)
Diffstat (limited to 'src/set/gnunet-set-ibf-profiler.c')
-rw-r--r--src/set/gnunet-set-ibf-profiler.c245
1 files changed, 245 insertions, 0 deletions
diff --git a/src/set/gnunet-set-ibf-profiler.c b/src/set/gnunet-set-ibf-profiler.c
new file mode 100644
index 000000000..92feb3db4
--- /dev/null
+++ b/src/set/gnunet-set-ibf-profiler.c
@@ -0,0 +1,245 @@
1/*
2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/**
22 * @file set/gnunet-set-ibf-profiler.c
23 * @brief tool for profiling the invertible bloom filter implementation
24 * @author Florian Dold
25 */
26
27
28#include "platform.h"
29#include "gnunet_common.h"
30#include "gnunet_container_lib.h"
31#include "gnunet_util_lib.h"
32
33#include "ibf.h"
34
35static unsigned int asize = 10;
36static unsigned int bsize = 10;
37static unsigned int csize = 10;
38static unsigned int hash_num = 4;
39static unsigned int ibf_size = 80;
40
41/* FIXME: add parameter for this */
42static enum GNUNET_CRYPTO_Quality random_quality = GNUNET_CRYPTO_QUALITY_WEAK;
43
44static struct GNUNET_CONTAINER_MultiHashMap *set_a;
45static struct GNUNET_CONTAINER_MultiHashMap *set_b;
46/* common elements in a and b */
47static struct GNUNET_CONTAINER_MultiHashMap *set_c;
48
49static struct GNUNET_CONTAINER_MultiHashMap *key_to_hashcode;
50
51static struct InvertibleBloomFilter *ibf_a;
52static struct InvertibleBloomFilter *ibf_b;
53
54
55static void
56register_hashcode (struct GNUNET_HashCode *hash)
57{
58 struct GNUNET_HashCode replicated;
59 struct IBF_Key key;
60 key = ibf_key_from_hashcode (hash);
61 ibf_hashcode_from_key (key, &replicated);
62 GNUNET_CONTAINER_multihashmap_put (key_to_hashcode, &replicated, GNUNET_memdup (hash, sizeof *hash),
63 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
64}
65
66static void
67iter_hashcodes (struct IBF_Key key, GNUNET_CONTAINER_HashMapIterator iter, void *cls)
68{
69 struct GNUNET_HashCode replicated;
70 ibf_hashcode_from_key (key, &replicated);
71 GNUNET_CONTAINER_multihashmap_get_multiple (key_to_hashcode, &replicated, iter, cls);
72}
73
74
75static int
76insert_iterator (void *cls,
77 const struct GNUNET_HashCode *key,
78 void *value)
79{
80 struct InvertibleBloomFilter *ibf = (struct InvertibleBloomFilter *) cls;
81 ibf_insert (ibf, ibf_key_from_hashcode (key));
82 return GNUNET_YES;
83}
84
85
86static int
87remove_iterator (void *cls,
88 const struct GNUNET_HashCode *key,
89 void *value)
90{
91 struct GNUNET_CONTAINER_MultiHashMap *hashmap = cls;
92 /* if remove fails, there just was a collision with another key */
93 (void) GNUNET_CONTAINER_multihashmap_remove (hashmap, value, NULL);
94 return GNUNET_YES;
95}
96
97
98static void
99run (void *cls, char *const *args, const char *cfgfile,
100 const struct GNUNET_CONFIGURATION_Handle *cfg)
101{
102 struct GNUNET_HashCode id;
103 struct IBF_Key ibf_key;
104 int i;
105 int side;
106 int res;
107 struct GNUNET_TIME_Absolute start_time;
108 struct GNUNET_TIME_Relative delta_time;
109
110 set_a = GNUNET_CONTAINER_multihashmap_create (((asize == 0) ? 1 : (asize + csize)),
111 GNUNET_NO);
112 set_b = GNUNET_CONTAINER_multihashmap_create (((bsize == 0) ? 1 : (bsize + csize)),
113 GNUNET_NO);
114 set_c = GNUNET_CONTAINER_multihashmap_create (((csize == 0) ? 1 : csize),
115 GNUNET_NO);
116
117 key_to_hashcode = GNUNET_CONTAINER_multihashmap_create (((asize+bsize+csize == 0) ? 1 : (asize+bsize+csize)),
118 GNUNET_NO);
119
120 printf ("hash-num=%u, size=%u, #(A-B)=%u, #(B-A)=%u, #(A&B)=%u\n",
121 hash_num, ibf_size, asize, bsize, csize);
122
123 i = 0;
124 while (i < asize)
125 {
126 GNUNET_CRYPTO_hash_create_random (random_quality, &id);
127 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id))
128 continue;
129 GNUNET_CONTAINER_multihashmap_put (
130 set_a, &id, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
131 register_hashcode (&id);
132 i++;
133 }
134 i = 0;
135 while (i < bsize)
136 {
137 GNUNET_CRYPTO_hash_create_random (random_quality, &id);
138 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id))
139 continue;
140 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_b, &id))
141 continue;
142 GNUNET_CONTAINER_multihashmap_put (
143 set_b, &id, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
144 register_hashcode (&id);
145 i++;
146 }
147 i = 0;
148 while (i < csize)
149 {
150 GNUNET_CRYPTO_hash_create_random (random_quality, &id);
151 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id))
152 continue;
153 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_b, &id))
154 continue;
155 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_c, &id))
156 continue;
157 GNUNET_CONTAINER_multihashmap_put (
158 set_c, &id, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
159 register_hashcode (&id);
160 i++;
161 }
162
163 ibf_a = ibf_create (ibf_size, hash_num);
164 ibf_b = ibf_create (ibf_size, hash_num);
165
166 printf ("generated sets\n");
167
168 start_time = GNUNET_TIME_absolute_get ();
169
170 GNUNET_CONTAINER_multihashmap_iterate (set_a, &insert_iterator, ibf_a);
171 GNUNET_CONTAINER_multihashmap_iterate (set_b, &insert_iterator, ibf_b);
172 GNUNET_CONTAINER_multihashmap_iterate (set_c, &insert_iterator, ibf_a);
173 GNUNET_CONTAINER_multihashmap_iterate (set_c, &insert_iterator, ibf_b);
174
175 delta_time = GNUNET_TIME_absolute_get_duration (start_time);
176
177 printf ("encoded in: %s\n", GNUNET_STRINGS_relative_time_to_string (delta_time, GNUNET_NO));
178
179 ibf_subtract (ibf_a, ibf_b);
180
181
182 start_time = GNUNET_TIME_absolute_get ();
183
184 for (i = 0; i <= asize + bsize; i++)
185 {
186 res = ibf_decode (ibf_a, &side, &ibf_key);
187 if (GNUNET_SYSERR == res)
188 {
189 printf ("decode failed, %u/%u elements left\n",
190 GNUNET_CONTAINER_multihashmap_size (set_a) + GNUNET_CONTAINER_multihashmap_size (set_b),
191 asize + bsize);
192 return;
193 }
194 if (GNUNET_NO == res)
195 {
196 if ((0 == GNUNET_CONTAINER_multihashmap_size (set_b)) &&
197 (0 == GNUNET_CONTAINER_multihashmap_size (set_a)))
198 {
199 delta_time = GNUNET_TIME_absolute_get_duration (start_time);
200 printf ("decoded successfully in: %s\n", GNUNET_STRINGS_relative_time_to_string (delta_time, GNUNET_NO));
201 }
202 else
203 {
204 printf ("decode missed elements (should never happen)\n");
205 }
206 return;
207 }
208
209 if (side == 1)
210 iter_hashcodes (ibf_key, remove_iterator, set_a);
211 if (side == -1)
212 iter_hashcodes (ibf_key, remove_iterator, set_b);
213 }
214 printf("cyclic IBF, %u/%u elements left\n",
215 GNUNET_CONTAINER_multihashmap_size (set_a) + GNUNET_CONTAINER_multihashmap_size (set_b),
216 asize + bsize);
217}
218
219int
220main (int argc, char **argv)
221{
222 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
223 {'A', "asize", NULL,
224 gettext_noop ("number of element in set A-B"), 1,
225 &GNUNET_GETOPT_set_uint, &asize},
226 {'B', "bsize", NULL,
227 gettext_noop ("number of element in set B-A"), 1,
228 &GNUNET_GETOPT_set_uint, &bsize},
229 {'C', "csize", NULL,
230 gettext_noop ("number of common elements in A and B"), 1,
231 &GNUNET_GETOPT_set_uint, &csize},
232 {'k', "hash-num", NULL,
233 gettext_noop ("hash num"), 1,
234 &GNUNET_GETOPT_set_uint, &hash_num},
235 {'s', "ibf-size", NULL,
236 gettext_noop ("ibf size"), 1,
237 &GNUNET_GETOPT_set_uint, &ibf_size},
238 GNUNET_GETOPT_OPTION_END
239 };
240 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-ibf",
241 "help",
242 options, &run, NULL, GNUNET_YES);
243 return 0;
244}
245