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