- another fix to generation handling and lazy copying
[oweals/gnunet.git] / src / set / gnunet-set-profiler.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2013 Christian Grothoff (and other contributing authors)
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_set_service.h"
29 #include "gnunet_testbed_service.h"
30
31
32 static int ret;
33
34 static unsigned int num_a = 5;
35 static unsigned int num_b = 5;
36 static unsigned int num_c = 20;
37
38 static char *op_str = "union";
39
40 const static struct GNUNET_CONFIGURATION_Handle *config;
41
42 struct SetInfo
43 {
44   char *id;
45   struct GNUNET_SET_Handle *set;
46   struct GNUNET_SET_OperationHandle *oh;
47   struct GNUNET_CONTAINER_MultiHashMap *sent;
48   struct GNUNET_CONTAINER_MultiHashMap *received;
49   int done;
50 } info1, info2;
51
52 static struct GNUNET_CONTAINER_MultiHashMap *common_sent;
53
54 static struct GNUNET_HashCode app_id;
55
56 static struct GNUNET_PeerIdentity local_peer;
57
58 static struct GNUNET_SET_ListenHandle *set_listener;
59
60
61 static int
62 map_remove_iterator (void *cls,
63                      const struct GNUNET_HashCode *key,
64                      void *value)
65 {
66   struct GNUNET_CONTAINER_MultiHashMap *m = cls;
67   int ret;
68
69   GNUNET_assert (NULL != key);
70
71   ret = GNUNET_CONTAINER_multihashmap_remove (m, key, NULL);
72   if (GNUNET_OK != ret)
73     printf ("spurious element\n");
74   return GNUNET_YES;
75
76 }
77
78
79 static void
80 check_all_done (void)
81 {
82   if (info1.done == GNUNET_NO || info2.done == GNUNET_NO)
83     return;
84
85   GNUNET_CONTAINER_multihashmap_iterate (info1.received, map_remove_iterator, info2.sent);
86   GNUNET_CONTAINER_multihashmap_iterate (info2.received, map_remove_iterator, info1.sent);
87
88   printf ("set a: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info1.sent));
89   printf ("set b: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info2.sent));
90
91   GNUNET_SCHEDULER_shutdown ();
92 }
93
94
95 static void
96 set_result_cb (void *cls,
97                  const struct GNUNET_SET_Element *element,
98                  enum GNUNET_SET_Status status)
99 {
100   struct SetInfo *info = cls;
101   struct GNUNET_HashCode hash;
102
103   GNUNET_assert (GNUNET_NO == info->done);
104   switch (status)
105   {
106     case GNUNET_SET_STATUS_DONE:
107     case GNUNET_SET_STATUS_HALF_DONE:
108       info->done = GNUNET_YES;
109       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s done\n", info->id);
110       check_all_done ();
111       info->oh = NULL;
112       return;
113     case GNUNET_SET_STATUS_FAILURE:
114       info->oh = NULL;
115       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failure\n");
116       GNUNET_SCHEDULER_shutdown ();
117       return;
118     case GNUNET_SET_STATUS_ADD_LOCAL:
119       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: local element\n", info->id);
120       break;
121     case GNUNET_SET_STATUS_ADD_REMOTE:
122       GNUNET_CRYPTO_hash (element->data, element->size, &hash);
123       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: remote element %s\n", info->id,
124                   GNUNET_h2s (&hash));
125       // XXX: record and check
126       return;
127     default:
128       GNUNET_assert (0);
129   }
130
131   if (element->size != sizeof (struct GNUNET_HashCode))
132   {
133     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "wrong element size: %u, expected %u\n", element->size, sizeof (struct GNUNET_HashCode));
134     GNUNET_assert (0);
135   }
136
137   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: got element (%s)\n",
138               info->id, GNUNET_h2s (element->data));
139   GNUNET_assert (NULL != element->data);
140   GNUNET_CONTAINER_multihashmap_put (info->received,
141                                      element->data, NULL,
142                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
143 }
144
145
146 static void
147 set_listen_cb (void *cls,
148                const struct GNUNET_PeerIdentity *other_peer,
149                const struct GNUNET_MessageHeader *context_msg,
150                struct GNUNET_SET_Request *request)
151 {
152   if (NULL == request)
153   {
154     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
155                 "listener failed\n");
156     return;
157   }
158   GNUNET_assert (NULL == info2.oh);
159   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160               "set listen cb called\n");
161   info2.oh = GNUNET_SET_accept (request, GNUNET_SET_RESULT_SYMMETRIC,
162                                set_result_cb, &info2);
163   GNUNET_SET_commit (info2.oh, info2.set);
164 }
165
166
167 static int
168 set_insert_iterator (void *cls,
169                      const struct GNUNET_HashCode *key,
170                      void *value)
171 {
172   struct GNUNET_SET_Handle *set = cls;
173   struct GNUNET_SET_Element *el;
174
175   el = GNUNET_malloc (sizeof (struct GNUNET_SET_Element) +
176                       sizeof (struct GNUNET_HashCode));
177   el->element_type = 0;
178   memcpy (&el[1], key, sizeof *key);
179   el->data = &el[1];
180   el->size = sizeof *key;
181   GNUNET_SET_add_element (set, el, NULL, NULL);
182   GNUNET_free (el);
183   return GNUNET_YES;
184 }
185
186
187 static void
188 handle_shutdown (void *cls,
189                  const struct GNUNET_SCHEDULER_TaskContext *tc)
190 {
191   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
192               "Shutting down set profiler\n");
193   if (NULL != set_listener)
194   {
195     GNUNET_SET_listen_cancel (set_listener);
196     set_listener = NULL;
197   }
198   if (NULL != info1.oh)
199   {
200     GNUNET_SET_operation_cancel (info1.oh);
201     info1.oh = NULL;
202   }
203   if (NULL != info2.oh)
204   {
205     GNUNET_SET_operation_cancel (info2.oh);
206     info2.oh = NULL;
207   }
208   if (NULL != info1.set)
209   {
210     GNUNET_SET_destroy (info1.set);
211     info1.set = NULL;
212   }
213   if (NULL != info2.set)
214   {
215     GNUNET_SET_destroy (info2.set);
216     info2.set = NULL;
217   }
218 }
219
220
221 static void
222 run (void *cls,
223      const struct GNUNET_CONFIGURATION_Handle *cfg,
224      struct GNUNET_TESTING_Peer *peer)
225 {
226   unsigned int i;
227   struct GNUNET_HashCode hash;
228   struct GNUNET_HashCode hashhash;
229
230   config = cfg;
231
232   if (GNUNET_OK != GNUNET_CRYPTO_get_peer_identity (cfg, &local_peer))
233   {
234     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not retrieve host identity\n");
235     ret = 0;
236     return;
237   }
238
239   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, handle_shutdown, NULL);
240
241   info1.id = "a";
242   info2.id = "b";
243
244   info1.sent = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
245   info2.sent = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
246   common_sent = GNUNET_CONTAINER_multihashmap_create (num_c+1, GNUNET_NO);
247
248   info1.received = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
249   info2.received = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
250
251   for (i = 0; i < num_a; i++)
252   {
253     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
254     GNUNET_CRYPTO_hash (&hash, sizeof (struct GNUNET_HashCode), &hashhash);
255     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Set a: Created element %s\n",
256                 GNUNET_h2s (&hashhash));
257     GNUNET_CONTAINER_multihashmap_put (info1.sent, &hash, NULL,
258                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
259   }
260
261   for (i = 0; i < num_b; i++)
262   {
263     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
264     GNUNET_CRYPTO_hash (&hash, sizeof (struct GNUNET_HashCode), &hashhash);
265     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Set b: Created element %s\n",
266                 GNUNET_h2s (&hashhash));
267     GNUNET_CONTAINER_multihashmap_put (info2.sent, &hash, NULL,
268                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
269   }
270
271   for (i = 0; i < num_c; i++)
272   {
273     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
274     GNUNET_CRYPTO_hash (&hash, sizeof (struct GNUNET_HashCode), &hashhash);
275     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Set c: Created element %s\n",
276                 GNUNET_h2s (&hashhash));
277     GNUNET_CONTAINER_multihashmap_put (common_sent, &hash, NULL,
278                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
279   }
280
281   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &app_id);
282
283   /* FIXME: also implement intersection etc. */
284   info1.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
285   info2.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
286
287   GNUNET_CONTAINER_multihashmap_iterate (info1.sent, set_insert_iterator, info1.set);
288   GNUNET_CONTAINER_multihashmap_iterate (info2.sent, set_insert_iterator, info2.set);
289   GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info1.set);
290   GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info2.set);
291
292   set_listener = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
293                                     &app_id, set_listen_cb, NULL);
294
295   info1.oh = GNUNET_SET_prepare (&local_peer, &app_id, NULL,
296                                  GNUNET_SET_RESULT_SYMMETRIC,
297                                  set_result_cb, &info1);
298   GNUNET_SET_commit (info1.oh, info1.set);
299   GNUNET_SET_destroy (info1.set);
300   info1.set = NULL;
301 }
302
303
304 static void
305 pre_run (void *cls, char *const *args, const char *cfgfile,
306          const struct GNUNET_CONFIGURATION_Handle *cfg)
307 {
308   if (0 != GNUNET_TESTING_peer_run ("set-profiler",
309                                     cfgfile,
310                                     &run, NULL))
311     ret = 2;
312 }
313
314
315 int
316 main (int argc, char **argv)
317 {
318    static const struct GNUNET_GETOPT_CommandLineOption options[] = {
319       { 'A', "num-first", NULL,
320         gettext_noop ("number of values"),
321         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_a },
322       { 'B', "num-second", NULL,
323         gettext_noop ("number of values"),
324         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_b },
325       { 'C', "num-common", NULL,
326         gettext_noop ("number of values"),
327         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_c },
328       { 'x', "operation", NULL,
329         gettext_noop ("operation to execute"),
330         GNUNET_YES, &GNUNET_GETOPT_set_string, &op_str },
331       GNUNET_GETOPT_OPTION_END
332   };
333   GNUNET_PROGRAM_run2 (argc, argv, "gnunet-set-profiler",
334                       "help",
335                       options, &pre_run, NULL, GNUNET_YES);
336   return ret;
337 }
338