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,
88 * An array of hosts loaded from the hostkeys file
90 static struct GNUNET_TESTBED_Host **hosts;
93 * The array of peers; we fill this as the peers are given to us by the testbed
95 static struct GNUNET_TESTBED_Peer **peers;
98 /* * Operation handle */
100 /* static struct GNUNET_TESTBED_Operation *op; */
103 * Host registration handle
105 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
108 * Handle to the master controller process
110 struct GNUNET_TESTBED_ControllerProc *mc_proc;
113 * Handle to the master controller
115 struct GNUNET_TESTBED_Controller *mc;
118 * Handle to global configuration
120 struct GNUNET_CONFIGURATION_Handle *cfg;
123 * Head of the operations list
125 struct DLLOperation *dll_op_head;
128 * Tail of the operations list
130 struct DLLOperation *dll_op_tail;
133 * Abort task identifier
135 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
138 * Host registration task identifier
140 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
143 * Global event mask for all testbed events
148 * The starting time of a profiling step
150 struct GNUNET_TIME_Absolute prof_start_time;
153 * Duration profiling step has taken
155 struct GNUNET_TIME_Relative prof_time;
160 unsigned int peer_id;
163 * Number of peers to be started by the profiler
165 static unsigned int num_peers;
168 * Number of hosts in the hosts array
170 static unsigned int num_hosts;
173 * Global testing status
178 * current state of profiling
187 * @param tc the task context
190 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
192 struct DLLOperation *dll_op;
195 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
196 GNUNET_SCHEDULER_cancel (abort_task);
197 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
198 GNUNET_SCHEDULER_cancel (register_hosts_task);
199 if (NULL != reg_handle)
200 GNUNET_TESTBED_cancel_registration (reg_handle);
201 for (nhost = 0; nhost < num_hosts; nhost++)
202 if (NULL != hosts[nhost])
203 GNUNET_TESTBED_host_destroy (hosts[nhost]);
204 GNUNET_free_non_null (hosts);
205 while (NULL != (dll_op = dll_op_head))
207 GNUNET_TESTBED_operation_cancel (dll_op->op);
208 GNUNET_free_non_null (dll_op->cls);
209 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
210 GNUNET_free (dll_op);
213 GNUNET_TESTBED_controller_disconnect (mc);
215 GNUNET_TESTBED_controller_stop (mc_proc);
217 GNUNET_CONFIGURATION_destroy (cfg);
218 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
223 * abort task to run on test timed out
226 * @param tc the task context
229 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
231 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
232 abort_task = GNUNET_SCHEDULER_NO_TASK;
233 result = GNUNET_SYSERR;
234 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
239 * Functions of this signature are called when a peer has been successfully
242 * @param cls the closure from GNUNET_TESTBED_peer_create()
243 * @param peer the handle for the created peer; NULL on any error during
245 * @param emsg NULL if peer is not NULL; else MAY contain the error description
248 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
250 struct DLLOperation *dll_op = cls;
251 struct GNUNET_TESTBED_Peer **peer_ptr;
252 static unsigned int created_peers;
256 LOG (GNUNET_ERROR_TYPE_WARNING,
257 _("Creating a peer failed. Error: %s\n"), emsg);
258 GNUNET_TESTBED_operation_done (dll_op->op);
259 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
260 GNUNET_free (dll_op);
261 GNUNET_SCHEDULER_cancel (abort_task);
262 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
265 peer_ptr = dll_op->cls;
266 GNUNET_assert (NULL == *peer_ptr);
268 GNUNET_TESTBED_operation_done (dll_op->op);
269 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
270 GNUNET_free (dll_op);
271 if (++created_peers == num_peers)
273 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
274 printf ("All peers created successfully in %.2f seconds\n",
275 ((double) prof_time.rel_value) / 1000.00);
276 /* Now peers are to be started */
282 * Controller event callback
285 * @param event the controller event
288 controller_event_cb (void *cls,
289 const struct GNUNET_TESTBED_EventInformation *event)
291 struct DLLOperation *dll_op;
292 struct GNUNET_TESTBED_Operation *op;
296 case STATE_SLAVES_STARTING:
299 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
301 static unsigned int slaves_started;
302 unsigned int peer_cnt;
304 dll_op = event->details.operation_finished.op_cls;
305 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
306 GNUNET_free (dll_op);
307 op = event->details.operation_finished.operation;
308 if (NULL != event->details.operation_finished.emsg)
310 LOG (GNUNET_ERROR_TYPE_WARNING,
311 _("An operation has failed while starting slaves\n"));
312 GNUNET_TESTBED_operation_done (op);
313 GNUNET_SCHEDULER_cancel (abort_task);
314 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
317 GNUNET_TESTBED_operation_done (op);
318 /* Proceed to start peers */
319 if (++slaves_started == num_hosts - 1)
321 printf ("All slaves started successfully\n");
322 state = STATE_PEERS_CREATING;
323 prof_start_time = GNUNET_TIME_absolute_get ();
324 peers = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *)
326 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
328 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
329 dll_op->cls = &peers[peer_cnt];
330 dll_op->op = GNUNET_TESTBED_peer_create (mc,
332 [peer_cnt % num_peers],
336 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
352 * Task to register all hosts available in the global host list
355 * @param tc the scheduler task context
358 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
362 * Callback which will be called to after a host registration succeeded or failed
364 * @param cls the closure
365 * @param emsg the error message; NULL if host registration is successful
368 host_registration_completion (void *cls, const char *emsg)
373 LOG (GNUNET_ERROR_TYPE_WARNING,
374 _("Host registration failed for a host. Error: %s\n"), emsg);
375 GNUNET_SCHEDULER_cancel (abort_task);
376 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
379 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
384 * Task to register all hosts available in the global host list
387 * @param tc the scheduler task context
390 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
392 struct DLLOperation *dll_op;
393 static unsigned int reg_host;
396 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
397 if (reg_host == num_hosts - 1)
399 LOG (GNUNET_ERROR_TYPE_DEBUG,
400 "All hosts successfully registered\n");
402 state = STATE_SLAVES_STARTING;
403 for (slave = 1; slave < num_hosts; slave++)
405 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
406 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
412 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
416 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
417 host_registration_completion,
423 * Callback to signal successfull startup of the controller process
425 * @param cls the closure from GNUNET_TESTBED_controller_start()
426 * @param cfg the configuration with which the controller has been started;
427 * NULL if status is not GNUNET_OK
428 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
429 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
432 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
434 GNUNET_SCHEDULER_cancel (abort_task);
435 if (GNUNET_OK != status)
438 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
442 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
443 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
444 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
445 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
446 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
447 mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
448 &controller_event_cb, NULL);
451 LOG (GNUNET_ERROR_TYPE_WARNING,
452 _("Unable to connect to master controller -- Check config\n"));
453 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
456 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
457 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
463 * Main function that will be run by the scheduler.
466 * @param args remaining command-line arguments
467 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
468 * @param cfg configuration
471 run (void *cls, char *const *args, const char *cfgfile,
472 const struct GNUNET_CONFIGURATION_Handle *config)
478 fprintf (stderr, _("No hosts-file specified on command line\n"));
486 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
489 fprintf (stderr, _("No hosts loaded. Need atleast one host\n"));
492 for (nhost = 0; nhost < num_hosts; nhost++)
494 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
496 fprintf (stderr, _("Host %s cannot start testbed\n"),
497 GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
501 if (num_hosts != nhost)
503 fprintf (stderr, _("Exiting\n"));
504 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
507 cfg = GNUNET_CONFIGURATION_dup (config);
509 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname_
516 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
517 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
525 * @return 0 on success
528 main (int argc, char *const *argv)
530 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
531 { 'n', "num-peers", "COUNT",
532 gettext_noop ("create COUNT number of peers"),
533 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
534 { 'n', "num-peers", "COUNT",
535 gettext_noop ("create COUNT number of peers"),
536 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
537 GNUNET_GETOPT_OPTION_END
541 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
544 result = GNUNET_SYSERR;
546 GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS] hosts-file",
547 _("Profiler for testbed"),
548 options, &run, NULL);
549 if (GNUNET_OK != ret)
551 if (GNUNET_OK != result)