output status for connects
[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 (0 == established_links)
455           printf ("Establishing links\n");
456         else
457           printf (".");
458         if (++established_links == num_links)
459         {
460           prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
461           printf ("%u links established in %.2f seconds\n",
462                   num_links, ((double) prof_time.rel_value) / 1000.00);
463           result = GNUNET_OK;
464           GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
465         }
466       }
467       break;
468     default:
469       GNUNET_assert (0);
470     }
471     break;
472   default:
473     GNUNET_assert (0);
474   }
475 }
476
477
478 /**
479  * Task to register all hosts available in the global host list
480  *
481  * @param cls NULL
482  * @param tc the scheduler task context
483  */
484 static void
485 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
486
487
488 /**
489  * Callback which will be called to after a host registration succeeded or failed
490  *
491  * @param cls the closure
492  * @param emsg the error message; NULL if host registration is successful
493  */
494 static void
495 host_registration_completion (void *cls, const char *emsg)
496 {
497   reg_handle = NULL;
498   if (NULL != emsg)
499   {
500     LOG (GNUNET_ERROR_TYPE_WARNING,
501          _("Host registration failed for a host. Error: %s\n"), emsg);
502     GNUNET_SCHEDULER_cancel (abort_task);
503     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
504     return;
505   }
506   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
507 }
508
509
510 /**
511  * Task to register all hosts available in the global host list
512  *
513  * @param cls NULL
514  * @param tc the scheduler task context
515  */
516 static void
517 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
518 {
519   struct DLLOperation *dll_op;
520   static unsigned int reg_host;
521   unsigned int slave;
522
523   register_hosts_task = GNUNET_SCHEDULER_NO_TASK;  
524   if (reg_host == num_hosts - 1)
525   {
526     LOG (GNUNET_ERROR_TYPE_DEBUG,
527          "All hosts successfully registered\n");
528     /* Start slaves */
529     state = STATE_SLAVES_STARTING;
530     for (slave = 1; slave < num_hosts; slave++)
531     {
532       dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
533       dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
534                                                    mc,
535                                                    hosts[slave],
536                                                    hosts[0],
537                                                    cfg,
538                                                    GNUNET_YES);
539       GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
540     }
541     return;
542   }
543   reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
544                                              host_registration_completion,
545                                              NULL);
546 }
547
548
549 /**
550  * Callback to signal successfull startup of the controller process
551  *
552  * @param cls the closure from GNUNET_TESTBED_controller_start()
553  * @param cfg the configuration with which the controller has been started;
554  *          NULL if status is not GNUNET_OK
555  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
556  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
557  */
558 static void
559 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
560 {
561   GNUNET_SCHEDULER_cancel (abort_task);
562   if (GNUNET_OK != status)
563   {
564     mc_proc = NULL;
565     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
566     return;
567   }
568   event_mask = 0;
569   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
570   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
571   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
572   event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
573   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
574   mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
575                                           &controller_event_cb, NULL);
576   if (NULL == mc)
577   {
578     LOG (GNUNET_ERROR_TYPE_WARNING,
579          _("Unable to connect to master controller -- Check config\n"));
580     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
581     return;
582   }
583   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
584   abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
585                                              &do_abort, NULL);
586 }
587
588
589 /**
590  * Main function that will be run by the scheduler.
591  *
592  * @param cls closure
593  * @param args remaining command-line arguments
594  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
595  * @param config configuration
596  */
597 static void
598 run (void *cls, char *const *args, const char *cfgfile,
599      const struct GNUNET_CONFIGURATION_Handle *config)
600 {
601   unsigned int nhost;
602
603   if (NULL == args[0])
604   {
605     fprintf (stderr, _("No hosts-file specified on command line\n"));
606     return;
607   }
608   if (0 == num_peers)
609   {
610     result = GNUNET_OK;
611     return;
612   }
613   num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
614   if (0 == num_hosts)
615   {
616     fprintf (stderr, _("No hosts loaded. Need atleast one host\n"));
617     return;
618   }
619   for (nhost = 0; nhost < num_hosts; nhost++)
620   {
621     if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
622     {
623       fprintf (stderr, _("Host %s cannot start testbed\n"),
624                          GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
625       break;
626     }
627   }
628   if (num_hosts != nhost)
629   {
630     fprintf (stderr, _("Exiting\n"));
631     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
632     return;
633   }
634   cfg = GNUNET_CONFIGURATION_dup (config);
635   mc_proc = 
636       GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname_ 
637                                        (hosts[0]),
638                                        hosts[0],
639                                        cfg,
640                                        status_cb,
641                                        NULL);
642   abort_task =
643       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
644                                     (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
645                                     NULL);
646 }
647
648
649 /**
650  * Main function.
651  *
652  * @return 0 on success
653  */
654 int
655 main (int argc, char *const *argv)
656 {
657   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
658     { 'p', "num-peers", "COUNT",
659       gettext_noop ("create COUNT number of peers"),
660       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
661     { 'n', "num-links", "COUNT",
662       gettext_noop ("create COUNT number of random links"),
663       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links },
664     GNUNET_GETOPT_OPTION_END
665   };
666   int ret;
667
668   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
669     return 2;
670   
671   result = GNUNET_SYSERR;
672   ret =
673       GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS] hosts-file",
674                           _("Profiler for testbed"),
675                           options, &run, NULL);
676   if (GNUNET_OK != ret)
677     return ret;
678   if (GNUNET_OK != result)
679     return 1;
680   return 0;
681 }