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