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 * Number of times we try overlay connect operations
212 static unsigned int retry_links;
215 * Continuous failures during overlay connect operations
217 static unsigned int cont_fails;
220 * Global testing status
225 * current state of profiling
234 * @param tc the task context
237 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
239 struct DLLOperation *dll_op;
242 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
243 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
244 GNUNET_SCHEDULER_cancel (abort_task);
245 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
246 GNUNET_SCHEDULER_cancel (register_hosts_task);
247 if (NULL != reg_handle)
248 GNUNET_TESTBED_cancel_registration (reg_handle);
249 if (NULL != topology_op)
250 GNUNET_TESTBED_operation_cancel (topology_op);
251 for (nhost = 0; nhost < num_hosts; nhost++)
252 if (NULL != hosts[nhost])
253 GNUNET_TESTBED_host_destroy (hosts[nhost]);
254 GNUNET_free_non_null (hosts);
255 while (NULL != (dll_op = dll_op_head))
257 GNUNET_TESTBED_operation_cancel (dll_op->op);
258 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
259 GNUNET_free (dll_op);
262 GNUNET_TESTBED_controller_disconnect (mc);
264 GNUNET_TESTBED_controller_stop (mc_proc);
266 GNUNET_CONFIGURATION_destroy (cfg);
267 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
272 * abort task to run on test timed out
275 * @param tc the task context
278 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
280 LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
281 abort_task = GNUNET_SCHEDULER_NO_TASK;
282 result = GNUNET_SYSERR;
283 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
284 GNUNET_SCHEDULER_cancel (shutdown_task);
285 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
293 * Functions of this signature are called when a peer has been successfully
294 * started or stopped.
296 * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
297 * @param emsg NULL on success; otherwise an error description
300 peer_churn_cb (void *cls, const char *emsg)
302 struct DLLOperation *dll_op = cls;
303 struct GNUNET_TESTBED_Operation *op;
304 static unsigned int started_peers;
307 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
308 GNUNET_free (dll_op);
311 LOG (GNUNET_ERROR_TYPE_WARNING,
312 _("An operation has failed while starting peers\n"));
313 GNUNET_TESTBED_operation_done (op);
314 GNUNET_SCHEDULER_cancel (abort_task);
315 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
318 GNUNET_TESTBED_operation_done (op);
319 if (++started_peers == num_peers)
321 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
322 printf ("%u peers started successfully in %.2f seconds\n",
323 num_peers, ((double) prof_time.rel_value) / 1000.00);
328 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
331 state = STATE_PEERS_LINKING;
332 /* Do overlay connect */
333 prof_start_time = GNUNET_TIME_absolute_get ();
335 GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peers,
336 GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
344 * Functions of this signature are called when a peer has been successfully
347 * @param cls the closure from GNUNET_TESTBED_peer_create()
348 * @param peer the handle for the created peer; NULL on any error during
350 * @param emsg NULL if peer is not NULL; else MAY contain the error description
353 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
355 struct DLLOperation *dll_op = cls;
356 struct GNUNET_TESTBED_Peer **peer_ptr;
357 static unsigned int created_peers;
358 unsigned int peer_cnt;
362 LOG (GNUNET_ERROR_TYPE_WARNING,
363 _("Creating a peer failed. Error: %s\n"), emsg);
364 GNUNET_TESTBED_operation_done (dll_op->op);
365 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
366 GNUNET_free (dll_op);
367 GNUNET_SCHEDULER_cancel (abort_task);
368 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
371 peer_ptr = dll_op->cls;
372 GNUNET_assert (NULL == *peer_ptr);
374 GNUNET_TESTBED_operation_done (dll_op->op);
375 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
376 GNUNET_free (dll_op);
377 if (++created_peers == num_peers)
379 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
380 printf ("%u peers created successfully in %.2f seconds\n",
381 num_peers, ((double) prof_time.rel_value) / 1000.00);
383 /* Now peers are to be started */
384 state = STATE_PEERS_STARTING;
385 prof_start_time = GNUNET_TIME_absolute_get ();
386 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
388 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
389 dll_op->op = GNUNET_TESTBED_peer_start (dll_op, peers[peer_cnt],
390 &peer_churn_cb, dll_op);
391 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
398 * Function to print summary about how many overlay links we have made and how
402 print_overlay_links_summary ()
404 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
405 printf ("\n%u links established in %.2f seconds\n",
406 num_links, ((double) prof_time.rel_value) / 1000.00);
407 printf ("Overlay link operations have been retried %u times upon timeouts\n",
413 * Controller event callback
416 * @param event the controller event
419 controller_event_cb (void *cls,
420 const struct GNUNET_TESTBED_EventInformation *event)
422 struct DLLOperation *dll_op;
423 struct GNUNET_TESTBED_Operation *op;
427 case STATE_SLAVES_STARTING:
430 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
432 static unsigned int slaves_started;
433 unsigned int peer_cnt;
435 dll_op = event->details.operation_finished.op_cls;
436 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
437 GNUNET_free (dll_op);
438 op = event->details.operation_finished.operation;
439 if (NULL != event->details.operation_finished.emsg)
441 LOG (GNUNET_ERROR_TYPE_WARNING,
442 _("An operation has failed while starting slaves\n"));
443 GNUNET_TESTBED_operation_done (op);
444 GNUNET_SCHEDULER_cancel (abort_task);
445 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
448 GNUNET_TESTBED_operation_done (op);
449 /* Proceed to start peers */
450 if (++slaves_started == num_hosts - 1)
452 printf ("%u controllers started successfully\n", num_hosts);
454 state = STATE_PEERS_CREATING;
455 prof_start_time = GNUNET_TIME_absolute_get ();
456 peers = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *)
458 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
460 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
461 dll_op->cls = &peers[peer_cnt];
462 dll_op->op = GNUNET_TESTBED_peer_create (mc,
464 [peer_cnt % num_hosts],
468 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
477 case STATE_PEERS_STARTING:
480 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
481 /* Control reaches here when peer start fails */
482 case GNUNET_TESTBED_ET_PEER_START:
483 /* we handle peer starts in peer_churn_cb */
489 case STATE_PEERS_LINKING:
492 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
493 /* Control reaches here when a peer linking operation fails */
494 if (NULL != event->details.operation_finished.emsg)
496 LOG (GNUNET_ERROR_TYPE_WARNING,
497 _("An operation has failed while linking\n"));
501 if (++cont_fails > num_cont_fails)
503 printf ("\nAborting due to very high failure rate");
504 print_overlay_links_summary ();
505 GNUNET_SCHEDULER_cancel (abort_task);
506 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
510 case GNUNET_TESTBED_ET_CONNECT:
512 static unsigned int established_links;
516 if (0 == established_links)
517 printf ("Establishing links. Please wait\n");
520 if (++established_links == num_links)
522 print_overlay_links_summary ();
524 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
539 * Task to register all hosts available in the global host list
542 * @param tc the scheduler task context
545 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
549 * Callback which will be called to after a host registration succeeded or failed
551 * @param cls the closure
552 * @param emsg the error message; NULL if host registration is successful
555 host_registration_completion (void *cls, const char *emsg)
560 LOG (GNUNET_ERROR_TYPE_WARNING,
561 _("Host registration failed for a host. Error: %s\n"), emsg);
562 GNUNET_SCHEDULER_cancel (abort_task);
563 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
566 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
571 * Task to register all hosts available in the global host list
574 * @param tc the scheduler task context
577 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
579 struct DLLOperation *dll_op;
580 static unsigned int reg_host;
583 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
584 if (reg_host == num_hosts - 1)
586 LOG (GNUNET_ERROR_TYPE_DEBUG,
587 "All hosts successfully registered\n");
589 state = STATE_SLAVES_STARTING;
590 for (slave = 1; slave < num_hosts; slave++)
592 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
593 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
599 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
603 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
604 host_registration_completion,
610 * Callback to signal successfull startup of the controller process
612 * @param cls the closure from GNUNET_TESTBED_controller_start()
613 * @param cfg the configuration with which the controller has been started;
614 * NULL if status is not GNUNET_OK
615 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
616 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
619 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
621 GNUNET_SCHEDULER_cancel (abort_task);
622 if (GNUNET_OK != status)
625 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
629 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
630 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
631 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
632 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
633 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
634 mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
635 &controller_event_cb, NULL);
638 LOG (GNUNET_ERROR_TYPE_WARNING,
639 _("Unable to connect to master controller -- Check config\n"));
640 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
643 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
644 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
650 * Main function that will be run by the scheduler.
653 * @param args remaining command-line arguments
654 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
655 * @param config configuration
658 run (void *cls, char *const *args, const char *cfgfile,
659 const struct GNUNET_CONFIGURATION_Handle *config)
665 fprintf (stderr, _("No hosts-file specified on command line\n"));
673 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
676 fprintf (stderr, _("No hosts loaded. Need atleast one host\n"));
679 for (nhost = 0; nhost < num_hosts; nhost++)
681 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
683 fprintf (stderr, _("Host %s cannot start testbed\n"),
684 GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
688 if (num_hosts != nhost)
690 fprintf (stderr, _("Exiting\n"));
691 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
694 cfg = GNUNET_CONFIGURATION_dup (config);
696 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname_
703 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
704 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
712 * @return 0 on success
715 main (int argc, char *const *argv)
717 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
718 { 'p', "num-peers", "COUNT",
719 gettext_noop ("create COUNT number of peers"),
720 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
721 { 'n', "num-links", "COUNT",
722 gettext_noop ("create COUNT number of random links"),
723 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links },
724 { 'e', "num-errors", "COUNT",
725 gettext_noop ("tolerate COUNT number of continious timeout failures"),
726 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_cont_fails },
727 GNUNET_GETOPT_OPTION_END
731 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
734 result = GNUNET_SYSERR;
736 GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS] hosts-file",
737 _("Profiler for testbed"),
738 options, &run, NULL);
739 if (GNUNET_OK != ret)
741 if (GNUNET_OK != result)