2 This file is part of GNUnet
3 Copyright (C) 2013 Christian Grothoff (and other contributing authors)
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 unsigned int dump_statistics;
41 static char *op_str = "union";
43 const static struct GNUNET_CONFIGURATION_Handle *config;
48 struct GNUNET_SET_Handle *set;
49 struct GNUNET_SET_OperationHandle *oh;
50 struct GNUNET_CONTAINER_MultiHashMap *sent;
51 struct GNUNET_CONTAINER_MultiHashMap *received;
55 static struct GNUNET_CONTAINER_MultiHashMap *common_sent;
57 static struct GNUNET_HashCode app_id;
59 static struct GNUNET_PeerIdentity local_peer;
61 static struct GNUNET_SET_ListenHandle *set_listener;
63 static struct GNUNET_STATISTICS_Handle *statistics;
67 map_remove_iterator (void *cls,
68 const struct GNUNET_HashCode *key,
71 struct GNUNET_CONTAINER_MultiHashMap *m = cls;
74 GNUNET_assert (NULL != key);
76 ret = GNUNET_CONTAINER_multihashmap_remove (m, key, NULL);
78 printf ("spurious element\n");
85 * Callback function to process statistic values.
88 * @param subsystem name of subsystem that created the statistic
89 * @param name the name of the datum
90 * @param value the current value
91 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
92 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
95 statistics_result (void *cls,
96 const char *subsystem,
101 printf ("stat %s/%s=%lu\n", subsystem, name, (unsigned long) value);
107 statistics_done (void *cls,
110 GNUNET_assert (GNUNET_YES == success);
111 printf("dumped statistics\n");
112 GNUNET_SCHEDULER_shutdown ();
116 check_all_done (void)
118 if (info1.done == GNUNET_NO || info2.done == GNUNET_NO)
121 GNUNET_CONTAINER_multihashmap_iterate (info1.received, map_remove_iterator, info2.sent);
122 GNUNET_CONTAINER_multihashmap_iterate (info2.received, map_remove_iterator, info1.sent);
124 printf ("set a: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info1.sent));
125 printf ("set b: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info2.sent));
127 if (0 == dump_statistics)
129 GNUNET_SCHEDULER_shutdown ();
133 GNUNET_STATISTICS_get (statistics, NULL, NULL, GNUNET_TIME_UNIT_FOREVER_REL,
134 statistics_done, statistics_result, NULL);
139 set_result_cb (void *cls,
140 const struct GNUNET_SET_Element *element,
141 enum GNUNET_SET_Status status)
143 struct SetInfo *info = cls;
144 struct GNUNET_HashCode hash;
146 GNUNET_assert (GNUNET_NO == info->done);
149 case GNUNET_SET_STATUS_DONE:
150 case GNUNET_SET_STATUS_HALF_DONE:
151 info->done = GNUNET_YES;
152 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s done\n", info->id);
156 case GNUNET_SET_STATUS_FAILURE:
158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failure\n");
159 GNUNET_SCHEDULER_shutdown ();
161 case GNUNET_SET_STATUS_ADD_LOCAL:
162 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: local element\n", info->id);
164 case GNUNET_SET_STATUS_ADD_REMOTE:
165 GNUNET_CRYPTO_hash (element->data, element->size, &hash);
166 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: remote element %s\n", info->id,
168 // XXX: record and check
174 if (element->size != sizeof (struct GNUNET_HashCode))
176 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "wrong element size: %u, expected %u\n", element->size, sizeof (struct GNUNET_HashCode));
180 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: got element (%s)\n",
181 info->id, GNUNET_h2s (element->data));
182 GNUNET_assert (NULL != element->data);
183 GNUNET_CONTAINER_multihashmap_put (info->received,
185 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
190 set_listen_cb (void *cls,
191 const struct GNUNET_PeerIdentity *other_peer,
192 const struct GNUNET_MessageHeader *context_msg,
193 struct GNUNET_SET_Request *request)
197 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
198 "listener failed\n");
201 GNUNET_assert (NULL == info2.oh);
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 "set listen cb called\n");
204 info2.oh = GNUNET_SET_accept (request, GNUNET_SET_RESULT_SYMMETRIC,
205 set_result_cb, &info2);
206 GNUNET_SET_commit (info2.oh, info2.set);
211 set_insert_iterator (void *cls,
212 const struct GNUNET_HashCode *key,
215 struct GNUNET_SET_Handle *set = cls;
216 struct GNUNET_SET_Element *el;
218 el = GNUNET_malloc (sizeof (struct GNUNET_SET_Element) +
219 sizeof (struct GNUNET_HashCode));
220 el->element_type = 0;
221 memcpy (&el[1], key, sizeof *key);
223 el->size = sizeof *key;
224 GNUNET_SET_add_element (set, el, NULL, NULL);
231 handle_shutdown (void *cls,
232 const struct GNUNET_SCHEDULER_TaskContext *tc)
234 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
235 "Shutting down set profiler\n");
236 if (NULL != set_listener)
238 GNUNET_SET_listen_cancel (set_listener);
241 if (NULL != info1.oh)
243 GNUNET_SET_operation_cancel (info1.oh);
246 if (NULL != info2.oh)
248 GNUNET_SET_operation_cancel (info2.oh);
251 if (NULL != info1.set)
253 GNUNET_SET_destroy (info1.set);
256 if (NULL != info2.set)
258 GNUNET_SET_destroy (info2.set);
261 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
267 const struct GNUNET_CONFIGURATION_Handle *cfg,
268 struct GNUNET_TESTING_Peer *peer)
271 struct GNUNET_HashCode hash;
275 if (GNUNET_OK != GNUNET_CRYPTO_get_peer_identity (cfg, &local_peer))
277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not retrieve host identity\n");
282 statistics = GNUNET_STATISTICS_create ("set-profiler", cfg);
284 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, handle_shutdown, NULL);
289 info1.sent = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
290 info2.sent = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
291 common_sent = GNUNET_CONTAINER_multihashmap_create (num_c+1, GNUNET_NO);
293 info1.received = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
294 info2.received = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
296 for (i = 0; i < num_a; i++)
298 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
299 GNUNET_CONTAINER_multihashmap_put (info1.sent, &hash, NULL,
300 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
303 for (i = 0; i < num_b; i++)
305 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
306 GNUNET_CONTAINER_multihashmap_put (info2.sent, &hash, NULL,
307 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
310 for (i = 0; i < num_c; i++)
312 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
313 GNUNET_CONTAINER_multihashmap_put (common_sent, &hash, NULL,
314 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
317 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &app_id);
319 /* FIXME: also implement intersection etc. */
320 info1.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
321 info2.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
323 GNUNET_CONTAINER_multihashmap_iterate (info1.sent, set_insert_iterator, info1.set);
324 GNUNET_CONTAINER_multihashmap_iterate (info2.sent, set_insert_iterator, info2.set);
325 GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info1.set);
326 GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info2.set);
328 set_listener = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
329 &app_id, set_listen_cb, NULL);
331 info1.oh = GNUNET_SET_prepare (&local_peer, &app_id, NULL,
332 GNUNET_SET_RESULT_SYMMETRIC,
333 set_result_cb, &info1);
334 GNUNET_SET_commit (info1.oh, info1.set);
335 GNUNET_SET_destroy (info1.set);
341 pre_run (void *cls, char *const *args, const char *cfgfile,
342 const struct GNUNET_CONFIGURATION_Handle *cfg)
344 if (0 != GNUNET_TESTING_peer_run ("set-profiler",
352 main (int argc, char **argv)
354 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
355 { 'A', "num-first", NULL,
356 gettext_noop ("number of values"),
357 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_a },
358 { 'B', "num-second", NULL,
359 gettext_noop ("number of values"),
360 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_b },
361 { 'C', "num-common", NULL,
362 gettext_noop ("number of values"),
363 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_c },
364 { 'x', "operation", NULL,
365 gettext_noop ("operation to execute"),
366 GNUNET_YES, &GNUNET_GETOPT_set_string, &op_str },
367 { 's', "statistics", NULL,
368 gettext_noop ("dump statistics to stdout after completion"),
369 GNUNET_NO, &GNUNET_GETOPT_set_one, &dump_statistics },
370 GNUNET_GETOPT_OPTION_END
372 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-set-profiler",
374 options, &pre_run, NULL, GNUNET_YES);