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
239 * @param tc the task context
242 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
244 struct DLLOperation *dll_op;
247 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
248 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
249 GNUNET_SCHEDULER_cancel (abort_task);
250 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
251 GNUNET_SCHEDULER_cancel (register_hosts_task);
252 if (NULL != reg_handle)
253 GNUNET_TESTBED_cancel_registration (reg_handle);
254 if (NULL != topology_op)
255 GNUNET_TESTBED_operation_cancel (topology_op);
256 for (nhost = 0; nhost < num_hosts; nhost++)
257 if (NULL != hosts[nhost])
258 GNUNET_TESTBED_host_destroy (hosts[nhost]);
259 GNUNET_free_non_null (hosts);
260 while (NULL != (dll_op = dll_op_head))
262 GNUNET_TESTBED_operation_cancel (dll_op->op);
263 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
264 GNUNET_free (dll_op);
267 GNUNET_TESTBED_controller_disconnect (mc);
269 GNUNET_TESTBED_controller_stop (mc_proc);
271 GNUNET_CONFIGURATION_destroy (cfg);
272 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
277 * abort task to run on test timed out
280 * @param tc the task context
283 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
285 LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
286 abort_task = GNUNET_SCHEDULER_NO_TASK;
287 result = GNUNET_SYSERR;
288 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
289 GNUNET_SCHEDULER_cancel (shutdown_task);
290 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
298 * Functions of this signature are called when a peer has been successfully
299 * started or stopped.
301 * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
302 * @param emsg NULL on success; otherwise an error description
305 peer_churn_cb (void *cls, const char *emsg)
307 struct DLLOperation *dll_op = cls;
308 struct GNUNET_TESTBED_Operation *op;
309 static unsigned int started_peers;
312 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
313 GNUNET_free (dll_op);
316 LOG (GNUNET_ERROR_TYPE_WARNING,
317 _("An operation has failed while starting peers\n"));
318 GNUNET_TESTBED_operation_done (op);
319 GNUNET_SCHEDULER_cancel (abort_task);
320 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
323 GNUNET_TESTBED_operation_done (op);
324 if (++started_peers == num_peers)
326 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
327 printf ("%u peers started successfully in %.2f seconds\n",
328 num_peers, ((double) prof_time.rel_value) / 1000.00);
333 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
336 state = STATE_PEERS_LINKING;
337 /* Do overlay connect */
338 prof_start_time = GNUNET_TIME_absolute_get ();
340 GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peers,
341 GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
343 GNUNET_TESTBED_TOPOLOGY_DISABLE_AUTO_RETRY,
344 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
350 * Functions of this signature are called when a peer has been successfully
353 * @param cls the closure from GNUNET_TESTBED_peer_create()
354 * @param peer the handle for the created peer; NULL on any error during
356 * @param emsg NULL if peer is not NULL; else MAY contain the error description
359 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
361 struct DLLOperation *dll_op = cls;
362 struct GNUNET_TESTBED_Peer **peer_ptr;
363 static unsigned int created_peers;
364 unsigned int peer_cnt;
368 LOG (GNUNET_ERROR_TYPE_WARNING,
369 _("Creating a peer failed. Error: %s\n"), emsg);
370 GNUNET_TESTBED_operation_done (dll_op->op);
371 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
372 GNUNET_free (dll_op);
373 GNUNET_SCHEDULER_cancel (abort_task);
374 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
377 peer_ptr = dll_op->cls;
378 GNUNET_assert (NULL == *peer_ptr);
380 GNUNET_TESTBED_operation_done (dll_op->op);
381 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
382 GNUNET_free (dll_op);
383 if (++created_peers == num_peers)
385 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
386 printf ("%u peers created successfully in %.2f seconds\n",
387 num_peers, ((double) prof_time.rel_value) / 1000.00);
389 /* Now peers are to be started */
390 state = STATE_PEERS_STARTING;
391 prof_start_time = GNUNET_TIME_absolute_get ();
392 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
394 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
395 dll_op->op = GNUNET_TESTBED_peer_start (dll_op, peers[peer_cnt],
396 &peer_churn_cb, dll_op);
397 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
404 * Function to print summary about how many overlay links we have made and how
408 print_overlay_links_summary ()
410 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
411 printf ("\n%u links established in %.2f seconds\n",
412 established_links, ((double) prof_time.rel_value) / 1000.00);
413 printf ("%u links failed due to timeouts\n", failed_links);
418 * Function to start peers
423 struct DLLOperation *dll_op;
424 unsigned int peer_cnt;
426 state = STATE_PEERS_CREATING;
427 prof_start_time = GNUNET_TIME_absolute_get ();
428 peers = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *)
430 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
432 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
433 dll_op->cls = &peers[peer_cnt];
434 dll_op->op = GNUNET_TESTBED_peer_create (mc,
436 [peer_cnt % num_hosts],
440 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
446 * Controller event callback
449 * @param event the controller event
452 controller_event_cb (void *cls,
453 const struct GNUNET_TESTBED_EventInformation *event)
455 struct DLLOperation *dll_op;
456 struct GNUNET_TESTBED_Operation *op;
460 case STATE_SLAVES_STARTING:
463 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
465 static unsigned int slaves_started;
467 dll_op = event->details.operation_finished.op_cls;
468 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
469 GNUNET_free (dll_op);
470 op = event->details.operation_finished.operation;
471 if (NULL != event->details.operation_finished.emsg)
473 LOG (GNUNET_ERROR_TYPE_WARNING,
474 _("An operation has failed while starting slaves\n"));
475 GNUNET_TESTBED_operation_done (op);
476 GNUNET_SCHEDULER_cancel (abort_task);
477 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
480 GNUNET_TESTBED_operation_done (op);
481 /* Proceed to start peers */
482 if (++slaves_started == num_hosts - 1)
484 printf ("%u controllers started successfully\n", num_hosts);
494 case STATE_PEERS_STARTING:
497 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
498 /* Control reaches here when peer start fails */
499 case GNUNET_TESTBED_ET_PEER_START:
500 /* we handle peer starts in peer_churn_cb */
506 case STATE_PEERS_LINKING:
509 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
510 /* Control reaches here when a peer linking operation fails */
511 if (NULL != event->details.operation_finished.emsg)
516 if (++cont_fails > num_cont_fails)
518 printf ("\nAborting due to very high failure rate");
519 print_overlay_links_summary ();
520 GNUNET_SCHEDULER_cancel (abort_task);
521 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
525 case GNUNET_TESTBED_ET_CONNECT:
529 if (0 == established_links)
530 printf ("Establishing links. Please wait\n");
534 if ((established_links + failed_links) == num_links)
536 print_overlay_links_summary ();
538 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
553 * Task to register all hosts available in the global host list
556 * @param tc the scheduler task context
559 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
563 * Callback which will be called to after a host registration succeeded or failed
565 * @param cls the closure
566 * @param emsg the error message; NULL if host registration is successful
569 host_registration_completion (void *cls, const char *emsg)
574 LOG (GNUNET_ERROR_TYPE_WARNING,
575 _("Host registration failed for a host. Error: %s\n"), emsg);
576 GNUNET_SCHEDULER_cancel (abort_task);
577 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
580 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
585 * Task to register all hosts available in the global host list
588 * @param tc the scheduler task context
591 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
593 struct DLLOperation *dll_op;
594 static unsigned int reg_host;
597 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
598 if (reg_host == num_hosts - 1)
600 LOG (GNUNET_ERROR_TYPE_DEBUG,
601 "All hosts successfully registered\n");
603 state = STATE_SLAVES_STARTING;
604 for (slave = 1; slave < num_hosts; slave++)
606 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
607 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
613 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
617 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
618 host_registration_completion,
624 * Callback to signal successfull startup of the controller process
626 * @param cls the closure from GNUNET_TESTBED_controller_start()
627 * @param config the configuration with which the controller has been started;
628 * NULL if status is not GNUNET_OK
629 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
630 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
633 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
635 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
636 GNUNET_SCHEDULER_cancel (abort_task);
637 if (GNUNET_OK != status)
640 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
644 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
645 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
646 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
647 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
648 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
649 mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
650 &controller_event_cb, NULL);
653 LOG (GNUNET_ERROR_TYPE_WARNING,
654 _("Unable to connect to master controller -- Check config\n"));
655 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
659 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
662 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
668 * Main function that will be run by the scheduler.
671 * @param args remaining command-line arguments
672 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
673 * @param config configuration
676 run (void *cls, char *const *args, const char *cfgfile,
677 const struct GNUNET_CONFIGURATION_Handle *config)
683 fprintf (stderr, _("No hosts-file specified on command line\n"));
691 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
694 fprintf (stderr, _("No hosts loaded. Need atleast one host\n"));
697 for (nhost = 0; nhost < num_hosts; nhost++)
699 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
701 fprintf (stderr, _("Host %s cannot start testbed\n"),
702 GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
706 if (num_hosts != nhost)
708 fprintf (stderr, _("Exiting\n"));
709 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
712 cfg = GNUNET_CONFIGURATION_dup (config);
714 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname_
721 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
722 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
730 * @return 0 on success
733 main (int argc, char *const *argv)
735 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
736 { 'p', "num-peers", "COUNT",
737 gettext_noop ("create COUNT number of peers"),
738 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
739 { 'n', "num-links", "COUNT",
740 gettext_noop ("create COUNT number of random links"),
741 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links },
742 { 'e', "num-errors", "COUNT",
743 gettext_noop ("tolerate COUNT number of continious timeout failures"),
744 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_cont_fails },
745 GNUNET_GETOPT_OPTION_END
749 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
752 result = GNUNET_SYSERR;
754 GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS] hosts-file",
755 _("Profiler for testbed"),
756 options, &run, NULL);
757 if (GNUNET_OK != ret)
759 if (GNUNET_OK != result)