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_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 for (i = 0; i < num_values; i++)
224 struct GNUNET_HashCode val;
225 struct GNUNET_SET_Element element;
227 generate_indices (unique_indices);
228 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &val);
231 element.size = sizeof (val);
232 for (j = 0; j < replication; j++)
236 cid = unique_indices[j];
237 GNUNET_CONSENSUS_insert (consensus_handles[cid],
243 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
244 "all elements inserted, calling conclude\n");
246 for (i = 0; i < num_peers; i++)
247 GNUNET_CONSENSUS_conclude (consensus_handles[i],
248 conclude_cb, &consensus_handles[i]);
253 * Callback to be called when a service connect operation is completed
255 * @param cls the callback closure from functions generating an operation
256 * @param op the operation that has been finished
257 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
258 * @param emsg error message in case the operation has failed; will be NULL if
259 * operation has executed successfully.
262 connect_complete (void *cls,
263 struct GNUNET_TESTBED_Operation *op,
270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
271 "testbed connect emsg: %s\n",
276 num_connected_handles++;
278 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
279 "connect complete\n");
281 if (num_connected_handles == num_peers)
289 new_element_cb (void *cls,
290 const struct GNUNET_SET_Element *element)
292 struct GNUNET_CONSENSUS_Handle **chp = cls;
293 int idx = chp - consensus_handles;
295 GNUNET_assert (NULL != cls);
297 results_for_peer[idx]++;
299 GNUNET_assert (sizeof (struct GNUNET_HashCode) == element->size);
301 if (GNUNET_YES == verbose)
303 printf ("P%d received %s\n",
305 GNUNET_h2s ((struct GNUNET_HashCode *) element->data));
311 * Adapter function called to establish a connection to
315 * @param cfg configuration of the peer to connect to; will be available until
316 * GNUNET_TESTBED_operation_done() is called on the operation returned
317 * from GNUNET_TESTBED_service_connect()
318 * @return service handle to return in 'op_result', NULL on error
321 connect_adapter (void *cls,
322 const struct GNUNET_CONFIGURATION_Handle *cfg)
324 struct GNUNET_CONSENSUS_Handle **chp = cls;
325 struct GNUNET_CONSENSUS_Handle *consensus;
326 chp = (struct GNUNET_CONSENSUS_Handle **) cls;
328 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
329 "connect adapter, %d peers\n",
331 consensus = GNUNET_CONSENSUS_create (cfg,
336 &new_element_cb, chp);
337 *chp = (struct GNUNET_CONSENSUS_Handle *) consensus;
343 * Adapter function called to destroy a connection to
347 * @param op_result service handle returned from the connect adapter
350 disconnect_adapter(void *cls, void *op_result)
352 /* FIXME: what to do here? */
353 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
354 "disconnect adapter called\n");
359 * Callback to be called when the requested peer information is available
361 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
362 * @param op the operation this callback corresponds to
363 * @param pinfo the result; will be NULL if the operation has failed
364 * @param emsg error message if the operation has failed; will be NULL if the
365 * operation is successfull
368 peer_info_cb (void *cb_cls,
369 struct GNUNET_TESTBED_Operation *op,
370 const struct GNUNET_TESTBED_PeerInformation *pinfo,
373 struct GNUNET_PeerIdentity *p;
376 GNUNET_assert (NULL == emsg);
378 p = (struct GNUNET_PeerIdentity *) cb_cls;
380 if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
382 *p = *pinfo->result.id;
383 num_retrieved_peer_ids++;
384 if (num_retrieved_peer_ids == num_peers)
385 for (i = 0; i < num_peers; i++)
386 testbed_operations[i] =
387 GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus", connect_complete, NULL,
388 connect_adapter, disconnect_adapter, &consensus_handles[i]);
395 GNUNET_TESTBED_operation_done (op);
400 * Signature of a main function for a testcase.
403 * @param h the run handle
404 * @param num_peers number of peers in 'peers'
405 * @param started_peers handle to peers run in the testbed. NULL upon timeout (see
406 * GNUNET_TESTBED_test_run()).
407 * @param links_succeeded the number of overlay link connection attempts that
409 * @param links_failed the number of overlay link connection attempts that
413 test_master (void *cls,
414 struct GNUNET_TESTBED_RunHandle *h,
415 unsigned int num_peers,
416 struct GNUNET_TESTBED_Peer **started_peers,
417 unsigned int links_succeeded,
418 unsigned int links_failed)
422 GNUNET_log_setup ("gnunet-consensus", "INFO", NULL);
424 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
426 peers = started_peers;
428 peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
430 results_for_peer = GNUNET_malloc (num_peers * sizeof (unsigned int));
431 consensus_handles = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
432 testbed_operations = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
434 for (i = 0; i < num_peers; i++)
435 GNUNET_TESTBED_peer_get_information (peers[i],
436 GNUNET_TESTBED_PIT_IDENTITY,
443 run (void *cls, char *const *args, const char *cfgfile,
444 const struct GNUNET_CONFIGURATION_Handle *cfg)
446 static char *session_str = "gnunet-consensus/test";
448 int topology_cmp_result;
450 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed", "OVERLAY_TOPOLOGY", &topology))
453 "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
454 "seems like you passed the wrong configuration file\n");
458 topology_cmp_result = strcasecmp (topology, "NONE");
459 GNUNET_free (topology);
461 if (0 == topology_cmp_result)
464 "'OVERLAY_TOPOLOGY' set to 'NONE', "
465 "seems like you passed the wrong configuration file\n");
469 if (num_peers < replication)
471 fprintf (stderr, "k must be <=n\n");
475 start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), consensus_delay);
476 deadline = GNUNET_TIME_absolute_add (start, conclude_timeout);
478 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
479 "running gnunet-consensus\n");
481 GNUNET_CRYPTO_hash (session_str, strlen(session_str), &session_id);
483 (void) GNUNET_TESTBED_test_run ("gnunet-consensus",
495 main (int argc, char **argv)
497 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
498 { 'n', "num-peers", NULL,
499 gettext_noop ("number of peers in consensus"),
500 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
501 { 'k', "value-replication", NULL,
502 gettext_noop ("how many peers (random selection without replacement) receive one value?"),
503 GNUNET_YES, &GNUNET_GETOPT_set_uint, &replication },
504 { 'x', "num-values", NULL,
505 gettext_noop ("number of values"),
506 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_values },
507 { 't', "timeout", NULL,
508 gettext_noop ("consensus timeout"),
509 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &conclude_timeout },
510 { 'd', "delay", NULL,
511 gettext_noop ("delay until consensus starts"),
512 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &consensus_delay },
513 { 's', "statistics", NULL,
514 gettext_noop ("write statistics to file"),
515 GNUNET_YES, &GNUNET_GETOPT_set_filename, &statistics_filename },
516 { 'V', "verbose", NULL,
517 gettext_noop ("be more verbose (print received values)"),
518 GNUNET_NO, &GNUNET_GETOPT_set_one, &verbose },
519 GNUNET_GETOPT_OPTION_END
521 conclude_timeout = GNUNET_TIME_UNIT_SECONDS;
522 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-profiler",
524 options, &run, NULL, GNUNET_YES);