2 This file is part of GNUnet.
3 Copyright (C) 2014 GNUnet e.V.
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.
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.
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.
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"
33 GNUNET_log (GNUNET_ERROR_TYPE_INFO, __VA_ARGS__)
36 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
39 * Number of peers which should perform a PUT out of 100 peers
41 #define PUT_PROBABILITY 50
45 * Number of peers which should act as malicious peers
47 #define MALICIOUS_PROBABILITY 20
50 * Context for a peer which should act maliciously.
52 struct MaliciousContext;
59 static struct GNUNET_CONFIGURATION_Handle *cfg;
62 * Name of the file with the hosts to run the test over
64 static char *hosts_file;
67 * Context for a peer which actively does DHT PUT/GET
72 * Context to hold data of peer
77 * The testbed peer this context belongs to
79 struct GNUNET_TESTBED_Peer *peer;
82 * Testbed operation acting on this peer
84 struct GNUNET_TESTBED_Operation *op;
87 * Active context; NULL if this peer is not an active peer
89 struct ActiveContext *ac;
93 * Malicious context; NULL if this peer is NOT malicious.
95 struct MaliciousContext *mc;
102 * Context for a peer which should act maliciously.
104 struct MaliciousContext
107 * The linked peer context
112 * Handler to the DHT service
114 struct GNUNET_DHT_Handle *dht;
117 * Handler to malicious api
119 struct GNUNET_DHT_ActMaliciousHandle *dht_malicious;
123 * List of all the malicious peers contexts.
125 struct Context **malicious_peer_contexts = NULL;
128 * Context for a peer which should act maliciously.
130 struct Malicious_Context
133 * The linked peer context
138 * Handler to the DHT service
140 struct GNUNET_DHT_Handle *dht;
144 * Array of malicious peers.
146 static struct MaliciousContext *a_mc;
149 * Number or malicious peers.
151 static unsigned int n_malicious;
156 * Context for a peer which actively does DHT PUT/GET
161 * The linked peer context
166 * Handler to the DHT service
168 struct GNUNET_DHT_Handle *dht;
171 * The data used for do a PUT. Will be NULL if a PUT hasn't been performed yet
176 * The active context used for our DHT GET
178 struct ActiveContext *get_ac;
183 struct GNUNET_DHT_PutHandle *dht_put;
188 struct GNUNET_DHT_GetHandle *dht_get;
191 * The hash of the @e put_data
193 struct GNUNET_HashCode hash;
198 struct GNUNET_SCHEDULER_Task * delay_task;
201 * The size of the @e put_data
203 uint16_t put_data_size;
206 * The number of peers currently doing GET on our data
213 * An array of contexts. The size of this array should be equal to @a num_peers
215 static struct Context *a_ctx;
218 * Array of active peers
220 static struct ActiveContext *a_ac;
223 * The delay between rounds for collecting statistics
225 static struct GNUNET_TIME_Relative delay_stats;
228 * The delay to start puts.
230 static struct GNUNET_TIME_Relative delay_put;
233 * The delay to start puts.
235 static struct GNUNET_TIME_Relative delay_get;
238 * The timeout for GET and PUT
240 static struct GNUNET_TIME_Relative timeout;
245 static unsigned int num_peers;
248 * Number of active peers
250 static unsigned int n_active;
253 * Number of DHT service connections we currently have
255 static unsigned int n_dht;
258 * Number of DHT PUTs made
260 static unsigned int n_puts;
263 * Number of DHT PUTs succeeded
265 static unsigned int n_puts_ok;
268 * Number of DHT PUTs failed
270 static unsigned int n_puts_fail;
273 * Number of DHT GETs made
275 static unsigned int n_gets;
278 * Number of DHT GETs succeeded
280 static unsigned int n_gets_ok;
283 * Number of DHT GETs succeeded
285 static unsigned int n_gets_fail;
290 static unsigned int replication;
293 * Number of times we try to find the successor circle formation
295 static unsigned int max_searches;
298 * Testbed Operation (to get stats).
300 static struct GNUNET_TESTBED_Operation *bandwidth_stats_op;
303 * To get successor stats.
305 static struct GNUNET_TESTBED_Operation *successor_stats_op;
308 * Testbed peer handles.
310 static struct GNUNET_TESTBED_Peer **testbed_handles;
313 * Total number of messages sent by peer.
315 static uint64_t outgoing_bandwidth;
318 * Total number of messages received by peer.
320 static uint64_t incoming_bandwidth;
323 * Average number of hops taken to do put.
325 static double average_put_path_length;
328 * Average number of hops taken to do get.
330 static double average_get_path_length;
333 * Total put path length across all peers.
335 static unsigned int total_put_path_length;
338 * Total get path length across all peers.
340 static unsigned int total_get_path_length;
343 * Hashmap to store pair of peer and its corresponding successor.
345 static struct GNUNET_CONTAINER_MultiHashMap *successor_peer_hashmap;
348 * Key to start the lookup on successor_peer_hashmap.
350 static struct GNUNET_HashCode *start_key;
353 * Flag used to get the start_key.
358 * Task to collect peer and its current successor statistics.
360 static struct GNUNET_SCHEDULER_Task * successor_stats_task;
363 * Closure for successor_stats_task.
365 struct Collect_Stat_Context
368 * Current Peer Context.
370 struct Context *service_connect_ctx;
373 * Testbed operation acting on this peer
375 struct GNUNET_TESTBED_Operation *op;
379 * List of all the peers contexts.
381 struct Context **peer_contexts = NULL;
384 * Counter to keep track of peers added to peer_context lists.
386 static int peers_started = 0;
389 * Should we do a PUT (mode = 0) or GET (mode = 1);
400 * Are we shutting down
402 static int in_shutdown = 0;
405 * Total number of times to check if circle is formed or not.
407 static unsigned int tries;
411 * Task that collects successor statistics from all the peers.
416 collect_stats (void *cls);
420 * Connect to DHT services of active peers
423 start_profiling (void);
427 * Shutdown task. Cleanup all resources and operations.
432 do_shutdown (void *cls)
434 struct ActiveContext *ac;
437 in_shutdown = GNUNET_YES;
440 for (cnt=0; cnt < num_peers; cnt++)
442 /* Cleanup active context if this peer is an active peer */
446 if (NULL != ac->delay_task)
447 GNUNET_SCHEDULER_cancel (ac->delay_task);
448 if (NULL != ac->put_data)
449 GNUNET_free (ac->put_data);
450 if (NULL != ac->dht_put)
451 GNUNET_DHT_put_cancel (ac->dht_put);
452 if (NULL != ac->dht_get)
453 GNUNET_DHT_get_stop (ac->dht_get);
455 /* Cleanup testbed operation handle at the last as this operation may
456 contain service connection to DHT */
457 if (NULL != a_ctx[cnt].op)
458 GNUNET_TESTBED_operation_done (a_ctx[cnt].op);
463 //FIXME: Should we collect stats only for put/get not for other messages.
464 if(NULL != bandwidth_stats_op)
465 GNUNET_TESTBED_operation_done (bandwidth_stats_op);
466 bandwidth_stats_op = NULL;
467 GNUNET_free_non_null (a_ac);
472 * Stats callback. Finish the stats testbed operation and when all stats have
473 * been iterated, shutdown the test.
476 * @param op the operation that has been finished
477 * @param emsg error message in case the operation has failed; will be NULL if
478 * operation has executed successfully.
481 bandwidth_stats_cont (void *cls,
482 struct GNUNET_TESTBED_Operation *op,
485 INFO ("# Outgoing bandwidth: %u\n", outgoing_bandwidth);
486 INFO ("# Incoming bandwidth: %u\n", incoming_bandwidth);
487 GNUNET_SCHEDULER_shutdown ();
492 * Process statistic values.
495 * @param peer the peer the statistic belong to
496 * @param subsystem name of subsystem that created the statistic
497 * @param name the name of the datum
498 * @param value the current value
499 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
500 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
503 bandwidth_stats_iterator (void *cls,
504 const struct GNUNET_TESTBED_Peer *peer,
505 const char *subsystem,
510 static const char *s_sent = "# Bytes transmitted to other peers";
511 static const char *s_recv = "# Bytes received from other peers";
513 if (0 == strncmp (s_sent, name, strlen (s_sent)))
514 outgoing_bandwidth = outgoing_bandwidth + value;
515 else if (0 == strncmp(s_recv, name, strlen (s_recv)))
516 incoming_bandwidth = incoming_bandwidth + value;
525 INFO ("# PUTS made: %u\n", n_puts);
526 INFO ("# PUTS succeeded: %u\n", n_puts_ok);
527 INFO ("# PUTS failed: %u\n", n_puts_fail);
528 INFO ("# GETS made: %u\n", n_gets);
529 INFO ("# GETS succeeded: %u\n", n_gets_ok);
530 INFO ("# GETS failed: %u\n", n_gets_fail);
531 INFO ("# average_put_path_length: %f\n", average_put_path_length);
532 INFO ("# average_get_path_length: %f\n", average_get_path_length);
534 if (NULL == testbed_handles)
536 INFO ("No peers found\n");
540 bandwidth_stats_op = GNUNET_TESTBED_get_statistics (n_active, testbed_handles,
542 bandwidth_stats_iterator,
543 bandwidth_stats_cont, NULL);
548 * Task to cancel DHT GET.
553 cancel_get (void *cls)
555 struct ActiveContext *ac = cls;
556 struct Context *ctx = ac->ctx;
558 ac->delay_task = NULL;
559 GNUNET_assert (NULL != ac->dht_get);
560 GNUNET_DHT_get_stop (ac->dht_get);
563 GNUNET_assert (NULL != ctx->op);
564 GNUNET_TESTBED_operation_done (ctx->op);
567 /* If profiling is complete, summarize */
568 if (n_active == n_gets_fail + n_gets_ok)
570 average_put_path_length = (double)total_put_path_length/(double)n_active;
571 average_get_path_length = (double)total_get_path_length/(double )n_gets_ok;
578 * Iterator called on each result obtained for a DHT
579 * operation that expects a reply
582 * @param exp when will this value expire
583 * @param key key of the result
584 * @param get_path peers on reply path (or NULL if not recorded)
585 * [0] = datastore's first neighbor, [length - 1] = local peer
586 * @param get_path_length number of entries in @a get_path
587 * @param put_path peers on the PUT path (or NULL if not recorded)
588 * [0] = origin, [length - 1] = datastore
589 * @param put_path_length number of entries in @a put_path
590 * @param type type of the result
591 * @param size number of bytes in @a data
592 * @param data pointer to the result data
596 struct GNUNET_TIME_Absolute exp,
597 const struct GNUNET_HashCode *key,
598 const struct GNUNET_PeerIdentity *get_path,
599 unsigned int get_path_length,
600 const struct GNUNET_PeerIdentity *put_path,
601 unsigned int put_path_length,
602 enum GNUNET_BLOCK_Type type,
603 size_t size, const void *data)
605 struct ActiveContext *ac = cls;
606 struct ActiveContext *get_ac = ac->get_ac;
607 struct Context *ctx = ac->ctx;
609 /* Check the keys of put and get match or not. */
610 GNUNET_assert (0 == memcmp (key, &get_ac->hash, sizeof (struct GNUNET_HashCode)));
611 /* we found the data we are looking for */
612 DEBUG ("We found a GET request; %u remaining\n", n_gets - (n_gets_fail + n_gets_ok)); //FIXME: It always prints 1.
615 GNUNET_DHT_get_stop (ac->dht_get);
617 if (ac->delay_task != NULL)
618 GNUNET_SCHEDULER_cancel (ac->delay_task);
619 ac->delay_task = NULL;
620 GNUNET_assert (NULL != ctx->op);
621 GNUNET_TESTBED_operation_done (ctx->op);
624 total_put_path_length = total_put_path_length + (double)put_path_length;
625 total_get_path_length = total_get_path_length + (double)get_path_length;
626 DEBUG ("total_put_path_length = %f,put_path \n",total_put_path_length);
627 /* Summarize if profiling is complete */
628 if (n_active == n_gets_fail + n_gets_ok)
630 average_put_path_length = (double)total_put_path_length/(double)n_active;
631 average_get_path_length = (double)total_get_path_length/(double )n_gets_ok;
638 * Task to do DHT GETs
640 * @param cls the active context
643 delayed_get (void *cls)
645 struct ActiveContext *ac = cls;
646 struct ActiveContext *get_ac;
649 ac->delay_task = NULL;
653 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, n_active);
655 if (NULL != get_ac->put_data)
660 DEBUG ("GET_REQUEST_START key %s \n", GNUNET_h2s((struct GNUNET_HashCode *)ac->put_data));
661 ac->dht_get = GNUNET_DHT_get_start (ac->dht,
662 GNUNET_BLOCK_TYPE_TEST,
664 1, /* replication level */
666 NULL, 0, /* extended query and size */
667 get_iter, ac); /* GET iterator and closure
671 /* schedule the timeout task for GET */
672 ac->delay_task = GNUNET_SCHEDULER_add_delayed (timeout, &cancel_get, ac);
677 * Task to teardown the dht connection. We do it as a task because calling
678 * GNUNET_DHT_disconnect() from put_continutation_callback seems illegal (the
679 * put_continuation_callback() is getting called again synchronously). Also,
680 * only free the operation when we are not shutting down; the shutdown task will
681 * clear the operation during shutdown.
683 * @param cls the context
686 teardown_dht_connection (void *cls)
688 struct Context *ctx = cls;
689 struct GNUNET_TESTBED_Operation *op;
690 const struct GNUNET_SCHEDULER_TaskContext *tc;
692 tc = GNUNET_SCHEDULER_get_task_context ();
693 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
695 GNUNET_assert (NULL != ctx);
696 GNUNET_assert (NULL != (op = ctx->op));
698 GNUNET_TESTBED_operation_done (op);
703 * Queue up a delayed task for doing DHT GET
705 * @param cls the active context
706 * @param success #GNUNET_OK if the PUT was transmitted,
707 * #GNUNET_NO on timeout,
708 * #GNUNET_SYSERR on disconnect from service
709 * after the PUT message was transmitted
710 * (so we don't know if it was received or not)
713 put_cont (void *cls, int success)
715 struct ActiveContext *ac = cls;
716 struct Context *ctx = ac->ctx;
723 GNUNET_assert (NULL != ctx);
724 (void) GNUNET_SCHEDULER_add_now (&teardown_dht_connection, ctx);
729 * Task to do DHT PUTs
731 * @param cls the active context
734 delayed_put (void *cls)
736 struct ActiveContext *ac = cls;
738 ac->delay_task = NULL;
739 /* Generate and DHT PUT some random data */
740 ac->put_data_size = 16; /* minimum */
741 ac->put_data_size += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
743 ac->put_data = GNUNET_malloc (ac->put_data_size);
744 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
745 ac->put_data, ac->put_data_size);
746 GNUNET_CRYPTO_hash (ac->put_data, ac->put_data_size, &ac->hash);
747 DEBUG ("PUT_REQUEST_START key %s \n", GNUNET_h2s((struct GNUNET_HashCode *)ac->put_data));
748 ac->dht_put = GNUNET_DHT_put (ac->dht, &ac->hash,
750 GNUNET_DHT_RO_RECORD_ROUTE,
751 GNUNET_BLOCK_TYPE_TEST,
754 GNUNET_TIME_UNIT_FOREVER_ABS, /* expiration time */
755 timeout, /* PUT timeout */
756 put_cont, ac); /* continuation and its closure */
762 * Connection to DHT has been established. Call the delay task.
764 * @param cls the active context
765 * @param op the operation that has been finished
766 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
767 * @param emsg error message in case the operation has failed; will be NULL if
768 * operation has executed successfully.
771 dht_connected (void *cls,
772 struct GNUNET_TESTBED_Operation *op,
776 struct ActiveContext *ac = cls;
777 struct Context *ctx = ac->ctx;
779 GNUNET_assert (NULL != ctx); //FIXME: Fails
780 GNUNET_assert (NULL != ctx->op);
781 GNUNET_assert (ctx->op == op);
782 ac->dht = (struct GNUNET_DHT_Handle *) ca_result;
785 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connection to DHT service failed: %s\n", emsg);
786 GNUNET_TESTBED_operation_done (ctx->op); /* Calls dht_disconnect() */
794 struct GNUNET_TIME_Relative peer_delay_put;
795 peer_delay_put.rel_value_us =
796 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
797 delay_put.rel_value_us);
798 ac->delay_task = GNUNET_SCHEDULER_add_delayed (peer_delay_put, &delayed_put, ac);
803 struct GNUNET_TIME_Relative peer_delay_get;
804 peer_delay_get.rel_value_us =
805 delay_get.rel_value_us +
806 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
807 delay_get.rel_value_us);
808 ac->delay_task = GNUNET_SCHEDULER_add_delayed (peer_delay_get, &delayed_get, ac);
816 * Connect to DHT service and return the DHT client handler
818 * @param cls the active context
819 * @param cfg configuration of the peer to connect to; will be available until
820 * GNUNET_TESTBED_operation_done() is called on the operation returned
821 * from GNUNET_TESTBED_service_connect()
822 * @return service handle to return in 'op_result', NULL on error
825 dht_connect (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
828 return GNUNET_DHT_connect (cfg, 10);
833 * Adapter function called to destroy a connection to
836 * @param cls the active context
837 * @param op_result service handle returned from the connect adapter
840 dht_disconnect (void *cls, void *op_result)
842 struct ActiveContext *ac = cls;
844 GNUNET_assert (NULL != ac->dht);
845 GNUNET_assert (ac->dht == op_result);
846 GNUNET_DHT_disconnect (ac->dht);
851 if (GNUNET_YES == in_shutdown)
856 if ((n_puts_ok + n_puts_fail) != n_active)
858 /* Start GETs if all PUTs have been made */
860 //(void) GNUNET_SCHEDULER_add_now (&call_start_profiling, NULL);
864 if ((n_gets_ok + n_gets_fail) != n_active)
871 * Connect to DHT services of active peers
879 DEBUG("GNUNET_TESTBED_service_connect \n");
880 GNUNET_break (GNUNET_YES != in_shutdown);
881 for(i = 0; i < n_active; i++)
883 struct ActiveContext *ac = &a_ac[i];
884 GNUNET_assert (NULL != (ctx = ac->ctx));
885 GNUNET_assert (NULL == ctx->op);
887 GNUNET_TESTBED_service_connect (ctx,
899 * Count of total number of malicious peers.
901 static unsigned int count_malicious;
904 * Continuation of GNUNET_DHT_act_malicious
905 * @param cls Malicious context
906 * @param success #GNUNET_OK if the ACT_MALICIOUS was transmitted,
907 * #GNUNET_NO on timeout,
908 * #GNUNET_SYSERR on disconnect from service
909 * after the ACT_MALICIOUS message was transmitted
910 * (so we don't know if it was received or not)
913 act_malicious_cont (void *cls, int success)
915 struct MaliciousContext *mc = cls;
916 struct Context *ctx = mc->ctx;
918 GNUNET_TESTBED_operation_done (ctx->op);
925 * Call malicious API for all the malicious peers.
926 * @param cls the malicious context.
927 * @param op the operation that has been finished
928 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
929 * @param emsg error message in case the operation has failed; will be NULL if
930 * operation has executed successfully.
933 dht_set_malicious(void *cls,
934 struct GNUNET_TESTBED_Operation *op,
938 struct MaliciousContext *mc = cls;
939 struct Context *ctx = mc->ctx;
941 GNUNET_assert (NULL != ctx);
942 GNUNET_assert (NULL != ctx->op);
943 GNUNET_assert (ctx->op == op);
944 mc->dht = (struct GNUNET_DHT_Handle *) ca_result;
947 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connection to DHT service failed: %s\n", emsg);
948 GNUNET_TESTBED_operation_done (ctx->op); /* Calls dht_disconnect_malicious() */
952 mc->dht_malicious = GNUNET_DHT_act_malicious(mc->dht, 1, act_malicious_cont, mc);
957 * Adapter function called to destroy a connection to
960 * @param cls the active context
961 * @param op_result service handle returned from the connect adapter
964 dht_disconnect_malicious (void *cls, void *op_result)
966 struct MaliciousContext *mc = cls;
968 GNUNET_assert (NULL != mc->dht);
969 GNUNET_assert (mc->dht == op_result);
970 GNUNET_DHT_disconnect (mc->dht);
978 if(n_malicious == count_malicious)
980 DEBUG("\n Call start_profiling()");
987 * Set the malicious variable in peer malicious context.
994 DEBUG ("Setting %u peers malicious",
996 for(i = 0; i < n_malicious; i++)
998 struct MaliciousContext *mc = &a_mc[i];
1000 GNUNET_TESTBED_service_connect (mc->ctx,
1003 &dht_set_malicious, mc,
1005 &dht_disconnect_malicious,
1014 * Start collecting relevant statistics. If ENABLE_MALICIOUS set, first
1015 * set the malicious peers. If not, then start with PUT operation on active
1021 #if ENABLE_MALICIOUS
1030 * Remove entry from successor peer hashmap.
1031 * @param cls closure
1032 * @param key current public key
1033 * @param value value in the hash map
1034 * @return #GNUNET_YES if we should continue to iterate,
1035 * #GNUNET_NO if not.
1038 hashmap_iterate_remove(void *cls,
1039 const struct GNUNET_HashCode *key,
1042 GNUNET_assert (GNUNET_YES ==
1043 GNUNET_CONTAINER_multihashmap_remove(successor_peer_hashmap, key, value));
1049 * Stats callback. Iterate over the hashmap and check if all th peers form
1050 * a virtual ring topology.
1052 * @param cls closure
1053 * @param op the operation that has been finished
1054 * @param emsg error message in case the operation has failed; will be NULL if
1055 * operation has executed successfully.
1058 successor_stats_cont (void *cls,
1059 struct GNUNET_TESTBED_Operation *op,
1062 struct GNUNET_HashCode *val;
1063 struct GNUNET_HashCode *start_val;
1064 struct GNUNET_HashCode *key;
1067 /* Don't schedule the task till we are looking for circle here. */
1068 successor_stats_task = NULL;
1069 GNUNET_TESTBED_operation_done (successor_stats_op);
1070 successor_stats_op = NULL;
1071 if (0 == max_searches)
1077 GNUNET_assert (NULL != start_key);
1078 start_val = GNUNET_CONTAINER_multihashmap_get (successor_peer_hashmap,
1080 GNUNET_assert (NULL != start_val);
1082 for (count = 0; count < num_peers; count++)
1085 val = GNUNET_CONTAINER_multihashmap_get (successor_peer_hashmap,
1089 /* Remove the entry from hashmap. This is done to take care of loop. */
1091 GNUNET_CONTAINER_multihashmap_remove (successor_peer_hashmap,
1094 DEBUG ("Failed to remove entry from hashmap\n");
1097 /* If a peer has its own identity as its successor. */
1098 if (0 == memcmp(key, val, sizeof (struct GNUNET_HashCode)))
1102 GNUNET_assert (GNUNET_SYSERR !=
1103 GNUNET_CONTAINER_multihashmap_iterate (successor_peer_hashmap,
1104 &hashmap_iterate_remove,
1107 successor_peer_hashmap = GNUNET_CONTAINER_multihashmap_create (num_peers,
1109 if ((start_val == val) && (count == num_peers))
1111 DEBUG("CIRCLE COMPLETED after %u tries", tries);
1112 if(NULL == successor_stats_task)
1120 if (max_searches == ++tries)
1122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1123 "Maximum tries %u exceeded while checking successor TOTAL TRIES %u"
1124 " circle formation. Exiting\n",
1125 max_searches,tries);
1126 if (NULL != successor_stats_task)
1128 successor_stats_task = NULL;
1130 if(NULL == successor_stats_task)
1140 successor_stats_task = GNUNET_SCHEDULER_add_delayed (delay_stats,
1141 &collect_stats, cls);
1148 * Process successor statistic values.
1150 * @param cls closure
1151 * @param peer the peer the statistic belong to
1152 * @param subsystem name of subsystem that created the statistic
1153 * @param name the name of the datum
1154 * @param value the current value
1155 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
1156 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
1159 successor_stats_iterator (void *cls,
1160 const struct GNUNET_TESTBED_Peer *peer,
1161 const char *subsystem,
1166 static const char *key_string = "XDHT";
1167 if (0 == max_searches)
1170 if (0 == strncmp (key_string, name, strlen (key_string)))
1173 char successor_str[13];
1174 char truncated_my_id_str[13];
1175 char truncated_successor_str[13];
1176 struct GNUNET_HashCode *my_id_key;
1177 struct GNUNET_HashCode *succ_key;
1179 strtok((char *)name,":");
1180 my_id_str = strtok(NULL,":");
1182 strncpy(truncated_my_id_str, my_id_str, 12);
1183 truncated_my_id_str[12] = '\0';
1184 my_id_key = GNUNET_new(struct GNUNET_HashCode);
1185 GNUNET_CRYPTO_hash (truncated_my_id_str, sizeof(truncated_my_id_str),my_id_key);
1186 GNUNET_STRINGS_data_to_string(&value, sizeof(uint64_t), successor_str, 13);
1187 strncpy(truncated_successor_str, successor_str, 12);
1188 truncated_successor_str[12] ='\0';
1190 succ_key = GNUNET_new(struct GNUNET_HashCode);
1191 GNUNET_CRYPTO_hash (truncated_successor_str, sizeof(truncated_successor_str),succ_key);
1195 GNUNET_assert(NULL != my_id_key);
1196 start_key = my_id_key;
1197 GNUNET_assert(NULL != start_key);
1200 GNUNET_CONTAINER_multihashmap_put (successor_peer_hashmap,
1201 my_id_key, (void *)succ_key,
1202 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1210 * Task that collects peer and its corresponding successors.
1212 * @param cls Closure (NULL).
1215 collect_stats (void *cls)
1217 const struct GNUNET_SCHEDULER_TaskContext *tc;
1219 tc = GNUNET_SCHEDULER_get_task_context ();
1220 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
1223 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1224 "Start collecting statistics...\n");
1225 GNUNET_assert(NULL != testbed_handles);
1227 if (0 != max_searches)
1228 successor_peer_hashmap = GNUNET_CONTAINER_multihashmap_create (num_peers,
1230 successor_stats_op =
1231 GNUNET_TESTBED_get_statistics (num_peers, testbed_handles,
1233 successor_stats_iterator,
1234 successor_stats_cont, cls);
1236 GNUNET_assert(NULL != successor_stats_op);
1241 * Callback called when DHT service on the peer is started
1243 * @param cls the context
1244 * @param op the operation that has been finished
1245 * @param emsg error message in case the operation has failed; will be NULL if
1246 * operation has executed successfully.
1249 service_started (void *cls,
1250 struct GNUNET_TESTBED_Operation *op,
1253 struct Context *ctx = cls;
1255 GNUNET_assert (NULL != ctx);
1256 GNUNET_assert (NULL != ctx->op);
1257 GNUNET_TESTBED_operation_done (ctx->op);
1260 DEBUG("Peers Started = %d; num_peers = %d \n", peers_started, num_peers);
1261 if (NULL == successor_stats_task && peers_started == num_peers)
1263 DEBUG("successor_stats_task \n");
1264 struct Collect_Stat_Context *collect_stat_cls = GNUNET_new(struct Collect_Stat_Context);
1265 collect_stat_cls->service_connect_ctx = cls;
1266 collect_stat_cls->op = op;
1268 successor_stats_task = GNUNET_SCHEDULER_add_delayed (delay_stats,
1276 * Signature of a main function for a testcase.
1278 * @param cls closure
1279 * @param h the run handle
1280 * @param num_peers number of peers in 'peers'
1281 * @param peers handle to peers run in the testbed
1282 * @param links_succeeded the number of overlay link connection attempts that
1284 * @param links_failed the number of overlay link
1287 test_run (void *cls,
1288 struct GNUNET_TESTBED_RunHandle *h,
1289 unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers,
1290 unsigned int links_succeeded,
1291 unsigned int links_failed)
1294 unsigned int ac_cnt;
1295 testbed_handles = peers;
1301 INFO ("%u peers started\n", num_peers);
1302 a_ctx = GNUNET_malloc (sizeof (struct Context) * num_peers);
1304 /* select the peers which actively participate in profiling */
1305 n_active = num_peers * PUT_PROBABILITY / 100;
1308 GNUNET_SCHEDULER_shutdown ();
1309 GNUNET_free (a_ctx);
1313 a_ac = GNUNET_malloc (n_active * sizeof (struct ActiveContext));
1316 #if ENABLE_MALICIOUS
1317 unsigned int malicious_peers;
1318 if(PUT_PROBABILITY + MALICIOUS_PROBABILITY > 100)
1320 DEBUG ("Reduce either number of malicious peer or active peers. ");
1321 GNUNET_SCHEDULER_shutdown ();
1322 GNUNET_free (a_ctx);
1326 /* Select the peers which should act maliciously. */
1327 n_malicious = num_peers * MALICIOUS_PROBABILITY / 100;
1329 a_mc = GNUNET_malloc (n_malicious * sizeof (struct MaliciousContext));
1330 malicious_peers = 0;
1332 for (cnt = 0; cnt < num_peers && malicious_peers < n_malicious; cnt++)
1334 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100) >=
1335 MALICIOUS_PROBABILITY)
1337 a_ctx[cnt].mc = &a_mc[malicious_peers];
1338 a_mc[malicious_peers].ctx = &a_ctx[cnt];
1341 n_malicious = malicious_peers;
1342 INFO ("Malicious Peers: %u\n",malicious_peers);
1346 a_ac = GNUNET_malloc (n_active * sizeof (struct ActiveContext));
1348 for (cnt = 0; cnt < num_peers && ac_cnt < n_active; cnt++)
1350 if ((GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100) >=
1354 #if ENABLE_MALICIOUS
1355 if(a_ctx[ac_cnt].mc != NULL)
1359 a_ctx[cnt].ac = &a_ac[ac_cnt];
1360 a_ac[ac_cnt].ctx = &a_ctx[cnt];
1364 INFO ("Active peers: %u\n", n_active);
1366 /* start DHT service on all peers */
1367 for (cnt = 0; cnt < num_peers; cnt++)
1369 a_ctx[cnt].peer = peers[cnt];
1370 a_ctx[cnt].op = GNUNET_TESTBED_peer_manage_service (&a_ctx[cnt],
1381 * Main function that will be run by the scheduler.
1383 * @param cls closure
1384 * @param args remaining command-line arguments
1385 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1386 * @param config configuration
1389 run (void *cls, char *const *args, const char *cfgfile,
1390 const struct GNUNET_CONFIGURATION_Handle *config)
1392 uint64_t event_mask;
1396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Exiting as the number of peers is %u\n"),
1400 cfg = GNUNET_CONFIGURATION_dup (config);
1402 GNUNET_TESTBED_run (hosts_file, cfg, num_peers, event_mask, NULL,
1403 NULL, &test_run, NULL);
1404 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
1412 * @return 0 on success
1415 main (int argc, char *const *argv)
1419 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1420 {'n', "peers", "COUNT",
1421 gettext_noop ("number of peers to start"),
1422 1, &GNUNET_GETOPT_set_uint, &num_peers},
1423 {'s', "searches", "COUNT",
1424 gettext_noop ("maximum number of times we try to search for successor circle formation (0 for R5N)"),
1425 1, &GNUNET_GETOPT_set_uint, &max_searches},
1426 {'H', "hosts", "FILENAME",
1427 gettext_noop ("name of the file with the login information for the testbed"),
1428 1, &GNUNET_GETOPT_set_string, &hosts_file},
1429 {'D', "delay", "DELAY",
1430 gettext_noop ("delay between rounds for collecting statistics (default: 30 sec)"),
1431 1, &GNUNET_GETOPT_set_relative_time, &delay_stats},
1432 {'P', "PUT-delay", "DELAY",
1433 gettext_noop ("delay to start doing PUTs (default: 1 sec)"),
1434 1, &GNUNET_GETOPT_set_relative_time, &delay_put},
1435 {'G', "GET-delay", "DELAY",
1436 gettext_noop ("delay to start doing GETs (default: 5 min)"),
1437 1, &GNUNET_GETOPT_set_relative_time, &delay_get},
1438 {'r', "replication", "DEGREE",
1439 gettext_noop ("replication degree for DHT PUTs"),
1440 1, &GNUNET_GETOPT_set_uint, &replication},
1441 {'t', "timeout", "TIMEOUT",
1442 gettext_noop ("timeout for DHT PUT and GET requests (default: 1 min)"),
1443 1, &GNUNET_GETOPT_set_relative_time, &timeout},
1444 GNUNET_GETOPT_OPTION_END
1448 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1450 /* set default delays */
1451 delay_stats = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1452 delay_put = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1453 delay_get = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1454 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1455 replication = 1; /* default replication */
1458 GNUNET_PROGRAM_run (argc, argv, "dht-profiler",
1460 ("Measure quality and performance of the DHT service."),
1461 options, &run, NULL))