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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file consensus/gnunet-consensus-profiler.c
21 * @brief profiling tool for gnunet-consensus
22 * @author Florian Dold
25 #include "gnunet_util_lib.h"
26 #include "gnunet_time_lib.h"
27 #include "gnunet_consensus_service.h"
28 #include "gnunet_testbed_service.h"
30 static unsigned int num_peers = 2;
32 static unsigned int replication = 1;
34 static unsigned int num_values = 5;
36 static struct GNUNET_TIME_Relative conclude_timeout;
38 static struct GNUNET_TIME_Relative consensus_delay;
40 static struct GNUNET_CONSENSUS_Handle **consensus_handles;
42 static struct GNUNET_TESTBED_Operation **testbed_operations;
44 static unsigned int num_connected_handles;
46 static struct GNUNET_TESTBED_Peer **peers;
48 static struct GNUNET_PeerIdentity *peer_ids;
50 static unsigned int num_retrieved_peer_ids;
52 static struct GNUNET_HashCode session_id;
54 static unsigned int peers_done = 0;
56 static int dist_static;
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_cb (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_log (GNUNET_ERROR_TYPE_INFO, "got statistics, shutting down\n");
112 GNUNET_SCHEDULER_shutdown ();
117 * Callback function to process statistic values from all peers.
120 * @param peer the peer the statistic belong to
121 * @param subsystem name of subsystem that created the statistic
122 * @param name the name of the datum
123 * @param value the current value
124 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
125 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
128 statistics_cb (void *cls,
129 const struct GNUNET_TESTBED_Peer *peer,
130 const char *subsystem,
135 if (NULL != statistics_file)
137 fprintf (statistics_file, "P%u\t%s\t%s\t%lu\n", GNUNET_TESTBED_get_index (peer), subsystem, name, (unsigned long) value);
146 struct GNUNET_CONSENSUS_Handle *consensus = cls;
148 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
149 "destroying consensus\n");
150 GNUNET_CONSENSUS_destroy (consensus);
152 if (peers_done == num_peers)
155 for (i = 0; i < num_peers; i++)
156 GNUNET_TESTBED_operation_done (testbed_operations[i]);
157 for (i = 0; i < num_peers; i++)
158 printf ("P%u got %u of %u elements\n",
162 if (NULL != statistics_filename)
163 statistics_file = fopen (statistics_filename, "w");
164 GNUNET_TESTBED_get_statistics (num_peers, peers, NULL, NULL,
173 * Called when a conclusion was successful.
175 * @param cls closure, the consensus handle
176 * @return #GNUNET_YES if more consensus groups should be offered,
180 conclude_cb (void *cls)
182 struct GNUNET_CONSENSUS_Handle **chp = cls;
184 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
185 "consensus %d done\n",
186 (int) (chp - consensus_handles));
187 GNUNET_SCHEDULER_add_now (destroy, *chp);
192 generate_indices (int *indices)
196 while (j < replication)
201 n = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
203 for (k = 0; k < j; k++)
209 if (GNUNET_NO == repeat)
218 int unique_indices[replication];
221 struct GNUNET_HashCode val;
222 struct GNUNET_SET_Element element;
226 for (i = 0; i < num_values; i++)
229 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &val);
232 element.size = sizeof (val);
233 for (j = 0; j < replication; j++)
235 GNUNET_CONSENSUS_insert (consensus_handles[j],
243 for (i = 0; i < num_values; i++)
245 generate_indices (unique_indices);
246 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &val);
249 element.size = sizeof (val);
250 for (j = 0; j < replication; j++)
254 cid = unique_indices[j];
255 GNUNET_CONSENSUS_insert (consensus_handles[cid],
262 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
263 "all elements inserted, calling conclude\n");
265 for (i = 0; i < num_peers; i++)
266 GNUNET_CONSENSUS_conclude (consensus_handles[i],
267 conclude_cb, &consensus_handles[i]);
272 * Callback to be called when a service connect operation is completed
274 * @param cls the callback closure from functions generating an operation
275 * @param op the operation that has been finished
276 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
277 * @param emsg error message in case the operation has failed; will be NULL if
278 * operation has executed successfully.
281 connect_complete (void *cls,
282 struct GNUNET_TESTBED_Operation *op,
289 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
290 "testbed connect emsg: %s\n",
295 num_connected_handles++;
297 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
298 "connect complete\n");
300 if (num_connected_handles == num_peers)
308 new_element_cb (void *cls,
309 const struct GNUNET_SET_Element *element)
311 struct GNUNET_CONSENSUS_Handle **chp = cls;
312 int idx = chp - consensus_handles;
314 GNUNET_assert (NULL != cls);
316 results_for_peer[idx]++;
318 GNUNET_assert (sizeof (struct GNUNET_HashCode) == element->size);
320 if (GNUNET_YES == verbose)
322 printf ("P%d received %s\n",
324 GNUNET_h2s ((struct GNUNET_HashCode *) element->data));
330 * Adapter function called to establish a connection to
334 * @param cfg configuration of the peer to connect to; will be available until
335 * GNUNET_TESTBED_operation_done() is called on the operation returned
336 * from GNUNET_TESTBED_service_connect()
337 * @return service handle to return in 'op_result', NULL on error
340 connect_adapter (void *cls,
341 const struct GNUNET_CONFIGURATION_Handle *cfg)
343 struct GNUNET_CONSENSUS_Handle **chp = cls;
344 struct GNUNET_CONSENSUS_Handle *consensus;
345 chp = (struct GNUNET_CONSENSUS_Handle **) cls;
347 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
348 "connect adapter, %d peers\n",
350 consensus = GNUNET_CONSENSUS_create (cfg,
355 &new_element_cb, chp);
356 *chp = (struct GNUNET_CONSENSUS_Handle *) consensus;
362 * Adapter function called to destroy a connection to
366 * @param op_result service handle returned from the connect adapter
369 disconnect_adapter(void *cls, void *op_result)
371 /* FIXME: what to do here? */
372 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
373 "disconnect adapter called\n");
378 * Callback to be called when the requested peer information is available
380 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
381 * @param op the operation this callback corresponds to
382 * @param pinfo the result; will be NULL if the operation has failed
383 * @param emsg error message if the operation has failed; will be NULL if the
384 * operation is successfull
387 peer_info_cb (void *cb_cls,
388 struct GNUNET_TESTBED_Operation *op,
389 const struct GNUNET_TESTBED_PeerInformation *pinfo,
392 struct GNUNET_PeerIdentity *p;
395 GNUNET_assert (NULL == emsg);
397 p = (struct GNUNET_PeerIdentity *) cb_cls;
399 if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
401 *p = *pinfo->result.id;
402 num_retrieved_peer_ids++;
403 if (num_retrieved_peer_ids == num_peers)
404 for (i = 0; i < num_peers; i++)
405 testbed_operations[i] =
406 GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus", connect_complete, NULL,
407 connect_adapter, disconnect_adapter, &consensus_handles[i]);
414 GNUNET_TESTBED_operation_done (op);
419 * Signature of a main function for a testcase.
422 * @param h the run handle
423 * @param num_peers number of peers in 'peers'
424 * @param started_peers handle to peers run in the testbed. NULL upon timeout (see
425 * GNUNET_TESTBED_test_run()).
426 * @param links_succeeded the number of overlay link connection attempts that
428 * @param links_failed the number of overlay link connection attempts that
432 test_master (void *cls,
433 struct GNUNET_TESTBED_RunHandle *h,
434 unsigned int num_peers,
435 struct GNUNET_TESTBED_Peer **started_peers,
436 unsigned int links_succeeded,
437 unsigned int links_failed)
441 GNUNET_log_setup ("gnunet-consensus", "INFO", NULL);
443 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
445 peers = started_peers;
447 peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
449 results_for_peer = GNUNET_malloc (num_peers * sizeof (unsigned int));
450 consensus_handles = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
451 testbed_operations = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
453 for (i = 0; i < num_peers; i++)
454 GNUNET_TESTBED_peer_get_information (peers[i],
455 GNUNET_TESTBED_PIT_IDENTITY,
462 run (void *cls, char *const *args, const char *cfgfile,
463 const struct GNUNET_CONFIGURATION_Handle *cfg)
465 static char *session_str = "gnunet-consensus/test";
467 int topology_cmp_result;
469 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed", "OVERLAY_TOPOLOGY", &topology))
472 "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
473 "seems like you passed the wrong configuration file\n");
477 topology_cmp_result = strcasecmp (topology, "NONE");
478 GNUNET_free (topology);
480 if (0 == topology_cmp_result)
483 "'OVERLAY_TOPOLOGY' set to 'NONE', "
484 "seems like you passed the wrong configuration file\n");
488 if (num_peers < replication)
490 fprintf (stderr, "k must be <=n\n");
494 start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), consensus_delay);
495 deadline = GNUNET_TIME_absolute_add (start, conclude_timeout);
497 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
498 "running gnunet-consensus\n");
500 GNUNET_CRYPTO_hash (session_str, strlen(session_str), &session_id);
502 (void) GNUNET_TESTBED_test_run ("gnunet-consensus",
514 main (int argc, char **argv)
516 struct GNUNET_GETOPT_CommandLineOption options[] = {
518 GNUNET_GETOPT_option_uint ('n',
521 gettext_noop ("number of peers in consensus"),
524 GNUNET_GETOPT_option_uint ('k',
527 gettext_noop ("how many peers (random selection without replacement) receive one value?"),
530 GNUNET_GETOPT_option_uint ('x',
533 gettext_noop ("number of values"),
536 GNUNET_GETOPT_option_relative_time ('t',
539 gettext_noop ("consensus timeout"),
543 GNUNET_GETOPT_option_relative_time ('d',
546 gettext_noop ("delay until consensus starts"),
549 GNUNET_GETOPT_option_filename ('s',
552 gettext_noop ("write statistics to file"),
553 &statistics_filename),
555 GNUNET_GETOPT_option_flag ('S',
557 gettext_noop ("distribute elements to a static subset of good peers"),
560 GNUNET_GETOPT_option_flag ('V',
562 gettext_noop ("be more verbose (print received values)"),
565 GNUNET_GETOPT_OPTION_END
567 conclude_timeout = GNUNET_TIME_UNIT_SECONDS;
568 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-profiler",
570 options, &run, NULL, GNUNET_YES);