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