4b6a1e202a59cc176f8d6a63fcf05fa6340a3096
[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_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 unsigned int dump_statistics;
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 static struct GNUNET_CONTAINER_MultiHashMap *common_sent;
56
57 static struct GNUNET_HashCode app_id;
58
59 static struct GNUNET_PeerIdentity local_peer;
60
61 static struct GNUNET_SET_ListenHandle *set_listener;
62
63 static struct GNUNET_STATISTICS_Handle *statistics;
64
65
66 static int
67 map_remove_iterator (void *cls,
68                      const struct GNUNET_HashCode *key,
69                      void *value)
70 {
71   struct GNUNET_CONTAINER_MultiHashMap *m = cls;
72   int ret;
73
74   GNUNET_assert (NULL != key);
75
76   ret = GNUNET_CONTAINER_multihashmap_remove (m, key, NULL);
77   if (GNUNET_OK != ret)
78     printf ("spurious element\n");
79   return GNUNET_YES;
80
81 }
82
83
84 /**
85  * Callback function to process statistic values.
86  *
87  * @param cls closure
88  * @param subsystem name of subsystem that created the statistic
89  * @param name the name of the datum
90  * @param value the current value
91  * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
92  * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
93  */
94 static int
95 statistics_result (void *cls,
96                    const char *subsystem,
97                    const char *name,
98                    uint64_t value,
99                    int is_persistent)
100 {
101   printf ("stat %s/%s=%lu\n", subsystem, name, (unsigned long) value);
102   return GNUNET_OK;
103 }
104
105
106 static void
107 statistics_done (void *cls,
108                  int success)
109 {
110   GNUNET_assert (GNUNET_YES == success);
111   printf("dumped statistics\n");
112   GNUNET_SCHEDULER_shutdown ();
113 }
114
115 static void
116 check_all_done (void)
117 {
118   if (info1.done == GNUNET_NO || info2.done == GNUNET_NO)
119     return;
120
121   GNUNET_CONTAINER_multihashmap_iterate (info1.received, map_remove_iterator, info2.sent);
122   GNUNET_CONTAINER_multihashmap_iterate (info2.received, map_remove_iterator, info1.sent);
123
124   printf ("set a: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info1.sent));
125   printf ("set b: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info2.sent));
126
127   if (0 == dump_statistics)
128   {
129     GNUNET_SCHEDULER_shutdown ();
130     return;
131   }
132
133   GNUNET_STATISTICS_get (statistics, NULL, NULL, GNUNET_TIME_UNIT_FOREVER_REL,
134                          statistics_done, statistics_result, NULL);
135 }
136
137
138 static void
139 set_result_cb (void *cls,
140                  const struct GNUNET_SET_Element *element,
141                  enum GNUNET_SET_Status status)
142 {
143   struct SetInfo *info = cls;
144   struct GNUNET_HashCode hash;
145
146   GNUNET_assert (GNUNET_NO == info->done);
147   switch (status)
148   {
149     case GNUNET_SET_STATUS_DONE:
150     case GNUNET_SET_STATUS_HALF_DONE:
151       info->done = GNUNET_YES;
152       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s done\n", info->id);
153       check_all_done ();
154       info->oh = NULL;
155       return;
156     case GNUNET_SET_STATUS_FAILURE:
157       info->oh = NULL;
158       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failure\n");
159       GNUNET_SCHEDULER_shutdown ();
160       return;
161     case GNUNET_SET_STATUS_ADD_LOCAL:
162       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: local element\n", info->id);
163       break;
164     case GNUNET_SET_STATUS_ADD_REMOTE:
165       GNUNET_CRYPTO_hash (element->data, element->size, &hash);
166       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: remote element %s\n", info->id,
167                   GNUNET_h2s (&hash));
168       // XXX: record and check
169       return;
170     default:
171       GNUNET_assert (0);
172   }
173
174   if (element->size != sizeof (struct GNUNET_HashCode))
175   {
176     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "wrong element size: %u, expected %u\n", element->size, sizeof (struct GNUNET_HashCode));
177     GNUNET_assert (0);
178   }
179
180   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: got element (%s)\n",
181               info->id, GNUNET_h2s (element->data));
182   GNUNET_assert (NULL != element->data);
183   GNUNET_CONTAINER_multihashmap_put (info->received,
184                                      element->data, NULL,
185                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
186 }
187
188
189 static void
190 set_listen_cb (void *cls,
191                const struct GNUNET_PeerIdentity *other_peer,
192                const struct GNUNET_MessageHeader *context_msg,
193                struct GNUNET_SET_Request *request)
194 {
195   if (NULL == request)
196   {
197     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
198                 "listener failed\n");
199     return;
200   }
201   GNUNET_assert (NULL == info2.oh);
202   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203               "set listen cb called\n");
204   info2.oh = GNUNET_SET_accept (request, GNUNET_SET_RESULT_SYMMETRIC,
205                                set_result_cb, &info2);
206   GNUNET_SET_commit (info2.oh, info2.set);
207 }
208
209
210 static int
211 set_insert_iterator (void *cls,
212                      const struct GNUNET_HashCode *key,
213                      void *value)
214 {
215   struct GNUNET_SET_Handle *set = cls;
216   struct GNUNET_SET_Element *el;
217
218   el = GNUNET_malloc (sizeof (struct GNUNET_SET_Element) +
219                       sizeof (struct GNUNET_HashCode));
220   el->element_type = 0;
221   memcpy (&el[1], key, sizeof *key);
222   el->data = &el[1];
223   el->size = sizeof *key;
224   GNUNET_SET_add_element (set, el, NULL, NULL);
225   GNUNET_free (el);
226   return GNUNET_YES;
227 }
228
229
230 static void
231 handle_shutdown (void *cls,
232                  const struct GNUNET_SCHEDULER_TaskContext *tc)
233 {
234   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
235               "Shutting down set profiler\n");
236   if (NULL != set_listener)
237   {
238     GNUNET_SET_listen_cancel (set_listener);
239     set_listener = NULL;
240   }
241   if (NULL != info1.oh)
242   {
243     GNUNET_SET_operation_cancel (info1.oh);
244     info1.oh = NULL;
245   }
246   if (NULL != info2.oh)
247   {
248     GNUNET_SET_operation_cancel (info2.oh);
249     info2.oh = NULL;
250   }
251   if (NULL != info1.set)
252   {
253     GNUNET_SET_destroy (info1.set);
254     info1.set = NULL;
255   }
256   if (NULL != info2.set)
257   {
258     GNUNET_SET_destroy (info2.set);
259     info2.set = NULL;
260   }
261   GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
262 }
263
264
265 static void
266 run (void *cls,
267      const struct GNUNET_CONFIGURATION_Handle *cfg,
268      struct GNUNET_TESTING_Peer *peer)
269 {
270   unsigned int i;
271   struct GNUNET_HashCode hash;
272
273   config = cfg;
274
275   if (GNUNET_OK != GNUNET_CRYPTO_get_peer_identity (cfg, &local_peer))
276   {
277     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not retrieve host identity\n");
278     ret = 0;
279     return;
280   }
281
282   statistics = GNUNET_STATISTICS_create ("set-profiler", cfg);
283
284   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, handle_shutdown, NULL);
285
286   info1.id = "a";
287   info2.id = "b";
288
289   info1.sent = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
290   info2.sent = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
291   common_sent = GNUNET_CONTAINER_multihashmap_create (num_c+1, GNUNET_NO);
292
293   info1.received = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
294   info2.received = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
295
296   for (i = 0; i < num_a; i++)
297   {
298     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
299     GNUNET_CONTAINER_multihashmap_put (info1.sent, &hash, NULL,
300                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
301   }
302
303   for (i = 0; i < num_b; i++)
304   {
305     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
306     GNUNET_CONTAINER_multihashmap_put (info2.sent, &hash, NULL,
307                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
308   }
309
310   for (i = 0; i < num_c; i++)
311   {
312     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
313     GNUNET_CONTAINER_multihashmap_put (common_sent, &hash, NULL,
314                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
315   }
316
317   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &app_id);
318
319   /* FIXME: also implement intersection etc. */
320   info1.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
321   info2.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
322
323   GNUNET_CONTAINER_multihashmap_iterate (info1.sent, set_insert_iterator, info1.set);
324   GNUNET_CONTAINER_multihashmap_iterate (info2.sent, set_insert_iterator, info2.set);
325   GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info1.set);
326   GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info2.set);
327
328   set_listener = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
329                                     &app_id, set_listen_cb, NULL);
330
331   info1.oh = GNUNET_SET_prepare (&local_peer, &app_id, NULL,
332                                  GNUNET_SET_RESULT_SYMMETRIC,
333                                  set_result_cb, &info1);
334   GNUNET_SET_commit (info1.oh, info1.set);
335   GNUNET_SET_destroy (info1.set);
336   info1.set = NULL;
337 }
338
339
340 static void
341 pre_run (void *cls, char *const *args, const char *cfgfile,
342          const struct GNUNET_CONFIGURATION_Handle *cfg)
343 {
344   if (0 != GNUNET_TESTING_peer_run ("set-profiler",
345                                     cfgfile,
346                                     &run, NULL))
347     ret = 2;
348 }
349
350
351 int
352 main (int argc, char **argv)
353 {
354    static const struct GNUNET_GETOPT_CommandLineOption options[] = {
355       { 'A', "num-first", NULL,
356         gettext_noop ("number of values"),
357         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_a },
358       { 'B', "num-second", NULL,
359         gettext_noop ("number of values"),
360         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_b },
361       { 'C', "num-common", NULL,
362         gettext_noop ("number of values"),
363         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_c },
364       { 'x', "operation", NULL,
365         gettext_noop ("operation to execute"),
366         GNUNET_YES, &GNUNET_GETOPT_set_string, &op_str },
367       { 's', "statistics", NULL,
368         gettext_noop ("dump statistics to stdout after completion"),
369         GNUNET_NO, &GNUNET_GETOPT_set_one, &dump_statistics },
370       GNUNET_GETOPT_OPTION_END
371   };
372   GNUNET_PROGRAM_run2 (argc, argv, "gnunet-set-profiler",
373                       "help",
374                       options, &pre_run, NULL, GNUNET_YES);
375   return ret;
376 }
377