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__)
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,
96 * Destroying peers; we can do this as the controller takes care of stopping a
97 * peer if it is running
99 STATE_PEERS_DESTROYING
104 * An array of hosts loaded from the hostkeys file
106 static struct GNUNET_TESTBED_Host **hosts;
109 * The array of peers; we fill this as the peers are given to us by the testbed
111 static struct GNUNET_TESTBED_Peer **peers;
114 /* * Operation handle */
116 /* static struct GNUNET_TESTBED_Operation *op; */
119 * Host registration handle
121 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
124 * Handle to the master controller process
126 struct GNUNET_TESTBED_ControllerProc *mc_proc;
129 * Handle to the master controller
131 struct GNUNET_TESTBED_Controller *mc;
134 * Handle to global configuration
136 struct GNUNET_CONFIGURATION_Handle *cfg;
139 * Head of the operations list
141 struct DLLOperation *dll_op_head;
144 * Tail of the operations list
146 struct DLLOperation *dll_op_tail;
149 * Peer linking - topology operation
151 struct GNUNET_TESTBED_Operation *topology_op;
154 * Abort task identifier
156 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
159 * Host registration task identifier
161 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
164 * Global event mask for all testbed events
169 * The starting time of a profiling step
171 struct GNUNET_TIME_Absolute prof_start_time;
174 * Duration profiling step has taken
176 struct GNUNET_TIME_Relative prof_time;
181 unsigned int peer_id;
184 * Number of peers to be started by the profiler
186 static unsigned int num_peers;
189 * Number of hosts in the hosts array
191 static unsigned int num_hosts;
194 * Number of random links to be established between peers
196 static unsigned int num_links;
199 * Global testing status
204 * current state of profiling
213 * @param tc the task context
216 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
218 struct DLLOperation *dll_op;
221 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
222 GNUNET_SCHEDULER_cancel (abort_task);
223 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
224 GNUNET_SCHEDULER_cancel (register_hosts_task);
225 if (NULL != reg_handle)
226 GNUNET_TESTBED_cancel_registration (reg_handle);
227 if (NULL != topology_op)
228 GNUNET_TESTBED_operation_cancel (topology_op);
229 for (nhost = 0; nhost < num_hosts; nhost++)
230 if (NULL != hosts[nhost])
231 GNUNET_TESTBED_host_destroy (hosts[nhost]);
232 GNUNET_free_non_null (hosts);
233 while (NULL != (dll_op = dll_op_head))
235 GNUNET_TESTBED_operation_cancel (dll_op->op);
236 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
237 GNUNET_free (dll_op);
240 GNUNET_TESTBED_controller_disconnect (mc);
242 GNUNET_TESTBED_controller_stop (mc_proc);
244 GNUNET_CONFIGURATION_destroy (cfg);
245 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
250 * abort task to run on test timed out
253 * @param tc the task context
256 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
258 LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
259 abort_task = GNUNET_SCHEDULER_NO_TASK;
260 result = GNUNET_SYSERR;
261 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
269 * Functions of this signature are called when a peer has been successfully
270 * started or stopped.
272 * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
273 * @param emsg NULL on success; otherwise an error description
276 peer_churn_cb (void *cls, const char *emsg)
278 struct DLLOperation *dll_op = cls;
279 struct GNUNET_TESTBED_Operation *op;
280 static unsigned int started_peers;
283 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
284 GNUNET_free (dll_op);
287 LOG (GNUNET_ERROR_TYPE_WARNING,
288 _("An operation has failed while starting peers\n"));
289 GNUNET_TESTBED_operation_done (op);
290 GNUNET_SCHEDULER_cancel (abort_task);
291 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
294 GNUNET_TESTBED_operation_done (op);
295 if (++started_peers == num_peers)
297 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
298 printf ("All peers started successfully in %.2f seconds\n",
299 ((double) prof_time.rel_value) / 1000.00);
303 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
306 state = STATE_PEERS_LINKING;
307 /* Do overlay connect */
308 prof_start_time = GNUNET_TIME_absolute_get ();
310 GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peers,
311 GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
318 * Functions of this signature are called when a peer has been successfully
321 * @param cls the closure from GNUNET_TESTBED_peer_create()
322 * @param peer the handle for the created peer; NULL on any error during
324 * @param emsg NULL if peer is not NULL; else MAY contain the error description
327 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
329 struct DLLOperation *dll_op = cls;
330 struct GNUNET_TESTBED_Peer **peer_ptr;
331 static unsigned int created_peers;
332 unsigned int peer_cnt;
336 LOG (GNUNET_ERROR_TYPE_WARNING,
337 _("Creating a peer failed. Error: %s\n"), emsg);
338 GNUNET_TESTBED_operation_done (dll_op->op);
339 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
340 GNUNET_free (dll_op);
341 GNUNET_SCHEDULER_cancel (abort_task);
342 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
345 peer_ptr = dll_op->cls;
346 GNUNET_assert (NULL == *peer_ptr);
348 GNUNET_TESTBED_operation_done (dll_op->op);
349 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
350 GNUNET_free (dll_op);
351 if (++created_peers == num_peers)
353 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
354 printf ("All peers created successfully in %.2f seconds\n",
355 ((double) prof_time.rel_value) / 1000.00);
356 /* Now peers are to be started */
357 state = STATE_PEERS_STARTING;
358 prof_start_time = GNUNET_TIME_absolute_get ();
359 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
361 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
362 dll_op->op = GNUNET_TESTBED_peer_start (dll_op, peers[peer_cnt],
363 &peer_churn_cb, dll_op);
364 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
371 * Controller event callback
374 * @param event the controller event
377 controller_event_cb (void *cls,
378 const struct GNUNET_TESTBED_EventInformation *event)
380 struct DLLOperation *dll_op;
381 struct GNUNET_TESTBED_Operation *op;
385 case STATE_SLAVES_STARTING:
388 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
390 static unsigned int slaves_started;
391 unsigned int peer_cnt;
393 dll_op = event->details.operation_finished.op_cls;
394 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
395 GNUNET_free (dll_op);
396 op = event->details.operation_finished.operation;
397 if (NULL != event->details.operation_finished.emsg)
399 LOG (GNUNET_ERROR_TYPE_WARNING,
400 _("An operation has failed while starting slaves\n"));
401 GNUNET_TESTBED_operation_done (op);
402 GNUNET_SCHEDULER_cancel (abort_task);
403 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
406 GNUNET_TESTBED_operation_done (op);
407 /* Proceed to start peers */
408 if (++slaves_started == num_hosts - 1)
410 printf ("All slaves started successfully\n");
411 state = STATE_PEERS_CREATING;
412 prof_start_time = GNUNET_TIME_absolute_get ();
413 peers = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *)
415 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
417 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
418 dll_op->cls = &peers[peer_cnt];
419 dll_op->op = GNUNET_TESTBED_peer_create (mc,
421 [peer_cnt % num_hosts],
425 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
434 case STATE_PEERS_STARTING:
437 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
438 /* Control reaches here when peer start fails */
439 case GNUNET_TESTBED_ET_PEER_START:
440 /* we handle peer starts in peer_churn_cb */
446 case STATE_PEERS_LINKING:
449 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
450 /* Control reaches here when a peer linking operation fails */
451 if (NULL != event->details.operation_finished.emsg)
453 LOG (GNUNET_ERROR_TYPE_WARNING,
454 _("An operation has failed while linking\n"));
455 GNUNET_SCHEDULER_cancel (abort_task);
456 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
459 case GNUNET_TESTBED_ET_CONNECT:
461 static unsigned int established_links;
463 if (0 == established_links)
464 printf ("Establishing links\n .");
470 if (++established_links == num_links)
472 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
473 printf ("\n%u links established in %.2f seconds\n",
474 num_links, ((double) prof_time.rel_value) / 1000.00);
476 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
491 * Task to register all hosts available in the global host list
494 * @param tc the scheduler task context
497 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
501 * Callback which will be called to after a host registration succeeded or failed
503 * @param cls the closure
504 * @param emsg the error message; NULL if host registration is successful
507 host_registration_completion (void *cls, const char *emsg)
512 LOG (GNUNET_ERROR_TYPE_WARNING,
513 _("Host registration failed for a host. Error: %s\n"), emsg);
514 GNUNET_SCHEDULER_cancel (abort_task);
515 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
518 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
523 * Task to register all hosts available in the global host list
526 * @param tc the scheduler task context
529 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
531 struct DLLOperation *dll_op;
532 static unsigned int reg_host;
535 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
536 if (reg_host == num_hosts - 1)
538 LOG (GNUNET_ERROR_TYPE_DEBUG,
539 "All hosts successfully registered\n");
541 state = STATE_SLAVES_STARTING;
542 for (slave = 1; slave < num_hosts; slave++)
544 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
545 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
551 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
555 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
556 host_registration_completion,
562 * Callback to signal successfull startup of the controller process
564 * @param cls the closure from GNUNET_TESTBED_controller_start()
565 * @param cfg the configuration with which the controller has been started;
566 * NULL if status is not GNUNET_OK
567 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
568 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
571 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
573 GNUNET_SCHEDULER_cancel (abort_task);
574 if (GNUNET_OK != status)
577 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
581 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
582 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
583 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
584 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
585 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
586 mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
587 &controller_event_cb, NULL);
590 LOG (GNUNET_ERROR_TYPE_WARNING,
591 _("Unable to connect to master controller -- Check config\n"));
592 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
595 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
596 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
602 * Main function that will be run by the scheduler.
605 * @param args remaining command-line arguments
606 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
607 * @param config configuration
610 run (void *cls, char *const *args, const char *cfgfile,
611 const struct GNUNET_CONFIGURATION_Handle *config)
617 fprintf (stderr, _("No hosts-file specified on command line\n"));
625 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
628 fprintf (stderr, _("No hosts loaded. Need atleast one host\n"));
631 for (nhost = 0; nhost < num_hosts; nhost++)
633 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
635 fprintf (stderr, _("Host %s cannot start testbed\n"),
636 GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
640 if (num_hosts != nhost)
642 fprintf (stderr, _("Exiting\n"));
643 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
646 cfg = GNUNET_CONFIGURATION_dup (config);
648 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname_
655 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
656 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
664 * @return 0 on success
667 main (int argc, char *const *argv)
669 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
670 { 'p', "num-peers", "COUNT",
671 gettext_noop ("create COUNT number of peers"),
672 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
673 { 'n', "num-links", "COUNT",
674 gettext_noop ("create COUNT number of random links"),
675 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links },
676 GNUNET_GETOPT_OPTION_END
680 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
683 result = GNUNET_SYSERR;
685 GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS] hosts-file",
686 _("Profiler for testbed"),
687 options, &run, NULL);
688 if (GNUNET_OK != ret)
690 if (GNUNET_OK != result)