diff options
author | Christian Grothoff <christian@grothoff.org> | 2020-08-16 20:46:39 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2020-08-16 20:46:39 +0200 |
commit | be0475f2a583d203465d3091ff933806a5ace613 (patch) | |
tree | cbb1ad1a27cd91a6ff1ccb8025595cbe4c15972a /src/setu/gnunet-setu-profiler.c | |
parent | f1f40feb2beb5c036da0e2b93c433b09b920e0b4 (diff) | |
download | gnunet-be0475f2a583d203465d3091ff933806a5ace613.tar.gz gnunet-be0475f2a583d203465d3091ff933806a5ace613.zip |
split of set union from set service (preliminary)
Diffstat (limited to 'src/setu/gnunet-setu-profiler.c')
-rw-r--r-- | src/setu/gnunet-setu-profiler.c | 499 |
1 files changed, 499 insertions, 0 deletions
diff --git a/src/setu/gnunet-setu-profiler.c b/src/setu/gnunet-setu-profiler.c new file mode 100644 index 000000000..8d6a2dc8c --- /dev/null +++ b/src/setu/gnunet-setu-profiler.c | |||
@@ -0,0 +1,499 @@ | |||
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 setu/gnunet-setu-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_setu_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 | { | ||
45 | char *id; | ||
46 | struct GNUNET_SETU_Handle *set; | ||
47 | struct GNUNET_SETU_OperationHandle *oh; | ||
48 | struct GNUNET_CONTAINER_MultiHashMap *sent; | ||
49 | struct GNUNET_CONTAINER_MultiHashMap *received; | ||
50 | int done; | ||
51 | } info1, info2; | ||
52 | |||
53 | static struct GNUNET_CONTAINER_MultiHashMap *common_sent; | ||
54 | |||
55 | static struct GNUNET_HashCode app_id; | ||
56 | |||
57 | static struct GNUNET_PeerIdentity local_peer; | ||
58 | |||
59 | static struct GNUNET_SETU_ListenHandle *set_listener; | ||
60 | |||
61 | static int byzantine; | ||
62 | static unsigned int force_delta; | ||
63 | static unsigned int force_full; | ||
64 | static unsigned int element_size = 32; | ||
65 | |||
66 | /** | ||
67 | * Handle to the statistics service. | ||
68 | */ | ||
69 | static struct GNUNET_STATISTICS_Handle *statistics; | ||
70 | |||
71 | /** | ||
72 | * The profiler will write statistics | ||
73 | * for all peers to the file with this name. | ||
74 | */ | ||
75 | static char *statistics_filename; | ||
76 | |||
77 | /** | ||
78 | * The profiler will write statistics | ||
79 | * for all peers to this file. | ||
80 | */ | ||
81 | static FILE *statistics_file; | ||
82 | |||
83 | |||
84 | static int | ||
85 | map_remove_iterator (void *cls, | ||
86 | const struct GNUNET_HashCode *key, | ||
87 | void *value) | ||
88 | { | ||
89 | struct GNUNET_CONTAINER_MultiHashMap *m = cls; | ||
90 | int ret; | ||
91 | |||
92 | GNUNET_assert (NULL != key); | ||
93 | |||
94 | ret = GNUNET_CONTAINER_multihashmap_remove_all (m, key); | ||
95 | if (GNUNET_OK != ret) | ||
96 | printf ("spurious element\n"); | ||
97 | return GNUNET_YES; | ||
98 | } | ||
99 | |||
100 | |||
101 | /** | ||
102 | * Callback function to process statistic values. | ||
103 | * | ||
104 | * @param cls closure | ||
105 | * @param subsystem name of subsystem that created the statistic | ||
106 | * @param name the name of the datum | ||
107 | * @param value the current value | ||
108 | * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not | ||
109 | * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration | ||
110 | */ | ||
111 | static int | ||
112 | statistics_result (void *cls, | ||
113 | const char *subsystem, | ||
114 | const char *name, | ||
115 | uint64_t value, | ||
116 | int is_persistent) | ||
117 | { | ||
118 | if (NULL != statistics_file) | ||
119 | { | ||
120 | fprintf (statistics_file, "%s\t%s\t%lu\n", subsystem, name, (unsigned | ||
121 | long) value); | ||
122 | } | ||
123 | return GNUNET_OK; | ||
124 | } | ||
125 | |||
126 | |||
127 | static void | ||
128 | statistics_done (void *cls, | ||
129 | int success) | ||
130 | { | ||
131 | GNUNET_assert (GNUNET_YES == success); | ||
132 | if (NULL != statistics_file) | ||
133 | fclose (statistics_file); | ||
134 | GNUNET_SCHEDULER_shutdown (); | ||
135 | } | ||
136 | |||
137 | |||
138 | static void | ||
139 | check_all_done (void) | ||
140 | { | ||
141 | if ((info1.done == GNUNET_NO) || (info2.done == GNUNET_NO)) | ||
142 | return; | ||
143 | |||
144 | GNUNET_CONTAINER_multihashmap_iterate (info1.received, map_remove_iterator, | ||
145 | info2.sent); | ||
146 | GNUNET_CONTAINER_multihashmap_iterate (info2.received, map_remove_iterator, | ||
147 | info1.sent); | ||
148 | |||
149 | printf ("set a: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size ( | ||
150 | info1.sent)); | ||
151 | printf ("set b: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size ( | ||
152 | info2.sent)); | ||
153 | |||
154 | if (NULL == statistics_filename) | ||
155 | { | ||
156 | GNUNET_SCHEDULER_shutdown (); | ||
157 | return; | ||
158 | } | ||
159 | |||
160 | statistics_file = fopen (statistics_filename, "w"); | ||
161 | GNUNET_STATISTICS_get (statistics, NULL, NULL, | ||
162 | &statistics_done, | ||
163 | &statistics_result, NULL); | ||
164 | } | ||
165 | |||
166 | |||
167 | static void | ||
168 | set_result_cb (void *cls, | ||
169 | const struct GNUNET_SETU_Element *element, | ||
170 | uint64_t current_size, | ||
171 | enum GNUNET_SETU_Status status) | ||
172 | { | ||
173 | struct SetInfo *info = cls; | ||
174 | |||
175 | GNUNET_assert (GNUNET_NO == info->done); | ||
176 | switch (status) | ||
177 | { | ||
178 | case GNUNET_SETU_STATUS_DONE: | ||
179 | info->done = GNUNET_YES; | ||
180 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s done\n", info->id); | ||
181 | check_all_done (); | ||
182 | info->oh = NULL; | ||
183 | return; | ||
184 | |||
185 | case GNUNET_SETU_STATUS_FAILURE: | ||
186 | info->oh = NULL; | ||
187 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failure\n"); | ||
188 | GNUNET_SCHEDULER_shutdown (); | ||
189 | return; | ||
190 | |||
191 | case GNUNET_SETU_STATUS_ADD_LOCAL: | ||
192 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: local element\n", info->id); | ||
193 | break; | ||
194 | default: | ||
195 | GNUNET_assert (0); | ||
196 | } | ||
197 | |||
198 | if (element->size != element_size) | ||
199 | { | ||
200 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
201 | "wrong element size: %u, expected %u\n", | ||
202 | element->size, | ||
203 | (unsigned int) sizeof(struct GNUNET_HashCode)); | ||
204 | GNUNET_assert (0); | ||
205 | } | ||
206 | |||
207 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: got element (%s)\n", | ||
208 | info->id, GNUNET_h2s (element->data)); | ||
209 | GNUNET_assert (NULL != element->data); | ||
210 | struct GNUNET_HashCode data_hash; | ||
211 | GNUNET_CRYPTO_hash (element->data, element_size, &data_hash); | ||
212 | GNUNET_CONTAINER_multihashmap_put (info->received, | ||
213 | &data_hash, NULL, | ||
214 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE); | ||
215 | } | ||
216 | |||
217 | |||
218 | static void | ||
219 | set_listen_cb (void *cls, | ||
220 | const struct GNUNET_PeerIdentity *other_peer, | ||
221 | const struct GNUNET_MessageHeader *context_msg, | ||
222 | struct GNUNET_SETU_Request *request) | ||
223 | { | ||
224 | /* max. 2 options plus terminator */ | ||
225 | struct GNUNET_SETU_Option opts[3] = { { 0 } }; | ||
226 | unsigned int n_opts = 0; | ||
227 | |||
228 | if (NULL == request) | ||
229 | { | ||
230 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
231 | "listener failed\n"); | ||
232 | return; | ||
233 | } | ||
234 | GNUNET_assert (NULL == info2.oh); | ||
235 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
236 | "set listen cb called\n"); | ||
237 | if (byzantine) | ||
238 | { | ||
239 | opts[n_opts++] = (struct GNUNET_SETU_Option) { .type = | ||
240 | GNUNET_SETU_OPTION_BYZANTINE }; | ||
241 | } | ||
242 | GNUNET_assert (! (force_full && force_delta)); | ||
243 | if (force_full) | ||
244 | { | ||
245 | opts[n_opts++] = (struct GNUNET_SETU_Option) { .type = | ||
246 | GNUNET_SETU_OPTION_FORCE_FULL }; | ||
247 | } | ||
248 | if (force_delta) | ||
249 | { | ||
250 | opts[n_opts++] = (struct GNUNET_SETU_Option) { .type = | ||
251 | GNUNET_SETU_OPTION_FORCE_DELTA }; | ||
252 | } | ||
253 | |||
254 | opts[n_opts].type = 0; | ||
255 | info2.oh = GNUNET_SETU_accept (request, | ||
256 | opts, | ||
257 | set_result_cb, &info2); | ||
258 | GNUNET_SETU_commit (info2.oh, info2.set); | ||
259 | } | ||
260 | |||
261 | |||
262 | static int | ||
263 | set_insert_iterator (void *cls, | ||
264 | const struct GNUNET_HashCode *key, | ||
265 | void *value) | ||
266 | { | ||
267 | struct GNUNET_SETU_Handle *set = cls; | ||
268 | struct GNUNET_SETU_Element el; | ||
269 | |||
270 | el.element_type = 0; | ||
271 | el.data = value; | ||
272 | el.size = element_size; | ||
273 | GNUNET_SETU_add_element (set, &el, NULL, NULL); | ||
274 | return GNUNET_YES; | ||
275 | } | ||
276 | |||
277 | |||
278 | static void | ||
279 | handle_shutdown (void *cls) | ||
280 | { | ||
281 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
282 | "Shutting down set profiler\n"); | ||
283 | if (NULL != set_listener) | ||
284 | { | ||
285 | GNUNET_SETU_listen_cancel (set_listener); | ||
286 | set_listener = NULL; | ||
287 | } | ||
288 | if (NULL != info1.oh) | ||
289 | { | ||
290 | GNUNET_SETU_operation_cancel (info1.oh); | ||
291 | info1.oh = NULL; | ||
292 | } | ||
293 | if (NULL != info2.oh) | ||
294 | { | ||
295 | GNUNET_SETU_operation_cancel (info2.oh); | ||
296 | info2.oh = NULL; | ||
297 | } | ||
298 | if (NULL != info1.set) | ||
299 | { | ||
300 | GNUNET_SETU_destroy (info1.set); | ||
301 | info1.set = NULL; | ||
302 | } | ||
303 | if (NULL != info2.set) | ||
304 | { | ||
305 | GNUNET_SETU_destroy (info2.set); | ||
306 | info2.set = NULL; | ||
307 | } | ||
308 | GNUNET_STATISTICS_destroy (statistics, GNUNET_NO); | ||
309 | } | ||
310 | |||
311 | |||
312 | static void | ||
313 | run (void *cls, | ||
314 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
315 | struct GNUNET_TESTING_Peer *peer) | ||
316 | { | ||
317 | unsigned int i; | ||
318 | struct GNUNET_HashCode hash; | ||
319 | /* max. 2 options plus terminator */ | ||
320 | struct GNUNET_SETU_Option opts[3] = { { 0 } }; | ||
321 | unsigned int n_opts = 0; | ||
322 | |||
323 | config = cfg; | ||
324 | |||
325 | GNUNET_assert (element_size > 0); | ||
326 | |||
327 | if (GNUNET_OK != GNUNET_CRYPTO_get_peer_identity (cfg, &local_peer)) | ||
328 | { | ||
329 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not retrieve host identity\n"); | ||
330 | ret = 0; | ||
331 | return; | ||
332 | } | ||
333 | |||
334 | statistics = GNUNET_STATISTICS_create ("set-profiler", cfg); | ||
335 | |||
336 | GNUNET_SCHEDULER_add_shutdown (&handle_shutdown, NULL); | ||
337 | |||
338 | info1.id = "a"; | ||
339 | info2.id = "b"; | ||
340 | |||
341 | info1.sent = GNUNET_CONTAINER_multihashmap_create (num_a + 1, GNUNET_NO); | ||
342 | info2.sent = GNUNET_CONTAINER_multihashmap_create (num_b + 1, GNUNET_NO); | ||
343 | common_sent = GNUNET_CONTAINER_multihashmap_create (num_c + 1, GNUNET_NO); | ||
344 | |||
345 | info1.received = GNUNET_CONTAINER_multihashmap_create (num_a + 1, GNUNET_NO); | ||
346 | info2.received = GNUNET_CONTAINER_multihashmap_create (num_b + 1, GNUNET_NO); | ||
347 | |||
348 | for (i = 0; i < num_a; i++) | ||
349 | { | ||
350 | char *data = GNUNET_malloc (element_size); | ||
351 | GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, data, element_size); | ||
352 | GNUNET_CRYPTO_hash (data, element_size, &hash); | ||
353 | GNUNET_CONTAINER_multihashmap_put (info1.sent, &hash, data, | ||
354 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE); | ||
355 | } | ||
356 | |||
357 | for (i = 0; i < num_b; i++) | ||
358 | { | ||
359 | char *data = GNUNET_malloc (element_size); | ||
360 | GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, data, element_size); | ||
361 | GNUNET_CRYPTO_hash (data, element_size, &hash); | ||
362 | GNUNET_CONTAINER_multihashmap_put (info2.sent, &hash, data, | ||
363 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE); | ||
364 | } | ||
365 | |||
366 | for (i = 0; i < num_c; i++) | ||
367 | { | ||
368 | char *data = GNUNET_malloc (element_size); | ||
369 | GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, data, element_size); | ||
370 | GNUNET_CRYPTO_hash (data, element_size, &hash); | ||
371 | GNUNET_CONTAINER_multihashmap_put (common_sent, &hash, data, | ||
372 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE); | ||
373 | } | ||
374 | |||
375 | GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &app_id); | ||
376 | |||
377 | /* FIXME: also implement intersection etc. */ | ||
378 | info1.set = GNUNET_SETU_create (config); | ||
379 | info2.set = GNUNET_SETU_create (config); | ||
380 | |||
381 | GNUNET_CONTAINER_multihashmap_iterate (info1.sent, set_insert_iterator, | ||
382 | info1.set); | ||
383 | GNUNET_CONTAINER_multihashmap_iterate (info2.sent, set_insert_iterator, | ||
384 | info2.set); | ||
385 | GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, | ||
386 | info1.set); | ||
387 | GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, | ||
388 | info2.set); | ||
389 | |||
390 | set_listener = GNUNET_SETU_listen (config, | ||
391 | &app_id, | ||
392 | &set_listen_cb, | ||
393 | NULL); | ||
394 | |||
395 | |||
396 | if (byzantine) | ||
397 | { | ||
398 | opts[n_opts++] = (struct GNUNET_SETU_Option) { .type = | ||
399 | GNUNET_SETU_OPTION_BYZANTINE }; | ||
400 | } | ||
401 | GNUNET_assert (! (force_full && force_delta)); | ||
402 | if (force_full) | ||
403 | { | ||
404 | opts[n_opts++] = (struct GNUNET_SETU_Option) { .type = | ||
405 | GNUNET_SETU_OPTION_FORCE_FULL }; | ||
406 | } | ||
407 | if (force_delta) | ||
408 | { | ||
409 | opts[n_opts++] = (struct GNUNET_SETU_Option) { .type = | ||
410 | GNUNET_SETU_OPTION_FORCE_DELTA }; | ||
411 | } | ||
412 | |||
413 | opts[n_opts].type = 0; | ||
414 | |||
415 | info1.oh = GNUNET_SETU_prepare (&local_peer, &app_id, NULL, | ||
416 | opts, | ||
417 | set_result_cb, &info1); | ||
418 | GNUNET_SETU_commit (info1.oh, info1.set); | ||
419 | GNUNET_SETU_destroy (info1.set); | ||
420 | info1.set = NULL; | ||
421 | } | ||
422 | |||
423 | |||
424 | static void | ||
425 | pre_run (void *cls, char *const *args, const char *cfgfile, | ||
426 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
427 | { | ||
428 | if (0 != GNUNET_TESTING_peer_run ("set-profiler", | ||
429 | cfgfile, | ||
430 | &run, NULL)) | ||
431 | ret = 2; | ||
432 | } | ||
433 | |||
434 | |||
435 | int | ||
436 | main (int argc, char **argv) | ||
437 | { | ||
438 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
439 | GNUNET_GETOPT_option_uint ('A', | ||
440 | "num-first", | ||
441 | NULL, | ||
442 | gettext_noop ("number of values"), | ||
443 | &num_a), | ||
444 | |||
445 | GNUNET_GETOPT_option_uint ('B', | ||
446 | "num-second", | ||
447 | NULL, | ||
448 | gettext_noop ("number of values"), | ||
449 | &num_b), | ||
450 | |||
451 | GNUNET_GETOPT_option_flag ('b', | ||
452 | "byzantine", | ||
453 | gettext_noop ("use byzantine mode"), | ||
454 | &byzantine), | ||
455 | |||
456 | GNUNET_GETOPT_option_uint ('f', | ||
457 | "force-full", | ||
458 | NULL, | ||
459 | gettext_noop ("force sending full set"), | ||
460 | &force_full), | ||
461 | |||
462 | GNUNET_GETOPT_option_uint ('d', | ||
463 | "force-delta", | ||
464 | NULL, | ||
465 | gettext_noop ("number delta operation"), | ||
466 | &force_delta), | ||
467 | |||
468 | GNUNET_GETOPT_option_uint ('C', | ||
469 | "num-common", | ||
470 | NULL, | ||
471 | gettext_noop ("number of values"), | ||
472 | &num_c), | ||
473 | |||
474 | GNUNET_GETOPT_option_string ('x', | ||
475 | "operation", | ||
476 | NULL, | ||
477 | gettext_noop ("operation to execute"), | ||
478 | &op_str), | ||
479 | |||
480 | GNUNET_GETOPT_option_uint ('w', | ||
481 | "element-size", | ||
482 | NULL, | ||
483 | gettext_noop ("element size"), | ||
484 | &element_size), | ||
485 | |||
486 | GNUNET_GETOPT_option_filename ('s', | ||
487 | "statistics", | ||
488 | "FILENAME", | ||
489 | gettext_noop ("write statistics to file"), | ||
490 | &statistics_filename), | ||
491 | |||
492 | GNUNET_GETOPT_OPTION_END | ||
493 | }; | ||
494 | |||
495 | GNUNET_PROGRAM_run2 (argc, argv, "gnunet-set-profiler", | ||
496 | "help", | ||
497 | options, &pre_run, NULL, GNUNET_YES); | ||
498 | return ret; | ||
499 | } | ||