f5b9b25667eacc1f7c3df241f6f14510d145f0bf
[oweals/gnunet.git] / src / testbed / gnunet-testbed-profiler.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011, 2012 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  * Abort task identifier
52  */
53 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
54
55 /**
56  * Shutdown task identifier
57  */
58 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
59
60 /**
61  * Global event mask for all testbed events
62  */
63 uint64_t event_mask;
64
65 /**
66  * Number of peers to be started by the profiler
67  */
68 static unsigned int num_peers;
69
70 /**
71  * Number of timeout failures to tolerate
72  */
73 static unsigned int num_cont_fails;
74
75 /**
76  * Continuous failures during overlay connect operations
77  */
78 static unsigned int cont_fails;
79
80 /**
81  * Links which are successfully established
82  */
83 static unsigned int established_links;
84
85 /**
86  * Links which are not successfully established
87  */
88 static unsigned int failed_links;
89
90 /**
91  * Global testing status
92  */
93 static int result;
94
95
96 /**
97  * Shutdown nicely
98  *
99  * @param cls NULL
100  * @param tc the task context
101  */
102 static void
103 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 {
105   shutdown_task = GNUNET_SCHEDULER_NO_TASK;
106   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
107     GNUNET_SCHEDULER_cancel (abort_task);
108   if (NULL != cfg)
109     GNUNET_CONFIGURATION_destroy (cfg);
110   GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
111 }
112
113
114 /**
115  * abort task to run on test timed out
116  *
117  * @param cls NULL
118  * @param tc the task context
119  */
120 static void
121 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
122 {
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);
129 }
130
131
132 /**
133  * Function to print summary about how many overlay links we have made and how
134  * many failed
135  */
136 static void
137 print_overlay_links_summary ()
138 {
139   static int printed_already;
140
141   if (GNUNET_YES == printed_already)
142     return;
143   printed_already = GNUNET_YES;
144   printf ("%u links failed due to timeouts\n", failed_links);
145 }
146
147
148 /**
149  * Controller event callback
150  *
151  * @param cls NULL
152  * @param event the controller event
153  */
154 static void
155 controller_event_cb (void *cls,
156                      const struct GNUNET_TESTBED_EventInformation *event)
157 {
158   switch (event->type)
159   {
160   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
161     /* Control reaches here when a peer linking operation fails */
162     if (NULL != event->details.operation_finished.emsg)
163     {
164       printf ("F");
165       fflush (stdout);
166       failed_links++;
167       if (++cont_fails > num_cont_fails)
168       {
169         printf ("\nAborting due to very high failure rate");
170         print_overlay_links_summary ();   
171         GNUNET_SCHEDULER_cancel (abort_task);
172         abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
173         return;
174       }
175     }
176     break;
177   case GNUNET_TESTBED_ET_CONNECT:
178     {
179       if (0 != cont_fails)
180         cont_fails--;
181       if (0 == established_links)
182         printf ("Establishing links. Please wait\n");
183       printf (".");
184       fflush (stdout);
185       established_links++;
186     }
187     break;
188   default:
189     GNUNET_break (0);
190   }
191 }
192
193
194 /**
195  * Signature of a main function for a testcase.
196  *
197  * @param cls closure
198  * @param num_peers number of peers in 'peers'
199  * @param peers handle to peers run in the testbed
200  */
201 static void test_run (void *cls,
202                       unsigned int num_peers,
203                       struct GNUNET_TESTBED_Peer **peers)
204 {
205   result = GNUNET_OK;
206   fprintf (stdout, "\nTestbed running, waiting for keystroke to shut down\n");
207   fflush (stdout);
208   (void) getc (stdin);
209   fprintf (stdout, "Shutting down. Please wait\n");
210   fflush (stdout);
211   shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
212 }
213
214
215 /**
216  * Main function that will be run by the scheduler.
217  *
218  * @param cls closure
219  * @param args remaining command-line arguments
220  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
221  * @param config configuration
222  */
223 static void
224 run (void *cls, char *const *args, const char *cfgfile,
225      const struct GNUNET_CONFIGURATION_Handle *config)
226 {
227   if (NULL == args[0])
228   {
229     fprintf (stderr, _("No hosts-file specified on command line\n"));
230     return;
231   }
232   if (0 == num_peers)
233   {
234     result = GNUNET_OK;
235     return;
236   }
237   cfg = GNUNET_CONFIGURATION_dup (config);
238   event_mask = 0;
239   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
240   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
241   GNUNET_TESTBED_run (args[0], cfg, 
242                       num_peers, event_mask,
243                       controller_event_cb, NULL,
244                       &test_run, NULL);
245   abort_task =
246       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
247                                     &do_abort,
248                                     NULL);
249 }
250
251
252 /**
253  * Main function.
254  *
255  * @return 0 on success
256  */
257 int
258 main (int argc, char *const *argv)
259 {
260   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
261     { 'p', "num-peers", "COUNT",
262       gettext_noop ("create COUNT number of peers"),
263       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
264     { 'e', "num-errors", "COUNT",
265       gettext_noop ("tolerate COUNT number of continious timeout failures"),
266       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_cont_fails },
267     GNUNET_GETOPT_OPTION_END
268   };
269   int ret;
270
271   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
272     return 2;  
273   result = GNUNET_SYSERR;
274   ret =
275       GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS] hosts-file",
276                           _("Profiler for testbed"),
277                           options, &run, NULL);
278   GNUNET_free ((void*) argv);
279   if (GNUNET_OK != ret)
280     return ret;
281   if (GNUNET_OK != result)
282     return 1;
283   return 0;
284 }