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