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 * An array of hosts loaded from the hostkeys file
68 static struct GNUNET_TESTBED_Host **hosts;
71 * The array of peers; we fill this as the peers are given to us by the testbed
73 static struct GNUNET_TESTBED_Peer **peers;
78 static struct GNUNET_TESTBED_Operation *op;
81 * Host registration handle
83 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
86 * Handle to the master controller process
88 struct GNUNET_TESTBED_ControllerProc *mc_proc;
91 * Handle to the master controller
93 struct GNUNET_TESTBED_Controller *mc;
96 * Handle to global configuration
98 struct GNUNET_CONFIGURATION_Handle *cfg;
101 * Abort task identifier
103 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
106 * Host registration task identifier
108 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
111 * Global event mask for all testbed events
118 unsigned int peer_id;
121 * Number of peers to be started by the profiler
123 static unsigned int num_peers;
126 * Number of hosts in the hosts array
128 static unsigned int num_hosts;
131 * Global testing status
140 * @param tc the task context
143 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
147 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
148 GNUNET_SCHEDULER_cancel (abort_task);
149 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
150 GNUNET_SCHEDULER_cancel (register_hosts_task);
151 GNUNET_free_non_null (peers);
152 if (NULL != reg_handle)
153 GNUNET_TESTBED_cancel_registration (reg_handle);
154 for (nhost = 0; nhost < num_hosts; nhost++)
155 if (NULL != hosts[nhost])
156 GNUNET_TESTBED_host_destroy (hosts[nhost]);
157 GNUNET_free_non_null (hosts);
159 GNUNET_TESTBED_controller_stop (mc_proc);
161 GNUNET_CONFIGURATION_destroy (cfg);
162 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
167 * abort task to run on test timed out
170 * @param tc the task context
173 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
175 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
176 abort_task = GNUNET_SCHEDULER_NO_TASK;
177 result = GNUNET_SYSERR;
178 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
183 * Controller event callback
186 * @param event the controller event
189 controller_event_cb (void *cls,
190 const struct GNUNET_TESTBED_EventInformation *event)
195 case GNUNET_TESTBED_ET_PEER_START:
196 GNUNET_assert (NULL == peers[peer_id]);
197 GNUNET_assert (NULL != event->details.peer_start.peer);
198 peers[peer_id++] = event->details.peer_start.peer;
200 case GNUNET_TESTBED_ET_PEER_STOP:
201 GNUNET_assert (NULL != op);
202 GNUNET_TESTBED_operation_done (op);
203 GNUNET_assert (peers[0] == event->details.peer_stop.peer);
204 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
213 * Task to register all hosts available in the global host list
216 * @param tc the scheduler task context
219 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
223 * Callback which will be called to after a host registration succeeded or failed
225 * @param cls the closure
226 * @param emsg the error message; NULL if host registration is successful
229 host_registration_completion (void *cls, const char *emsg)
234 LOG (GNUNET_ERROR_TYPE_WARNING,
235 _("Host registration failed for a host. Error: %s\n"), emsg);
236 GNUNET_SCHEDULER_cancel (abort_task);
237 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
240 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
245 * Task to register all hosts available in the global host list
248 * @param tc the scheduler task context
251 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
253 static unsigned int reg_host;
255 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
256 if (reg_host == num_hosts)
258 LOG (GNUNET_ERROR_TYPE_DEBUG,
259 "All hosts successfully registered\n");
260 /* Start peer create task */
262 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[reg_host++],
263 host_registration_completion,
269 * Callback to signal successfull startup of the controller process
271 * @param cls the closure from GNUNET_TESTBED_controller_start()
272 * @param cfg the configuration with which the controller has been started;
273 * NULL if status is not GNUNET_OK
274 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
275 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
278 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
280 GNUNET_SCHEDULER_cancel (abort_task);
281 if (GNUNET_OK != status)
284 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
288 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
289 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
290 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
291 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
292 mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
293 &controller_event_cb, NULL);
296 LOG (GNUNET_ERROR_TYPE_WARNING,
297 _("Unable to connect to master controller -- Check config\n"));
298 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
301 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
302 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
308 * Main function that will be run by the scheduler.
311 * @param args remaining command-line arguments
312 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
313 * @param cfg configuration
316 run (void *cls, char *const *args, const char *cfgfile,
317 const struct GNUNET_CONFIGURATION_Handle *config)
323 fprintf (stderr, _("No hosts-file specified on command line\n"));
331 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
334 fprintf (stderr, _("No hosts loaded. Need atleast one host\n"));
337 for (nhost = 0; nhost < num_hosts; nhost++)
339 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
341 fprintf (stderr, _("Host %s cannot start testbed\n"),
342 GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
346 if (num_hosts != nhost)
348 fprintf (stderr, _("Exiting\n"));
349 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
352 cfg = GNUNET_CONFIGURATION_dup (config);
354 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname_
361 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
362 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
370 * @return 0 on success
373 main (int argc, char *const *argv)
375 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
376 { 'n', "num-peers", "COUNT",
377 gettext_noop ("create COUNT number of peers"),
378 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
379 { 'n', "num-peers", "COUNT",
380 gettext_noop ("create COUNT number of peers"),
381 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
382 GNUNET_GETOPT_OPTION_END
386 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
389 result = GNUNET_SYSERR;
391 GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS] hosts-file",
392 _("Profiler for testbed"),
393 options, &run, NULL);
394 if (GNUNET_OK != ret)
396 if (GNUNET_OK != result)