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
83 * An array of hosts loaded from the hostkeys file
85 static struct GNUNET_TESTBED_Host **hosts;
88 * The array of peers; we fill this as the peers are given to us by the testbed
90 static struct GNUNET_TESTBED_Peer **peers;
93 /* * Operation handle */
95 /* static struct GNUNET_TESTBED_Operation *op; */
98 * Host registration handle
100 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
103 * Handle to the master controller process
105 struct GNUNET_TESTBED_ControllerProc *mc_proc;
108 * Handle to the master controller
110 struct GNUNET_TESTBED_Controller *mc;
113 * Handle to global configuration
115 struct GNUNET_CONFIGURATION_Handle *cfg;
118 * Head of the operations list
120 struct DLLOperation *dll_op_head;
123 * Tail of the operations list
125 struct DLLOperation *dll_op_tail;
128 * Abort task identifier
130 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
133 * Host registration task identifier
135 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
138 * Global event mask for all testbed events
145 unsigned int peer_id;
148 * Number of peers to be started by the profiler
150 static unsigned int num_peers;
153 * Number of hosts in the hosts array
155 static unsigned int num_hosts;
158 * Global testing status
163 * current state of profiling
172 * @param tc the task context
175 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
177 struct DLLOperation *dll_op;
180 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
181 GNUNET_SCHEDULER_cancel (abort_task);
182 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
183 GNUNET_SCHEDULER_cancel (register_hosts_task);
184 GNUNET_free_non_null (peers);
185 if (NULL != reg_handle)
186 GNUNET_TESTBED_cancel_registration (reg_handle);
187 for (nhost = 0; nhost < num_hosts; nhost++)
188 if (NULL != hosts[nhost])
189 GNUNET_TESTBED_host_destroy (hosts[nhost]);
190 GNUNET_free_non_null (hosts);
191 while (NULL != (dll_op = dll_op_head))
193 GNUNET_TESTBED_operation_cancel (dll_op->op);
194 GNUNET_free_non_null (dll_op->cls);
195 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
196 GNUNET_free (dll_op);
199 GNUNET_TESTBED_controller_disconnect (mc);
201 GNUNET_TESTBED_controller_stop (mc_proc);
203 GNUNET_CONFIGURATION_destroy (cfg);
204 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
209 * abort task to run on test timed out
212 * @param tc the task context
215 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
217 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
218 abort_task = GNUNET_SCHEDULER_NO_TASK;
219 result = GNUNET_SYSERR;
220 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
225 * Controller event callback
228 * @param event the controller event
231 controller_event_cb (void *cls,
232 const struct GNUNET_TESTBED_EventInformation *event)
234 struct DLLOperation *dll_op;
235 struct GNUNET_TESTBED_Operation *op;
239 case STATE_SLAVES_STARTING:
242 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
244 static unsigned int slaves_started;
246 dll_op = event->details.operation_finished.op_cls;
247 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
248 GNUNET_free (dll_op);
249 op = event->details.operation_finished.operation;
250 if (NULL != event->details.operation_finished.emsg)
252 LOG (GNUNET_ERROR_TYPE_WARNING,
253 _("An operation has failed while starting slaves\n"));
254 GNUNET_TESTBED_operation_done (op);
255 GNUNET_SCHEDULER_cancel (abort_task);
256 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
259 GNUNET_TESTBED_operation_done (op);
260 /* Proceed to start peers */
261 if (++slaves_started == num_hosts - 1)
262 printf ("All slaves started successfully\n");
276 * Task to register all hosts available in the global host list
279 * @param tc the scheduler task context
282 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
286 * Callback which will be called to after a host registration succeeded or failed
288 * @param cls the closure
289 * @param emsg the error message; NULL if host registration is successful
292 host_registration_completion (void *cls, const char *emsg)
297 LOG (GNUNET_ERROR_TYPE_WARNING,
298 _("Host registration failed for a host. Error: %s\n"), emsg);
299 GNUNET_SCHEDULER_cancel (abort_task);
300 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
303 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
308 * Task to register all hosts available in the global host list
311 * @param tc the scheduler task context
314 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
316 struct DLLOperation *dll_op;
317 static unsigned int reg_host;
320 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
321 if (reg_host == num_hosts - 1)
323 LOG (GNUNET_ERROR_TYPE_DEBUG,
324 "All hosts successfully registered\n");
326 state = STATE_SLAVES_STARTING;
327 for (slave = 1; slave < num_hosts; slave++)
329 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
330 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
336 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
340 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
341 host_registration_completion,
347 * Callback to signal successfull startup of the controller process
349 * @param cls the closure from GNUNET_TESTBED_controller_start()
350 * @param cfg the configuration with which the controller has been started;
351 * NULL if status is not GNUNET_OK
352 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
353 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
356 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
358 GNUNET_SCHEDULER_cancel (abort_task);
359 if (GNUNET_OK != status)
362 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
366 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
367 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
368 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
369 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
370 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
371 mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
372 &controller_event_cb, NULL);
375 LOG (GNUNET_ERROR_TYPE_WARNING,
376 _("Unable to connect to master controller -- Check config\n"));
377 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
380 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
381 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
387 * Main function that will be run by the scheduler.
390 * @param args remaining command-line arguments
391 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
392 * @param cfg configuration
395 run (void *cls, char *const *args, const char *cfgfile,
396 const struct GNUNET_CONFIGURATION_Handle *config)
402 fprintf (stderr, _("No hosts-file specified on command line\n"));
410 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
413 fprintf (stderr, _("No hosts loaded. Need atleast one host\n"));
416 for (nhost = 0; nhost < num_hosts; nhost++)
418 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
420 fprintf (stderr, _("Host %s cannot start testbed\n"),
421 GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
425 if (num_hosts != nhost)
427 fprintf (stderr, _("Exiting\n"));
428 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
431 cfg = GNUNET_CONFIGURATION_dup (config);
433 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname_
440 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
441 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
449 * @return 0 on success
452 main (int argc, char *const *argv)
454 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
455 { 'n', "num-peers", "COUNT",
456 gettext_noop ("create COUNT number of peers"),
457 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
458 { 'n', "num-peers", "COUNT",
459 gettext_noop ("create COUNT number of peers"),
460 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
461 GNUNET_GETOPT_OPTION_END
465 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
468 result = GNUNET_SYSERR;
470 GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS] hosts-file",
471 _("Profiler for testbed"),
472 options, &run, NULL);
473 if (GNUNET_OK != ret)
475 if (GNUNET_OK != result)