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