aboutsummaryrefslogtreecommitdiff
path: root/src/rps/gnunet-rps-profiler.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rps/gnunet-rps-profiler.c')
-rw-r--r--src/rps/gnunet-rps-profiler.c2853
1 files changed, 2853 insertions, 0 deletions
diff --git a/src/rps/gnunet-rps-profiler.c b/src/rps/gnunet-rps-profiler.c
new file mode 100644
index 000000000..277688b56
--- /dev/null
+++ b/src/rps/gnunet-rps-profiler.c
@@ -0,0 +1,2853 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009, 2012 GNUnet e.V.
4
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.
9
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.
14
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/>.
17*/
18/**
19 * @file rps/test_rps.c
20 * @brief Testcase for the random peer sampling service. Starts
21 * a peergroup with a given number of peers, then waits to
22 * receive size pushes/pulls from each peer. Expects to wait
23 * for one message from each peer.
24 */
25#include "platform.h"
26//#include "rps_test_lib.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_testbed_service.h"
29
30#include "gnunet_rps_service.h"
31#include "rps-test_util.h"
32#include "gnunet-service-rps_sampler_elem.h"
33
34#include <inttypes.h>
35
36
37#define BIT(n) (1 << (n))
38
39/**
40 * How many peers do we start?
41 */
42static uint32_t num_peers;
43
44/**
45 * @brief numer of bits required to represent the largest peer id
46 */
47static unsigned bits_needed;
48
49/**
50 * How long do we run the test?
51 */
52static struct GNUNET_TIME_Relative duration;
53
54/**
55 * When do we do a hard shutdown?
56 */
57static struct GNUNET_TIME_Relative timeout;
58
59
60/**
61 * Portion of malicious peers
62 */
63static double portion = .1;
64
65/**
66 * Type of malicious peer to test
67 */
68static unsigned int mal_type = 0;
69
70/**
71 * Handles to all of the running peers
72 */
73static struct GNUNET_TESTBED_Peer **testbed_peers;
74
75enum STAT_TYPE
76{
77 STAT_TYPE_ROUNDS, /* 0 */
78 STAT_TYPE_BLOCKS, /* 1 */
79 STAT_TYPE_BLOCKS_MANY_PUSH, /* 2 */
80 STAT_TYPE_BLOCKS_NO_PUSH, /* 3 */
81 STAT_TYPE_BLOCKS_NO_PULL, /* 4 */
82 STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL, /* 5 */
83 STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL, /* 6 */
84 STAT_TYPE_ISSUED_PUSH_SEND, /* 7 */
85 STAT_TYPE_ISSUED_PULL_REQ, /* 8 */
86 STAT_TYPE_ISSUED_PULL_REP, /* 9 */
87 STAT_TYPE_SENT_PUSH_SEND, /* 10 */
88 STAT_TYPE_SENT_PULL_REQ, /* 11 */
89 STAT_TYPE_SENT_PULL_REP, /* 12 */
90 STAT_TYPE_RECV_PUSH_SEND, /* 13 */
91 STAT_TYPE_RECV_PULL_REQ, /* 14 */
92 STAT_TYPE_RECV_PULL_REP, /* 15 */
93 STAT_TYPE_MAX, /* 16 */
94};
95
96struct STATcls
97{
98 struct RPSPeer *rps_peer;
99 enum STAT_TYPE stat_type;
100};
101
102
103/**
104 * @brief Converts string representation to the corresponding #STAT_TYPE enum.
105 *
106 * @param stat_str string representation of statistics specifier
107 *
108 * @return corresponding enum
109 */
110enum STAT_TYPE stat_str_2_type (const char *stat_str)
111{
112 if (0 == strncmp ("# rounds blocked - no pull replies", stat_str, strlen ("# rounds blocked - no pull replies")))
113 {
114 return STAT_TYPE_BLOCKS_NO_PULL;
115 }
116 else if (0 == strncmp ("# rounds blocked - too many pushes, no pull replies", stat_str, strlen ("# rounds blocked - too many pushes, no pull replies")))
117 {
118 return STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL;
119 }
120 else if (0 == strncmp ("# rounds blocked - too many pushes", stat_str, strlen ("# rounds blocked - too many pushes")))
121 {
122 return STAT_TYPE_BLOCKS_MANY_PUSH;
123 }
124 else if (0 == strncmp ("# rounds blocked - no pushes, no pull replies", stat_str, strlen ("# rounds blocked - no pushes, no pull replies")))
125 {
126 return STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL;
127 }
128 else if (0 == strncmp ("# rounds blocked - no pushes", stat_str, strlen ("# rounds blocked - no pushes")))
129 {
130 return STAT_TYPE_BLOCKS_NO_PUSH;
131 }
132 else if (0 == strncmp ("# rounds blocked", stat_str, strlen ("# rounds blocked")))
133 {
134 return STAT_TYPE_BLOCKS;
135 }
136 else if (0 == strncmp ("# rounds", stat_str, strlen ("# rounds")))
137 {
138 return STAT_TYPE_ROUNDS;
139 }
140 else if (0 == strncmp ("# push send issued", stat_str, strlen ("# push send issued")))
141 {
142 return STAT_TYPE_ISSUED_PUSH_SEND;
143 }
144 else if (0 == strncmp ("# pull request send issued", stat_str, strlen ("# pull request send issued")))
145 {
146 return STAT_TYPE_ISSUED_PULL_REQ;
147 }
148 else if (0 == strncmp ("# pull reply send issued", stat_str, strlen ("# pull reply send issued")))
149 {
150 return STAT_TYPE_ISSUED_PULL_REP;
151 }
152 else if (0 == strncmp ("# pushes sent", stat_str, strlen ("# pushes sent")))
153 {
154 return STAT_TYPE_SENT_PUSH_SEND;
155 }
156 else if (0 == strncmp ("# pull requests sent", stat_str, strlen ("# pull requests sent")))
157 {
158 return STAT_TYPE_SENT_PULL_REQ;
159 }
160 else if (0 == strncmp ("# pull replys sent", stat_str, strlen ("# pull replys sent")))
161 {
162 return STAT_TYPE_SENT_PULL_REP;
163 }
164 else if (0 == strncmp ("# push message received", stat_str, strlen ("# push message received")))
165 {
166 return STAT_TYPE_RECV_PUSH_SEND;
167 }
168 else if (0 == strncmp ("# pull request message received", stat_str, strlen ("# pull request message received")))
169 {
170 return STAT_TYPE_RECV_PULL_REQ;
171 }
172 else if (0 == strncmp ("# pull reply messages received", stat_str, strlen ("# pull reply messages received")))
173 {
174 return STAT_TYPE_RECV_PULL_REP;
175 }
176 return STAT_TYPE_MAX;
177}
178
179
180/**
181 * @brief Converts #STAT_TYPE enum to the equivalent string representation that
182 * is stored with the statistics service.
183 *
184 * @param stat_type #STAT_TYPE enum
185 *
186 * @return string representation that matches statistics value
187 */
188char* stat_type_2_str (enum STAT_TYPE stat_type)
189{
190 switch (stat_type)
191 {
192 case STAT_TYPE_ROUNDS:
193 return "# rounds";
194 case STAT_TYPE_BLOCKS:
195 return "# rounds blocked";
196 case STAT_TYPE_BLOCKS_MANY_PUSH:
197 return "# rounds blocked - too many pushes";
198 case STAT_TYPE_BLOCKS_NO_PUSH:
199 return "# rounds blocked - no pushes";
200 case STAT_TYPE_BLOCKS_NO_PULL:
201 return "# rounds blocked - no pull replies";
202 case STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL:
203 return "# rounds blocked - too many pushes, no pull replies";
204 case STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL:
205 return "# rounds blocked - no pushes, no pull replies";
206 case STAT_TYPE_ISSUED_PUSH_SEND:
207 return "# push send issued";
208 case STAT_TYPE_ISSUED_PULL_REQ:
209 return "# pull request send issued";
210 case STAT_TYPE_ISSUED_PULL_REP:
211 return "# pull reply send issued";
212 case STAT_TYPE_SENT_PUSH_SEND:
213 return "# pushes sent";
214 case STAT_TYPE_SENT_PULL_REQ:
215 return "# pull requests sent";
216 case STAT_TYPE_SENT_PULL_REP:
217 return "# pull replys sent";
218 case STAT_TYPE_RECV_PUSH_SEND:
219 return "# push message received";
220 case STAT_TYPE_RECV_PULL_REQ:
221 return "# pull request message received";
222 case STAT_TYPE_RECV_PULL_REP:
223 return "# pull reply messages received";
224 case STAT_TYPE_MAX:
225 default:
226 return "ERROR";
227 ;
228 }
229}
230
231
232/**
233 * @brief Indicates whether peer should go off- or online
234 */
235enum PEER_ONLINE_DELTA {
236 /**
237 * @brief Indicates peer going online
238 */
239 PEER_GO_ONLINE = 1,
240 /**
241 * @brief Indicates peer going offline
242 */
243 PEER_GO_OFFLINE = -1,
244};
245
246/**
247 * Operation map entry
248 */
249struct OpListEntry
250{
251 /**
252 * DLL next ptr
253 */
254 struct OpListEntry *next;
255
256 /**
257 * DLL prev ptr
258 */
259 struct OpListEntry *prev;
260
261 /**
262 * The testbed operation
263 */
264 struct GNUNET_TESTBED_Operation *op;
265
266 /**
267 * Depending on whether we start or stop RPS service at the peer, set this to
268 * #PEER_GO_ONLINE (1) or #PEER_GO_OFFLINE (-1)
269 */
270 enum PEER_ONLINE_DELTA delta;
271
272 /**
273 * Index of the regarding peer
274 */
275 unsigned int index;
276};
277
278/**
279 * OpList DLL head
280 */
281static struct OpListEntry *oplist_head;
282
283/**
284 * OpList DLL tail
285 */
286static struct OpListEntry *oplist_tail;
287
288
289/**
290 * A pending reply: A request was sent and the reply is pending.
291 */
292struct PendingReply
293{
294 /**
295 * DLL next,prev ptr
296 */
297 struct PendingReply *next;
298 struct PendingReply *prev;
299
300 /**
301 * Handle to the request we are waiting for
302 */
303 struct GNUNET_RPS_Request_Handle *req_handle;
304
305 /**
306 * The peer that requested
307 */
308 struct RPSPeer *rps_peer;
309};
310
311
312/**
313 * A pending request: A request was not made yet but is scheduled for later.
314 */
315struct PendingRequest
316{
317 /**
318 * DLL next,prev ptr
319 */
320 struct PendingRequest *next;
321 struct PendingRequest *prev;
322
323 /**
324 * Handle to the request we are waiting for
325 */
326 struct GNUNET_SCHEDULER_Task *request_task;
327
328 /**
329 * The peer that requested
330 */
331 struct RPSPeer *rps_peer;
332};
333
334
335/**
336 * Information we track for each peer.
337 */
338struct RPSPeer
339{
340 /**
341 * Index of the peer.
342 */
343 unsigned int index;
344
345 /**
346 * Handle for RPS connect operation.
347 */
348 struct GNUNET_TESTBED_Operation *op;
349
350 /**
351 * Handle to RPS service.
352 */
353 struct GNUNET_RPS_Handle *rps_handle;
354
355 /**
356 * ID of the peer.
357 */
358 struct GNUNET_PeerIdentity *peer_id;
359
360 /**
361 * A request handle to check for an request
362 */
363 //struct GNUNET_RPS_Request_Handle *req_handle;
364
365 /**
366 * Peer on- or offline?
367 */
368 int online;
369
370 /**
371 * Number of Peer IDs to request during the whole test
372 */
373 unsigned int num_ids_to_request;
374
375 /**
376 * Pending requests DLL
377 */
378 struct PendingRequest *pending_req_head;
379 struct PendingRequest *pending_req_tail;
380
381 /**
382 * Number of pending requests
383 */
384 unsigned int num_pending_reqs;
385
386 /**
387 * Pending replies DLL
388 */
389 struct PendingReply *pending_rep_head;
390 struct PendingReply *pending_rep_tail;
391
392 /**
393 * Number of pending replies
394 */
395 unsigned int num_pending_reps;
396
397 /**
398 * Number of received PeerIDs
399 */
400 unsigned int num_recv_ids;
401
402 /**
403 * Pending operation on that peer
404 */
405 const struct OpListEntry *entry_op_manage;
406
407 /**
408 * Testbed operation to connect to statistics service
409 */
410 struct GNUNET_TESTBED_Operation *stat_op;
411
412 /**
413 * Handle to the statistics service
414 */
415 struct GNUNET_STATISTICS_Handle *stats_h;
416
417 /**
418 * @brief flags to indicate which statistics values have been already
419 * collected from the statistics service.
420 * Used to check whether we are able to shutdown.
421 */
422 uint32_t stat_collected_flags;
423
424 /**
425 * @brief File name of the file the stats are finally written to
426 */
427 const char *file_name_stats;
428
429 /**
430 * @brief File name of the file the stats are finally written to
431 */
432 const char *file_name_probs;
433
434 /**
435 * @brief The current view
436 */
437 struct GNUNET_PeerIdentity *cur_view;
438
439 /**
440 * @brief Number of peers in the #cur_view.
441 */
442 uint32_t cur_view_count;
443
444 /**
445 * @brief Number of occurrences in other peer's view
446 */
447 uint32_t count_in_views;
448
449 /**
450 * @brief statistics values
451 */
452 uint64_t stats[STAT_TYPE_MAX];
453 /**
454 * @brief Handle for the statistics get request
455 */
456 struct GNUNET_STATISTICS_GetHandle *h_stat_get[STAT_TYPE_MAX];
457};
458
459/**
460 * Information for all the peers.
461 */
462static struct RPSPeer *rps_peers;
463
464/**
465 * Peermap to get the index of a given peer ID quick.
466 */
467static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
468
469/**
470 * IDs of the peers.
471 */
472static struct GNUNET_PeerIdentity *rps_peer_ids;
473
474/**
475 * ID of the targeted peer.
476 */
477static struct GNUNET_PeerIdentity *target_peer;
478
479/**
480 * ID of the peer that requests for the evaluation.
481 */
482static struct RPSPeer *eval_peer;
483
484/**
485 * Number of online peers.
486 */
487static unsigned int num_peers_online;
488
489/**
490 * @brief The added sizes of the peer's views
491 */
492static unsigned int view_sizes;
493
494/**
495 * Return value from 'main'.
496 */
497static int ok;
498
499/**
500 * Identifier for the task that runs after the test to collect results
501 */
502static struct GNUNET_SCHEDULER_Task *post_test_task;
503
504/**
505 * Identifier for the shutdown task
506 */
507static struct GNUNET_SCHEDULER_Task *shutdown_task;
508
509
510/**
511 * Identifier for the churn task that runs periodically
512 */
513static struct GNUNET_SCHEDULER_Task *churn_task;
514
515/**
516 * Called to initialise the given RPSPeer
517 */
518typedef void (*InitPeer) (struct RPSPeer *rps_peer);
519
520/**
521 * @brief Called directly after connecting to the service
522 *
523 * @param rps_peer Specific peer the function is called on
524 * @param h the handle to the rps service
525 */
526typedef void (*PreTest) (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h);
527
528/**
529 * @brief Executes functions to test the api/service for a given peer
530 *
531 * Called from within #rps_connect_complete_cb ()
532 * Implemented by #churn_test_cb, #profiler_cb, #mal_cb, #single_req_cb,
533 * #delay_req_cb, #seed_big_cb, #single_peer_seed_cb, #seed_cb, #req_cancel_cb
534 *
535 * @param rps_peer the peer the task runs on
536 */
537typedef void (*MainTest) (struct RPSPeer *rps_peer);
538
539/**
540 * Callback called once the requested random peers are available
541 */
542typedef void (*ReplyHandle) (void *cls,
543 uint64_t n,
544 const struct GNUNET_PeerIdentity *recv_peers);
545
546/**
547 * Called directly before disconnecting from the service
548 */
549typedef void (*PostTest) (struct RPSPeer *peer);
550
551/**
552 * Function called after disconnect to evaluate test success
553 */
554typedef int (*EvaluationCallback) (void);
555
556/**
557 * @brief Do we have Churn?
558 */
559enum OPTION_CHURN {
560 /**
561 * @brief If we have churn this is set
562 */
563 HAVE_CHURN,
564 /**
565 * @brief If we have no churn this is set
566 */
567 HAVE_NO_CHURN,
568};
569
570/**
571 * @brief Is it ok to quit the test before the timeout?
572 */
573enum OPTION_QUICK_QUIT {
574 /**
575 * @brief It is ok for the test to quit before the timeout triggers
576 */
577 HAVE_QUICK_QUIT,
578
579 /**
580 * @brief It is NOT ok for the test to quit before the timeout triggers
581 */
582 HAVE_NO_QUICK_QUIT,
583};
584
585/**
586 * @brief Do we collect statistics at the end?
587 */
588enum OPTION_COLLECT_STATISTICS {
589 /**
590 * @brief We collect statistics at the end
591 */
592 COLLECT_STATISTICS,
593
594 /**
595 * @brief We do not collect statistics at the end
596 */
597 NO_COLLECT_STATISTICS,
598};
599
600/**
601 * @brief Do we collect views during run?
602 */
603enum OPTION_COLLECT_VIEW {
604 /**
605 * @brief We collect view during run
606 */
607 COLLECT_VIEW,
608
609 /**
610 * @brief We do not collect the view during run
611 */
612 NO_COLLECT_VIEW,
613};
614
615/**
616 * Structure to define a single test
617 */
618struct SingleTestRun
619{
620 /**
621 * Name of the test
622 */
623 char *name;
624
625 /**
626 * Called with a single peer in order to initialise that peer
627 */
628 InitPeer init_peer;
629
630 /**
631 * Called directly after connecting to the service
632 */
633 PreTest pre_test;
634
635 /**
636 * Main function for each peer
637 */
638 MainTest main_test;
639
640 /**
641 * Callback called once the requested peers are available
642 */
643 ReplyHandle reply_handle;
644
645 /**
646 * Called directly before disconnecting from the service
647 */
648 PostTest post_test;
649
650 /**
651 * Function to evaluate the test results
652 */
653 EvaluationCallback eval_cb;
654
655 /**
656 * Request interval
657 */
658 uint32_t request_interval;
659
660 /**
661 * Number of Requests to make.
662 */
663 uint32_t num_requests;
664
665 /**
666 * Run with (-out) churn
667 */
668 enum OPTION_CHURN have_churn;
669
670 /**
671 * Quit test before timeout?
672 */
673 enum OPTION_QUICK_QUIT have_quick_quit;
674
675 /**
676 * Collect statistics at the end?
677 */
678 enum OPTION_COLLECT_STATISTICS have_collect_statistics;
679
680 /**
681 * Collect view during run?
682 */
683 enum OPTION_COLLECT_VIEW have_collect_view;
684
685 /**
686 * @brief Mark which values from the statistics service to collect at the end
687 * of the run
688 */
689 uint32_t stat_collect_flags;
690} cur_test_run;
691
692/**
693 * Did we finish the test?
694 */
695static int post_test;
696
697/**
698 * Are we shutting down?
699 */
700static int in_shutdown;
701
702/**
703 * Append arguments to file
704 */
705static void
706tofile_ (const char *file_name, const char *line)
707{
708 struct GNUNET_DISK_FileHandle *f;
709 /* char output_buffer[512]; */
710 size_t size;
711 /* int size; */
712 size_t size2;
713
714 if (NULL == (f = GNUNET_DISK_file_open (file_name,
715 GNUNET_DISK_OPEN_APPEND |
716 GNUNET_DISK_OPEN_WRITE |
717 GNUNET_DISK_OPEN_CREATE,
718 GNUNET_DISK_PERM_USER_READ |
719 GNUNET_DISK_PERM_USER_WRITE |
720 GNUNET_DISK_PERM_GROUP_READ |
721 GNUNET_DISK_PERM_OTHER_READ)))
722 {
723 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
724 "Not able to open file %s\n",
725 file_name);
726 return;
727 }
728 /* size = GNUNET_snprintf (output_buffer,
729 sizeof (output_buffer),
730 "%llu %s\n",
731 GNUNET_TIME_absolute_get ().abs_value_us,
732 line);
733 if (0 > size)
734 {
735 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
736 "Failed to write string to buffer (size: %i)\n",
737 size);
738 return;
739 } */
740
741 size = strlen (line) * sizeof (char);
742
743 size2 = GNUNET_DISK_file_write (f, line, size);
744 if (size != size2)
745 {
746 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
747 "Unable to write to file! (Size: %lu, size2: %lu)\n",
748 size,
749 size2);
750 if (GNUNET_YES != GNUNET_DISK_file_close (f))
751 {
752 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
753 "Unable to close file\n");
754 }
755 return;
756 }
757
758 if (GNUNET_YES != GNUNET_DISK_file_close (f))
759 {
760 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
761 "Unable to close file\n");
762 }
763}
764
765/**
766 * This function is used to facilitate writing important information to disk
767 */
768#define tofile(file_name, ...) do {\
769 char tmp_buf[512];\
770 int size;\
771 size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
772 if (0 > size)\
773 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
774 "Failed to create tmp_buf\n");\
775 else\
776 tofile_(file_name,tmp_buf);\
777 } while (0);
778
779
780/**
781 * Write the ids and their according index in the given array to a file
782 * Unused
783 */
784/* static void
785ids_to_file (char *file_name,
786 struct GNUNET_PeerIdentity *peer_ids,
787 unsigned int num_peer_ids)
788{
789 unsigned int i;
790
791 for (i=0 ; i < num_peer_ids ; i++)
792 {
793 to_file (file_name,
794 "%u\t%s",
795 i,
796 GNUNET_i2s_full (&peer_ids[i]));
797 }
798} */
799
800/**
801 * Test the success of a single test
802 */
803static int
804evaluate (void)
805{
806 unsigned int i;
807 int tmp_ok;
808
809 tmp_ok = 1;
810
811 for (i = 0; i < num_peers; i++)
812 {
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
815 i,
816 GNUNET_i2s (rps_peers[i].peer_id),
817 rps_peers[i].num_recv_ids,
818 rps_peers[i].num_ids_to_request,
819 (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
820 tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
821 }
822 return tmp_ok? 0 : 1;
823}
824
825
826/**
827 * Creates an oplist entry and adds it to the oplist DLL
828 */
829static struct OpListEntry *
830make_oplist_entry ()
831{
832 struct OpListEntry *entry;
833
834 entry = GNUNET_new (struct OpListEntry);
835 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
836 return entry;
837}
838
839
840/**
841 * @brief Checks if given peer already received its statistics value from the
842 * statistics service.
843 *
844 * @param rps_peer the peer to check for
845 *
846 * @return #GNUNET_YES if so
847 * #GNUNET_NO otherwise
848 */
849static int check_statistics_collect_completed_single_peer (
850 const struct RPSPeer *rps_peer)
851{
852 if (cur_test_run.stat_collect_flags !=
853 (cur_test_run.stat_collect_flags &
854 rps_peer->stat_collected_flags))
855 {
856 return GNUNET_NO;
857 }
858 return GNUNET_YES;
859}
860/**
861 * @brief Checks if all peers already received their statistics value from the
862 * statistics service.
863 *
864 * @return #GNUNET_YES if so
865 * #GNUNET_NO otherwise
866 */
867static int check_statistics_collect_completed ()
868{
869 uint32_t i;
870
871 for (i = 0; i < num_peers; i++)
872 {
873 if (GNUNET_NO == check_statistics_collect_completed_single_peer (&rps_peers[i]))
874 {
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876 "At least Peer %" PRIu32 " did not yet receive all statistics values\n",
877 i);
878 return GNUNET_NO;
879 }
880 }
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
882 "All peers received their statistics values\n");
883 return GNUNET_YES;
884}
885
886static void
887rps_disconnect_adapter (void *cls,
888 void *op_result);
889
890static void
891cancel_pending_req (struct PendingRequest *pending_req)
892{
893 struct RPSPeer *rps_peer;
894
895 rps_peer = pending_req->rps_peer;
896 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
897 rps_peer->pending_req_tail,
898 pending_req);
899 rps_peer->num_pending_reqs--;
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "Cancelling pending rps get request\n");
902 GNUNET_SCHEDULER_cancel (pending_req->request_task);
903 GNUNET_free (pending_req);
904}
905
906static void
907cancel_request (struct PendingReply *pending_rep)
908{
909 struct RPSPeer *rps_peer;
910
911 rps_peer = pending_rep->rps_peer;
912 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
913 rps_peer->pending_rep_tail,
914 pending_rep);
915 rps_peer->num_pending_reps--;
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917 "Cancelling rps get reply\n");
918 GNUNET_RPS_request_cancel (pending_rep->req_handle);
919 GNUNET_free (pending_rep);
920}
921
922void
923clean_peer (unsigned peer_index)
924{
925 struct PendingRequest *pending_req;
926
927 while (NULL != (pending_req = rps_peers[peer_index].pending_req_head))
928 {
929 cancel_pending_req (pending_req);
930 }
931 pending_req = rps_peers[peer_index].pending_req_head;
932 rps_disconnect_adapter (&rps_peers[peer_index],
933 &rps_peers[peer_index].rps_handle);
934 for (unsigned stat_type = STAT_TYPE_ROUNDS;
935 stat_type < STAT_TYPE_MAX;
936 stat_type++)
937 {
938 if (NULL != rps_peers[peer_index].h_stat_get[stat_type])
939 {
940 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
941 "(%u) did not yet receive stat value for `%s'\n",
942 rps_peers[peer_index].index,
943 stat_type_2_str (stat_type));
944 GNUNET_STATISTICS_get_cancel (
945 rps_peers[peer_index].h_stat_get[stat_type]);
946 }
947 }
948 if (NULL != rps_peers[peer_index].op)
949 {
950 GNUNET_TESTBED_operation_done (rps_peers[peer_index].op);
951 rps_peers[peer_index].op = NULL;
952 }
953}
954
955/**
956 * Task run on timeout to shut everything down.
957 */
958static void
959shutdown_op (void *cls)
960{
961 unsigned int i;
962 struct OpListEntry *entry;
963
964 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
965 "Shutdown task scheduled, going down.\n");
966 in_shutdown = GNUNET_YES;
967
968 if (NULL != shutdown_task)
969 {
970 GNUNET_SCHEDULER_cancel (shutdown_task);
971 shutdown_task = NULL;
972 }
973 if (NULL != post_test_task)
974 {
975 GNUNET_SCHEDULER_cancel (post_test_task);
976 post_test_task = NULL;
977 }
978 if (NULL != churn_task)
979 {
980 GNUNET_SCHEDULER_cancel (churn_task);
981 churn_task = NULL;
982 }
983 entry = oplist_head;
984 while (NULL != (entry = oplist_head))
985 {
986 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
987 "Operation still pending on shutdown (%u)\n",
988 entry->index);
989 GNUNET_TESTBED_operation_done (entry->op);
990 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
991 GNUNET_free (entry);
992 }
993 for (i = 0; i < num_peers; i++)
994 {
995 clean_peer (i);
996 }
997}
998
999static void
1000trigger_shutdown (void *cls)
1001{
1002 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1003 "Shutdown was triggerd by timeout, going down.\n");
1004 shutdown_task = NULL;
1005 GNUNET_SCHEDULER_shutdown ();
1006}
1007
1008
1009/**
1010 * Task run after #duration to collect statistics and potentially shut down.
1011 */
1012static void
1013post_test_op (void *cls)
1014{
1015 unsigned int i;
1016
1017 post_test_task = NULL;
1018 post_test = GNUNET_YES;
1019 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1020 "Post test task scheduled.\n");
1021 if (NULL != churn_task)
1022 {
1023 GNUNET_SCHEDULER_cancel (churn_task);
1024 churn_task = NULL;
1025 }
1026 for (i = 0; i < num_peers; i++)
1027 {
1028 if (NULL != rps_peers[i].op)
1029 {
1030 GNUNET_TESTBED_operation_done (rps_peers[i].op);
1031 rps_peers[i].op = NULL;
1032 }
1033 if (NULL != cur_test_run.post_test)
1034 {
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing post_test for peer %u\n", i);
1036 cur_test_run.post_test (&rps_peers[i]);
1037 }
1038 }
1039 /* If we do not collect statistics, shut down directly */
1040 if (NO_COLLECT_STATISTICS == cur_test_run.have_collect_statistics ||
1041 GNUNET_YES == check_statistics_collect_completed())
1042 {
1043 GNUNET_SCHEDULER_cancel (shutdown_task);
1044 shutdown_task = NULL;
1045 GNUNET_SCHEDULER_shutdown ();
1046 }
1047}
1048
1049
1050/**
1051 * Seed peers.
1052 */
1053static void
1054seed_peers (void *cls)
1055{
1056 struct RPSPeer *peer = cls;
1057 unsigned int amount;
1058 unsigned int i;
1059
1060 // TODO if malicious don't seed mal peers
1061 amount = round (.5 * num_peers);
1062
1063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
1064 for (i = 0 ; i < amount ; i++)
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
1066 i,
1067 GNUNET_i2s (&rps_peer_ids[i]));
1068
1069 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
1070}
1071
1072
1073/**
1074 * Get the id of peer i.
1075 */
1076 void
1077info_cb (void *cb_cls,
1078 struct GNUNET_TESTBED_Operation *op,
1079 const struct GNUNET_TESTBED_PeerInformation *pinfo,
1080 const char *emsg)
1081{
1082 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
1083
1084 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1085 {
1086 return;
1087 }
1088
1089 if (NULL == pinfo || NULL != emsg)
1090 {
1091 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
1092 GNUNET_TESTBED_operation_done (entry->op);
1093 return;
1094 }
1095
1096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1097 "Peer %u is %s\n",
1098 entry->index,
1099 GNUNET_i2s (pinfo->result.id));
1100
1101 rps_peer_ids[entry->index] = *(pinfo->result.id);
1102 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
1103
1104 GNUNET_assert (GNUNET_OK ==
1105 GNUNET_CONTAINER_multipeermap_put (peer_map,
1106 &rps_peer_ids[entry->index],
1107 &rps_peers[entry->index],
1108 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1109 tofile ("/tmp/rps/peer_ids",
1110 "%u\t%s\n",
1111 entry->index,
1112 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
1113
1114 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1115 GNUNET_TESTBED_operation_done (entry->op);
1116 GNUNET_free (entry);
1117}
1118
1119
1120/**
1121 * Callback to be called when RPS service connect operation is completed
1122 *
1123 * @param cls the callback closure from functions generating an operation
1124 * @param op the operation that has been finished
1125 * @param ca_result the RPS service handle returned from rps_connect_adapter
1126 * @param emsg error message in case the operation has failed; will be NULL if
1127 * operation has executed successfully.
1128 */
1129static void
1130rps_connect_complete_cb (void *cls,
1131 struct GNUNET_TESTBED_Operation *op,
1132 void *ca_result,
1133 const char *emsg)
1134{
1135 struct RPSPeer *rps_peer = cls;
1136 struct GNUNET_RPS_Handle *rps = ca_result;
1137
1138 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1139 {
1140 return;
1141 }
1142
1143 rps_peer->rps_handle = rps;
1144 rps_peer->online = GNUNET_YES;
1145 num_peers_online++;
1146
1147 GNUNET_assert (op == rps_peer->op);
1148 if (NULL != emsg)
1149 {
1150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1151 "Failed to connect to RPS service: %s\n",
1152 emsg);
1153 ok = 1;
1154 GNUNET_SCHEDULER_shutdown ();
1155 return;
1156 }
1157
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1159 "Started client successfully (%u)\n",
1160 rps_peer->index);
1161
1162 cur_test_run.main_test (rps_peer);
1163}
1164
1165
1166/**
1167 * Adapter function called to establish a connection to
1168 * the RPS service.
1169 *
1170 * @param cls closure
1171 * @param cfg configuration of the peer to connect to; will be available until
1172 * GNUNET_TESTBED_operation_done() is called on the operation returned
1173 * from GNUNET_TESTBED_service_connect()
1174 * @return service handle to return in 'op_result', NULL on error
1175 */
1176static void *
1177rps_connect_adapter (void *cls,
1178 const struct GNUNET_CONFIGURATION_Handle *cfg)
1179{
1180 struct GNUNET_RPS_Handle *h;
1181
1182 h = GNUNET_RPS_connect (cfg);
1183
1184 if (NULL != cur_test_run.pre_test)
1185 cur_test_run.pre_test (cls, h);
1186
1187 return h;
1188}
1189
1190/**
1191 * Called to open a connection to the peer's statistics
1192 *
1193 * @param cls peer context
1194 * @param cfg configuration of the peer to connect to; will be available until
1195 * GNUNET_TESTBED_operation_done() is called on the operation returned
1196 * from GNUNET_TESTBED_service_connect()
1197 * @return service handle to return in 'op_result', NULL on error
1198 */
1199static void *
1200stat_connect_adapter (void *cls,
1201 const struct GNUNET_CONFIGURATION_Handle *cfg)
1202{
1203 struct RPSPeer *peer = cls;
1204
1205 peer->stats_h = GNUNET_STATISTICS_create ("rps-profiler", cfg);
1206 return peer->stats_h;
1207}
1208
1209/**
1210 * Called to disconnect from peer's statistics service
1211 *
1212 * @param cls peer context
1213 * @param op_result service handle returned from the connect adapter
1214 */
1215static void
1216stat_disconnect_adapter (void *cls, void *op_result)
1217{
1218 struct RPSPeer *peer = cls;
1219
1220 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1221 // (peer->stats_h, "core", "# peers connected",
1222 // stat_iterator, peer));
1223 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1224 // (peer->stats_h, "nse", "# peers connected",
1225 // stat_iterator, peer));
1226 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
1227 peer->stats_h = NULL;
1228}
1229
1230/**
1231 * Called after successfully opening a connection to a peer's statistics
1232 * service; we register statistics monitoring for CORE and NSE here.
1233 *
1234 * @param cls the callback closure from functions generating an operation
1235 * @param op the operation that has been finished
1236 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
1237 * @param emsg error message in case the operation has failed; will be NULL if
1238 * operation has executed successfully.
1239 */
1240static void
1241stat_complete_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1242 void *ca_result, const char *emsg )
1243{
1244 //struct GNUNET_STATISTICS_Handle *sh = ca_result;
1245 //struct RPSPeer *peer = (struct RPSPeer *) cls;
1246
1247 if (NULL != emsg)
1248 {
1249 GNUNET_break (0);
1250 return;
1251 }
1252 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1253 // (sh, "core", "# peers connected",
1254 // stat_iterator, peer));
1255 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1256 // (sh, "nse", "# peers connected",
1257 // stat_iterator, peer));
1258}
1259
1260
1261/**
1262 * Adapter function called to destroy connection to
1263 * RPS service.
1264 *
1265 * @param cls closure
1266 * @param op_result service handle returned from the connect adapter
1267 */
1268static void
1269rps_disconnect_adapter (void *cls,
1270 void *op_result)
1271{
1272 struct RPSPeer *peer = cls;
1273 struct GNUNET_RPS_Handle *h = op_result;
1274 struct PendingReply *pending_rep;
1275
1276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1277 "disconnect_adapter (%u)\n",
1278 peer->index);
1279 GNUNET_assert (NULL != peer);
1280 if (NULL != peer->rps_handle)
1281 {
1282 while (NULL != (pending_rep = peer->pending_rep_head))
1283 {
1284 cancel_request (pending_rep);
1285 }
1286 GNUNET_assert (h == peer->rps_handle);
1287 GNUNET_RPS_disconnect (h);
1288 peer->rps_handle = NULL;
1289 }
1290}
1291
1292
1293/***********************************************************************
1294 * Definition of tests
1295***********************************************************************/
1296
1297/**
1298 * Callback to call on receipt of a reply
1299 *
1300 * @param cls closure
1301 * @param n number of peers
1302 * @param recv_peers the received peers
1303 */
1304static void
1305default_reply_handle (void *cls,
1306 uint64_t n,
1307 const struct GNUNET_PeerIdentity *recv_peers)
1308{
1309 struct RPSPeer *rps_peer;
1310 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1311 unsigned int i;
1312
1313 rps_peer = pending_rep->rps_peer;
1314 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1315 rps_peer->pending_rep_tail,
1316 pending_rep);
1317 rps_peer->num_pending_reps--;
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1319 "[%s] got %" PRIu64 " peers:\n",
1320 GNUNET_i2s (rps_peer->peer_id),
1321 n);
1322
1323 for (i = 0; i < n; i++)
1324 {
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1326 "%u: %s\n",
1327 i,
1328 GNUNET_i2s (&recv_peers[i]));
1329
1330 rps_peer->num_recv_ids++;
1331 }
1332
1333 if (GNUNET_YES != post_test) return;
1334 if (HAVE_QUICK_QUIT != cur_test_run.have_quick_quit) return;
1335 if (0 == evaluate())
1336 {
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1338 "Test succeeded before end of duration\n");
1339 if (NULL != post_test_task) GNUNET_SCHEDULER_cancel (post_test_task);
1340 post_test_task = GNUNET_SCHEDULER_add_now (&post_test_op, NULL);
1341 GNUNET_assert (NULL != post_test_task);
1342 }
1343}
1344
1345/**
1346 * Request random peers.
1347 */
1348static void
1349request_peers (void *cls)
1350{
1351 struct PendingRequest *pending_req = cls;
1352 struct RPSPeer *rps_peer;
1353 struct PendingReply *pending_rep;
1354
1355 rps_peer = pending_req->rps_peer;
1356 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
1357 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1358 rps_peer->pending_req_tail,
1359 pending_req);
1360 rps_peer->num_pending_reqs--;
1361 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test) return;
1362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1363 "Requesting one peer\n");
1364 pending_rep = GNUNET_new (struct PendingReply);
1365 pending_rep->rps_peer = rps_peer;
1366 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
1367 1,
1368 cur_test_run.reply_handle,
1369 pending_rep);
1370 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
1371 rps_peer->pending_rep_tail,
1372 pending_rep);
1373 rps_peer->num_pending_reps++;
1374}
1375
1376
1377/**
1378 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
1379 * issued, nor replied
1380 */
1381void
1382schedule_missing_requests (struct RPSPeer *rps_peer)
1383{
1384 unsigned int i;
1385 struct PendingRequest *pending_req;
1386
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 "Scheduling %u - %u missing requests\n",
1389 rps_peer->num_ids_to_request,
1390 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
1391 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
1392 rps_peer->num_ids_to_request);
1393 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
1394 i < rps_peer->num_ids_to_request; i++)
1395 {
1396 pending_req = GNUNET_new (struct PendingRequest);
1397 pending_req->rps_peer = rps_peer;
1398 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
1399 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1400 cur_test_run.request_interval * i),
1401 request_peers,
1402 pending_req);
1403 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
1404 rps_peer->pending_req_tail,
1405 pending_req);
1406 rps_peer->num_pending_reqs++;
1407 }
1408}
1409
1410void
1411cancel_pending_req_rep (struct RPSPeer *rps_peer)
1412{
1413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1414 "Cancelling all (pending) requests.\n");
1415 while (NULL != rps_peer->pending_req_head)
1416 cancel_pending_req (rps_peer->pending_req_head);
1417 GNUNET_assert (0 == rps_peer->num_pending_reqs);
1418 while (NULL != rps_peer->pending_rep_head)
1419 cancel_request (rps_peer->pending_rep_head);
1420 GNUNET_assert (0 == rps_peer->num_pending_reps);
1421}
1422
1423/***********************************
1424 * MALICIOUS
1425***********************************/
1426
1427/**
1428 * Initialise only non-mal RPSPeers
1429 */
1430static void mal_init_peer (struct RPSPeer *rps_peer)
1431{
1432 if (rps_peer->index >= round (portion * num_peers))
1433 rps_peer->num_ids_to_request = 1;
1434}
1435
1436
1437/**
1438 * @brief Set peers to (non-)malicious before execution
1439 *
1440 * Of signature #PreTest
1441 *
1442 * @param rps_peer the peer to set (non-) malicious
1443 * @param h the handle to the service
1444 */
1445static void
1446mal_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
1447{
1448 #ifdef ENABLE_MALICIOUS
1449 uint32_t num_mal_peers;
1450
1451 GNUNET_assert ( (1 >= portion) &&
1452 (0 < portion) );
1453 num_mal_peers = round (portion * num_peers);
1454
1455 if (rps_peer->index < num_mal_peers)
1456 {
1457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1458 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
1459 rps_peer->index,
1460 GNUNET_i2s (rps_peer->peer_id),
1461 num_mal_peers);
1462
1463 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
1464 rps_peer_ids, target_peer);
1465 }
1466 #endif /* ENABLE_MALICIOUS */
1467}
1468
1469static void
1470mal_cb (struct RPSPeer *rps_peer)
1471{
1472 uint32_t num_mal_peers;
1473
1474 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1475 {
1476 return;
1477 }
1478
1479 #ifdef ENABLE_MALICIOUS
1480 GNUNET_assert ( (1 >= portion) &&
1481 (0 < portion) );
1482 num_mal_peers = round (portion * num_peers);
1483
1484 if (rps_peer->index >= num_mal_peers)
1485 { /* It's useless to ask a malicious peer about a random sample -
1486 it's not sampling */
1487 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1488 seed_peers, rps_peer);
1489 schedule_missing_requests (rps_peer);
1490 }
1491 #endif /* ENABLE_MALICIOUS */
1492}
1493
1494/***********************************
1495 * CHURN
1496***********************************/
1497
1498static void
1499churn (void *cls);
1500
1501/**
1502 * @brief Starts churn
1503 *
1504 * Has signature of #MainTest
1505 *
1506 * This is not implemented too nicely as this is called for each peer, but we
1507 * only need to call it once. (Yes we check that we only schedule the task
1508 * once.)
1509 *
1510 * @param rps_peer The peer it's called for
1511 */
1512static void
1513churn_test_cb (struct RPSPeer *rps_peer)
1514{
1515 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1516 {
1517 return;
1518 }
1519
1520 /* Start churn */
1521 if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
1522 {
1523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1524 "Starting churn task\n");
1525 churn_task = GNUNET_SCHEDULER_add_delayed (
1526 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1527 churn,
1528 NULL);
1529 } else {
1530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1531 "Not starting churn task\n");
1532 }
1533
1534 schedule_missing_requests (rps_peer);
1535}
1536
1537/***********************************
1538 * PROFILER
1539***********************************/
1540
1541/**
1542 * Callback to be called when RPS service is started or stopped at peers
1543 *
1544 * @param cls NULL
1545 * @param op the operation handle
1546 * @param emsg NULL on success; otherwise an error description
1547 */
1548static void
1549churn_cb (void *cls,
1550 struct GNUNET_TESTBED_Operation *op,
1551 const char *emsg)
1552{
1553 // FIXME
1554 struct OpListEntry *entry = cls;
1555
1556 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1557 {
1558 return;
1559 }
1560
1561 GNUNET_TESTBED_operation_done (entry->op);
1562 if (NULL != emsg)
1563 {
1564 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1565 GNUNET_SCHEDULER_shutdown ();
1566 return;
1567 }
1568 GNUNET_assert (0 != entry->delta);
1569
1570 num_peers_online += entry->delta;
1571
1572 if (PEER_GO_OFFLINE == entry->delta)
1573 { /* Peer hopefully just went offline */
1574 if (GNUNET_YES != rps_peers[entry->index].online)
1575 {
1576 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1577 "peer %s was expected to go offline but is still marked as online\n",
1578 GNUNET_i2s (rps_peers[entry->index].peer_id));
1579 GNUNET_break (0);
1580 }
1581 else
1582 {
1583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1584 "peer %s probably went offline as expected\n",
1585 GNUNET_i2s (rps_peers[entry->index].peer_id));
1586 }
1587 rps_peers[entry->index].online = GNUNET_NO;
1588 }
1589
1590 else if (PEER_GO_ONLINE < entry->delta)
1591 { /* Peer hopefully just went online */
1592 if (GNUNET_NO != rps_peers[entry->index].online)
1593 {
1594 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1595 "peer %s was expected to go online but is still marked as offline\n",
1596 GNUNET_i2s (rps_peers[entry->index].peer_id));
1597 GNUNET_break (0);
1598 }
1599 else
1600 {
1601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1602 "peer %s probably went online as expected\n",
1603 GNUNET_i2s (rps_peers[entry->index].peer_id));
1604 if (NULL != cur_test_run.pre_test)
1605 {
1606 cur_test_run.pre_test (&rps_peers[entry->index],
1607 rps_peers[entry->index].rps_handle);
1608 schedule_missing_requests (&rps_peers[entry->index]);
1609 }
1610 }
1611 rps_peers[entry->index].online = GNUNET_YES;
1612 }
1613 else
1614 {
1615 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1616 "Invalid value for delta: %i\n", entry->delta);
1617 GNUNET_break (0);
1618 }
1619
1620 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1621 rps_peers[entry->index].entry_op_manage = NULL;
1622 GNUNET_free (entry);
1623 //if (num_peers_in_round[current_round] == peers_running)
1624 // run_round ();
1625}
1626
1627/**
1628 * @brief Set the rps-service up or down for a specific peer
1629 *
1630 * @param i index of action
1631 * @param j index of peer
1632 * @param delta (#PEER_ONLINE_DELTA) down (-1) or up (1)
1633 * @param prob_go_on_off the probability of the action
1634 */
1635static void
1636manage_service_wrapper (unsigned int i, unsigned int j,
1637 enum PEER_ONLINE_DELTA delta,
1638 double prob_go_on_off)
1639{
1640 struct OpListEntry *entry = NULL;
1641 uint32_t prob;
1642
1643 /* make sure that management operation is not already scheduled */
1644 if (NULL != rps_peers[j].entry_op_manage)
1645 {
1646 return;
1647 }
1648
1649 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1650 UINT32_MAX);
1651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1652 "%u. selected peer (%u: %s) is %s.\n",
1653 i,
1654 j,
1655 GNUNET_i2s (rps_peers[j].peer_id),
1656 (PEER_GO_ONLINE == delta) ? "online" : "offline");
1657 if (prob < prob_go_on_off * UINT32_MAX)
1658 {
1659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1660 "%s goes %s\n",
1661 GNUNET_i2s (rps_peers[j].peer_id),
1662 (PEER_GO_OFFLINE == delta) ? "offline" : "online");
1663
1664 if (PEER_GO_OFFLINE == delta)
1665 cancel_pending_req_rep (&rps_peers[j]);
1666 entry = make_oplist_entry ();
1667 entry->delta = delta;
1668 entry->index = j;
1669 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1670 testbed_peers[j],
1671 "rps",
1672 &churn_cb,
1673 entry,
1674 (PEER_GO_OFFLINE == delta) ? 0 : 1);
1675 rps_peers[j].entry_op_manage = entry;
1676 }
1677}
1678
1679
1680static void
1681churn (void *cls)
1682{
1683 unsigned int i;
1684 unsigned int j;
1685 double portion_online;
1686 unsigned int *permut;
1687 double prob_go_offline;
1688 double portion_go_online;
1689 double portion_go_offline;
1690
1691 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1692 {
1693 return;
1694 }
1695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1696 "Churn function executing\n");
1697
1698 churn_task = NULL; /* Should be invalid by now */
1699
1700 /* Compute the probability for an online peer to go offline
1701 * this round */
1702 portion_online = num_peers_online * 1.0 / num_peers;
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1704 "Portion online: %f\n",
1705 portion_online);
1706 portion_go_online = ((1 - portion_online) * .5 * .66);
1707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1708 "Portion that should go online: %f\n",
1709 portion_go_online);
1710 portion_go_offline = (portion_online + portion_go_online) - .75;
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1712 "Portion that probably goes offline: %f\n",
1713 portion_go_offline);
1714 prob_go_offline = portion_go_offline / (portion_online * .5);
1715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1716 "Probability of a selected online peer to go offline: %f\n",
1717 prob_go_offline);
1718
1719 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1720 (unsigned int) num_peers);
1721
1722 /* Go over 50% randomly chosen peers */
1723 for (i = 0; i < .5 * num_peers; i++)
1724 {
1725 j = permut[i];
1726
1727 /* If online, shut down with certain probability */
1728 if (GNUNET_YES == rps_peers[j].online)
1729 {
1730 manage_service_wrapper (i, j, -1, prob_go_offline);
1731 }
1732
1733 /* If offline, restart with certain probability */
1734 else if (GNUNET_NO == rps_peers[j].online)
1735 {
1736 manage_service_wrapper (i, j, 1, 0.66);
1737 }
1738 }
1739
1740 GNUNET_free (permut);
1741
1742 churn_task = GNUNET_SCHEDULER_add_delayed (
1743 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1744 churn,
1745 NULL);
1746}
1747
1748
1749/**
1750 * Initialise given RPSPeer
1751 */
1752static void profiler_init_peer (struct RPSPeer *rps_peer)
1753{
1754 if (num_peers - 1 == rps_peer->index)
1755 {
1756 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1757 } else {
1758 rps_peer->num_ids_to_request = 0;
1759 }
1760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer shall request %i peers\n",
1761 rps_peer->num_ids_to_request);
1762}
1763
1764
1765/**
1766 * Callback to call on receipt of a reply
1767 *
1768 * @param cls closure
1769 * @param n number of peers
1770 * @param recv_peers the received peers
1771 */
1772static void
1773profiler_reply_handle (void *cls,
1774 uint64_t n,
1775 const struct GNUNET_PeerIdentity *recv_peers)
1776{
1777 struct RPSPeer *rps_peer;
1778 struct RPSPeer *rcv_rps_peer;
1779 char *file_name;
1780 char *file_name_dh;
1781 char *file_name_dhr;
1782 char *file_name_dhru;
1783 unsigned int i;
1784 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1785
1786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "profiler_reply_handle()\n");
1787 rps_peer = pending_rep->rps_peer;
1788 file_name = "/tmp/rps/received_ids";
1789 file_name_dh = "/tmp/rps/diehard_input";
1790 file_name_dhr = "/tmp/rps/diehard_input_raw";
1791 file_name_dhru = "/tmp/rps/diehard_input_raw_aligned";
1792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1793 "[%s] got %" PRIu64 " peers:\n",
1794 GNUNET_i2s (rps_peer->peer_id),
1795 n);
1796 for (i = 0; i < n; i++)
1797 {
1798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1799 "%u: %s\n",
1800 i,
1801 GNUNET_i2s (&recv_peers[i]));
1802 tofile (file_name,
1803 "%s\n",
1804 GNUNET_i2s_full (&recv_peers[i]));
1805 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1806 GNUNET_assert (NULL != rcv_rps_peer);
1807 tofile (file_name_dh,
1808 "%" PRIu32 "\n",
1809 (uint32_t) rcv_rps_peer->index);
1810 to_file_raw (file_name_dhr,
1811 (char *) &rcv_rps_peer->index,
1812 sizeof (uint32_t));
1813 to_file_raw_unaligned (file_name_dhru,
1814 (char *) &rcv_rps_peer->index,
1815 sizeof (uint32_t),
1816 bits_needed);
1817 }
1818 default_reply_handle (cls, n, recv_peers);
1819}
1820
1821
1822static void
1823profiler_cb (struct RPSPeer *rps_peer)
1824{
1825 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1826 {
1827 return;
1828 }
1829
1830 /* Start churn */
1831 if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
1832 {
1833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1834 "Starting churn task\n");
1835 churn_task = GNUNET_SCHEDULER_add_delayed (
1836 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1837 churn,
1838 NULL);
1839 } else {
1840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1841 "Not starting churn task\n");
1842 }
1843
1844 /* Only request peer ids at one peer.
1845 * (It's the before-last because last one is target of the focussed attack.)
1846 */
1847 if (eval_peer == rps_peer)
1848 schedule_missing_requests (rps_peer);
1849}
1850
1851/**
1852 * Function called from #profiler_eval with a filename.
1853 *
1854 * @param cls closure
1855 * @param filename complete filename (absolute path)
1856 * @return #GNUNET_OK to continue to iterate,
1857 * #GNUNET_NO to stop iteration with no error,
1858 * #GNUNET_SYSERR to abort iteration with error!
1859 */
1860int
1861file_name_cb (void *cls, const char *filename)
1862{
1863 if (NULL != strstr (filename, "sampler_el"))
1864 {
1865 struct RPS_SamplerElement *s_elem;
1866 struct GNUNET_CRYPTO_AuthKey auth_key;
1867 const char *key_char;
1868 uint32_t i;
1869
1870 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1871 tofile (filename, "--------------------------\n");
1872
1873 auth_key = string_to_auth_key (key_char);
1874 s_elem = RPS_sampler_elem_create ();
1875 RPS_sampler_elem_set (s_elem, auth_key);
1876
1877 for (i = 0; i < num_peers; i++)
1878 {
1879 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1880 }
1881 RPS_sampler_elem_destroy (s_elem);
1882 }
1883 return GNUNET_OK;
1884}
1885
1886/**
1887 * This is run after the test finished.
1888 *
1889 * Compute all perfect samples.
1890 */
1891int
1892profiler_eval (void)
1893{
1894 /* Compute perfect sample for each sampler element */
1895 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1896 {
1897 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1898 }
1899
1900 return evaluate ();
1901}
1902
1903static uint32_t fac (uint32_t x)
1904{
1905 if (1 >= x)
1906 {
1907 return x;
1908 }
1909 return x * fac (x - 1);
1910}
1911
1912static uint32_t binom (uint32_t n, uint32_t k)
1913{
1914 //GNUNET_assert (n >= k);
1915 if (k > n) return 0;
1916 if (0 > n) return 0;
1917 if (0 > k) return 0;
1918 if (0 == k) return 1;
1919 return fac (n)
1920 /
1921 fac(k) * fac(n - k);
1922}
1923
1924/**
1925 * @brief is b in view of a?
1926 *
1927 * @param a
1928 * @param b
1929 *
1930 * @return
1931 */
1932static int is_in_view (uint32_t a, uint32_t b)
1933{
1934 uint32_t i;
1935 for (i = 0; i < rps_peers[a].cur_view_count; i++)
1936 {
1937 if (0 == memcmp (rps_peers[b].peer_id,
1938 &rps_peers[a].cur_view[i],
1939 sizeof (struct GNUNET_PeerIdentity)))
1940 {
1941 return GNUNET_YES;
1942 }
1943 }
1944 return GNUNET_NO;
1945}
1946
1947static uint32_t get_idx_of_pid (const struct GNUNET_PeerIdentity *pid)
1948{
1949 uint32_t i;
1950
1951 for (i = 0; i < num_peers; i++)
1952 {
1953 if (0 == memcmp (pid,
1954 rps_peers[i].peer_id,
1955 sizeof (struct GNUNET_PeerIdentity)))
1956 {
1957 return i;
1958 }
1959 }
1960 //return 0; /* Should not happen - make compiler happy */
1961 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1962 "No known _PeerIdentity %s!\n",
1963 GNUNET_i2s_full (pid));
1964 GNUNET_assert (0);
1965}
1966
1967/**
1968 * @brief Counts number of peers in view of a that have b in their view
1969 *
1970 * @param a
1971 * @param uint32_tb
1972 *
1973 * @return
1974 */
1975static uint32_t count_containing_views (uint32_t a, uint32_t b)
1976{
1977 uint32_t i;
1978 uint32_t peer_idx;
1979 uint32_t count = 0;
1980
1981 for (i = 0; i < rps_peers[a].cur_view_count; i++)
1982 {
1983 peer_idx = get_idx_of_pid (&rps_peers[a].cur_view[i]);
1984 if (GNUNET_YES == is_in_view (peer_idx, b))
1985 {
1986 count++;
1987 }
1988 }
1989 return count;
1990}
1991
1992/**
1993 * @brief Computes the probability for each other peer to be selected by the
1994 * sampling process based on the views of all peers
1995 *
1996 * @param peer_idx index of the peer that is about to sample
1997 */
1998static void compute_probabilities (uint32_t peer_idx)
1999{
2000 //double probs[num_peers] = { 0 };
2001 double probs[num_peers];
2002 size_t probs_as_str_size = (num_peers * 10 + 1) * sizeof (char);
2003 char *probs_as_str = GNUNET_malloc (probs_as_str_size);
2004 char *probs_as_str_cpy;
2005 uint32_t i;
2006 double prob_push;
2007 double prob_pull;
2008 uint32_t view_size;
2009 uint32_t cont_views;
2010 uint32_t number_of_being_in_pull_events;
2011 int tmp;
2012 uint32_t count_non_zero_prob = 0;
2013
2014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2015 "Computing probabilities for peer %" PRIu32 "\n", peer_idx);
2016 /* Firstly without knowledge of old views */
2017 for (i = 0; i < num_peers; i++)
2018 {
2019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2020 "\tfor peer %" PRIu32 ":\n", i);
2021 view_size = rps_peers[i].cur_view_count;
2022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2023 "\t\tview_size: %" PRIu32 "\n", view_size);
2024 /* For peer i the probability of being sampled is
2025 * evenly distributed among all possibly observed peers. */
2026 /* We could have observed a peer in three cases:
2027 * 1. peer sent a push
2028 * 2. peer was contained in a pull reply
2029 * 3. peer was in history (sampler) - ignored for now */
2030 /* 1. Probability of having received a push from peer i */
2031 if ((GNUNET_YES == is_in_view (i, peer_idx)) &&
2032 (1 <= (0.45 * view_size)))
2033 {
2034 if (0 == binom (view_size, 0.45 * view_size)) prob_push = 0;
2035 else
2036 {
2037 prob_push = 1.0 * binom (0.45 * view_size, 1)
2038 /
2039 binom (view_size, 0.45 * view_size);
2040 }
2041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2042 "\t\t%" PRIu32 " is in %" PRIu32 "'s view, prob: %f\n",
2043 peer_idx,
2044 i,
2045 prob_push);
2046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2047 "\t\tposs choices from view: %" PRIu32 ", containing i: %" PRIu32 "\n",
2048 binom (view_size, 0.45 * view_size),
2049 binom (0.45 * view_size, 1));
2050 } else {
2051 prob_push = 0;
2052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2053 "\t\t%" PRIu32 " is not in %" PRIu32 "'s view, prob: 0\n",
2054 peer_idx,
2055 i);
2056 }
2057 /* 2. Probability of peer i being contained in pulls */
2058 view_size = rps_peers[peer_idx].cur_view_count;
2059 cont_views = count_containing_views (peer_idx, i);
2060 number_of_being_in_pull_events =
2061 (binom (view_size, 0.45 * view_size) -
2062 binom (view_size - cont_views, 0.45 * view_size));
2063 if (0 != number_of_being_in_pull_events)
2064 {
2065 prob_pull = number_of_being_in_pull_events
2066 /
2067 (1.0 * binom (view_size, 0.45 * view_size));
2068 } else
2069 {
2070 prob_pull = 0;
2071 }
2072 probs[i] = prob_push + prob_pull - (prob_push * prob_pull);
2073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2074 "\t\t%" PRIu32 " has %" PRIu32 " of %" PRIu32
2075 " peers in its view who know %" PRIu32 " prob: %f\n",
2076 peer_idx,
2077 cont_views,
2078 view_size,
2079 i,
2080 prob_pull);
2081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2082 "\t\tnumber of possible pull combinations: %" PRIu32 "\n",
2083 binom (view_size, 0.45 * view_size));
2084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2085 "\t\tnumber of possible pull combinations without %" PRIu32
2086 ": %" PRIu32 "\n",
2087 i,
2088 binom (view_size - cont_views, 0.45 * view_size));
2089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2090 "\t\tnumber of possible pull combinations with %" PRIu32
2091 ": %" PRIu32 "\n",
2092 i,
2093 number_of_being_in_pull_events);
2094
2095 if (0 != probs[i]) count_non_zero_prob++;
2096 }
2097 /* normalize */
2098 if (0 != count_non_zero_prob)
2099 {
2100 for (i = 0; i < num_peers; i++)
2101 {
2102 probs[i] = probs[i] * (1.0 / count_non_zero_prob);
2103 }
2104 } else {
2105 for (i = 0; i < num_peers; i++)
2106 {
2107 probs[i] = 0;
2108 }
2109 }
2110 /* str repr */
2111 for (i = 0; i < num_peers; i++)
2112 {
2113 probs_as_str_cpy = GNUNET_strndup (probs_as_str, probs_as_str_size);
2114 tmp = GNUNET_snprintf (probs_as_str,
2115 probs_as_str_size,
2116 "%s %7.6f", probs_as_str_cpy, probs[i]);
2117 GNUNET_free (probs_as_str_cpy);
2118 GNUNET_assert (0 <= tmp);
2119 }
2120
2121 to_file_w_len (rps_peers[peer_idx].file_name_probs,
2122 probs_as_str_size,
2123 probs_as_str);
2124 GNUNET_free (probs_as_str);
2125}
2126
2127/**
2128 * @brief This counts the number of peers in which views a given peer occurs.
2129 *
2130 * It also stores this value in the rps peer.
2131 *
2132 * @param peer_idx the index of the peer to count the representation
2133 *
2134 * @return the number of occurrences
2135 */
2136static uint32_t count_peer_in_views_2 (uint32_t peer_idx)
2137{
2138 uint32_t i, j;
2139 uint32_t count = 0;
2140
2141 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2142 {
2143 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2144 {
2145 if (0 == memcmp (rps_peers[peer_idx].peer_id,
2146 &rps_peers[i].cur_view[j],
2147 sizeof (struct GNUNET_PeerIdentity)))
2148 {
2149 count++;
2150 break;
2151 }
2152 }
2153 }
2154 rps_peers[peer_idx].count_in_views = count;
2155 return count;
2156}
2157
2158static uint32_t cumulated_view_sizes ()
2159{
2160 uint32_t i;
2161
2162 view_sizes = 0;
2163 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2164 {
2165 view_sizes += rps_peers[i].cur_view_count;
2166 }
2167 return view_sizes;
2168}
2169
2170static void count_peer_in_views (uint32_t *count_peers)
2171{
2172 uint32_t i, j;
2173
2174 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2175 {
2176 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2177 {
2178 if (0 == memcmp (rps_peers[i].peer_id,
2179 &rps_peers[i].cur_view[j],
2180 sizeof (struct GNUNET_PeerIdentity)))
2181 {
2182 count_peers[i]++;
2183 }
2184 }
2185 }
2186}
2187
2188void compute_diversity ()
2189{
2190 uint32_t i;
2191 /* ith entry represents the numer of occurrences in other peer's views */
2192 uint32_t *count_peers = GNUNET_new_array (num_peers, uint32_t);
2193 uint32_t views_total_size;
2194 double expected;
2195 /* deviation from expected number of peers */
2196 double *deviation = GNUNET_new_array (num_peers, double);
2197
2198 views_total_size = 0;
2199 expected = 0;
2200
2201 /* For each peer count its representation in other peer's views*/
2202 for (i = 0; i < num_peers; i++) /* Peer to count */
2203 {
2204 views_total_size += rps_peers[i].cur_view_count;
2205 count_peer_in_views (count_peers);
2206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2207 "Counted representation of %" PRIu32 "th peer [%s]: %" PRIu32"\n",
2208 i,
2209 GNUNET_i2s (rps_peers[i].peer_id),
2210 count_peers[i]);
2211 }
2212
2213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2214 "size of all views combined: %" PRIu32 "\n",
2215 views_total_size);
2216 expected = ((double) 1/num_peers) * views_total_size;
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2218 "Expected number of occurrences of each peer in all views: %f\n",
2219 expected);
2220 for (i = 0; i < num_peers; i++) /* Peer to count */
2221 {
2222 deviation[i] = expected - count_peers[i];
2223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2224 "Deviation from expectation: %f\n", deviation[i]);
2225 }
2226 GNUNET_free (count_peers);
2227 GNUNET_free (deviation);
2228}
2229
2230void print_view_sizes()
2231{
2232 uint32_t i;
2233
2234 for (i = 0; i < num_peers; i++) /* Peer to count */
2235 {
2236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2237 "View size of %" PRIu32 ". [%s] is %" PRIu32 "\n",
2238 i,
2239 GNUNET_i2s (rps_peers[i].peer_id),
2240 rps_peers[i].cur_view_count);
2241 }
2242}
2243
2244void all_views_updated_cb()
2245{
2246 compute_diversity();
2247 print_view_sizes();
2248}
2249
2250void view_update_cb (void *cls,
2251 uint64_t view_size,
2252 const struct GNUNET_PeerIdentity *peers)
2253{
2254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2255 "View was updated (%" PRIu64 ")\n", view_size);
2256 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
2257 to_file ("/tmp/rps/view_sizes.txt",
2258 "%" PRIu64 " %" PRIu32 "",
2259 rps_peer->index,
2260 view_size);
2261 for (int i = 0; i < view_size; i++)
2262 {
2263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2264 "\t%s\n", GNUNET_i2s (&peers[i]));
2265 }
2266 GNUNET_array_grow (rps_peer->cur_view,
2267 rps_peer->cur_view_count,
2268 view_size);
2269 //*rps_peer->cur_view = *peers;
2270 GNUNET_memcpy (rps_peer->cur_view,
2271 peers,
2272 view_size * sizeof (struct GNUNET_PeerIdentity));
2273 to_file ("/tmp/rps/count_in_views.txt",
2274 "%" PRIu64 " %" PRIu32 "",
2275 rps_peer->index,
2276 count_peer_in_views_2 (rps_peer->index));
2277 cumulated_view_sizes();
2278 if (0 != view_size)
2279 {
2280 to_file ("/tmp/rps/repr.txt",
2281 "%" PRIu64 /* index */
2282 " %" PRIu32 /* occurrence in views */
2283 " %" PRIu32 /* view sizes */
2284 " %f" /* fraction of repr in views */
2285 " %f" /* average view size */
2286 " %f" /* prob of occurrence in view slot */
2287 " %f" "", /* exp frac of repr in views */
2288 rps_peer->index,
2289 count_peer_in_views_2 (rps_peer->index),
2290 view_sizes,
2291 count_peer_in_views_2 (rps_peer->index) / (view_size * 1.0), /* fraction of representation in views */
2292 view_sizes / (view_size * 1.0), /* average view size */
2293 1.0 /view_size, /* prob of occurrence in view slot */
2294 (1.0/view_size) * (view_sizes/view_size) /* expected fraction of repr in views */
2295 );
2296 }
2297 compute_probabilities (rps_peer->index);
2298 all_views_updated_cb();
2299}
2300
2301static void
2302pre_profiler (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
2303{
2304 rps_peer->file_name_probs =
2305 store_prefix_file_name (rps_peer->peer_id, "probs");
2306 GNUNET_RPS_view_request (h, 0, view_update_cb, rps_peer);
2307}
2308
2309void write_final_stats (void){
2310 uint64_t sums[STAT_TYPE_MAX] = { 0 };
2311
2312 for (uint32_t i = 0; i < num_peers; i++)
2313 {
2314 to_file ("/tmp/rps/final_stats.csv",
2315 ", %" PRIu32 ", " /* index */
2316 "%s, %" /* id */
2317 PRIu64 ", %" /* rounds */
2318 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* blocking */
2319 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* issued */
2320 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* sent */
2321 PRIu64 ", %" PRIu64 ", %" PRIu64 /* recv */,
2322 i,
2323 GNUNET_i2s (rps_peers[i].peer_id),
2324 rps_peers[i].stats[STAT_TYPE_ROUNDS],
2325 rps_peers[i].stats[STAT_TYPE_BLOCKS],
2326 rps_peers[i].stats[STAT_TYPE_BLOCKS_MANY_PUSH],
2327 rps_peers[i].stats[STAT_TYPE_BLOCKS_NO_PUSH],
2328 rps_peers[i].stats[STAT_TYPE_BLOCKS_NO_PULL],
2329 rps_peers[i].stats[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL],
2330 rps_peers[i].stats[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL],
2331 rps_peers[i].stats[STAT_TYPE_ISSUED_PUSH_SEND],
2332 rps_peers[i].stats[STAT_TYPE_ISSUED_PULL_REQ],
2333 rps_peers[i].stats[STAT_TYPE_ISSUED_PULL_REP],
2334 rps_peers[i].stats[STAT_TYPE_SENT_PUSH_SEND],
2335 rps_peers[i].stats[STAT_TYPE_SENT_PULL_REQ],
2336 rps_peers[i].stats[STAT_TYPE_SENT_PULL_REP],
2337 rps_peers[i].stats[STAT_TYPE_RECV_PUSH_SEND],
2338 rps_peers[i].stats[STAT_TYPE_RECV_PULL_REQ],
2339 rps_peers[i].stats[STAT_TYPE_RECV_PULL_REP]);
2340 for (uint32_t stat_type = STAT_TYPE_ROUNDS;
2341 stat_type < STAT_TYPE_MAX;
2342 stat_type++)
2343 {
2344 sums[stat_type] += rps_peers[i].stats[stat_type];
2345 }
2346 }
2347 to_file ("/tmp/rps/final_stats.dat",
2348 "SUM %"
2349 PRIu64 " %" /* rounds */
2350 PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" /* blocking */
2351 PRIu64 " %" PRIu64 " %" PRIu64 " %" /* issued */
2352 PRIu64 " %" PRIu64 " %" PRIu64 " %" /* sent */
2353 PRIu64 " %" PRIu64 " %" PRIu64 /* recv */,
2354 sums[STAT_TYPE_ROUNDS],
2355 sums[STAT_TYPE_BLOCKS],
2356 sums[STAT_TYPE_BLOCKS_MANY_PUSH],
2357 sums[STAT_TYPE_BLOCKS_NO_PUSH],
2358 sums[STAT_TYPE_BLOCKS_NO_PULL],
2359 sums[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL],
2360 sums[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL],
2361 sums[STAT_TYPE_ISSUED_PUSH_SEND],
2362 sums[STAT_TYPE_ISSUED_PULL_REQ],
2363 sums[STAT_TYPE_ISSUED_PULL_REP],
2364 sums[STAT_TYPE_SENT_PUSH_SEND],
2365 sums[STAT_TYPE_SENT_PULL_REQ],
2366 sums[STAT_TYPE_SENT_PULL_REP],
2367 sums[STAT_TYPE_RECV_PUSH_SEND],
2368 sums[STAT_TYPE_RECV_PULL_REQ],
2369 sums[STAT_TYPE_RECV_PULL_REP]);
2370}
2371
2372/**
2373 * Continuation called by #GNUNET_STATISTICS_get() functions.
2374 *
2375 * Remembers that this specific statistics value was received for this peer.
2376 * Checks whether all peers received their statistics yet.
2377 * Issues the shutdown.
2378 *
2379 * @param cls closure
2380 * @param success #GNUNET_OK if statistics were
2381 * successfully obtained, #GNUNET_SYSERR if not.
2382 */
2383void
2384post_test_shutdown_ready_cb (void *cls,
2385 int success)
2386{
2387 struct STATcls *stat_cls = (struct STATcls *) cls;
2388 struct RPSPeer *rps_peer = stat_cls->rps_peer;
2389
2390 rps_peer->h_stat_get[stat_cls->stat_type] = NULL;
2391 if (GNUNET_OK == success)
2392 {
2393 /* set flag that we we got the value */
2394 rps_peer->stat_collected_flags |= BIT(stat_cls->stat_type);
2395 } else {
2396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2397 "Peer %u did not receive statistics value\n",
2398 rps_peer->index);
2399 GNUNET_free (stat_cls);
2400 GNUNET_break (0);
2401 return;
2402 }
2403
2404 if (NULL != rps_peer->stat_op &&
2405 GNUNET_YES == check_statistics_collect_completed_single_peer (rps_peer))
2406 {
2407 GNUNET_TESTBED_operation_done (rps_peer->stat_op);
2408 }
2409
2410 //write_final_stats ();
2411 if (GNUNET_YES == check_statistics_collect_completed())
2412 {
2413 write_final_stats ();
2414 GNUNET_free (stat_cls);
2415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2416 "Shutting down\n");
2417 GNUNET_SCHEDULER_shutdown ();
2418 } else {
2419 GNUNET_free (stat_cls);
2420 }
2421}
2422
2423/**
2424 * Callback function to process statistic values.
2425 *
2426 * @param cls closure
2427 * @param subsystem name of subsystem that created the statistic
2428 * @param name the name of the datum
2429 * @param value the current value
2430 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
2431 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
2432 */
2433int
2434stat_iterator (void *cls,
2435 const char *subsystem,
2436 const char *name,
2437 uint64_t value,
2438 int is_persistent)
2439{
2440 const struct STATcls *stat_cls = (const struct STATcls *) cls;
2441 struct RPSPeer *rps_peer = (struct RPSPeer *) stat_cls->rps_peer;
2442
2443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got stat value: %s - %" PRIu64 "\n",
2444 //stat_type_2_str (stat_cls->stat_type),
2445 name,
2446 value);
2447 to_file (rps_peer->file_name_stats,
2448 "%s: %" PRIu64 "\n",
2449 name,
2450 value);
2451 switch (stat_str_2_type (name))
2452 {
2453 case STAT_TYPE_ROUNDS:
2454 rps_peer->stats[STAT_TYPE_ROUNDS] = value;
2455 break;
2456 case STAT_TYPE_BLOCKS:
2457 rps_peer->stats[STAT_TYPE_BLOCKS] = value;
2458 break;
2459 case STAT_TYPE_BLOCKS_MANY_PUSH:
2460 rps_peer->stats[STAT_TYPE_BLOCKS_MANY_PUSH] = value;
2461 break;
2462 case STAT_TYPE_BLOCKS_NO_PUSH:
2463 rps_peer->stats[STAT_TYPE_BLOCKS_NO_PUSH] = value;
2464 break;
2465 case STAT_TYPE_BLOCKS_NO_PULL:
2466 rps_peer->stats[STAT_TYPE_BLOCKS_NO_PULL] = value;
2467 break;
2468 case STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL:
2469 rps_peer->stats[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL] = value;
2470 break;
2471 case STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL:
2472 rps_peer->stats[STAT_TYPE_BLOCKS] = value;
2473 break;
2474 case STAT_TYPE_ISSUED_PUSH_SEND:
2475 rps_peer->stats[STAT_TYPE_ISSUED_PUSH_SEND] = value;
2476 break;
2477 case STAT_TYPE_ISSUED_PULL_REQ:
2478 rps_peer->stats[STAT_TYPE_ISSUED_PULL_REQ] = value;
2479 break;
2480 case STAT_TYPE_ISSUED_PULL_REP:
2481 rps_peer->stats[STAT_TYPE_ISSUED_PULL_REP] = value;
2482 break;
2483 case STAT_TYPE_SENT_PUSH_SEND:
2484 rps_peer->stats[STAT_TYPE_SENT_PUSH_SEND] = value;
2485 break;
2486 case STAT_TYPE_SENT_PULL_REQ:
2487 rps_peer->stats[STAT_TYPE_SENT_PULL_REQ] = value;
2488 break;
2489 case STAT_TYPE_SENT_PULL_REP:
2490 rps_peer->stats[STAT_TYPE_SENT_PULL_REP] = value;
2491 break;
2492 case STAT_TYPE_RECV_PUSH_SEND:
2493 rps_peer->stats[STAT_TYPE_RECV_PUSH_SEND] = value;
2494 break;
2495 case STAT_TYPE_RECV_PULL_REQ:
2496 rps_peer->stats[STAT_TYPE_RECV_PULL_REQ] = value;
2497 break;
2498 case STAT_TYPE_RECV_PULL_REP:
2499 rps_peer->stats[STAT_TYPE_RECV_PULL_REP] = value;
2500 break;
2501 case STAT_TYPE_MAX:
2502 default:
2503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2504 "Unknown statistics string: %s\n",
2505 name);
2506 break;
2507 }
2508 return GNUNET_OK;
2509}
2510
2511void post_profiler (struct RPSPeer *rps_peer)
2512{
2513 if (COLLECT_STATISTICS != cur_test_run.have_collect_statistics)
2514 {
2515 return;
2516 }
2517
2518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2519 "Going to request statistic values with mask 0x%" PRIx32 "\n",
2520 cur_test_run.stat_collect_flags);
2521
2522 struct STATcls *stat_cls;
2523 uint32_t stat_type;
2524 for (stat_type = STAT_TYPE_ROUNDS;
2525 stat_type < STAT_TYPE_MAX;
2526 stat_type++)
2527 {
2528 if (BIT(stat_type) & cur_test_run.stat_collect_flags)
2529 {
2530 stat_cls = GNUNET_malloc (sizeof (struct STATcls));
2531 stat_cls->rps_peer = rps_peer;
2532 stat_cls->stat_type = stat_type;
2533 rps_peer->file_name_stats =
2534 store_prefix_file_name (rps_peer->peer_id, "stats");
2535 rps_peer->h_stat_get[stat_type] = GNUNET_STATISTICS_get (
2536 rps_peer->stats_h,
2537 "rps",
2538 stat_type_2_str (stat_type),
2539 post_test_shutdown_ready_cb,
2540 stat_iterator,
2541 (struct STATcls *) stat_cls);
2542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2543 "Requested statistics for %s (peer %" PRIu32 ")\n",
2544 stat_type_2_str (stat_type),
2545 rps_peer->index);
2546 }
2547 }
2548}
2549
2550
2551/***********************************************************************
2552 * /Definition of tests
2553***********************************************************************/
2554
2555
2556/**
2557 * Actual "main" function for the testcase.
2558 *
2559 * @param cls closure
2560 * @param h the run handle
2561 * @param n_peers number of peers in 'peers'
2562 * @param peers handle to peers run in the testbed
2563 * @param links_succeeded the number of overlay link connection attempts that
2564 * succeeded
2565 * @param links_failed the number of overlay link connection attempts that
2566 * failed
2567 */
2568static void
2569test_run (void *cls,
2570 struct GNUNET_TESTBED_RunHandle *h,
2571 unsigned int n_peers,
2572 struct GNUNET_TESTBED_Peer **peers,
2573 unsigned int links_succeeded,
2574 unsigned int links_failed)
2575{
2576 unsigned int i;
2577 struct OpListEntry *entry;
2578
2579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
2580
2581 /* Check whether we timed out */
2582 if (n_peers != num_peers ||
2583 NULL == peers ||
2584 0 == links_succeeded)
2585 {
2586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Going down due to args (eg. timeout)\n");
2587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tn_peers: %u\n", n_peers);
2588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tnum_peers: %" PRIu32 "\n", num_peers);
2589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tpeers: %p\n", peers);
2590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tlinks_succeeded: %u\n", links_succeeded);
2591 GNUNET_SCHEDULER_shutdown ();
2592 return;
2593 }
2594
2595
2596 /* Initialize peers */
2597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "going to initialise peers\n");
2598 testbed_peers = peers;
2599 num_peers_online = 0;
2600 for (i = 0; i < num_peers; i++)
2601 {
2602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "initialising %u\n", i);
2603 entry = make_oplist_entry ();
2604 entry->index = i;
2605 rps_peers[i].index = i;
2606 if (NULL != cur_test_run.init_peer)
2607 cur_test_run.init_peer (&rps_peers[i]);
2608 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
2609 {
2610 rps_peers->cur_view_count = 0;
2611 rps_peers->cur_view = NULL;
2612 }
2613 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
2614 GNUNET_TESTBED_PIT_IDENTITY,
2615 &info_cb,
2616 entry);
2617 }
2618
2619 /* Bring peers up */
2620 GNUNET_assert (num_peers == n_peers);
2621 for (i = 0; i < n_peers; i++)
2622 {
2623 rps_peers[i].index = i;
2624 rps_peers[i].op =
2625 GNUNET_TESTBED_service_connect (&rps_peers[i],
2626 peers[i],
2627 "rps",
2628 &rps_connect_complete_cb,
2629 &rps_peers[i],
2630 &rps_connect_adapter,
2631 &rps_disconnect_adapter,
2632 &rps_peers[i]);
2633 /* Connect all peers to statistics service */
2634 if (COLLECT_STATISTICS == cur_test_run.have_collect_statistics)
2635 {
2636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2637 "Connecting to statistics service\n");
2638 rps_peers[i].stat_op =
2639 GNUNET_TESTBED_service_connect (NULL,
2640 peers[i],
2641 "statistics",
2642 stat_complete_cb,
2643 &rps_peers[i],
2644 &stat_connect_adapter,
2645 &stat_disconnect_adapter,
2646 &rps_peers[i]);
2647 }
2648 }
2649
2650 if (NULL != churn_task)
2651 GNUNET_SCHEDULER_cancel (churn_task);
2652 post_test_task = GNUNET_SCHEDULER_add_delayed (duration, &post_test_op, NULL);
2653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout for shutdown is %lu\n", timeout.rel_value_us/1000000);
2654 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout,
2655 &trigger_shutdown,
2656 NULL);
2657 GNUNET_SCHEDULER_add_shutdown (shutdown_op, NULL);
2658}
2659
2660
2661/**
2662 * Entry point for the testcase, sets up the testbed.
2663 *
2664 * @param argc unused
2665 * @param argv unused
2666 */
2667static void
2668run (void *cls,
2669 char *const *args,
2670 const char *cfgfile,
2671 const struct GNUNET_CONFIGURATION_Handle *cfg)
2672{
2673 //int ret_value;
2674
2675 /* Defaults for tests */
2676 churn_task = NULL;
2677
2678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
2679 cur_test_run.name = "test-rps-profiler";
2680 if (0 == num_peers) num_peers = 10;
2681 mal_type = 3;
2682 cur_test_run.init_peer = profiler_init_peer;
2683 //cur_test_run.pre_test = mal_pre;
2684 cur_test_run.pre_test = pre_profiler;
2685 cur_test_run.main_test = profiler_cb;
2686 cur_test_run.reply_handle = profiler_reply_handle;
2687 cur_test_run.eval_cb = profiler_eval;
2688 cur_test_run.post_test = post_profiler;
2689 cur_test_run.request_interval = 2;
2690 if (0 == cur_test_run.num_requests) cur_test_run.num_requests = 5;
2691 //cur_test_run.have_churn = HAVE_CHURN;
2692 cur_test_run.have_churn = HAVE_NO_CHURN;
2693 cur_test_run.have_quick_quit = HAVE_QUICK_QUIT;
2694 cur_test_run.have_collect_statistics = COLLECT_STATISTICS;
2695 cur_test_run.stat_collect_flags = BIT(STAT_TYPE_ROUNDS) |
2696 BIT(STAT_TYPE_BLOCKS) |
2697 BIT(STAT_TYPE_BLOCKS_MANY_PUSH) |
2698 BIT(STAT_TYPE_BLOCKS_NO_PUSH) |
2699 BIT(STAT_TYPE_BLOCKS_NO_PULL) |
2700 BIT(STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL) |
2701 BIT(STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL) |
2702 BIT(STAT_TYPE_ISSUED_PUSH_SEND) |
2703 BIT(STAT_TYPE_ISSUED_PULL_REQ) |
2704 BIT(STAT_TYPE_ISSUED_PULL_REP) |
2705 BIT(STAT_TYPE_SENT_PUSH_SEND) |
2706 BIT(STAT_TYPE_SENT_PULL_REQ) |
2707 BIT(STAT_TYPE_SENT_PULL_REP) |
2708 BIT(STAT_TYPE_RECV_PUSH_SEND) |
2709 BIT(STAT_TYPE_RECV_PULL_REQ) |
2710 BIT(STAT_TYPE_RECV_PULL_REP);
2711 cur_test_run.have_collect_view = COLLECT_VIEW;
2712
2713 /* 'Clean' directory */
2714 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
2715 GNUNET_DISK_directory_create ("/tmp/rps/");
2716 if (0 == duration.rel_value_us)
2717 {
2718 if (0 == timeout.rel_value_us)
2719 {
2720 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
2721 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
2722 (90 * 1.2) +
2723 (0.01 * num_peers));
2724 }
2725 else
2726 {
2727 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
2728 (timeout.rel_value_us/1000000)
2729 * 0.75);
2730 }
2731 }
2732 else
2733 {
2734 if (0 == timeout.rel_value_us)
2735 {
2736 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
2737 ((duration.rel_value_us/1000000)
2738 * 1.2) + (0.01 * num_peers));
2739 }
2740 }
2741 GNUNET_assert (duration.rel_value_us < timeout.rel_value_us);
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2743 "duration is %lus\n",
2744 duration.rel_value_us/1000000);
2745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2746 "timeout is %lus\n",
2747 timeout.rel_value_us/1000000);
2748
2749 /* Compute number of bits for representing largest peer id */
2750 for (bits_needed = 1; (1 << bits_needed) < num_peers; bits_needed++)
2751 ;
2752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2753 "Need %u bits to represent %" PRIu32 " peers\n",
2754 bits_needed,
2755 num_peers);
2756
2757 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
2758 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
2759 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
2760 if ( (2 == mal_type) ||
2761 (3 == mal_type))
2762 target_peer = &rps_peer_ids[num_peers - 2];
2763 if (profiler_eval == cur_test_run.eval_cb)
2764 eval_peer = &rps_peers[num_peers - 1]; /* FIXME: eval_peer could be a
2765 malicious peer if not careful
2766 with the malicious portion */
2767
2768 ok = 1;
2769 GNUNET_TESTBED_run (NULL,
2770 cfg,
2771 num_peers,
2772 0, /* event mask */
2773 NULL,
2774 NULL,
2775 &test_run,
2776 NULL);
2777}
2778
2779/**
2780 * Entry point for the testcase, sets up the testbed.
2781 *
2782 * @param argc unused
2783 * @param argv unused
2784 * @return 0 on success
2785 */
2786int
2787main (int argc, char *argv[])
2788{
2789 int ret_value;
2790 struct GNUNET_GETOPT_CommandLineOption options[] = {
2791 GNUNET_GETOPT_option_uint ('n',
2792 "num-peers",
2793 "COUNT",
2794 gettext_noop ("number of peers to start"),
2795 &num_peers),
2796
2797 GNUNET_GETOPT_option_relative_time ('d',
2798 "duration",
2799 "DURATION",
2800 gettext_noop ("duration of the profiling"),
2801 &duration),
2802
2803 GNUNET_GETOPT_option_relative_time ('t',
2804 "timeout",
2805 "TIMEOUT",
2806 gettext_noop ("timeout for the profiling"),
2807 &timeout),
2808
2809 GNUNET_GETOPT_option_uint ('r',
2810 "num-requests",
2811 "COUNT",
2812 gettext_noop ("number of PeerIDs to request"),
2813 &cur_test_run.num_requests),
2814
2815 GNUNET_GETOPT_OPTION_END
2816 };
2817
2818 //if (GNUNET_OK !=
2819 // GNUNET_STRINGS_get_utf8_args (argc, argv,
2820 // &argc, &argv))
2821 // return 2;
2822 ret_value = 0;
2823 if (GNUNET_OK !=
2824 GNUNET_PROGRAM_run (argc,
2825 argv,
2826 "gnunet-rps-profiler",
2827 gettext_noop ("Measure quality and performance of the RPS service."),
2828 options,
2829 &run,
2830 NULL))
2831 {
2832 ret_value = 1;
2833 }
2834 if (GNUNET_OK != ret_value)
2835 {
2836 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2837 "Test did not run successfully!\n");
2838 }
2839
2840 ret_value = cur_test_run.eval_cb();
2841 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
2842 {
2843 GNUNET_array_grow (rps_peers->cur_view,
2844 rps_peers->cur_view_count,
2845 0);
2846 }
2847 GNUNET_free (rps_peers);
2848 GNUNET_free (rps_peer_ids);
2849 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2850 return ret_value;
2851}
2852
2853/* end of test_rps.c */