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