f19776f79a350cabd287dc3bc3a66b2cf31de14c
[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_common.h"
28 #include "gnunet_util_lib.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 unsigned int salt = 42;
40
41 static char *op_str = "union";
42
43 const static struct GNUNET_CONFIGURATION_Handle *config;
44
45 struct SetInfo
46 {
47   char *id;
48   struct GNUNET_SET_Handle *set;
49   struct GNUNET_SET_OperationHandle *oh;
50   struct GNUNET_CONTAINER_MultiHashMap *sent;
51   struct GNUNET_CONTAINER_MultiHashMap *received;
52   int done;
53 } info1, info2;
54
55 struct GNUNET_CONTAINER_MultiHashMap *common_sent;
56
57 struct GNUNET_HashCode app_id;
58
59 struct GNUNET_PeerIdentity local_peer;
60
61 struct GNUNET_SET_ListenHandle *set_listener;
62
63
64 static int
65 map_remove_iterator (void *cls,
66                      const struct GNUNET_HashCode *key,
67                      void *value)
68 {
69   struct GNUNET_CONTAINER_MultiHashMap *m = cls;
70   int ret;
71
72   GNUNET_assert (NULL != key);
73
74   ret = GNUNET_CONTAINER_multihashmap_remove (m, key, NULL);
75   if (GNUNET_OK != ret)
76     printf ("spurious element\n");
77   return GNUNET_YES;
78
79 }
80
81 static void
82 check_all_done (void)
83 {
84   if (info1.done == GNUNET_NO || info2.done == GNUNET_NO)
85     return;
86
87   GNUNET_CONTAINER_multihashmap_iterate (info1.received, map_remove_iterator, info2.sent);
88   GNUNET_CONTAINER_multihashmap_iterate (info2.received, map_remove_iterator, info1.sent);
89
90   printf ("set a: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info1.sent));
91   printf ("set b: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info2.sent));
92
93   GNUNET_SCHEDULER_shutdown ();
94 }
95
96
97 static void
98 set_result_cb (void *cls,
99                  const struct GNUNET_SET_Element *element,
100                  enum GNUNET_SET_Status status)
101 {
102   struct SetInfo *info = cls;
103
104   GNUNET_assert (GNUNET_NO == info->done);
105   switch (status)
106   {
107     case GNUNET_SET_STATUS_DONE:
108     case GNUNET_SET_STATUS_HALF_DONE:
109       info->done = GNUNET_YES;
110       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s done\n", info->id);
111       check_all_done ();
112       info->oh = NULL;
113       return;
114     case GNUNET_SET_STATUS_FAILURE:
115       info->oh = NULL;
116       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failure\n");
117       GNUNET_SCHEDULER_shutdown ();
118       return;
119     case GNUNET_SET_STATUS_OK:
120       break;
121     default:
122       GNUNET_assert (0);
123   }
124
125   if (element->size != sizeof (struct GNUNET_HashCode))
126   {
127     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "wrong element size: %u\n", element->size);
128     GNUNET_assert (0);
129   }
130
131   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: got element (%s)\n",
132               info->id, GNUNET_h2s (element->data));
133   GNUNET_assert (NULL != element->data);
134   GNUNET_CONTAINER_multihashmap_put (info->received,
135                                      element->data, NULL,
136                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
137 }
138
139
140 static void
141 set_listen_cb (void *cls,
142                const struct GNUNET_PeerIdentity *other_peer,
143                const struct GNUNET_MessageHeader *context_msg,
144                struct GNUNET_SET_Request *request)
145 {
146   if (NULL == request)
147   {
148     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "listener failed\n");
149     return;
150   }
151   GNUNET_assert (NULL == info2.oh);
152   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "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
160 static int
161 set_insert_iterator (void *cls,
162                      const struct GNUNET_HashCode *key,
163                      void *value)
164 {
165   struct GNUNET_SET_Handle *set = cls;
166   struct GNUNET_SET_Element *el;
167
168   el = GNUNET_malloc (sizeof *el + sizeof *key);
169   el->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 static void
211 run (void *cls, char *const *args, const char *cfgfile,
212      const struct GNUNET_CONFIGURATION_Handle *cfg)
213 {
214   unsigned int i;
215   struct GNUNET_HashCode hash;
216
217   config = cfg;
218
219   if (GNUNET_OK != GNUNET_CRYPTO_get_host_identity (cfg, &local_peer))
220   {
221     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not retrieve host identity\n");
222     ret = 0;
223     return;
224   }
225
226   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, handle_shutdown, NULL);
227
228   info1.id = "a";
229   info2.id = "b";
230   
231   info1.sent = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
232   info2.sent = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
233   common_sent = GNUNET_CONTAINER_multihashmap_create (num_c+1, GNUNET_NO);
234
235   info1.received = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
236   info2.received = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
237
238   for (i = 0; i < num_a; i++)
239   {
240     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
241     GNUNET_CONTAINER_multihashmap_put (info1.sent, &hash, NULL,
242                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
243   }
244
245   for (i = 0; i < num_b; i++)
246   {
247     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
248     GNUNET_CONTAINER_multihashmap_put (info2.sent, &hash, NULL,
249                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
250   }
251
252   for (i = 0; i < num_c; i++)
253   {
254     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
255     GNUNET_CONTAINER_multihashmap_put (common_sent, &hash, NULL,
256                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
257   }
258
259   /* use last hash for app id */
260   app_id = hash;
261
262   /* FIXME: also implement intersection etc. */
263   info1.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
264   info2.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
265
266   GNUNET_CONTAINER_multihashmap_iterate (info1.sent, set_insert_iterator, info1.set);
267   GNUNET_CONTAINER_multihashmap_iterate (info2.sent, set_insert_iterator, info2.set);
268   GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info1.set);
269   GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info2.set);
270
271   set_listener = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
272                                     &app_id, set_listen_cb, NULL);
273
274   info1.oh = GNUNET_SET_prepare (&local_peer, &app_id, NULL, salt, GNUNET_SET_RESULT_ADDED,
275                        set_result_cb, &info1);
276   GNUNET_SET_commit (info1.oh, info1.set);
277   GNUNET_SET_destroy (info1.set);
278   info1.set = NULL;
279 }
280
281
282 int
283 main (int argc, char **argv)
284 {
285    static const struct GNUNET_GETOPT_CommandLineOption options[] = {
286       { 'A', "num-first", NULL,
287         gettext_noop ("number of values"),
288         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_a },
289       { 'B', "num-second", NULL,
290         gettext_noop ("number of values"),
291         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_b },
292       { 'C', "num-common", NULL,
293         gettext_noop ("number of values"),
294         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_c },
295       { 'x', "operation", NULL,
296         gettext_noop ("oeration to execute"),
297         GNUNET_YES, &GNUNET_GETOPT_set_string, &op_str },
298       GNUNET_GETOPT_OPTION_END
299   };
300   GNUNET_PROGRAM_run (argc, argv, "gnunet-set-profiler",
301                       "help",
302                       options, &run, NULL);
303   return ret;
304 }
305