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