remove CYGWIN codeblocks, drop vendored Windows openvpn, drop win32 specific files.
[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 it
6       under the terms of the GNU Affero General Public License as published
7       by the Free Software Foundation, either version 3 of the License,
8       or (at your 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       Affero General Public License for more details.
14
15       You should have received a copy of the GNU Affero General Public License
16       along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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   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 static int byzantine;
61 static unsigned int force_delta;
62 static unsigned int force_full;
63 static unsigned int element_size = 32;
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_all(m, key);
94   if (GNUNET_OK != ret)
95     printf("spurious element\n");
96   return GNUNET_YES;
97 }
98
99
100 /**
101  * Callback function to process statistic values.
102  *
103  * @param cls closure
104  * @param subsystem name of subsystem that created the statistic
105  * @param name the name of the datum
106  * @param value the current value
107  * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
108  * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
109  */
110 static int
111 statistics_result(void *cls,
112                   const char *subsystem,
113                   const char *name,
114                   uint64_t value,
115                   int is_persistent)
116 {
117   if (NULL != statistics_file)
118     {
119       fprintf(statistics_file, "%s\t%s\t%lu\n", subsystem, name, (unsigned long)value);
120     }
121   return GNUNET_OK;
122 }
123
124
125 static void
126 statistics_done(void *cls,
127                 int success)
128 {
129   GNUNET_assert(GNUNET_YES == success);
130   if (NULL != statistics_file)
131     fclose(statistics_file);
132   GNUNET_SCHEDULER_shutdown();
133 }
134
135
136 static void
137 check_all_done(void)
138 {
139   if (info1.done == GNUNET_NO || info2.done == GNUNET_NO)
140     return;
141
142   GNUNET_CONTAINER_multihashmap_iterate(info1.received, map_remove_iterator, info2.sent);
143   GNUNET_CONTAINER_multihashmap_iterate(info2.received, map_remove_iterator, info1.sent);
144
145   printf("set a: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size(info1.sent));
146   printf("set b: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size(info2.sent));
147
148   if (NULL == statistics_filename)
149     {
150       GNUNET_SCHEDULER_shutdown();
151       return;
152     }
153
154   statistics_file = fopen(statistics_filename, "w");
155   GNUNET_STATISTICS_get(statistics, NULL, NULL,
156                         &statistics_done,
157                         &statistics_result, NULL);
158 }
159
160
161 static void
162 set_result_cb(void *cls,
163               const struct GNUNET_SET_Element *element,
164               uint64_t current_size,
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
181     case GNUNET_SET_STATUS_FAILURE:
182       info->oh = NULL;
183       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "failure\n");
184       GNUNET_SCHEDULER_shutdown();
185       return;
186
187     case GNUNET_SET_STATUS_ADD_LOCAL:
188       GNUNET_log(GNUNET_ERROR_TYPE_INFO, "set %s: local element\n", info->id);
189       break;
190
191     case GNUNET_SET_STATUS_ADD_REMOTE:
192       GNUNET_CRYPTO_hash(element->data, element->size, &hash);
193       GNUNET_log(GNUNET_ERROR_TYPE_INFO, "set %s: remote element %s\n", info->id,
194                  GNUNET_h2s(&hash));
195       // XXX: record and check
196       return;
197
198     default:
199       GNUNET_assert(0);
200     }
201
202   if (element->size != element_size)
203     {
204       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
205                  "wrong element size: %u, expected %u\n",
206                  element->size,
207                  (unsigned int)sizeof(struct GNUNET_HashCode));
208       GNUNET_assert(0);
209     }
210
211   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "set %s: got element (%s)\n",
212              info->id, GNUNET_h2s(element->data));
213   GNUNET_assert(NULL != element->data);
214   struct GNUNET_HashCode data_hash;
215   GNUNET_CRYPTO_hash(element->data, element_size, &data_hash);
216   GNUNET_CONTAINER_multihashmap_put(info->received,
217                                     &data_hash, NULL,
218                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
219 }
220
221
222 static void
223 set_listen_cb(void *cls,
224               const struct GNUNET_PeerIdentity *other_peer,
225               const struct GNUNET_MessageHeader *context_msg,
226               struct GNUNET_SET_Request *request)
227 {
228   /* max. 2 options plus terminator */
229   struct GNUNET_SET_Option opts[3] = { { 0 } };
230   unsigned int n_opts = 0;
231
232   if (NULL == request)
233     {
234       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
235                  "listener failed\n");
236       return;
237     }
238   GNUNET_assert(NULL == info2.oh);
239   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
240              "set listen cb called\n");
241   if (byzantine)
242     {
243       opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_BYZANTINE };
244     }
245   GNUNET_assert(!(force_full && force_delta));
246   if (force_full)
247     {
248       opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_FULL };
249     }
250   if (force_delta)
251     {
252       opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_DELTA };
253     }
254
255   opts[n_opts].type = 0;
256   info2.oh = GNUNET_SET_accept(request, GNUNET_SET_RESULT_SYMMETRIC,
257                                opts,
258                                set_result_cb, &info2);
259   GNUNET_SET_commit(info2.oh, info2.set);
260 }
261
262
263 static int
264 set_insert_iterator(void *cls,
265                     const struct GNUNET_HashCode *key,
266                     void *value)
267 {
268   struct GNUNET_SET_Handle *set = cls;
269   struct GNUNET_SET_Element el;
270
271   el.element_type = 0;
272   el.data = value;
273   el.size = element_size;
274   GNUNET_SET_add_element(set, &el, NULL, NULL);
275   return GNUNET_YES;
276 }
277
278
279 static void
280 handle_shutdown(void *cls)
281 {
282   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
283              "Shutting down set profiler\n");
284   if (NULL != set_listener)
285     {
286       GNUNET_SET_listen_cancel(set_listener);
287       set_listener = NULL;
288     }
289   if (NULL != info1.oh)
290     {
291       GNUNET_SET_operation_cancel(info1.oh);
292       info1.oh = NULL;
293     }
294   if (NULL != info2.oh)
295     {
296       GNUNET_SET_operation_cancel(info2.oh);
297       info2.oh = NULL;
298     }
299   if (NULL != info1.set)
300     {
301       GNUNET_SET_destroy(info1.set);
302       info1.set = NULL;
303     }
304   if (NULL != info2.set)
305     {
306       GNUNET_SET_destroy(info2.set);
307       info2.set = NULL;
308     }
309   GNUNET_STATISTICS_destroy(statistics, GNUNET_NO);
310 }
311
312
313 static void
314 run(void *cls,
315     const struct GNUNET_CONFIGURATION_Handle *cfg,
316     struct GNUNET_TESTING_Peer *peer)
317 {
318   unsigned int i;
319   struct GNUNET_HashCode hash;
320   /* max. 2 options plus terminator */
321   struct GNUNET_SET_Option opts[3] = { { 0 } };
322   unsigned int n_opts = 0;
323
324   config = cfg;
325
326   GNUNET_assert(element_size > 0);
327
328   if (GNUNET_OK != GNUNET_CRYPTO_get_peer_identity(cfg, &local_peer))
329     {
330       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "could not retrieve host identity\n");
331       ret = 0;
332       return;
333     }
334
335   statistics = GNUNET_STATISTICS_create("set-profiler", cfg);
336
337   GNUNET_SCHEDULER_add_shutdown(&handle_shutdown, NULL);
338
339   info1.id = "a";
340   info2.id = "b";
341
342   info1.sent = GNUNET_CONTAINER_multihashmap_create(num_a + 1, GNUNET_NO);
343   info2.sent = GNUNET_CONTAINER_multihashmap_create(num_b + 1, GNUNET_NO);
344   common_sent = GNUNET_CONTAINER_multihashmap_create(num_c + 1, GNUNET_NO);
345
346   info1.received = GNUNET_CONTAINER_multihashmap_create(num_a + 1, GNUNET_NO);
347   info2.received = GNUNET_CONTAINER_multihashmap_create(num_b + 1, GNUNET_NO);
348
349   for (i = 0; i < num_a; i++)
350     {
351       char *data = GNUNET_malloc(element_size);
352       GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, data, element_size);
353       GNUNET_CRYPTO_hash(data, element_size, &hash);
354       GNUNET_CONTAINER_multihashmap_put(info1.sent, &hash, data,
355                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
356     }
357
358   for (i = 0; i < num_b; i++)
359     {
360       char *data = GNUNET_malloc(element_size);
361       GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, data, element_size);
362       GNUNET_CRYPTO_hash(data, element_size, &hash);
363       GNUNET_CONTAINER_multihashmap_put(info2.sent, &hash, data,
364                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
365     }
366
367   for (i = 0; i < num_c; i++)
368     {
369       char *data = GNUNET_malloc(element_size);
370       GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, data, element_size);
371       GNUNET_CRYPTO_hash(data, element_size, &hash);
372       GNUNET_CONTAINER_multihashmap_put(common_sent, &hash, data,
373                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
374     }
375
376   GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_STRONG, &app_id);
377
378   /* FIXME: also implement intersection etc. */
379   info1.set = GNUNET_SET_create(config, GNUNET_SET_OPERATION_UNION);
380   info2.set = GNUNET_SET_create(config, GNUNET_SET_OPERATION_UNION);
381
382   GNUNET_CONTAINER_multihashmap_iterate(info1.sent, set_insert_iterator, info1.set);
383   GNUNET_CONTAINER_multihashmap_iterate(info2.sent, set_insert_iterator, info2.set);
384   GNUNET_CONTAINER_multihashmap_iterate(common_sent, set_insert_iterator, info1.set);
385   GNUNET_CONTAINER_multihashmap_iterate(common_sent, set_insert_iterator, info2.set);
386
387   set_listener = GNUNET_SET_listen(config, GNUNET_SET_OPERATION_UNION,
388                                    &app_id, set_listen_cb, NULL);
389
390
391   if (byzantine)
392     {
393       opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_BYZANTINE };
394     }
395   GNUNET_assert(!(force_full && force_delta));
396   if (force_full)
397     {
398       opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_FULL };
399     }
400   if (force_delta)
401     {
402       opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_DELTA };
403     }
404
405   opts[n_opts].type = 0;
406
407   info1.oh = GNUNET_SET_prepare(&local_peer, &app_id, NULL,
408                                 GNUNET_SET_RESULT_SYMMETRIC,
409                                 opts,
410                                 set_result_cb, &info1);
411   GNUNET_SET_commit(info1.oh, info1.set);
412   GNUNET_SET_destroy(info1.set);
413   info1.set = NULL;
414 }
415
416
417 static void
418 pre_run(void *cls, char *const *args, const char *cfgfile,
419         const struct GNUNET_CONFIGURATION_Handle *cfg)
420 {
421   if (0 != GNUNET_TESTING_peer_run("set-profiler",
422                                    cfgfile,
423                                    &run, NULL))
424     ret = 2;
425 }
426
427
428 int
429 main(int argc, char **argv)
430 {
431   struct GNUNET_GETOPT_CommandLineOption options[] = {
432     GNUNET_GETOPT_option_uint('A',
433                               "num-first",
434                               NULL,
435                               gettext_noop("number of values"),
436                               &num_a),
437
438     GNUNET_GETOPT_option_uint('B',
439                               "num-second",
440                               NULL,
441                               gettext_noop("number of values"),
442                               &num_b),
443
444     GNUNET_GETOPT_option_flag('b',
445                               "byzantine",
446                               gettext_noop("use byzantine mode"),
447                               &byzantine),
448
449     GNUNET_GETOPT_option_uint('f',
450                               "force-full",
451                               NULL,
452                               gettext_noop("force sending full set"),
453                               &force_full),
454
455     GNUNET_GETOPT_option_uint('d',
456                               "force-delta",
457                               NULL,
458                               gettext_noop("number delta operation"),
459                               &force_delta),
460
461     GNUNET_GETOPT_option_uint('C',
462                               "num-common",
463                               NULL,
464                               gettext_noop("number of values"),
465                               &num_c),
466
467     GNUNET_GETOPT_option_string('x',
468                                 "operation",
469                                 NULL,
470                                 gettext_noop("operation to execute"),
471                                 &op_str),
472
473     GNUNET_GETOPT_option_uint('w',
474                               "element-size",
475                               NULL,
476                               gettext_noop("element size"),
477                               &element_size),
478
479     GNUNET_GETOPT_option_filename('s',
480                                   "statistics",
481                                   "FILENAME",
482                                   gettext_noop("write statistics to file"),
483                                   &statistics_filename),
484
485     GNUNET_GETOPT_OPTION_END
486   };
487
488   GNUNET_PROGRAM_run2(argc, argv, "gnunet-set-profiler",
489                       "help",
490                       options, &pre_run, NULL, GNUNET_YES);
491   return ret;
492 }