2 This file is part of GNUnet.
3 Copyright (C) 2014, 2018 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file dht/gnunet_dht_profiler.c
23 * @brief Profiler for GNUnet DHT
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_constants.h"
34 #define MESSAGE(...) \
35 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, __VA_ARGS__)
38 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
41 * Number of peers which should perform a PUT out of 100 peers
43 static unsigned int put_probability = 100;
48 static const struct GNUNET_CONFIGURATION_Handle *cfg;
51 * Name of the file with the hosts to run the test over
53 static char *hosts_file;
56 * Context for a peer which actively does DHT PUT/GET
61 * Context to hold data of peer
65 * The testbed peer this context belongs to
67 struct GNUNET_TESTBED_Peer *peer;
70 * Testbed operation acting on this peer
72 struct GNUNET_TESTBED_Operation *op;
75 * Active context; NULL if this peer is not an active peer
77 struct ActiveContext *ac;
82 * Context for a peer which actively does DHT PUT/GET
84 struct ActiveContext {
86 * The linked peer context
91 * Handler to the DHT service
93 struct GNUNET_DHT_Handle *dht;
96 * The active context used for our DHT GET
98 struct ActiveContext *get_ac;
103 struct GNUNET_DHT_PutHandle *dht_put;
108 struct GNUNET_DHT_GetHandle *dht_get;
111 * The hashes of the values stored via this activity context.
112 * Array of length #num_puts_per_peer.
114 struct GNUNET_HashCode *hash;
119 struct GNUNET_SCHEDULER_Task *delay_task;
122 * How many puts should we still issue?
124 unsigned int put_count;
127 * The number of peers currently doing GET on our data
134 * An array of contexts. The size of this array should be equal to @a num_peers
136 static struct Context *a_ctx;
139 * Array of active peers
141 static struct ActiveContext *a_ac;
144 * The delay between rounds for collecting statistics
146 static struct GNUNET_TIME_Relative delay_stats;
149 * The delay to start puts.
151 static struct GNUNET_TIME_Relative delay_put;
154 * The delay to start puts.
156 static struct GNUNET_TIME_Relative delay_get;
159 * The timeout for GET and PUT
161 static struct GNUNET_TIME_Relative timeout;
166 static unsigned int num_peers;
169 * Number of active peers
171 static unsigned int n_active;
174 * Number of DHT service connections we currently have
176 static unsigned int n_dht;
179 * Number of DHT PUTs made
181 static unsigned long long n_puts;
184 * Number of DHT PUTs to be made per peer.
186 static unsigned int num_puts_per_peer = 1;
189 * Number of DHT PUTs succeeded
191 static unsigned long long n_puts_ok;
194 * Number of DHT GETs made
196 static unsigned int n_gets;
199 * Number of DHT GETs succeeded
201 static unsigned int n_gets_ok;
204 * Number of DHT GETs succeeded
206 static unsigned int n_gets_fail;
211 static unsigned int replication;
214 * Testbed Operation (to get stats).
216 static struct GNUNET_TESTBED_Operation *bandwidth_stats_op;
219 * Testbed peer handles.
221 static struct GNUNET_TESTBED_Peer **testbed_handles;
224 * Total number of messages sent by peer.
226 static uint64_t outgoing_bandwidth;
229 * Total number of messages received by peer.
231 static uint64_t incoming_bandwidth;
234 * Average number of hops taken to do put.
236 static double average_put_path_length;
239 * Average number of hops taken to do get.
241 static double average_get_path_length;
244 * Total put path length across all peers.
246 static unsigned int total_put_path_length;
249 * Total get path length across all peers.
251 static unsigned int total_get_path_length;
254 * Counter to keep track of peers added to peer_context lists.
256 static int peers_started = 0;
259 * Should we do a PUT (mode = 0) or GET (mode = 1);
269 * Are we shutting down
271 static int in_shutdown = 0;
275 * Connect to DHT services of active peers
278 start_profiling(void);
282 * Shutdown task. Cleanup all resources and operations.
287 do_shutdown(void *cls)
289 struct ActiveContext *ac;
291 in_shutdown = GNUNET_YES;
294 for (unsigned int cnt = 0; cnt < num_peers; cnt++)
296 /* Cleanup active context if this peer is an active peer */
300 if (NULL != ac->delay_task)
301 GNUNET_SCHEDULER_cancel(ac->delay_task);
302 if (NULL != ac->hash)
304 if (NULL != ac->dht_put)
305 GNUNET_DHT_put_cancel(ac->dht_put);
306 if (NULL != ac->dht_get)
307 GNUNET_DHT_get_stop(ac->dht_get);
309 /* Cleanup testbed operation handle at the last as this operation may
310 contain service connection to DHT */
311 if (NULL != a_ctx[cnt].op)
312 GNUNET_TESTBED_operation_done(a_ctx[cnt].op);
317 //FIXME: Should we collect stats only for put/get not for other messages.
318 if (NULL != bandwidth_stats_op)
320 GNUNET_TESTBED_operation_done(bandwidth_stats_op);
321 bandwidth_stats_op = NULL;
323 GNUNET_free_non_null(a_ac);
328 * Stats callback. Finish the stats testbed operation and when all stats have
329 * been iterated, shutdown the test.
332 * @param op the operation that has been finished
333 * @param emsg error message in case the operation has failed; will be NULL if
334 * operation has executed successfully.
337 bandwidth_stats_cont(void *cls,
338 struct GNUNET_TESTBED_Operation *op,
341 MESSAGE("# Outgoing (core) bandwidth: %llu bytes\n",
342 (unsigned long long)outgoing_bandwidth);
343 MESSAGE("# Incoming (core) bandwidth: %llu bytes\n",
344 (unsigned long long)incoming_bandwidth);
346 "Benchmark done. Collect data via gnunet-statistics, then press ENTER to exit.\n");
348 GNUNET_SCHEDULER_shutdown();
353 * Process statistic values.
356 * @param peer the peer the statistic belong to
357 * @param subsystem name of subsystem that created the statistic
358 * @param name the name of the datum
359 * @param value the current value
360 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
361 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
364 bandwidth_stats_iterator(void *cls,
365 const struct GNUNET_TESTBED_Peer *peer,
366 const char *subsystem,
371 static const char *s_sent = "# bytes encrypted";
372 static const char *s_recv = "# bytes decrypted";
374 if (0 == strncmp(s_sent, name, strlen(s_sent)))
375 outgoing_bandwidth = outgoing_bandwidth + value;
376 else if (0 == strncmp(s_recv, name, strlen(s_recv)))
377 incoming_bandwidth = incoming_bandwidth + value;
385 MESSAGE("# PUTS started: %llu\n",
387 MESSAGE("# PUTS succeeded: %llu\n",
389 MESSAGE("# GETS made: %u\n",
391 MESSAGE("# GETS succeeded: %u\n",
393 MESSAGE("# GETS failed: %u\n",
395 MESSAGE("# average_put_path_length: %f\n",
396 average_put_path_length);
397 MESSAGE("# average_get_path_length: %f\n",
398 average_get_path_length);
400 if (NULL == testbed_handles)
402 MESSAGE("No peers found\n");
406 bandwidth_stats_op = GNUNET_TESTBED_get_statistics(n_active,
410 &bandwidth_stats_iterator,
411 &bandwidth_stats_cont,
417 * Task to cancel DHT GET.
422 cancel_get(void *cls)
424 struct ActiveContext *ac = cls;
425 struct Context *ctx = ac->ctx;
427 ac->delay_task = NULL;
428 GNUNET_assert(NULL != ac->dht_get);
429 GNUNET_DHT_get_stop(ac->dht_get);
432 GNUNET_assert(NULL != ctx->op);
433 GNUNET_TESTBED_operation_done(ctx->op);
436 /* If profiling is complete, summarize */
437 if (n_active == n_gets_fail + n_gets_ok)
439 average_put_path_length = (double)total_put_path_length / (double)n_active;
440 average_get_path_length = (double)total_get_path_length / (double )n_gets_ok;
447 * Iterator called on each result obtained for a DHT
448 * operation that expects a reply
451 * @param exp when will this value expire
452 * @param key key of the result
453 * @param get_path peers on reply path (or NULL if not recorded)
454 * [0] = datastore's first neighbor, [length - 1] = local peer
455 * @param get_path_length number of entries in @a get_path
456 * @param put_path peers on the PUT path (or NULL if not recorded)
457 * [0] = origin, [length - 1] = datastore
458 * @param put_path_length number of entries in @a put_path
459 * @param type type of the result
460 * @param size number of bytes in @a data
461 * @param data pointer to the result data
465 struct GNUNET_TIME_Absolute exp,
466 const struct GNUNET_HashCode *key,
467 const struct GNUNET_PeerIdentity *get_path,
468 unsigned int get_path_length,
469 const struct GNUNET_PeerIdentity *put_path,
470 unsigned int put_path_length,
471 enum GNUNET_BLOCK_Type type,
472 size_t size, const void *data)
474 struct ActiveContext *ac = cls;
475 struct ActiveContext *get_ac = ac->get_ac;
476 struct Context *ctx = ac->ctx;
478 /* we found the data we are looking for */
479 DEBUG("We found a GET request; %u remaining\n",
480 n_gets - (n_gets_fail + n_gets_ok)); //FIXME: It always prints 1.
483 GNUNET_DHT_get_stop(ac->dht_get);
485 if (ac->delay_task != NULL)
486 GNUNET_SCHEDULER_cancel(ac->delay_task);
487 ac->delay_task = NULL;
488 GNUNET_assert(NULL != ctx->op);
489 GNUNET_TESTBED_operation_done(ctx->op);
492 total_put_path_length = total_put_path_length + (double)put_path_length;
493 total_get_path_length = total_get_path_length + (double)get_path_length;
494 DEBUG("total_put_path_length = %u,put_path \n",
495 total_put_path_length);
496 /* Summarize if profiling is complete */
497 if (n_active == n_gets_fail + n_gets_ok)
499 average_put_path_length = (double)total_put_path_length / (double)n_active;
500 average_get_path_length = (double)total_get_path_length / (double )n_gets_ok;
507 * Task to do DHT GETs
509 * @param cls the active context
512 delayed_get(void *cls)
514 struct ActiveContext *ac = cls;
515 struct ActiveContext *get_ac;
518 ac->delay_task = NULL;
522 r = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
525 if (NULL != get_ac->hash)
530 r = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
532 DEBUG("GET_REQUEST_START key %s \n",
533 GNUNET_h2s(&get_ac->hash[r]));
534 ac->dht_get = GNUNET_DHT_get_start(ac->dht,
535 GNUNET_BLOCK_TYPE_TEST,
537 1, /* replication level */
540 0, /* extended query and size */
542 ac); /* GET iterator and closure */
545 /* schedule the timeout task for GET */
546 ac->delay_task = GNUNET_SCHEDULER_add_delayed(timeout,
553 * Task to do DHT PUTs. If the "put_count" hits zero,
554 * we stop the TESTBED operation (connection to DHT)
555 * so that others PUTs have a chance.
557 * @param cls the active context
560 delayed_put(void *cls);
564 * Conclude individual PUT operation, schedule the
567 * @param cls the active context
572 struct ActiveContext *ac = cls;
576 ac->delay_task = GNUNET_SCHEDULER_add_now(&delayed_put,
582 * Task to do DHT PUTs. If the "put_count" hits zero,
583 * we stop the TESTBED operation (connection to DHT)
584 * so that others PUTs have a chance.
586 * @param cls the active context
589 delayed_put(void *cls)
591 struct ActiveContext *ac = cls;
595 ac->delay_task = NULL;
596 if (0 == ac->put_count)
598 struct Context *ctx = ac->ctx;
599 struct GNUNET_TESTBED_Operation *op;
601 GNUNET_assert(NULL != ctx);
604 GNUNET_TESTBED_operation_done(op);
609 /* Generate and DHT PUT some random data */
610 block_size = 16; /* minimum */
611 /* make random payload, reserve 512 - 16 bytes for DHT headers */
612 block_size += GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
613 GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE - 512);
614 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK,
618 GNUNET_CRYPTO_hash(block,
620 &ac->hash[ac->put_count]);
621 DEBUG("PUT_REQUEST_START key %s\n",
622 GNUNET_h2s(&ac->hash[ac->put_count]));
623 ac->dht_put = GNUNET_DHT_put(ac->dht,
624 &ac->hash[ac->put_count],
626 GNUNET_DHT_RO_RECORD_ROUTE,
627 GNUNET_BLOCK_TYPE_TEST,
630 GNUNET_TIME_UNIT_FOREVER_ABS, /* expiration time */
632 ac); /* continuation and its closure */
638 * Connection to DHT has been established. Call the delay task.
640 * @param cls the active context
641 * @param op the operation that has been finished
642 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
643 * @param emsg error message in case the operation has failed; will be NULL if
644 * operation has executed successfully.
647 dht_connected(void *cls,
648 struct GNUNET_TESTBED_Operation *op,
652 struct ActiveContext *ac = cls;
653 struct Context *ctx = ac->ctx;
655 GNUNET_assert(NULL != ctx); //FIXME: Fails
656 GNUNET_assert(NULL != ctx->op);
657 GNUNET_assert(ctx->op == op);
658 ac->dht = (struct GNUNET_DHT_Handle *)ca_result;
661 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
662 "Connection to DHT service failed: %s\n",
664 GNUNET_TESTBED_operation_done(ctx->op); /* Calls dht_disconnect() */
672 struct GNUNET_TIME_Relative peer_delay_put;
674 peer_delay_put.rel_value_us =
675 GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
676 delay_put.rel_value_us);
677 ac->put_count = num_puts_per_peer;
678 ac->hash = calloc(ac->put_count,
679 sizeof(struct GNUNET_HashCode));
680 if (NULL == ac->hash)
682 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR,
684 GNUNET_SCHEDULER_shutdown();
687 ac->delay_task = GNUNET_SCHEDULER_add_delayed(peer_delay_put,
695 struct GNUNET_TIME_Relative peer_delay_get;
697 peer_delay_get.rel_value_us =
698 delay_get.rel_value_us +
699 GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
700 delay_get.rel_value_us);
701 ac->delay_task = GNUNET_SCHEDULER_add_delayed(peer_delay_get,
711 * Connect to DHT service and return the DHT client handler
713 * @param cls the active context
714 * @param cfg configuration of the peer to connect to; will be available until
715 * GNUNET_TESTBED_operation_done() is called on the operation returned
716 * from GNUNET_TESTBED_service_connect()
717 * @return service handle to return in 'op_result', NULL on error
720 dht_connect(void *cls,
721 const struct GNUNET_CONFIGURATION_Handle *cfg)
724 return GNUNET_DHT_connect(cfg,
730 * Adapter function called to destroy a connection to
733 * @param cls the active context
734 * @param op_result service handle returned from the connect adapter
737 dht_disconnect(void *cls,
740 struct ActiveContext *ac = cls;
742 GNUNET_assert(NULL != ac->dht);
743 GNUNET_assert(ac->dht == op_result);
744 GNUNET_DHT_disconnect(ac->dht);
749 if (GNUNET_YES == in_shutdown)
754 if (n_puts_ok != ((unsigned long long)n_active) * num_puts_per_peer)
756 /* Start GETs if all PUTs have been made */
762 if ((n_gets_ok + n_gets_fail) != n_active)
770 * Connect to DHT services of active peers
777 DEBUG("GNUNET_TESTBED_service_connect\n");
778 GNUNET_break(GNUNET_YES != in_shutdown);
779 for (unsigned int i = 0; i < n_active; i++)
781 struct ActiveContext *ac = &a_ac[i];
782 GNUNET_assert(NULL != (ctx = ac->ctx));
783 GNUNET_assert(NULL == ctx->op);
784 ctx->op = GNUNET_TESTBED_service_connect(ctx,
796 * Callback called when DHT service on the peer is started
798 * @param cls the context
799 * @param op the operation that has been finished
800 * @param emsg error message in case the operation has failed; will be NULL if
801 * operation has executed successfully.
804 service_started(void *cls,
805 struct GNUNET_TESTBED_Operation *op,
808 struct Context *ctx = cls;
810 GNUNET_assert(NULL != ctx);
811 GNUNET_assert(NULL != ctx->op);
812 GNUNET_TESTBED_operation_done(ctx->op);
815 DEBUG("Peers Started = %d; num_peers = %d \n",
818 if (peers_started == num_peers)
824 * Signature of a main function for a testcase.
827 * @param h the run handle
828 * @param num_peers number of peers in 'peers'
829 * @param peers handle to peers run in the testbed
830 * @param links_succeeded the number of overlay link connection attempts that
832 * @param links_failed the number of overlay link
836 struct GNUNET_TESTBED_RunHandle *h,
837 unsigned int num_peers,
838 struct GNUNET_TESTBED_Peer **peers,
839 unsigned int links_succeeded,
840 unsigned int links_failed)
844 testbed_handles = peers;
850 MESSAGE("%u peers started, %u/%u links up\n",
853 links_succeeded + links_failed);
854 a_ctx = GNUNET_new_array(num_peers,
856 /* select the peers which actively participate in profiling */
857 n_active = num_peers * put_probability / 100;
860 GNUNET_SCHEDULER_shutdown();
866 a_ac = GNUNET_new_array(n_active,
867 struct ActiveContext);
869 for (unsigned int cnt = 0; cnt < num_peers && ac_cnt < n_active; cnt++)
871 if (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
872 100) >= put_probability)
875 a_ctx[cnt].ac = &a_ac[ac_cnt];
876 a_ac[ac_cnt].ctx = &a_ctx[cnt];
881 /* start DHT service on all peers */
882 for (unsigned int cnt = 0; cnt < num_peers; cnt++)
884 a_ctx[cnt].peer = peers[cnt];
885 a_ctx[cnt].op = GNUNET_TESTBED_peer_manage_service(&a_ctx[cnt],
896 * Main function that will be run by the scheduler.
899 * @param args remaining command-line arguments
900 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
901 * @param config configuration
907 const struct GNUNET_CONFIGURATION_Handle *config)
913 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
914 _("Exiting as the number of peers is %u\n"),
920 GNUNET_TESTBED_run(hosts_file,
928 GNUNET_SCHEDULER_add_shutdown(&do_shutdown,
936 * @return 0 on success
943 struct GNUNET_GETOPT_CommandLineOption options[] = {
944 GNUNET_GETOPT_option_uint('n',
947 gettext_noop("number of peers to start"),
949 GNUNET_GETOPT_option_uint('p',
952 gettext_noop("number of PUTs to perform per peer"),
954 GNUNET_GETOPT_option_string('H',
957 gettext_noop("name of the file with the login information for the testbed"),
959 GNUNET_GETOPT_option_relative_time('D',
962 gettext_noop("delay between rounds for collecting statistics (default: 30 sec)"),
964 GNUNET_GETOPT_option_relative_time('P',
967 gettext_noop("delay to start doing PUTs (default: 1 sec)"),
969 GNUNET_GETOPT_option_relative_time('G',
972 gettext_noop("delay to start doing GETs (default: 5 min)"),
974 GNUNET_GETOPT_option_uint('r',
977 gettext_noop("replication degree for DHT PUTs"),
979 GNUNET_GETOPT_option_uint('R',
982 gettext_noop("chance that a peer is selected at random for PUTs"),
984 GNUNET_GETOPT_option_relative_time('t',
987 gettext_noop("timeout for DHT PUT and GET requests (default: 1 min)"),
989 GNUNET_GETOPT_OPTION_END
993 GNUNET_STRINGS_get_utf8_args(argc, argv,
996 /* set default delays */
997 delay_stats = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10);
998 delay_put = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10);
999 delay_get = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10);
1000 timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10);
1001 replication = 1; /* default replication */
1004 GNUNET_PROGRAM_run(argc,
1006 "gnunet-dht-profiler",
1007 gettext_noop("Measure quality and performance of the DHT service."),