Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / set / gnunet-set-profiler.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2013 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 set/gnunet-set-profiler.c
23  * @brief profiling tool for set
24  * @author Florian Dold
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet_set_service.h"
30 #include "gnunet_testbed_service.h"
31
32
33 static int ret;
34
35 static unsigned int num_a = 5;
36 static unsigned int num_b = 5;
37 static unsigned int num_c = 20;
38
39 static char *op_str = "union";
40
41 const static struct GNUNET_CONFIGURATION_Handle *config;
42
43 struct SetInfo
44 {
45   char *id;
46   struct GNUNET_SET_Handle *set;
47   struct GNUNET_SET_OperationHandle *oh;
48   struct GNUNET_CONTAINER_MultiHashMap *sent;
49   struct GNUNET_CONTAINER_MultiHashMap *received;
50   int done;
51 } info1, info2;
52
53 static struct GNUNET_CONTAINER_MultiHashMap *common_sent;
54
55 static struct GNUNET_HashCode app_id;
56
57 static struct GNUNET_PeerIdentity local_peer;
58
59 static struct GNUNET_SET_ListenHandle *set_listener;
60
61 static int byzantine;
62 static int force_delta;
63 static int force_full;
64 static unsigned int element_size = 32;
65
66 /**
67  * Handle to the statistics service.
68  */
69 static struct GNUNET_STATISTICS_Handle *statistics;
70
71 /**
72  * The profiler will write statistics
73  * for all peers to the file with this name.
74  */
75 static char *statistics_filename;
76
77 /**
78  * The profiler will write statistics
79  * for all peers to this file.
80  */
81 static FILE *statistics_file;
82
83
84 static int
85 map_remove_iterator (void *cls,
86                      const struct GNUNET_HashCode *key,
87                      void *value)
88 {
89   struct GNUNET_CONTAINER_MultiHashMap *m = cls;
90   int ret;
91
92   GNUNET_assert (NULL != key);
93
94   ret = GNUNET_CONTAINER_multihashmap_remove_all (m, key);
95   if (GNUNET_OK != ret)
96     printf ("spurious element\n");
97   return GNUNET_YES;
98
99 }
100
101
102 /**
103  * Callback function to process statistic values.
104  *
105  * @param cls closure
106  * @param subsystem name of subsystem that created the statistic
107  * @param name the name of the datum
108  * @param value the current value
109  * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
110  * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
111  */
112 static int
113 statistics_result (void *cls,
114                    const char *subsystem,
115                    const char *name,
116                    uint64_t value,
117                    int is_persistent)
118 {
119   if (NULL != statistics_file)
120   {
121     fprintf (statistics_file, "%s\t%s\t%lu\n", subsystem, name, (unsigned long) value);
122   }
123   return GNUNET_OK;
124 }
125
126
127 static void
128 statistics_done (void *cls,
129                  int success)
130 {
131   GNUNET_assert (GNUNET_YES == success);
132   if (NULL != statistics_file)
133     fclose (statistics_file);
134   GNUNET_SCHEDULER_shutdown ();
135 }
136
137
138 static void
139 check_all_done (void)
140 {
141   if (info1.done == GNUNET_NO || info2.done == GNUNET_NO)
142     return;
143
144   GNUNET_CONTAINER_multihashmap_iterate (info1.received, map_remove_iterator, info2.sent);
145   GNUNET_CONTAINER_multihashmap_iterate (info2.received, map_remove_iterator, info1.sent);
146
147   printf ("set a: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info1.sent));
148   printf ("set b: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info2.sent));
149
150   if (NULL == statistics_filename)
151   {
152     GNUNET_SCHEDULER_shutdown ();
153     return;
154   }
155
156   statistics_file = fopen (statistics_filename, "w");
157   GNUNET_STATISTICS_get (statistics, NULL, NULL,
158                          &statistics_done,
159                          &statistics_result, NULL);
160 }
161
162
163 static void
164 set_result_cb (void *cls,
165                const struct GNUNET_SET_Element *element,
166                uint64_t current_size,
167                enum GNUNET_SET_Status status)
168 {
169   struct SetInfo *info = cls;
170   struct GNUNET_HashCode hash;
171
172   GNUNET_assert (GNUNET_NO == info->done);
173   switch (status)
174   {
175     case GNUNET_SET_STATUS_DONE:
176     case GNUNET_SET_STATUS_HALF_DONE:
177       info->done = GNUNET_YES;
178       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s done\n", info->id);
179       check_all_done ();
180       info->oh = NULL;
181       return;
182     case GNUNET_SET_STATUS_FAILURE:
183       info->oh = NULL;
184       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failure\n");
185       GNUNET_SCHEDULER_shutdown ();
186       return;
187     case GNUNET_SET_STATUS_ADD_LOCAL:
188       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: local element\n", info->id);
189       break;
190     case GNUNET_SET_STATUS_ADD_REMOTE:
191       GNUNET_CRYPTO_hash (element->data, element->size, &hash);
192       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: remote element %s\n", info->id,
193                   GNUNET_h2s (&hash));
194       // XXX: record and check
195       return;
196     default:
197       GNUNET_assert (0);
198   }
199
200   if (element->size != element_size)
201   {
202     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
203                 "wrong element size: %u, expected %u\n",
204                 element->size,
205                 (unsigned int) sizeof (struct GNUNET_HashCode));
206     GNUNET_assert (0);
207   }
208
209   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: got element (%s)\n",
210               info->id, GNUNET_h2s (element->data));
211   GNUNET_assert (NULL != element->data);
212   struct GNUNET_HashCode data_hash;
213   GNUNET_CRYPTO_hash (element->data, element_size, &data_hash);
214   GNUNET_CONTAINER_multihashmap_put (info->received,
215                                      &data_hash, NULL,
216                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
217 }
218
219
220 static void
221 set_listen_cb (void *cls,
222                const struct GNUNET_PeerIdentity *other_peer,
223                const struct GNUNET_MessageHeader *context_msg,
224                struct GNUNET_SET_Request *request)
225 {
226   /* max. 2 options plus terminator */
227   struct GNUNET_SET_Option opts[3] = {{0}};
228   unsigned int n_opts = 0;
229
230   if (NULL == request)
231   {
232     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
233                 "listener failed\n");
234     return;
235   }
236   GNUNET_assert (NULL == info2.oh);
237   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238               "set listen cb called\n");
239   if (byzantine)
240   {
241     opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_BYZANTINE };
242   }
243   GNUNET_assert (!(force_full && force_delta));
244   if (force_full)
245   {
246     opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_FULL };
247   }
248   if (force_delta)
249   {
250     opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_DELTA };
251   }
252
253   opts[n_opts].type = 0;
254   info2.oh = GNUNET_SET_accept (request, GNUNET_SET_RESULT_SYMMETRIC,
255                                 opts,
256                                 set_result_cb, &info2);
257   GNUNET_SET_commit (info2.oh, info2.set);
258 }
259
260
261 static int
262 set_insert_iterator (void *cls,
263                      const struct GNUNET_HashCode *key,
264                      void *value)
265 {
266   struct GNUNET_SET_Handle *set = cls;
267   struct GNUNET_SET_Element el;
268
269   el.element_type = 0;
270   el.data = value;
271   el.size = element_size;
272   GNUNET_SET_add_element (set, &el, NULL, NULL);
273   return GNUNET_YES;
274 }
275
276
277 static void
278 handle_shutdown (void *cls)
279 {
280   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
281               "Shutting down set profiler\n");
282   if (NULL != set_listener)
283   {
284     GNUNET_SET_listen_cancel (set_listener);
285     set_listener = NULL;
286   }
287   if (NULL != info1.oh)
288   {
289     GNUNET_SET_operation_cancel (info1.oh);
290     info1.oh = NULL;
291   }
292   if (NULL != info2.oh)
293   {
294     GNUNET_SET_operation_cancel (info2.oh);
295     info2.oh = NULL;
296   }
297   if (NULL != info1.set)
298   {
299     GNUNET_SET_destroy (info1.set);
300     info1.set = NULL;
301   }
302   if (NULL != info2.set)
303   {
304     GNUNET_SET_destroy (info2.set);
305     info2.set = NULL;
306   }
307   GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
308 }
309
310
311 static void
312 run (void *cls,
313      const struct GNUNET_CONFIGURATION_Handle *cfg,
314      struct GNUNET_TESTING_Peer *peer)
315 {
316   unsigned int i;
317   struct GNUNET_HashCode hash;
318   /* max. 2 options plus terminator */
319   struct GNUNET_SET_Option opts[3] = {{0}};
320   unsigned int n_opts = 0;
321
322   config = cfg;
323
324   GNUNET_assert (element_size > 0);
325
326   if (GNUNET_OK != GNUNET_CRYPTO_get_peer_identity (cfg, &local_peer))
327   {
328     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not retrieve host identity\n");
329     ret = 0;
330     return;
331   }
332
333   statistics = GNUNET_STATISTICS_create ("set-profiler", cfg);
334
335   GNUNET_SCHEDULER_add_shutdown (&handle_shutdown, NULL);
336
337   info1.id = "a";
338   info2.id = "b";
339
340   info1.sent = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
341   info2.sent = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
342   common_sent = GNUNET_CONTAINER_multihashmap_create (num_c+1, GNUNET_NO);
343
344   info1.received = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
345   info2.received = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
346
347   for (i = 0; i < num_a; i++)
348   {
349     char *data = GNUNET_malloc (element_size);
350     GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, data, element_size);
351     GNUNET_CRYPTO_hash (data, element_size, &hash);
352     GNUNET_CONTAINER_multihashmap_put (info1.sent, &hash, data,
353                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
354   }
355
356   for (i = 0; i < num_b; i++)
357   {
358     char *data = GNUNET_malloc (element_size);
359     GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, data, element_size);
360     GNUNET_CRYPTO_hash (data, element_size, &hash);
361     GNUNET_CONTAINER_multihashmap_put (info2.sent, &hash, data,
362                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
363   }
364
365   for (i = 0; i < num_c; i++)
366   {
367     char *data = GNUNET_malloc (element_size);
368     GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, data, element_size);
369     GNUNET_CRYPTO_hash (data, element_size, &hash);
370     GNUNET_CONTAINER_multihashmap_put (common_sent, &hash, data,
371                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
372   }
373
374   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &app_id);
375
376   /* FIXME: also implement intersection etc. */
377   info1.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
378   info2.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
379
380   GNUNET_CONTAINER_multihashmap_iterate (info1.sent, set_insert_iterator, info1.set);
381   GNUNET_CONTAINER_multihashmap_iterate (info2.sent, set_insert_iterator, info2.set);
382   GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info1.set);
383   GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info2.set);
384
385   set_listener = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
386                                     &app_id, set_listen_cb, NULL);
387
388
389   if (byzantine)
390   {
391     opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_BYZANTINE };
392   }
393   GNUNET_assert (!(force_full && force_delta));
394   if (force_full)
395   {
396     opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_FULL };
397   }
398   if (force_delta)
399   {
400     opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_DELTA };
401   }
402
403   opts[n_opts].type = 0;
404
405   info1.oh = GNUNET_SET_prepare (&local_peer, &app_id, NULL,
406                                  GNUNET_SET_RESULT_SYMMETRIC,
407                                  opts,
408                                  set_result_cb, &info1);
409   GNUNET_SET_commit (info1.oh, info1.set);
410   GNUNET_SET_destroy (info1.set);
411   info1.set = NULL;
412 }
413
414
415 static void
416 pre_run (void *cls, char *const *args, const char *cfgfile,
417          const struct GNUNET_CONFIGURATION_Handle *cfg)
418 {
419   if (0 != GNUNET_TESTING_peer_run ("set-profiler",
420                                     cfgfile,
421                                     &run, NULL))
422     ret = 2;
423 }
424
425
426 int
427 main (int argc, char **argv)
428 {
429    static const struct GNUNET_GETOPT_CommandLineOption options[] = {
430       { 'A', "num-first", NULL,
431         gettext_noop ("number of values"),
432         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_a },
433       { 'B', "num-second", NULL,
434         gettext_noop ("number of values"),
435         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_b },
436       { 'b', "byzantine", NULL,
437         gettext_noop ("use byzantine mode"),
438         GNUNET_NO, &GNUNET_GETOPT_set_one, &byzantine },
439       { 'f', "force-full", NULL,
440         gettext_noop ("force sending full set"),
441         GNUNET_NO, &GNUNET_GETOPT_set_uint, &force_full },
442       { 'd', "force-delta", NULL,
443         gettext_noop ("number delta operation"),
444         GNUNET_NO, &GNUNET_GETOPT_set_uint, &force_delta },
445       { 'C', "num-common", NULL,
446         gettext_noop ("number of values"),
447         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_c },
448       { 'x', "operation", NULL,
449         gettext_noop ("operation to execute"),
450         GNUNET_YES, &GNUNET_GETOPT_set_string, &op_str },
451       { 'w', "element-size", NULL,
452         gettext_noop ("element size"),
453         GNUNET_YES, &GNUNET_GETOPT_set_uint, &element_size },
454       { 's', "statistics", NULL,
455         gettext_noop ("write statistics to file"),
456         GNUNET_YES, &GNUNET_GETOPT_set_filename, &statistics_filename },
457       GNUNET_GETOPT_OPTION_END
458   };
459   GNUNET_PROGRAM_run2 (argc, argv, "gnunet-set-profiler",
460                       "help",
461                       options, &pre_run, NULL, GNUNET_YES);
462   return ret;
463 }