-fixing misc issues and bugs, including better termination logic for intersection...
[oweals/gnunet.git] / src / set / gnunet-set-profiler.c
1 /*
2       This file is part of GNUnet
3       (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., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, 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
102   GNUNET_assert (GNUNET_NO == info->done);
103   switch (status)
104   {
105     case GNUNET_SET_STATUS_DONE:
106     case GNUNET_SET_STATUS_HALF_DONE:
107       info->done = GNUNET_YES;
108       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s done\n", info->id);
109       check_all_done ();
110       info->oh = NULL;
111       return;
112     case GNUNET_SET_STATUS_FAILURE:
113       info->oh = NULL;
114       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failure\n");
115       GNUNET_SCHEDULER_shutdown ();
116       return;
117     case GNUNET_SET_STATUS_OK:
118       break;
119     default:
120       GNUNET_assert (0);
121   }
122
123   if (element->size != sizeof (struct GNUNET_HashCode))
124   {
125     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "wrong element size: %u\n", element->size);
126     GNUNET_assert (0);
127   }
128
129   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: got element (%s)\n",
130               info->id, GNUNET_h2s (element->data));
131   GNUNET_assert (NULL != element->data);
132   GNUNET_CONTAINER_multihashmap_put (info->received,
133                                      element->data, NULL,
134                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
135 }
136
137
138 static void
139 set_listen_cb (void *cls,
140                const struct GNUNET_PeerIdentity *other_peer,
141                const struct GNUNET_MessageHeader *context_msg,
142                struct GNUNET_SET_Request *request)
143 {
144   if (NULL == request)
145   {
146     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
147                 "listener failed\n");
148     return;
149   }
150   GNUNET_assert (NULL == info2.oh);
151   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152               "set listen cb called\n");
153   info2.oh = GNUNET_SET_accept (request, GNUNET_SET_RESULT_ADDED,
154                                set_result_cb, &info2);
155   GNUNET_SET_commit (info2.oh, info2.set);
156 }
157
158
159 static int
160 set_insert_iterator (void *cls,
161                      const struct GNUNET_HashCode *key,
162                      void *value)
163 {
164   struct GNUNET_SET_Handle *set = cls;
165   struct GNUNET_SET_Element *el;
166
167   el = GNUNET_malloc (sizeof (struct GNUNET_SET_Element) +
168                       sizeof (struct GNUNET_HashCode));
169   el->element_type = 0;
170   memcpy (&el[1], key, sizeof *key);
171   el->data = &el[1];
172   el->size = sizeof *key;
173   GNUNET_SET_add_element (set, el, NULL, NULL);
174   GNUNET_free (el);
175   return GNUNET_YES;
176 }
177
178
179 static void
180 handle_shutdown (void *cls,
181                  const struct GNUNET_SCHEDULER_TaskContext *tc)
182 {
183   if (NULL != set_listener)
184   {
185     GNUNET_SET_listen_cancel (set_listener);
186     set_listener = NULL;
187   }
188   if (NULL != info1.oh)
189   {
190     GNUNET_SET_operation_cancel (info1.oh);
191     info1.oh = NULL;
192   }
193   if (NULL != info2.oh)
194   {
195     GNUNET_SET_operation_cancel (info2.oh);
196     info2.oh = NULL;
197   }
198   if (NULL != info1.set)
199   {
200     GNUNET_SET_destroy (info1.set);
201     info1.set = NULL;
202   }
203   if (NULL != info2.set)
204   {
205     GNUNET_SET_destroy (info2.set);
206     info2.set = NULL;
207   }
208 }
209
210
211 static void
212 run (void *cls, char *const *args, const char *cfgfile,
213      const struct GNUNET_CONFIGURATION_Handle *cfg)
214 {
215   unsigned int i;
216   struct GNUNET_HashCode hash;
217
218   config = cfg;
219
220   if (GNUNET_OK != GNUNET_CRYPTO_get_peer_identity (cfg, &local_peer))
221   {
222     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not retrieve host identity\n");
223     ret = 0;
224     return;
225   }
226
227   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, handle_shutdown, NULL);
228
229   info1.id = "a";
230   info2.id = "b";
231
232   info1.sent = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
233   info2.sent = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
234   common_sent = GNUNET_CONTAINER_multihashmap_create (num_c+1, GNUNET_NO);
235
236   info1.received = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
237   info2.received = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
238
239   for (i = 0; i < num_a; i++)
240   {
241     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
242     GNUNET_CONTAINER_multihashmap_put (info1.sent, &hash, NULL,
243                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
244   }
245
246   for (i = 0; i < num_b; i++)
247   {
248     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
249     GNUNET_CONTAINER_multihashmap_put (info2.sent, &hash, NULL,
250                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
251   }
252
253   for (i = 0; i < num_c; i++)
254   {
255     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
256     GNUNET_CONTAINER_multihashmap_put (common_sent, &hash, NULL,
257                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
258   }
259
260   /* use last hash for app id */
261   app_id = hash;
262
263   /* FIXME: also implement intersection etc. */
264   info1.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
265   info2.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
266
267   GNUNET_CONTAINER_multihashmap_iterate (info1.sent, set_insert_iterator, info1.set);
268   GNUNET_CONTAINER_multihashmap_iterate (info2.sent, set_insert_iterator, info2.set);
269   GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info1.set);
270   GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info2.set);
271
272   set_listener = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
273                                     &app_id, set_listen_cb, NULL);
274
275   info1.oh = GNUNET_SET_prepare (&local_peer, &app_id, NULL,
276                                  GNUNET_SET_RESULT_ADDED,
277                                  set_result_cb, &info1);
278   GNUNET_SET_commit (info1.oh, info1.set);
279   GNUNET_SET_destroy (info1.set);
280   info1.set = NULL;
281 }
282
283
284 int
285 main (int argc, char **argv)
286 {
287    static const struct GNUNET_GETOPT_CommandLineOption options[] = {
288       { 'A', "num-first", NULL,
289         gettext_noop ("number of values"),
290         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_a },
291       { 'B', "num-second", NULL,
292         gettext_noop ("number of values"),
293         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_b },
294       { 'C', "num-common", NULL,
295         gettext_noop ("number of values"),
296         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_c },
297       { 'x', "operation", NULL,
298         gettext_noop ("oeration to execute"),
299         GNUNET_YES, &GNUNET_GETOPT_set_string, &op_str },
300       GNUNET_GETOPT_OPTION_END
301   };
302   GNUNET_PROGRAM_run (argc, argv, "gnunet-set-profiler",
303                       "help",
304                       options, &run, NULL);
305   return ret;
306 }
307