2 This file is part of GNUnet
3 (C) 2012 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file consensus/gnunet-consensus-profiler.c
23 * @brief profiling tool for gnunet-consensus
24 * @author Florian Dold
27 #include "gnunet_util_lib.h"
28 #include "gnunet_time_lib.h"
29 #include "gnunet_consensus_service.h"
30 #include "gnunet_testbed_service.h"
32 static unsigned int num_peers = 2;
34 static unsigned int replication = 1;
36 static unsigned int num_values = 5;
38 static struct GNUNET_TIME_Relative conclude_timeout;
40 static struct GNUNET_TIME_Relative consensus_delay;
42 static struct GNUNET_CONSENSUS_Handle **consensus_handles;
44 static struct GNUNET_TESTBED_Operation **testbed_operations;
46 static unsigned int num_connected_handles;
48 static struct GNUNET_TESTBED_Peer **peers;
50 static struct GNUNET_PeerIdentity *peer_ids;
52 static unsigned int num_retrieved_peer_ids;
54 static struct GNUNET_HashCode session_id;
56 static unsigned int peers_done = 0;
58 static unsigned *results_for_peer;
63 * Start time for all consensuses.
65 static struct GNUNET_TIME_Absolute start;
68 * Deadline for all consensuses.
70 static struct GNUNET_TIME_Absolute deadline;
74 * Signature of the event handler function called by the
75 * respective event controller.
78 * @param event information about the event
81 controller_cb (void *cls,
82 const struct GNUNET_TESTBED_EventInformation *event)
89 destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *ctx)
91 struct GNUNET_CONSENSUS_Handle *consensus = cls;
93 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
94 "destroying consensus\n");
95 GNUNET_CONSENSUS_destroy (consensus);
97 if (peers_done == num_peers)
100 for (i = 0; i < num_peers; i++)
101 GNUNET_TESTBED_operation_done (testbed_operations[i]);
102 for (i = 0; i < num_peers; i++)
103 printf ("P%u got %u of %u elements\n",
107 GNUNET_SCHEDULER_shutdown ();
113 * Called when a conclusion was successful.
115 * @param cls closure, the consensus handle
116 * @return #GNUNET_YES if more consensus groups should be offered,
120 conclude_cb (void *cls)
122 struct GNUNET_CONSENSUS_Handle **chp = cls;
124 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
125 "consensus %d done\n",
126 chp - consensus_handles);
127 GNUNET_SCHEDULER_add_now (destroy, *chp);
132 generate_indices (int *indices)
136 while (j < replication)
141 n = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
143 for (k = 0; k < j; k++)
149 if (GNUNET_NO == repeat)
158 int unique_indices[replication];
161 for (i = 0; i < num_values; i++)
164 struct GNUNET_HashCode val;
165 struct GNUNET_SET_Element element;
167 generate_indices (unique_indices);
168 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &val);
171 element.size = sizeof (val);
172 for (j = 0; j < replication; j++)
176 cid = unique_indices[j];
177 GNUNET_CONSENSUS_insert (consensus_handles[cid],
183 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
184 "all elements inserted, calling conclude\n");
186 for (i = 0; i < num_peers; i++)
187 GNUNET_CONSENSUS_conclude (consensus_handles[i],
188 conclude_cb, &consensus_handles[i]);
193 * Callback to be called when a service connect operation is completed
195 * @param cls the callback closure from functions generating an operation
196 * @param op the operation that has been finished
197 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
198 * @param emsg error message in case the operation has failed; will be NULL if
199 * operation has executed successfully.
202 connect_complete (void *cls,
203 struct GNUNET_TESTBED_Operation *op,
210 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
211 "testbed connect emsg: %s\n",
216 num_connected_handles++;
218 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
219 "connect complete\n");
221 if (num_connected_handles == num_peers)
229 new_element_cb (void *cls,
230 const struct GNUNET_SET_Element *element)
232 struct GNUNET_CONSENSUS_Handle **chp = cls;
233 int idx = chp - consensus_handles;
235 GNUNET_assert (NULL != cls);
237 results_for_peer[idx]++;
239 GNUNET_assert (sizeof (struct GNUNET_HashCode) == element->size);
241 if (GNUNET_YES == verbose)
243 printf ("P%d received %s\n",
245 GNUNET_h2s ((struct GNUNET_HashCode *) element->data));
251 * Adapter function called to establish a connection to
255 * @param cfg configuration of the peer to connect to; will be available until
256 * GNUNET_TESTBED_operation_done() is called on the operation returned
257 * from GNUNET_TESTBED_service_connect()
258 * @return service handle to return in 'op_result', NULL on error
261 connect_adapter (void *cls,
262 const struct GNUNET_CONFIGURATION_Handle *cfg)
264 struct GNUNET_CONSENSUS_Handle **chp = cls;
265 struct GNUNET_CONSENSUS_Handle *consensus;
266 chp = (struct GNUNET_CONSENSUS_Handle **) cls;
268 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
269 "connect adapter, %d peers\n",
271 consensus = GNUNET_CONSENSUS_create (cfg,
276 &new_element_cb, chp);
277 *chp = (struct GNUNET_CONSENSUS_Handle *) consensus;
283 * Adapter function called to destroy a connection to
287 * @param op_result service handle returned from the connect adapter
290 disconnect_adapter(void *cls, void *op_result)
292 /* FIXME: what to do here? */
297 * Callback to be called when the requested peer information is available
299 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
300 * @param op the operation this callback corresponds to
301 * @param pinfo the result; will be NULL if the operation has failed
302 * @param emsg error message if the operation has failed; will be NULL if the
303 * operation is successfull
306 peer_info_cb (void *cb_cls,
307 struct GNUNET_TESTBED_Operation *op,
308 const struct GNUNET_TESTBED_PeerInformation *pinfo,
311 struct GNUNET_PeerIdentity *p;
314 GNUNET_assert (NULL == emsg);
316 p = (struct GNUNET_PeerIdentity *) cb_cls;
318 if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
320 *p = *pinfo->result.id;
321 num_retrieved_peer_ids++;
322 if (num_retrieved_peer_ids == num_peers)
323 for (i = 0; i < num_peers; i++)
324 testbed_operations[i] =
325 GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus", connect_complete, NULL,
326 connect_adapter, disconnect_adapter, &consensus_handles[i]);
333 GNUNET_TESTBED_operation_done (op);
338 * Signature of a main function for a testcase.
341 * @param h the run handle
342 * @param num_peers number of peers in 'peers'
343 * @param started_peers handle to peers run in the testbed. NULL upon timeout (see
344 * GNUNET_TESTBED_test_run()).
345 * @param links_succeeded the number of overlay link connection attempts that
347 * @param links_failed the number of overlay link connection attempts that
351 test_master (void *cls,
352 struct GNUNET_TESTBED_RunHandle *h,
353 unsigned int num_peers,
354 struct GNUNET_TESTBED_Peer **started_peers,
355 unsigned int links_succeeded,
356 unsigned int links_failed)
360 GNUNET_log_setup ("gnunet-consensus", "INFO", NULL);
362 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
364 peers = started_peers;
366 peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
368 results_for_peer = GNUNET_malloc (num_peers * sizeof (unsigned int));
369 consensus_handles = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
370 testbed_operations = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
372 for (i = 0; i < num_peers; i++)
373 GNUNET_TESTBED_peer_get_information (peers[i],
374 GNUNET_TESTBED_PIT_IDENTITY,
381 run (void *cls, char *const *args, const char *cfgfile,
382 const struct GNUNET_CONFIGURATION_Handle *cfg)
384 static char *session_str = "gnunet-consensus/test";
386 int topology_cmp_result;
388 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed", "OVERLAY_TOPOLOGY", &topology))
391 "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
392 "seems like you passed the wrong configuration file\n");
396 topology_cmp_result = strcasecmp (topology, "NONE");
397 GNUNET_free (topology);
399 if (0 == topology_cmp_result)
402 "'OVERLAY_TOPOLOGY' set to 'NONE', "
403 "seems like you passed the wrong configuration file\n");
407 if (num_peers < replication)
409 fprintf (stderr, "k must be <=n\n");
413 start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), consensus_delay);
414 deadline = GNUNET_TIME_absolute_add (start, conclude_timeout);
416 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
417 "running gnunet-consensus\n");
419 GNUNET_CRYPTO_hash (session_str, strlen(session_str), &session_id);
421 (void) GNUNET_TESTBED_test_run ("gnunet-consensus",
433 main (int argc, char **argv)
435 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
436 { 'n', "num-peers", NULL,
437 gettext_noop ("number of peers in consensus"),
438 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
439 { 'k', "value-replication", NULL,
440 gettext_noop ("how many peers receive one value?"),
441 GNUNET_YES, &GNUNET_GETOPT_set_uint, &replication },
442 { 'x', "num-values", NULL,
443 gettext_noop ("number of values"),
444 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_values },
445 { 't', "timeout", NULL,
446 gettext_noop ("consensus timeout"),
447 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &conclude_timeout },
448 { 'd', "delay", NULL,
449 gettext_noop ("delay until consensus starts"),
450 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &consensus_delay },
451 { 'V', "verbose", NULL,
452 gettext_noop ("be more verbose (print received values)"),
453 GNUNET_NO, &GNUNET_GETOPT_set_one, &verbose },
454 GNUNET_GETOPT_OPTION_END
456 conclude_timeout = GNUNET_TIME_UNIT_SECONDS;
457 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-profiler",
459 options, &run, NULL, GNUNET_YES);