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