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