6bfc15728c3a2d4cab5bb09f5d81d69b4ee84cab
[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  * Availanle states during profiling
67  */
68 enum State
69 {
70   /**
71    * Initial state
72    */
73   STATE_INIT = 0,
74
75   /**
76    * Starting slaves
77    */
78   STATE_SLAVES_STARTING
79 };
80
81
82 /**
83  * An array of hosts loaded from the hostkeys file
84  */
85 static struct GNUNET_TESTBED_Host **hosts;
86
87 /**
88  * The array of peers; we fill this as the peers are given to us by the testbed
89  */
90 static struct GNUNET_TESTBED_Peer **peers;
91
92 /* /\** */
93 /*  * Operation handle */
94 /*  *\/ */
95 /* static struct GNUNET_TESTBED_Operation *op; */
96
97 /**
98  * Host registration handle
99  */
100 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
101
102 /**
103  * Handle to the master controller process
104  */
105 struct GNUNET_TESTBED_ControllerProc *mc_proc;
106
107 /**
108  * Handle to the master controller
109  */
110 struct GNUNET_TESTBED_Controller *mc;
111
112 /**
113  * Handle to global configuration
114  */
115 struct GNUNET_CONFIGURATION_Handle *cfg;
116
117 /**
118  * Head of the operations list
119  */
120 struct DLLOperation *dll_op_head;
121
122 /**
123  * Tail of the operations list
124  */
125 struct DLLOperation *dll_op_tail;
126
127 /**
128  * Abort task identifier
129  */
130 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
131
132 /**
133  * Host registration task identifier
134  */
135 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
136
137 /**
138  * Global event mask for all testbed events
139  */
140 uint64_t event_mask;
141
142 /**
143  * Current peer id
144  */
145 unsigned int peer_id;
146
147 /**
148  * Number of peers to be started by the profiler
149  */
150 static unsigned int num_peers;
151
152 /**
153  * Number of hosts in the hosts array
154  */
155 static unsigned int num_hosts;
156
157 /**
158  * Global testing status
159  */
160 static int result;
161
162 /**
163  * current state of profiling
164  */
165 enum State state;
166
167
168 /**
169  * Shutdown nicely
170  *
171  * @param cls NULL
172  * @param tc the task context
173  */
174 static void
175 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
176 {
177   struct DLLOperation *dll_op;
178   unsigned int nhost;
179
180   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
181     GNUNET_SCHEDULER_cancel (abort_task);
182   if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
183     GNUNET_SCHEDULER_cancel (register_hosts_task);
184   GNUNET_free_non_null (peers);
185   if (NULL != reg_handle)
186     GNUNET_TESTBED_cancel_registration (reg_handle);
187   for (nhost = 0; nhost < num_hosts; nhost++)
188     if (NULL != hosts[nhost])
189       GNUNET_TESTBED_host_destroy (hosts[nhost]);
190   GNUNET_free_non_null (hosts);
191   while (NULL != (dll_op = dll_op_head))
192   {
193     GNUNET_TESTBED_operation_cancel (dll_op->op);
194     GNUNET_free_non_null (dll_op->cls);
195     GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
196     GNUNET_free (dll_op);
197   }
198   if (NULL != mc)
199     GNUNET_TESTBED_controller_disconnect (mc);
200   if (NULL != mc_proc)
201     GNUNET_TESTBED_controller_stop (mc_proc);
202   if (NULL != cfg)
203     GNUNET_CONFIGURATION_destroy (cfg);
204   GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */  
205 }
206
207
208 /**
209  * abort task to run on test timed out
210  *
211  * @param cls NULL
212  * @param tc the task context
213  */
214 static void
215 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
216 {
217   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
218   abort_task = GNUNET_SCHEDULER_NO_TASK;
219   result = GNUNET_SYSERR;
220   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
221 }
222
223
224 /**
225  * Controller event callback
226  *
227  * @param cls NULL
228  * @param event the controller event
229  */
230 static void
231 controller_event_cb (void *cls,
232                      const struct GNUNET_TESTBED_EventInformation *event)
233 {
234   struct DLLOperation *dll_op;
235   struct GNUNET_TESTBED_Operation *op;
236
237   switch (state)
238   {
239   case STATE_SLAVES_STARTING:
240     switch (event->type)
241     {
242     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
243       {
244         static unsigned int slaves_started;
245       
246         dll_op = event->details.operation_finished.op_cls;
247         GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
248         GNUNET_free (dll_op);
249         op = event->details.operation_finished.operation;
250         if (NULL != event->details.operation_finished.emsg)
251         {
252           LOG (GNUNET_ERROR_TYPE_WARNING,
253                _("An operation has failed while starting slaves\n"));
254           GNUNET_TESTBED_operation_done (op);
255           GNUNET_SCHEDULER_cancel (abort_task);
256           abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
257           return;
258         }
259         GNUNET_TESTBED_operation_done (op);
260         /* Proceed to start peers */
261         if (++slaves_started == num_hosts - 1)
262           printf ("All slaves started successfully\n"); 
263       }
264       break;
265     default:
266       GNUNET_assert (0);
267     }
268     break;
269   default:
270     GNUNET_assert (0);
271   }
272 }
273
274
275 /**
276  * Task to register all hosts available in the global host list
277  *
278  * @param cls NULL
279  * @param tc the scheduler task context
280  */
281 static void
282 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
283
284
285 /**
286  * Callback which will be called to after a host registration succeeded or failed
287  *
288  * @param cls the closure
289  * @param emsg the error message; NULL if host registration is successful
290  */
291 static void
292 host_registration_completion (void *cls, const char *emsg)
293 {
294   reg_handle = NULL;
295   if (NULL != emsg)
296   {
297     LOG (GNUNET_ERROR_TYPE_WARNING,
298          _("Host registration failed for a host. Error: %s\n"), emsg);
299     GNUNET_SCHEDULER_cancel (abort_task);
300     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
301     return;
302   }
303   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
304 }
305
306
307 /**
308  * Task to register all hosts available in the global host list
309  *
310  * @param cls NULL
311  * @param tc the scheduler task context
312  */
313 static void
314 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
315 {
316   struct DLLOperation *dll_op;
317   static unsigned int reg_host;
318   unsigned int slave;
319
320   register_hosts_task = GNUNET_SCHEDULER_NO_TASK;  
321   if (reg_host == num_hosts - 1)
322   {
323     LOG (GNUNET_ERROR_TYPE_DEBUG,
324          "All hosts successfully registered\n");
325     /* Start slaves */
326     state = STATE_SLAVES_STARTING;
327     for (slave = 1; slave < num_hosts; slave++)
328     {
329       dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
330       dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
331                                                    mc,
332                                                    hosts[slave],
333                                                    hosts[0],
334                                                    cfg,
335                                                    GNUNET_YES);
336       GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
337     }
338     return;
339   }
340   reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
341                                              host_registration_completion,
342                                              NULL);
343 }
344
345
346 /**
347  * Callback to signal successfull startup of the controller process
348  *
349  * @param cls the closure from GNUNET_TESTBED_controller_start()
350  * @param cfg the configuration with which the controller has been started;
351  *          NULL if status is not GNUNET_OK
352  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
353  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
354  */
355 static void
356 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
357 {
358   GNUNET_SCHEDULER_cancel (abort_task);
359   if (GNUNET_OK != status)
360   {
361     mc_proc = NULL;
362     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
363     return;
364   }
365   event_mask = 0;
366   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
367   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
368   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
369   event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
370   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
371   mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
372                                           &controller_event_cb, NULL);
373   if (NULL == mc)
374   {
375     LOG (GNUNET_ERROR_TYPE_WARNING,
376          _("Unable to connect to master controller -- Check config\n"));
377     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
378     return;
379   }
380   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
381   abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
382                                              &do_abort, NULL);
383 }
384
385
386 /**
387  * Main function that will be run by the scheduler.
388  *
389  * @param cls closure
390  * @param args remaining command-line arguments
391  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
392  * @param cfg configuration
393  */
394 static void
395 run (void *cls, char *const *args, const char *cfgfile,
396      const struct GNUNET_CONFIGURATION_Handle *config)
397 {
398   unsigned int nhost;
399
400   if (NULL == args[0])
401   {
402     fprintf (stderr, _("No hosts-file specified on command line\n"));
403     return;
404   }
405   if (0 == num_peers)
406   {
407     result = GNUNET_OK;
408     return;
409   }
410   num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
411   if (0 == num_hosts)
412   {
413     fprintf (stderr, _("No hosts loaded. Need atleast one host\n"));
414     return;
415   }
416   for (nhost = 0; nhost < num_hosts; nhost++)
417   {
418     if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
419     {
420       fprintf (stderr, _("Host %s cannot start testbed\n"),
421                          GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
422       break;
423     }
424   }
425   if (num_hosts != nhost)
426   {
427     fprintf (stderr, _("Exiting\n"));
428     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
429     return;
430   }
431   cfg = GNUNET_CONFIGURATION_dup (config);
432   mc_proc = 
433       GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname_ 
434                                        (hosts[0]),
435                                        hosts[0],
436                                        cfg,
437                                        status_cb,
438                                        NULL);
439   abort_task =
440       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
441                                     (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
442                                     NULL);
443 }
444
445
446 /**
447  * Main function.
448  *
449  * @return 0 on success
450  */
451 int
452 main (int argc, char *const *argv)
453 {
454   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
455     { 'n', "num-peers", "COUNT",
456       gettext_noop ("create COUNT number of peers"),
457       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
458     { 'n', "num-peers", "COUNT",
459       gettext_noop ("create COUNT number of peers"),
460       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
461     GNUNET_GETOPT_OPTION_END
462   };
463   int ret;
464
465   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
466     return 2;
467   
468   result = GNUNET_SYSERR;
469   ret =
470       GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS] hosts-file",
471                           _("Profiler for testbed"),
472                           options, &run, NULL);
473   if (GNUNET_OK != ret)
474     return ret;
475   if (GNUNET_OK != result)
476     return 1;
477   return 0;
478 }