2 This file is part of GNUnet
3 Copyright (C) 2012 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 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;
61 * The profiler will write statistics
62 * for all peers to the file with this name.
64 static char *statistics_filename;
67 * The profiler will write statistics
68 * for all peers to this file.
70 static FILE *statistics_file;
75 * Start time for all consensuses.
77 static struct GNUNET_TIME_Absolute start;
80 * Deadline for all consensuses.
82 static struct GNUNET_TIME_Absolute deadline;
86 * Signature of the event handler function called by the
87 * respective event controller.
90 * @param event information about the event
93 controller_cb (void *cls,
94 const struct GNUNET_TESTBED_EventInformation *event)
101 statistics_done_db (void *cls,
103 GNUNET_TESTBED_Operation
107 GNUNET_assert (NULL == emsg);
108 GNUNET_TESTBED_operation_done (op);
109 if (NULL != statistics_file)
110 fclose (statistics_file);
111 GNUNET_SCHEDULER_shutdown ();
116 * Callback function to process statistic values from all peers.
119 * @param peer the peer the statistic belong to
120 * @param subsystem name of subsystem that created the statistic
121 * @param name the name of the datum
122 * @param value the current value
123 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
124 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
127 statistics_cb (void *cls,
128 const struct GNUNET_TESTBED_Peer *peer,
129 const char *subsystem,
134 if (NULL != statistics_file)
136 fprintf (statistics_file, "P%u\t%s\t%s\t%lu\n", GNUNET_TESTBED_get_index (peer), subsystem, name, (unsigned long) value);
145 struct GNUNET_CONSENSUS_Handle *consensus = cls;
147 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
148 "destroying consensus\n");
149 GNUNET_CONSENSUS_destroy (consensus);
151 if (peers_done == num_peers)
154 for (i = 0; i < num_peers; i++)
155 GNUNET_TESTBED_operation_done (testbed_operations[i]);
156 for (i = 0; i < num_peers; i++)
157 printf ("P%u got %u of %u elements\n",
161 if (NULL != statistics_filename)
162 statistics_file = fopen (statistics_filename, "w");
163 GNUNET_TESTBED_get_statistics (num_peers, peers, NULL, NULL,
172 * Called when a conclusion was successful.
174 * @param cls closure, the consensus handle
175 * @return #GNUNET_YES if more consensus groups should be offered,
179 conclude_cb (void *cls)
181 struct GNUNET_CONSENSUS_Handle **chp = cls;
183 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
184 "consensus %d done\n",
185 chp - consensus_handles);
186 GNUNET_SCHEDULER_add_now (destroy, *chp);
191 generate_indices (int *indices)
195 while (j < replication)
200 n = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
202 for (k = 0; k < j; k++)
208 if (GNUNET_NO == repeat)
217 int unique_indices[replication];
220 for (i = 0; i < num_values; i++)
223 struct GNUNET_HashCode val;
224 struct GNUNET_SET_Element element;
226 generate_indices (unique_indices);
227 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &val);
230 element.size = sizeof (val);
231 for (j = 0; j < replication; j++)
235 cid = unique_indices[j];
236 GNUNET_CONSENSUS_insert (consensus_handles[cid],
242 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
243 "all elements inserted, calling conclude\n");
245 for (i = 0; i < num_peers; i++)
246 GNUNET_CONSENSUS_conclude (consensus_handles[i],
247 conclude_cb, &consensus_handles[i]);
252 * Callback to be called when a service connect operation is completed
254 * @param cls the callback closure from functions generating an operation
255 * @param op the operation that has been finished
256 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
257 * @param emsg error message in case the operation has failed; will be NULL if
258 * operation has executed successfully.
261 connect_complete (void *cls,
262 struct GNUNET_TESTBED_Operation *op,
269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
270 "testbed connect emsg: %s\n",
275 num_connected_handles++;
277 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
278 "connect complete\n");
280 if (num_connected_handles == num_peers)
288 new_element_cb (void *cls,
289 const struct GNUNET_SET_Element *element)
291 struct GNUNET_CONSENSUS_Handle **chp = cls;
292 int idx = chp - consensus_handles;
294 GNUNET_assert (NULL != cls);
296 results_for_peer[idx]++;
298 GNUNET_assert (sizeof (struct GNUNET_HashCode) == element->size);
300 if (GNUNET_YES == verbose)
302 printf ("P%d received %s\n",
304 GNUNET_h2s ((struct GNUNET_HashCode *) element->data));
310 * Adapter function called to establish a connection to
314 * @param cfg configuration of the peer to connect to; will be available until
315 * GNUNET_TESTBED_operation_done() is called on the operation returned
316 * from GNUNET_TESTBED_service_connect()
317 * @return service handle to return in 'op_result', NULL on error
320 connect_adapter (void *cls,
321 const struct GNUNET_CONFIGURATION_Handle *cfg)
323 struct GNUNET_CONSENSUS_Handle **chp = cls;
324 struct GNUNET_CONSENSUS_Handle *consensus;
325 chp = (struct GNUNET_CONSENSUS_Handle **) cls;
327 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
328 "connect adapter, %d peers\n",
330 consensus = GNUNET_CONSENSUS_create (cfg,
335 &new_element_cb, chp);
336 *chp = (struct GNUNET_CONSENSUS_Handle *) consensus;
342 * Adapter function called to destroy a connection to
346 * @param op_result service handle returned from the connect adapter
349 disconnect_adapter(void *cls, void *op_result)
351 /* FIXME: what to do here? */
356 * Callback to be called when the requested peer information is available
358 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
359 * @param op the operation this callback corresponds to
360 * @param pinfo the result; will be NULL if the operation has failed
361 * @param emsg error message if the operation has failed; will be NULL if the
362 * operation is successfull
365 peer_info_cb (void *cb_cls,
366 struct GNUNET_TESTBED_Operation *op,
367 const struct GNUNET_TESTBED_PeerInformation *pinfo,
370 struct GNUNET_PeerIdentity *p;
373 GNUNET_assert (NULL == emsg);
375 p = (struct GNUNET_PeerIdentity *) cb_cls;
377 if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
379 *p = *pinfo->result.id;
380 num_retrieved_peer_ids++;
381 if (num_retrieved_peer_ids == num_peers)
382 for (i = 0; i < num_peers; i++)
383 testbed_operations[i] =
384 GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus", connect_complete, NULL,
385 connect_adapter, disconnect_adapter, &consensus_handles[i]);
392 GNUNET_TESTBED_operation_done (op);
397 * Signature of a main function for a testcase.
400 * @param h the run handle
401 * @param num_peers number of peers in 'peers'
402 * @param started_peers handle to peers run in the testbed. NULL upon timeout (see
403 * GNUNET_TESTBED_test_run()).
404 * @param links_succeeded the number of overlay link connection attempts that
406 * @param links_failed the number of overlay link connection attempts that
410 test_master (void *cls,
411 struct GNUNET_TESTBED_RunHandle *h,
412 unsigned int num_peers,
413 struct GNUNET_TESTBED_Peer **started_peers,
414 unsigned int links_succeeded,
415 unsigned int links_failed)
419 GNUNET_log_setup ("gnunet-consensus", "INFO", NULL);
421 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
423 peers = started_peers;
425 peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
427 results_for_peer = GNUNET_malloc (num_peers * sizeof (unsigned int));
428 consensus_handles = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
429 testbed_operations = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
431 for (i = 0; i < num_peers; i++)
432 GNUNET_TESTBED_peer_get_information (peers[i],
433 GNUNET_TESTBED_PIT_IDENTITY,
440 run (void *cls, char *const *args, const char *cfgfile,
441 const struct GNUNET_CONFIGURATION_Handle *cfg)
443 static char *session_str = "gnunet-consensus/test";
445 int topology_cmp_result;
447 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed", "OVERLAY_TOPOLOGY", &topology))
450 "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
451 "seems like you passed the wrong configuration file\n");
455 topology_cmp_result = strcasecmp (topology, "NONE");
456 GNUNET_free (topology);
458 if (0 == topology_cmp_result)
461 "'OVERLAY_TOPOLOGY' set to 'NONE', "
462 "seems like you passed the wrong configuration file\n");
466 if (num_peers < replication)
468 fprintf (stderr, "k must be <=n\n");
472 start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), consensus_delay);
473 deadline = GNUNET_TIME_absolute_add (start, conclude_timeout);
475 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
476 "running gnunet-consensus\n");
478 GNUNET_CRYPTO_hash (session_str, strlen(session_str), &session_id);
480 (void) GNUNET_TESTBED_test_run ("gnunet-consensus",
492 main (int argc, char **argv)
494 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
495 { 'n', "num-peers", NULL,
496 gettext_noop ("number of peers in consensus"),
497 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
498 { 'k', "value-replication", NULL,
499 gettext_noop ("how many peers (random selection without replacement) receive one value?"),
500 GNUNET_YES, &GNUNET_GETOPT_set_uint, &replication },
501 { 'x', "num-values", NULL,
502 gettext_noop ("number of values"),
503 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_values },
504 { 't', "timeout", NULL,
505 gettext_noop ("consensus timeout"),
506 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &conclude_timeout },
507 { 'd', "delay", NULL,
508 gettext_noop ("delay until consensus starts"),
509 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &consensus_delay },
510 { 's', "statistics", NULL,
511 gettext_noop ("write statistics to file"),
512 GNUNET_YES, &GNUNET_GETOPT_set_filename, &statistics_filename },
513 { 'V', "verbose", NULL,
514 gettext_noop ("be more verbose (print received values)"),
515 GNUNET_NO, &GNUNET_GETOPT_set_one, &verbose },
516 GNUNET_GETOPT_OPTION_END
518 conclude_timeout = GNUNET_TIME_UNIT_SECONDS;
519 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-profiler",
521 options, &run, NULL, GNUNET_YES);