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_util_lib.h"
30 #include "gnunet_testbed_service.h"
31 #include "testbed_api_hosts.h"
34 * Generic loggins shorthand
36 #define LOG(kind,...) \
37 GNUNET_log (kind, __VA_ARGS__)
41 * Handle to global configuration
43 struct GNUNET_CONFIGURATION_Handle *cfg;
46 * Peer linking - topology operation
48 struct GNUNET_TESTBED_Operation *topology_op;
51 * Abort task identifier
53 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
56 * Shutdown task identifier
58 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
61 * Global event mask for all testbed events
66 * Number of peers to be started by the profiler
68 static unsigned int num_peers;
71 * Number of timeout failures to tolerate
73 static unsigned int num_cont_fails;
76 * Continuous failures during overlay connect operations
78 static unsigned int cont_fails;
81 * Links which are successfully established
83 static unsigned int established_links;
86 * Links which are not successfully established
88 static unsigned int failed_links;
91 * Global testing status
100 * @param tc the task context
103 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
105 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
106 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
107 GNUNET_SCHEDULER_cancel (abort_task);
109 GNUNET_CONFIGURATION_destroy (cfg);
110 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
115 * abort task to run on test timed out
118 * @param tc the task context
121 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
123 LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
124 abort_task = GNUNET_SCHEDULER_NO_TASK;
125 result = GNUNET_SYSERR;
126 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
127 GNUNET_SCHEDULER_cancel (shutdown_task);
128 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
133 * Function to print summary about how many overlay links we have made and how
137 print_overlay_links_summary ()
139 static int printed_already;
141 if (GNUNET_YES == printed_already)
143 printed_already = GNUNET_YES;
144 printf ("%u links succeeded\n", established_links);
145 printf ("%u links failed due to timeouts\n", failed_links);
150 * Controller event callback
153 * @param event the controller event
156 controller_event_cb (void *cls,
157 const struct GNUNET_TESTBED_EventInformation *event)
161 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
162 /* Control reaches here when a peer linking operation fails */
163 if (NULL != event->details.operation_finished.emsg)
168 if (++cont_fails > num_cont_fails)
170 printf ("\nAborting due to very high failure rate\n");
171 print_overlay_links_summary ();
172 GNUNET_SCHEDULER_cancel (abort_task);
173 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
178 case GNUNET_TESTBED_ET_CONNECT:
182 if (0 == established_links)
183 printf ("Establishing links. Please wait\n");
196 * Signature of a main function for a testcase.
199 * @param num_peers number of peers in 'peers'
200 * @param peers handle to peers run in the testbed
203 test_run (void *cls, unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers)
206 fprintf (stdout, "\n");
207 print_overlay_links_summary ();
208 fprintf (stdout, "Testbed running, waiting for keystroke to shut down\n");
211 fprintf (stdout, "Shutting down. Please wait\n");
213 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
218 * Main function that will be run by the scheduler.
221 * @param args remaining command-line arguments
222 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
223 * @param config configuration
226 run (void *cls, char *const *args, const char *cfgfile,
227 const struct GNUNET_CONFIGURATION_Handle *config)
231 fprintf (stderr, _("No hosts-file specified on command line\n"));
239 cfg = GNUNET_CONFIGURATION_dup (config);
241 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
242 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
243 GNUNET_TESTBED_run (args[0], cfg, num_peers, event_mask, controller_event_cb,
244 NULL, &test_run, NULL);
246 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_abort,
254 * @return 0 on success
257 main (int argc, char *const *argv)
259 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
260 {'p', "num-peers", "COUNT",
261 gettext_noop ("create COUNT number of peers"),
262 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers},
263 {'e', "num-errors", "COUNT",
264 gettext_noop ("tolerate COUNT number of continious timeout failures"),
265 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_cont_fails},
266 GNUNET_GETOPT_OPTION_END
270 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
272 result = GNUNET_SYSERR;
274 GNUNET_PROGRAM_run (argc, argv,
275 "gnunet-testbed-profiler [OPTIONS] hosts-file",
276 _("Profiler for testbed"), options, &run, NULL);
277 GNUNET_free ((void *) argv);
278 if (GNUNET_OK != ret)
280 if (GNUNET_OK != result)