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