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_consensus_service.h"
29 #include "gnunet_testbed_service.h"
31 static unsigned int num_peers = 2;
33 static unsigned int replication = 1;
35 static unsigned int num_values = 5;
37 static struct GNUNET_TIME_Relative conclude_timeout;
39 static struct GNUNET_CONSENSUS_Handle **consensus_handles;
41 static struct GNUNET_TESTBED_Operation **testbed_operations;
43 static unsigned int num_connected_handles;
45 static struct GNUNET_TESTBED_Peer **peers;
47 static struct GNUNET_PeerIdentity *peer_ids;
49 static unsigned int num_retrieved_peer_ids;
51 static struct GNUNET_HashCode session_id;
53 static unsigned int peers_done = 0;
55 static unsigned *results_for_peer;
61 * Signature of the event handler function called by the
62 * respective event controller.
65 * @param event information about the event
68 controller_cb (void *cls,
69 const struct GNUNET_TESTBED_EventInformation *event)
76 destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *ctx)
78 struct GNUNET_CONSENSUS_Handle *consensus = cls;
80 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
81 "destroying consensus\n");
82 GNUNET_CONSENSUS_destroy (consensus);
84 if (peers_done == num_peers)
87 for (i = 0; i < num_peers; i++)
88 GNUNET_TESTBED_operation_done (testbed_operations[i]);
89 for (i = 0; i < num_peers; i++)
90 printf ("P%u got %u of %u elements\n",
94 GNUNET_SCHEDULER_shutdown ();
100 * Called when a conclusion was successful.
102 * @param cls closure, the consensus handle
103 * @return #GNUNET_YES if more consensus groups should be offered,
107 conclude_cb (void *cls)
109 struct GNUNET_CONSENSUS_Handle **chp = cls;
111 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
112 "consensus %d done\n",
113 chp - consensus_handles);
114 GNUNET_SCHEDULER_add_now (destroy, *chp);
119 generate_indices (int *indices)
123 while (j < replication)
128 n = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
130 for (k = 0; k < j; k++)
136 if (GNUNET_NO == repeat)
145 int unique_indices[replication];
148 for (i = 0; i < num_values; i++)
151 struct GNUNET_HashCode val;
152 struct GNUNET_SET_Element element;
154 generate_indices (unique_indices);
155 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &val);
158 element.size = sizeof (val);
159 for (j = 0; j < replication; j++)
163 cid = unique_indices[j];
164 GNUNET_CONSENSUS_insert (consensus_handles[cid],
170 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
171 "all elements inserted, calling conclude\n");
173 for (i = 0; i < num_peers; i++)
174 GNUNET_CONSENSUS_conclude (consensus_handles[i],
175 GNUNET_TIME_relative_to_absolute (conclude_timeout),
176 conclude_cb, &consensus_handles[i]);
181 * Callback to be called when a service connect operation is completed
183 * @param cls the callback closure from functions generating an operation
184 * @param op the operation that has been finished
185 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
186 * @param emsg error message in case the operation has failed; will be NULL if
187 * operation has executed successfully.
190 connect_complete (void *cls,
191 struct GNUNET_TESTBED_Operation *op,
198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
199 "testbed connect emsg: %s\n",
204 num_connected_handles++;
206 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
207 "connect complete\n");
209 if (num_connected_handles == num_peers)
217 new_element_cb (void *cls,
218 const struct GNUNET_SET_Element *element)
220 struct GNUNET_CONSENSUS_Handle **chp = cls;
221 int idx = chp - consensus_handles;
223 GNUNET_assert (NULL != cls);
225 results_for_peer[idx]++;
227 GNUNET_assert (sizeof (struct GNUNET_HashCode) == element->size);
229 if (GNUNET_YES == verbose)
231 printf ("P%d received %s\n",
233 GNUNET_h2s ((struct GNUNET_HashCode *) element->data));
239 * Adapter function called to establish a connection to
243 * @param cfg configuration of the peer to connect to; will be available until
244 * GNUNET_TESTBED_operation_done() is called on the operation returned
245 * from GNUNET_TESTBED_service_connect()
246 * @return service handle to return in 'op_result', NULL on error
249 connect_adapter (void *cls,
250 const struct GNUNET_CONFIGURATION_Handle *cfg)
252 struct GNUNET_CONSENSUS_Handle **chp = cls;
253 struct GNUNET_CONSENSUS_Handle *consensus;
254 chp = (struct GNUNET_CONSENSUS_Handle **) cls;
256 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
257 "connect adapter, %d peers\n",
259 consensus = GNUNET_CONSENSUS_create (cfg,
262 &new_element_cb, chp);
263 *chp = (struct GNUNET_CONSENSUS_Handle *) consensus;
269 * Adapter function called to destroy a connection to
273 * @param op_result service handle returned from the connect adapter
276 disconnect_adapter(void *cls, void *op_result)
278 /* FIXME: what to do here? */
283 * Callback to be called when the requested peer information is available
285 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
286 * @param op the operation this callback corresponds to
287 * @param pinfo the result; will be NULL if the operation has failed
288 * @param emsg error message if the operation has failed; will be NULL if the
289 * operation is successfull
292 peer_info_cb (void *cb_cls,
293 struct GNUNET_TESTBED_Operation *op,
294 const struct GNUNET_TESTBED_PeerInformation *pinfo,
297 struct GNUNET_PeerIdentity *p;
300 GNUNET_assert (NULL == emsg);
302 p = (struct GNUNET_PeerIdentity *) cb_cls;
304 if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
306 *p = *pinfo->result.id;
307 num_retrieved_peer_ids++;
308 if (num_retrieved_peer_ids == num_peers)
309 for (i = 0; i < num_peers; i++)
310 testbed_operations[i] =
311 GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus", connect_complete, NULL,
312 connect_adapter, disconnect_adapter, &consensus_handles[i]);
319 GNUNET_TESTBED_operation_done (op);
324 * Signature of a main function for a testcase.
327 * @param h the run handle
328 * @param num_peers number of peers in 'peers'
329 * @param started_peers handle to peers run in the testbed. NULL upon timeout (see
330 * GNUNET_TESTBED_test_run()).
331 * @param links_succeeded the number of overlay link connection attempts that
333 * @param links_failed the number of overlay link connection attempts that
337 test_master (void *cls,
338 struct GNUNET_TESTBED_RunHandle *h,
339 unsigned int num_peers,
340 struct GNUNET_TESTBED_Peer **started_peers,
341 unsigned int links_succeeded,
342 unsigned int links_failed)
346 GNUNET_log_setup ("gnunet-consensus", "INFO", NULL);
348 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
350 peers = started_peers;
352 peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
354 results_for_peer = GNUNET_malloc (num_peers * sizeof (unsigned int));
355 consensus_handles = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
356 testbed_operations = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
358 for (i = 0; i < num_peers; i++)
359 GNUNET_TESTBED_peer_get_information (peers[i],
360 GNUNET_TESTBED_PIT_IDENTITY,
367 run (void *cls, char *const *args, const char *cfgfile,
368 const struct GNUNET_CONFIGURATION_Handle *cfg)
370 static char *session_str = "gnunet-consensus/test";
372 int topology_cmp_result;
374 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed", "OVERLAY_TOPOLOGY", &topology))
377 "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
378 "seems like you passed the wrong configuration file\n");
382 topology_cmp_result = strcasecmp (topology, "NONE");
383 GNUNET_free (topology);
385 if (0 == topology_cmp_result)
388 "'OVERLAY_TOPOLOGY' set to 'NONE', "
389 "seems like you passed the wrong configuration file\n");
393 if (num_peers < replication)
395 fprintf (stderr, "k must be <=n\n");
399 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
400 "running gnunet-consensus\n");
402 GNUNET_CRYPTO_hash (session_str, strlen(session_str), &session_id);
404 (void) GNUNET_TESTBED_test_run ("gnunet-consensus",
416 main (int argc, char **argv)
418 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
419 { 'n', "num-peers", NULL,
420 gettext_noop ("number of peers in consensus"),
421 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
422 { 'k', "value-replication", NULL,
423 gettext_noop ("how many peers receive one value?"),
424 GNUNET_YES, &GNUNET_GETOPT_set_uint, &replication },
425 { 'x', "num-values", NULL,
426 gettext_noop ("number of values"),
427 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_values },
428 { 't', "timeout", NULL,
429 gettext_noop ("consensus timeout"),
430 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &conclude_timeout },
431 { 'V', "verbose", NULL,
432 gettext_noop ("be more verbose (print received values)"),
433 GNUNET_NO, &GNUNET_GETOPT_set_one, &verbose },
434 GNUNET_GETOPT_OPTION_END
436 conclude_timeout = GNUNET_TIME_UNIT_SECONDS;
437 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-profiler",
439 options, &run, NULL, GNUNET_YES);