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_testbed_service.h"
30 #include "testbed_api_hosts.h"
33 * Generic loggins shorthand
35 #define LOG(kind,...) \
36 GNUNET_log (kind, __VA_ARGS__)
45 * The testbed operation handle
47 struct GNUNET_TESTBED_Operation *op;
55 * The next pointer for DLL
57 struct DLLOperation *next;
60 * The prev pointer for DLL
62 struct DLLOperation *prev;
67 * Availanle states during profiling
79 STATE_SLAVES_STARTING,
97 * Destroying peers; we can do this as the controller takes care of stopping a
98 * peer if it is running
100 STATE_PEERS_DESTROYING
105 * An array of hosts loaded from the hostkeys file
107 static struct GNUNET_TESTBED_Host **hosts;
110 * The array of peers; we fill this as the peers are given to us by the testbed
112 static struct GNUNET_TESTBED_Peer **peers;
115 /* * Operation handle */
117 /* static struct GNUNET_TESTBED_Operation *op; */
120 * Host registration handle
122 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
125 * Handle to the master controller process
127 struct GNUNET_TESTBED_ControllerProc *mc_proc;
130 * Handle to the master controller
132 struct GNUNET_TESTBED_Controller *mc;
135 * Handle to global configuration
137 struct GNUNET_CONFIGURATION_Handle *cfg;
140 * Head of the operations list
142 struct DLLOperation *dll_op_head;
145 * Tail of the operations list
147 struct DLLOperation *dll_op_tail;
150 * Peer linking - topology operation
152 struct GNUNET_TESTBED_Operation *topology_op;
155 * Abort task identifier
157 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
160 * Shutdown task identifier
162 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
165 * Host registration task identifier
167 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
170 * Global event mask for all testbed events
175 * The starting time of a profiling step
177 struct GNUNET_TIME_Absolute prof_start_time;
180 * Duration profiling step has taken
182 struct GNUNET_TIME_Relative prof_time;
187 unsigned int peer_id;
190 * Number of peers to be started by the profiler
192 static unsigned int num_peers;
195 * Number of hosts in the hosts array
197 static unsigned int num_hosts;
200 * Number of random links to be established between peers
202 static unsigned int num_links;
205 * Number of timeout failures to tolerate
207 static unsigned int num_cont_fails;
210 * Continuous failures during overlay connect operations
212 static unsigned int cont_fails;
215 * Links which are successfully established
217 static unsigned int established_links;
220 * Links which are not successfully established
222 static unsigned int failed_links;
225 * Global testing status
230 * current state of profiling
235 * The topology we want to acheive
237 enum GNUNET_TESTBED_TopologyOption topology;
244 * @param tc the task context
247 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
249 struct DLLOperation *dll_op;
252 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
253 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
254 GNUNET_SCHEDULER_cancel (abort_task);
255 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
256 GNUNET_SCHEDULER_cancel (register_hosts_task);
257 if (NULL != reg_handle)
258 GNUNET_TESTBED_cancel_registration (reg_handle);
259 if (NULL != topology_op)
260 GNUNET_TESTBED_operation_cancel (topology_op);
261 for (nhost = 0; nhost < num_hosts; nhost++)
262 if (NULL != hosts[nhost])
263 GNUNET_TESTBED_host_destroy (hosts[nhost]);
264 GNUNET_free_non_null (hosts);
265 while (NULL != (dll_op = dll_op_head))
267 GNUNET_TESTBED_operation_cancel (dll_op->op);
268 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
269 GNUNET_free (dll_op);
272 GNUNET_TESTBED_controller_disconnect (mc);
274 GNUNET_TESTBED_controller_stop (mc_proc);
276 GNUNET_CONFIGURATION_destroy (cfg);
277 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
282 * abort task to run on test timed out
285 * @param tc the task context
288 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
290 LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
291 abort_task = GNUNET_SCHEDULER_NO_TASK;
292 result = GNUNET_SYSERR;
293 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
294 GNUNET_SCHEDULER_cancel (shutdown_task);
295 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
303 * Functions of this signature are called when a peer has been successfully
304 * started or stopped.
306 * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
307 * @param emsg NULL on success; otherwise an error description
310 peer_churn_cb (void *cls, const char *emsg)
312 struct DLLOperation *dll_op = cls;
313 struct GNUNET_TESTBED_Operation *op;
314 static unsigned int started_peers;
317 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
318 GNUNET_free (dll_op);
321 LOG (GNUNET_ERROR_TYPE_WARNING,
322 _("An operation has failed while starting peers\n"));
323 GNUNET_TESTBED_operation_done (op);
324 GNUNET_SCHEDULER_cancel (abort_task);
325 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
328 GNUNET_TESTBED_operation_done (op);
329 if (++started_peers == num_peers)
331 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
332 printf ("%u peers started successfully in %.2f seconds\n",
333 num_peers, ((double) prof_time.rel_value) / 1000.00);
336 if ((0 == num_links) && (topology == GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI))
338 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
341 state = STATE_PEERS_LINKING;
342 /* Do overlay connect */
343 prof_start_time = GNUNET_TIME_absolute_get ();
346 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
348 GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peers,
351 GNUNET_TESTBED_TOPOLOGY_DISABLE_AUTO_RETRY,
352 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
354 case GNUNET_TESTBED_TOPOLOGY_CLIQUE:
356 GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peers,
358 GNUNET_TESTBED_TOPOLOGY_DISABLE_AUTO_RETRY,
359 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
369 * Functions of this signature are called when a peer has been successfully
372 * @param cls the closure from GNUNET_TESTBED_peer_create()
373 * @param peer the handle for the created peer; NULL on any error during
375 * @param emsg NULL if peer is not NULL; else MAY contain the error description
378 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
380 struct DLLOperation *dll_op = cls;
381 struct GNUNET_TESTBED_Peer **peer_ptr;
382 static unsigned int created_peers;
383 unsigned int peer_cnt;
387 LOG (GNUNET_ERROR_TYPE_WARNING,
388 _("Creating a peer failed. Error: %s\n"), emsg);
389 GNUNET_TESTBED_operation_done (dll_op->op);
390 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
391 GNUNET_free (dll_op);
392 GNUNET_SCHEDULER_cancel (abort_task);
393 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
396 peer_ptr = dll_op->cls;
397 GNUNET_assert (NULL == *peer_ptr);
399 GNUNET_TESTBED_operation_done (dll_op->op);
400 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
401 GNUNET_free (dll_op);
402 if (++created_peers == num_peers)
404 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
405 printf ("%u peers created successfully in %.2f seconds\n",
406 num_peers, ((double) prof_time.rel_value) / 1000.00);
408 /* Now peers are to be started */
409 state = STATE_PEERS_STARTING;
410 prof_start_time = GNUNET_TIME_absolute_get ();
411 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
413 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
414 dll_op->op = GNUNET_TESTBED_peer_start (dll_op, peers[peer_cnt],
415 &peer_churn_cb, dll_op);
416 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
423 * Function to print summary about how many overlay links we have made and how
427 print_overlay_links_summary ()
429 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
430 printf ("\n%u links established in %.2f seconds\n",
431 established_links, ((double) prof_time.rel_value) / 1000.00);
432 printf ("%u links failed due to timeouts\n", failed_links);
437 * Function to start peers
442 struct DLLOperation *dll_op;
443 unsigned int peer_cnt;
445 state = STATE_PEERS_CREATING;
446 prof_start_time = GNUNET_TIME_absolute_get ();
447 peers = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *)
449 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
451 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
452 dll_op->cls = &peers[peer_cnt];
453 dll_op->op = GNUNET_TESTBED_peer_create (mc,
455 [peer_cnt % num_hosts],
459 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
465 * Controller event callback
468 * @param event the controller event
471 controller_event_cb (void *cls,
472 const struct GNUNET_TESTBED_EventInformation *event)
474 struct DLLOperation *dll_op;
475 struct GNUNET_TESTBED_Operation *op;
479 case STATE_SLAVES_STARTING:
482 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
484 static unsigned int slaves_started;
486 dll_op = event->details.operation_finished.op_cls;
487 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
488 GNUNET_free (dll_op);
489 op = event->details.operation_finished.operation;
490 if (NULL != event->details.operation_finished.emsg)
492 LOG (GNUNET_ERROR_TYPE_WARNING,
493 _("An operation has failed while starting slaves\n"));
494 GNUNET_TESTBED_operation_done (op);
495 GNUNET_SCHEDULER_cancel (abort_task);
496 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
499 GNUNET_TESTBED_operation_done (op);
500 /* Proceed to start peers */
501 if (++slaves_started == num_hosts - 1)
503 printf ("%u controllers started successfully\n", num_hosts);
513 case STATE_PEERS_STARTING:
516 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
517 /* Control reaches here when peer start fails */
518 case GNUNET_TESTBED_ET_PEER_START:
519 /* we handle peer starts in peer_churn_cb */
525 case STATE_PEERS_LINKING:
528 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
529 /* Control reaches here when a peer linking operation fails */
530 if (NULL != event->details.operation_finished.emsg)
535 if (++cont_fails > num_cont_fails)
537 printf ("\nAborting due to very high failure rate");
538 print_overlay_links_summary ();
539 GNUNET_SCHEDULER_cancel (abort_task);
540 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
544 case GNUNET_TESTBED_ET_CONNECT:
548 if (0 == established_links)
549 printf ("Establishing links. Please wait\n");
553 if ((established_links + failed_links) ==
554 (GNUNET_TESTBED_TOPOLOGY_CLIQUE == topology ?
555 num_peers * (num_peers -1) : num_links))
557 print_overlay_links_summary ();
559 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
574 * Task to register all hosts available in the global host list
577 * @param tc the scheduler task context
580 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
584 * Callback which will be called to after a host registration succeeded or failed
586 * @param cls the closure
587 * @param emsg the error message; NULL if host registration is successful
590 host_registration_completion (void *cls, const char *emsg)
595 LOG (GNUNET_ERROR_TYPE_WARNING,
596 _("Host registration failed for a host. Error: %s\n"), emsg);
597 GNUNET_SCHEDULER_cancel (abort_task);
598 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
601 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
606 * Task to register all hosts available in the global host list
609 * @param tc the scheduler task context
612 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
614 struct DLLOperation *dll_op;
615 static unsigned int reg_host;
618 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
619 if (reg_host == num_hosts - 1)
621 LOG (GNUNET_ERROR_TYPE_DEBUG,
622 "All hosts successfully registered\n");
624 state = STATE_SLAVES_STARTING;
625 for (slave = 1; slave < num_hosts; slave++)
627 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
628 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
634 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
638 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
639 host_registration_completion,
645 * Callback to signal successfull startup of the controller process
647 * @param cls the closure from GNUNET_TESTBED_controller_start()
648 * @param config the configuration with which the controller has been started;
649 * NULL if status is not GNUNET_OK
650 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
651 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
654 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
656 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
657 GNUNET_SCHEDULER_cancel (abort_task);
658 if (GNUNET_OK != status)
661 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
665 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
666 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
667 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
668 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
669 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
670 mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
671 &controller_event_cb, NULL);
674 LOG (GNUNET_ERROR_TYPE_WARNING,
675 _("Unable to connect to master controller -- Check config\n"));
676 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
680 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
683 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
689 * Main function that will be run by the scheduler.
692 * @param args remaining command-line arguments
693 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
694 * @param config configuration
697 run (void *cls, char *const *args, const char *cfgfile,
698 const struct GNUNET_CONFIGURATION_Handle *config)
704 fprintf (stderr, _("No hosts-file specified on command line\n"));
712 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
715 fprintf (stderr, _("No hosts loaded. Need atleast one host\n"));
718 for (nhost = 0; nhost < num_hosts; nhost++)
720 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
722 fprintf (stderr, _("Host %s cannot start testbed\n"),
723 GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
727 if (num_hosts != nhost)
729 fprintf (stderr, _("Exiting\n"));
730 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
733 cfg = GNUNET_CONFIGURATION_dup (config);
735 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname_
742 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
743 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
749 * Set an option of type 'char *' from the command line.
750 * A pointer to this function should be passed as part of the
751 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
752 * of this type. It should be followed by a pointer to a value of
755 * @param ctx command line processing context
756 * @param scls additional closure (will point to the 'char *',
757 * which will be allocated)
758 * @param option name of the option
759 * @param value actual value of the option (a string)
760 * @return GNUNET_OK to continue procesing; GNUNET_SYSERR to signal error
763 set_topology (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
764 void *scls, const char *option, const char *value)
766 enum GNUNET_TESTBED_TopologyOption *val = scls;
768 if (0 == strncasecmp ("CLIQUE", value, strlen ("CLIQUE")))
770 *val = GNUNET_TESTBED_TOPOLOGY_CLIQUE;
773 if (0 == strncasecmp ("RANDOM", value, strlen ("RANDOM")))
775 *val = GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI;
778 FPRINTF (stderr, _("Only `CLIQUE' and `RANDOM' are permitted"));
779 return GNUNET_SYSERR;
786 * @return 0 on success
789 main (int argc, char *const *argv)
791 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
792 { 'p', "num-peers", "COUNT",
793 gettext_noop ("create COUNT number of peers"),
794 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
795 { 'n', "num-links", "COUNT",
796 gettext_noop ("create COUNT number of random links"),
797 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links },
798 { 'e', "num-errors", "COUNT",
799 gettext_noop ("tolerate COUNT number of continious timeout failures"),
800 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_cont_fails },
801 { 't', "topology", "TOPOLOGY",
802 gettext_noop ("Try to acheive TOPOLOGY. This options takes either CLIQUE "
803 "or RANDOM. For CLIQUE the parameter -n is ignored. The "
804 "default is to acheive a random graph topology."),
805 GNUNET_YES, &set_topology, &topology },
806 GNUNET_GETOPT_OPTION_END
810 topology = GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI;
811 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
814 result = GNUNET_SYSERR;
816 GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS] hosts-file",
817 _("Profiler for testbed"),
818 options, &run, NULL);
819 if (GNUNET_OK != ret)
821 if (GNUNET_OK != result)