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