- give out run handle through which master controller's handle can be retrieved
[oweals/gnunet.git] / src / testbed / gnunet-testbed-profiler.c
1 /*
2      This file is part of GNUnet.
3      (C) 2008--2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file testbed/gnunet-testbed-profiler.c
23  * @brief Profiling driver for the testbed.
24  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31 #include "testbed_api_hosts.h"
32
33 /**
34  * Generic loggins shorthand
35  */
36 #define LOG(kind,...)                                           \
37   GNUNET_log (kind, __VA_ARGS__)
38
39
40 /**
41  * Handle to global configuration
42  */
43 struct GNUNET_CONFIGURATION_Handle *cfg;
44
45 /**
46  * Peer linking - topology operation
47  */
48 struct GNUNET_TESTBED_Operation *topology_op;
49
50 /**
51  * Name of the file with the hosts to run the test over (configuration option).
52  * It will be NULL if ENABLE_LL is set
53  */ 
54 static char *hosts_file;
55
56 /**
57  * Abort task identifier
58  */
59 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
60
61 /**
62  * Shutdown task identifier
63  */
64 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
65
66 /**
67  * Global event mask for all testbed events
68  */
69 uint64_t event_mask;
70
71 /**
72  * Number of peers to be started by the profiler
73  */
74 static unsigned int num_peers;
75
76 /**
77  * Number of timeout failures to tolerate
78  */
79 static unsigned int num_cont_fails;
80
81 /**
82  * Continuous failures during overlay connect operations
83  */
84 static unsigned int cont_fails;
85
86 /**
87  * Links which are successfully established
88  */
89 static unsigned int established_links;
90
91 /**
92  * Links which are not successfully established
93  */
94 static unsigned int failed_links;
95
96 /**
97  * Global testing status
98  */
99 static int result;
100
101
102 /**
103  * Shutdown nicely
104  *
105  * @param cls NULL
106  * @param tc the task context
107  */
108 static void
109 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
110 {
111   shutdown_task = GNUNET_SCHEDULER_NO_TASK;
112   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
113   {
114     GNUNET_SCHEDULER_cancel (abort_task);
115     abort_task = GNUNET_SCHEDULER_NO_TASK;
116   }
117   if (NULL != cfg)
118   {
119     GNUNET_CONFIGURATION_destroy (cfg);
120     cfg = NULL;
121   }
122   GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
123 }
124
125
126 /**
127  * abort task to run on test timed out
128  *
129  * @param cls NULL
130  * @param tc the task context
131  */
132 static void
133 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
134 {
135   LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
136   abort_task = GNUNET_SCHEDULER_NO_TASK;
137   result = GNUNET_SYSERR;
138   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
139     GNUNET_SCHEDULER_cancel (shutdown_task);
140   shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
141 }
142
143
144 /**
145  * Function to print summary about how many overlay links we have made and how
146  * many failed
147  */
148 static void
149 print_overlay_links_summary ()
150 {
151   static int printed_already;
152
153   if (GNUNET_YES == printed_already)
154     return;
155   printed_already = GNUNET_YES;
156   printf ("%u links succeeded\n", established_links);
157   printf ("%u links failed due to timeouts\n", failed_links);
158 }
159
160
161 /**
162  * Controller event callback
163  *
164  * @param cls NULL
165  * @param event the controller event
166  */
167 static void
168 controller_event_cb (void *cls,
169                      const struct GNUNET_TESTBED_EventInformation *event)
170 {
171   switch (event->type)
172   {
173   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
174     /* Control reaches here when a peer linking operation fails */
175     if (NULL != event->details.operation_finished.emsg)
176     {
177       printf ("F");
178       fflush (stdout);
179       failed_links++;
180       if (++cont_fails > num_cont_fails)
181       {
182         printf ("\nAborting due to very high failure rate\n");
183         print_overlay_links_summary ();
184         if (GNUNET_SCHEDULER_NO_TASK != abort_task)
185           GNUNET_SCHEDULER_cancel (abort_task);
186         abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
187         return;
188       }
189     }
190     break;
191   case GNUNET_TESTBED_ET_CONNECT:
192   {
193     if (0 != cont_fails)
194       cont_fails--;
195     if (0 == established_links)
196       printf ("Establishing links. Please wait\n");
197     printf (".");
198     fflush (stdout);
199     established_links++;
200   }
201     break;
202   default:
203     GNUNET_break (0);
204   }
205 }
206
207
208 /**
209  * Signature of a main function for a testcase.
210  *
211  * @param cls closure
212  * @param h the run handle
213  * @param num_peers number of peers in 'peers'
214  * @param peers handle to peers run in the testbed
215  * @param links_succeeded the number of overlay link connection attempts that
216  *          succeeded
217  * @param links_failed the number of overlay link
218  */
219 static void
220 test_run (void *cls, 
221           struct GNUNET_TESTBED_RunHandle *h,
222           unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers,
223           unsigned int links_succeeded,
224           unsigned int links_failed)
225 {
226   result = GNUNET_OK;
227   fprintf (stdout, "\n");
228   print_overlay_links_summary ();
229 #if !ENABLE_LL
230   fprintf (stdout, "Testbed running, waiting for keystroke to shut down\n");
231   fflush (stdout);
232   (void) getc (stdin);
233 #endif
234   fprintf (stdout, "Shutting down. Please wait\n");
235   fflush (stdout);
236   shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
237 }
238
239
240 /**
241  * Main function that will be run by the scheduler.
242  *
243  * @param cls closure
244  * @param args remaining command-line arguments
245  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
246  * @param config configuration
247  */
248 static void
249 run (void *cls, char *const *args, const char *cfgfile,
250      const struct GNUNET_CONFIGURATION_Handle *config)
251 {
252   if (0 == num_peers)
253   {
254     LOG (GNUNET_ERROR_TYPE_ERROR, _("Exiting as the number of peers is %u\n"),
255          num_peers);
256     return;
257   }
258   cfg = GNUNET_CONFIGURATION_dup (config);
259   event_mask = 0;
260   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
261   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
262   GNUNET_TESTBED_run (hosts_file, cfg, num_peers, event_mask, controller_event_cb,
263                       NULL, &test_run, NULL);
264   abort_task =
265       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_abort,
266                                     NULL);
267 }
268
269
270 /**
271  * Main function.
272  *
273  * @return 0 on success
274  */
275 int
276 main (int argc, char *const *argv)
277 {
278   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
279     {'p', "num-peers", "COUNT",
280      gettext_noop ("create COUNT number of peers"),
281      GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers},
282     {'e', "num-errors", "COUNT",
283      gettext_noop ("tolerate COUNT number of continious timeout failures"),
284      GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_cont_fails},
285 #if !ENABLE_LL
286     {'H', "hosts", "FILENAME",
287      gettext_noop ("name of the file with the login information for the testbed"),
288      GNUNET_YES, &GNUNET_GETOPT_set_string, &hosts_file},
289 #endif
290     GNUNET_GETOPT_OPTION_END
291   };
292   const char *binaryHelp = "gnunet-testbed-profiler [OPTIONS]";
293   int ret;
294
295   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
296     return 2;
297   result = GNUNET_SYSERR;
298   ret =
299       GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler", binaryHelp,
300                           options, &run, NULL);
301   GNUNET_free ((void *) argv);
302   if (GNUNET_OK != ret)
303     return ret;
304   if (GNUNET_OK != result)
305     return 1;
306   return 0;
307 }