ead28f360507395761c47b14513e1bb5cc514be7
[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
31
32 /**
33  * The array of peers; we fill this as the peers are given to us by the testbed
34  */
35 static struct GNUNET_TESTBED_Peer **peers;
36
37 /**
38  * Operation handle
39  */
40 static struct GNUNET_TESTBED_Operation *op;
41
42 /**
43  * Abort task identifier
44  */
45 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
46
47 /**
48  * Current peer id
49  */
50 unsigned int peer_id;
51
52 /**
53  * Number of peers to be started by the profiler
54  */
55 static unsigned int num_peers;
56
57 /**
58  * Global testing status
59  */
60 static int result;
61
62
63 /**
64  * Shutdown nicely
65  *
66  * @param cls NULL
67  * @param tc the task context
68  */
69 static void
70 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
71 {
72   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
73     GNUNET_SCHEDULER_cancel (abort_task);
74   GNUNET_free_non_null (peers);
75   GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
76 }
77
78
79 /**
80  * abort task to run on test timed out
81  *
82  * @param cls NULL
83  * @param tc the task context
84  */
85 static void
86 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
87 {
88   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
89   abort_task = GNUNET_SCHEDULER_NO_TASK;
90   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
91 }
92
93
94 /**
95  * Task to be executed when peers are ready
96  *
97  * @param cls NULL
98  * @param tc the task context
99  */
100 static void
101 master_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102 {
103   result = GNUNET_OK;
104   GNUNET_assert (NULL != peers[0]);
105   op = GNUNET_TESTBED_peer_stop (peers[0], NULL, NULL);
106   GNUNET_assert (NULL != op);
107 }
108
109
110 /**
111  * Controller event callback
112  *
113  * @param cls NULL
114  * @param event the controller event
115  */
116 static void
117 controller_event_cb (void *cls,
118                      const struct GNUNET_TESTBED_EventInformation *event)
119 {
120
121   switch (event->type)
122   {
123   case GNUNET_TESTBED_ET_PEER_START:
124     GNUNET_assert (NULL == peers[peer_id]);
125     GNUNET_assert (NULL != event->details.peer_start.peer);
126     peers[peer_id++] = event->details.peer_start.peer;
127     break;
128   case GNUNET_TESTBED_ET_PEER_STOP:
129     GNUNET_assert (NULL != op);
130     GNUNET_TESTBED_operation_done (op);
131     GNUNET_assert (peers[0] == event->details.peer_stop.peer);
132     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
133     break;
134   default:
135     GNUNET_assert (0);
136   }
137 }
138
139
140 /**
141  * Main function that will be run by the scheduler.
142  *
143  * @param cls closure
144  * @param args remaining command-line arguments
145  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
146  * @param cfg configuration
147  */
148 static void
149 run (void *cls, char *const *args, const char *cfgfile,
150      const struct GNUNET_CONFIGURATION_Handle *config)
151 {
152   uint64_t event_mask;
153
154   if (0 == num_peers)
155   {
156     result = GNUNET_OK;
157     return;
158   }
159   peers = GNUNET_malloc (num_peers * sizeof (struct GNUNET_TESTBED_Peer *));
160   event_mask = 0;
161   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
162   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
163   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
164   event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
165   GNUNET_TESTBED_run (NULL, config, num_peers, event_mask, &controller_event_cb,
166                       NULL, &master_task, NULL);
167   abort_task =
168       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
169                                     (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
170                                     NULL);
171 }
172
173
174 /**
175  * Main function.
176  *
177  * @return 0 on success
178  */
179 int
180 main (int argc, char *const *argv)
181 {
182   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
183     { 'n', "num-peers", "COUNT",
184       gettext_noop ("create COUNT number of peers"),
185       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
186     GNUNET_GETOPT_OPTION_END
187   };
188   int ret;
189
190   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
191     return 2;
192   
193   result = GNUNET_SYSERR;
194   ret =
195       GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS]",
196                           _("Profiler for testbed"),
197                           options, &run, NULL);
198   if (GNUNET_OK != ret)
199     return ret;
200   if (GNUNET_OK != result)
201     return 1;
202   return 0;
203 }