2 This file is part of GNUnet.
3 Copyright (C) 2009, 2012 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
21 * @file rps/test_rps.c
22 * @brief Testcase for the random peer sampling service. Starts
23 * a peergroup with a given number of peers, then waits to
24 * receive size pushes/pulls from each peer. Expects to wait
25 * for one message from each peer.
28 //#include "rps_test_lib.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
32 #include "gnunet_rps_service.h"
33 #include "rps-test_util.h"
34 #include "gnunet-service-rps_sampler_elem.h"
39 #define BIT(n) (1 << (n))
42 * How many peers do we start?
44 static uint32_t num_peers;
47 * @brief numer of bits required to represent the largest peer id
49 static unsigned bits_needed;
52 * How long do we run the test?
54 static struct GNUNET_TIME_Relative duration;
57 * When do we do a hard shutdown?
59 static struct GNUNET_TIME_Relative timeout;
63 * Portion of malicious peers
65 static double portion = .1;
68 * Type of malicious peer to test
70 static unsigned int mal_type = 0;
73 * Handles to all of the running peers
75 static struct GNUNET_TESTBED_Peer **testbed_peers;
79 STAT_TYPE_ROUNDS, /* 0 */
80 STAT_TYPE_BLOCKS, /* 1 */
81 STAT_TYPE_BLOCKS_MANY_PUSH, /* 2 */
82 STAT_TYPE_BLOCKS_NO_PUSH, /* 3 */
83 STAT_TYPE_BLOCKS_NO_PULL, /* 4 */
84 STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL, /* 5 */
85 STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL, /* 6 */
86 STAT_TYPE_ISSUED_PUSH_SEND, /* 7 */
87 STAT_TYPE_ISSUED_PUSH_SEND_MH, /* 8 */
88 STAT_TYPE_ISSUED_PULL_REQ, /* 9 */
89 STAT_TYPE_ISSUED_PULL_REQ_MH, /* 10 */
90 STAT_TYPE_ISSUED_PULL_REP, /* 11 */
91 STAT_TYPE_SENT_PUSH_SEND, /* 12 */
92 STAT_TYPE_SENT_PULL_REQ, /* 13 */
93 STAT_TYPE_SENT_PULL_REQ_MH, /* 14 */
94 STAT_TYPE_SENT_PULL_REP, /* 15 */
95 STAT_TYPE_RECV_PUSH_SEND, /* 16 */
96 STAT_TYPE_RECV_PUSH_SEND_MH, /* 17 */
97 STAT_TYPE_RECV_PULL_REQ, /* 18 */
98 STAT_TYPE_RECV_PULL_REQ_MH, /* 19 */
99 STAT_TYPE_RECV_PULL_REP, /* 20 */
100 STAT_TYPE_RECV_PULL_REP_MH, /* 21 */
101 STAT_TYPE_VIEW_SIZE, /* 22 */
102 STAT_TYPE_KNOWN_PEERS, /* 23 */
103 STAT_TYPE_VALID_PEERS, /* 24 */
104 STAT_TYPE_LEARND_PEERS, /* 25 */
105 STAT_TYPE_PENDING_ONLINE_CHECKS, /* 26 */
106 STAT_TYPE_UNREQUESTED_PULL_REPLIES, /* 27 */
107 STAT_TYPE_PEERS_IN_PUSH_MAP, /* 28 */
108 STAT_TYPE_PEERS_IN_PULL_MAP, /* 29 */
109 STAT_TYPE_PEERS_IN_VIEW, /* 30 */
110 STAT_TYPE_VIEW_SIZE_AIM, /* 31 */
111 STAT_TYPE_MAX, /* 32 */
114 static char* stat_type_strings[] = {
117 "# rounds blocked - too many pushes",
118 "# rounds blocked - no pushes",
119 "# rounds blocked - no pull replies",
120 "# rounds blocked - too many pushes, no pull replies",
121 "# rounds blocked - no pushes, no pull replies",
122 "# push send issued",
123 "# push send issued (multi-hop peer)",
124 "# pull request send issued",
125 "# pull request send issued (multi-hop peer)",
126 "# pull reply send issued",
128 "# pull requests sent",
129 "# pull requests sent (multi-hop peer)",
130 "# pull replys sent",
131 "# push message received",
132 "# push message received (multi-hop peer)",
133 "# pull request message received",
134 "# pull request message received (multi-hop peer)",
135 "# pull reply messages received",
136 "# pull reply messages received (multi-hop peer)",
141 "# pending online checks",
142 "# unrequested pull replies",
143 "# peers in push map at end of round",
144 "# peers in pull map at end of round",
145 "# peers in view at end of round",
151 struct RPSPeer *rps_peer;
152 enum STAT_TYPE stat_type;
157 * @brief Converts string representation to the corresponding #STAT_TYPE enum.
159 * @param stat_str string representation of statistics specifier
161 * @return corresponding enum
163 enum STAT_TYPE stat_str_2_type (const char *stat_str)
165 if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_NO_PULL],
167 strlen (stat_type_strings[STAT_TYPE_BLOCKS_NO_PULL])))
169 return STAT_TYPE_BLOCKS_NO_PULL;
171 else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL],
173 strlen (stat_type_strings[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL])))
175 return STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL;
177 else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_MANY_PUSH],
179 strlen (stat_type_strings[STAT_TYPE_BLOCKS_MANY_PUSH])))
181 return STAT_TYPE_BLOCKS_MANY_PUSH;
183 else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL],
185 strlen (stat_type_strings[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL])))
187 return STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL;
189 else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_NO_PUSH],
191 strlen (stat_type_strings[STAT_TYPE_BLOCKS_NO_PUSH])))
193 return STAT_TYPE_BLOCKS_NO_PUSH;
195 else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS],
197 strlen (stat_type_strings[STAT_TYPE_BLOCKS])))
199 return STAT_TYPE_BLOCKS;
201 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ROUNDS],
203 strlen (stat_type_strings[STAT_TYPE_ROUNDS])))
205 return STAT_TYPE_ROUNDS;
207 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PUSH_SEND],
209 strlen (stat_type_strings[STAT_TYPE_ISSUED_PUSH_SEND])))
211 return STAT_TYPE_ISSUED_PUSH_SEND;
213 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PUSH_SEND_MH],
215 strlen (stat_type_strings[STAT_TYPE_ISSUED_PUSH_SEND_MH])))
217 return STAT_TYPE_ISSUED_PUSH_SEND_MH;
219 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PULL_REQ],
221 strlen (stat_type_strings[STAT_TYPE_ISSUED_PULL_REQ])))
223 return STAT_TYPE_ISSUED_PULL_REQ;
225 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PULL_REQ_MH],
227 strlen (stat_type_strings[STAT_TYPE_ISSUED_PULL_REQ_MH])))
229 return STAT_TYPE_ISSUED_PULL_REQ_MH;
231 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PULL_REP],
233 strlen (stat_type_strings[STAT_TYPE_ISSUED_PULL_REP])))
235 return STAT_TYPE_ISSUED_PULL_REP;
237 else if (0 == strncmp (stat_type_strings[STAT_TYPE_SENT_PUSH_SEND],
239 strlen (stat_type_strings[STAT_TYPE_SENT_PUSH_SEND])))
241 return STAT_TYPE_SENT_PUSH_SEND;
243 else if (0 == strncmp (stat_type_strings[STAT_TYPE_SENT_PULL_REQ],
245 strlen (stat_type_strings[STAT_TYPE_SENT_PULL_REQ])))
247 return STAT_TYPE_SENT_PULL_REQ;
249 else if (0 == strncmp (stat_type_strings[STAT_TYPE_SENT_PULL_REQ_MH],
251 strlen (stat_type_strings[STAT_TYPE_SENT_PULL_REQ_MH])))
253 return STAT_TYPE_SENT_PULL_REQ_MH;
255 else if (0 == strncmp (stat_type_strings[STAT_TYPE_SENT_PULL_REP],
257 strlen (stat_type_strings[STAT_TYPE_SENT_PULL_REP])))
259 return STAT_TYPE_SENT_PULL_REP;
261 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PUSH_SEND],
263 strlen (stat_type_strings[STAT_TYPE_RECV_PUSH_SEND])))
265 return STAT_TYPE_RECV_PUSH_SEND;
267 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PUSH_SEND_MH],
269 strlen (stat_type_strings[STAT_TYPE_RECV_PUSH_SEND_MH])))
271 return STAT_TYPE_RECV_PUSH_SEND_MH;
273 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PULL_REQ],
275 strlen (stat_type_strings[STAT_TYPE_RECV_PULL_REQ])))
277 return STAT_TYPE_RECV_PULL_REQ;
279 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PULL_REQ_MH],
281 strlen (stat_type_strings[STAT_TYPE_RECV_PULL_REQ_MH])))
283 return STAT_TYPE_RECV_PULL_REQ_MH;
285 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PULL_REP],
287 strlen (stat_type_strings[STAT_TYPE_RECV_PULL_REP])))
289 return STAT_TYPE_RECV_PULL_REP;
291 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PULL_REP_MH],
293 strlen (stat_type_strings[STAT_TYPE_RECV_PULL_REP_MH])))
295 return STAT_TYPE_RECV_PULL_REP_MH;
297 else if (0 == strncmp (stat_type_strings[STAT_TYPE_VIEW_SIZE],
299 strlen (stat_type_strings[STAT_TYPE_VIEW_SIZE])))
301 return STAT_TYPE_VIEW_SIZE;
303 else if (0 == strncmp (stat_type_strings[STAT_TYPE_KNOWN_PEERS],
305 strlen (stat_type_strings[STAT_TYPE_KNOWN_PEERS])))
307 return STAT_TYPE_KNOWN_PEERS;
309 else if (0 == strncmp (stat_type_strings[STAT_TYPE_VALID_PEERS],
311 strlen (stat_type_strings[STAT_TYPE_VALID_PEERS])))
313 return STAT_TYPE_VALID_PEERS;
315 else if (0 == strncmp (stat_type_strings[STAT_TYPE_LEARND_PEERS],
317 strlen (stat_type_strings[STAT_TYPE_LEARND_PEERS])))
319 return STAT_TYPE_LEARND_PEERS;
321 else if (0 == strncmp (stat_type_strings[STAT_TYPE_PENDING_ONLINE_CHECKS],
323 strlen (stat_type_strings[STAT_TYPE_PENDING_ONLINE_CHECKS])))
325 return STAT_TYPE_PENDING_ONLINE_CHECKS;
327 else if (0 == strncmp (stat_type_strings[STAT_TYPE_UNREQUESTED_PULL_REPLIES],
329 strlen (stat_type_strings[STAT_TYPE_UNREQUESTED_PULL_REPLIES])))
331 return STAT_TYPE_UNREQUESTED_PULL_REPLIES;
333 else if (0 == strncmp (stat_type_strings[STAT_TYPE_PEERS_IN_PUSH_MAP],
335 strlen (stat_type_strings[STAT_TYPE_PEERS_IN_PUSH_MAP])))
337 return STAT_TYPE_PEERS_IN_PUSH_MAP;
339 else if (0 == strncmp (stat_type_strings[STAT_TYPE_PEERS_IN_PULL_MAP],
341 strlen (stat_type_strings[STAT_TYPE_PEERS_IN_PULL_MAP])))
343 return STAT_TYPE_PEERS_IN_PULL_MAP;
345 else if (0 == strncmp (stat_type_strings[STAT_TYPE_PEERS_IN_VIEW],
347 strlen (stat_type_strings[STAT_TYPE_PEERS_IN_VIEW])))
349 return STAT_TYPE_PEERS_IN_VIEW;
351 else if (0 == strncmp (stat_type_strings[STAT_TYPE_VIEW_SIZE_AIM],
353 strlen (stat_type_strings[STAT_TYPE_VIEW_SIZE_AIM])))
355 return STAT_TYPE_VIEW_SIZE_AIM;
357 return STAT_TYPE_MAX;
362 * @brief Indicates whether peer should go off- or online
364 enum PEER_ONLINE_DELTA {
366 * @brief Indicates peer going online
370 * @brief Indicates peer going offline
372 PEER_GO_OFFLINE = -1,
376 * Operation map entry
383 struct OpListEntry *next;
388 struct OpListEntry *prev;
391 * The testbed operation
393 struct GNUNET_TESTBED_Operation *op;
396 * Depending on whether we start or stop RPS service at the peer, set this to
397 * #PEER_GO_ONLINE (1) or #PEER_GO_OFFLINE (-1)
399 enum PEER_ONLINE_DELTA delta;
402 * Index of the regarding peer
410 static struct OpListEntry *oplist_head;
415 static struct OpListEntry *oplist_tail;
419 * A pending reply: A request was sent and the reply is pending.
426 struct PendingReply *next;
427 struct PendingReply *prev;
430 * Handle to the request we are waiting for
432 struct GNUNET_RPS_Request_Handle_Single_Info *req_handle;
435 * The peer that requested
437 struct RPSPeer *rps_peer;
442 * A pending request: A request was not made yet but is scheduled for later.
444 struct PendingRequest
449 struct PendingRequest *next;
450 struct PendingRequest *prev;
453 * Handle to the request we are waiting for
455 struct GNUNET_SCHEDULER_Task *request_task;
458 * The peer that requested
460 struct RPSPeer *rps_peer;
465 * Information we track for each peer.
475 * Handle for RPS connect operation.
477 struct GNUNET_TESTBED_Operation *op;
480 * Handle to RPS service.
482 struct GNUNET_RPS_Handle *rps_handle;
487 struct GNUNET_PeerIdentity *peer_id;
490 * A request handle to check for an request
492 //struct GNUNET_RPS_Request_Handle *req_handle;
495 * Peer on- or offline?
500 * Number of Peer IDs to request during the whole test
502 unsigned int num_ids_to_request;
505 * Pending requests DLL
507 struct PendingRequest *pending_req_head;
508 struct PendingRequest *pending_req_tail;
511 * Number of pending requests
513 unsigned int num_pending_reqs;
516 * Pending replies DLL
518 struct PendingReply *pending_rep_head;
519 struct PendingReply *pending_rep_tail;
522 * Number of pending replies
524 unsigned int num_pending_reps;
527 * Number of received PeerIDs
529 unsigned int num_recv_ids;
532 * Pending operation on that peer
534 const struct OpListEntry *entry_op_manage;
537 * Testbed operation to connect to statistics service
539 struct GNUNET_TESTBED_Operation *stat_op;
542 * Handle to the statistics service
544 struct GNUNET_STATISTICS_Handle *stats_h;
547 * @brief flags to indicate which statistics values have been already
548 * collected from the statistics service.
549 * Used to check whether we are able to shutdown.
551 uint32_t stat_collected_flags;
554 * @brief File name of the file the stats are finally written to
556 const char *file_name_stats;
559 * @brief File name of the file the stats are finally written to
561 const char *file_name_probs;
564 * @brief File name of the file the stats are finally written to
566 const char *file_name_probs_hist;
569 * @brief The current view
571 struct GNUNET_PeerIdentity *cur_view;
574 * @brief Number of peers in the #cur_view.
576 uint32_t cur_view_count;
579 * @brief Number of occurrences in other peer's view
581 uint32_t count_in_views;
584 * @brief statistics values
586 uint64_t stats[STAT_TYPE_MAX];
588 * @brief Handle for the statistics get request
590 struct GNUNET_STATISTICS_GetHandle *h_stat_get[STAT_TYPE_MAX];
593 * @brief Keep the probabilities in cache for computing the probabilities
594 * with respect to history.
596 double *eval_probs_cache;
600 * Information for all the peers.
602 static struct RPSPeer *rps_peers;
605 * Peermap to get the index of a given peer ID quick.
607 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
612 static struct GNUNET_PeerIdentity *rps_peer_ids;
615 * ID of the targeted peer.
617 static struct GNUNET_PeerIdentity *target_peer;
620 * Number of online peers.
622 static unsigned int num_peers_online;
625 * @brief The added sizes of the peer's views
627 static unsigned int view_sizes;
630 * Return value from 'main'.
635 * Identifier for the task that runs after the test to collect results
637 static struct GNUNET_SCHEDULER_Task *post_test_task;
640 * Identifier for the shutdown task
642 static struct GNUNET_SCHEDULER_Task *shutdown_task;
646 * Identifier for the churn task that runs periodically
648 static struct GNUNET_SCHEDULER_Task *churn_task;
651 * Called to initialise the given RPSPeer
653 typedef void (*InitPeer) (struct RPSPeer *rps_peer);
656 * @brief Called directly after connecting to the service
658 * @param rps_peer Specific peer the function is called on
659 * @param h the handle to the rps service
661 typedef void (*PreTest) (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h);
664 * @brief Executes functions to test the api/service for a given peer
666 * Called from within #rps_connect_complete_cb ()
667 * Implemented by #churn_test_cb, #profiler_cb, #mal_cb, #single_req_cb,
668 * #delay_req_cb, #seed_big_cb, #single_peer_seed_cb, #seed_cb, #req_cancel_cb
670 * @param rps_peer the peer the task runs on
672 typedef void (*MainTest) (struct RPSPeer *rps_peer);
675 * Callback called once the requested random peers are available
677 typedef void (*ReplyHandle) (void *cls,
679 const struct GNUNET_PeerIdentity *recv_peers);
682 * Called directly before disconnecting from the service
684 typedef void (*PostTest) (struct RPSPeer *peer);
687 * Function called after disconnect to evaluate test success
689 typedef int (*EvaluationCallback) (void);
692 * @brief Do we have Churn?
696 * @brief If we have churn this is set
700 * @brief If we have no churn this is set
706 * @brief Is it ok to quit the test before the timeout?
708 enum OPTION_QUICK_QUIT {
710 * @brief It is ok for the test to quit before the timeout triggers
715 * @brief It is NOT ok for the test to quit before the timeout triggers
721 * @brief Do we collect statistics at the end?
723 enum OPTION_COLLECT_STATISTICS {
725 * @brief We collect statistics at the end
730 * @brief We do not collect statistics at the end
732 NO_COLLECT_STATISTICS,
736 * @brief Do we collect views during run?
738 enum OPTION_COLLECT_VIEW {
740 * @brief We collect view during run
745 * @brief We do not collect the view during run
751 * Structure to define a single test
761 * Called with a single peer in order to initialise that peer
766 * Called directly after connecting to the service
771 * Main function for each peer
776 * Callback called once the requested peers are available
778 ReplyHandle reply_handle;
781 * Called directly before disconnecting from the service
786 * Function to evaluate the test results
788 EvaluationCallback eval_cb;
793 uint32_t request_interval;
796 * Number of Requests to make.
798 uint32_t num_requests;
801 * Run with (-out) churn
803 enum OPTION_CHURN have_churn;
806 * Quit test before timeout?
808 enum OPTION_QUICK_QUIT have_quick_quit;
811 * Collect statistics at the end?
813 enum OPTION_COLLECT_STATISTICS have_collect_statistics;
816 * Collect view during run?
818 enum OPTION_COLLECT_VIEW have_collect_view;
821 * @brief Mark which values from the statistics service to collect at the end
824 uint32_t stat_collect_flags;
828 * Did we finish the test?
830 static int post_test;
833 * Are we shutting down?
835 static int in_shutdown;
838 * Append arguments to file
841 tofile_ (const char *file_name, const char *line)
843 struct GNUNET_DISK_FileHandle *f;
844 /* char output_buffer[512]; */
849 if (NULL == (f = GNUNET_DISK_file_open (file_name,
850 GNUNET_DISK_OPEN_APPEND |
851 GNUNET_DISK_OPEN_WRITE |
852 GNUNET_DISK_OPEN_CREATE,
853 GNUNET_DISK_PERM_USER_READ |
854 GNUNET_DISK_PERM_USER_WRITE |
855 GNUNET_DISK_PERM_GROUP_READ |
856 GNUNET_DISK_PERM_OTHER_READ)))
858 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
859 "Not able to open file %s\n",
863 /* size = GNUNET_snprintf (output_buffer,
864 sizeof (output_buffer),
866 GNUNET_TIME_absolute_get ().abs_value_us,
870 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
871 "Failed to write string to buffer (size: %i)\n",
876 size = strlen (line) * sizeof (char);
878 size2 = GNUNET_DISK_file_write (f, line, size);
881 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
882 "Unable to write to file! (Size: %lu, size2: %lu)\n",
885 if (GNUNET_YES != GNUNET_DISK_file_close (f))
887 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
888 "Unable to close file\n");
893 if (GNUNET_YES != GNUNET_DISK_file_close (f))
895 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
896 "Unable to close file\n");
901 * This function is used to facilitate writing important information to disk
903 #define tofile(file_name, ...) do {\
906 size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
908 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
909 "Failed to create tmp_buf\n");\
911 tofile_(file_name,tmp_buf);\
916 * Write the ids and their according index in the given array to a file
920 ids_to_file (char *file_name,
921 struct GNUNET_PeerIdentity *peer_ids,
922 unsigned int num_peer_ids)
926 for (i=0 ; i < num_peer_ids ; i++)
931 GNUNET_i2s_full (&peer_ids[i]));
936 * Test the success of a single test
946 for (i = 0; i < num_peers; i++)
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
949 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
951 GNUNET_i2s (rps_peers[i].peer_id),
952 rps_peers[i].num_recv_ids,
953 rps_peers[i].num_ids_to_request,
954 (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
955 tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
957 return tmp_ok? 0 : 1;
962 * Creates an oplist entry and adds it to the oplist DLL
964 static struct OpListEntry *
967 struct OpListEntry *entry;
969 entry = GNUNET_new (struct OpListEntry);
970 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
976 * @brief Checks if given peer already received its statistics value from the
977 * statistics service.
979 * @param rps_peer the peer to check for
981 * @return #GNUNET_YES if so
982 * #GNUNET_NO otherwise
984 static int check_statistics_collect_completed_single_peer (
985 const struct RPSPeer *rps_peer)
987 if (cur_test_run.stat_collect_flags !=
988 (cur_test_run.stat_collect_flags &
989 rps_peer->stat_collected_flags))
996 * @brief Checks if all peers already received their statistics value from the
997 * statistics service.
999 * @return #GNUNET_YES if so
1000 * #GNUNET_NO otherwise
1002 static int check_statistics_collect_completed ()
1006 for (i = 0; i < num_peers; i++)
1008 if (GNUNET_NO == check_statistics_collect_completed_single_peer (&rps_peers[i]))
1010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1011 "At least Peer %" PRIu32 " did not yet receive all statistics values\n",
1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1017 "All peers received their statistics values\n");
1022 rps_disconnect_adapter (void *cls,
1026 cancel_pending_req (struct PendingRequest *pending_req)
1028 struct RPSPeer *rps_peer;
1030 rps_peer = pending_req->rps_peer;
1031 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1032 rps_peer->pending_req_tail,
1034 rps_peer->num_pending_reqs--;
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 "Cancelling pending rps get request\n");
1037 GNUNET_SCHEDULER_cancel (pending_req->request_task);
1038 GNUNET_free (pending_req);
1042 cancel_request (struct PendingReply *pending_rep)
1044 struct RPSPeer *rps_peer;
1046 rps_peer = pending_rep->rps_peer;
1047 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1048 rps_peer->pending_rep_tail,
1050 rps_peer->num_pending_reps--;
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052 "Cancelling rps get reply\n");
1053 GNUNET_assert (NULL != pending_rep->req_handle);
1054 GNUNET_RPS_request_single_info_cancel (pending_rep->req_handle);
1055 pending_rep->req_handle = NULL;
1056 GNUNET_free (pending_rep);
1061 clean_peer (unsigned peer_index)
1063 struct PendingRequest *pending_req;
1065 while (NULL != (pending_req = rps_peers[peer_index].pending_req_head))
1067 cancel_pending_req (pending_req);
1069 pending_req = rps_peers[peer_index].pending_req_head;
1070 rps_disconnect_adapter (&rps_peers[peer_index],
1071 &rps_peers[peer_index].rps_handle);
1072 for (unsigned stat_type = STAT_TYPE_ROUNDS;
1073 stat_type < STAT_TYPE_MAX;
1076 if (NULL != rps_peers[peer_index].h_stat_get[stat_type])
1078 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1079 "(%u) did not yet receive stat value for `%s'\n",
1080 rps_peers[peer_index].index,
1081 stat_type_strings[stat_type]);
1082 GNUNET_STATISTICS_get_cancel (
1083 rps_peers[peer_index].h_stat_get[stat_type]);
1086 if (NULL != rps_peers[peer_index].op)
1088 GNUNET_TESTBED_operation_done (rps_peers[peer_index].op);
1089 rps_peers[peer_index].op = NULL;
1094 * Task run on timeout to shut everything down.
1097 shutdown_op (void *cls)
1100 struct OpListEntry *entry;
1103 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1104 "Shutdown task scheduled, going down.\n");
1105 in_shutdown = GNUNET_YES;
1107 if (NULL != shutdown_task)
1109 GNUNET_SCHEDULER_cancel (shutdown_task);
1110 shutdown_task = NULL;
1112 if (NULL != post_test_task)
1114 GNUNET_SCHEDULER_cancel (post_test_task);
1115 post_test_task = NULL;
1117 if (NULL != churn_task)
1119 GNUNET_SCHEDULER_cancel (churn_task);
1122 entry = oplist_head;
1123 while (NULL != (entry = oplist_head))
1125 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1126 "Operation still pending on shutdown (%u)\n",
1128 GNUNET_TESTBED_operation_done (entry->op);
1129 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1130 GNUNET_free (entry);
1132 for (i = 0; i < num_peers; i++)
1140 trigger_shutdown (void *cls)
1144 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1145 "Shutdown was triggerd by timeout, going down.\n");
1146 shutdown_task = NULL;
1147 GNUNET_SCHEDULER_shutdown ();
1152 * Task run after #duration to collect statistics and potentially shut down.
1155 post_test_op (void *cls)
1160 post_test_task = NULL;
1161 post_test = GNUNET_YES;
1162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1163 "Executing post test op.\n");
1164 if (NULL != churn_task)
1166 GNUNET_SCHEDULER_cancel (churn_task);
1169 for (i = 0; i < num_peers; i++)
1171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1172 "Executing post test op. (peer %" PRIu32 ")\n",
1173 rps_peers[i].index);
1174 if (NULL != rps_peers[i].op)
1176 GNUNET_TESTBED_operation_done (rps_peers[i].op);
1177 rps_peers[i].op = NULL;
1178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1179 "Cancelled testbed operation\n");
1181 if (NULL != cur_test_run.post_test)
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing post_test for peer %u\n", i);
1184 cur_test_run.post_test (&rps_peers[i]);
1187 /* If we do not collect statistics, shut down directly */
1188 if (NO_COLLECT_STATISTICS == cur_test_run.have_collect_statistics ||
1189 GNUNET_YES == check_statistics_collect_completed())
1191 GNUNET_SCHEDULER_cancel (shutdown_task);
1192 shutdown_task = NULL;
1193 GNUNET_SCHEDULER_shutdown ();
1202 seed_peers (void *cls)
1204 struct RPSPeer *peer = cls;
1205 unsigned int amount;
1208 // TODO if malicious don't seed mal peers
1209 amount = round (.5 * num_peers);
1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
1212 for (i = 0 ; i < amount ; i++)
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
1215 GNUNET_i2s (&rps_peer_ids[i]));
1217 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
1222 * Get the id of peer i.
1225 info_cb (void *cb_cls,
1226 struct GNUNET_TESTBED_Operation *op,
1227 const struct GNUNET_TESTBED_PeerInformation *pinfo,
1230 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
1233 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1238 if (NULL == pinfo || NULL != emsg)
1240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
1241 GNUNET_TESTBED_operation_done (entry->op);
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1248 GNUNET_i2s (pinfo->result.id));
1250 rps_peer_ids[entry->index] = *(pinfo->result.id);
1251 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
1253 GNUNET_assert (GNUNET_OK ==
1254 GNUNET_CONTAINER_multipeermap_put (peer_map,
1255 &rps_peer_ids[entry->index],
1256 &rps_peers[entry->index],
1257 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1258 tofile ("/tmp/rps/peer_ids",
1261 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
1263 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1264 GNUNET_TESTBED_operation_done (entry->op);
1265 GNUNET_free (entry);
1270 * Callback to be called when RPS service connect operation is completed
1272 * @param cls the callback closure from functions generating an operation
1273 * @param op the operation that has been finished
1274 * @param ca_result the RPS service handle returned from rps_connect_adapter
1275 * @param emsg error message in case the operation has failed; will be NULL if
1276 * operation has executed successfully.
1279 rps_connect_complete_cb (void *cls,
1280 struct GNUNET_TESTBED_Operation *op,
1284 struct RPSPeer *rps_peer = cls;
1285 struct GNUNET_RPS_Handle *rps = ca_result;
1287 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1292 rps_peer->rps_handle = rps;
1293 rps_peer->online = GNUNET_YES;
1296 GNUNET_assert (op == rps_peer->op);
1299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1300 "Failed to connect to RPS service: %s\n",
1303 GNUNET_SCHEDULER_shutdown ();
1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1308 "Started client successfully (%u)\n",
1311 cur_test_run.main_test (rps_peer);
1316 * Adapter function called to establish a connection to
1319 * @param cls closure
1320 * @param cfg configuration of the peer to connect to; will be available until
1321 * GNUNET_TESTBED_operation_done() is called on the operation returned
1322 * from GNUNET_TESTBED_service_connect()
1323 * @return service handle to return in 'op_result', NULL on error
1326 rps_connect_adapter (void *cls,
1327 const struct GNUNET_CONFIGURATION_Handle *cfg)
1329 struct GNUNET_RPS_Handle *h;
1331 h = GNUNET_RPS_connect (cfg);
1333 if (NULL != cur_test_run.pre_test)
1334 cur_test_run.pre_test (cls, h);
1340 * Called to open a connection to the peer's statistics
1342 * @param cls peer context
1343 * @param cfg configuration of the peer to connect to; will be available until
1344 * GNUNET_TESTBED_operation_done() is called on the operation returned
1345 * from GNUNET_TESTBED_service_connect()
1346 * @return service handle to return in 'op_result', NULL on error
1349 stat_connect_adapter (void *cls,
1350 const struct GNUNET_CONFIGURATION_Handle *cfg)
1352 struct RPSPeer *peer = cls;
1354 peer->stats_h = GNUNET_STATISTICS_create ("rps-profiler", cfg);
1355 return peer->stats_h;
1359 * Called to disconnect from peer's statistics service
1361 * @param cls peer context
1362 * @param op_result service handle returned from the connect adapter
1365 stat_disconnect_adapter (void *cls, void *op_result)
1367 struct RPSPeer *peer = cls;
1369 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1370 // (peer->stats_h, "core", "# peers connected",
1371 // stat_iterator, peer));
1372 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1373 // (peer->stats_h, "nse", "# peers connected",
1374 // stat_iterator, peer));
1375 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
1376 peer->stats_h = NULL;
1380 * Called after successfully opening a connection to a peer's statistics
1381 * service; we register statistics monitoring for CORE and NSE here.
1383 * @param cls the callback closure from functions generating an operation
1384 * @param op the operation that has been finished
1385 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
1386 * @param emsg error message in case the operation has failed; will be NULL if
1387 * operation has executed successfully.
1390 stat_complete_cb (void *cls,
1391 struct GNUNET_TESTBED_Operation *op,
1395 //struct GNUNET_STATISTICS_Handle *sh = ca_result;
1396 //struct RPSPeer *peer = (struct RPSPeer *) cls;
1406 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1407 // (sh, "core", "# peers connected",
1408 // stat_iterator, peer));
1409 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1410 // (sh, "nse", "# peers connected",
1411 // stat_iterator, peer));
1416 * Adapter function called to destroy connection to
1419 * @param cls closure
1420 * @param op_result service handle returned from the connect adapter
1423 rps_disconnect_adapter (void *cls,
1426 struct RPSPeer *peer = cls;
1427 struct GNUNET_RPS_Handle *h = op_result;
1428 struct PendingReply *pending_rep;
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1431 "disconnect_adapter (%u)\n",
1433 GNUNET_assert (NULL != peer);
1434 if (NULL != peer->rps_handle)
1436 while (NULL != (pending_rep = peer->pending_rep_head))
1438 cancel_request (pending_rep);
1440 GNUNET_assert (h == peer->rps_handle);
1443 GNUNET_RPS_disconnect (h);
1446 peer->rps_handle = NULL;
1451 /***********************************************************************
1452 * Definition of tests
1453 ***********************************************************************/
1456 * Callback to call on receipt of a reply
1458 * @param cls closure
1459 * @param n number of peers
1460 * @param recv_peers the received peers
1463 default_reply_handle (void *cls,
1465 const struct GNUNET_PeerIdentity *recv_peers)
1467 struct RPSPeer *rps_peer;
1468 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1471 rps_peer = pending_rep->rps_peer;
1472 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1473 rps_peer->pending_rep_tail,
1475 rps_peer->num_pending_reps--;
1476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1477 "[%s] got %" PRIu64 " peers:\n",
1478 GNUNET_i2s (rps_peer->peer_id),
1481 for (i = 0; i < n; i++)
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1486 GNUNET_i2s (&recv_peers[i]));
1488 rps_peer->num_recv_ids++;
1491 if (GNUNET_YES != post_test) return;
1492 if (HAVE_QUICK_QUIT != cur_test_run.have_quick_quit) return;
1493 if (0 == evaluate())
1495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1496 "Test succeeded before end of duration\n");
1497 if (NULL != post_test_task) GNUNET_SCHEDULER_cancel (post_test_task);
1498 post_test_task = GNUNET_SCHEDULER_add_now (&post_test_op, NULL);
1499 GNUNET_assert (NULL != post_test_task);
1505 profiler_reply_handle_info (void *cls,
1506 const struct GNUNET_PeerIdentity *recv_peer,
1508 uint32_t num_observed);
1511 * Request random peers.
1514 request_peers (void *cls)
1516 struct PendingRequest *pending_req = cls;
1517 struct RPSPeer *rps_peer;
1518 struct PendingReply *pending_rep;
1520 rps_peer = pending_req->rps_peer;
1521 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
1522 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1523 rps_peer->pending_req_tail,
1525 rps_peer->num_pending_reqs--;
1526 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test) return;
1527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1528 "Requesting one peer\n");
1529 pending_rep = GNUNET_new (struct PendingReply);
1530 pending_rep->rps_peer = rps_peer;
1531 //pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
1533 // cur_test_run.reply_handle,
1535 pending_rep->req_handle = GNUNET_RPS_request_peer_info (rps_peer->rps_handle,
1536 profiler_reply_handle_info,
1538 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
1539 rps_peer->pending_rep_tail,
1541 rps_peer->num_pending_reps++;
1546 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
1547 * issued, nor replied
1550 schedule_missing_requests (struct RPSPeer *rps_peer)
1553 struct PendingRequest *pending_req;
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1556 "Scheduling %u - %u missing requests\n",
1557 rps_peer->num_ids_to_request,
1558 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
1559 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
1560 rps_peer->num_ids_to_request);
1561 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
1562 i < rps_peer->num_ids_to_request; i++)
1564 pending_req = GNUNET_new (struct PendingRequest);
1565 pending_req->rps_peer = rps_peer;
1566 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
1567 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1568 cur_test_run.request_interval * i),
1571 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
1572 rps_peer->pending_req_tail,
1574 rps_peer->num_pending_reqs++;
1579 cancel_pending_req_rep (struct RPSPeer *rps_peer)
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1582 "Cancelling all (pending) requests.\n");
1583 while (NULL != rps_peer->pending_req_head)
1584 cancel_pending_req (rps_peer->pending_req_head);
1585 GNUNET_assert (0 == rps_peer->num_pending_reqs);
1586 while (NULL != rps_peer->pending_rep_head)
1587 cancel_request (rps_peer->pending_rep_head);
1588 GNUNET_assert (0 == rps_peer->num_pending_reps);
1591 /***********************************
1593 ***********************************/
1596 * Initialise only non-mal RPSPeers
1598 static void mal_init_peer (struct RPSPeer *rps_peer)
1600 if (rps_peer->index >= round (portion * num_peers))
1601 rps_peer->num_ids_to_request = 1;
1606 * @brief Set peers to (non-)malicious before execution
1608 * Of signature #PreTest
1610 * @param rps_peer the peer to set (non-) malicious
1611 * @param h the handle to the service
1614 mal_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
1616 #if ENABLE_MALICIOUS
1617 uint32_t num_mal_peers;
1619 GNUNET_assert ( (1 >= portion) &&
1621 num_mal_peers = round (portion * num_peers);
1623 if (rps_peer->index < num_mal_peers)
1625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1626 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
1628 GNUNET_i2s (rps_peer->peer_id),
1631 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
1632 rps_peer_ids, target_peer);
1634 #endif /* ENABLE_MALICIOUS */
1638 mal_cb (struct RPSPeer *rps_peer)
1640 uint32_t num_mal_peers;
1642 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1647 #if ENABLE_MALICIOUS
1648 GNUNET_assert ( (1 >= portion) &&
1650 num_mal_peers = round (portion * num_peers);
1652 if (rps_peer->index >= num_mal_peers)
1653 { /* It's useless to ask a malicious peer about a random sample -
1654 it's not sampling */
1655 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1656 seed_peers, rps_peer);
1657 schedule_missing_requests (rps_peer);
1659 #endif /* ENABLE_MALICIOUS */
1662 /***********************************
1664 ***********************************/
1670 * @brief Starts churn
1672 * Has signature of #MainTest
1674 * This is not implemented too nicely as this is called for each peer, but we
1675 * only need to call it once. (Yes we check that we only schedule the task
1678 * @param rps_peer The peer it's called for
1681 churn_test_cb (struct RPSPeer *rps_peer)
1683 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1689 if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
1691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1692 "Starting churn task\n");
1693 churn_task = GNUNET_SCHEDULER_add_delayed (
1694 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1699 "Not starting churn task\n");
1702 schedule_missing_requests (rps_peer);
1705 /***********************************
1707 ***********************************/
1710 * Callback to be called when RPS service is started or stopped at peers
1713 * @param op the operation handle
1714 * @param emsg NULL on success; otherwise an error description
1717 churn_cb (void *cls,
1718 struct GNUNET_TESTBED_Operation *op,
1722 struct OpListEntry *entry = cls;
1725 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1730 GNUNET_TESTBED_operation_done (entry->op);
1733 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1734 GNUNET_SCHEDULER_shutdown ();
1737 GNUNET_assert (0 != entry->delta);
1739 num_peers_online += entry->delta;
1741 if (PEER_GO_OFFLINE == entry->delta)
1742 { /* Peer hopefully just went offline */
1743 if (GNUNET_YES != rps_peers[entry->index].online)
1745 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1746 "peer %s was expected to go offline but is still marked as online\n",
1747 GNUNET_i2s (rps_peers[entry->index].peer_id));
1752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1753 "peer %s probably went offline as expected\n",
1754 GNUNET_i2s (rps_peers[entry->index].peer_id));
1756 rps_peers[entry->index].online = GNUNET_NO;
1759 else if (PEER_GO_ONLINE < entry->delta)
1760 { /* Peer hopefully just went online */
1761 if (GNUNET_NO != rps_peers[entry->index].online)
1763 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1764 "peer %s was expected to go online but is still marked as offline\n",
1765 GNUNET_i2s (rps_peers[entry->index].peer_id));
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1771 "peer %s probably went online as expected\n",
1772 GNUNET_i2s (rps_peers[entry->index].peer_id));
1773 if (NULL != cur_test_run.pre_test)
1775 cur_test_run.pre_test (&rps_peers[entry->index],
1776 rps_peers[entry->index].rps_handle);
1777 schedule_missing_requests (&rps_peers[entry->index]);
1780 rps_peers[entry->index].online = GNUNET_YES;
1784 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1785 "Invalid value for delta: %i\n", entry->delta);
1789 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1790 rps_peers[entry->index].entry_op_manage = NULL;
1791 GNUNET_free (entry);
1792 //if (num_peers_in_round[current_round] == peers_running)
1797 * @brief Set the rps-service up or down for a specific peer
1799 * @param i index of action
1800 * @param j index of peer
1801 * @param delta (#PEER_ONLINE_DELTA) down (-1) or up (1)
1802 * @param prob_go_on_off the probability of the action
1805 manage_service_wrapper (unsigned int i, unsigned int j,
1806 enum PEER_ONLINE_DELTA delta,
1807 double prob_go_on_off)
1809 struct OpListEntry *entry = NULL;
1812 /* make sure that management operation is not already scheduled */
1813 if (NULL != rps_peers[j].entry_op_manage)
1818 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1821 "%u. selected peer (%u: %s) is %s.\n",
1824 GNUNET_i2s (rps_peers[j].peer_id),
1825 (PEER_GO_ONLINE == delta) ? "online" : "offline");
1826 if (prob < prob_go_on_off * UINT32_MAX)
1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1830 GNUNET_i2s (rps_peers[j].peer_id),
1831 (PEER_GO_OFFLINE == delta) ? "offline" : "online");
1833 if (PEER_GO_OFFLINE == delta)
1834 cancel_pending_req_rep (&rps_peers[j]);
1835 entry = make_oplist_entry ();
1836 entry->delta = delta;
1838 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1843 (PEER_GO_OFFLINE == delta) ? 0 : 1);
1844 rps_peers[j].entry_op_manage = entry;
1854 double portion_online;
1855 unsigned int *permut;
1856 double prob_go_offline;
1857 double portion_go_online;
1858 double portion_go_offline;
1861 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1866 "Churn function executing\n");
1868 churn_task = NULL; /* Should be invalid by now */
1870 /* Compute the probability for an online peer to go offline
1872 portion_online = num_peers_online * 1.0 / num_peers;
1873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1874 "Portion online: %f\n",
1876 portion_go_online = ((1 - portion_online) * .5 * .66);
1877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1878 "Portion that should go online: %f\n",
1880 portion_go_offline = (portion_online + portion_go_online) - .75;
1881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1882 "Portion that probably goes offline: %f\n",
1883 portion_go_offline);
1884 prob_go_offline = portion_go_offline / (portion_online * .5);
1885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1886 "Probability of a selected online peer to go offline: %f\n",
1889 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1890 (unsigned int) num_peers);
1892 /* Go over 50% randomly chosen peers */
1893 for (i = 0; i < .5 * num_peers; i++)
1897 /* If online, shut down with certain probability */
1898 if (GNUNET_YES == rps_peers[j].online)
1900 manage_service_wrapper (i, j, -1, prob_go_offline);
1903 /* If offline, restart with certain probability */
1904 else if (GNUNET_NO == rps_peers[j].online)
1906 manage_service_wrapper (i, j, 1, 0.66);
1910 GNUNET_free (permut);
1912 churn_task = GNUNET_SCHEDULER_add_delayed (
1913 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1920 * Initialise given RPSPeer
1922 static void profiler_init_peer (struct RPSPeer *rps_peer)
1924 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer shall request %i peers\n",
1926 rps_peer->num_ids_to_request);
1931 * Callback to call on receipt of a reply
1933 * @param cls closure
1934 * @param n number of peers
1935 * @param recv_peers the received peers
1938 profiler_reply_handle (void *cls,
1940 const struct GNUNET_PeerIdentity *recv_peers)
1942 struct RPSPeer *rps_peer;
1943 struct RPSPeer *rcv_rps_peer;
1944 char file_name_buf[128];
1945 char file_name_dh_buf[128];
1946 char file_name_dhr_buf[128];
1947 char file_name_dhru_buf[128];
1948 char *file_name = file_name_buf;
1949 char *file_name_dh = file_name_dh_buf;
1950 char *file_name_dhr = file_name_dhr_buf;
1951 char *file_name_dhru = file_name_dhru_buf;
1953 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1955 pending_rep->req_handle = NULL;
1956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "profiler_reply_handle()\n");
1957 rps_peer = pending_rep->rps_peer;
1958 (void) GNUNET_asprintf (&file_name,
1959 "/tmp/rps/received_ids-%u",
1962 (void) GNUNET_asprintf (&file_name_dh,
1963 "/tmp/rps/diehard_input-%u",
1965 (void) GNUNET_asprintf (&file_name_dhr,
1966 "/tmp/rps/diehard_input_raw-%u",
1968 (void) GNUNET_asprintf (&file_name_dhru,
1969 "/tmp/rps/diehard_input_raw_aligned-%u",
1971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1972 "[%s] got %" PRIu64 " peers:\n",
1973 GNUNET_i2s (rps_peer->peer_id),
1975 for (i = 0; i < n; i++)
1977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1980 GNUNET_i2s (&recv_peers[i]));
1983 GNUNET_i2s_full (&recv_peers[i]));
1984 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1985 GNUNET_assert (NULL != rcv_rps_peer);
1986 tofile (file_name_dh,
1988 (uint32_t) rcv_rps_peer->index);
1990 to_file_raw (file_name_dhr,
1991 (char *) &rcv_rps_peer->index,
1993 to_file_raw_unaligned (file_name_dhru,
1994 (char *) &rcv_rps_peer->index,
1997 #endif /* TO_FILE */
1999 default_reply_handle (cls, n, recv_peers);
2004 * Callback to call on receipt of a reply
2006 * @param cls closure
2007 * @param n number of peers
2008 * @param recv_peers the received peers
2011 profiler_reply_handle_info (void *cls,
2012 const struct GNUNET_PeerIdentity *recv_peer,
2014 uint32_t num_observed)
2016 struct RPSPeer *rps_peer;
2017 struct RPSPeer *rcv_rps_peer;
2018 char file_name_buf[128];
2019 char file_name_dh_buf[128];
2020 char file_name_dhr_buf[128];
2021 char file_name_dhru_buf[128];
2022 char *file_name = file_name_buf;
2023 char *file_name_dh = file_name_dh_buf;
2024 char *file_name_dhr = file_name_dhr_buf;
2025 char *file_name_dhru = file_name_dhru_buf;
2027 struct PendingReply *pending_rep = (struct PendingReply *) cls;
2029 pending_rep->req_handle = NULL;
2030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "profiler_reply_handle()\n");
2031 rps_peer = pending_rep->rps_peer;
2032 (void) GNUNET_asprintf (&file_name,
2033 "/tmp/rps/received_ids-%u",
2036 (void) GNUNET_asprintf (&file_name_dh,
2037 "/tmp/rps/diehard_input-%u",
2039 (void) GNUNET_asprintf (&file_name_dhr,
2040 "/tmp/rps/diehard_input_raw-%u",
2042 (void) GNUNET_asprintf (&file_name_dhru,
2043 "/tmp/rps/diehard_input_raw_aligned-%u",
2045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2046 "[%s] got peer with info:\n",
2047 GNUNET_i2s (rps_peer->peer_id));
2048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2050 GNUNET_i2s (recv_peer));
2052 "%s %d %" PRIu32 " \n",
2053 GNUNET_i2s_full (recv_peer),
2056 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, recv_peer);
2057 GNUNET_assert (NULL != rcv_rps_peer);
2058 tofile (file_name_dh,
2060 (uint32_t) rcv_rps_peer->index);
2062 to_file_raw (file_name_dhr,
2063 (char *) &rcv_rps_peer->index,
2065 to_file_raw_unaligned (file_name_dhru,
2066 (char *) &rcv_rps_peer->index,
2069 #endif /* TO_FILE */
2070 default_reply_handle (cls, 1, recv_peer);
2075 profiler_cb (struct RPSPeer *rps_peer)
2077 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
2083 if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
2085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2086 "Starting churn task\n");
2087 churn_task = GNUNET_SCHEDULER_add_delayed (
2088 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
2092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2093 "Not starting churn task\n");
2096 /* Only request peer ids at one peer.
2097 * (It's the before-last because last one is target of the focussed attack.)
2099 if (0 < rps_peer->num_ids_to_request)
2100 schedule_missing_requests (rps_peer);
2104 * Function called from #profiler_eval with a filename.
2106 * @param cls closure
2107 * @param filename complete filename (absolute path)
2108 * @return #GNUNET_OK to continue to iterate,
2109 * #GNUNET_NO to stop iteration with no error,
2110 * #GNUNET_SYSERR to abort iteration with error!
2113 file_name_cb (void *cls, const char *filename)
2115 if (NULL != strstr (filename, "sampler_el"))
2117 struct RPS_SamplerElement *s_elem;
2118 struct GNUNET_CRYPTO_AuthKey auth_key;
2119 const char *key_char;
2123 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
2124 tofile (filename, "--------------------------\n");
2126 auth_key = string_to_auth_key (key_char);
2127 s_elem = RPS_sampler_elem_create ();
2128 RPS_sampler_elem_set (s_elem, auth_key);
2130 for (i = 0; i < num_peers; i++)
2132 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
2134 RPS_sampler_elem_destroy (s_elem);
2140 * This is run after the test finished.
2142 * Compute all perfect samples.
2145 profiler_eval (void)
2148 /* Compute perfect sample for each sampler element */
2149 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
2151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
2153 #endif /* TO_FILE */
2159 /** @brief is b in view of a?
2166 static int is_in_view (uint32_t a, uint32_t b)
2169 for (i = 0; i < rps_peers[a].cur_view_count; i++)
2171 if (0 == memcmp (rps_peers[b].peer_id,
2172 &rps_peers[a].cur_view[i],
2173 sizeof (struct GNUNET_PeerIdentity)))
2181 static uint32_t get_idx_of_pid (const struct GNUNET_PeerIdentity *pid)
2185 for (i = 0; i < num_peers; i++)
2187 if (0 == memcmp (pid,
2188 rps_peers[i].peer_id,
2189 sizeof (struct GNUNET_PeerIdentity)))
2194 //return 0; /* Should not happen - make compiler happy */
2195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2196 "No known _PeerIdentity %s!\n",
2197 GNUNET_i2s_full (pid));
2202 * @brief Counts number of peers in view of a that have b in their view
2209 static uint32_t count_containing_views (uint32_t a, uint32_t b)
2215 for (i = 0; i < rps_peers[a].cur_view_count; i++)
2217 peer_idx = get_idx_of_pid (&rps_peers[a].cur_view[i]);
2218 if (GNUNET_YES == is_in_view (peer_idx, b))
2227 * @brief Computes the probability for each other peer to be selected by the
2228 * sampling process based on the views of all peers
2230 * @param peer_idx index of the peer that is about to sample
2232 static void compute_probabilities (uint32_t peer_idx)
2234 //double probs[num_peers] = { 0 };
2235 double probs[num_peers];
2236 double probs_hist[num_peers]; /* Probability respecting the history */
2237 size_t probs_as_str_size = (num_peers * 10 + 2) * sizeof (char);
2238 char *probs_as_str = GNUNET_malloc (probs_as_str_size);
2239 char *probs_as_str_cpy;
2244 uint32_t cont_views;
2245 uint32_t number_of_being_in_pull_events;
2247 double sum_non_zero_prob = 0;
2248 double sum_non_zero_prob_hist = 0;
2250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2251 "Computing probabilities for peer %" PRIu32 "\n", peer_idx);
2252 /* Firstly without knowledge of old views */
2253 for (i = 0; i < num_peers; i++)
2255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2256 "\tfor peer %" PRIu32 ":\n", i);
2257 view_size = rps_peers[i].cur_view_count;
2258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2259 "\t\tview_size: %" PRIu32 "\n", view_size);
2260 /* For peer i the probability of being sampled is
2261 * evenly distributed among all possibly observed peers. */
2262 /* We could have observed a peer in three cases:
2263 * 1. peer sent a push
2264 * 2. peer was contained in a pull reply
2265 * 3. peer was in history (sampler) - ignored for now */
2266 /* 1. Probability of having received a push from peer i */
2267 if ((GNUNET_YES == is_in_view (i, peer_idx)) &&
2268 (1 <= (0.45 * view_size)))
2270 if (0 == binom (view_size, 0.45 * view_size)) prob_push = 0;
2273 prob_push = 1.0 * binom (0.45 * view_size, 1)
2275 binom (view_size, 0.45 * view_size);
2277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2278 "\t\t%" PRIu32 " is in %" PRIu32 "'s view, prob: %f\n",
2282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2283 "\t\tposs choices from view: %" PRIu32 ", containing i: %" PRIu32 "\n",
2284 binom (view_size, 0.45 * view_size),
2285 binom (0.45 * view_size, 1));
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2289 "\t\t%" PRIu32 " is not in %" PRIu32 "'s view, prob: 0\n",
2293 /* 2. Probability of peer i being contained in pulls */
2294 view_size = rps_peers[peer_idx].cur_view_count;
2295 cont_views = count_containing_views (peer_idx, i);
2296 number_of_being_in_pull_events =
2297 (binom (view_size, 0.45 * view_size) -
2298 binom (view_size - cont_views, 0.45 * view_size));
2299 if (0 != number_of_being_in_pull_events)
2301 prob_pull = number_of_being_in_pull_events
2303 (1.0 * binom (view_size, 0.45 * view_size));
2308 probs[i] = prob_push + prob_pull - (prob_push * prob_pull);
2309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2310 "\t\t%" PRIu32 " has %" PRIu32 " of %" PRIu32
2311 " peers in its view who know %" PRIu32 " prob: %f\n",
2317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2318 "\t\tnumber of possible pull combinations: %" PRIu32 "\n",
2319 binom (view_size, 0.45 * view_size));
2320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2321 "\t\tnumber of possible pull combinations without %" PRIu32
2324 binom (view_size - cont_views, 0.45 * view_size));
2325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2326 "\t\tnumber of possible pull combinations with %" PRIu32
2329 number_of_being_in_pull_events);
2331 probs_hist[i] = 0.9 * rps_peers[peer_idx].eval_probs_cache[i] + probs[i];
2332 rps_peers[peer_idx].eval_probs_cache[i] = probs_hist[i];
2334 sum_non_zero_prob += probs[i];
2335 sum_non_zero_prob_hist += probs_hist[i];
2338 if (0 != sum_non_zero_prob)
2340 for (i = 0; i < num_peers; i++)
2342 probs[i] = probs[i] * (1.0 / sum_non_zero_prob);
2345 if (0 != sum_non_zero_prob_hist)
2347 for (i = 0; i < num_peers; i++)
2349 probs_hist[i] = probs_hist[i] * (1.0 / sum_non_zero_prob_hist);
2354 for (i = 0; i < num_peers; i++)
2356 probs_as_str_cpy = GNUNET_strndup (probs_as_str, probs_as_str_size);
2357 tmp = GNUNET_snprintf (probs_as_str,
2359 "%s %7.6f", probs_as_str_cpy, probs[i]);
2360 GNUNET_free (probs_as_str_cpy);
2361 GNUNET_assert (0 <= tmp);
2364 to_file_w_len (rps_peers[peer_idx].file_name_probs,
2368 probs_as_str[0] = '\0';
2369 for (i = 0; i < num_peers; i++)
2371 probs_as_str_cpy = GNUNET_strndup (probs_as_str, probs_as_str_size);
2372 tmp = GNUNET_snprintf (probs_as_str,
2374 "%s %7.6f", probs_as_str_cpy, probs_hist[i]);
2375 GNUNET_free (probs_as_str_cpy);
2376 GNUNET_assert (0 <= tmp);
2379 to_file_w_len (rps_peers[peer_idx].file_name_probs_hist,
2382 GNUNET_free (probs_as_str);
2386 * @brief This counts the number of peers in which views a given peer occurs.
2388 * It also stores this value in the rps peer.
2390 * @param peer_idx the index of the peer to count the representation
2392 * @return the number of occurrences
2394 static uint32_t count_peer_in_views_2 (uint32_t peer_idx)
2399 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2401 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2403 if (0 == memcmp (rps_peers[peer_idx].peer_id,
2404 &rps_peers[i].cur_view[j],
2405 sizeof (struct GNUNET_PeerIdentity)))
2412 rps_peers[peer_idx].count_in_views = count;
2416 static uint32_t cumulated_view_sizes ()
2421 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2423 view_sizes += rps_peers[i].cur_view_count;
2428 static void count_peer_in_views (uint32_t *count_peers)
2432 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2434 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2436 if (0 == memcmp (rps_peers[i].peer_id,
2437 &rps_peers[i].cur_view[j],
2438 sizeof (struct GNUNET_PeerIdentity)))
2446 void compute_diversity ()
2449 /* ith entry represents the numer of occurrences in other peer's views */
2450 uint32_t *count_peers = GNUNET_new_array (num_peers, uint32_t);
2451 uint32_t views_total_size;
2453 /* deviation from expected number of peers */
2454 double *deviation = GNUNET_new_array (num_peers, double);
2456 views_total_size = 0;
2459 /* For each peer count its representation in other peer's views*/
2460 for (i = 0; i < num_peers; i++) /* Peer to count */
2462 views_total_size += rps_peers[i].cur_view_count;
2463 count_peer_in_views (count_peers);
2464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2465 "Counted representation of %" PRIu32 "th peer [%s]: %" PRIu32"\n",
2467 GNUNET_i2s (rps_peers[i].peer_id),
2471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2472 "size of all views combined: %" PRIu32 "\n",
2474 expected = ((double) 1/num_peers) * views_total_size;
2475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2476 "Expected number of occurrences of each peer in all views: %f\n",
2478 for (i = 0; i < num_peers; i++) /* Peer to count */
2480 deviation[i] = expected - count_peers[i];
2481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2482 "Deviation from expectation: %f\n", deviation[i]);
2484 GNUNET_free (count_peers);
2485 GNUNET_free (deviation);
2488 void print_view_sizes()
2492 for (i = 0; i < num_peers; i++) /* Peer to count */
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2495 "View size of %" PRIu32 ". [%s] is %" PRIu32 "\n",
2497 GNUNET_i2s (rps_peers[i].peer_id),
2498 rps_peers[i].cur_view_count);
2502 void all_views_updated_cb()
2504 compute_diversity();
2508 void view_update_cb (void *cls,
2510 const struct GNUNET_PeerIdentity *peers)
2512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2513 "View was updated (%" PRIu64 ")\n", view_size);
2514 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
2515 to_file ("/tmp/rps/view_sizes.txt",
2516 "%" PRIu64 " %" PRIu32 "",
2519 for (uint64_t i = 0; i < view_size; i++)
2521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2522 "\t%s\n", GNUNET_i2s (&peers[i]));
2524 GNUNET_array_grow (rps_peer->cur_view,
2525 rps_peer->cur_view_count,
2527 //*rps_peer->cur_view = *peers;
2528 GNUNET_memcpy (rps_peer->cur_view,
2530 view_size * sizeof (struct GNUNET_PeerIdentity));
2531 to_file ("/tmp/rps/count_in_views.txt",
2532 "%" PRIu64 " %" PRIu32 "",
2534 count_peer_in_views_2 (rps_peer->index));
2535 cumulated_view_sizes();
2538 to_file ("/tmp/rps/repr.txt",
2539 "%" PRIu64 /* index */
2540 " %" PRIu32 /* occurrence in views */
2541 " %" PRIu32 /* view sizes */
2542 " %f" /* fraction of repr in views */
2543 " %f" /* average view size */
2544 " %f" /* prob of occurrence in view slot */
2545 " %f" "", /* exp frac of repr in views */
2547 count_peer_in_views_2 (rps_peer->index),
2549 count_peer_in_views_2 (rps_peer->index) / (view_size * 1.0), /* fraction of representation in views */
2550 view_sizes / (view_size * 1.0), /* average view size */
2551 1.0 /view_size, /* prob of occurrence in view slot */
2552 (1.0/view_size) * (view_sizes/view_size) /* expected fraction of repr in views */
2555 compute_probabilities (rps_peer->index);
2556 all_views_updated_cb();
2560 pre_profiler (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
2562 rps_peer->file_name_probs =
2563 store_prefix_file_name (rps_peer->index, "probs");
2564 rps_peer->file_name_probs_hist =
2565 store_prefix_file_name (rps_peer->index, "probs_hist");
2566 GNUNET_RPS_view_request (h, 0, view_update_cb, rps_peer);
2569 void write_final_stats (void){
2570 uint64_t sums[STAT_TYPE_MAX] = { 0 };
2572 for (uint32_t i = 0; i < num_peers; i++)
2574 to_file ("/tmp/rps/final_stats.csv",
2575 "%" PRIu32 ", " /* index */
2577 PRIu64 ", %" /* rounds */
2578 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* blocking */
2579 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* issued */
2580 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* sent */
2581 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* recv */
2582 PRIu64 ", %" /* view size */
2583 PRIu64 ", %" /* known peers */
2584 PRIu64 ", %" /* valid peers */
2585 PRIu64 ", %" /* learned peers */
2586 PRIu64 ", %" /* pending online checks */
2587 PRIu64 ", %" /* unrequested pull replies */
2588 PRIu64 ", %" /* peers in push map */
2589 PRIu64 ", %" /* peers in pull map */
2590 PRIu64 ", %" /* peers in view */
2591 PRIu64 "\n"/* view size aim */,
2593 GNUNET_i2s (rps_peers[i].peer_id),
2594 rps_peers[i].stats[STAT_TYPE_ROUNDS],
2595 rps_peers[i].stats[STAT_TYPE_BLOCKS],
2596 rps_peers[i].stats[STAT_TYPE_BLOCKS_MANY_PUSH],
2597 rps_peers[i].stats[STAT_TYPE_BLOCKS_NO_PUSH],
2598 rps_peers[i].stats[STAT_TYPE_BLOCKS_NO_PULL],
2599 rps_peers[i].stats[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL],
2600 rps_peers[i].stats[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL],
2601 rps_peers[i].stats[STAT_TYPE_ISSUED_PUSH_SEND],
2602 rps_peers[i].stats[STAT_TYPE_ISSUED_PUSH_SEND_MH],
2603 rps_peers[i].stats[STAT_TYPE_ISSUED_PULL_REQ],
2604 rps_peers[i].stats[STAT_TYPE_ISSUED_PULL_REQ_MH],
2605 rps_peers[i].stats[STAT_TYPE_ISSUED_PULL_REP],
2606 rps_peers[i].stats[STAT_TYPE_SENT_PUSH_SEND],
2607 rps_peers[i].stats[STAT_TYPE_SENT_PULL_REQ],
2608 rps_peers[i].stats[STAT_TYPE_SENT_PULL_REQ_MH],
2609 rps_peers[i].stats[STAT_TYPE_SENT_PULL_REP],
2610 rps_peers[i].stats[STAT_TYPE_RECV_PUSH_SEND],
2611 rps_peers[i].stats[STAT_TYPE_RECV_PUSH_SEND_MH],
2612 rps_peers[i].stats[STAT_TYPE_RECV_PULL_REQ],
2613 rps_peers[i].stats[STAT_TYPE_RECV_PULL_REQ_MH],
2614 rps_peers[i].stats[STAT_TYPE_RECV_PULL_REP_MH],
2615 rps_peers[i].stats[STAT_TYPE_RECV_PULL_REP],
2616 rps_peers[i].stats[STAT_TYPE_VIEW_SIZE],
2617 rps_peers[i].stats[STAT_TYPE_KNOWN_PEERS],
2618 rps_peers[i].stats[STAT_TYPE_VALID_PEERS],
2619 rps_peers[i].stats[STAT_TYPE_LEARND_PEERS],
2620 rps_peers[i].stats[STAT_TYPE_PENDING_ONLINE_CHECKS],
2621 rps_peers[i].stats[STAT_TYPE_UNREQUESTED_PULL_REPLIES],
2622 rps_peers[i].stats[STAT_TYPE_PEERS_IN_PUSH_MAP],
2623 rps_peers[i].stats[STAT_TYPE_PEERS_IN_PULL_MAP],
2624 rps_peers[i].stats[STAT_TYPE_PEERS_IN_VIEW],
2625 rps_peers[i].stats[STAT_TYPE_VIEW_SIZE_AIM]);
2626 for (enum STAT_TYPE stat_type = STAT_TYPE_ROUNDS;
2627 stat_type < STAT_TYPE_MAX;
2630 sums[stat_type] += rps_peers[i].stats[stat_type];
2633 to_file ("/tmp/rps/final_stats.dat",
2635 PRIu64 " %" /* rounds */
2636 PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" /* blocking */
2637 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* issued */
2638 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* sent */
2639 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* recv */
2640 PRIu64 ", %" /* view size */
2641 PRIu64 ", %" /* known peers */
2642 PRIu64 ", %" /* valid peers */
2643 PRIu64 ", %" /* learned peers */
2644 PRIu64 ", %" /* pending online checks */
2645 PRIu64 ", %" /* unrequested pull replies */
2646 PRIu64 ", %" /* peers in push map */
2647 PRIu64 ", %" /* peers in pull map */
2648 PRIu64 ", %" /* peers in view */
2649 PRIu64 "\n"/* view size aim */,
2650 sums[STAT_TYPE_ROUNDS],
2651 sums[STAT_TYPE_BLOCKS],
2652 sums[STAT_TYPE_BLOCKS_MANY_PUSH],
2653 sums[STAT_TYPE_BLOCKS_NO_PUSH],
2654 sums[STAT_TYPE_BLOCKS_NO_PULL],
2655 sums[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL],
2656 sums[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL],
2657 sums[STAT_TYPE_ISSUED_PUSH_SEND],
2658 sums[STAT_TYPE_ISSUED_PUSH_SEND_MH],
2659 sums[STAT_TYPE_ISSUED_PULL_REQ],
2660 sums[STAT_TYPE_ISSUED_PULL_REQ_MH],
2661 sums[STAT_TYPE_ISSUED_PULL_REP],
2662 sums[STAT_TYPE_SENT_PUSH_SEND],
2663 sums[STAT_TYPE_SENT_PULL_REQ],
2664 sums[STAT_TYPE_SENT_PULL_REQ_MH],
2665 sums[STAT_TYPE_SENT_PULL_REP],
2666 sums[STAT_TYPE_RECV_PUSH_SEND],
2667 sums[STAT_TYPE_RECV_PUSH_SEND_MH],
2668 sums[STAT_TYPE_RECV_PULL_REQ],
2669 sums[STAT_TYPE_RECV_PULL_REQ_MH],
2670 sums[STAT_TYPE_RECV_PULL_REP],
2671 sums[STAT_TYPE_RECV_PULL_REP_MH],
2672 sums[STAT_TYPE_VIEW_SIZE],
2673 sums[STAT_TYPE_KNOWN_PEERS],
2674 sums[STAT_TYPE_VALID_PEERS],
2675 sums[STAT_TYPE_LEARND_PEERS],
2676 sums[STAT_TYPE_PENDING_ONLINE_CHECKS],
2677 sums[STAT_TYPE_UNREQUESTED_PULL_REPLIES],
2678 sums[STAT_TYPE_PEERS_IN_PUSH_MAP],
2679 sums[STAT_TYPE_PEERS_IN_PULL_MAP],
2680 sums[STAT_TYPE_PEERS_IN_VIEW],
2681 sums[STAT_TYPE_VIEW_SIZE_AIM]);
2685 * Continuation called by #GNUNET_STATISTICS_get() functions.
2687 * Remembers that this specific statistics value was received for this peer.
2688 * Checks whether all peers received their statistics yet.
2689 * Issues the shutdown.
2691 * @param cls closure
2692 * @param success #GNUNET_OK if statistics were
2693 * successfully obtained, #GNUNET_SYSERR if not.
2696 post_test_shutdown_ready_cb (void *cls,
2699 struct STATcls *stat_cls = (struct STATcls *) cls;
2700 struct RPSPeer *rps_peer = stat_cls->rps_peer;
2702 rps_peer->h_stat_get[stat_cls->stat_type] = NULL;
2703 if (GNUNET_OK == success)
2705 /* set flag that we we got the value */
2706 rps_peer->stat_collected_flags |= BIT(stat_cls->stat_type);
2708 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2709 "Peer %u did not receive statistics value\n",
2711 GNUNET_free (stat_cls);
2716 if (NULL != rps_peer->stat_op &&
2717 GNUNET_YES == check_statistics_collect_completed_single_peer (rps_peer))
2719 GNUNET_TESTBED_operation_done (rps_peer->stat_op);
2722 write_final_stats ();
2723 if (GNUNET_YES == check_statistics_collect_completed())
2725 //write_final_stats ();
2726 GNUNET_free (stat_cls);
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2729 GNUNET_SCHEDULER_shutdown ();
2731 GNUNET_free (stat_cls);
2736 * Callback function to process statistic values.
2738 * @param cls closure
2739 * @param subsystem name of subsystem that created the statistic
2740 * @param name the name of the datum
2741 * @param value the current value
2742 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
2743 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
2746 stat_iterator (void *cls,
2747 const char *subsystem,
2752 const struct STATcls *stat_cls = (const struct STATcls *) cls;
2753 struct RPSPeer *rps_peer = (struct RPSPeer *) stat_cls->rps_peer;
2754 enum STAT_TYPE stat_type;
2756 (void) is_persistent;
2758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2759 "Got stat value: %s - %" PRIu64 " (%u)\n",
2763 to_file (rps_peer->file_name_stats,
2764 "%s: %" PRIu64 "\n",
2767 stat_type = stat_str_2_type (name);
2768 GNUNET_assert (STAT_TYPE_ROUNDS <= stat_type &&
2769 STAT_TYPE_MAX > stat_type);
2770 rps_peer->stats[stat_type] = value;
2776 post_profiler (struct RPSPeer *rps_peer)
2778 if (COLLECT_STATISTICS != cur_test_run.have_collect_statistics)
2783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2784 "Going to request statistic values with mask 0x%" PRIx32 "\n",
2785 cur_test_run.stat_collect_flags);
2787 struct STATcls *stat_cls;
2789 for (stat_type = STAT_TYPE_ROUNDS;
2790 stat_type < STAT_TYPE_MAX;
2793 if (BIT(stat_type) & cur_test_run.stat_collect_flags)
2795 stat_cls = GNUNET_malloc (sizeof (struct STATcls));
2796 stat_cls->rps_peer = rps_peer;
2797 stat_cls->stat_type = stat_type;
2798 rps_peer->file_name_stats =
2799 store_prefix_file_name (rps_peer->peer_id, "stats");
2800 rps_peer->h_stat_get[stat_type] =
2801 GNUNET_STATISTICS_get (rps_peer->stats_h,
2803 stat_type_strings [stat_type],
2804 post_test_shutdown_ready_cb,
2806 (struct STATcls *) stat_cls);
2807 GNUNET_assert (NULL != rps_peer->h_stat_get);
2808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2809 "Requested statistics for %s (peer %" PRIu32 ")\n",
2810 stat_type_strings [stat_type],
2814 GNUNET_free (rps_peer->eval_probs_cache);
2818 /***********************************************************************
2819 * /Definition of tests
2820 ***********************************************************************/
2824 * Actual "main" function for the testcase.
2826 * @param cls closure
2827 * @param h the run handle
2828 * @param n_peers number of peers in 'peers'
2829 * @param peers handle to peers run in the testbed
2830 * @param links_succeeded the number of overlay link connection attempts that
2832 * @param links_failed the number of overlay link connection attempts that
2836 test_run (void *cls,
2837 struct GNUNET_TESTBED_RunHandle *h,
2838 unsigned int n_peers,
2839 struct GNUNET_TESTBED_Peer **peers,
2840 unsigned int links_succeeded,
2841 unsigned int links_failed)
2844 struct OpListEntry *entry;
2847 (void) links_failed;
2849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
2851 /* Check whether we timed out */
2852 if (n_peers != num_peers ||
2854 0 == links_succeeded)
2856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Going down due to args (eg. timeout)\n");
2857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tn_peers: %u\n", n_peers);
2858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tnum_peers: %" PRIu32 "\n", num_peers);
2859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tpeers: %p\n", peers);
2860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tlinks_succeeded: %u\n", links_succeeded);
2862 GNUNET_SCHEDULER_shutdown ();
2867 /* Initialize peers */
2868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "going to initialise peers\n");
2869 testbed_peers = peers;
2870 num_peers_online = 0;
2871 for (i = 0; i < num_peers; i++)
2873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "initialising %u\n", i);
2874 entry = make_oplist_entry ();
2876 rps_peers[i].index = i;
2877 if (NULL != cur_test_run.init_peer)
2878 cur_test_run.init_peer (&rps_peers[i]);
2879 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
2881 rps_peers->cur_view_count = 0;
2882 rps_peers->cur_view = NULL;
2884 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
2885 GNUNET_TESTBED_PIT_IDENTITY,
2890 /* Bring peers up */
2891 GNUNET_assert (num_peers == n_peers);
2892 for (i = 0; i < n_peers; i++)
2894 rps_peers[i].index = i;
2896 GNUNET_TESTBED_service_connect (&rps_peers[i],
2899 &rps_connect_complete_cb,
2901 &rps_connect_adapter,
2902 &rps_disconnect_adapter,
2904 /* Connect all peers to statistics service */
2905 if (COLLECT_STATISTICS == cur_test_run.have_collect_statistics)
2907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2908 "Connecting to statistics service\n");
2909 rps_peers[i].stat_op =
2910 GNUNET_TESTBED_service_connect (NULL,
2915 &stat_connect_adapter,
2916 &stat_disconnect_adapter,
2921 if (NULL != churn_task)
2922 GNUNET_SCHEDULER_cancel (churn_task);
2923 post_test_task = GNUNET_SCHEDULER_add_delayed (duration, &post_test_op, NULL);
2924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout for shutdown is %lu\n", timeout.rel_value_us/1000000);
2925 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout,
2928 GNUNET_SCHEDULER_add_shutdown (shutdown_op, NULL);
2933 * Entry point for the testcase, sets up the testbed.
2935 * @param argc unused
2936 * @param argv unused
2941 const char *cfgfile,
2942 const struct GNUNET_CONFIGURATION_Handle *cfg)
2949 /* Defaults for tests */
2952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
2953 cur_test_run.name = "test-rps-profiler";
2957 cur_test_run.init_peer = profiler_init_peer;
2958 //cur_test_run.pre_test = mal_pre;
2959 cur_test_run.pre_test = pre_profiler;
2960 cur_test_run.main_test = profiler_cb;
2961 cur_test_run.reply_handle = profiler_reply_handle;
2962 cur_test_run.eval_cb = profiler_eval;
2963 cur_test_run.post_test = post_profiler;
2964 cur_test_run.request_interval = 2;
2965 if (0 == cur_test_run.num_requests) cur_test_run.num_requests = 5;
2966 //cur_test_run.have_churn = HAVE_CHURN;
2967 cur_test_run.have_churn = HAVE_NO_CHURN;
2968 cur_test_run.have_quick_quit = HAVE_QUICK_QUIT;
2969 cur_test_run.have_collect_statistics = COLLECT_STATISTICS;
2970 cur_test_run.stat_collect_flags = BIT(STAT_TYPE_ROUNDS) |
2971 BIT(STAT_TYPE_BLOCKS) |
2972 BIT(STAT_TYPE_BLOCKS_MANY_PUSH) |
2973 BIT(STAT_TYPE_BLOCKS_NO_PUSH) |
2974 BIT(STAT_TYPE_BLOCKS_NO_PULL) |
2975 BIT(STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL) |
2976 BIT(STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL) |
2977 BIT(STAT_TYPE_ISSUED_PUSH_SEND) |
2978 BIT(STAT_TYPE_ISSUED_PUSH_SEND_MH) |
2979 BIT(STAT_TYPE_ISSUED_PULL_REQ) |
2980 BIT(STAT_TYPE_ISSUED_PULL_REQ_MH) |
2981 BIT(STAT_TYPE_ISSUED_PULL_REP) |
2982 BIT(STAT_TYPE_SENT_PUSH_SEND) |
2983 BIT(STAT_TYPE_SENT_PULL_REQ) |
2984 BIT(STAT_TYPE_SENT_PULL_REQ_MH) |
2985 BIT(STAT_TYPE_SENT_PULL_REP) |
2986 BIT(STAT_TYPE_RECV_PUSH_SEND) |
2987 BIT(STAT_TYPE_RECV_PUSH_SEND_MH) |
2988 BIT(STAT_TYPE_RECV_PULL_REQ) |
2989 BIT(STAT_TYPE_RECV_PULL_REQ_MH) |
2990 BIT(STAT_TYPE_RECV_PULL_REP) |
2991 BIT(STAT_TYPE_RECV_PULL_REP_MH) |
2992 BIT(STAT_TYPE_VIEW_SIZE) |
2993 BIT(STAT_TYPE_KNOWN_PEERS) |
2994 BIT(STAT_TYPE_VALID_PEERS) |
2995 BIT(STAT_TYPE_LEARND_PEERS) |
2996 BIT(STAT_TYPE_PENDING_ONLINE_CHECKS) |
2997 BIT(STAT_TYPE_UNREQUESTED_PULL_REPLIES) |
2998 BIT(STAT_TYPE_PEERS_IN_PUSH_MAP) |
2999 BIT(STAT_TYPE_PEERS_IN_PULL_MAP) |
3000 BIT(STAT_TYPE_PEERS_IN_VIEW) |
3001 BIT(STAT_TYPE_VIEW_SIZE_AIM);
3002 cur_test_run.have_collect_view = COLLECT_VIEW;
3004 /* 'Clean' directory */
3005 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
3006 GNUNET_DISK_directory_create ("/tmp/rps/");
3007 if (0 == duration.rel_value_us)
3009 if (0 == timeout.rel_value_us)
3011 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
3012 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3014 (0.01 * num_peers));
3018 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3019 (timeout.rel_value_us/1000000)
3025 if (0 == timeout.rel_value_us)
3027 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3028 ((duration.rel_value_us/1000000)
3029 * 1.2) + (0.01 * num_peers));
3032 GNUNET_assert (duration.rel_value_us < timeout.rel_value_us);
3033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3034 "duration is %lus\n",
3035 duration.rel_value_us/1000000);
3036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3037 "timeout is %lus\n",
3038 timeout.rel_value_us/1000000);
3040 /* Compute number of bits for representing largest peer id */
3041 for (bits_needed = 1; (1 << bits_needed) < num_peers; bits_needed++)
3043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3044 "Need %u bits to represent %" PRIu32 " peers\n",
3048 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
3049 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
3050 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
3051 if ( (2 == mal_type) ||
3053 target_peer = &rps_peer_ids[num_peers - 2];
3056 GNUNET_TESTBED_run (NULL,
3067 * Entry point for the testcase, sets up the testbed.
3069 * @param argc unused
3070 * @param argv unused
3071 * @return 0 on success
3074 main (int argc, char *argv[])
3077 struct GNUNET_GETOPT_CommandLineOption options[] = {
3078 GNUNET_GETOPT_option_uint ('n',
3081 gettext_noop ("number of peers to start"),
3083 GNUNET_GETOPT_option_relative_time ('d',
3086 gettext_noop ("duration of the profiling"),
3088 GNUNET_GETOPT_option_relative_time ('t',
3091 gettext_noop ("timeout for the profiling"),
3093 GNUNET_GETOPT_option_uint ('r',
3096 gettext_noop ("number of PeerIDs to request"),
3097 &cur_test_run.num_requests),
3098 GNUNET_GETOPT_OPTION_END
3101 unsetenv ("XDG_DATA_HOME");
3102 unsetenv ("XDG_CONFIG_HOME");
3104 // GNUNET_STRINGS_get_utf8_args (argc, argv,
3109 GNUNET_PROGRAM_run (argc,
3111 "gnunet-rps-profiler",
3112 gettext_noop ("Measure quality and performance of the RPS service."),
3121 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3122 "Test did not run successfully!\n");
3126 ret_value = cur_test_run.eval_cb();
3127 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
3129 GNUNET_array_grow (rps_peers->cur_view,
3130 rps_peers->cur_view_count,
3133 GNUNET_free (rps_peers);
3134 GNUNET_free (rps_peer_ids);
3135 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
3140 /* end of test_rps.c */