def3012a39eb6e604a97c5a77518d7945bd40805
[oweals/gnunet.git] / src / consensus / gnunet-consensus-profiler.c
1 /*
2       This file is part of GNUnet
3       (C) 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 consensus/gnunet-consensus-profiler.c
23  * @brief profiling tool for gnunet-consensus
24  * @author Florian Dold
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_consensus_service.h"
30 #include "gnunet_testbed_service.h"
31
32 static unsigned int num_peers = 2;
33
34 static unsigned int replication = 1;
35
36 static unsigned int num_values = 5;
37
38 static struct GNUNET_TIME_Relative conclude_timeout;
39
40 static struct GNUNET_CONSENSUS_Handle **consensus_handles;
41
42 static struct GNUNET_TESTBED_Operation **testbed_operations;
43
44 static unsigned int num_connected_handles;
45
46 static struct GNUNET_TESTBED_Peer **peers;
47
48 static struct GNUNET_PeerIdentity *peer_ids;
49
50 static unsigned int num_retrieved_peer_ids;
51
52 static struct GNUNET_HashCode session_id;
53
54 static unsigned int peers_done = 0;
55
56 static unsigned *results_for_peer;
57
58 static int verbose;
59
60
61 /**
62  * Signature of the event handler function called by the
63  * respective event controller.
64  *
65  * @param cls closure
66  * @param event information about the event
67  */
68 static void
69 controller_cb(void *cls,
70               const struct GNUNET_TESTBED_EventInformation *event)
71 {
72   GNUNET_assert (0);
73 }
74
75 static void
76 destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *ctx)
77 {
78   struct GNUNET_CONSENSUS_Handle *consensus;
79   consensus = cls;
80   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying consensus\n");
81   GNUNET_CONSENSUS_destroy (consensus);
82   peers_done++;
83   if (peers_done == num_peers)
84   {
85     unsigned int i;
86     for (i = 0; i < num_peers; i++)
87       GNUNET_TESTBED_operation_done (testbed_operations[i]);
88     for (i = 0; i < num_peers; i++)
89       printf ("P%u got %u of %u elements\n", i, results_for_peer[i], num_values);
90     GNUNET_SCHEDULER_shutdown ();
91   }
92 }
93
94
95 /**
96  * Called when a conclusion was successful.
97  *
98  * @param cls closure, the consensus handle
99  * @return GNUNET_YES if more consensus groups should be offered, GNUNET_NO if not
100  */
101 static void
102 conclude_cb (void *cls)
103 {
104   struct GNUNET_CONSENSUS_Handle **chp = cls;
105   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "consensus %d done\n", chp - consensus_handles);
106   GNUNET_SCHEDULER_add_now (destroy, *chp);
107 }
108
109
110 static void
111 generate_indices (int *indices)
112 {
113   int j;
114   j = 0;
115   while (j < replication)
116   {
117     int n;
118     int k;
119     int repeat;
120     n = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
121     repeat = GNUNET_NO;
122     for (k = 0; k < j; k++)
123       if (indices[k] == n)
124       {
125         repeat = GNUNET_YES;
126         break;
127       }
128     if (GNUNET_NO == repeat)
129       indices[j++] = n;
130   }
131 }
132
133
134 static void
135 do_consensus ()
136 {
137   int unique_indices[replication];
138   int i;
139
140   for (i = 0; i < num_values; i++)
141   {
142     int j;
143     struct GNUNET_HashCode *val;
144     struct GNUNET_SET_Element *element;
145     generate_indices(unique_indices);
146
147     val = GNUNET_malloc (sizeof *val);
148     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, val);
149
150     element = GNUNET_malloc (sizeof *element);
151     element->data = val;
152     element->size = sizeof *val;
153
154     for (j = 0; j < replication; j++)
155     {
156       int cid;
157       cid = unique_indices[j];
158       GNUNET_CONSENSUS_insert (consensus_handles[cid], element, NULL, NULL);
159     }
160   }
161
162   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "all elements inserted, calling conclude\n");
163
164   for (i = 0; i < num_peers; i++)
165     GNUNET_CONSENSUS_conclude (consensus_handles[i], conclude_timeout, conclude_cb, &consensus_handles[i]);
166 }
167
168
169 /**
170  * Callback to be called when a service connect operation is completed
171  *
172  * @param cls the callback closure from functions generating an operation
173  * @param op the operation that has been finished
174  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
175  * @param emsg error message in case the operation has failed; will be NULL if
176  *          operation has executed successfully.
177  */
178 static void
179 connect_complete (void *cls,
180                   struct GNUNET_TESTBED_Operation *op,
181                   void *ca_result,
182                   const char *emsg)
183 {
184
185   if (NULL != emsg)
186   {
187     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "testbed connect emsg: %s\n", emsg);
188     GNUNET_assert (0);
189   }
190
191   num_connected_handles++;
192
193   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "connect complete\n");
194
195   if (num_connected_handles == num_peers)
196   {
197     do_consensus ();
198   }
199 }
200
201
202 static void
203 new_element_cb (void *cls,
204                 const struct GNUNET_SET_Element *element)
205 {
206   struct GNUNET_CONSENSUS_Handle **chp = cls;
207   int idx = chp - consensus_handles;
208
209   GNUNET_assert (NULL != cls);
210   
211   results_for_peer[idx]++;
212
213   GNUNET_assert (sizeof (struct GNUNET_HashCode) == element->size);
214
215   if (GNUNET_YES == verbose)
216   {
217     printf ("P%d received %s\n", idx, GNUNET_h2s ((struct GNUNET_HashCode *) element->data));
218   }
219 }
220
221
222 /**
223  * Adapter function called to establish a connection to
224  * a service.
225  *
226  * @param cls closure
227  * @param cfg configuration of the peer to connect to; will be available until
228  *          GNUNET_TESTBED_operation_done() is called on the operation returned
229  *          from GNUNET_TESTBED_service_connect()
230  * @return service handle to return in 'op_result', NULL on error
231  */
232 static void *
233 connect_adapter (void *cls,
234                  const struct GNUNET_CONFIGURATION_Handle *cfg)
235 {
236   struct GNUNET_CONSENSUS_Handle **chp = cls;
237   struct GNUNET_CONSENSUS_Handle *consensus;
238   chp = (struct GNUNET_CONSENSUS_Handle **) cls;
239
240   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "connect adapter, %d peers\n", num_peers);
241   consensus = GNUNET_CONSENSUS_create (cfg, num_peers, peer_ids, &session_id, new_element_cb, chp);
242   *chp = (struct GNUNET_CONSENSUS_Handle *) consensus;
243   return consensus;
244 }
245
246
247 /**
248  * Adapter function called to destroy a connection to
249  * a service.
250  *
251  * @param cls closure
252  * @param op_result service handle returned from the connect adapter
253  */
254 static void
255 disconnect_adapter(void *cls, void *op_result)
256 {
257   /* FIXME: what to do here? */
258 }
259
260
261 /**
262  * Callback to be called when the requested peer information is available
263  *
264  * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
265  * @param op the operation this callback corresponds to
266  * @param pinfo the result; will be NULL if the operation has failed
267  * @param emsg error message if the operation has failed; will be NULL if the
268  *          operation is successfull
269  */
270 static void
271 peer_info_cb (void *cb_cls,
272               struct GNUNET_TESTBED_Operation *op,
273               const struct GNUNET_TESTBED_PeerInformation *pinfo,
274               const char *emsg)
275 {
276   struct GNUNET_PeerIdentity *p;
277   int i;
278
279   GNUNET_assert (NULL == emsg);
280
281   p = (struct GNUNET_PeerIdentity *) cb_cls;
282
283   if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
284   {
285     *p = *pinfo->result.id;
286     num_retrieved_peer_ids++;
287     if (num_retrieved_peer_ids == num_peers)
288       for (i = 0; i < num_peers; i++)
289         testbed_operations[i] =
290             GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus", connect_complete, NULL,
291                                             connect_adapter, disconnect_adapter, &consensus_handles[i]);
292   }
293   else
294   {
295     GNUNET_assert (0);
296   }
297
298   GNUNET_TESTBED_operation_done (op);
299 }
300
301
302 /**
303  * Signature of a main function for a testcase.
304  *
305  * @param cls closure
306  * @param h the run handle
307  * @param num_peers number of peers in 'peers'
308  * @param started_peers handle to peers run in the testbed.  NULL upon timeout (see
309  *          GNUNET_TESTBED_test_run()).
310  * @param links_succeeded the number of overlay link connection attempts that
311  *          succeeded
312  * @param links_failed the number of overlay link connection attempts that
313  *          failed
314  */
315 static void
316 test_master (void *cls,
317              struct GNUNET_TESTBED_RunHandle *h,
318              unsigned int num_peers,
319              struct GNUNET_TESTBED_Peer **started_peers,
320              unsigned int links_succeeded,
321              unsigned int links_failed)
322 {
323   int i;
324
325   GNUNET_log_setup ("gnunet-consensus", "INFO", NULL);
326
327   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
328
329   peers = started_peers;
330
331   peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
332
333   results_for_peer = GNUNET_malloc (num_peers * sizeof (unsigned int));
334   consensus_handles = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
335   testbed_operations = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
336
337   for (i = 0; i < num_peers; i++)
338     GNUNET_TESTBED_peer_get_information (peers[i],
339                                          GNUNET_TESTBED_PIT_IDENTITY,
340                                          peer_info_cb,
341                                          &peer_ids[i]);
342 }
343
344 static void
345 run (void *cls, char *const *args, const char *cfgfile,
346      const struct GNUNET_CONFIGURATION_Handle *cfg)
347 {
348   static char *session_str = "gnunet-consensus/test";
349   char *topology;
350   int topology_cmp_result;
351
352   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed", "OVERLAY_TOPOLOGY", &topology))
353   {
354     fprintf (stderr, "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
355                      "seems like you passed the wrong configuration file\n");
356     return;
357   }
358
359   topology_cmp_result = strcasecmp (topology, "NONE");
360   GNUNET_free (topology);
361
362   if (0 == topology_cmp_result)
363   {
364     fprintf (stderr, "'OVERLAY_TOPOLOGY' set to 'NONE', "
365                      "seems like you passed the wrong configuration file\n");
366     return;
367   }
368
369   if (num_peers < replication)
370   {
371     fprintf (stderr, "k must be <=n\n");
372     return;
373   }
374
375   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "running gnunet-consensus\n");
376
377   GNUNET_CRYPTO_hash (session_str, strlen(session_str), &session_id);
378
379   (void) GNUNET_TESTBED_test_run ("gnunet-consensus",
380                                   cfgfile,
381                                   num_peers,
382                                   0,
383                                   controller_cb,
384                                   NULL,
385                                   test_master,
386                                   NULL);
387 }
388
389
390 int
391 main (int argc, char **argv)
392 {
393    static const struct GNUNET_GETOPT_CommandLineOption options[] = {
394       { 'n', "num-peers", NULL,
395         gettext_noop ("number of peers in consensus"),
396         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
397       { 'k', "value-replication", NULL,
398         gettext_noop ("how many peers receive one value?"),
399         GNUNET_YES, &GNUNET_GETOPT_set_uint, &replication },
400       { 'x', "num-values", NULL,
401         gettext_noop ("number of values"),
402         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_values },
403       { 't', "timeout", NULL,
404         gettext_noop ("consensus timeout"),
405         GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &conclude_timeout },
406       { 'V', "verbose", NULL,
407         gettext_noop ("be more verbose (print received values)"),
408         GNUNET_NO, &GNUNET_GETOPT_set_one, &verbose },
409       GNUNET_GETOPT_OPTION_END
410   };
411   conclude_timeout = GNUNET_TIME_UNIT_SECONDS;
412   GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus",
413                       "help",
414                       options, &run, NULL, GNUNET_YES);
415   return 0;
416 }
417