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