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
66 * The testbed peer this context belongs to
68 struct GNUNET_TESTBED_Peer *peer;
71 * Testbed operation acting on this peer
73 struct GNUNET_TESTBED_Operation *op;
76 * Active context; NULL if this peer is not an active peer
78 struct ActiveContext *ac;
84 * Context for a peer which actively does DHT PUT/GET
89 * The linked peer context
94 * Handler to the DHT service
96 struct GNUNET_DHT_Handle *dht;
99 * The active context used for our DHT GET
101 struct ActiveContext *get_ac;
106 struct GNUNET_DHT_PutHandle *dht_put;
111 struct GNUNET_DHT_GetHandle *dht_get;
114 * The hashes of the values stored via this activity context.
115 * Array of length #num_puts_per_peer.
117 struct GNUNET_HashCode *hash;
122 struct GNUNET_SCHEDULER_Task *delay_task;
125 * How many puts should we still issue?
127 unsigned int put_count;
130 * The number of peers currently doing GET on our data
137 * An array of contexts. The size of this array should be equal to @a num_peers
139 static struct Context *a_ctx;
142 * Array of active peers
144 static struct ActiveContext *a_ac;
147 * The delay between rounds for collecting statistics
149 static struct GNUNET_TIME_Relative delay_stats;
152 * The delay to start puts.
154 static struct GNUNET_TIME_Relative delay_put;
157 * The delay to start puts.
159 static struct GNUNET_TIME_Relative delay_get;
162 * The timeout for GET and PUT
164 static struct GNUNET_TIME_Relative timeout;
169 static unsigned int num_peers;
172 * Number of active peers
174 static unsigned int n_active;
177 * Number of DHT service connections we currently have
179 static unsigned int n_dht;
182 * Number of DHT PUTs made
184 static unsigned long long n_puts;
187 * Number of DHT PUTs to be made per peer.
189 static unsigned int num_puts_per_peer = 1;
192 * Number of DHT PUTs succeeded
194 static unsigned long long n_puts_ok;
197 * Number of DHT GETs made
199 static unsigned int n_gets;
202 * Number of DHT GETs succeeded
204 static unsigned int n_gets_ok;
207 * Number of DHT GETs succeeded
209 static unsigned int n_gets_fail;
214 static unsigned int replication;
217 * Testbed Operation (to get stats).
219 static struct GNUNET_TESTBED_Operation *bandwidth_stats_op;
222 * Testbed peer handles.
224 static struct GNUNET_TESTBED_Peer **testbed_handles;
227 * Total number of messages sent by peer.
229 static uint64_t outgoing_bandwidth;
232 * Total number of messages received by peer.
234 static uint64_t incoming_bandwidth;
237 * Average number of hops taken to do put.
239 static double average_put_path_length;
242 * Average number of hops taken to do get.
244 static double average_get_path_length;
247 * Total put path length across all peers.
249 static unsigned int total_put_path_length;
252 * Total get path length across all peers.
254 static unsigned int total_get_path_length;
257 * Counter to keep track of peers added to peer_context lists.
259 static int peers_started = 0;
262 * Should we do a PUT (mode = 0) or GET (mode = 1);
273 * Are we shutting down
275 static int in_shutdown = 0;
279 * Connect to DHT services of active peers
282 start_profiling (void);
286 * Shutdown task. Cleanup all resources and operations.
291 do_shutdown (void *cls)
293 struct ActiveContext *ac;
295 in_shutdown = GNUNET_YES;
298 for (unsigned int cnt=0; cnt < num_peers; cnt++)
300 /* Cleanup active context if this peer is an active peer */
304 if (NULL != ac->delay_task)
305 GNUNET_SCHEDULER_cancel (ac->delay_task);
306 if (NULL != ac->hash)
308 if (NULL != ac->dht_put)
309 GNUNET_DHT_put_cancel (ac->dht_put);
310 if (NULL != ac->dht_get)
311 GNUNET_DHT_get_stop (ac->dht_get);
313 /* Cleanup testbed operation handle at the last as this operation may
314 contain service connection to DHT */
315 if (NULL != a_ctx[cnt].op)
316 GNUNET_TESTBED_operation_done (a_ctx[cnt].op);
321 //FIXME: Should we collect stats only for put/get not for other messages.
322 if (NULL != bandwidth_stats_op)
324 GNUNET_TESTBED_operation_done (bandwidth_stats_op);
325 bandwidth_stats_op = NULL;
327 GNUNET_free_non_null (a_ac);
332 * Stats callback. Finish the stats testbed operation and when all stats have
333 * been iterated, shutdown the test.
336 * @param op the operation that has been finished
337 * @param emsg error message in case the operation has failed; will be NULL if
338 * operation has executed successfully.
341 bandwidth_stats_cont (void *cls,
342 struct GNUNET_TESTBED_Operation *op,
345 MESSAGE ("# Outgoing (core) bandwidth: %llu bytes\n",
346 (unsigned long long) outgoing_bandwidth);
347 MESSAGE ("# Incoming (core) bandwidth: %llu bytes\n",
348 (unsigned long long) incoming_bandwidth);
350 "Benchmark done. Collect data via gnunet-statistics, then press ENTER to exit.\n");
352 GNUNET_SCHEDULER_shutdown ();
357 * Process statistic values.
360 * @param peer the peer the statistic belong to
361 * @param subsystem name of subsystem that created the statistic
362 * @param name the name of the datum
363 * @param value the current value
364 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
365 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
368 bandwidth_stats_iterator (void *cls,
369 const struct GNUNET_TESTBED_Peer *peer,
370 const char *subsystem,
375 static const char *s_sent = "# bytes encrypted";
376 static const char *s_recv = "# bytes decrypted";
378 if (0 == strncmp (s_sent, name, strlen (s_sent)))
379 outgoing_bandwidth = outgoing_bandwidth + value;
380 else if (0 == strncmp(s_recv, name, strlen (s_recv)))
381 incoming_bandwidth = incoming_bandwidth + value;
389 MESSAGE ("# PUTS started: %llu\n",
391 MESSAGE ("# PUTS succeeded: %llu\n",
393 MESSAGE ("# GETS made: %u\n",
395 MESSAGE ("# GETS succeeded: %u\n",
397 MESSAGE ("# GETS failed: %u\n",
399 MESSAGE ("# average_put_path_length: %f\n",
400 average_put_path_length);
401 MESSAGE ("# average_get_path_length: %f\n",
402 average_get_path_length);
404 if (NULL == testbed_handles)
406 MESSAGE ("No peers found\n");
410 bandwidth_stats_op = GNUNET_TESTBED_get_statistics (n_active,
414 &bandwidth_stats_iterator,
415 &bandwidth_stats_cont,
421 * Task to cancel DHT GET.
426 cancel_get (void *cls)
428 struct ActiveContext *ac = cls;
429 struct Context *ctx = ac->ctx;
431 ac->delay_task = NULL;
432 GNUNET_assert (NULL != ac->dht_get);
433 GNUNET_DHT_get_stop (ac->dht_get);
436 GNUNET_assert (NULL != ctx->op);
437 GNUNET_TESTBED_operation_done (ctx->op);
440 /* If profiling is complete, summarize */
441 if (n_active == n_gets_fail + n_gets_ok)
443 average_put_path_length = (double)total_put_path_length/(double)n_active;
444 average_get_path_length = (double)total_get_path_length/(double )n_gets_ok;
451 * Iterator called on each result obtained for a DHT
452 * operation that expects a reply
455 * @param exp when will this value expire
456 * @param key key of the result
457 * @param get_path peers on reply path (or NULL if not recorded)
458 * [0] = datastore's first neighbor, [length - 1] = local peer
459 * @param get_path_length number of entries in @a get_path
460 * @param put_path peers on the PUT path (or NULL if not recorded)
461 * [0] = origin, [length - 1] = datastore
462 * @param put_path_length number of entries in @a put_path
463 * @param type type of the result
464 * @param size number of bytes in @a data
465 * @param data pointer to the result data
469 struct GNUNET_TIME_Absolute exp,
470 const struct GNUNET_HashCode *key,
471 const struct GNUNET_PeerIdentity *get_path,
472 unsigned int get_path_length,
473 const struct GNUNET_PeerIdentity *put_path,
474 unsigned int put_path_length,
475 enum GNUNET_BLOCK_Type type,
476 size_t size, const void *data)
478 struct ActiveContext *ac = cls;
479 struct ActiveContext *get_ac = ac->get_ac;
480 struct Context *ctx = ac->ctx;
482 /* we found the data we are looking for */
483 DEBUG ("We found a GET request; %u remaining\n",
484 n_gets - (n_gets_fail + n_gets_ok)); //FIXME: It always prints 1.
487 GNUNET_DHT_get_stop (ac->dht_get);
489 if (ac->delay_task != NULL)
490 GNUNET_SCHEDULER_cancel (ac->delay_task);
491 ac->delay_task = NULL;
492 GNUNET_assert (NULL != ctx->op);
493 GNUNET_TESTBED_operation_done (ctx->op);
496 total_put_path_length = total_put_path_length + (double)put_path_length;
497 total_get_path_length = total_get_path_length + (double)get_path_length;
498 DEBUG ("total_put_path_length = %u,put_path \n",
499 total_put_path_length);
500 /* Summarize if profiling is complete */
501 if (n_active == n_gets_fail + n_gets_ok)
503 average_put_path_length = (double)total_put_path_length/(double)n_active;
504 average_get_path_length = (double)total_get_path_length/(double )n_gets_ok;
511 * Task to do DHT GETs
513 * @param cls the active context
516 delayed_get (void *cls)
518 struct ActiveContext *ac = cls;
519 struct ActiveContext *get_ac;
522 ac->delay_task = NULL;
526 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
529 if (NULL != get_ac->hash)
534 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
536 DEBUG ("GET_REQUEST_START key %s \n",
537 GNUNET_h2s(&get_ac->hash[r]));
538 ac->dht_get = GNUNET_DHT_get_start (ac->dht,
539 GNUNET_BLOCK_TYPE_TEST,
541 1, /* replication level */
544 0, /* extended query and size */
546 ac); /* GET iterator and closure */
549 /* schedule the timeout task for GET */
550 ac->delay_task = GNUNET_SCHEDULER_add_delayed (timeout,
557 * Task to do DHT PUTs. If the "put_count" hits zero,
558 * we stop the TESTBED operation (connection to DHT)
559 * so that others PUTs have a chance.
561 * @param cls the active context
564 delayed_put (void *cls);
568 * Conclude individual PUT operation, schedule the
571 * @param cls the active context
576 struct ActiveContext *ac = cls;
580 ac->delay_task = GNUNET_SCHEDULER_add_now (&delayed_put,
586 * Task to do DHT PUTs. If the "put_count" hits zero,
587 * we stop the TESTBED operation (connection to DHT)
588 * so that others PUTs have a chance.
590 * @param cls the active context
593 delayed_put (void *cls)
595 struct ActiveContext *ac = cls;
599 ac->delay_task = NULL;
600 if (0 == ac->put_count)
602 struct Context *ctx = ac->ctx;
603 struct GNUNET_TESTBED_Operation *op;
605 GNUNET_assert (NULL != ctx);
608 GNUNET_TESTBED_operation_done (op);
613 /* Generate and DHT PUT some random data */
614 block_size = 16; /* minimum */
615 /* make random payload, reserve 512 - 16 bytes for DHT headers */
616 block_size += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
617 GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE - 512);
618 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
622 GNUNET_CRYPTO_hash (block,
624 &ac->hash[ac->put_count]);
625 DEBUG ("PUT_REQUEST_START key %s\n",
626 GNUNET_h2s (&ac->hash[ac->put_count]));
627 ac->dht_put = GNUNET_DHT_put (ac->dht,
628 &ac->hash[ac->put_count],
630 GNUNET_DHT_RO_RECORD_ROUTE,
631 GNUNET_BLOCK_TYPE_TEST,
634 GNUNET_TIME_UNIT_FOREVER_ABS, /* expiration time */
636 ac); /* continuation and its closure */
642 * Connection to DHT has been established. Call the delay task.
644 * @param cls the active context
645 * @param op the operation that has been finished
646 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
647 * @param emsg error message in case the operation has failed; will be NULL if
648 * operation has executed successfully.
651 dht_connected (void *cls,
652 struct GNUNET_TESTBED_Operation *op,
656 struct ActiveContext *ac = cls;
657 struct Context *ctx = ac->ctx;
659 GNUNET_assert (NULL != ctx); //FIXME: Fails
660 GNUNET_assert (NULL != ctx->op);
661 GNUNET_assert (ctx->op == op);
662 ac->dht = (struct GNUNET_DHT_Handle *) ca_result;
665 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
666 "Connection to DHT service failed: %s\n",
668 GNUNET_TESTBED_operation_done (ctx->op); /* Calls dht_disconnect() */
676 struct GNUNET_TIME_Relative peer_delay_put;
678 peer_delay_put.rel_value_us =
679 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
680 delay_put.rel_value_us);
681 ac->put_count = num_puts_per_peer;
682 ac->hash = calloc (ac->put_count,
683 sizeof (struct GNUNET_HashCode));
684 if (NULL == ac->hash)
686 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
688 GNUNET_SCHEDULER_shutdown ();
691 ac->delay_task = GNUNET_SCHEDULER_add_delayed (peer_delay_put,
698 struct GNUNET_TIME_Relative peer_delay_get;
700 peer_delay_get.rel_value_us =
701 delay_get.rel_value_us +
702 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
703 delay_get.rel_value_us);
704 ac->delay_task = GNUNET_SCHEDULER_add_delayed (peer_delay_get,
714 * Connect to DHT service and return the DHT client handler
716 * @param cls the active context
717 * @param cfg configuration of the peer to connect to; will be available until
718 * GNUNET_TESTBED_operation_done() is called on the operation returned
719 * from GNUNET_TESTBED_service_connect()
720 * @return service handle to return in 'op_result', NULL on error
723 dht_connect (void *cls,
724 const struct GNUNET_CONFIGURATION_Handle *cfg)
727 return GNUNET_DHT_connect (cfg,
733 * Adapter function called to destroy a connection to
736 * @param cls the active context
737 * @param op_result service handle returned from the connect adapter
740 dht_disconnect (void *cls,
743 struct ActiveContext *ac = cls;
745 GNUNET_assert (NULL != ac->dht);
746 GNUNET_assert (ac->dht == op_result);
747 GNUNET_DHT_disconnect (ac->dht);
752 if (GNUNET_YES == in_shutdown)
757 if (n_puts_ok != ((unsigned long long) n_active) * num_puts_per_peer)
759 /* Start GETs if all PUTs have been made */
764 if ((n_gets_ok + n_gets_fail) != n_active)
772 * Connect to DHT services of active peers
779 DEBUG ("GNUNET_TESTBED_service_connect\n");
780 GNUNET_break (GNUNET_YES != in_shutdown);
781 for (unsigned int i = 0; i < n_active; i++)
783 struct ActiveContext *ac = &a_ac[i];
784 GNUNET_assert (NULL != (ctx = ac->ctx));
785 GNUNET_assert (NULL == ctx->op);
786 ctx->op = GNUNET_TESTBED_service_connect (ctx,
798 * Callback called when DHT service on the peer is started
800 * @param cls the context
801 * @param op the operation that has been finished
802 * @param emsg error message in case the operation has failed; will be NULL if
803 * operation has executed successfully.
806 service_started (void *cls,
807 struct GNUNET_TESTBED_Operation *op,
810 struct Context *ctx = cls;
812 GNUNET_assert (NULL != ctx);
813 GNUNET_assert (NULL != ctx->op);
814 GNUNET_TESTBED_operation_done (ctx->op);
817 DEBUG ("Peers Started = %d; num_peers = %d \n",
820 if (peers_started == num_peers)
826 * Signature of a main function for a testcase.
829 * @param h the run handle
830 * @param num_peers number of peers in 'peers'
831 * @param peers handle to peers run in the testbed
832 * @param links_succeeded the number of overlay link connection attempts that
834 * @param links_failed the number of overlay link
838 struct GNUNET_TESTBED_RunHandle *h,
839 unsigned int num_peers,
840 struct GNUNET_TESTBED_Peer **peers,
841 unsigned int links_succeeded,
842 unsigned int links_failed)
846 testbed_handles = peers;
852 MESSAGE ("%u peers started, %u/%u links up\n",
855 links_succeeded + links_failed);
856 a_ctx = GNUNET_new_array (num_peers,
858 /* select the peers which actively participate in profiling */
859 n_active = num_peers * put_probability / 100;
862 GNUNET_SCHEDULER_shutdown ();
868 a_ac = GNUNET_new_array (n_active,
869 struct ActiveContext);
871 for (unsigned int cnt = 0; cnt < num_peers && ac_cnt < n_active; cnt++)
873 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
874 100) >= put_probability)
877 a_ctx[cnt].ac = &a_ac[ac_cnt];
878 a_ac[ac_cnt].ctx = &a_ctx[cnt];
883 /* start DHT service on all peers */
884 for (unsigned int cnt = 0; cnt < num_peers; cnt++)
886 a_ctx[cnt].peer = peers[cnt];
887 a_ctx[cnt].op = GNUNET_TESTBED_peer_manage_service (&a_ctx[cnt],
898 * Main function that will be run by the scheduler.
901 * @param args remaining command-line arguments
902 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
903 * @param config configuration
909 const struct GNUNET_CONFIGURATION_Handle *config)
915 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
916 _("Exiting as the number of peers is %u\n"),
922 GNUNET_TESTBED_run (hosts_file,
930 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
938 * @return 0 on success
945 struct GNUNET_GETOPT_CommandLineOption options[] = {
946 GNUNET_GETOPT_option_uint ('n',
949 gettext_noop ("number of peers to start"),
951 GNUNET_GETOPT_option_uint ('p',
954 gettext_noop ("number of PUTs to perform per peer"),
956 GNUNET_GETOPT_option_string ('H',
959 gettext_noop ("name of the file with the login information for the testbed"),
961 GNUNET_GETOPT_option_relative_time ('D',
964 gettext_noop ("delay between rounds for collecting statistics (default: 30 sec)"),
966 GNUNET_GETOPT_option_relative_time ('P',
969 gettext_noop ("delay to start doing PUTs (default: 1 sec)"),
971 GNUNET_GETOPT_option_relative_time ('G',
974 gettext_noop ("delay to start doing GETs (default: 5 min)"),
976 GNUNET_GETOPT_option_uint ('r',
979 gettext_noop ("replication degree for DHT PUTs"),
981 GNUNET_GETOPT_option_uint ('R',
984 gettext_noop ("chance that a peer is selected at random for PUTs"),
986 GNUNET_GETOPT_option_relative_time ('t',
989 gettext_noop ("timeout for DHT PUT and GET requests (default: 1 min)"),
991 GNUNET_GETOPT_OPTION_END
995 GNUNET_STRINGS_get_utf8_args (argc, argv,
998 /* set default delays */
999 delay_stats = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1000 delay_put = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1001 delay_get = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1002 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1003 replication = 1; /* default replication */
1006 GNUNET_PROGRAM_run (argc,
1008 "gnunet-dht-profiler",
1009 gettext_noop ("Measure quality and performance of the DHT service."),