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_PULL_REQ, /* 8 */
88 STAT_TYPE_ISSUED_PULL_REQ_MH, /* 9 */
89 STAT_TYPE_ISSUED_PULL_REP, /* 10 */
90 STAT_TYPE_SENT_PUSH_SEND, /* 11 */
91 STAT_TYPE_SENT_PULL_REQ, /* 12 */
92 STAT_TYPE_SENT_PULL_REQ_MH, /* 13 */
93 STAT_TYPE_SENT_PULL_REP, /* 14 */
94 STAT_TYPE_RECV_PUSH_SEND, /* 15 */
95 STAT_TYPE_RECV_PULL_REQ, /* 16 */
96 STAT_TYPE_RECV_PULL_REQ_MH, /* 17 */
97 STAT_TYPE_RECV_PULL_REP, /* 18 */
98 STAT_TYPE_RECV_PULL_REP_MH, /* 19 */
99 STAT_TYPE_VIEW_SIZE, /* 20 */
100 STAT_TYPE_KNOWN_PEERS, /* 21 */
101 STAT_TYPE_VALID_PEERS, /* 22 */
102 STAT_TYPE_LEARND_PEERS, /* 23 */
103 STAT_TYPE_PENDING_ONLINE_CHECKS, /* 24 */
104 STAT_TYPE_UNREQUESTED_PULL_REPLIES, /* 25 */
105 STAT_TYPE_PEERS_IN_PUSH_MAP, /* 26 */
106 STAT_TYPE_PEERS_IN_PULL_MAP, /* 27 */
107 STAT_TYPE_PEERS_IN_VIEW, /* 28 */
108 STAT_TYPE_VIEW_SIZE_AIM, /* 29 */
109 STAT_TYPE_MAX, /* 30 */
112 static char* stat_type_strings[] = {
115 "# rounds blocked - too many pushes",
116 "# rounds blocked - no pushes",
117 "# rounds blocked - no pull replies",
118 "# rounds blocked - too many pushes, no pull replies",
119 "# rounds blocked - no pushes, no pull replies",
120 "# push send issued",
121 "# pull request send issued",
122 "# pull request send issued (multi-hop peer)",
123 "# pull reply send issued",
125 "# pull requests sent",
126 "# pull requests sent (multi-hop peer)",
127 "# pull replys sent",
128 "# push message received",
129 "# pull request message received",
130 "# pull request message received (multi-hop peer)",
131 "# pull reply messages received",
132 "# pull reply messages received (multi-hop peer)",
137 "# pending online checks",
138 "# unrequested pull replies",
139 "# peers in push map at end of round",
140 "# peers in pull map at end of round",
141 "# peers in view at end of round",
147 struct RPSPeer *rps_peer;
148 enum STAT_TYPE stat_type;
153 * @brief Converts string representation to the corresponding #STAT_TYPE enum.
155 * @param stat_str string representation of statistics specifier
157 * @return corresponding enum
159 enum STAT_TYPE stat_str_2_type (const char *stat_str)
161 if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_NO_PULL],
163 strlen (stat_type_strings[STAT_TYPE_BLOCKS_NO_PULL])))
165 return STAT_TYPE_BLOCKS_NO_PULL;
167 else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL],
169 strlen (stat_type_strings[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL])))
171 return STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL;
173 else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_MANY_PUSH],
175 strlen (stat_type_strings[STAT_TYPE_BLOCKS_MANY_PUSH])))
177 return STAT_TYPE_BLOCKS_MANY_PUSH;
179 else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL],
181 strlen (stat_type_strings[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL])))
183 return STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL;
185 else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_NO_PUSH],
187 strlen (stat_type_strings[STAT_TYPE_BLOCKS_NO_PUSH])))
189 return STAT_TYPE_BLOCKS_NO_PUSH;
191 else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS],
193 strlen (stat_type_strings[STAT_TYPE_BLOCKS])))
195 return STAT_TYPE_BLOCKS;
197 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ROUNDS],
199 strlen (stat_type_strings[STAT_TYPE_ROUNDS])))
201 return STAT_TYPE_ROUNDS;
203 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PUSH_SEND],
205 strlen (stat_type_strings[STAT_TYPE_ISSUED_PUSH_SEND])))
207 return STAT_TYPE_ISSUED_PUSH_SEND;
209 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PULL_REQ],
211 strlen (stat_type_strings[STAT_TYPE_ISSUED_PULL_REQ])))
213 return STAT_TYPE_ISSUED_PULL_REQ;
215 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PULL_REQ_MH],
217 strlen (stat_type_strings[STAT_TYPE_ISSUED_PULL_REQ_MH])))
219 return STAT_TYPE_ISSUED_PULL_REQ_MH;
221 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PULL_REP],
223 strlen (stat_type_strings[STAT_TYPE_ISSUED_PULL_REP])))
225 return STAT_TYPE_ISSUED_PULL_REP;
227 else if (0 == strncmp (stat_type_strings[STAT_TYPE_SENT_PUSH_SEND],
229 strlen (stat_type_strings[STAT_TYPE_SENT_PUSH_SEND])))
231 return STAT_TYPE_SENT_PUSH_SEND;
233 else if (0 == strncmp (stat_type_strings[STAT_TYPE_SENT_PULL_REQ],
235 strlen (stat_type_strings[STAT_TYPE_SENT_PULL_REQ])))
237 return STAT_TYPE_SENT_PULL_REQ;
239 else if (0 == strncmp (stat_type_strings[STAT_TYPE_SENT_PULL_REQ_MH],
241 strlen (stat_type_strings[STAT_TYPE_SENT_PULL_REQ_MH])))
243 return STAT_TYPE_SENT_PULL_REQ_MH;
245 else if (0 == strncmp (stat_type_strings[STAT_TYPE_SENT_PULL_REP],
247 strlen (stat_type_strings[STAT_TYPE_SENT_PULL_REP])))
249 return STAT_TYPE_SENT_PULL_REP;
251 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PUSH_SEND],
253 strlen (stat_type_strings[STAT_TYPE_RECV_PUSH_SEND])))
255 return STAT_TYPE_RECV_PUSH_SEND;
257 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PULL_REQ],
259 strlen (stat_type_strings[STAT_TYPE_RECV_PULL_REQ])))
261 return STAT_TYPE_RECV_PULL_REQ;
263 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PULL_REQ_MH],
265 strlen (stat_type_strings[STAT_TYPE_RECV_PULL_REQ_MH])))
267 return STAT_TYPE_RECV_PULL_REQ_MH;
269 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PULL_REP],
271 strlen (stat_type_strings[STAT_TYPE_RECV_PULL_REP])))
273 return STAT_TYPE_RECV_PULL_REP;
275 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PULL_REP_MH],
277 strlen (stat_type_strings[STAT_TYPE_RECV_PULL_REP_MH])))
279 return STAT_TYPE_RECV_PULL_REP_MH;
281 else if (0 == strncmp (stat_type_strings[STAT_TYPE_VIEW_SIZE],
283 strlen (stat_type_strings[STAT_TYPE_VIEW_SIZE])))
285 return STAT_TYPE_VIEW_SIZE;
287 else if (0 == strncmp (stat_type_strings[STAT_TYPE_KNOWN_PEERS],
289 strlen (stat_type_strings[STAT_TYPE_KNOWN_PEERS])))
291 return STAT_TYPE_KNOWN_PEERS;
293 else if (0 == strncmp (stat_type_strings[STAT_TYPE_VALID_PEERS],
295 strlen (stat_type_strings[STAT_TYPE_VALID_PEERS])))
297 return STAT_TYPE_VALID_PEERS;
299 else if (0 == strncmp (stat_type_strings[STAT_TYPE_LEARND_PEERS],
301 strlen (stat_type_strings[STAT_TYPE_LEARND_PEERS])))
303 return STAT_TYPE_LEARND_PEERS;
305 else if (0 == strncmp (stat_type_strings[STAT_TYPE_PENDING_ONLINE_CHECKS],
307 strlen (stat_type_strings[STAT_TYPE_PENDING_ONLINE_CHECKS])))
309 return STAT_TYPE_PENDING_ONLINE_CHECKS;
311 else if (0 == strncmp (stat_type_strings[STAT_TYPE_UNREQUESTED_PULL_REPLIES],
313 strlen (stat_type_strings[STAT_TYPE_UNREQUESTED_PULL_REPLIES])))
315 return STAT_TYPE_UNREQUESTED_PULL_REPLIES;
317 else if (0 == strncmp (stat_type_strings[STAT_TYPE_PEERS_IN_PUSH_MAP],
319 strlen (stat_type_strings[STAT_TYPE_PEERS_IN_PUSH_MAP])))
321 return STAT_TYPE_PEERS_IN_PUSH_MAP;
323 else if (0 == strncmp (stat_type_strings[STAT_TYPE_PEERS_IN_PULL_MAP],
325 strlen (stat_type_strings[STAT_TYPE_PEERS_IN_PULL_MAP])))
327 return STAT_TYPE_PEERS_IN_PULL_MAP;
329 else if (0 == strncmp (stat_type_strings[STAT_TYPE_PEERS_IN_VIEW],
331 strlen (stat_type_strings[STAT_TYPE_PEERS_IN_VIEW])))
333 return STAT_TYPE_PEERS_IN_VIEW;
335 else if (0 == strncmp (stat_type_strings[STAT_TYPE_VIEW_SIZE_AIM],
337 strlen (stat_type_strings[STAT_TYPE_VIEW_SIZE_AIM])))
339 return STAT_TYPE_VIEW_SIZE_AIM;
341 return STAT_TYPE_MAX;
346 * @brief Indicates whether peer should go off- or online
348 enum PEER_ONLINE_DELTA {
350 * @brief Indicates peer going online
354 * @brief Indicates peer going offline
356 PEER_GO_OFFLINE = -1,
360 * Operation map entry
367 struct OpListEntry *next;
372 struct OpListEntry *prev;
375 * The testbed operation
377 struct GNUNET_TESTBED_Operation *op;
380 * Depending on whether we start or stop RPS service at the peer, set this to
381 * #PEER_GO_ONLINE (1) or #PEER_GO_OFFLINE (-1)
383 enum PEER_ONLINE_DELTA delta;
386 * Index of the regarding peer
394 static struct OpListEntry *oplist_head;
399 static struct OpListEntry *oplist_tail;
403 * A pending reply: A request was sent and the reply is pending.
410 struct PendingReply *next;
411 struct PendingReply *prev;
414 * Handle to the request we are waiting for
416 struct GNUNET_RPS_Request_Handle *req_handle;
419 * The peer that requested
421 struct RPSPeer *rps_peer;
426 * A pending request: A request was not made yet but is scheduled for later.
428 struct PendingRequest
433 struct PendingRequest *next;
434 struct PendingRequest *prev;
437 * Handle to the request we are waiting for
439 struct GNUNET_SCHEDULER_Task *request_task;
442 * The peer that requested
444 struct RPSPeer *rps_peer;
449 * Information we track for each peer.
459 * Handle for RPS connect operation.
461 struct GNUNET_TESTBED_Operation *op;
464 * Handle to RPS service.
466 struct GNUNET_RPS_Handle *rps_handle;
471 struct GNUNET_PeerIdentity *peer_id;
474 * A request handle to check for an request
476 //struct GNUNET_RPS_Request_Handle *req_handle;
479 * Peer on- or offline?
484 * Number of Peer IDs to request during the whole test
486 unsigned int num_ids_to_request;
489 * Pending requests DLL
491 struct PendingRequest *pending_req_head;
492 struct PendingRequest *pending_req_tail;
495 * Number of pending requests
497 unsigned int num_pending_reqs;
500 * Pending replies DLL
502 struct PendingReply *pending_rep_head;
503 struct PendingReply *pending_rep_tail;
506 * Number of pending replies
508 unsigned int num_pending_reps;
511 * Number of received PeerIDs
513 unsigned int num_recv_ids;
516 * Pending operation on that peer
518 const struct OpListEntry *entry_op_manage;
521 * Testbed operation to connect to statistics service
523 struct GNUNET_TESTBED_Operation *stat_op;
526 * Handle to the statistics service
528 struct GNUNET_STATISTICS_Handle *stats_h;
531 * @brief flags to indicate which statistics values have been already
532 * collected from the statistics service.
533 * Used to check whether we are able to shutdown.
535 uint32_t stat_collected_flags;
538 * @brief File name of the file the stats are finally written to
540 const char *file_name_stats;
543 * @brief File name of the file the stats are finally written to
545 const char *file_name_probs;
548 * @brief The current view
550 struct GNUNET_PeerIdentity *cur_view;
553 * @brief Number of peers in the #cur_view.
555 uint32_t cur_view_count;
558 * @brief Number of occurrences in other peer's view
560 uint32_t count_in_views;
563 * @brief statistics values
565 uint64_t stats[STAT_TYPE_MAX];
567 * @brief Handle for the statistics get request
569 struct GNUNET_STATISTICS_GetHandle *h_stat_get[STAT_TYPE_MAX];
573 * Information for all the peers.
575 static struct RPSPeer *rps_peers;
578 * Peermap to get the index of a given peer ID quick.
580 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
585 static struct GNUNET_PeerIdentity *rps_peer_ids;
588 * ID of the targeted peer.
590 static struct GNUNET_PeerIdentity *target_peer;
593 * Number of online peers.
595 static unsigned int num_peers_online;
598 * @brief The added sizes of the peer's views
600 static unsigned int view_sizes;
603 * Return value from 'main'.
608 * Identifier for the task that runs after the test to collect results
610 static struct GNUNET_SCHEDULER_Task *post_test_task;
613 * Identifier for the shutdown task
615 static struct GNUNET_SCHEDULER_Task *shutdown_task;
619 * Identifier for the churn task that runs periodically
621 static struct GNUNET_SCHEDULER_Task *churn_task;
624 * Called to initialise the given RPSPeer
626 typedef void (*InitPeer) (struct RPSPeer *rps_peer);
629 * @brief Called directly after connecting to the service
631 * @param rps_peer Specific peer the function is called on
632 * @param h the handle to the rps service
634 typedef void (*PreTest) (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h);
637 * @brief Executes functions to test the api/service for a given peer
639 * Called from within #rps_connect_complete_cb ()
640 * Implemented by #churn_test_cb, #profiler_cb, #mal_cb, #single_req_cb,
641 * #delay_req_cb, #seed_big_cb, #single_peer_seed_cb, #seed_cb, #req_cancel_cb
643 * @param rps_peer the peer the task runs on
645 typedef void (*MainTest) (struct RPSPeer *rps_peer);
648 * Callback called once the requested random peers are available
650 typedef void (*ReplyHandle) (void *cls,
652 const struct GNUNET_PeerIdentity *recv_peers);
655 * Called directly before disconnecting from the service
657 typedef void (*PostTest) (struct RPSPeer *peer);
660 * Function called after disconnect to evaluate test success
662 typedef int (*EvaluationCallback) (void);
665 * @brief Do we have Churn?
669 * @brief If we have churn this is set
673 * @brief If we have no churn this is set
679 * @brief Is it ok to quit the test before the timeout?
681 enum OPTION_QUICK_QUIT {
683 * @brief It is ok for the test to quit before the timeout triggers
688 * @brief It is NOT ok for the test to quit before the timeout triggers
694 * @brief Do we collect statistics at the end?
696 enum OPTION_COLLECT_STATISTICS {
698 * @brief We collect statistics at the end
703 * @brief We do not collect statistics at the end
705 NO_COLLECT_STATISTICS,
709 * @brief Do we collect views during run?
711 enum OPTION_COLLECT_VIEW {
713 * @brief We collect view during run
718 * @brief We do not collect the view during run
724 * Structure to define a single test
734 * Called with a single peer in order to initialise that peer
739 * Called directly after connecting to the service
744 * Main function for each peer
749 * Callback called once the requested peers are available
751 ReplyHandle reply_handle;
754 * Called directly before disconnecting from the service
759 * Function to evaluate the test results
761 EvaluationCallback eval_cb;
766 uint32_t request_interval;
769 * Number of Requests to make.
771 uint32_t num_requests;
774 * Run with (-out) churn
776 enum OPTION_CHURN have_churn;
779 * Quit test before timeout?
781 enum OPTION_QUICK_QUIT have_quick_quit;
784 * Collect statistics at the end?
786 enum OPTION_COLLECT_STATISTICS have_collect_statistics;
789 * Collect view during run?
791 enum OPTION_COLLECT_VIEW have_collect_view;
794 * @brief Mark which values from the statistics service to collect at the end
797 uint32_t stat_collect_flags;
801 * Did we finish the test?
803 static int post_test;
806 * Are we shutting down?
808 static int in_shutdown;
811 * Append arguments to file
814 tofile_ (const char *file_name, const char *line)
816 struct GNUNET_DISK_FileHandle *f;
817 /* char output_buffer[512]; */
822 if (NULL == (f = GNUNET_DISK_file_open (file_name,
823 GNUNET_DISK_OPEN_APPEND |
824 GNUNET_DISK_OPEN_WRITE |
825 GNUNET_DISK_OPEN_CREATE,
826 GNUNET_DISK_PERM_USER_READ |
827 GNUNET_DISK_PERM_USER_WRITE |
828 GNUNET_DISK_PERM_GROUP_READ |
829 GNUNET_DISK_PERM_OTHER_READ)))
831 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
832 "Not able to open file %s\n",
836 /* size = GNUNET_snprintf (output_buffer,
837 sizeof (output_buffer),
839 GNUNET_TIME_absolute_get ().abs_value_us,
843 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
844 "Failed to write string to buffer (size: %i)\n",
849 size = strlen (line) * sizeof (char);
851 size2 = GNUNET_DISK_file_write (f, line, size);
854 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
855 "Unable to write to file! (Size: %lu, size2: %lu)\n",
858 if (GNUNET_YES != GNUNET_DISK_file_close (f))
860 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
861 "Unable to close file\n");
866 if (GNUNET_YES != GNUNET_DISK_file_close (f))
868 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
869 "Unable to close file\n");
874 * This function is used to facilitate writing important information to disk
876 #define tofile(file_name, ...) do {\
879 size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
881 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
882 "Failed to create tmp_buf\n");\
884 tofile_(file_name,tmp_buf);\
889 * Write the ids and their according index in the given array to a file
893 ids_to_file (char *file_name,
894 struct GNUNET_PeerIdentity *peer_ids,
895 unsigned int num_peer_ids)
899 for (i=0 ; i < num_peer_ids ; i++)
904 GNUNET_i2s_full (&peer_ids[i]));
909 * Test the success of a single test
919 for (i = 0; i < num_peers; i++)
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
924 GNUNET_i2s (rps_peers[i].peer_id),
925 rps_peers[i].num_recv_ids,
926 rps_peers[i].num_ids_to_request,
927 (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
928 tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
930 return tmp_ok? 0 : 1;
935 * Creates an oplist entry and adds it to the oplist DLL
937 static struct OpListEntry *
940 struct OpListEntry *entry;
942 entry = GNUNET_new (struct OpListEntry);
943 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
949 * @brief Checks if given peer already received its statistics value from the
950 * statistics service.
952 * @param rps_peer the peer to check for
954 * @return #GNUNET_YES if so
955 * #GNUNET_NO otherwise
957 static int check_statistics_collect_completed_single_peer (
958 const struct RPSPeer *rps_peer)
960 if (cur_test_run.stat_collect_flags !=
961 (cur_test_run.stat_collect_flags &
962 rps_peer->stat_collected_flags))
969 * @brief Checks if all peers already received their statistics value from the
970 * statistics service.
972 * @return #GNUNET_YES if so
973 * #GNUNET_NO otherwise
975 static int check_statistics_collect_completed ()
979 for (i = 0; i < num_peers; i++)
981 if (GNUNET_NO == check_statistics_collect_completed_single_peer (&rps_peers[i]))
983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
984 "At least Peer %" PRIu32 " did not yet receive all statistics values\n",
989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
990 "All peers received their statistics values\n");
995 rps_disconnect_adapter (void *cls,
999 cancel_pending_req (struct PendingRequest *pending_req)
1001 struct RPSPeer *rps_peer;
1003 rps_peer = pending_req->rps_peer;
1004 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1005 rps_peer->pending_req_tail,
1007 rps_peer->num_pending_reqs--;
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1009 "Cancelling pending rps get request\n");
1010 GNUNET_SCHEDULER_cancel (pending_req->request_task);
1011 GNUNET_free (pending_req);
1015 cancel_request (struct PendingReply *pending_rep)
1017 struct RPSPeer *rps_peer;
1019 rps_peer = pending_rep->rps_peer;
1020 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1021 rps_peer->pending_rep_tail,
1023 rps_peer->num_pending_reps--;
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 "Cancelling rps get reply\n");
1026 GNUNET_assert (NULL != pending_rep->req_handle);
1027 GNUNET_RPS_request_cancel (pending_rep->req_handle);
1028 GNUNET_free (pending_rep);
1032 clean_peer (unsigned peer_index)
1034 struct PendingRequest *pending_req;
1036 while (NULL != (pending_req = rps_peers[peer_index].pending_req_head))
1038 cancel_pending_req (pending_req);
1040 pending_req = rps_peers[peer_index].pending_req_head;
1041 rps_disconnect_adapter (&rps_peers[peer_index],
1042 &rps_peers[peer_index].rps_handle);
1043 for (unsigned stat_type = STAT_TYPE_ROUNDS;
1044 stat_type < STAT_TYPE_MAX;
1047 if (NULL != rps_peers[peer_index].h_stat_get[stat_type])
1049 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1050 "(%u) did not yet receive stat value for `%s'\n",
1051 rps_peers[peer_index].index,
1052 stat_type_strings[stat_type]);
1053 GNUNET_STATISTICS_get_cancel (
1054 rps_peers[peer_index].h_stat_get[stat_type]);
1057 if (NULL != rps_peers[peer_index].op)
1059 GNUNET_TESTBED_operation_done (rps_peers[peer_index].op);
1060 rps_peers[peer_index].op = NULL;
1065 * Task run on timeout to shut everything down.
1068 shutdown_op (void *cls)
1071 struct OpListEntry *entry;
1074 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1075 "Shutdown task scheduled, going down.\n");
1076 in_shutdown = GNUNET_YES;
1078 if (NULL != shutdown_task)
1080 GNUNET_SCHEDULER_cancel (shutdown_task);
1081 shutdown_task = NULL;
1083 if (NULL != post_test_task)
1085 GNUNET_SCHEDULER_cancel (post_test_task);
1086 post_test_task = NULL;
1088 if (NULL != churn_task)
1090 GNUNET_SCHEDULER_cancel (churn_task);
1093 entry = oplist_head;
1094 while (NULL != (entry = oplist_head))
1096 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1097 "Operation still pending on shutdown (%u)\n",
1099 GNUNET_TESTBED_operation_done (entry->op);
1100 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1101 GNUNET_free (entry);
1103 for (i = 0; i < num_peers; i++)
1111 trigger_shutdown (void *cls)
1115 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1116 "Shutdown was triggerd by timeout, going down.\n");
1117 shutdown_task = NULL;
1118 GNUNET_SCHEDULER_shutdown ();
1123 * Task run after #duration to collect statistics and potentially shut down.
1126 post_test_op (void *cls)
1131 post_test_task = NULL;
1132 post_test = GNUNET_YES;
1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1134 "Executing post test op.\n");
1135 if (NULL != churn_task)
1137 GNUNET_SCHEDULER_cancel (churn_task);
1140 for (i = 0; i < num_peers; i++)
1142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1143 "Executing post test op. (peer %" PRIu32 ")\n",
1144 rps_peers[i].index);
1145 if (NULL != rps_peers[i].op)
1147 GNUNET_TESTBED_operation_done (rps_peers[i].op);
1148 rps_peers[i].op = NULL;
1149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1150 "Cancelled testbed operation\n");
1152 if (NULL != cur_test_run.post_test)
1154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing post_test for peer %u\n", i);
1155 cur_test_run.post_test (&rps_peers[i]);
1158 /* If we do not collect statistics, shut down directly */
1159 if (NO_COLLECT_STATISTICS == cur_test_run.have_collect_statistics ||
1160 GNUNET_YES == check_statistics_collect_completed())
1162 GNUNET_SCHEDULER_cancel (shutdown_task);
1163 shutdown_task = NULL;
1164 GNUNET_SCHEDULER_shutdown ();
1173 seed_peers (void *cls)
1175 struct RPSPeer *peer = cls;
1176 unsigned int amount;
1179 // TODO if malicious don't seed mal peers
1180 amount = round (.5 * num_peers);
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
1183 for (i = 0 ; i < amount ; i++)
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
1186 GNUNET_i2s (&rps_peer_ids[i]));
1188 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
1193 * Get the id of peer i.
1196 info_cb (void *cb_cls,
1197 struct GNUNET_TESTBED_Operation *op,
1198 const struct GNUNET_TESTBED_PeerInformation *pinfo,
1201 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
1204 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1209 if (NULL == pinfo || NULL != emsg)
1211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
1212 GNUNET_TESTBED_operation_done (entry->op);
1216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1219 GNUNET_i2s (pinfo->result.id));
1221 rps_peer_ids[entry->index] = *(pinfo->result.id);
1222 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
1224 GNUNET_assert (GNUNET_OK ==
1225 GNUNET_CONTAINER_multipeermap_put (peer_map,
1226 &rps_peer_ids[entry->index],
1227 &rps_peers[entry->index],
1228 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1229 tofile ("/tmp/rps/peer_ids",
1232 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
1234 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1235 GNUNET_TESTBED_operation_done (entry->op);
1236 GNUNET_free (entry);
1241 * Callback to be called when RPS service connect operation is completed
1243 * @param cls the callback closure from functions generating an operation
1244 * @param op the operation that has been finished
1245 * @param ca_result the RPS service handle returned from rps_connect_adapter
1246 * @param emsg error message in case the operation has failed; will be NULL if
1247 * operation has executed successfully.
1250 rps_connect_complete_cb (void *cls,
1251 struct GNUNET_TESTBED_Operation *op,
1255 struct RPSPeer *rps_peer = cls;
1256 struct GNUNET_RPS_Handle *rps = ca_result;
1258 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1263 rps_peer->rps_handle = rps;
1264 rps_peer->online = GNUNET_YES;
1267 GNUNET_assert (op == rps_peer->op);
1270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1271 "Failed to connect to RPS service: %s\n",
1274 GNUNET_SCHEDULER_shutdown ();
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1279 "Started client successfully (%u)\n",
1282 cur_test_run.main_test (rps_peer);
1287 * Adapter function called to establish a connection to
1290 * @param cls closure
1291 * @param cfg configuration of the peer to connect to; will be available until
1292 * GNUNET_TESTBED_operation_done() is called on the operation returned
1293 * from GNUNET_TESTBED_service_connect()
1294 * @return service handle to return in 'op_result', NULL on error
1297 rps_connect_adapter (void *cls,
1298 const struct GNUNET_CONFIGURATION_Handle *cfg)
1300 struct GNUNET_RPS_Handle *h;
1302 h = GNUNET_RPS_connect (cfg);
1304 if (NULL != cur_test_run.pre_test)
1305 cur_test_run.pre_test (cls, h);
1311 * Called to open a connection to the peer's statistics
1313 * @param cls peer context
1314 * @param cfg configuration of the peer to connect to; will be available until
1315 * GNUNET_TESTBED_operation_done() is called on the operation returned
1316 * from GNUNET_TESTBED_service_connect()
1317 * @return service handle to return in 'op_result', NULL on error
1320 stat_connect_adapter (void *cls,
1321 const struct GNUNET_CONFIGURATION_Handle *cfg)
1323 struct RPSPeer *peer = cls;
1325 peer->stats_h = GNUNET_STATISTICS_create ("rps-profiler", cfg);
1326 return peer->stats_h;
1330 * Called to disconnect from peer's statistics service
1332 * @param cls peer context
1333 * @param op_result service handle returned from the connect adapter
1336 stat_disconnect_adapter (void *cls, void *op_result)
1338 struct RPSPeer *peer = cls;
1340 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1341 // (peer->stats_h, "core", "# peers connected",
1342 // stat_iterator, peer));
1343 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1344 // (peer->stats_h, "nse", "# peers connected",
1345 // stat_iterator, peer));
1346 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
1347 peer->stats_h = NULL;
1351 * Called after successfully opening a connection to a peer's statistics
1352 * service; we register statistics monitoring for CORE and NSE here.
1354 * @param cls the callback closure from functions generating an operation
1355 * @param op the operation that has been finished
1356 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
1357 * @param emsg error message in case the operation has failed; will be NULL if
1358 * operation has executed successfully.
1361 stat_complete_cb (void *cls,
1362 struct GNUNET_TESTBED_Operation *op,
1366 //struct GNUNET_STATISTICS_Handle *sh = ca_result;
1367 //struct RPSPeer *peer = (struct RPSPeer *) cls;
1377 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1378 // (sh, "core", "# peers connected",
1379 // stat_iterator, peer));
1380 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1381 // (sh, "nse", "# peers connected",
1382 // stat_iterator, peer));
1387 * Adapter function called to destroy connection to
1390 * @param cls closure
1391 * @param op_result service handle returned from the connect adapter
1394 rps_disconnect_adapter (void *cls,
1397 struct RPSPeer *peer = cls;
1398 struct GNUNET_RPS_Handle *h = op_result;
1399 struct PendingReply *pending_rep;
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 "disconnect_adapter (%u)\n",
1404 GNUNET_assert (NULL != peer);
1405 if (NULL != peer->rps_handle)
1407 while (NULL != (pending_rep = peer->pending_rep_head))
1409 cancel_request (pending_rep);
1411 GNUNET_assert (h == peer->rps_handle);
1414 GNUNET_RPS_disconnect (h);
1417 peer->rps_handle = NULL;
1422 /***********************************************************************
1423 * Definition of tests
1424 ***********************************************************************/
1427 * Callback to call on receipt of a reply
1429 * @param cls closure
1430 * @param n number of peers
1431 * @param recv_peers the received peers
1434 default_reply_handle (void *cls,
1436 const struct GNUNET_PeerIdentity *recv_peers)
1438 struct RPSPeer *rps_peer;
1439 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1442 rps_peer = pending_rep->rps_peer;
1443 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1444 rps_peer->pending_rep_tail,
1446 rps_peer->num_pending_reps--;
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1448 "[%s] got %" PRIu64 " peers:\n",
1449 GNUNET_i2s (rps_peer->peer_id),
1452 for (i = 0; i < n; i++)
1454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1457 GNUNET_i2s (&recv_peers[i]));
1459 rps_peer->num_recv_ids++;
1462 if (GNUNET_YES != post_test) return;
1463 if (HAVE_QUICK_QUIT != cur_test_run.have_quick_quit) return;
1464 if (0 == evaluate())
1466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1467 "Test succeeded before end of duration\n");
1468 if (NULL != post_test_task) GNUNET_SCHEDULER_cancel (post_test_task);
1469 post_test_task = GNUNET_SCHEDULER_add_now (&post_test_op, NULL);
1470 GNUNET_assert (NULL != post_test_task);
1475 * Request random peers.
1478 request_peers (void *cls)
1480 struct PendingRequest *pending_req = cls;
1481 struct RPSPeer *rps_peer;
1482 struct PendingReply *pending_rep;
1484 rps_peer = pending_req->rps_peer;
1485 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
1486 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1487 rps_peer->pending_req_tail,
1489 rps_peer->num_pending_reqs--;
1490 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test) return;
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1492 "Requesting one peer\n");
1493 pending_rep = GNUNET_new (struct PendingReply);
1494 pending_rep->rps_peer = rps_peer;
1495 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
1497 cur_test_run.reply_handle,
1499 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
1500 rps_peer->pending_rep_tail,
1502 rps_peer->num_pending_reps++;
1507 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
1508 * issued, nor replied
1511 schedule_missing_requests (struct RPSPeer *rps_peer)
1514 struct PendingRequest *pending_req;
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1517 "Scheduling %u - %u missing requests\n",
1518 rps_peer->num_ids_to_request,
1519 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
1520 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
1521 rps_peer->num_ids_to_request);
1522 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
1523 i < rps_peer->num_ids_to_request; i++)
1525 pending_req = GNUNET_new (struct PendingRequest);
1526 pending_req->rps_peer = rps_peer;
1527 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
1528 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1529 cur_test_run.request_interval * i),
1532 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
1533 rps_peer->pending_req_tail,
1535 rps_peer->num_pending_reqs++;
1540 cancel_pending_req_rep (struct RPSPeer *rps_peer)
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543 "Cancelling all (pending) requests.\n");
1544 while (NULL != rps_peer->pending_req_head)
1545 cancel_pending_req (rps_peer->pending_req_head);
1546 GNUNET_assert (0 == rps_peer->num_pending_reqs);
1547 while (NULL != rps_peer->pending_rep_head)
1548 cancel_request (rps_peer->pending_rep_head);
1549 GNUNET_assert (0 == rps_peer->num_pending_reps);
1552 /***********************************
1554 ***********************************/
1557 * Initialise only non-mal RPSPeers
1559 static void mal_init_peer (struct RPSPeer *rps_peer)
1561 if (rps_peer->index >= round (portion * num_peers))
1562 rps_peer->num_ids_to_request = 1;
1567 * @brief Set peers to (non-)malicious before execution
1569 * Of signature #PreTest
1571 * @param rps_peer the peer to set (non-) malicious
1572 * @param h the handle to the service
1575 mal_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
1577 #if ENABLE_MALICIOUS
1578 uint32_t num_mal_peers;
1580 GNUNET_assert ( (1 >= portion) &&
1582 num_mal_peers = round (portion * num_peers);
1584 if (rps_peer->index < num_mal_peers)
1586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1587 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
1589 GNUNET_i2s (rps_peer->peer_id),
1592 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
1593 rps_peer_ids, target_peer);
1595 #endif /* ENABLE_MALICIOUS */
1599 mal_cb (struct RPSPeer *rps_peer)
1601 uint32_t num_mal_peers;
1603 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1608 #if ENABLE_MALICIOUS
1609 GNUNET_assert ( (1 >= portion) &&
1611 num_mal_peers = round (portion * num_peers);
1613 if (rps_peer->index >= num_mal_peers)
1614 { /* It's useless to ask a malicious peer about a random sample -
1615 it's not sampling */
1616 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1617 seed_peers, rps_peer);
1618 schedule_missing_requests (rps_peer);
1620 #endif /* ENABLE_MALICIOUS */
1623 /***********************************
1625 ***********************************/
1631 * @brief Starts churn
1633 * Has signature of #MainTest
1635 * This is not implemented too nicely as this is called for each peer, but we
1636 * only need to call it once. (Yes we check that we only schedule the task
1639 * @param rps_peer The peer it's called for
1642 churn_test_cb (struct RPSPeer *rps_peer)
1644 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1650 if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
1652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1653 "Starting churn task\n");
1654 churn_task = GNUNET_SCHEDULER_add_delayed (
1655 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1660 "Not starting churn task\n");
1663 schedule_missing_requests (rps_peer);
1666 /***********************************
1668 ***********************************/
1671 * Callback to be called when RPS service is started or stopped at peers
1674 * @param op the operation handle
1675 * @param emsg NULL on success; otherwise an error description
1678 churn_cb (void *cls,
1679 struct GNUNET_TESTBED_Operation *op,
1683 struct OpListEntry *entry = cls;
1686 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1691 GNUNET_TESTBED_operation_done (entry->op);
1694 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1695 GNUNET_SCHEDULER_shutdown ();
1698 GNUNET_assert (0 != entry->delta);
1700 num_peers_online += entry->delta;
1702 if (PEER_GO_OFFLINE == entry->delta)
1703 { /* Peer hopefully just went offline */
1704 if (GNUNET_YES != rps_peers[entry->index].online)
1706 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1707 "peer %s was expected to go offline but is still marked as online\n",
1708 GNUNET_i2s (rps_peers[entry->index].peer_id));
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1714 "peer %s probably went offline as expected\n",
1715 GNUNET_i2s (rps_peers[entry->index].peer_id));
1717 rps_peers[entry->index].online = GNUNET_NO;
1720 else if (PEER_GO_ONLINE < entry->delta)
1721 { /* Peer hopefully just went online */
1722 if (GNUNET_NO != rps_peers[entry->index].online)
1724 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1725 "peer %s was expected to go online but is still marked as offline\n",
1726 GNUNET_i2s (rps_peers[entry->index].peer_id));
1731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1732 "peer %s probably went online as expected\n",
1733 GNUNET_i2s (rps_peers[entry->index].peer_id));
1734 if (NULL != cur_test_run.pre_test)
1736 cur_test_run.pre_test (&rps_peers[entry->index],
1737 rps_peers[entry->index].rps_handle);
1738 schedule_missing_requests (&rps_peers[entry->index]);
1741 rps_peers[entry->index].online = GNUNET_YES;
1745 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1746 "Invalid value for delta: %i\n", entry->delta);
1750 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1751 rps_peers[entry->index].entry_op_manage = NULL;
1752 GNUNET_free (entry);
1753 //if (num_peers_in_round[current_round] == peers_running)
1758 * @brief Set the rps-service up or down for a specific peer
1760 * @param i index of action
1761 * @param j index of peer
1762 * @param delta (#PEER_ONLINE_DELTA) down (-1) or up (1)
1763 * @param prob_go_on_off the probability of the action
1766 manage_service_wrapper (unsigned int i, unsigned int j,
1767 enum PEER_ONLINE_DELTA delta,
1768 double prob_go_on_off)
1770 struct OpListEntry *entry = NULL;
1773 /* make sure that management operation is not already scheduled */
1774 if (NULL != rps_peers[j].entry_op_manage)
1779 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1782 "%u. selected peer (%u: %s) is %s.\n",
1785 GNUNET_i2s (rps_peers[j].peer_id),
1786 (PEER_GO_ONLINE == delta) ? "online" : "offline");
1787 if (prob < prob_go_on_off * UINT32_MAX)
1789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1791 GNUNET_i2s (rps_peers[j].peer_id),
1792 (PEER_GO_OFFLINE == delta) ? "offline" : "online");
1794 if (PEER_GO_OFFLINE == delta)
1795 cancel_pending_req_rep (&rps_peers[j]);
1796 entry = make_oplist_entry ();
1797 entry->delta = delta;
1799 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1804 (PEER_GO_OFFLINE == delta) ? 0 : 1);
1805 rps_peers[j].entry_op_manage = entry;
1815 double portion_online;
1816 unsigned int *permut;
1817 double prob_go_offline;
1818 double portion_go_online;
1819 double portion_go_offline;
1822 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1827 "Churn function executing\n");
1829 churn_task = NULL; /* Should be invalid by now */
1831 /* Compute the probability for an online peer to go offline
1833 portion_online = num_peers_online * 1.0 / num_peers;
1834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1835 "Portion online: %f\n",
1837 portion_go_online = ((1 - portion_online) * .5 * .66);
1838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1839 "Portion that should go online: %f\n",
1841 portion_go_offline = (portion_online + portion_go_online) - .75;
1842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1843 "Portion that probably goes offline: %f\n",
1844 portion_go_offline);
1845 prob_go_offline = portion_go_offline / (portion_online * .5);
1846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1847 "Probability of a selected online peer to go offline: %f\n",
1850 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1851 (unsigned int) num_peers);
1853 /* Go over 50% randomly chosen peers */
1854 for (i = 0; i < .5 * num_peers; i++)
1858 /* If online, shut down with certain probability */
1859 if (GNUNET_YES == rps_peers[j].online)
1861 manage_service_wrapper (i, j, -1, prob_go_offline);
1864 /* If offline, restart with certain probability */
1865 else if (GNUNET_NO == rps_peers[j].online)
1867 manage_service_wrapper (i, j, 1, 0.66);
1871 GNUNET_free (permut);
1873 churn_task = GNUNET_SCHEDULER_add_delayed (
1874 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1881 * Initialise given RPSPeer
1883 static void profiler_init_peer (struct RPSPeer *rps_peer)
1885 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer shall request %i peers\n",
1887 rps_peer->num_ids_to_request);
1892 * Callback to call on receipt of a reply
1894 * @param cls closure
1895 * @param n number of peers
1896 * @param recv_peers the received peers
1899 profiler_reply_handle (void *cls,
1901 const struct GNUNET_PeerIdentity *recv_peers)
1903 struct RPSPeer *rps_peer;
1904 struct RPSPeer *rcv_rps_peer;
1905 char file_name_buf[128];
1906 char file_name_dh_buf[128];
1907 char file_name_dhr_buf[128];
1908 char file_name_dhru_buf[128];
1909 char *file_name = file_name_buf;
1910 char *file_name_dh = file_name_dh_buf;
1911 char *file_name_dhr = file_name_dhr_buf;
1912 char *file_name_dhru = file_name_dhru_buf;
1914 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1916 pending_rep->req_handle = NULL;
1917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "profiler_reply_handle()\n");
1918 rps_peer = pending_rep->rps_peer;
1919 (void) GNUNET_asprintf (&file_name,
1920 "/tmp/rps/received_ids-%u",
1923 (void) GNUNET_asprintf (&file_name_dh,
1924 "/tmp/rps/diehard_input-%u",
1926 (void) GNUNET_asprintf (&file_name_dhr,
1927 "/tmp/rps/diehard_input_raw-%u",
1929 (void) GNUNET_asprintf (&file_name_dhru,
1930 "/tmp/rps/diehard_input_raw_aligned-%u",
1932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1933 "[%s] got %" PRIu64 " peers:\n",
1934 GNUNET_i2s (rps_peer->peer_id),
1936 for (i = 0; i < n; i++)
1938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1941 GNUNET_i2s (&recv_peers[i]));
1944 GNUNET_i2s_full (&recv_peers[i]));
1945 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1946 GNUNET_assert (NULL != rcv_rps_peer);
1947 tofile (file_name_dh,
1949 (uint32_t) rcv_rps_peer->index);
1951 to_file_raw (file_name_dhr,
1952 (char *) &rcv_rps_peer->index,
1954 to_file_raw_unaligned (file_name_dhru,
1955 (char *) &rcv_rps_peer->index,
1958 #endif /* TO_FILE */
1960 default_reply_handle (cls, n, recv_peers);
1965 profiler_cb (struct RPSPeer *rps_peer)
1967 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1973 if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
1975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1976 "Starting churn task\n");
1977 churn_task = GNUNET_SCHEDULER_add_delayed (
1978 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1983 "Not starting churn task\n");
1986 /* Only request peer ids at one peer.
1987 * (It's the before-last because last one is target of the focussed attack.)
1989 if (0 < rps_peer->num_ids_to_request)
1990 schedule_missing_requests (rps_peer);
1994 * Function called from #profiler_eval with a filename.
1996 * @param cls closure
1997 * @param filename complete filename (absolute path)
1998 * @return #GNUNET_OK to continue to iterate,
1999 * #GNUNET_NO to stop iteration with no error,
2000 * #GNUNET_SYSERR to abort iteration with error!
2003 file_name_cb (void *cls, const char *filename)
2005 if (NULL != strstr (filename, "sampler_el"))
2007 struct RPS_SamplerElement *s_elem;
2008 struct GNUNET_CRYPTO_AuthKey auth_key;
2009 const char *key_char;
2013 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
2014 tofile (filename, "--------------------------\n");
2016 auth_key = string_to_auth_key (key_char);
2017 s_elem = RPS_sampler_elem_create ();
2018 RPS_sampler_elem_set (s_elem, auth_key);
2020 for (i = 0; i < num_peers; i++)
2022 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
2024 RPS_sampler_elem_destroy (s_elem);
2030 * This is run after the test finished.
2032 * Compute all perfect samples.
2035 profiler_eval (void)
2038 /* Compute perfect sample for each sampler element */
2039 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
2041 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
2043 #endif /* TO_FILE */
2048 static uint32_t fac (uint32_t x)
2054 return x * fac (x - 1);
2057 static uint32_t binom (uint32_t n, uint32_t k)
2059 //GNUNET_assert (n >= k);
2060 if (k > n) return 0;
2061 /* if (0 > n) return 0; - always false */
2062 /* if (0 > k) return 0; - always false */
2063 if (0 == k) return 1;
2066 fac(k) * fac(n - k);
2070 * @brief is b in view of a?
2077 static int is_in_view (uint32_t a, uint32_t b)
2080 for (i = 0; i < rps_peers[a].cur_view_count; i++)
2082 if (0 == memcmp (rps_peers[b].peer_id,
2083 &rps_peers[a].cur_view[i],
2084 sizeof (struct GNUNET_PeerIdentity)))
2092 static uint32_t get_idx_of_pid (const struct GNUNET_PeerIdentity *pid)
2096 for (i = 0; i < num_peers; i++)
2098 if (0 == memcmp (pid,
2099 rps_peers[i].peer_id,
2100 sizeof (struct GNUNET_PeerIdentity)))
2105 //return 0; /* Should not happen - make compiler happy */
2106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2107 "No known _PeerIdentity %s!\n",
2108 GNUNET_i2s_full (pid));
2113 * @brief Counts number of peers in view of a that have b in their view
2120 static uint32_t count_containing_views (uint32_t a, uint32_t b)
2126 for (i = 0; i < rps_peers[a].cur_view_count; i++)
2128 peer_idx = get_idx_of_pid (&rps_peers[a].cur_view[i]);
2129 if (GNUNET_YES == is_in_view (peer_idx, b))
2138 * @brief Computes the probability for each other peer to be selected by the
2139 * sampling process based on the views of all peers
2141 * @param peer_idx index of the peer that is about to sample
2143 static void compute_probabilities (uint32_t peer_idx)
2145 //double probs[num_peers] = { 0 };
2146 double probs[num_peers];
2147 size_t probs_as_str_size = (num_peers * 10 + 1) * sizeof (char);
2148 char *probs_as_str = GNUNET_malloc (probs_as_str_size);
2149 char *probs_as_str_cpy;
2154 uint32_t cont_views;
2155 uint32_t number_of_being_in_pull_events;
2157 uint32_t count_non_zero_prob = 0;
2159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2160 "Computing probabilities for peer %" PRIu32 "\n", peer_idx);
2161 /* Firstly without knowledge of old views */
2162 for (i = 0; i < num_peers; i++)
2164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2165 "\tfor peer %" PRIu32 ":\n", i);
2166 view_size = rps_peers[i].cur_view_count;
2167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2168 "\t\tview_size: %" PRIu32 "\n", view_size);
2169 /* For peer i the probability of being sampled is
2170 * evenly distributed among all possibly observed peers. */
2171 /* We could have observed a peer in three cases:
2172 * 1. peer sent a push
2173 * 2. peer was contained in a pull reply
2174 * 3. peer was in history (sampler) - ignored for now */
2175 /* 1. Probability of having received a push from peer i */
2176 if ((GNUNET_YES == is_in_view (i, peer_idx)) &&
2177 (1 <= (0.45 * view_size)))
2179 if (0 == binom (view_size, 0.45 * view_size)) prob_push = 0;
2182 prob_push = 1.0 * binom (0.45 * view_size, 1)
2184 binom (view_size, 0.45 * view_size);
2186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2187 "\t\t%" PRIu32 " is in %" PRIu32 "'s view, prob: %f\n",
2191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2192 "\t\tposs choices from view: %" PRIu32 ", containing i: %" PRIu32 "\n",
2193 binom (view_size, 0.45 * view_size),
2194 binom (0.45 * view_size, 1));
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198 "\t\t%" PRIu32 " is not in %" PRIu32 "'s view, prob: 0\n",
2202 /* 2. Probability of peer i being contained in pulls */
2203 view_size = rps_peers[peer_idx].cur_view_count;
2204 cont_views = count_containing_views (peer_idx, i);
2205 number_of_being_in_pull_events =
2206 (binom (view_size, 0.45 * view_size) -
2207 binom (view_size - cont_views, 0.45 * view_size));
2208 if (0 != number_of_being_in_pull_events)
2210 prob_pull = number_of_being_in_pull_events
2212 (1.0 * binom (view_size, 0.45 * view_size));
2217 probs[i] = prob_push + prob_pull - (prob_push * prob_pull);
2218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2219 "\t\t%" PRIu32 " has %" PRIu32 " of %" PRIu32
2220 " peers in its view who know %" PRIu32 " prob: %f\n",
2226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2227 "\t\tnumber of possible pull combinations: %" PRIu32 "\n",
2228 binom (view_size, 0.45 * view_size));
2229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2230 "\t\tnumber of possible pull combinations without %" PRIu32
2233 binom (view_size - cont_views, 0.45 * view_size));
2234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2235 "\t\tnumber of possible pull combinations with %" PRIu32
2238 number_of_being_in_pull_events);
2240 if (0 != probs[i]) count_non_zero_prob++;
2243 if (0 != count_non_zero_prob)
2245 for (i = 0; i < num_peers; i++)
2247 probs[i] = probs[i] * (1.0 / count_non_zero_prob);
2250 for (i = 0; i < num_peers; i++)
2256 for (i = 0; i < num_peers; i++)
2258 probs_as_str_cpy = GNUNET_strndup (probs_as_str, probs_as_str_size);
2259 tmp = GNUNET_snprintf (probs_as_str,
2261 "%s %7.6f", probs_as_str_cpy, probs[i]);
2262 GNUNET_free (probs_as_str_cpy);
2263 GNUNET_assert (0 <= tmp);
2266 to_file_w_len (rps_peers[peer_idx].file_name_probs,
2269 GNUNET_free (probs_as_str);
2273 * @brief This counts the number of peers in which views a given peer occurs.
2275 * It also stores this value in the rps peer.
2277 * @param peer_idx the index of the peer to count the representation
2279 * @return the number of occurrences
2281 static uint32_t count_peer_in_views_2 (uint32_t peer_idx)
2286 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2288 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2290 if (0 == memcmp (rps_peers[peer_idx].peer_id,
2291 &rps_peers[i].cur_view[j],
2292 sizeof (struct GNUNET_PeerIdentity)))
2299 rps_peers[peer_idx].count_in_views = count;
2303 static uint32_t cumulated_view_sizes ()
2308 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2310 view_sizes += rps_peers[i].cur_view_count;
2315 static void count_peer_in_views (uint32_t *count_peers)
2319 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2321 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2323 if (0 == memcmp (rps_peers[i].peer_id,
2324 &rps_peers[i].cur_view[j],
2325 sizeof (struct GNUNET_PeerIdentity)))
2333 void compute_diversity ()
2336 /* ith entry represents the numer of occurrences in other peer's views */
2337 uint32_t *count_peers = GNUNET_new_array (num_peers, uint32_t);
2338 uint32_t views_total_size;
2340 /* deviation from expected number of peers */
2341 double *deviation = GNUNET_new_array (num_peers, double);
2343 views_total_size = 0;
2346 /* For each peer count its representation in other peer's views*/
2347 for (i = 0; i < num_peers; i++) /* Peer to count */
2349 views_total_size += rps_peers[i].cur_view_count;
2350 count_peer_in_views (count_peers);
2351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2352 "Counted representation of %" PRIu32 "th peer [%s]: %" PRIu32"\n",
2354 GNUNET_i2s (rps_peers[i].peer_id),
2358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2359 "size of all views combined: %" PRIu32 "\n",
2361 expected = ((double) 1/num_peers) * views_total_size;
2362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2363 "Expected number of occurrences of each peer in all views: %f\n",
2365 for (i = 0; i < num_peers; i++) /* Peer to count */
2367 deviation[i] = expected - count_peers[i];
2368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2369 "Deviation from expectation: %f\n", deviation[i]);
2371 GNUNET_free (count_peers);
2372 GNUNET_free (deviation);
2375 void print_view_sizes()
2379 for (i = 0; i < num_peers; i++) /* Peer to count */
2381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2382 "View size of %" PRIu32 ". [%s] is %" PRIu32 "\n",
2384 GNUNET_i2s (rps_peers[i].peer_id),
2385 rps_peers[i].cur_view_count);
2389 void all_views_updated_cb()
2391 compute_diversity();
2395 void view_update_cb (void *cls,
2397 const struct GNUNET_PeerIdentity *peers)
2399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2400 "View was updated (%" PRIu64 ")\n", view_size);
2401 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
2402 to_file ("/tmp/rps/view_sizes.txt",
2403 "%" PRIu64 " %" PRIu32 "",
2406 for (uint64_t i = 0; i < view_size; i++)
2408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2409 "\t%s\n", GNUNET_i2s (&peers[i]));
2411 GNUNET_array_grow (rps_peer->cur_view,
2412 rps_peer->cur_view_count,
2414 //*rps_peer->cur_view = *peers;
2415 GNUNET_memcpy (rps_peer->cur_view,
2417 view_size * sizeof (struct GNUNET_PeerIdentity));
2418 to_file ("/tmp/rps/count_in_views.txt",
2419 "%" PRIu64 " %" PRIu32 "",
2421 count_peer_in_views_2 (rps_peer->index));
2422 cumulated_view_sizes();
2425 to_file ("/tmp/rps/repr.txt",
2426 "%" PRIu64 /* index */
2427 " %" PRIu32 /* occurrence in views */
2428 " %" PRIu32 /* view sizes */
2429 " %f" /* fraction of repr in views */
2430 " %f" /* average view size */
2431 " %f" /* prob of occurrence in view slot */
2432 " %f" "", /* exp frac of repr in views */
2434 count_peer_in_views_2 (rps_peer->index),
2436 count_peer_in_views_2 (rps_peer->index) / (view_size * 1.0), /* fraction of representation in views */
2437 view_sizes / (view_size * 1.0), /* average view size */
2438 1.0 /view_size, /* prob of occurrence in view slot */
2439 (1.0/view_size) * (view_sizes/view_size) /* expected fraction of repr in views */
2442 compute_probabilities (rps_peer->index);
2443 all_views_updated_cb();
2447 pre_profiler (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
2449 rps_peer->file_name_probs =
2450 store_prefix_file_name (rps_peer->peer_id, "probs");
2451 GNUNET_RPS_view_request (h, 0, view_update_cb, rps_peer);
2454 void write_final_stats (void){
2455 uint64_t sums[STAT_TYPE_MAX] = { 0 };
2457 for (uint32_t i = 0; i < num_peers; i++)
2459 to_file ("/tmp/rps/final_stats.csv",
2460 "%" PRIu32 ", " /* index */
2462 PRIu64 ", %" /* rounds */
2463 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* blocking */
2464 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* issued */
2465 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* sent */
2466 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* recv */
2467 PRIu64 ", %" /* view size */
2468 PRIu64 ", %" /* known peers */
2469 PRIu64 ", %" /* valid peers */
2470 PRIu64 ", %" /* learned peers */
2471 PRIu64 ", %" /* pending online checks */
2472 PRIu64 ", %" /* unrequested pull replies */
2473 PRIu64 ", %" /* peers in push map */
2474 PRIu64 ", %" /* peers in pull map */
2475 PRIu64 ", %" /* peers in view */
2476 PRIu64 "\n"/* view size aim */,
2478 GNUNET_i2s (rps_peers[i].peer_id),
2479 rps_peers[i].stats[STAT_TYPE_ROUNDS],
2480 rps_peers[i].stats[STAT_TYPE_BLOCKS],
2481 rps_peers[i].stats[STAT_TYPE_BLOCKS_MANY_PUSH],
2482 rps_peers[i].stats[STAT_TYPE_BLOCKS_NO_PUSH],
2483 rps_peers[i].stats[STAT_TYPE_BLOCKS_NO_PULL],
2484 rps_peers[i].stats[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL],
2485 rps_peers[i].stats[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL],
2486 rps_peers[i].stats[STAT_TYPE_ISSUED_PUSH_SEND],
2487 rps_peers[i].stats[STAT_TYPE_ISSUED_PULL_REQ],
2488 rps_peers[i].stats[STAT_TYPE_ISSUED_PULL_REQ_MH],
2489 rps_peers[i].stats[STAT_TYPE_ISSUED_PULL_REP],
2490 rps_peers[i].stats[STAT_TYPE_SENT_PUSH_SEND],
2491 rps_peers[i].stats[STAT_TYPE_SENT_PULL_REQ],
2492 rps_peers[i].stats[STAT_TYPE_SENT_PULL_REQ_MH],
2493 rps_peers[i].stats[STAT_TYPE_SENT_PULL_REP],
2494 rps_peers[i].stats[STAT_TYPE_RECV_PUSH_SEND],
2495 rps_peers[i].stats[STAT_TYPE_RECV_PULL_REQ],
2496 rps_peers[i].stats[STAT_TYPE_RECV_PULL_REQ_MH],
2497 rps_peers[i].stats[STAT_TYPE_RECV_PULL_REP_MH],
2498 rps_peers[i].stats[STAT_TYPE_RECV_PULL_REP],
2499 rps_peers[i].stats[STAT_TYPE_VIEW_SIZE],
2500 rps_peers[i].stats[STAT_TYPE_KNOWN_PEERS],
2501 rps_peers[i].stats[STAT_TYPE_VALID_PEERS],
2502 rps_peers[i].stats[STAT_TYPE_LEARND_PEERS],
2503 rps_peers[i].stats[STAT_TYPE_PENDING_ONLINE_CHECKS],
2504 rps_peers[i].stats[STAT_TYPE_UNREQUESTED_PULL_REPLIES],
2505 rps_peers[i].stats[STAT_TYPE_PEERS_IN_PUSH_MAP],
2506 rps_peers[i].stats[STAT_TYPE_PEERS_IN_PULL_MAP],
2507 rps_peers[i].stats[STAT_TYPE_PEERS_IN_VIEW],
2508 rps_peers[i].stats[STAT_TYPE_VIEW_SIZE_AIM]);
2509 for (enum STAT_TYPE stat_type = STAT_TYPE_ROUNDS;
2510 stat_type < STAT_TYPE_MAX;
2513 sums[stat_type] += rps_peers[i].stats[stat_type];
2516 to_file ("/tmp/rps/final_stats.dat",
2518 PRIu64 " %" /* rounds */
2519 PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" /* blocking */
2520 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* issued */
2521 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* sent */
2522 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* recv */
2523 PRIu64 ", %" /* view size */
2524 PRIu64 ", %" /* known peers */
2525 PRIu64 ", %" /* valid peers */
2526 PRIu64 ", %" /* learned peers */
2527 PRIu64 ", %" /* pending online checks */
2528 PRIu64 ", %" /* unrequested pull replies */
2529 PRIu64 ", %" /* peers in push map */
2530 PRIu64 ", %" /* peers in pull map */
2531 PRIu64 ", %" /* peers in view */
2532 PRIu64 "\n"/* view size aim */,
2533 sums[STAT_TYPE_ROUNDS],
2534 sums[STAT_TYPE_BLOCKS],
2535 sums[STAT_TYPE_BLOCKS_MANY_PUSH],
2536 sums[STAT_TYPE_BLOCKS_NO_PUSH],
2537 sums[STAT_TYPE_BLOCKS_NO_PULL],
2538 sums[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL],
2539 sums[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL],
2540 sums[STAT_TYPE_ISSUED_PUSH_SEND],
2541 sums[STAT_TYPE_ISSUED_PULL_REQ],
2542 sums[STAT_TYPE_ISSUED_PULL_REQ_MH],
2543 sums[STAT_TYPE_ISSUED_PULL_REP],
2544 sums[STAT_TYPE_SENT_PUSH_SEND],
2545 sums[STAT_TYPE_SENT_PULL_REQ],
2546 sums[STAT_TYPE_SENT_PULL_REQ_MH],
2547 sums[STAT_TYPE_SENT_PULL_REP],
2548 sums[STAT_TYPE_RECV_PUSH_SEND],
2549 sums[STAT_TYPE_RECV_PULL_REQ],
2550 sums[STAT_TYPE_RECV_PULL_REQ_MH],
2551 sums[STAT_TYPE_RECV_PULL_REP],
2552 sums[STAT_TYPE_RECV_PULL_REP_MH],
2553 sums[STAT_TYPE_VIEW_SIZE],
2554 sums[STAT_TYPE_KNOWN_PEERS],
2555 sums[STAT_TYPE_VALID_PEERS],
2556 sums[STAT_TYPE_LEARND_PEERS],
2557 sums[STAT_TYPE_PENDING_ONLINE_CHECKS],
2558 sums[STAT_TYPE_UNREQUESTED_PULL_REPLIES],
2559 sums[STAT_TYPE_PEERS_IN_PUSH_MAP],
2560 sums[STAT_TYPE_PEERS_IN_PULL_MAP],
2561 sums[STAT_TYPE_PEERS_IN_VIEW],
2562 sums[STAT_TYPE_VIEW_SIZE_AIM]);
2566 * Continuation called by #GNUNET_STATISTICS_get() functions.
2568 * Remembers that this specific statistics value was received for this peer.
2569 * Checks whether all peers received their statistics yet.
2570 * Issues the shutdown.
2572 * @param cls closure
2573 * @param success #GNUNET_OK if statistics were
2574 * successfully obtained, #GNUNET_SYSERR if not.
2577 post_test_shutdown_ready_cb (void *cls,
2580 struct STATcls *stat_cls = (struct STATcls *) cls;
2581 struct RPSPeer *rps_peer = stat_cls->rps_peer;
2583 rps_peer->h_stat_get[stat_cls->stat_type] = NULL;
2584 if (GNUNET_OK == success)
2586 /* set flag that we we got the value */
2587 rps_peer->stat_collected_flags |= BIT(stat_cls->stat_type);
2589 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2590 "Peer %u did not receive statistics value\n",
2592 GNUNET_free (stat_cls);
2597 if (NULL != rps_peer->stat_op &&
2598 GNUNET_YES == check_statistics_collect_completed_single_peer (rps_peer))
2600 GNUNET_TESTBED_operation_done (rps_peer->stat_op);
2603 write_final_stats ();
2604 if (GNUNET_YES == check_statistics_collect_completed())
2606 //write_final_stats ();
2607 GNUNET_free (stat_cls);
2608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2610 GNUNET_SCHEDULER_shutdown ();
2612 GNUNET_free (stat_cls);
2617 * Callback function to process statistic values.
2619 * @param cls closure
2620 * @param subsystem name of subsystem that created the statistic
2621 * @param name the name of the datum
2622 * @param value the current value
2623 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
2624 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
2627 stat_iterator (void *cls,
2628 const char *subsystem,
2633 const struct STATcls *stat_cls = (const struct STATcls *) cls;
2634 struct RPSPeer *rps_peer = (struct RPSPeer *) stat_cls->rps_peer;
2635 enum STAT_TYPE stat_type;
2637 (void) is_persistent;
2639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2640 "Got stat value: %s - %" PRIu64 " (%u)\n",
2644 to_file (rps_peer->file_name_stats,
2645 "%s: %" PRIu64 "\n",
2648 stat_type = stat_str_2_type (name);
2649 GNUNET_assert (STAT_TYPE_ROUNDS <= stat_type &&
2650 STAT_TYPE_MAX > stat_type);
2651 rps_peer->stats[stat_type] = value;
2657 post_profiler (struct RPSPeer *rps_peer)
2659 if (COLLECT_STATISTICS != cur_test_run.have_collect_statistics)
2664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2665 "Going to request statistic values with mask 0x%" PRIx32 "\n",
2666 cur_test_run.stat_collect_flags);
2668 struct STATcls *stat_cls;
2670 for (stat_type = STAT_TYPE_ROUNDS;
2671 stat_type < STAT_TYPE_MAX;
2674 if (BIT(stat_type) & cur_test_run.stat_collect_flags)
2676 stat_cls = GNUNET_malloc (sizeof (struct STATcls));
2677 stat_cls->rps_peer = rps_peer;
2678 stat_cls->stat_type = stat_type;
2679 rps_peer->file_name_stats =
2680 store_prefix_file_name (rps_peer->peer_id, "stats");
2681 rps_peer->h_stat_get[stat_type] =
2682 GNUNET_STATISTICS_get (rps_peer->stats_h,
2684 stat_type_strings [stat_type],
2685 post_test_shutdown_ready_cb,
2687 (struct STATcls *) stat_cls);
2688 GNUNET_assert (NULL != rps_peer->h_stat_get);
2689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2690 "Requested statistics for %s (peer %" PRIu32 ")\n",
2691 stat_type_strings [stat_type],
2698 /***********************************************************************
2699 * /Definition of tests
2700 ***********************************************************************/
2704 * Actual "main" function for the testcase.
2706 * @param cls closure
2707 * @param h the run handle
2708 * @param n_peers number of peers in 'peers'
2709 * @param peers handle to peers run in the testbed
2710 * @param links_succeeded the number of overlay link connection attempts that
2712 * @param links_failed the number of overlay link connection attempts that
2716 test_run (void *cls,
2717 struct GNUNET_TESTBED_RunHandle *h,
2718 unsigned int n_peers,
2719 struct GNUNET_TESTBED_Peer **peers,
2720 unsigned int links_succeeded,
2721 unsigned int links_failed)
2724 struct OpListEntry *entry;
2727 (void) links_failed;
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
2731 /* Check whether we timed out */
2732 if (n_peers != num_peers ||
2734 0 == links_succeeded)
2736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Going down due to args (eg. timeout)\n");
2737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tn_peers: %u\n", n_peers);
2738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tnum_peers: %" PRIu32 "\n", num_peers);
2739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tpeers: %p\n", peers);
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tlinks_succeeded: %u\n", links_succeeded);
2742 GNUNET_SCHEDULER_shutdown ();
2747 /* Initialize peers */
2748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "going to initialise peers\n");
2749 testbed_peers = peers;
2750 num_peers_online = 0;
2751 for (i = 0; i < num_peers; i++)
2753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "initialising %u\n", i);
2754 entry = make_oplist_entry ();
2756 rps_peers[i].index = i;
2757 if (NULL != cur_test_run.init_peer)
2758 cur_test_run.init_peer (&rps_peers[i]);
2759 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
2761 rps_peers->cur_view_count = 0;
2762 rps_peers->cur_view = NULL;
2764 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
2765 GNUNET_TESTBED_PIT_IDENTITY,
2770 /* Bring peers up */
2771 GNUNET_assert (num_peers == n_peers);
2772 for (i = 0; i < n_peers; i++)
2774 rps_peers[i].index = i;
2776 GNUNET_TESTBED_service_connect (&rps_peers[i],
2779 &rps_connect_complete_cb,
2781 &rps_connect_adapter,
2782 &rps_disconnect_adapter,
2784 /* Connect all peers to statistics service */
2785 if (COLLECT_STATISTICS == cur_test_run.have_collect_statistics)
2787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2788 "Connecting to statistics service\n");
2789 rps_peers[i].stat_op =
2790 GNUNET_TESTBED_service_connect (NULL,
2795 &stat_connect_adapter,
2796 &stat_disconnect_adapter,
2801 if (NULL != churn_task)
2802 GNUNET_SCHEDULER_cancel (churn_task);
2803 post_test_task = GNUNET_SCHEDULER_add_delayed (duration, &post_test_op, NULL);
2804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout for shutdown is %lu\n", timeout.rel_value_us/1000000);
2805 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout,
2808 GNUNET_SCHEDULER_add_shutdown (shutdown_op, NULL);
2813 * Entry point for the testcase, sets up the testbed.
2815 * @param argc unused
2816 * @param argv unused
2821 const char *cfgfile,
2822 const struct GNUNET_CONFIGURATION_Handle *cfg)
2829 /* Defaults for tests */
2832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
2833 cur_test_run.name = "test-rps-profiler";
2837 cur_test_run.init_peer = profiler_init_peer;
2838 //cur_test_run.pre_test = mal_pre;
2839 cur_test_run.pre_test = pre_profiler;
2840 cur_test_run.main_test = profiler_cb;
2841 cur_test_run.reply_handle = profiler_reply_handle;
2842 cur_test_run.eval_cb = profiler_eval;
2843 cur_test_run.post_test = post_profiler;
2844 cur_test_run.request_interval = 2;
2845 if (0 == cur_test_run.num_requests) cur_test_run.num_requests = 5;
2846 //cur_test_run.have_churn = HAVE_CHURN;
2847 cur_test_run.have_churn = HAVE_NO_CHURN;
2848 cur_test_run.have_quick_quit = HAVE_QUICK_QUIT;
2849 cur_test_run.have_collect_statistics = COLLECT_STATISTICS;
2850 cur_test_run.stat_collect_flags = BIT(STAT_TYPE_ROUNDS) |
2851 BIT(STAT_TYPE_BLOCKS) |
2852 BIT(STAT_TYPE_BLOCKS_MANY_PUSH) |
2853 BIT(STAT_TYPE_BLOCKS_NO_PUSH) |
2854 BIT(STAT_TYPE_BLOCKS_NO_PULL) |
2855 BIT(STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL) |
2856 BIT(STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL) |
2857 BIT(STAT_TYPE_ISSUED_PUSH_SEND) |
2858 BIT(STAT_TYPE_ISSUED_PULL_REQ) |
2859 BIT(STAT_TYPE_ISSUED_PULL_REQ_MH) |
2860 BIT(STAT_TYPE_ISSUED_PULL_REP) |
2861 BIT(STAT_TYPE_SENT_PUSH_SEND) |
2862 BIT(STAT_TYPE_SENT_PULL_REQ) |
2863 BIT(STAT_TYPE_SENT_PULL_REQ_MH) |
2864 BIT(STAT_TYPE_SENT_PULL_REP) |
2865 BIT(STAT_TYPE_RECV_PUSH_SEND) |
2866 BIT(STAT_TYPE_RECV_PULL_REQ) |
2867 BIT(STAT_TYPE_RECV_PULL_REQ_MH) |
2868 BIT(STAT_TYPE_RECV_PULL_REP) |
2869 BIT(STAT_TYPE_RECV_PULL_REP_MH) |
2870 BIT(STAT_TYPE_VIEW_SIZE) |
2871 BIT(STAT_TYPE_KNOWN_PEERS) |
2872 BIT(STAT_TYPE_VALID_PEERS) |
2873 BIT(STAT_TYPE_LEARND_PEERS) |
2874 BIT(STAT_TYPE_PENDING_ONLINE_CHECKS) |
2875 BIT(STAT_TYPE_UNREQUESTED_PULL_REPLIES) |
2876 BIT(STAT_TYPE_PEERS_IN_PUSH_MAP) |
2877 BIT(STAT_TYPE_PEERS_IN_PULL_MAP) |
2878 BIT(STAT_TYPE_PEERS_IN_VIEW) |
2879 BIT(STAT_TYPE_VIEW_SIZE_AIM);
2880 cur_test_run.have_collect_view = COLLECT_VIEW;
2882 /* 'Clean' directory */
2883 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
2884 GNUNET_DISK_directory_create ("/tmp/rps/");
2885 if (0 == duration.rel_value_us)
2887 if (0 == timeout.rel_value_us)
2889 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
2890 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
2892 (0.01 * num_peers));
2896 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
2897 (timeout.rel_value_us/1000000)
2903 if (0 == timeout.rel_value_us)
2905 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
2906 ((duration.rel_value_us/1000000)
2907 * 1.2) + (0.01 * num_peers));
2910 GNUNET_assert (duration.rel_value_us < timeout.rel_value_us);
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2912 "duration is %lus\n",
2913 duration.rel_value_us/1000000);
2914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2915 "timeout is %lus\n",
2916 timeout.rel_value_us/1000000);
2918 /* Compute number of bits for representing largest peer id */
2919 for (bits_needed = 1; (1 << bits_needed) < num_peers; bits_needed++)
2921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2922 "Need %u bits to represent %" PRIu32 " peers\n",
2926 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
2927 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
2928 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
2929 if ( (2 == mal_type) ||
2931 target_peer = &rps_peer_ids[num_peers - 2];
2934 GNUNET_TESTBED_run (NULL,
2945 * Entry point for the testcase, sets up the testbed.
2947 * @param argc unused
2948 * @param argv unused
2949 * @return 0 on success
2952 main (int argc, char *argv[])
2955 struct GNUNET_GETOPT_CommandLineOption options[] = {
2956 GNUNET_GETOPT_option_uint ('n',
2959 gettext_noop ("number of peers to start"),
2961 GNUNET_GETOPT_option_relative_time ('d',
2964 gettext_noop ("duration of the profiling"),
2966 GNUNET_GETOPT_option_relative_time ('t',
2969 gettext_noop ("timeout for the profiling"),
2971 GNUNET_GETOPT_option_uint ('r',
2974 gettext_noop ("number of PeerIDs to request"),
2975 &cur_test_run.num_requests),
2976 GNUNET_GETOPT_OPTION_END
2979 unsetenv ("XDG_DATA_HOME");
2980 unsetenv ("XDG_CONFIG_HOME");
2982 // GNUNET_STRINGS_get_utf8_args (argc, argv,
2987 GNUNET_PROGRAM_run (argc,
2989 "gnunet-rps-profiler",
2990 gettext_noop ("Measure quality and performance of the RPS service."),
2999 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3000 "Test did not run successfully!\n");
3004 ret_value = cur_test_run.eval_cb();
3005 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
3007 GNUNET_array_grow (rps_peers->cur_view,
3008 rps_peers->cur_view_count,
3011 GNUNET_free (rps_peers);
3012 GNUNET_free (rps_peer_ids);
3013 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
3018 /* end of test_rps.c */