2 This file is part of GNUnet.
3 (C) 2011, 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 testbed/gnunet-testbed-profiler.c
23 * @brief Profiling driver for the testbed.
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
28 #include "gnunet_common.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31 #include "testbed_api_hosts.h"
34 * Generic loggins shorthand
36 #define LOG(kind,...) \
37 GNUNET_log (kind, __VA_ARGS__)
46 * The testbed operation handle
48 struct GNUNET_TESTBED_Operation *op;
56 * The next pointer for DLL
58 struct DLLOperation *next;
61 * The prev pointer for DLL
63 struct DLLOperation *prev;
68 * Availanle states during profiling
80 STATE_SLAVES_STARTING,
98 * Destroying peers; we can do this as the controller takes care of stopping a
99 * peer if it is running
101 STATE_PEERS_DESTROYING
106 * An array of hosts loaded from the hostkeys file
108 static struct GNUNET_TESTBED_Host **hosts;
111 * The array of peers; we fill this as the peers are given to us by the testbed
113 static struct GNUNET_TESTBED_Peer **peers;
116 /* * Operation handle */
118 /* static struct GNUNET_TESTBED_Operation *op; */
121 * Host registration handle
123 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
126 * Handle to the master controller process
128 struct GNUNET_TESTBED_ControllerProc *mc_proc;
131 * Handle to the master controller
133 struct GNUNET_TESTBED_Controller *mc;
136 * Handle to global configuration
138 struct GNUNET_CONFIGURATION_Handle *cfg;
141 * Head of the operations list
143 struct DLLOperation *dll_op_head;
146 * Tail of the operations list
148 struct DLLOperation *dll_op_tail;
151 * Peer linking - topology operation
153 struct GNUNET_TESTBED_Operation *topology_op;
156 * Abort task identifier
158 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
161 * Shutdown task identifier
163 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
166 * Host registration task identifier
168 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
171 * Global event mask for all testbed events
176 * The starting time of a profiling step
178 struct GNUNET_TIME_Absolute prof_start_time;
181 * Duration profiling step has taken
183 struct GNUNET_TIME_Relative prof_time;
188 unsigned int peer_id;
191 * Number of peers to be started by the profiler
193 static unsigned int num_peers;
196 * Number of hosts in the hosts array
198 static unsigned int num_hosts;
201 * Number of random links to be established between peers
203 static unsigned int num_links;
206 * Number of timeout failures to tolerate
208 static unsigned int num_cont_fails;
211 * Continuous failures during overlay connect operations
213 static unsigned int cont_fails;
216 * Links which are successfully established
218 static unsigned int established_links;
221 * Links which are not successfully established
223 static unsigned int failed_links;
226 * Global testing status
231 * current state of profiling
236 * The topology we want to acheive
238 enum GNUNET_TESTBED_TopologyOption topology;
245 * @param tc the task context
248 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
250 struct DLLOperation *dll_op;
253 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
254 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
255 GNUNET_SCHEDULER_cancel (abort_task);
256 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
257 GNUNET_SCHEDULER_cancel (register_hosts_task);
258 if (NULL != reg_handle)
259 GNUNET_TESTBED_cancel_registration (reg_handle);
260 if (NULL != topology_op)
261 GNUNET_TESTBED_operation_cancel (topology_op);
262 for (nhost = 0; nhost < num_hosts; nhost++)
263 if (NULL != hosts[nhost])
264 GNUNET_TESTBED_host_destroy (hosts[nhost]);
265 GNUNET_free_non_null (hosts);
266 while (NULL != (dll_op = dll_op_head))
268 GNUNET_TESTBED_operation_cancel (dll_op->op);
269 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
270 GNUNET_free (dll_op);
273 GNUNET_TESTBED_controller_disconnect (mc);
275 GNUNET_TESTBED_controller_stop (mc_proc);
277 GNUNET_CONFIGURATION_destroy (cfg);
278 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
283 * abort task to run on test timed out
286 * @param tc the task context
289 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
291 LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
292 abort_task = GNUNET_SCHEDULER_NO_TASK;
293 result = GNUNET_SYSERR;
294 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
295 GNUNET_SCHEDULER_cancel (shutdown_task);
296 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
304 * Functions of this signature are called when a peer has been successfully
305 * started or stopped.
307 * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
308 * @param emsg NULL on success; otherwise an error description
311 peer_churn_cb (void *cls, const char *emsg)
313 struct DLLOperation *dll_op = cls;
314 struct GNUNET_TESTBED_Operation *op;
315 static unsigned int started_peers;
318 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
319 GNUNET_free (dll_op);
322 LOG (GNUNET_ERROR_TYPE_WARNING,
323 _("An operation has failed while starting peers\n"));
324 GNUNET_TESTBED_operation_done (op);
325 GNUNET_SCHEDULER_cancel (abort_task);
326 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
329 GNUNET_TESTBED_operation_done (op);
330 if (++started_peers == num_peers)
332 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
333 printf ("%u peers started successfully in %.2f seconds\n",
334 num_peers, ((double) prof_time.rel_value) / 1000.00);
337 if ((0 == num_links) && (topology == GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI))
339 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
342 state = STATE_PEERS_LINKING;
343 /* Do overlay connect */
344 prof_start_time = GNUNET_TIME_absolute_get ();
347 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
349 GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peers,
352 GNUNET_TESTBED_TOPOLOGY_DISABLE_AUTO_RETRY,
353 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
355 case GNUNET_TESTBED_TOPOLOGY_CLIQUE:
357 GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peers,
359 GNUNET_TESTBED_TOPOLOGY_DISABLE_AUTO_RETRY,
360 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
361 num_links = num_peers * (num_peers - 1);
371 * Functions of this signature are called when a peer has been successfully
374 * @param cls the closure from GNUNET_TESTBED_peer_create()
375 * @param peer the handle for the created peer; NULL on any error during
377 * @param emsg NULL if peer is not NULL; else MAY contain the error description
380 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
382 struct DLLOperation *dll_op = cls;
383 struct GNUNET_TESTBED_Peer **peer_ptr;
384 static unsigned int created_peers;
385 unsigned int peer_cnt;
389 LOG (GNUNET_ERROR_TYPE_WARNING,
390 _("Creating a peer failed. Error: %s\n"), emsg);
391 GNUNET_TESTBED_operation_done (dll_op->op);
392 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
393 GNUNET_free (dll_op);
394 GNUNET_SCHEDULER_cancel (abort_task);
395 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
398 peer_ptr = dll_op->cls;
399 GNUNET_assert (NULL == *peer_ptr);
401 GNUNET_TESTBED_operation_done (dll_op->op);
402 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
403 GNUNET_free (dll_op);
404 if (++created_peers == num_peers)
406 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
407 printf ("%u peers created successfully in %.2f seconds\n",
408 num_peers, ((double) prof_time.rel_value) / 1000.00);
410 /* Now peers are to be started */
411 state = STATE_PEERS_STARTING;
412 prof_start_time = GNUNET_TIME_absolute_get ();
413 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
415 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
416 dll_op->op = GNUNET_TESTBED_peer_start (dll_op, peers[peer_cnt],
417 &peer_churn_cb, dll_op);
418 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
425 * Function to print summary about how many overlay links we have made and how
429 print_overlay_links_summary ()
431 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
432 printf ("\n%u links established in %.2f seconds\n",
433 established_links, ((double) prof_time.rel_value) / 1000.00);
434 printf ("%u links failed due to timeouts\n", failed_links);
439 * Function to start peers
444 struct DLLOperation *dll_op;
445 unsigned int peer_cnt;
447 state = STATE_PEERS_CREATING;
448 prof_start_time = GNUNET_TIME_absolute_get ();
449 peers = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *)
451 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
453 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
454 dll_op->cls = &peers[peer_cnt];
455 dll_op->op = GNUNET_TESTBED_peer_create (mc,
457 [peer_cnt % num_hosts],
461 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
467 * Controller event callback
470 * @param event the controller event
473 controller_event_cb (void *cls,
474 const struct GNUNET_TESTBED_EventInformation *event)
476 struct DLLOperation *dll_op;
477 struct GNUNET_TESTBED_Operation *op;
481 case STATE_SLAVES_STARTING:
484 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
486 static unsigned int slaves_started;
488 dll_op = event->details.operation_finished.op_cls;
489 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
490 GNUNET_free (dll_op);
491 op = event->details.operation_finished.operation;
492 if (NULL != event->details.operation_finished.emsg)
494 LOG (GNUNET_ERROR_TYPE_WARNING,
495 _("An operation has failed while starting slaves\n"));
496 GNUNET_TESTBED_operation_done (op);
497 GNUNET_SCHEDULER_cancel (abort_task);
498 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
501 GNUNET_TESTBED_operation_done (op);
502 /* Proceed to start peers */
503 if (++slaves_started == num_hosts - 1)
505 printf ("%u controllers started successfully\n", num_hosts);
515 case STATE_PEERS_STARTING:
518 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
519 /* Control reaches here when peer start fails */
520 case GNUNET_TESTBED_ET_PEER_START:
521 /* we handle peer starts in peer_churn_cb */
527 case STATE_PEERS_LINKING:
530 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
531 /* Control reaches here when a peer linking operation fails */
532 if (NULL != event->details.operation_finished.emsg)
537 if (++cont_fails > num_cont_fails)
539 printf ("\nAborting due to very high failure rate");
540 print_overlay_links_summary ();
541 GNUNET_SCHEDULER_cancel (abort_task);
542 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
547 case GNUNET_TESTBED_ET_CONNECT:
551 if (0 == established_links)
552 printf ("Establishing links. Please wait\n");
561 if ((established_links + failed_links) == num_links)
563 print_overlay_links_summary ();
565 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
575 * Task to register all hosts available in the global host list
578 * @param tc the scheduler task context
581 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
585 * Callback which will be called to after a host registration succeeded or failed
587 * @param cls the closure
588 * @param emsg the error message; NULL if host registration is successful
591 host_registration_completion (void *cls, const char *emsg)
596 LOG (GNUNET_ERROR_TYPE_WARNING,
597 _("Host registration failed for a host. Error: %s\n"), emsg);
598 GNUNET_SCHEDULER_cancel (abort_task);
599 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
602 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
607 * Task to register all hosts available in the global host list
610 * @param tc the scheduler task context
613 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
615 struct DLLOperation *dll_op;
616 static unsigned int reg_host;
619 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
620 if (reg_host == num_hosts - 1)
622 LOG (GNUNET_ERROR_TYPE_DEBUG,
623 "All hosts successfully registered\n");
625 state = STATE_SLAVES_STARTING;
626 for (slave = 1; slave < num_hosts; slave++)
628 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
629 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
635 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
639 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
640 host_registration_completion,
646 * Callback to signal successfull startup of the controller process
648 * @param cls the closure from GNUNET_TESTBED_controller_start()
649 * @param config the configuration with which the controller has been started;
650 * NULL if status is not GNUNET_OK
651 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
652 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
655 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
657 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
658 GNUNET_SCHEDULER_cancel (abort_task);
659 if (GNUNET_OK != status)
662 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
666 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
667 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
668 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
669 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
670 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
671 mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
672 &controller_event_cb, NULL);
675 LOG (GNUNET_ERROR_TYPE_WARNING,
676 _("Unable to connect to master controller -- Check config\n"));
677 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
681 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
684 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
690 * Main function that will be run by the scheduler.
693 * @param args remaining command-line arguments
694 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
695 * @param config configuration
698 run (void *cls, char *const *args, const char *cfgfile,
699 const struct GNUNET_CONFIGURATION_Handle *config)
705 fprintf (stderr, _("No hosts-file specified on command line\n"));
713 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
716 fprintf (stderr, _("No hosts loaded. Need atleast one host\n"));
719 for (nhost = 0; nhost < num_hosts; nhost++)
721 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
723 fprintf (stderr, _("Host %s cannot start testbed\n"),
724 GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
728 if (num_hosts != nhost)
730 fprintf (stderr, _("Exiting\n"));
731 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
734 cfg = GNUNET_CONFIGURATION_dup (config);
736 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname_
743 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
744 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
750 * Set an option of type 'char *' from the command line.
751 * A pointer to this function should be passed as part of the
752 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
753 * of this type. It should be followed by a pointer to a value of
756 * @param ctx command line processing context
757 * @param scls additional closure (will point to the 'char *',
758 * which will be allocated)
759 * @param option name of the option
760 * @param value actual value of the option (a string)
761 * @return GNUNET_OK to continue procesing; GNUNET_SYSERR to signal error
764 set_topology (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
765 void *scls, const char *option, const char *value)
767 enum GNUNET_TESTBED_TopologyOption *val = scls;
769 if (0 == strncasecmp ("CLIQUE", value, strlen ("CLIQUE")))
771 *val = GNUNET_TESTBED_TOPOLOGY_CLIQUE;
774 if (0 == strncasecmp ("RANDOM", value, strlen ("RANDOM")))
776 *val = GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI;
779 FPRINTF (stderr, "%s", _("Only `CLIQUE' and `RANDOM' are permitted.\n"));
780 return GNUNET_SYSERR;
787 * @return 0 on success
790 main (int argc, char *const *argv)
792 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
793 { 'p', "num-peers", "COUNT",
794 gettext_noop ("create COUNT number of peers"),
795 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
796 { 'n', "num-links", "COUNT",
797 gettext_noop ("create COUNT number of random links"),
798 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links },
799 { 'e', "num-errors", "COUNT",
800 gettext_noop ("tolerate COUNT number of continious timeout failures"),
801 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_cont_fails },
802 { 't', "topology", "TOPOLOGY",
803 gettext_noop ("Try to acheive TOPOLOGY. This options takes either CLIQUE "
804 "or RANDOM. For CLIQUE the parameter -n is ignored. The "
805 "default is to acheive a random graph topology."),
806 GNUNET_YES, &set_topology, &topology },
807 GNUNET_GETOPT_OPTION_END
811 topology = GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI;
812 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
815 result = GNUNET_SYSERR;
817 GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS] hosts-file",
818 _("Profiler for testbed"),
819 options, &run, NULL);
820 if (GNUNET_OK != ret)
822 if (GNUNET_OK != result)