-remove trailing whitespace
[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 /**
102  * Shutdown nicely
103  *
104  * @param cls NULL
105  * @param tc the task context
106  */
107 static void
108 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
109 {
110   shutdown_task = GNUNET_SCHEDULER_NO_TASK;
111   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
112   {
113     GNUNET_SCHEDULER_cancel (abort_task);
114     abort_task = GNUNET_SCHEDULER_NO_TASK;
115   }
116   if (NULL != cfg)
117   {
118     GNUNET_CONFIGURATION_destroy (cfg);
119     cfg = NULL;
120   }
121   GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
122 }
123
124
125 /**
126  * abort task to run on test timed out
127  *
128  * @param cls NULL
129  * @param tc the task context
130  */
131 static void
132 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
133 {
134   LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
135   abort_task = GNUNET_SCHEDULER_NO_TASK;
136   result = GNUNET_SYSERR;
137   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
138     GNUNET_SCHEDULER_cancel (shutdown_task);
139   shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
140 }
141
142
143 /**
144  * Function to print summary about how many overlay links we have made and how
145  * many failed
146  */
147 static void
148 print_overlay_links_summary ()
149 {
150   static int printed_already;
151
152   if (GNUNET_YES == printed_already)
153     return;
154   printed_already = GNUNET_YES;
155   printf ("%u links succeeded\n", established_links);
156   printf ("%u links failed due to timeouts\n", failed_links);
157 }
158
159
160 /**
161  * Controller event callback
162  *
163  * @param cls NULL
164  * @param event the controller event
165  */
166 static void
167 controller_event_cb (void *cls,
168                      const struct GNUNET_TESTBED_EventInformation *event)
169 {
170   switch (event->type)
171   {
172   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
173     /* Control reaches here when a peer linking operation fails */
174     if (NULL != event->details.operation_finished.emsg)
175     {
176       printf ("F");
177       fflush (stdout);
178       failed_links++;
179       if (++cont_fails > num_cont_fails)
180       {
181         printf ("\nAborting due to very high failure rate\n");
182         print_overlay_links_summary ();
183         if (GNUNET_SCHEDULER_NO_TASK != abort_task)
184           GNUNET_SCHEDULER_cancel (abort_task);
185         abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
186         return;
187       }
188     }
189     break;
190   case GNUNET_TESTBED_ET_CONNECT:
191   {
192     if (0 != cont_fails)
193       cont_fails--;
194     if (0 == established_links)
195       printf ("Establishing links. Please wait\n");
196     printf (".");
197     fflush (stdout);
198     established_links++;
199   }
200     break;
201   default:
202     GNUNET_break (0);
203   }
204 }
205
206
207 /**
208  * Signature of a main function for a testcase.
209  *
210  * @param cls closure
211  * @param h the run handle
212  * @param num_peers number of peers in 'peers'
213  * @param peers handle to peers run in the testbed
214  * @param links_succeeded the number of overlay link connection attempts that
215  *          succeeded
216  * @param links_failed the number of overlay link
217  */
218 static void
219 test_run (void *cls,
220           struct GNUNET_TESTBED_RunHandle *h,
221           unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers,
222           unsigned int links_succeeded,
223           unsigned int links_failed)
224 {
225   result = GNUNET_OK;
226   fprintf (stdout, "\n");
227   print_overlay_links_summary ();
228 #if !ENABLE_LL
229   fprintf (stdout, "Testbed running, waiting for keystroke to shut down\n");
230   fflush (stdout);
231   (void) getc (stdin);
232 #endif
233   fprintf (stdout, "Shutting down. Please wait\n");
234   fflush (stdout);
235   shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
236 }
237
238
239 /**
240  * Main function that will be run by the scheduler.
241  *
242  * @param cls closure
243  * @param args remaining command-line arguments
244  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
245  * @param config configuration
246  */
247 static void
248 run (void *cls, char *const *args, const char *cfgfile,
249      const struct GNUNET_CONFIGURATION_Handle *config)
250 {
251   if (0 == num_peers)
252   {
253     LOG (GNUNET_ERROR_TYPE_ERROR, _("Exiting as the number of peers is %u\n"),
254          num_peers);
255     return;
256   }
257   cfg = GNUNET_CONFIGURATION_dup (config);
258   event_mask = 0;
259   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
260   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
261   GNUNET_TESTBED_run (hosts_file, cfg, num_peers, event_mask, controller_event_cb,
262                       NULL, &test_run, NULL);
263   abort_task =
264       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_abort,
265                                     NULL);
266 }
267
268
269 /**
270  * Main function.
271  *
272  * @return 0 on success
273  */
274 int
275 main (int argc, char *const *argv)
276 {
277   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
278     {'p', "num-peers", "COUNT",
279      gettext_noop ("create COUNT number of peers"),
280      GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers},
281     {'e', "num-errors", "COUNT",
282      gettext_noop ("tolerate COUNT number of continious timeout failures"),
283      GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_cont_fails},
284 #if !ENABLE_LL
285     {'H', "hosts", "FILENAME",
286      gettext_noop ("name of the file with the login information for the testbed"),
287      GNUNET_YES, &GNUNET_GETOPT_set_string, &hosts_file},
288 #endif
289     GNUNET_GETOPT_OPTION_END
290   };
291   const char *binaryHelp = "gnunet-testbed-profiler [OPTIONS]";
292   int ret;
293
294   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
295     return 2;
296   result = GNUNET_SYSERR;
297   ret =
298       GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler", binaryHelp,
299                           options, &run, NULL);
300   GNUNET_free ((void *) argv);
301   if (GNUNET_OK != ret)
302     return ret;
303   if (GNUNET_OK != result)
304     return 1;
305   return 0;
306 }