0191580c43b7ad96368ec65e31ea941e36469f11
[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_testbed_service.h"
30 #include "testbed_api_hosts.h"
31
32 /**
33  * Generic loggins shorthand
34  */
35 #define LOG(kind,...)                                           \
36   GNUNET_log_from (kind, "testbed-api-testbed", __VA_ARGS__)
37
38 /**
39  * DLL of operations
40  */
41 struct DLLOperation
42 {
43   /**
44    * The testbed operation handle
45    */
46   struct GNUNET_TESTBED_Operation *op;
47
48   /**
49    * Closure
50    */
51   void *cls;
52
53   /**
54    * The next pointer for DLL
55    */
56   struct DLLOperation *next;
57
58   /**
59    * The prev pointer for DLL
60    */
61   struct DLLOperation *prev;
62 };
63
64
65 /**
66  * An array of hosts loaded from the hostkeys file
67  */
68 static struct GNUNET_TESTBED_Host **hosts;
69
70 /**
71  * The array of peers; we fill this as the peers are given to us by the testbed
72  */
73 static struct GNUNET_TESTBED_Peer **peers;
74
75 /**
76  * Operation handle
77  */
78 static struct GNUNET_TESTBED_Operation *op;
79
80 /**
81  * Host registration handle
82  */
83 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
84
85 /**
86  * Handle to the master controller process
87  */
88 struct GNUNET_TESTBED_ControllerProc *mc_proc;
89
90 /**
91  * Handle to the master controller
92  */
93 struct GNUNET_TESTBED_Controller *mc;
94
95 /**
96  * Handle to global configuration
97  */
98 struct GNUNET_CONFIGURATION_Handle *cfg;
99
100 /**
101  * Abort task identifier
102  */
103 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
104
105 /**
106  * Host registration task identifier
107  */
108 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
109
110 /**
111  * Global event mask for all testbed events
112  */
113 uint64_t event_mask;
114
115 /**
116  * Current peer id
117  */
118 unsigned int peer_id;
119
120 /**
121  * Number of peers to be started by the profiler
122  */
123 static unsigned int num_peers;
124
125 /**
126  * Number of hosts in the hosts array
127  */
128 static unsigned int num_hosts;
129
130 /**
131  * Global testing status
132  */
133 static int result;
134
135
136 /**
137  * Shutdown nicely
138  *
139  * @param cls NULL
140  * @param tc the task context
141  */
142 static void
143 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
144 {
145   unsigned int nhost;
146
147   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
148     GNUNET_SCHEDULER_cancel (abort_task);
149   if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
150     GNUNET_SCHEDULER_cancel (register_hosts_task);
151   GNUNET_free_non_null (peers);
152   if (NULL != reg_handle)
153     GNUNET_TESTBED_cancel_registration (reg_handle);
154   for (nhost = 0; nhost < num_hosts; nhost++)
155     if (NULL != hosts[nhost])
156       GNUNET_TESTBED_host_destroy (hosts[nhost]);
157   GNUNET_free_non_null (hosts);
158   if (NULL != mc_proc)
159     GNUNET_TESTBED_controller_stop (mc_proc);
160   if (NULL != cfg)
161     GNUNET_CONFIGURATION_destroy (cfg);
162   GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */  
163 }
164
165
166 /**
167  * abort task to run on test timed out
168  *
169  * @param cls NULL
170  * @param tc the task context
171  */
172 static void
173 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
174 {
175   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
176   abort_task = GNUNET_SCHEDULER_NO_TASK;
177   result = GNUNET_SYSERR;
178   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
179 }
180
181
182 /**
183  * Controller event callback
184  *
185  * @param cls NULL
186  * @param event the controller event
187  */
188 static void
189 controller_event_cb (void *cls,
190                      const struct GNUNET_TESTBED_EventInformation *event)
191 {
192
193   switch (event->type)
194   {
195   case GNUNET_TESTBED_ET_PEER_START:
196     GNUNET_assert (NULL == peers[peer_id]);
197     GNUNET_assert (NULL != event->details.peer_start.peer);
198     peers[peer_id++] = event->details.peer_start.peer;
199     break;
200   case GNUNET_TESTBED_ET_PEER_STOP:
201     GNUNET_assert (NULL != op);
202     GNUNET_TESTBED_operation_done (op);
203     GNUNET_assert (peers[0] == event->details.peer_stop.peer);
204     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
205     break;
206   default:
207     GNUNET_assert (0);
208   }
209 }
210
211
212 /**
213  * Task to register all hosts available in the global host list
214  *
215  * @param cls NULL
216  * @param tc the scheduler task context
217  */
218 static void
219 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
220
221
222 /**
223  * Callback which will be called to after a host registration succeeded or failed
224  *
225  * @param cls the closure
226  * @param emsg the error message; NULL if host registration is successful
227  */
228 static void
229 host_registration_completion (void *cls, const char *emsg)
230 {
231   reg_handle = NULL;
232   if (NULL != emsg)
233   {
234     LOG (GNUNET_ERROR_TYPE_WARNING,
235          _("Host registration failed for a host. Error: %s\n"), emsg);
236     GNUNET_SCHEDULER_cancel (abort_task);
237     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
238     return;
239   }
240   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
241 }
242
243
244 /**
245  * Task to register all hosts available in the global host list
246  *
247  * @param cls NULL
248  * @param tc the scheduler task context
249  */
250 static void
251 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
252 {
253   static unsigned int reg_host;
254
255   register_hosts_task = GNUNET_SCHEDULER_NO_TASK;  
256   if (reg_host == num_hosts - 1)
257   {
258     LOG (GNUNET_ERROR_TYPE_DEBUG,
259          "All hosts successfully registered\n");
260     /* Start peer create task */
261     return;
262   }
263   reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
264                                              host_registration_completion,
265                                              NULL);
266 }
267
268
269 /**
270  * Callback to signal successfull startup of the controller process
271  *
272  * @param cls the closure from GNUNET_TESTBED_controller_start()
273  * @param cfg the configuration with which the controller has been started;
274  *          NULL if status is not GNUNET_OK
275  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
276  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
277  */
278 static void
279 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
280 {
281   GNUNET_SCHEDULER_cancel (abort_task);
282   if (GNUNET_OK != status)
283   {
284     mc_proc = NULL;
285     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
286     return;
287   }
288   event_mask = 0;
289   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
290   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
291   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
292   event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
293   mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
294                                           &controller_event_cb, NULL);
295   if (NULL == mc)
296   {
297     LOG (GNUNET_ERROR_TYPE_WARNING,
298          _("Unable to connect to master controller -- Check config\n"));
299     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
300     return;
301   }
302   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
303   abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
304                                              &do_abort, NULL);
305 }
306
307
308 /**
309  * Main function that will be run by the scheduler.
310  *
311  * @param cls closure
312  * @param args remaining command-line arguments
313  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
314  * @param cfg configuration
315  */
316 static void
317 run (void *cls, char *const *args, const char *cfgfile,
318      const struct GNUNET_CONFIGURATION_Handle *config)
319 {
320   unsigned int nhost;
321
322   if (NULL == args[0])
323   {
324     fprintf (stderr, _("No hosts-file specified on command line\n"));
325     return;
326   }
327   if (0 == num_peers)
328   {
329     result = GNUNET_OK;
330     return;
331   }
332   num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
333   if (0 == num_hosts)
334   {
335     fprintf (stderr, _("No hosts loaded. Need atleast one host\n"));
336     return;
337   }
338   for (nhost = 0; nhost < num_hosts; nhost++)
339   {
340     if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
341     {
342       fprintf (stderr, _("Host %s cannot start testbed\n"),
343                          GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
344       break;
345     }
346   }
347   if (num_hosts != nhost)
348   {
349     fprintf (stderr, _("Exiting\n"));
350     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
351     return;
352   }
353   cfg = GNUNET_CONFIGURATION_dup (config);
354   mc_proc = 
355       GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname_ 
356                                        (hosts[0]),
357                                        hosts[0],
358                                        cfg,
359                                        status_cb,
360                                        NULL);
361   abort_task =
362       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
363                                     (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
364                                     NULL);
365 }
366
367
368 /**
369  * Main function.
370  *
371  * @return 0 on success
372  */
373 int
374 main (int argc, char *const *argv)
375 {
376   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
377     { 'n', "num-peers", "COUNT",
378       gettext_noop ("create COUNT number of peers"),
379       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
380     { 'n', "num-peers", "COUNT",
381       gettext_noop ("create COUNT number of peers"),
382       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
383     GNUNET_GETOPT_OPTION_END
384   };
385   int ret;
386
387   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
388     return 2;
389   
390   result = GNUNET_SYSERR;
391   ret =
392       GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS] hosts-file",
393                           _("Profiler for testbed"),
394                           options, &run, NULL);
395   if (GNUNET_OK != ret)
396     return ret;
397   if (GNUNET_OK != result)
398     return 1;
399   return 0;
400 }