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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 int dist_static;
60 static unsigned *results_for_peer;
63 * The profiler will write statistics
64 * for all peers to the file with this name.
66 static char *statistics_filename;
69 * The profiler will write statistics
70 * for all peers to this file.
72 static FILE *statistics_file;
77 * Start time for all consensuses.
79 static struct GNUNET_TIME_Absolute start;
82 * Deadline for all consensuses.
84 static struct GNUNET_TIME_Absolute deadline;
88 * Signature of the event handler function called by the
89 * respective event controller.
92 * @param event information about the event
95 controller_cb (void *cls,
96 const struct GNUNET_TESTBED_EventInformation *event)
103 statistics_done_cb (void *cls,
105 GNUNET_TESTBED_Operation
109 GNUNET_assert (NULL == emsg);
110 GNUNET_TESTBED_operation_done (op);
111 if (NULL != statistics_file)
112 fclose (statistics_file);
113 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "got statistics, shutting down\n");
114 GNUNET_SCHEDULER_shutdown ();
119 * Callback function to process statistic values from all peers.
122 * @param peer the peer the statistic belong to
123 * @param subsystem name of subsystem that created the statistic
124 * @param name the name of the datum
125 * @param value the current value
126 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
127 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
130 statistics_cb (void *cls,
131 const struct GNUNET_TESTBED_Peer *peer,
132 const char *subsystem,
137 if (NULL != statistics_file)
139 fprintf (statistics_file, "P%u\t%s\t%s\t%lu\n", GNUNET_TESTBED_get_index (
140 peer), subsystem, name, (unsigned long) value);
149 struct GNUNET_CONSENSUS_Handle *consensus = cls;
151 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
152 "destroying consensus\n");
153 GNUNET_CONSENSUS_destroy (consensus);
155 if (peers_done == num_peers)
158 for (i = 0; i < num_peers; i++)
159 GNUNET_TESTBED_operation_done (testbed_operations[i]);
160 for (i = 0; i < num_peers; i++)
161 printf ("P%u got %u of %u elements\n",
165 if (NULL != statistics_filename)
166 statistics_file = fopen (statistics_filename, "w");
167 GNUNET_TESTBED_get_statistics (num_peers, peers, NULL, NULL,
176 * Called when a conclusion was successful.
178 * @param cls closure, the consensus handle
179 * @return #GNUNET_YES if more consensus groups should be offered,
183 conclude_cb (void *cls)
185 struct GNUNET_CONSENSUS_Handle **chp = cls;
187 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
188 "consensus %d done\n",
189 (int) (chp - consensus_handles));
190 GNUNET_SCHEDULER_add_now (destroy, *chp);
195 generate_indices (int *indices)
200 while (j < replication)
205 n = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
207 for (k = 0; k < j; k++)
213 if (GNUNET_NO == repeat)
222 int unique_indices[replication];
225 struct GNUNET_HashCode val;
226 struct GNUNET_SET_Element element;
230 for (i = 0; i < num_values; i++)
232 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &val);
235 element.size = sizeof(val);
236 for (j = 0; j < replication; j++)
238 GNUNET_CONSENSUS_insert (consensus_handles[j],
246 for (i = 0; i < num_values; i++)
248 generate_indices (unique_indices);
249 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &val);
252 element.size = sizeof(val);
253 for (j = 0; j < replication; j++)
257 cid = unique_indices[j];
258 GNUNET_CONSENSUS_insert (consensus_handles[cid],
265 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
266 "all elements inserted, calling conclude\n");
268 for (i = 0; i < num_peers; i++)
269 GNUNET_CONSENSUS_conclude (consensus_handles[i],
270 conclude_cb, &consensus_handles[i]);
275 * Callback to be called when a service connect operation is completed
277 * @param cls the callback closure from functions generating an operation
278 * @param op the operation that has been finished
279 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
280 * @param emsg error message in case the operation has failed; will be NULL if
281 * operation has executed successfully.
284 connect_complete (void *cls,
285 struct GNUNET_TESTBED_Operation *op,
291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
292 "testbed connect emsg: %s\n",
297 num_connected_handles++;
299 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
300 "connect complete\n");
302 if (num_connected_handles == num_peers)
310 new_element_cb (void *cls,
311 const struct GNUNET_SET_Element *element)
313 struct GNUNET_CONSENSUS_Handle **chp = cls;
314 int idx = chp - consensus_handles;
316 GNUNET_assert (NULL != cls);
318 results_for_peer[idx]++;
320 GNUNET_assert (sizeof(struct GNUNET_HashCode) == element->size);
322 if (GNUNET_YES == verbose)
324 printf ("P%d received %s\n",
326 GNUNET_h2s ((struct GNUNET_HashCode *) element->data));
332 * Adapter function called to establish a connection to
336 * @param cfg configuration of the peer to connect to; will be available until
337 * GNUNET_TESTBED_operation_done() is called on the operation returned
338 * from GNUNET_TESTBED_service_connect()
339 * @return service handle to return in 'op_result', NULL on error
342 connect_adapter (void *cls,
343 const struct GNUNET_CONFIGURATION_Handle *cfg)
345 struct GNUNET_CONSENSUS_Handle **chp = cls;
346 struct GNUNET_CONSENSUS_Handle *consensus;
348 chp = (struct GNUNET_CONSENSUS_Handle **) cls;
350 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
351 "connect adapter, %d peers\n",
353 consensus = GNUNET_CONSENSUS_create (cfg,
358 &new_element_cb, chp);
359 *chp = (struct GNUNET_CONSENSUS_Handle *) consensus;
365 * Adapter function called to destroy a connection to
369 * @param op_result service handle returned from the connect adapter
372 disconnect_adapter (void *cls, void *op_result)
374 /* FIXME: what to do here? */
375 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
376 "disconnect adapter called\n");
381 * Callback to be called when the requested peer information is available
383 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
384 * @param op the operation this callback corresponds to
385 * @param pinfo the result; will be NULL if the operation has failed
386 * @param emsg error message if the operation has failed; will be NULL if the
387 * operation is successfull
390 peer_info_cb (void *cb_cls,
391 struct GNUNET_TESTBED_Operation *op,
392 const struct GNUNET_TESTBED_PeerInformation *pinfo,
395 struct GNUNET_PeerIdentity *p;
398 GNUNET_assert (NULL == emsg);
400 p = (struct GNUNET_PeerIdentity *) cb_cls;
402 if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
404 *p = *pinfo->result.id;
405 num_retrieved_peer_ids++;
406 if (num_retrieved_peer_ids == num_peers)
407 for (i = 0; i < num_peers; i++)
408 testbed_operations[i] =
409 GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus",
410 connect_complete, NULL,
411 connect_adapter, disconnect_adapter,
412 &consensus_handles[i]);
419 GNUNET_TESTBED_operation_done (op);
424 * Signature of a main function for a testcase.
427 * @param h the run handle
428 * @param num_peers number of peers in 'peers'
429 * @param started_peers handle to peers run in the testbed. NULL upon timeout (see
430 * GNUNET_TESTBED_test_run()).
431 * @param links_succeeded the number of overlay link connection attempts that
433 * @param links_failed the number of overlay link connection attempts that
437 test_master (void *cls,
438 struct GNUNET_TESTBED_RunHandle *h,
439 unsigned int num_peers,
440 struct GNUNET_TESTBED_Peer **started_peers,
441 unsigned int links_succeeded,
442 unsigned int links_failed)
446 GNUNET_log_setup ("gnunet-consensus", "INFO", NULL);
448 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
450 peers = started_peers;
452 peer_ids = GNUNET_malloc (num_peers * sizeof(struct GNUNET_PeerIdentity));
454 results_for_peer = GNUNET_malloc (num_peers * sizeof(unsigned int));
455 consensus_handles = GNUNET_malloc (num_peers * sizeof(struct
457 testbed_operations = GNUNET_malloc (num_peers * sizeof(struct
460 for (i = 0; i < num_peers; i++)
461 GNUNET_TESTBED_peer_get_information (peers[i],
462 GNUNET_TESTBED_PIT_IDENTITY,
469 run (void *cls, char *const *args, const char *cfgfile,
470 const struct GNUNET_CONFIGURATION_Handle *cfg)
472 static char *session_str = "gnunet-consensus/test";
474 int topology_cmp_result;
476 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed",
481 "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
482 "seems like you passed the wrong configuration file\n");
486 topology_cmp_result = strcasecmp (topology, "NONE");
487 GNUNET_free (topology);
489 if (0 == topology_cmp_result)
492 "'OVERLAY_TOPOLOGY' set to 'NONE', "
493 "seems like you passed the wrong configuration file\n");
497 if (num_peers < replication)
499 fprintf (stderr, "k must be <=n\n");
503 start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
505 deadline = GNUNET_TIME_absolute_add (start, conclude_timeout);
507 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
508 "running gnunet-consensus\n");
510 GNUNET_CRYPTO_hash (session_str, strlen (session_str), &session_id);
512 (void) GNUNET_TESTBED_test_run ("gnunet-consensus",
524 main (int argc, char **argv)
526 struct GNUNET_GETOPT_CommandLineOption options[] = {
527 GNUNET_GETOPT_option_uint ('n',
530 gettext_noop ("number of peers in consensus"),
533 GNUNET_GETOPT_option_uint ('k',
537 "how many peers (random selection without replacement) receive one value?"),
540 GNUNET_GETOPT_option_uint ('x',
543 gettext_noop ("number of values"),
546 GNUNET_GETOPT_option_relative_time ('t',
549 gettext_noop ("consensus timeout"),
553 GNUNET_GETOPT_option_relative_time ('d',
557 "delay until consensus starts"),
560 GNUNET_GETOPT_option_filename ('s',
563 gettext_noop ("write statistics to file"),
564 &statistics_filename),
566 GNUNET_GETOPT_option_flag ('S',
569 "distribute elements to a static subset of good peers"),
572 GNUNET_GETOPT_option_flag ('V',
575 "be more verbose (print received values)"),
578 GNUNET_GETOPT_OPTION_END
581 conclude_timeout = GNUNET_TIME_UNIT_SECONDS;
582 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-profiler",
584 options, &run, NULL, GNUNET_YES);