2 This file is part of GNUnet.
3 (C) 2014 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 100
44 * Percentage of peers that should act maliciously.
45 * These peers will never start PUT/GET request.
46 * n_active and n_malicious should not intersect.
48 #define MALICIOUS_PEERS 0
53 static struct GNUNET_CONFIGURATION_Handle *cfg;
56 * Name of the file with the hosts to run the test over
58 static char *hosts_file;
61 * Context for a peer which actively does DHT PUT/GET
66 * Context to hold data of peer
72 * The testbed peer this context belongs to
74 struct GNUNET_TESTBED_Peer *peer;
77 * Testbed operation acting on this peer
79 struct GNUNET_TESTBED_Operation *op;
82 * Active context; NULL if this peer is not an active peer
84 struct ActiveContext *ac;
90 * Context for a peer which should act maliciously.
92 struct MaliciousContext
95 * The linked peer context
100 * Handler to the DHT service
102 struct GNUNET_DHT_Handle *dht;
106 * List of all the malicious peers contexts.
108 struct Context **malicious_peer_contexts = NULL;
113 * Context for a peer which actively does DHT PUT/GET
118 * The linked peer context
123 * Handler to the DHT service
125 struct GNUNET_DHT_Handle *dht;
128 * The data used for do a PUT. Will be NULL if a PUT hasn't been performed yet
133 * The active context used for our DHT GET
135 struct ActiveContext *get_ac;
140 struct GNUNET_DHT_PutHandle *dht_put;
145 struct GNUNET_DHT_GetHandle *dht_get;
148 * The hash of the @e put_data
150 struct GNUNET_HashCode hash;
155 GNUNET_SCHEDULER_TaskIdentifier delay_task;
158 * The size of the @e put_data
160 uint16_t put_data_size;
163 * The number of peers currently doing GET on our data
170 * An array of contexts. The size of this array should be equal to @a num_peers
172 static struct Context *a_ctx;
175 * Array of active peers
177 static struct ActiveContext *a_ac;
180 * The delay between rounds for collecting statistics
182 static struct GNUNET_TIME_Relative delay_stats;
185 * The delay to start puts.
187 static struct GNUNET_TIME_Relative delay_put;
190 * The delay to start puts.
192 static struct GNUNET_TIME_Relative delay_get;
195 * The timeout for GET and PUT
197 static struct GNUNET_TIME_Relative timeout;
202 static unsigned int num_peers;
206 * Number or malicious peers.
208 static unsigned int n_malicious;
212 * Number of active peers
214 static unsigned int n_active;
217 * Number of DHT service connections we currently have
219 static unsigned int n_dht;
222 * Number of DHT PUTs made
224 static unsigned int n_puts;
227 * Number of DHT PUTs succeeded
229 static unsigned int n_puts_ok;
232 * Number of DHT PUTs failed
234 static unsigned int n_puts_fail;
237 * Number of DHT GETs made
239 static unsigned int n_gets;
242 * Number of DHT GETs succeeded
244 static unsigned int n_gets_ok;
247 * Number of DHT GETs succeeded
249 static unsigned int n_gets_fail;
254 static unsigned int replication;
257 * Number of times we try to find the successor circle formation
259 static unsigned int max_searches;
262 * Testbed Operation (to get stats).
264 static struct GNUNET_TESTBED_Operation *bandwidth_stats_op;
267 * To get successor stats.
269 static struct GNUNET_TESTBED_Operation *successor_stats_op;
272 * Testbed peer handles.
274 static struct GNUNET_TESTBED_Peer **testbed_handles;
277 * Total number of messages sent by peer.
279 static uint64_t outgoing_bandwidth;
282 * Total number of messages received by peer.
284 static uint64_t incoming_bandwidth;
287 * Average number of hops taken to do put.
289 static double average_put_path_length;
292 * Average number of hops taken to do get.
294 static double average_get_path_length;
297 * Total put path length across all peers.
299 static unsigned int total_put_path_length;
302 * Total get path length across all peers.
304 static unsigned int total_get_path_length;
307 * Hashmap to store pair of peer and its corresponding successor.
309 static struct GNUNET_CONTAINER_MultiHashMap *successor_peer_hashmap;
312 * Key to start the lookup on successor_peer_hashmap.
314 static struct GNUNET_HashCode *start_key;
317 * Flag used to get the start_key.
322 * Task to collect peer and its current successor statistics.
324 static GNUNET_SCHEDULER_TaskIdentifier successor_stats_task;
327 * Closure for successor_stats_task.
329 struct Collect_Stat_Context
332 * Current Peer Context.
334 struct Context *service_connect_ctx;
337 * Testbed operation acting on this peer
339 struct GNUNET_TESTBED_Operation *op;
343 * List of all the peers contexts.
345 struct Context **peer_contexts = NULL;
348 * Counter to keep track of peers added to peer_context lists.
350 static int peers_started = 0;
353 * Should we do a PUT (mode = 0) or GET (mode = 1);
364 * Are we shutting down
366 static int in_shutdown = 0;
369 * Task that collects successor statistics from all the peers.
374 collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
378 * Shutdown task. Cleanup all resources and operations.
381 * @param tc scheduler task context
384 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
386 struct ActiveContext *ac;
389 in_shutdown = GNUNET_YES;
392 for (cnt=0; cnt < num_peers; cnt++)
394 /* Cleanup active context if this peer is an active peer */
398 if (GNUNET_SCHEDULER_NO_TASK != ac->delay_task)
399 GNUNET_SCHEDULER_cancel (ac->delay_task);
400 if (NULL != ac->put_data)
401 GNUNET_free (ac->put_data);
402 if (NULL != ac->dht_put)
403 GNUNET_DHT_put_cancel (ac->dht_put);
404 if (NULL != ac->dht_get)
405 GNUNET_DHT_get_stop (ac->dht_get);
407 /* Cleanup testbed operation handle at the last as this operation may
408 contain service connection to DHT */
409 if (NULL != a_ctx[cnt].op)
410 GNUNET_TESTBED_operation_done (a_ctx[cnt].op);
415 //FIXME: Should we collect stats only for put/get not for other messages.
416 if(NULL != bandwidth_stats_op)
417 GNUNET_TESTBED_operation_done (bandwidth_stats_op);
418 bandwidth_stats_op = NULL;
419 GNUNET_free_non_null (a_ac);
424 * Stats callback. Finish the stats testbed operation and when all stats have
425 * been iterated, shutdown the test.
428 * @param op the operation that has been finished
429 * @param emsg error message in case the operation has failed; will be NULL if
430 * operation has executed successfully.
433 bandwidth_stats_cont (void *cls,
434 struct GNUNET_TESTBED_Operation *op,
437 INFO ("# Outgoing bandwidth: %u\n", outgoing_bandwidth);
438 INFO ("# Incoming bandwidth: %u\n", incoming_bandwidth);
439 GNUNET_SCHEDULER_shutdown ();
444 * Process statistic values.
447 * @param peer the peer the statistic belong to
448 * @param subsystem name of subsystem that created the statistic
449 * @param name the name of the datum
450 * @param value the current value
451 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
452 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
455 bandwidth_stats_iterator (void *cls,
456 const struct GNUNET_TESTBED_Peer *peer,
457 const char *subsystem,
462 static const char *s_sent = "# Bytes transmitted to other peers";
463 static const char *s_recv = "# Bytes received from other peers";
465 if (0 == strncmp (s_sent, name, strlen (s_sent)))
466 outgoing_bandwidth = outgoing_bandwidth + value;
467 else if (0 == strncmp(s_recv, name, strlen (s_recv)))
468 incoming_bandwidth = incoming_bandwidth + value;
477 INFO ("# PUTS made: %u\n", n_puts);
478 INFO ("# PUTS succeeded: %u\n", n_puts_ok);
479 INFO ("# PUTS failed: %u\n", n_puts_fail);
480 INFO ("# GETS made: %u\n", n_gets);
481 INFO ("# GETS succeeded: %u\n", n_gets_ok);
482 INFO ("# GETS failed: %u\n", n_gets_fail);
483 INFO ("# average_put_path_length: %f\n", average_put_path_length);
484 INFO ("# average_get_path_length: %f\n", average_get_path_length);
486 if (NULL == testbed_handles)
488 INFO ("No peers found\n");
492 bandwidth_stats_op = GNUNET_TESTBED_get_statistics (n_active, testbed_handles,
494 bandwidth_stats_iterator,
495 bandwidth_stats_cont, NULL);
500 * Task to cancel DHT GET.
503 * @param tc scheduler task context
506 cancel_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
508 struct ActiveContext *ac = cls;
509 struct Context *ctx = ac->ctx;
511 ac->delay_task = GNUNET_SCHEDULER_NO_TASK;
512 GNUNET_assert (NULL != ac->dht_get);
513 GNUNET_DHT_get_stop (ac->dht_get);
516 GNUNET_assert (NULL != ctx->op);
517 GNUNET_TESTBED_operation_done (ctx->op);
520 /* If profiling is complete, summarize */
521 if (n_active == n_gets_fail + n_gets_ok)
528 * Iterator called on each result obtained for a DHT
529 * operation that expects a reply
532 * @param exp when will this value expire
533 * @param key key of the result
534 * @param get_path peers on reply path (or NULL if not recorded)
535 * [0] = datastore's first neighbor, [length - 1] = local peer
536 * @param get_path_length number of entries in @a get_path
537 * @param put_path peers on the PUT path (or NULL if not recorded)
538 * [0] = origin, [length - 1] = datastore
539 * @param put_path_length number of entries in @a put_path
540 * @param type type of the result
541 * @param size number of bytes in @a data
542 * @param data pointer to the result data
546 struct GNUNET_TIME_Absolute exp,
547 const struct GNUNET_HashCode *key,
548 const struct GNUNET_PeerIdentity *get_path,
549 unsigned int get_path_length,
550 const struct GNUNET_PeerIdentity *put_path,
551 unsigned int put_path_length,
552 enum GNUNET_BLOCK_Type type,
553 size_t size, const void *data)
555 struct ActiveContext *ac = cls;
556 struct ActiveContext *get_ac = ac->get_ac;
557 struct Context *ctx = ac->ctx;
559 /* Check the keys of put and get match or not. */
560 GNUNET_assert (0 == memcmp (key, &get_ac->hash, sizeof (struct GNUNET_HashCode)));
561 /* we found the data we are looking for */
562 DEBUG ("We found a GET request; %u remaining\n", n_gets - (n_gets_fail + n_gets_ok));
565 GNUNET_DHT_get_stop (ac->dht_get);
567 GNUNET_SCHEDULER_cancel (ac->delay_task);
568 ac->delay_task = GNUNET_SCHEDULER_NO_TASK;
569 GNUNET_assert (NULL != ctx->op);
570 GNUNET_TESTBED_operation_done (ctx->op);
573 total_put_path_length = total_put_path_length + put_path_length;
574 total_get_path_length = total_get_path_length + get_path_length;
576 /* Summarize if profiling is complete */
577 if (n_active == n_gets_fail + n_gets_ok)
579 average_put_path_length = (double)total_put_path_length/(double)n_active;
580 average_get_path_length = (double)total_get_path_length/(double )n_active;
587 * Task to do DHT GETs
589 * @param cls the active context
590 * @param tc the scheduler task context
593 delayed_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
595 struct ActiveContext *ac = cls;
596 struct ActiveContext *get_ac;
599 ac->delay_task = GNUNET_SCHEDULER_NO_TASK;
603 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, n_active);
605 if (NULL != get_ac->put_data)
610 DEBUG ("GET_REQUEST_START key %s \n", GNUNET_h2s((struct GNUNET_HashCode *)ac->put_data));
611 ac->dht_get = GNUNET_DHT_get_start (ac->dht,
612 GNUNET_BLOCK_TYPE_TEST,
614 1, /* replication level */
616 NULL, 0, /* extended query and size */
617 get_iter, ac); /* GET iterator and closure
621 /* schedule the timeout task for GET */
622 ac->delay_task = GNUNET_SCHEDULER_add_delayed (timeout, &cancel_get, ac);
627 * Task to teardown the dht connection. We do it as a task because calling
628 * GNUNET_DHT_disconnect() from put_continutation_callback seems illegal (the
629 * put_continuation_callback() is getting called again synchronously). Also,
630 * only free the operation when we are not shutting down; the shutdown task will
631 * clear the operation during shutdown.
633 * @param cls the context
634 * @return tc scheduler task context.
637 teardown_dht_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
639 struct Context *ctx = cls;
640 struct GNUNET_TESTBED_Operation *op;
642 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
644 GNUNET_assert (NULL != ctx);
645 GNUNET_assert (NULL != (op = ctx->op));
647 GNUNET_TESTBED_operation_done (op);
652 * Queue up a delayed task for doing DHT GET
654 * @param cls the active context
655 * @param success #GNUNET_OK if the PUT was transmitted,
656 * #GNUNET_NO on timeout,
657 * #GNUNET_SYSERR on disconnect from service
658 * after the PUT message was transmitted
659 * (so we don't know if it was received or not)
662 put_cont (void *cls, int success)
664 struct ActiveContext *ac = cls;
665 struct Context *ctx = ac->ctx;
672 GNUNET_assert (NULL != ctx);
673 (void) GNUNET_SCHEDULER_add_now (&teardown_dht_connection, ctx);
678 * Task to do DHT PUTS
680 * @param cls the active context
681 * @param tc the scheduler task context
684 delayed_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
686 struct ActiveContext *ac = cls;
688 ac->delay_task = GNUNET_SCHEDULER_NO_TASK;
689 /* Generate and DHT PUT some random data */
690 ac->put_data_size = 16; /* minimum */
691 ac->put_data_size += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
693 ac->put_data = GNUNET_malloc (ac->put_data_size);
694 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
695 ac->put_data, ac->put_data_size);
696 GNUNET_CRYPTO_hash (ac->put_data, ac->put_data_size, &ac->hash);
697 DEBUG ("PUT_REQUEST_START key %s \n", GNUNET_h2s((struct GNUNET_HashCode *)ac->put_data));
698 ac->dht_put = GNUNET_DHT_put (ac->dht, &ac->hash,
701 GNUNET_BLOCK_TYPE_TEST,
704 GNUNET_TIME_UNIT_FOREVER_ABS, /* expiration time */
705 timeout, /* PUT timeout */
706 put_cont, ac); /* continuation and its closure */
712 * Connection to DHT has been established. Call the delay task.
714 * @param cls the active context
715 * @param op the operation that has been finished
716 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
717 * @param emsg error message in case the operation has failed; will be NULL if
718 * operation has executed successfully.
721 dht_connected (void *cls,
722 struct GNUNET_TESTBED_Operation *op,
726 struct ActiveContext *ac = cls;
727 struct Context *ctx = ac->ctx;
729 GNUNET_assert (NULL != ctx); //FIXME: Fails
730 GNUNET_assert (NULL != ctx->op);
731 GNUNET_assert (ctx->op == op);
732 ac->dht = (struct GNUNET_DHT_Handle *) ca_result;
735 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connection to DHT service failed: %s\n", emsg);
736 GNUNET_TESTBED_operation_done (ctx->op); /* Calls dht_disconnect() */
743 ac->delay_task = GNUNET_SCHEDULER_add_delayed (delay_put, &delayed_put, ac);
746 ac->delay_task = GNUNET_SCHEDULER_add_delayed (delay_get, &delayed_get, ac);
753 * Connect to DHT service and return the DHT client handler
755 * @param cls the active context
756 * @param cfg configuration of the peer to connect to; will be available until
757 * GNUNET_TESTBED_operation_done() is called on the operation returned
758 * from GNUNET_TESTBED_service_connect()
759 * @return service handle to return in 'op_result', NULL on error
762 dht_connect (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
765 return GNUNET_DHT_connect (cfg, 10);
770 * Connect to DHT services of active peers
777 * Adapter function called to destroy a connection to
780 * @param cls the active context
781 * @param op_result service handle returned from the connect adapter
784 dht_disconnect (void *cls, void *op_result)
786 struct ActiveContext *ac = cls;
788 GNUNET_assert (NULL != ac->dht);
789 GNUNET_assert (ac->dht == op_result);
790 GNUNET_DHT_disconnect (ac->dht);
795 if (GNUNET_YES == in_shutdown)
800 if ((n_puts_ok + n_puts_fail) != n_active)
802 /* Start GETs if all PUTs have been made */
804 //(void) GNUNET_SCHEDULER_add_now (&call_start_profiling, NULL);
808 if ((n_gets_ok + n_gets_fail) != n_active)
812 GNUNET_SCHEDULER_shutdown ();
817 * Connect to DHT services of active peers
825 DEBUG("GNUNET_TESTBED_service_connect \n");
826 GNUNET_break (GNUNET_YES != in_shutdown);
827 for(i = 0; i < n_active; i++)
829 struct ActiveContext *ac = &a_ac[i];
830 GNUNET_assert (NULL != (ctx = ac->ctx));
831 GNUNET_assert (NULL == ctx->op);
833 GNUNET_TESTBED_service_connect (ctx,
844 hashmap_iterate_remove(void *cls,
845 const struct GNUNET_HashCode *key,
848 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(successor_peer_hashmap, key, value));
853 static unsigned int tries;
856 * Stats callback. Iterate over the hashmap and check if all th peers form
857 * a virtual ring topology.
860 * @param op the operation that has been finished
861 * @param emsg error message in case the operation has failed; will be NULL if
862 * operation has executed successfully.
865 successor_stats_cont (void *cls,
866 struct GNUNET_TESTBED_Operation *op,
869 struct GNUNET_HashCode *val;
870 struct GNUNET_HashCode *start_val;
871 struct GNUNET_HashCode *key;
874 /* Don't schedule the task till we are looking for circle here. */
875 successor_stats_task = GNUNET_SCHEDULER_NO_TASK;
876 GNUNET_TESTBED_operation_done (successor_stats_op);
877 successor_stats_op = NULL;
880 (struct GNUNET_HashCode *) GNUNET_CONTAINER_multihashmap_get(successor_peer_hashmap,
884 for (count = 0; count < num_peers; count++)
887 val = GNUNET_CONTAINER_multihashmap_get (successor_peer_hashmap,
893 /* Remove the entry from hashmap. This is done to take care of loop. */
895 GNUNET_CONTAINER_multihashmap_remove (successor_peer_hashmap,
898 DEBUG ("Failed to remove entry from hashmap\n");
901 /* If a peer has its own identity as its successor. */
902 if (0 == memcmp(key, val, sizeof (struct GNUNET_HashCode)))
908 GNUNET_assert(GNUNET_SYSERR !=
909 GNUNET_CONTAINER_multihashmap_iterate (successor_peer_hashmap,
910 hashmap_iterate_remove,
913 successor_peer_hashmap = GNUNET_CONTAINER_multihashmap_create (num_peers,
916 if ((start_val == val) && (count == num_peers))
918 DEBUG("CIRCLE COMPLETED after %u tries", tries);
919 //FIXME: FREE HASHMAP.
920 //FIXME: If circle is done, then check that finger table of all the peers
921 //are fill atleast O(log N) and then start with the experiments.
922 if(GNUNET_SCHEDULER_NO_TASK == successor_stats_task)
929 if (max_searches == ++tries)
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
932 "Maximum tries %u exceeded while checking successor TOTAL TRIES %u"
933 " cirle formation. Exiting\n",
935 if (GNUNET_SCHEDULER_NO_TASK != successor_stats_task)
937 successor_stats_task = GNUNET_SCHEDULER_NO_TASK;
939 if(GNUNET_SCHEDULER_NO_TASK == successor_stats_task)
949 successor_stats_task = GNUNET_SCHEDULER_add_delayed (delay_stats, &collect_stats, cls);
956 * Process successor statistic values.
959 * @param peer the peer the statistic belong to
960 * @param subsystem name of subsystem that created the statistic
961 * @param name the name of the datum
962 * @param value the current value
963 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
964 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
967 successor_stats_iterator (void *cls,
968 const struct GNUNET_TESTBED_Peer *peer,
969 const char *subsystem,
974 static const char *key_string = "XDHT";
975 if (0 == strncmp (key_string, name, strlen (key_string)))
978 char successor_str[13];
979 char truncated_my_id_str[13];
980 char truncated_successor_str[13];
981 struct GNUNET_HashCode *my_id_key;
982 struct GNUNET_HashCode *succ_key;
984 strtok((char *)name,":");
985 my_id_str = strtok(NULL,":");
987 strncpy(truncated_my_id_str, my_id_str, 12);
988 truncated_my_id_str[12] = '\0';
989 my_id_key = GNUNET_new(struct GNUNET_HashCode);
990 GNUNET_CRYPTO_hash (truncated_my_id_str, sizeof(truncated_my_id_str),my_id_key);
991 GNUNET_STRINGS_data_to_string(&value, sizeof(uint64_t), successor_str, 13);
992 strncpy(truncated_successor_str, successor_str, 12);
993 truncated_successor_str[12] ='\0';
995 succ_key = GNUNET_new(struct GNUNET_HashCode);
996 GNUNET_CRYPTO_hash (truncated_successor_str, sizeof(truncated_successor_str),succ_key);
1000 start_key = my_id_key;
1003 /* FIXME: GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE do not free the value
1004 which is replaced, need to free it. */
1005 GNUNET_CONTAINER_multihashmap_put (successor_peer_hashmap,
1006 my_id_key, (void *)succ_key,
1007 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1014 * Task that collects peer and its corresponding successors.
1016 * @param cls Closure (NULL).
1017 * @param tc Task Context.
1020 collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1022 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
1025 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
1026 GNUNET_assert(NULL != testbed_handles);
1027 successor_peer_hashmap = GNUNET_CONTAINER_multihashmap_create (num_peers,
1029 successor_stats_op =
1030 GNUNET_TESTBED_get_statistics (num_peers, testbed_handles,
1032 successor_stats_iterator,
1033 successor_stats_cont, cls);
1035 GNUNET_assert(NULL != successor_stats_op);
1039 #if ENABLE_MALICIOUS
1041 * Set the malicious variable in peer malicious context.
1047 DEBUG ("Setting %u peers malicious");
1048 for(i = 0; i < n_malicious; i++)
1050 struct MaliciousContext *mc = &a_mc[i];
1052 GNUNET_TESTBED_service_connect (ac->ctx,
1055 &dht_set_malicious, mc,
1063 * Callback called when DHT service on the peer is started
1065 * @param cls the context
1066 * @param op the operation that has been finished
1067 * @param emsg error message in case the operation has failed; will be NULL if
1068 * operation has executed successfully.
1071 service_started (void *cls,
1072 struct GNUNET_TESTBED_Operation *op,
1075 struct Context *ctx = cls;
1077 GNUNET_assert (NULL != ctx);
1078 GNUNET_assert (NULL != ctx->op);
1079 GNUNET_TESTBED_operation_done (ctx->op);
1082 DEBUG("Peers Started = %d; num_peers = %d \n", peers_started, num_peers);
1083 if (GNUNET_SCHEDULER_NO_TASK == successor_stats_task && peers_started == num_peers)
1085 #if ENABLE_MALICIOUS
1088 DEBUG("successor_stats_task \n");
1089 struct Collect_Stat_Context *collect_stat_cls = GNUNET_new(struct Collect_Stat_Context);
1090 collect_stat_cls->service_connect_ctx = cls;
1091 collect_stat_cls->op = op;
1092 successor_stats_task = GNUNET_SCHEDULER_add_delayed (delay_stats,
1100 * Signature of a main function for a testcase.
1102 * @param cls closure
1103 * @param h the run handle
1104 * @param num_peers number of peers in 'peers'
1105 * @param peers handle to peers run in the testbed
1106 * @param links_succeeded the number of overlay link connection attempts that
1108 * @param links_failed the number of overlay link
1111 test_run (void *cls,
1112 struct GNUNET_TESTBED_RunHandle *h,
1113 unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers,
1114 unsigned int links_succeeded,
1115 unsigned int links_failed)
1118 unsigned int ac_cnt;
1120 testbed_handles = peers;
1126 INFO ("%u peers started\n", num_peers);
1127 a_ctx = GNUNET_malloc (sizeof (struct Context) * num_peers);
1129 /* select the peers which actively participate in profiling */
1130 n_active = num_peers * PUT_PROBABILITY / 100;
1133 GNUNET_SCHEDULER_shutdown ();
1134 GNUNET_free (a_ctx);
1138 #if ENABLE_MALICIOUS
1140 if(PUT_PROBABILITY + MALICIOUS_PEERS > 100)
1142 DEBUG ("Reduce either number of malicious peer or active peers. ");
1143 GNUNET_SCHEDULER_shutdown ();
1144 GNUNET_free (a_ctx);
1148 /* Select the peers which should act maliciously. */
1149 n_malicious = num_peers * MALICIOUS_PEERS / 100;
1151 /* Select n_malicious peers and ensure that those are not active peers.
1152 keep all malicious peer at one place, and call act malicious for all
1157 a_ac = GNUNET_malloc (n_active * sizeof (struct ActiveContext));
1159 for (cnt = 0; cnt < num_peers && ac_cnt < n_active; cnt++)
1161 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100) >=
1164 a_ctx[cnt].ac = &a_ac[ac_cnt];
1165 a_ac[ac_cnt].ctx = &a_ctx[cnt];
1169 INFO ("Active peers: %u\n", n_active);
1171 /* start DHT service on all peers */
1172 for (cnt = 0; cnt < num_peers; cnt++)
1174 a_ctx[cnt].peer = peers[cnt];
1175 a_ctx[cnt].op = GNUNET_TESTBED_peer_manage_service (&a_ctx[cnt],
1186 * Main function that will be run by the scheduler.
1188 * @param cls closure
1189 * @param args remaining command-line arguments
1190 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1191 * @param config configuration
1194 run (void *cls, char *const *args, const char *cfgfile,
1195 const struct GNUNET_CONFIGURATION_Handle *config)
1197 uint64_t event_mask;
1201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Exiting as the number of peers is %u\n"),
1205 cfg = GNUNET_CONFIGURATION_dup (config);
1207 GNUNET_TESTBED_run (hosts_file, cfg, num_peers, event_mask, NULL,
1208 NULL, &test_run, NULL);
1209 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
1217 * @return 0 on success
1220 main (int argc, char *const *argv)
1224 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1225 {'n', "peers", "COUNT",
1226 gettext_noop ("number of peers to start"),
1227 1, &GNUNET_GETOPT_set_uint, &num_peers},
1228 {'s', "searches", "COUNT",
1229 gettext_noop ("maximum number of times we try to search for successor circle formation (default is 1)"),
1230 1, &GNUNET_GETOPT_set_uint, &max_searches},
1231 {'H', "hosts", "FILENAME",
1232 gettext_noop ("name of the file with the login information for the testbed"),
1233 1, &GNUNET_GETOPT_set_string, &hosts_file},
1234 {'D', "delay", "DELAY",
1235 gettext_noop ("delay between rounds for collecting statistics (default: 30 sec)"),
1236 1, &GNUNET_GETOPT_set_relative_time, &delay_stats},
1237 {'P', "PUT-delay", "DELAY",
1238 gettext_noop ("delay to start doing PUTs (default: 1 sec)"),
1239 1, &GNUNET_GETOPT_set_relative_time, &delay_put},
1240 {'G', "GET-delay", "DELAY",
1241 gettext_noop ("delay to start doing GETs (default: 5 min)"),
1242 1, &GNUNET_GETOPT_set_relative_time, &delay_get},
1243 {'r', "replication", "DEGREE",
1244 gettext_noop ("replication degree for DHT PUTs"),
1245 1, &GNUNET_GETOPT_set_uint, &replication},
1246 {'t', "timeout", "TIMEOUT",
1247 gettext_noop ("timeout for DHT PUT and GET requests (default: 1 min)"),
1248 1, &GNUNET_GETOPT_set_relative_time, &timeout},
1249 GNUNET_GETOPT_OPTION_END
1253 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1255 /* set default delays */
1256 delay_stats = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
1257 delay_put = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
1258 delay_get = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5);
1259 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1);
1260 replication = 1; /* default replication */
1263 GNUNET_PROGRAM_run (argc, argv, "dht-profiler",
1265 ("Measure quality and performance of the DHT service."),
1266 options, &run, NULL))