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
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file set/gnunet-set-profiler.c
23 * @brief profiling tool for set
24 * @author Florian Dold
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet_set_service.h"
30 #include "gnunet_testbed_service.h"
35 static unsigned int num_a = 5;
36 static unsigned int num_b = 5;
37 static unsigned int num_c = 20;
39 static char *op_str = "union";
41 const static struct GNUNET_CONFIGURATION_Handle *config;
46 struct GNUNET_SET_Handle *set;
47 struct GNUNET_SET_OperationHandle *oh;
48 struct GNUNET_CONTAINER_MultiHashMap *sent;
49 struct GNUNET_CONTAINER_MultiHashMap *received;
53 static struct GNUNET_CONTAINER_MultiHashMap *common_sent;
55 static struct GNUNET_HashCode app_id;
57 static struct GNUNET_PeerIdentity local_peer;
59 static struct GNUNET_SET_ListenHandle *set_listener;
62 static int force_delta;
63 static int force_full;
66 * Handle to the statistics service.
68 static struct GNUNET_STATISTICS_Handle *statistics;
71 * The profiler will write statistics
72 * for all peers to the file with this name.
74 static char *statistics_filename;
77 * The profiler will write statistics
78 * for all peers to this file.
80 static FILE *statistics_file;
84 map_remove_iterator (void *cls,
85 const struct GNUNET_HashCode *key,
88 struct GNUNET_CONTAINER_MultiHashMap *m = cls;
91 GNUNET_assert (NULL != key);
93 ret = GNUNET_CONTAINER_multihashmap_remove (m, key, NULL);
95 printf ("spurious element\n");
102 * Callback function to process statistic values.
105 * @param subsystem name of subsystem that created the statistic
106 * @param name the name of the datum
107 * @param value the current value
108 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
109 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
112 statistics_result (void *cls,
113 const char *subsystem,
118 if (NULL != statistics_file)
120 fprintf (statistics_file, "%s\t%s\t%lu\n", subsystem, name, (unsigned long) value);
127 statistics_done (void *cls,
130 GNUNET_assert (GNUNET_YES == success);
131 if (NULL != statistics_file)
132 fclose (statistics_file);
133 GNUNET_SCHEDULER_shutdown ();
138 check_all_done (void)
140 if (info1.done == GNUNET_NO || info2.done == GNUNET_NO)
143 GNUNET_CONTAINER_multihashmap_iterate (info1.received, map_remove_iterator, info2.sent);
144 GNUNET_CONTAINER_multihashmap_iterate (info2.received, map_remove_iterator, info1.sent);
146 printf ("set a: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info1.sent));
147 printf ("set b: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info2.sent));
149 if (NULL == statistics_filename)
151 GNUNET_SCHEDULER_shutdown ();
155 statistics_file = fopen (statistics_filename, "w");
156 GNUNET_STATISTICS_get (statistics, NULL, NULL,
158 &statistics_result, NULL);
163 set_result_cb (void *cls,
164 const struct GNUNET_SET_Element *element,
165 uint64_t current_size,
166 enum GNUNET_SET_Status status)
168 struct SetInfo *info = cls;
169 struct GNUNET_HashCode hash;
171 GNUNET_assert (GNUNET_NO == info->done);
174 case GNUNET_SET_STATUS_DONE:
175 case GNUNET_SET_STATUS_HALF_DONE:
176 info->done = GNUNET_YES;
177 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s done\n", info->id);
181 case GNUNET_SET_STATUS_FAILURE:
183 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failure\n");
184 GNUNET_SCHEDULER_shutdown ();
186 case GNUNET_SET_STATUS_ADD_LOCAL:
187 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: local element\n", info->id);
189 case GNUNET_SET_STATUS_ADD_REMOTE:
190 GNUNET_CRYPTO_hash (element->data, element->size, &hash);
191 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: remote element %s\n", info->id,
193 // XXX: record and check
199 if (element->size != sizeof (struct GNUNET_HashCode))
201 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
202 "wrong element size: %u, expected %u\n",
204 (unsigned int) sizeof (struct GNUNET_HashCode));
208 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: got element (%s)\n",
209 info->id, GNUNET_h2s (element->data));
210 GNUNET_assert (NULL != element->data);
211 GNUNET_CONTAINER_multihashmap_put (info->received,
213 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
218 set_listen_cb (void *cls,
219 const struct GNUNET_PeerIdentity *other_peer,
220 const struct GNUNET_MessageHeader *context_msg,
221 struct GNUNET_SET_Request *request)
223 /* max. 2 options plus terminator */
224 struct GNUNET_SET_Option opts[3] = {{0}};
225 unsigned int n_opts = 0;
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230 "listener failed\n");
233 GNUNET_assert (NULL == info2.oh);
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235 "set listen cb called\n");
238 opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_BYZANTINE };
240 GNUNET_assert (!(force_full && force_delta));
243 opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_FULL };
247 opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_DELTA };
250 opts[n_opts].type = 0;
251 info2.oh = GNUNET_SET_accept (request, GNUNET_SET_RESULT_SYMMETRIC,
253 set_result_cb, &info2);
254 GNUNET_SET_commit (info2.oh, info2.set);
259 set_insert_iterator (void *cls,
260 const struct GNUNET_HashCode *key,
263 struct GNUNET_SET_Handle *set = cls;
264 struct GNUNET_SET_Element *el;
266 el = GNUNET_malloc (sizeof (struct GNUNET_SET_Element) +
267 sizeof (struct GNUNET_HashCode));
268 el->element_type = 0;
269 GNUNET_memcpy (&el[1], key, sizeof *key);
271 el->size = sizeof *key;
272 GNUNET_SET_add_element (set, el, NULL, NULL);
279 handle_shutdown (void *cls)
281 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
282 "Shutting down set profiler\n");
283 if (NULL != set_listener)
285 GNUNET_SET_listen_cancel (set_listener);
288 if (NULL != info1.oh)
290 GNUNET_SET_operation_cancel (info1.oh);
293 if (NULL != info2.oh)
295 GNUNET_SET_operation_cancel (info2.oh);
298 if (NULL != info1.set)
300 GNUNET_SET_destroy (info1.set);
303 if (NULL != info2.set)
305 GNUNET_SET_destroy (info2.set);
308 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
314 const struct GNUNET_CONFIGURATION_Handle *cfg,
315 struct GNUNET_TESTING_Peer *peer)
318 struct GNUNET_HashCode hash;
319 /* max. 2 options plus terminator */
320 struct GNUNET_SET_Option opts[3] = {{0}};
321 unsigned int n_opts = 0;
325 if (GNUNET_OK != GNUNET_CRYPTO_get_peer_identity (cfg, &local_peer))
327 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not retrieve host identity\n");
332 statistics = GNUNET_STATISTICS_create ("set-profiler", cfg);
334 GNUNET_SCHEDULER_add_shutdown (&handle_shutdown, NULL);
339 info1.sent = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
340 info2.sent = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
341 common_sent = GNUNET_CONTAINER_multihashmap_create (num_c+1, GNUNET_NO);
343 info1.received = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
344 info2.received = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
346 for (i = 0; i < num_a; i++)
348 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
349 GNUNET_CONTAINER_multihashmap_put (info1.sent, &hash, NULL,
350 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
353 for (i = 0; i < num_b; i++)
355 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
356 GNUNET_CONTAINER_multihashmap_put (info2.sent, &hash, NULL,
357 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
360 for (i = 0; i < num_c; i++)
362 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
363 GNUNET_CONTAINER_multihashmap_put (common_sent, &hash, NULL,
364 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
367 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &app_id);
369 /* FIXME: also implement intersection etc. */
370 info1.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
371 info2.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
373 GNUNET_CONTAINER_multihashmap_iterate (info1.sent, set_insert_iterator, info1.set);
374 GNUNET_CONTAINER_multihashmap_iterate (info2.sent, set_insert_iterator, info2.set);
375 GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info1.set);
376 GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info2.set);
378 set_listener = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
379 &app_id, set_listen_cb, NULL);
384 opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_BYZANTINE };
386 GNUNET_assert (!(force_full && force_delta));
389 opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_FULL };
393 opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_DELTA };
396 opts[n_opts].type = 0;
398 info1.oh = GNUNET_SET_prepare (&local_peer, &app_id, NULL,
399 GNUNET_SET_RESULT_SYMMETRIC,
401 set_result_cb, &info1);
402 GNUNET_SET_commit (info1.oh, info1.set);
403 GNUNET_SET_destroy (info1.set);
409 pre_run (void *cls, char *const *args, const char *cfgfile,
410 const struct GNUNET_CONFIGURATION_Handle *cfg)
412 if (0 != GNUNET_TESTING_peer_run ("set-profiler",
420 main (int argc, char **argv)
422 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
423 { 'A', "num-first", NULL,
424 gettext_noop ("number of values"),
425 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_a },
426 { 'B', "num-second", NULL,
427 gettext_noop ("number of values"),
428 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_b },
429 { 'b', "byzantine", NULL,
430 gettext_noop ("use byzantine mode"),
431 GNUNET_NO, &GNUNET_GETOPT_set_one, &byzantine },
432 { 'f', "force-full", NULL,
433 gettext_noop ("force sending full set"),
434 GNUNET_NO, &GNUNET_GETOPT_set_uint, &force_full },
435 { 'd', "force-delta", NULL,
436 gettext_noop ("number delta operation"),
437 GNUNET_NO, &GNUNET_GETOPT_set_uint, &force_delta },
438 { 'C', "num-common", NULL,
439 gettext_noop ("number of values"),
440 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_c },
441 { 'x', "operation", NULL,
442 gettext_noop ("operation to execute"),
443 GNUNET_YES, &GNUNET_GETOPT_set_string, &op_str },
444 { 's', "statistics", NULL,
445 gettext_noop ("write statistics to file"),
446 GNUNET_YES, &GNUNET_GETOPT_set_filename, &statistics_filename },
447 GNUNET_GETOPT_OPTION_END
449 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-set-profiler",
451 options, &pre_run, NULL, GNUNET_YES);