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