2 This file is part of GNUnet
3 Copyright (C) 2013 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-profiler.c
21 * @brief profiling tool for set
22 * @author Florian Dold
25 #include "gnunet_util_lib.h"
26 #include "gnunet_statistics_service.h"
27 #include "gnunet_set_service.h"
28 #include "gnunet_testbed_service.h"
33 static unsigned int num_a = 5;
34 static unsigned int num_b = 5;
35 static unsigned int num_c = 20;
37 static char *op_str = "union";
39 const static struct GNUNET_CONFIGURATION_Handle *config;
44 struct GNUNET_SET_Handle *set;
45 struct GNUNET_SET_OperationHandle *oh;
46 struct GNUNET_CONTAINER_MultiHashMap *sent;
47 struct GNUNET_CONTAINER_MultiHashMap *received;
51 static struct GNUNET_CONTAINER_MultiHashMap *common_sent;
53 static struct GNUNET_HashCode app_id;
55 static struct GNUNET_PeerIdentity local_peer;
57 static struct GNUNET_SET_ListenHandle *set_listener;
60 static unsigned int force_delta;
61 static unsigned int force_full;
62 static unsigned int element_size = 32;
65 * Handle to the statistics service.
67 static struct GNUNET_STATISTICS_Handle *statistics;
70 * The profiler will write statistics
71 * for all peers to the file with this name.
73 static char *statistics_filename;
76 * The profiler will write statistics
77 * for all peers to this file.
79 static FILE *statistics_file;
83 map_remove_iterator (void *cls,
84 const struct GNUNET_HashCode *key,
87 struct GNUNET_CONTAINER_MultiHashMap *m = cls;
90 GNUNET_assert (NULL != key);
92 ret = GNUNET_CONTAINER_multihashmap_remove_all (m, key);
94 printf ("spurious element\n");
101 * Callback function to process statistic values.
104 * @param subsystem name of subsystem that created the statistic
105 * @param name the name of the datum
106 * @param value the current value
107 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
108 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
111 statistics_result (void *cls,
112 const char *subsystem,
117 if (NULL != statistics_file)
119 fprintf (statistics_file, "%s\t%s\t%lu\n", subsystem, name, (unsigned long) value);
126 statistics_done (void *cls,
129 GNUNET_assert (GNUNET_YES == success);
130 if (NULL != statistics_file)
131 fclose (statistics_file);
132 GNUNET_SCHEDULER_shutdown ();
137 check_all_done (void)
139 if (info1.done == GNUNET_NO || info2.done == GNUNET_NO)
142 GNUNET_CONTAINER_multihashmap_iterate (info1.received, map_remove_iterator, info2.sent);
143 GNUNET_CONTAINER_multihashmap_iterate (info2.received, map_remove_iterator, info1.sent);
145 printf ("set a: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info1.sent));
146 printf ("set b: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info2.sent));
148 if (NULL == statistics_filename)
150 GNUNET_SCHEDULER_shutdown ();
154 statistics_file = fopen (statistics_filename, "w");
155 GNUNET_STATISTICS_get (statistics, NULL, NULL,
157 &statistics_result, NULL);
162 set_result_cb (void *cls,
163 const struct GNUNET_SET_Element *element,
164 uint64_t current_size,
165 enum GNUNET_SET_Status status)
167 struct SetInfo *info = cls;
168 struct GNUNET_HashCode hash;
170 GNUNET_assert (GNUNET_NO == info->done);
173 case GNUNET_SET_STATUS_DONE:
174 case GNUNET_SET_STATUS_HALF_DONE:
175 info->done = GNUNET_YES;
176 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s done\n", info->id);
180 case GNUNET_SET_STATUS_FAILURE:
182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failure\n");
183 GNUNET_SCHEDULER_shutdown ();
185 case GNUNET_SET_STATUS_ADD_LOCAL:
186 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: local element\n", info->id);
188 case GNUNET_SET_STATUS_ADD_REMOTE:
189 GNUNET_CRYPTO_hash (element->data, element->size, &hash);
190 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: remote element %s\n", info->id,
192 // XXX: record and check
198 if (element->size != element_size)
200 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
201 "wrong element size: %u, expected %u\n",
203 (unsigned int) sizeof (struct GNUNET_HashCode));
207 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: got element (%s)\n",
208 info->id, GNUNET_h2s (element->data));
209 GNUNET_assert (NULL != element->data);
210 struct GNUNET_HashCode data_hash;
211 GNUNET_CRYPTO_hash (element->data, element_size, &data_hash);
212 GNUNET_CONTAINER_multihashmap_put (info->received,
214 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
219 set_listen_cb (void *cls,
220 const struct GNUNET_PeerIdentity *other_peer,
221 const struct GNUNET_MessageHeader *context_msg,
222 struct GNUNET_SET_Request *request)
224 /* max. 2 options plus terminator */
225 struct GNUNET_SET_Option opts[3] = {{0}};
226 unsigned int n_opts = 0;
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
231 "listener failed\n");
234 GNUNET_assert (NULL == info2.oh);
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "set listen cb called\n");
239 opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_BYZANTINE };
241 GNUNET_assert (!(force_full && force_delta));
244 opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_FULL };
248 opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_DELTA };
251 opts[n_opts].type = 0;
252 info2.oh = GNUNET_SET_accept (request, GNUNET_SET_RESULT_SYMMETRIC,
254 set_result_cb, &info2);
255 GNUNET_SET_commit (info2.oh, info2.set);
260 set_insert_iterator (void *cls,
261 const struct GNUNET_HashCode *key,
264 struct GNUNET_SET_Handle *set = cls;
265 struct GNUNET_SET_Element el;
269 el.size = element_size;
270 GNUNET_SET_add_element (set, &el, NULL, NULL);
276 handle_shutdown (void *cls)
278 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
279 "Shutting down set profiler\n");
280 if (NULL != set_listener)
282 GNUNET_SET_listen_cancel (set_listener);
285 if (NULL != info1.oh)
287 GNUNET_SET_operation_cancel (info1.oh);
290 if (NULL != info2.oh)
292 GNUNET_SET_operation_cancel (info2.oh);
295 if (NULL != info1.set)
297 GNUNET_SET_destroy (info1.set);
300 if (NULL != info2.set)
302 GNUNET_SET_destroy (info2.set);
305 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
311 const struct GNUNET_CONFIGURATION_Handle *cfg,
312 struct GNUNET_TESTING_Peer *peer)
315 struct GNUNET_HashCode hash;
316 /* max. 2 options plus terminator */
317 struct GNUNET_SET_Option opts[3] = {{0}};
318 unsigned int n_opts = 0;
322 GNUNET_assert (element_size > 0);
324 if (GNUNET_OK != GNUNET_CRYPTO_get_peer_identity (cfg, &local_peer))
326 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not retrieve host identity\n");
331 statistics = GNUNET_STATISTICS_create ("set-profiler", cfg);
333 GNUNET_SCHEDULER_add_shutdown (&handle_shutdown, NULL);
338 info1.sent = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
339 info2.sent = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
340 common_sent = GNUNET_CONTAINER_multihashmap_create (num_c+1, GNUNET_NO);
342 info1.received = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
343 info2.received = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
345 for (i = 0; i < num_a; i++)
347 char *data = GNUNET_malloc (element_size);
348 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, data, element_size);
349 GNUNET_CRYPTO_hash (data, element_size, &hash);
350 GNUNET_CONTAINER_multihashmap_put (info1.sent, &hash, data,
351 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
354 for (i = 0; i < num_b; i++)
356 char *data = GNUNET_malloc (element_size);
357 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, data, element_size);
358 GNUNET_CRYPTO_hash (data, element_size, &hash);
359 GNUNET_CONTAINER_multihashmap_put (info2.sent, &hash, data,
360 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
363 for (i = 0; i < num_c; i++)
365 char *data = GNUNET_malloc (element_size);
366 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, data, element_size);
367 GNUNET_CRYPTO_hash (data, element_size, &hash);
368 GNUNET_CONTAINER_multihashmap_put (common_sent, &hash, data,
369 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
372 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &app_id);
374 /* FIXME: also implement intersection etc. */
375 info1.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
376 info2.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
378 GNUNET_CONTAINER_multihashmap_iterate (info1.sent, set_insert_iterator, info1.set);
379 GNUNET_CONTAINER_multihashmap_iterate (info2.sent, set_insert_iterator, info2.set);
380 GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info1.set);
381 GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info2.set);
383 set_listener = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
384 &app_id, set_listen_cb, NULL);
389 opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_BYZANTINE };
391 GNUNET_assert (!(force_full && force_delta));
394 opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_FULL };
398 opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_DELTA };
401 opts[n_opts].type = 0;
403 info1.oh = GNUNET_SET_prepare (&local_peer, &app_id, NULL,
404 GNUNET_SET_RESULT_SYMMETRIC,
406 set_result_cb, &info1);
407 GNUNET_SET_commit (info1.oh, info1.set);
408 GNUNET_SET_destroy (info1.set);
414 pre_run (void *cls, char *const *args, const char *cfgfile,
415 const struct GNUNET_CONFIGURATION_Handle *cfg)
417 if (0 != GNUNET_TESTING_peer_run ("set-profiler",
425 main (int argc, char **argv)
427 struct GNUNET_GETOPT_CommandLineOption options[] = {
428 GNUNET_GETOPT_option_uint ('A',
431 gettext_noop ("number of values"),
434 GNUNET_GETOPT_option_uint ('B',
437 gettext_noop ("number of values"),
440 GNUNET_GETOPT_option_flag ('b',
442 gettext_noop ("use byzantine mode"),
445 GNUNET_GETOPT_option_uint ('f',
448 gettext_noop ("force sending full set"),
451 GNUNET_GETOPT_option_uint ('d',
454 gettext_noop ("number delta operation"),
457 GNUNET_GETOPT_option_uint ('C',
460 gettext_noop ("number of values"),
463 GNUNET_GETOPT_option_string ('x',
466 gettext_noop ("operation to execute"),
469 GNUNET_GETOPT_option_uint ('w',
472 gettext_noop ("element size"),
475 GNUNET_GETOPT_option_filename ('s',
478 gettext_noop ("write statistics to file"),
479 &statistics_filename),
481 GNUNET_GETOPT_OPTION_END
483 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-set-profiler",
485 options, &pre_run, NULL, GNUNET_YES);