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