summaryrefslogtreecommitdiff
path: root/src/set/gnunet-set-ibf-profiler.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/set/gnunet-set-ibf-profiler.c')
-rw-r--r--src/set/gnunet-set-ibf-profiler.c362
1 files changed, 182 insertions, 180 deletions
diff --git a/src/set/gnunet-set-ibf-profiler.c b/src/set/gnunet-set-ibf-profiler.c
index 1e3f78595..c54557fb0 100644
--- a/src/set/gnunet-set-ibf-profiler.c
+++ b/src/set/gnunet-set-ibf-profiler.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file set/gnunet-set-ibf-profiler.c 22 * @file set/gnunet-set-ibf-profiler.c
@@ -50,60 +50,62 @@ static struct InvertibleBloomFilter *ibf_b;
50 50
51 51
52static void 52static void
53register_hashcode (struct GNUNET_HashCode *hash) 53register_hashcode(struct GNUNET_HashCode *hash)
54{ 54{
55 struct GNUNET_HashCode replicated; 55 struct GNUNET_HashCode replicated;
56 struct IBF_Key key; 56 struct IBF_Key key;
57 key = ibf_key_from_hashcode (hash); 57
58 ibf_hashcode_from_key (key, &replicated); 58 key = ibf_key_from_hashcode(hash);
59 (void) GNUNET_CONTAINER_multihashmap_put ( 59 ibf_hashcode_from_key(key, &replicated);
60 (void)GNUNET_CONTAINER_multihashmap_put(
60 key_to_hashcode, 61 key_to_hashcode,
61 &replicated, 62 &replicated,
62 GNUNET_memdup (hash, sizeof *hash), 63 GNUNET_memdup(hash, sizeof *hash),
63 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 64 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
64} 65}
65 66
66 67
67static void 68static void
68iter_hashcodes (struct IBF_Key key, 69iter_hashcodes(struct IBF_Key key,
69 GNUNET_CONTAINER_MulitHashMapIteratorCallback iter, 70 GNUNET_CONTAINER_MulitHashMapIteratorCallback iter,
70 void *cls) 71 void *cls)
71{ 72{
72 struct GNUNET_HashCode replicated; 73 struct GNUNET_HashCode replicated;
73 74
74 ibf_hashcode_from_key (key, &replicated); 75 ibf_hashcode_from_key(key, &replicated);
75 GNUNET_CONTAINER_multihashmap_get_multiple (key_to_hashcode, 76 GNUNET_CONTAINER_multihashmap_get_multiple(key_to_hashcode,
76 &replicated, 77 &replicated,
77 iter, 78 iter,
78 cls); 79 cls);
79} 80}
80 81
81 82
82static int 83static int
83insert_iterator (void *cls, const struct GNUNET_HashCode *key, void *value) 84insert_iterator(void *cls, const struct GNUNET_HashCode *key, void *value)
84{ 85{
85 struct InvertibleBloomFilter *ibf = cls; 86 struct InvertibleBloomFilter *ibf = cls;
86 87
87 ibf_insert (ibf, ibf_key_from_hashcode (key)); 88 ibf_insert(ibf, ibf_key_from_hashcode(key));
88 return GNUNET_YES; 89 return GNUNET_YES;
89} 90}
90 91
91 92
92static int 93static int
93remove_iterator (void *cls, const struct GNUNET_HashCode *key, void *value) 94remove_iterator(void *cls, const struct GNUNET_HashCode *key, void *value)
94{ 95{
95 struct GNUNET_CONTAINER_MultiHashMap *hashmap = cls; 96 struct GNUNET_CONTAINER_MultiHashMap *hashmap = cls;
97
96 /* if remove fails, there just was a collision with another key */ 98 /* if remove fails, there just was a collision with another key */
97 (void) GNUNET_CONTAINER_multihashmap_remove (hashmap, value, NULL); 99 (void)GNUNET_CONTAINER_multihashmap_remove(hashmap, value, NULL);
98 return GNUNET_YES; 100 return GNUNET_YES;
99} 101}
100 102
101 103
102static void 104static void
103run (void *cls, 105run(void *cls,
104 char *const *args, 106 char *const *args,
105 const char *cfgfile, 107 const char *cfgfile,
106 const struct GNUNET_CONFIGURATION_Handle *cfg) 108 const struct GNUNET_CONFIGURATION_Handle *cfg)
107{ 109{
108 struct GNUNET_HashCode id; 110 struct GNUNET_HashCode id;
109 struct IBF_Key ibf_key; 111 struct IBF_Key ibf_key;
@@ -114,193 +116,193 @@ run (void *cls,
114 struct GNUNET_TIME_Relative delta_time; 116 struct GNUNET_TIME_Relative delta_time;
115 117
116 set_a = 118 set_a =
117 GNUNET_CONTAINER_multihashmap_create (((asize == 0) ? 1 : (asize + csize)), 119 GNUNET_CONTAINER_multihashmap_create(((asize == 0) ? 1 : (asize + csize)),
118 GNUNET_NO); 120 GNUNET_NO);
119 set_b = 121 set_b =
120 GNUNET_CONTAINER_multihashmap_create (((bsize == 0) ? 1 : (bsize + csize)), 122 GNUNET_CONTAINER_multihashmap_create(((bsize == 0) ? 1 : (bsize + csize)),
121 GNUNET_NO); 123 GNUNET_NO);
122 set_c = GNUNET_CONTAINER_multihashmap_create (((csize == 0) ? 1 : csize), 124 set_c = GNUNET_CONTAINER_multihashmap_create(((csize == 0) ? 1 : csize),
123 GNUNET_NO); 125 GNUNET_NO);
124 126
125 key_to_hashcode = 127 key_to_hashcode =
126 GNUNET_CONTAINER_multihashmap_create (((asize + bsize + csize == 0) 128 GNUNET_CONTAINER_multihashmap_create(((asize + bsize + csize == 0)
127 ? 1 129 ? 1
128 : (asize + bsize + csize)), 130 : (asize + bsize + csize)),
129 GNUNET_NO); 131 GNUNET_NO);
130 132
131 printf ("hash-num=%u, size=%u, #(A-B)=%u, #(B-A)=%u, #(A&B)=%u\n", 133 printf("hash-num=%u, size=%u, #(A-B)=%u, #(B-A)=%u, #(A&B)=%u\n",
132 hash_num, 134 hash_num,
133 ibf_size, 135 ibf_size,
134 asize, 136 asize,
135 bsize, 137 bsize,
136 csize); 138 csize);
137 139
138 i = 0; 140 i = 0;
139 while (i < asize) 141 while (i < asize)
140 { 142 {
141 GNUNET_CRYPTO_hash_create_random (random_quality, &id); 143 GNUNET_CRYPTO_hash_create_random(random_quality, &id);
142 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id)) 144 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(set_a, &id))
143 continue; 145 continue;
144 GNUNET_break (GNUNET_OK == 146 GNUNET_break(GNUNET_OK ==
145 GNUNET_CONTAINER_multihashmap_put ( 147 GNUNET_CONTAINER_multihashmap_put(
146 set_a, 148 set_a,
147 &id, 149 &id,
148 NULL, 150 NULL,
149 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 151 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
150 register_hashcode (&id); 152 register_hashcode(&id);
151 i++; 153 i++;
152 } 154 }
153 i = 0; 155 i = 0;
154 while (i < bsize) 156 while (i < bsize)
155 { 157 {
156 GNUNET_CRYPTO_hash_create_random (random_quality, &id); 158 GNUNET_CRYPTO_hash_create_random(random_quality, &id);
157 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id)) 159 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(set_a, &id))
158 continue; 160 continue;
159 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_b, &id)) 161 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(set_b, &id))
160 continue; 162 continue;
161 GNUNET_break (GNUNET_OK == 163 GNUNET_break(GNUNET_OK ==
162 GNUNET_CONTAINER_multihashmap_put ( 164 GNUNET_CONTAINER_multihashmap_put(
163 set_b, 165 set_b,
164 &id, 166 &id,
165 NULL, 167 NULL,
166 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 168 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
167 register_hashcode (&id); 169 register_hashcode(&id);
168 i++; 170 i++;
169 } 171 }
170 i = 0; 172 i = 0;
171 while (i < csize) 173 while (i < csize)
172 { 174 {
173 GNUNET_CRYPTO_hash_create_random (random_quality, &id); 175 GNUNET_CRYPTO_hash_create_random(random_quality, &id);
174 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id)) 176 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(set_a, &id))
175 continue; 177 continue;
176 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_b, &id)) 178 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(set_b, &id))
177 continue; 179 continue;
178 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_c, &id)) 180 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(set_c, &id))
179 continue; 181 continue;
180 GNUNET_break (GNUNET_OK == 182 GNUNET_break(GNUNET_OK ==
181 GNUNET_CONTAINER_multihashmap_put ( 183 GNUNET_CONTAINER_multihashmap_put(
182 set_c, 184 set_c,
183 &id, 185 &id,
184 NULL, 186 NULL,
185 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 187 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
186 register_hashcode (&id); 188 register_hashcode(&id);
187 i++; 189 i++;
188 } 190 }
189 191
190 ibf_a = ibf_create (ibf_size, hash_num); 192 ibf_a = ibf_create(ibf_size, hash_num);
191 ibf_b = ibf_create (ibf_size, hash_num); 193 ibf_b = ibf_create(ibf_size, hash_num);
192 if ((NULL == ibf_a) || (NULL == ibf_b)) 194 if ((NULL == ibf_a) || (NULL == ibf_b))
193 { 195 {
194 /* insufficient memory */ 196 /* insufficient memory */
195 GNUNET_break (0); 197 GNUNET_break(0);
196 GNUNET_SCHEDULER_shutdown (); 198 GNUNET_SCHEDULER_shutdown();
197 return; 199 return;
198 } 200 }
199 201
200 202
201 printf ("generated sets\n"); 203 printf("generated sets\n");
202 204
203 start_time = GNUNET_TIME_absolute_get (); 205 start_time = GNUNET_TIME_absolute_get();
204 206
205 GNUNET_CONTAINER_multihashmap_iterate (set_a, &insert_iterator, ibf_a); 207 GNUNET_CONTAINER_multihashmap_iterate(set_a, &insert_iterator, ibf_a);
206 GNUNET_CONTAINER_multihashmap_iterate (set_b, &insert_iterator, ibf_b); 208 GNUNET_CONTAINER_multihashmap_iterate(set_b, &insert_iterator, ibf_b);
207 GNUNET_CONTAINER_multihashmap_iterate (set_c, &insert_iterator, ibf_a); 209 GNUNET_CONTAINER_multihashmap_iterate(set_c, &insert_iterator, ibf_a);
208 GNUNET_CONTAINER_multihashmap_iterate (set_c, &insert_iterator, ibf_b); 210 GNUNET_CONTAINER_multihashmap_iterate(set_c, &insert_iterator, ibf_b);
209 211
210 delta_time = GNUNET_TIME_absolute_get_duration (start_time); 212 delta_time = GNUNET_TIME_absolute_get_duration(start_time);
211 213
212 printf ("encoded in: %s\n", 214 printf("encoded in: %s\n",
213 GNUNET_STRINGS_relative_time_to_string (delta_time, GNUNET_NO)); 215 GNUNET_STRINGS_relative_time_to_string(delta_time, GNUNET_NO));
214 216
215 ibf_subtract (ibf_a, ibf_b); 217 ibf_subtract(ibf_a, ibf_b);
216 218
217 219
218 start_time = GNUNET_TIME_absolute_get (); 220 start_time = GNUNET_TIME_absolute_get();
219 221
220 for (i = 0; i <= asize + bsize; i++) 222 for (i = 0; i <= asize + bsize; i++)
221 {
222 res = ibf_decode (ibf_a, &side, &ibf_key);
223 if (GNUNET_SYSERR == res)
224 { 223 {
225 printf ("decode failed, %u/%u elements left\n", 224 res = ibf_decode(ibf_a, &side, &ibf_key);
226 GNUNET_CONTAINER_multihashmap_size (set_a) + 225 if (GNUNET_SYSERR == res)
227 GNUNET_CONTAINER_multihashmap_size (set_b), 226 {
228 asize + bsize); 227 printf("decode failed, %u/%u elements left\n",
229 return; 228 GNUNET_CONTAINER_multihashmap_size(set_a) +
229 GNUNET_CONTAINER_multihashmap_size(set_b),
230 asize + bsize);
231 return;
232 }
233 if (GNUNET_NO == res)
234 {
235 if ((0 == GNUNET_CONTAINER_multihashmap_size(set_b)) &&
236 (0 == GNUNET_CONTAINER_multihashmap_size(set_a)))
237 {
238 delta_time = GNUNET_TIME_absolute_get_duration(start_time);
239 printf("decoded successfully in: %s\n",
240 GNUNET_STRINGS_relative_time_to_string(delta_time, GNUNET_NO));
241 }
242 else
243 {
244 printf("decode missed elements (should never happen)\n");
245 }
246 return;
247 }
248
249 if (side == 1)
250 iter_hashcodes(ibf_key, remove_iterator, set_a);
251 if (side == -1)
252 iter_hashcodes(ibf_key, remove_iterator, set_b);
230 } 253 }
231 if (GNUNET_NO == res) 254 printf("cyclic IBF, %u/%u elements left\n",
232 { 255 GNUNET_CONTAINER_multihashmap_size(set_a) +
233 if ((0 == GNUNET_CONTAINER_multihashmap_size (set_b)) && 256 GNUNET_CONTAINER_multihashmap_size(set_b),
234 (0 == GNUNET_CONTAINER_multihashmap_size (set_a))) 257 asize + bsize);
235 {
236 delta_time = GNUNET_TIME_absolute_get_duration (start_time);
237 printf ("decoded successfully in: %s\n",
238 GNUNET_STRINGS_relative_time_to_string (delta_time, GNUNET_NO));
239 }
240 else
241 {
242 printf ("decode missed elements (should never happen)\n");
243 }
244 return;
245 }
246
247 if (side == 1)
248 iter_hashcodes (ibf_key, remove_iterator, set_a);
249 if (side == -1)
250 iter_hashcodes (ibf_key, remove_iterator, set_b);
251 }
252 printf ("cyclic IBF, %u/%u elements left\n",
253 GNUNET_CONTAINER_multihashmap_size (set_a) +
254 GNUNET_CONTAINER_multihashmap_size (set_b),
255 asize + bsize);
256} 258}
257 259
258 260
259int 261int
260main (int argc, char **argv) 262main(int argc, char **argv)
261{ 263{
262 struct GNUNET_GETOPT_CommandLineOption options[] = { 264 struct GNUNET_GETOPT_CommandLineOption options[] = {
263 265 GNUNET_GETOPT_option_uint('A',
264 GNUNET_GETOPT_option_uint ('A', 266 "asize",
265 "asize", 267 NULL,
266 NULL, 268 gettext_noop("number of element in set A-B"),
267 gettext_noop ("number of element in set A-B"), 269 &asize),
268 &asize), 270
269 271 GNUNET_GETOPT_option_uint('B',
270 GNUNET_GETOPT_option_uint ('B', 272 "bsize",
271 "bsize", 273 NULL,
272 NULL, 274 gettext_noop("number of element in set B-A"),
273 gettext_noop ("number of element in set B-A"), 275 &bsize),
274 &bsize), 276
275 277 GNUNET_GETOPT_option_uint('C',
276 GNUNET_GETOPT_option_uint ('C', 278 "csize",
277 "csize", 279 NULL,
278 NULL, 280 gettext_noop(
279 gettext_noop ( 281 "number of common elements in A and B"),
280 "number of common elements in A and B"), 282 &csize),
281 &csize), 283
282 284 GNUNET_GETOPT_option_uint('k',
283 GNUNET_GETOPT_option_uint ('k', 285 "hash-num",
284 "hash-num", 286 NULL,
285 NULL, 287 gettext_noop("hash num"),
286 gettext_noop ("hash num"), 288 &hash_num),
287 &hash_num), 289
288 290 GNUNET_GETOPT_option_uint('s',
289 GNUNET_GETOPT_option_uint ('s', 291 "ibf-size",
290 "ibf-size", 292 NULL,
291 NULL, 293 gettext_noop("ibf size"),
292 gettext_noop ("ibf size"), 294 &ibf_size),
293 &ibf_size), 295
294 296 GNUNET_GETOPT_OPTION_END
295 GNUNET_GETOPT_OPTION_END}; 297 };
296 298
297 GNUNET_PROGRAM_run2 (argc, 299 GNUNET_PROGRAM_run2(argc,
298 argv, 300 argv,
299 "gnunet-consensus-ibf", 301 "gnunet-consensus-ibf",
300 "help", 302 "help",
301 options, 303 options,
302 &run, 304 &run,
303 NULL, 305 NULL,
304 GNUNET_YES); 306 GNUNET_YES);
305 return 0; 307 return 0;
306} 308}