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_from (kind, "testbed-api-testbed", __VA_ARGS__)
44 * The testbed operation handle
46 struct GNUNET_TESTBED_Operation *op;
54 * The next pointer for DLL
56 struct DLLOperation *next;
59 * The prev pointer for DLL
61 struct DLLOperation *prev;
66 * Availanle states during profiling
78 STATE_SLAVES_STARTING,
98 * An array of hosts loaded from the hostkeys file
100 static struct GNUNET_TESTBED_Host **hosts;
103 * The array of peers; we fill this as the peers are given to us by the testbed
105 static struct GNUNET_TESTBED_Peer **peers;
108 /* * Operation handle */
110 /* static struct GNUNET_TESTBED_Operation *op; */
113 * Host registration handle
115 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
118 * Handle to the master controller process
120 struct GNUNET_TESTBED_ControllerProc *mc_proc;
123 * Handle to the master controller
125 struct GNUNET_TESTBED_Controller *mc;
128 * Handle to global configuration
130 struct GNUNET_CONFIGURATION_Handle *cfg;
133 * Head of the operations list
135 struct DLLOperation *dll_op_head;
138 * Tail of the operations list
140 struct DLLOperation *dll_op_tail;
143 * Peer linking - topology operation
145 struct GNUNET_TESTBED_Operation *topology_op;
148 * Abort task identifier
150 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
153 * Host registration task identifier
155 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
158 * Global event mask for all testbed events
163 * The starting time of a profiling step
165 struct GNUNET_TIME_Absolute prof_start_time;
168 * Duration profiling step has taken
170 struct GNUNET_TIME_Relative prof_time;
175 unsigned int peer_id;
178 * Number of peers to be started by the profiler
180 static unsigned int num_peers;
183 * Number of hosts in the hosts array
185 static unsigned int num_hosts;
188 * Number of random links to be established between peers
190 static unsigned int num_links;
193 * Global testing status
198 * current state of profiling
207 * @param tc the task context
210 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
212 struct DLLOperation *dll_op;
215 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
216 GNUNET_SCHEDULER_cancel (abort_task);
217 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
218 GNUNET_SCHEDULER_cancel (register_hosts_task);
219 if (NULL != reg_handle)
220 GNUNET_TESTBED_cancel_registration (reg_handle);
221 if (NULL != topology_op)
222 GNUNET_TESTBED_operation_cancel (topology_op);
223 for (nhost = 0; nhost < num_hosts; nhost++)
224 if (NULL != hosts[nhost])
225 GNUNET_TESTBED_host_destroy (hosts[nhost]);
226 GNUNET_free_non_null (hosts);
227 while (NULL != (dll_op = dll_op_head))
229 GNUNET_TESTBED_operation_cancel (dll_op->op);
230 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
231 GNUNET_free (dll_op);
234 GNUNET_TESTBED_controller_disconnect (mc);
236 GNUNET_TESTBED_controller_stop (mc_proc);
238 GNUNET_CONFIGURATION_destroy (cfg);
239 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
244 * abort task to run on test timed out
247 * @param tc the task context
250 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
252 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
253 abort_task = GNUNET_SCHEDULER_NO_TASK;
254 result = GNUNET_SYSERR;
255 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
260 * Functions of this signature are called when a peer has been successfully
261 * started or stopped.
263 * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
264 * @param emsg NULL on success; otherwise an error description
267 peer_churn_cb (void *cls, const char *emsg)
269 struct DLLOperation *dll_op = cls;
270 struct GNUNET_TESTBED_Operation *op;
271 static unsigned int started_peers;
274 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
275 GNUNET_free (dll_op);
278 LOG (GNUNET_ERROR_TYPE_WARNING,
279 _("An operation has failed while starting peers\n"));
280 GNUNET_TESTBED_operation_done (op);
281 GNUNET_SCHEDULER_cancel (abort_task);
282 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
285 GNUNET_TESTBED_operation_done (op);
286 if (++started_peers == num_peers)
288 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
289 printf ("All peers started successfully in %.2f seconds\n",
290 ((double) prof_time.rel_value) / 1000.00);
294 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
297 state = STATE_PEERS_LINKING;
298 /* Do overlay connect */
299 prof_start_time = GNUNET_TIME_absolute_get ();
301 GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peers,
302 GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
309 * Functions of this signature are called when a peer has been successfully
312 * @param cls the closure from GNUNET_TESTBED_peer_create()
313 * @param peer the handle for the created peer; NULL on any error during
315 * @param emsg NULL if peer is not NULL; else MAY contain the error description
318 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
320 struct DLLOperation *dll_op = cls;
321 struct GNUNET_TESTBED_Peer **peer_ptr;
322 static unsigned int created_peers;
323 unsigned int peer_cnt;
327 LOG (GNUNET_ERROR_TYPE_WARNING,
328 _("Creating a peer failed. Error: %s\n"), emsg);
329 GNUNET_TESTBED_operation_done (dll_op->op);
330 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
331 GNUNET_free (dll_op);
332 GNUNET_SCHEDULER_cancel (abort_task);
333 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
336 peer_ptr = dll_op->cls;
337 GNUNET_assert (NULL == *peer_ptr);
339 GNUNET_TESTBED_operation_done (dll_op->op);
340 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
341 GNUNET_free (dll_op);
342 if (++created_peers == num_peers)
344 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
345 printf ("All peers created successfully in %.2f seconds\n",
346 ((double) prof_time.rel_value) / 1000.00);
347 /* Now peers are to be started */
348 state = STATE_PEERS_STARTING;
349 prof_start_time = GNUNET_TIME_absolute_get ();
350 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
352 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
353 dll_op->op = GNUNET_TESTBED_peer_start (dll_op, peers[peer_cnt],
354 &peer_churn_cb, dll_op);
355 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
362 * Controller event callback
365 * @param event the controller event
368 controller_event_cb (void *cls,
369 const struct GNUNET_TESTBED_EventInformation *event)
371 struct DLLOperation *dll_op;
372 struct GNUNET_TESTBED_Operation *op;
376 case STATE_SLAVES_STARTING:
379 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
381 static unsigned int slaves_started;
382 unsigned int peer_cnt;
384 dll_op = event->details.operation_finished.op_cls;
385 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
386 GNUNET_free (dll_op);
387 op = event->details.operation_finished.operation;
388 if (NULL != event->details.operation_finished.emsg)
390 LOG (GNUNET_ERROR_TYPE_WARNING,
391 _("An operation has failed while starting slaves\n"));
392 GNUNET_TESTBED_operation_done (op);
393 GNUNET_SCHEDULER_cancel (abort_task);
394 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
397 GNUNET_TESTBED_operation_done (op);
398 /* Proceed to start peers */
399 if (++slaves_started == num_hosts - 1)
401 printf ("All slaves started successfully\n");
402 state = STATE_PEERS_CREATING;
403 prof_start_time = GNUNET_TIME_absolute_get ();
404 peers = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *)
406 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
408 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
409 dll_op->cls = &peers[peer_cnt];
410 dll_op->op = GNUNET_TESTBED_peer_create (mc,
412 [peer_cnt % num_hosts],
416 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
425 case STATE_PEERS_STARTING:
428 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
429 /* Control reaches here when peer start fails */
430 case GNUNET_TESTBED_ET_PEER_START:
431 /* we handle peer starts in peer_churn_cb */
437 case STATE_PEERS_LINKING:
440 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
441 /* Control reaches here when a peer linking operation fails */
442 if (NULL != event->details.operation_finished.emsg)
444 LOG (GNUNET_ERROR_TYPE_WARNING,
445 _("An operation has failed while starting slaves\n"));
446 GNUNET_SCHEDULER_cancel (abort_task);
447 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
450 case GNUNET_TESTBED_ET_CONNECT:
452 static unsigned int established_links;
454 if (0 == established_links)
455 printf ("Establishing links\n");
458 if (++established_links == num_links)
460 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
461 printf ("%u links established in %.2f seconds\n",
462 num_links, ((double) prof_time.rel_value) / 1000.00);
464 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
479 * Task to register all hosts available in the global host list
482 * @param tc the scheduler task context
485 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
489 * Callback which will be called to after a host registration succeeded or failed
491 * @param cls the closure
492 * @param emsg the error message; NULL if host registration is successful
495 host_registration_completion (void *cls, const char *emsg)
500 LOG (GNUNET_ERROR_TYPE_WARNING,
501 _("Host registration failed for a host. Error: %s\n"), emsg);
502 GNUNET_SCHEDULER_cancel (abort_task);
503 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
506 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
511 * Task to register all hosts available in the global host list
514 * @param tc the scheduler task context
517 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
519 struct DLLOperation *dll_op;
520 static unsigned int reg_host;
523 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
524 if (reg_host == num_hosts - 1)
526 LOG (GNUNET_ERROR_TYPE_DEBUG,
527 "All hosts successfully registered\n");
529 state = STATE_SLAVES_STARTING;
530 for (slave = 1; slave < num_hosts; slave++)
532 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
533 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
539 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
543 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
544 host_registration_completion,
550 * Callback to signal successfull startup of the controller process
552 * @param cls the closure from GNUNET_TESTBED_controller_start()
553 * @param cfg the configuration with which the controller has been started;
554 * NULL if status is not GNUNET_OK
555 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
556 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
559 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
561 GNUNET_SCHEDULER_cancel (abort_task);
562 if (GNUNET_OK != status)
565 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
569 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
570 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
571 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
572 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
573 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
574 mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
575 &controller_event_cb, NULL);
578 LOG (GNUNET_ERROR_TYPE_WARNING,
579 _("Unable to connect to master controller -- Check config\n"));
580 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
583 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
584 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
590 * Main function that will be run by the scheduler.
593 * @param args remaining command-line arguments
594 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
595 * @param config configuration
598 run (void *cls, char *const *args, const char *cfgfile,
599 const struct GNUNET_CONFIGURATION_Handle *config)
605 fprintf (stderr, _("No hosts-file specified on command line\n"));
613 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
616 fprintf (stderr, _("No hosts loaded. Need atleast one host\n"));
619 for (nhost = 0; nhost < num_hosts; nhost++)
621 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
623 fprintf (stderr, _("Host %s cannot start testbed\n"),
624 GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
628 if (num_hosts != nhost)
630 fprintf (stderr, _("Exiting\n"));
631 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
634 cfg = GNUNET_CONFIGURATION_dup (config);
636 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname_
643 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
644 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
652 * @return 0 on success
655 main (int argc, char *const *argv)
657 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
658 { 'p', "num-peers", "COUNT",
659 gettext_noop ("create COUNT number of peers"),
660 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
661 { 'n', "num-links", "COUNT",
662 gettext_noop ("create COUNT number of random links"),
663 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links },
664 GNUNET_GETOPT_OPTION_END
668 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
671 result = GNUNET_SYSERR;
673 GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS] hosts-file",
674 _("Profiler for testbed"),
675 options, &run, NULL);
676 if (GNUNET_OK != ret)
678 if (GNUNET_OK != result)