RPS: Change cosmetics - fix indentation
[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 it
6       under the terms of the GNU Affero General Public License as published
7       by the Free Software Foundation, either version 3 of the License,
8       or (at your 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       Affero General Public License for more details.
14      
15       You should have received a copy of the GNU Affero General Public License
16       along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /**
20  * @file consensus/gnunet-consensus-profiler.c
21  * @brief profiling tool for gnunet-consensus
22  * @author Florian Dold
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_time_lib.h"
27 #include "gnunet_consensus_service.h"
28 #include "gnunet_testbed_service.h"
29
30 static unsigned int num_peers = 2;
31
32 static unsigned int replication = 1;
33
34 static unsigned int num_values = 5;
35
36 static struct GNUNET_TIME_Relative conclude_timeout;
37
38 static struct GNUNET_TIME_Relative consensus_delay;
39
40 static struct GNUNET_CONSENSUS_Handle **consensus_handles;
41
42 static struct GNUNET_TESTBED_Operation **testbed_operations;
43
44 static unsigned int num_connected_handles;
45
46 static struct GNUNET_TESTBED_Peer **peers;
47
48 static struct GNUNET_PeerIdentity *peer_ids;
49
50 static unsigned int num_retrieved_peer_ids;
51
52 static struct GNUNET_HashCode session_id;
53
54 static unsigned int peers_done = 0;
55
56 static int dist_static;
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   unsigned int j;
221   struct GNUNET_HashCode val;
222   struct GNUNET_SET_Element element;
223
224   if (dist_static)
225   {
226     for (i = 0; i < num_values; i++)
227     {
228
229       GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &val);
230
231       element.data = &val;
232       element.size = sizeof (val);
233       for (j = 0; j < replication; j++)
234       {
235         GNUNET_CONSENSUS_insert (consensus_handles[j],
236                                  &element,
237                                  NULL, NULL);
238       }
239     }
240   }
241   else
242   {
243     for (i = 0; i < num_values; i++)
244     {
245       generate_indices (unique_indices);
246       GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &val);
247
248       element.data = &val;
249       element.size = sizeof (val);
250       for (j = 0; j < replication; j++)
251       {
252         int cid;
253
254         cid = unique_indices[j];
255         GNUNET_CONSENSUS_insert (consensus_handles[cid],
256                                  &element,
257                                  NULL, NULL);
258       }
259     }
260   }
261
262   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
263               "all elements inserted, calling conclude\n");
264
265   for (i = 0; i < num_peers; i++)
266     GNUNET_CONSENSUS_conclude (consensus_handles[i],
267                                conclude_cb, &consensus_handles[i]);
268 }
269
270
271 /**
272  * Callback to be called when a service connect operation is completed
273  *
274  * @param cls the callback closure from functions generating an operation
275  * @param op the operation that has been finished
276  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
277  * @param emsg error message in case the operation has failed; will be NULL if
278  *          operation has executed successfully.
279  */
280 static void
281 connect_complete (void *cls,
282                   struct GNUNET_TESTBED_Operation *op,
283                   void *ca_result,
284                   const char *emsg)
285 {
286
287   if (NULL != emsg)
288   {
289     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
290                 "testbed connect emsg: %s\n",
291                 emsg);
292     GNUNET_assert (0);
293   }
294
295   num_connected_handles++;
296
297   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
298               "connect complete\n");
299
300   if (num_connected_handles == num_peers)
301   {
302     do_consensus ();
303   }
304 }
305
306
307 static void
308 new_element_cb (void *cls,
309                 const struct GNUNET_SET_Element *element)
310 {
311   struct GNUNET_CONSENSUS_Handle **chp = cls;
312   int idx = chp - consensus_handles;
313
314   GNUNET_assert (NULL != cls);
315
316   results_for_peer[idx]++;
317
318   GNUNET_assert (sizeof (struct GNUNET_HashCode) == element->size);
319
320   if (GNUNET_YES == verbose)
321   {
322     printf ("P%d received %s\n",
323             idx,
324             GNUNET_h2s ((struct GNUNET_HashCode *) element->data));
325   }
326 }
327
328
329 /**
330  * Adapter function called to establish a connection to
331  * a service.
332  *
333  * @param cls closure
334  * @param cfg configuration of the peer to connect to; will be available until
335  *          GNUNET_TESTBED_operation_done() is called on the operation returned
336  *          from GNUNET_TESTBED_service_connect()
337  * @return service handle to return in 'op_result', NULL on error
338  */
339 static void *
340 connect_adapter (void *cls,
341                  const struct GNUNET_CONFIGURATION_Handle *cfg)
342 {
343   struct GNUNET_CONSENSUS_Handle **chp = cls;
344   struct GNUNET_CONSENSUS_Handle *consensus;
345   chp = (struct GNUNET_CONSENSUS_Handle **) cls;
346
347   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
348               "connect adapter, %d peers\n",
349               num_peers);
350   consensus = GNUNET_CONSENSUS_create (cfg,
351                                        num_peers, peer_ids,
352                                        &session_id,
353                                        start,
354                                        deadline,
355                                        &new_element_cb, chp);
356   *chp = (struct GNUNET_CONSENSUS_Handle *) consensus;
357   return consensus;
358 }
359
360
361 /**
362  * Adapter function called to destroy a connection to
363  * a service.
364  *
365  * @param cls closure
366  * @param op_result service handle returned from the connect adapter
367  */
368 static void
369 disconnect_adapter(void *cls, void *op_result)
370 {
371   /* FIXME: what to do here? */
372   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
373               "disconnect adapter called\n");
374 }
375
376
377 /**
378  * Callback to be called when the requested peer information is available
379  *
380  * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
381  * @param op the operation this callback corresponds to
382  * @param pinfo the result; will be NULL if the operation has failed
383  * @param emsg error message if the operation has failed; will be NULL if the
384  *          operation is successfull
385  */
386 static void
387 peer_info_cb (void *cb_cls,
388               struct GNUNET_TESTBED_Operation *op,
389               const struct GNUNET_TESTBED_PeerInformation *pinfo,
390               const char *emsg)
391 {
392   struct GNUNET_PeerIdentity *p;
393   int i;
394
395   GNUNET_assert (NULL == emsg);
396
397   p = (struct GNUNET_PeerIdentity *) cb_cls;
398
399   if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
400   {
401     *p = *pinfo->result.id;
402     num_retrieved_peer_ids++;
403     if (num_retrieved_peer_ids == num_peers)
404       for (i = 0; i < num_peers; i++)
405         testbed_operations[i] =
406             GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus", connect_complete, NULL,
407                                             connect_adapter, disconnect_adapter, &consensus_handles[i]);
408   }
409   else
410   {
411     GNUNET_assert (0);
412   }
413
414   GNUNET_TESTBED_operation_done (op);
415 }
416
417
418 /**
419  * Signature of a main function for a testcase.
420  *
421  * @param cls closure
422  * @param h the run handle
423  * @param num_peers number of peers in 'peers'
424  * @param started_peers handle to peers run in the testbed.  NULL upon timeout (see
425  *          GNUNET_TESTBED_test_run()).
426  * @param links_succeeded the number of overlay link connection attempts that
427  *          succeeded
428  * @param links_failed the number of overlay link connection attempts that
429  *          failed
430  */
431 static void
432 test_master (void *cls,
433              struct GNUNET_TESTBED_RunHandle *h,
434              unsigned int num_peers,
435              struct GNUNET_TESTBED_Peer **started_peers,
436              unsigned int links_succeeded,
437              unsigned int links_failed)
438 {
439   int i;
440
441   GNUNET_log_setup ("gnunet-consensus", "INFO", NULL);
442
443   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
444
445   peers = started_peers;
446
447   peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
448
449   results_for_peer = GNUNET_malloc (num_peers * sizeof (unsigned int));
450   consensus_handles = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
451   testbed_operations = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
452
453   for (i = 0; i < num_peers; i++)
454     GNUNET_TESTBED_peer_get_information (peers[i],
455                                          GNUNET_TESTBED_PIT_IDENTITY,
456                                          peer_info_cb,
457                                          &peer_ids[i]);
458 }
459
460
461 static void
462 run (void *cls, char *const *args, const char *cfgfile,
463      const struct GNUNET_CONFIGURATION_Handle *cfg)
464 {
465   static char *session_str = "gnunet-consensus/test";
466   char *topology;
467   int topology_cmp_result;
468
469   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed", "OVERLAY_TOPOLOGY", &topology))
470   {
471     fprintf (stderr,
472              "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
473              "seems like you passed the wrong configuration file\n");
474     return;
475   }
476
477   topology_cmp_result = strcasecmp (topology, "NONE");
478   GNUNET_free (topology);
479
480   if (0 == topology_cmp_result)
481   {
482     fprintf (stderr,
483              "'OVERLAY_TOPOLOGY' set to 'NONE', "
484              "seems like you passed the wrong configuration file\n");
485     return;
486   }
487
488   if (num_peers < replication)
489   {
490     fprintf (stderr, "k must be <=n\n");
491     return;
492   }
493
494   start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), consensus_delay);
495   deadline = GNUNET_TIME_absolute_add (start, conclude_timeout);
496
497   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
498               "running gnunet-consensus\n");
499
500   GNUNET_CRYPTO_hash (session_str, strlen(session_str), &session_id);
501
502   (void) GNUNET_TESTBED_test_run ("gnunet-consensus",
503                                   cfgfile,
504                                   num_peers,
505                                   0,
506                                   controller_cb,
507                                   NULL,
508                                   test_master,
509                                   NULL);
510 }
511
512
513 int
514 main (int argc, char **argv)
515 {
516    struct GNUNET_GETOPT_CommandLineOption options[] = {
517
518       GNUNET_GETOPT_option_uint ('n',
519                                      "num-peers",
520                                      NULL,
521                                      gettext_noop ("number of peers in consensus"),
522                                      &num_peers),
523
524       GNUNET_GETOPT_option_uint ('k',
525                                      "value-replication",
526                                      NULL,
527                                      gettext_noop ("how many peers (random selection without replacement) receive one value?"),
528                                      &replication),
529
530       GNUNET_GETOPT_option_uint ('x',
531                                      "num-values",
532                                      NULL,
533                                      gettext_noop ("number of values"),
534                                      &num_values),
535
536       GNUNET_GETOPT_option_relative_time ('t',
537                                               "timeout",
538                                               NULL,
539                                               gettext_noop ("consensus timeout"),
540                                               &conclude_timeout),
541
542
543       GNUNET_GETOPT_option_relative_time ('d',
544                                               "delay",
545                                               NULL,
546                                               gettext_noop ("delay until consensus starts"),
547                                               &consensus_delay),
548
549       GNUNET_GETOPT_option_filename ('s',
550                                      "statistics",
551                                      "FILENAME",
552                                      gettext_noop ("write statistics to file"),
553                                      &statistics_filename),
554
555       GNUNET_GETOPT_option_flag ('S',
556                                     "dist-static",
557                                     gettext_noop ("distribute elements to a static subset of good peers"),
558                                     &dist_static),
559
560       GNUNET_GETOPT_option_flag ('V',
561                                     "verbose",
562                                     gettext_noop ("be more verbose (print received values)"),
563                                     &verbose),
564
565       GNUNET_GETOPT_OPTION_END
566   };
567   conclude_timeout = GNUNET_TIME_UNIT_SECONDS;
568   GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-profiler",
569                       "help",
570                       options, &run, NULL, GNUNET_YES);
571   return 0;
572 }