tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / rps / test_rps.c
index e28210ed49a1480d8175c65c8a9ed76478ce8684..7fc91743b35d762395c855cd54e37f8e34637a25 100644 (file)
@@ -2,20 +2,20 @@
      This file is part of GNUnet.
      Copyright (C) 2009, 2012 GNUnet e.V.
 
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-     Boston, MA 02110-1301, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 /**
  * @file rps/test_rps.c
  */
 static uint32_t num_peers;
 
+/**
+ * How long do we run the test?
+ * In seconds.
+ */
+static uint32_t timeout_s;
+
 /**
  * How long do we run the test?
  */
@@ -185,6 +191,11 @@ struct RPSPeer
    */
   struct GNUNET_RPS_Handle *rps_handle;
 
+  /**
+   * Handle to stream requests
+   */
+  struct GNUNET_RPS_StreamRequestHandle *rps_srh;
+
   /**
    * ID of the peer.
    */
@@ -257,7 +268,12 @@ struct RPSPeer
   /**
    * @brief File name of the file the stats are finally written to
    */
-  char *file_name_stats;
+  const char *file_name_stats;
+
+  /**
+   * @brief File name of the file the stats are finally written to
+   */
+  const char *file_name_probs;
 
   /**
    * @brief The current view
@@ -353,11 +369,21 @@ static struct RPSPeer *eval_peer;
  */
 static unsigned int num_peers_online;
 
+/**
+ * @brief The added sizes of the peer's views
+ */
+static unsigned int view_sizes;
+
 /**
  * Return value from 'main'.
  */
 static int ok;
 
+/**
+ * Identifier for the churn task that runs periodically
+ */
+static struct GNUNET_SCHEDULER_Task *post_test_task;
+
 /**
  * Identifier for the churn task that runs periodically
  */
@@ -545,6 +571,11 @@ struct SingleTestRun
   uint32_t stat_collect_flags;
 } cur_test_run;
 
+/**
+ * Did we finish the test?
+ */
+static int post_test;
+
 /**
  * Are we shutting down?
  */
@@ -648,6 +679,13 @@ ids_to_file (char *file_name,
   }
 } */
 
+/**
+ * Task run on timeout to collect statistics and potentially shut down.
+ */
+static void
+post_test_op (void *cls);
+
+
 /**
  * Test the success of a single test
  */
@@ -708,6 +746,8 @@ static int check_statistics_collect_completed_single_peer (
   }
   return GNUNET_YES;
 }
+
+
 /**
  * @brief Checks if all peers already received their statistics value from the
  * statistics service.
@@ -734,6 +774,7 @@ static int check_statistics_collect_completed ()
   return GNUNET_YES;
 }
 
+
 /**
  * Task run on timeout to shut everything down.
  */
@@ -741,10 +782,16 @@ static void
 shutdown_op (void *cls)
 {
   unsigned int i;
+  (void) cls;
 
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
               "Shutdown task scheduled, going down.\n");
   in_shutdown = GNUNET_YES;
+  if (NULL != post_test_task)
+  {
+    GNUNET_SCHEDULER_cancel (post_test_task);
+    post_test_op (NULL);
+  }
   if (NULL != churn_task)
   {
     GNUNET_SCHEDULER_cancel (churn_task);
@@ -752,13 +799,48 @@ shutdown_op (void *cls)
   }
   for (i = 0; i < num_peers; i++)
   {
+    if (NULL != rps_peers[i].rps_handle)
+    {
+      GNUNET_RPS_disconnect (rps_peers[i].rps_handle);
+    }
     if (NULL != rps_peers[i].op)
+    {
       GNUNET_TESTBED_operation_done (rps_peers[i].op);
+    }
+  }
+}
+
+
+/**
+ * Task run on timeout to collect statistics and potentially shut down.
+ */
+static void
+post_test_op (void *cls)
+{
+  unsigned int i;
+  (void) cls;
+
+  post_test_task = NULL;
+  post_test = GNUNET_YES;
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "Post test task scheduled, going down.\n");
+  if (NULL != churn_task)
+  {
+    GNUNET_SCHEDULER_cancel (churn_task);
+    churn_task = NULL;
+  }
+  for (i = 0; i < num_peers; i++)
+  {
     if (NULL != cur_test_run.post_test)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing post_test for peer %u\n", i);
       cur_test_run.post_test (&rps_peers[i]);
     }
+    if (NULL != rps_peers[i].op)
+    {
+      GNUNET_TESTBED_operation_done (rps_peers[i].op);
+      rps_peers[i].op = NULL;
+    }
   }
   /* If we do not collect statistics, shut down directly */
   if (NO_COLLECT_STATISTICS == cur_test_run.have_collect_statistics ||
@@ -779,6 +861,13 @@ seed_peers (void *cls)
   unsigned int amount;
   unsigned int i;
 
+  if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
+  {
+    return;
+  }
+
+  GNUNET_assert (NULL != peer->rps_handle);
+
   // TODO if malicious don't seed mal peers
   amount = round (.5 * num_peers);
 
@@ -836,8 +925,9 @@ info_cb (void *cb_cls,
          const char *emsg)
 {
   struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
+  (void) op;
 
-  if (GNUNET_YES == in_shutdown)
+  if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
   {
     return;
   }
@@ -891,7 +981,9 @@ rps_connect_complete_cb (void *cls,
   struct RPSPeer *rps_peer = cls;
   struct GNUNET_RPS_Handle *rps = ca_result;
 
-  if (GNUNET_YES == in_shutdown)
+  GNUNET_assert (NULL != ca_result);
+
+  if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
   {
     return;
   }
@@ -934,9 +1026,11 @@ rps_connect_adapter (void *cls,
   struct GNUNET_RPS_Handle *h;
 
   h = GNUNET_RPS_connect (cfg);
+  GNUNET_assert (NULL != h);
 
   if (NULL != cur_test_run.pre_test)
     cur_test_run.pre_test (cls, h);
+  GNUNET_assert (NULL != h);
 
   return h;
 }
@@ -997,6 +1091,9 @@ stat_complete_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
 {
   //struct GNUNET_STATISTICS_Handle *sh = ca_result;
   //struct RPSPeer *peer = (struct RPSPeer *) cls;
+  (void) cls;
+  (void) op;
+  (void) ca_result;
 
   if (NULL != emsg)
   {
@@ -1025,6 +1122,12 @@ rps_disconnect_adapter (void *cls,
 {
   struct RPSPeer *peer = cls;
   struct GNUNET_RPS_Handle *h = op_result;
+
+  if (NULL != peer->rps_srh)
+  {
+    GNUNET_RPS_stream_cancel (peer->rps_srh);
+    peer->rps_srh = NULL;
+  }
   GNUNET_assert (NULL != peer);
   GNUNET_RPS_disconnect (h);
   peer->rps_handle = NULL;
@@ -1095,10 +1198,10 @@ default_reply_handle (void *cls,
   if (0 == evaluate () && HAVE_QUICK_QUIT == cur_test_run.have_quick_quit)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test succeeded before timeout\n");
-    GNUNET_assert (NULL != shutdown_task);
-    GNUNET_SCHEDULER_cancel (shutdown_task);
-    shutdown_task = GNUNET_SCHEDULER_add_now (&shutdown_op, NULL);
-    GNUNET_assert (NULL!= shutdown_task);
+    GNUNET_assert (NULL != post_test_task);
+    GNUNET_SCHEDULER_cancel (post_test_task);
+    post_test_task = GNUNET_SCHEDULER_add_now (&post_test_op, NULL);
+    GNUNET_assert (NULL!= post_test_task);
   }
 }
 
@@ -1112,7 +1215,7 @@ request_peers (void *cls)
   struct RPSPeer *rps_peer;
   struct PendingReply *pending_rep;
 
-  if (GNUNET_YES == in_shutdown)
+  if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
     return;
   rps_peer = pending_req->rps_peer;
   GNUNET_assert (1 <= rps_peer->num_pending_reqs);
@@ -1175,7 +1278,7 @@ cancel_request_cb (void *cls)
   struct RPSPeer *rps_peer = cls;
   struct PendingReply *pending_rep;
 
-  if (GNUNET_YES == in_shutdown)
+  if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
     return;
   pending_rep = rps_peer->pending_rep_head;
   GNUNET_assert (1 <= rps_peer->num_pending_reps);
@@ -1254,7 +1357,7 @@ static void mal_init_peer (struct RPSPeer *rps_peer)
 static void
 mal_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
 {
-  #ifdef ENABLE_MALICIOUS
+  #if ENABLE_MALICIOUS
   uint32_t num_mal_peers;
 
   GNUNET_assert ( (1 >= portion) &&
@@ -1280,12 +1383,12 @@ mal_cb (struct RPSPeer *rps_peer)
 {
   uint32_t num_mal_peers;
 
-  if (GNUNET_YES == in_shutdown)
+  if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
   {
     return;
   }
 
-  #ifdef ENABLE_MALICIOUS
+  #if ENABLE_MALICIOUS
   GNUNET_assert ( (1 >= portion) &&
                   (0 <  portion) );
   num_mal_peers = round (portion * num_peers);
@@ -1307,7 +1410,7 @@ mal_cb (struct RPSPeer *rps_peer)
 static void
 single_req_cb (struct RPSPeer *rps_peer)
 {
-  if (GNUNET_YES == in_shutdown)
+  if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
   {
     return;
   }
@@ -1321,7 +1424,7 @@ single_req_cb (struct RPSPeer *rps_peer)
 static void
 delay_req_cb (struct RPSPeer *rps_peer)
 {
-  if (GNUNET_YES == in_shutdown)
+  if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
   {
     return;
   }
@@ -1335,7 +1438,7 @@ delay_req_cb (struct RPSPeer *rps_peer)
 static void
 seed_cb (struct RPSPeer *rps_peer)
 {
-  if (GNUNET_YES == in_shutdown)
+  if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
   {
     return;
   }
@@ -1351,7 +1454,7 @@ seed_cb (struct RPSPeer *rps_peer)
 static void
 seed_big_cb (struct RPSPeer *rps_peer)
 {
-  if (GNUNET_YES == in_shutdown)
+  if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
   {
     return;
   }
@@ -1368,6 +1471,7 @@ seed_big_cb (struct RPSPeer *rps_peer)
 static void
 single_peer_seed_cb (struct RPSPeer *rps_peer)
 {
+  (void) rps_peer;
   // TODO
 }
 
@@ -1377,7 +1481,7 @@ single_peer_seed_cb (struct RPSPeer *rps_peer)
 static void
 seed_req_cb (struct RPSPeer *rps_peer)
 {
-  if (GNUNET_YES == in_shutdown)
+  if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
   {
     return;
   }
@@ -1396,7 +1500,7 @@ seed_req_cb (struct RPSPeer *rps_peer)
 static void
 req_cancel_cb (struct RPSPeer *rps_peer)
 {
-  if (GNUNET_YES == in_shutdown)
+  if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
   {
     return;
   }
@@ -1429,7 +1533,7 @@ churn (void *cls);
 static void
 churn_test_cb (struct RPSPeer *rps_peer)
 {
-  if (GNUNET_YES == in_shutdown)
+  if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
   {
     return;
   }
@@ -1451,6 +1555,63 @@ churn_test_cb (struct RPSPeer *rps_peer)
   schedule_missing_requests (rps_peer);
 }
 
+/***********************************
+ * SUB
+***********************************/
+
+static void
+got_stream_peer_cb (void *cls,
+                    uint64_t num_peers,
+                    const struct GNUNET_PeerIdentity *peers)
+{
+  const struct RPSPeer *rps_peer = cls;
+
+  for (uint64_t i = 0; i < num_peers; i++)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Peer %" PRIu32 " received [%s] from stream.\n",
+                rps_peer->index,
+                GNUNET_i2s (&peers[i]));
+    if (0 != rps_peer->index &&
+        0 == memcmp (&peers[i],
+                     &rps_peers[0].peer_id,
+                     sizeof (struct GNUNET_PeerIdentity)))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Received a peer id outside sub\n");
+      ok = 1;
+    }
+    else if (0 == rps_peer->index &&
+             0 != memcmp (&peers[i],
+                          &rps_peers[0].peer_id,
+                          sizeof (struct GNUNET_PeerIdentity)))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Received a peer id outside sub (lonely)\n");
+      ok = 1;
+    }
+  }
+}
+
+
+static void
+sub_post (struct RPSPeer *rps_peer)
+{
+  if (0 != rps_peer->index) GNUNET_RPS_sub_stop (rps_peer->rps_handle, "test");
+  else GNUNET_RPS_sub_stop (rps_peer->rps_handle, "lonely");
+}
+
+
+static void
+sub_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
+{
+  (void) rps_peer;
+
+  if (0 != rps_peer->index) GNUNET_RPS_sub_start (h, "test");
+  else GNUNET_RPS_sub_start (h, "lonely"); /* have a group of one */
+  rps_peer->rps_srh = GNUNET_RPS_stream_request (h,
+                                                 &got_stream_peer_cb,
+                                                 rps_peer);
+}
+
 /***********************************
  * PROFILER
 ***********************************/
@@ -1467,10 +1628,11 @@ churn_cb (void *cls,
           struct GNUNET_TESTBED_Operation *op,
           const char *emsg)
 {
+  (void) op;
   // FIXME
   struct OpListEntry *entry = cls;
 
-  if (GNUNET_YES == in_shutdown)
+  if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
   {
     return;
   }
@@ -1597,6 +1759,7 @@ manage_service_wrapper (unsigned int i, unsigned int j,
 static void
 churn (void *cls)
 {
+  (void) cls;
   unsigned int i;
   unsigned int j;
   double portion_online;
@@ -1605,7 +1768,7 @@ churn (void *cls)
   double portion_go_online;
   double portion_go_offline;
 
-  if (GNUNET_YES == in_shutdown)
+  if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
   {
     return;
   }
@@ -1721,7 +1884,7 @@ profiler_reply_handle (void *cls,
 static void
 profiler_cb (struct RPSPeer *rps_peer)
 {
-  if (GNUNET_YES == in_shutdown)
+  if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
   {
     return;
   }
@@ -1759,6 +1922,8 @@ profiler_cb (struct RPSPeer *rps_peer)
 int
 file_name_cb (void *cls, const char *filename)
 {
+  (void) cls;
+
   if (NULL != strstr (filename, "sampler_el"))
   {
     struct RPS_SamplerElement *s_elem;
@@ -1799,50 +1964,215 @@ profiler_eval (void)
   return evaluate ();
 }
 
+
 /**
- * @brief Try to ensure that `/tmp/rps` exists.
+ * @brief is b in view of a?
+ *
+ * @param a
+ * @param b
  *
- * @return #GNUNET_YES on success
- *         #GNUNET_SYSERR on failure
+ * @return
  */
-static int ensure_folder_exist (void)
+static int is_in_view (uint32_t a, uint32_t b)
 {
-  if (GNUNET_NO == GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
+  uint32_t i;
+  for (i = 0; i < rps_peers[a].cur_view_count; i++)
   {
-    GNUNET_DISK_directory_create ("/tmp/rps");
+    if (0 == memcmp (rps_peers[b].peer_id,
+          &rps_peers[a].cur_view[i],
+          sizeof (struct GNUNET_PeerIdentity)))
+    {
+      return GNUNET_YES;
+    }
   }
-  if (GNUNET_YES != GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
+  return GNUNET_NO;
+}
+
+static uint32_t get_idx_of_pid (const struct GNUNET_PeerIdentity *pid)
+{
+  uint32_t i;
+
+  for (i = 0; i < num_peers; i++)
   {
-    return GNUNET_SYSERR;
+    if (0 == memcmp (pid,
+          rps_peers[i].peer_id,
+          sizeof (struct GNUNET_PeerIdentity)))
+    {
+      return i;
+    }
   }
-  return GNUNET_YES;
+  //return 0; /* Should not happen - make compiler happy */
+  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+             "No known _PeerIdentity %s!\n",
+             GNUNET_i2s_full (pid));
+  GNUNET_assert (0);
 }
 
-static void
-store_stats_file_name (struct RPSPeer *rps_peer)
+/**
+ * @brief Counts number of peers in view of a that have b in their view
+ *
+ * @param a
+ * @param uint32_tb
+ *
+ * @return
+ */
+static uint32_t count_containing_views (uint32_t a, uint32_t b)
 {
-  unsigned int len_file_name;
-  unsigned int out_size;
-  char *file_name;
+  uint32_t i;
+  uint32_t peer_idx;
+  uint32_t count = 0;
 
-  if (GNUNET_SYSERR == ensure_folder_exist()) return;
-  len_file_name = (14 + strlen (GNUNET_i2s_full (rps_peer->peer_id)) + 1) * sizeof (char);
-  file_name = GNUNET_malloc (len_file_name);
-  out_size = GNUNET_snprintf (file_name,
-                              len_file_name,
-                              "/tmp/rps/stat-%s",
-                              GNUNET_i2s_full (rps_peer->peer_id));
-  if (len_file_name < out_size ||
-      0 > out_size)
+  for (i = 0; i < rps_peers[a].cur_view_count; i++)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-               "Failed to write string to buffer (size: %i, out_size: %i)\n",
-               len_file_name,
-               out_size);
+    peer_idx = get_idx_of_pid (&rps_peers[a].cur_view[i]);
+    if (GNUNET_YES == is_in_view (peer_idx, b))
+    {
+      count++;
+    }
+  }
+  return count;
+}
+
+/**
+ * @brief Computes the probability for each other peer to be selected by the
+ * sampling process based on the views of all peers
+ *
+ * @param peer_idx index of the peer that is about to sample
+ */
+static void compute_probabilities (uint32_t peer_idx)
+{
+  //double probs[num_peers] = { 0 };
+  double probs[num_peers];
+  size_t probs_as_str_size = (num_peers * 10 + 1) * sizeof (char);
+  char *probs_as_str = GNUNET_malloc (probs_as_str_size);
+  char *probs_as_str_cpy;
+  uint32_t i;
+  double prob_push;
+  double prob_pull;
+  uint32_t view_size;
+  uint32_t cont_views;
+  uint32_t number_of_being_in_pull_events;
+  int tmp;
+  uint32_t count_non_zero_prob = 0;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+      "Computing probabilities for peer %" PRIu32 "\n", peer_idx);
+  /* Firstly without knowledge of old views */
+  for (i = 0; i < num_peers; i++)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+        "\tfor peer %" PRIu32 ":\n", i);
+    view_size = rps_peers[i].cur_view_count;
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+        "\t\tview_size: %" PRIu32 "\n", view_size);
+    /* For peer i the probability of being sampled is
+     * evenly distributed among all possibly observed peers. */
+    /* We could have observed a peer in three cases:
+     *   1. peer sent a push
+     *   2. peer was contained in a pull reply
+     *   3. peer was in history (sampler) - ignored for now */
+    /* 1. Probability of having received a push from peer i */
+    if ((GNUNET_YES == is_in_view (i, peer_idx)) &&
+        (1 <= (0.45 * view_size)))
+    {
+      prob_push = 1.0 * binom (0.45 * view_size, 1)
+        /
+        binom (view_size, 0.45 * view_size);
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "\t\t%" PRIu32 " is in %" PRIu32 "'s view, prob: %f\n",
+                 peer_idx,
+                 i,
+                 prob_push);
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "\t\tposs choices from view: %" PRIu32 ", containing i: %" PRIu32 "\n",
+                 binom (view_size, 0.45 * view_size),
+                 binom (0.45 * view_size, 1));
+    } else {
+      prob_push = 0;
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "\t\t%" PRIu32 " is not in %" PRIu32 "'s view, prob: 0\n",
+                 peer_idx,
+                 i);
+    }
+    /* 2. Probability of peer i being contained in pulls */
+    view_size = rps_peers[peer_idx].cur_view_count;
+    cont_views = count_containing_views (peer_idx, i);
+    number_of_being_in_pull_events =
+      (binom (view_size, 0.45 * view_size) -
+       binom (view_size - cont_views, 0.45 * view_size));
+    if (0 != number_of_being_in_pull_events)
+    {
+      prob_pull = number_of_being_in_pull_events
+        /
+        (1.0 * binom (view_size, 0.45 * view_size));
+    } else
+    {
+      prob_pull = 0;
+    }
+    probs[i] = prob_push + prob_pull - (prob_push * prob_pull);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+               "\t\t%" PRIu32 " has %" PRIu32 " of %" PRIu32
+               " peers in its view who know %" PRIu32 " prob: %f\n",
+               peer_idx,
+               cont_views,
+               view_size,
+               i,
+               prob_pull);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+               "\t\tnumber of possible pull combinations: %" PRIu32 "\n",
+               binom (view_size, 0.45 * view_size));
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+               "\t\tnumber of possible pull combinations without %" PRIu32
+               ": %" PRIu32 "\n",
+               i,
+               binom (view_size - cont_views, 0.45 * view_size));
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+               "\t\tnumber of possible pull combinations with %" PRIu32
+               ": %" PRIu32 "\n",
+               i,
+               number_of_being_in_pull_events);
+
+    if (0 != probs[i]) count_non_zero_prob++;
+  }
+  /* normalize */
+  if (0 != count_non_zero_prob)
+  {
+    for (i = 0; i < num_peers; i++)
+    {
+      probs[i] = probs[i] * (1.0 / count_non_zero_prob);
+    }
+  } else {
+    for (i = 0; i < num_peers; i++)
+    {
+      probs[i] = 0;
+    }
+  }
+  /* str repr */
+  for (i = 0; i < num_peers; i++)
+  {
+    probs_as_str_cpy = GNUNET_strndup (probs_as_str, probs_as_str_size);
+    tmp = GNUNET_snprintf (probs_as_str,
+                           probs_as_str_size,
+                           "%s %7.6f", probs_as_str_cpy, probs[i]);
+    GNUNET_free (probs_as_str_cpy);
+    GNUNET_assert (0 <= tmp);
   }
-  rps_peer->file_name_stats = file_name;
+
+  to_file_w_len (rps_peers[peer_idx].file_name_probs,
+                 probs_as_str_size,
+                 probs_as_str);
+  GNUNET_free (probs_as_str);
 }
 
+/**
+ * @brief This counts the number of peers in which views a given peer occurs.
+ *
+ * It also stores this value in the rps peer.
+ *
+ * @param peer_idx the index of the peer to count the representation
+ *
+ * @return the number of occurrences
+ */
 static uint32_t count_peer_in_views_2 (uint32_t peer_idx)
 {
   uint32_t i, j;
@@ -1857,6 +2187,7 @@ static uint32_t count_peer_in_views_2 (uint32_t peer_idx)
             sizeof (struct GNUNET_PeerIdentity)))
       {
         count++;
+        break;
       }
     }
   }
@@ -1864,6 +2195,18 @@ static uint32_t count_peer_in_views_2 (uint32_t peer_idx)
   return count;
 }
 
+static uint32_t cumulated_view_sizes ()
+{
+  uint32_t i;
+
+  view_sizes = 0;
+  for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
+  {
+    view_sizes += rps_peers[i].cur_view_count;
+  }
+  return view_sizes;
+}
+
 static void count_peer_in_views (uint32_t *count_peers)
 {
   uint32_t i, j;
@@ -1945,38 +2288,61 @@ void all_views_updated_cb()
 }
 
 void view_update_cb (void *cls,
-                     uint64_t num_peers,
+                     uint64_t view_size,
                      const struct GNUNET_PeerIdentity *peers)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "View was updated (%" PRIu64 ")\n", num_peers);
+              "View was updated (%" PRIu64 ")\n", view_size);
   struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
   to_file ("/tmp/rps/view_sizes.txt",
          "%" PRIu64 " %" PRIu32 "",
          rps_peer->index,
-         num_peers);
-  for (int i = 0; i < num_peers; i++)
+         view_size);
+  for (int i = 0; i < view_size; i++)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "\t%s\n", GNUNET_i2s (&peers[i]));
   }
   GNUNET_array_grow (rps_peer->cur_view,
                      rps_peer->cur_view_count,
-                     num_peers);
+                     view_size);
   //*rps_peer->cur_view = *peers;
-  memcpy (rps_peer->cur_view,
-          peers,
-          num_peers * sizeof (struct GNUNET_PeerIdentity));
+  GNUNET_memcpy (rps_peer->cur_view,
+                 peers,
+                 view_size * sizeof (struct GNUNET_PeerIdentity));
   to_file ("/tmp/rps/count_in_views.txt",
          "%" PRIu64 " %" PRIu32 "",
          rps_peer->index,
          count_peer_in_views_2 (rps_peer->index));
+  cumulated_view_sizes();
+  if (0 != view_size)
+  {
+    to_file ("/tmp/rps/repr.txt",
+           "%" PRIu64 /* index */
+           " %" PRIu32 /* occurrence in views */
+           " %" PRIu32 /* view sizes */
+           " %f" /* fraction of repr in views */
+           " %f" /* average view size */
+           " %f" /* prob of occurrence in view slot */
+           " %f" "", /* exp frac of repr in views */
+           rps_peer->index,
+           count_peer_in_views_2 (rps_peer->index),
+           view_sizes,
+           count_peer_in_views_2 (rps_peer->index) / (view_size * 1.0), /* fraction of representation in views */
+           view_sizes / (view_size * 1.0), /* average view size */
+           1.0 /view_size, /* prob of occurrence in view slot */
+           (1.0/view_size) * (view_sizes/view_size) /* expected fraction of repr in views */
+           );
+  }
+  compute_probabilities (rps_peer->index);
   all_views_updated_cb();
 }
 
 static void
 pre_profiler (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
 {
+  rps_peer->file_name_probs =
+    store_prefix_file_name (rps_peer->peer_id, "probs");
   GNUNET_RPS_view_request (h, 0, view_update_cb, rps_peer);
 }
 
@@ -2049,9 +2415,10 @@ post_test_shutdown_ready_cb (void *cls,
     GNUNET_TESTBED_operation_done (rps_peer->stat_op);
   }
 
+  write_final_stats ();
   if (GNUNET_YES == check_statistics_collect_completed())
   {
-    write_final_stats ();
+    //write_final_stats ();
     GNUNET_free (stat_cls);
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
         "Shutting down\n");
@@ -2206,6 +2573,8 @@ stat_iterator (void *cls,
                uint64_t value,
                int is_persistent)
 {
+  (void) subsystem;
+  (void) is_persistent;
   const struct STATcls *stat_cls = (const struct STATcls *) cls;
   struct RPSPeer *rps_peer = (struct RPSPeer *) stat_cls->rps_peer;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got stat value: %s - %" PRIu64 "\n",
@@ -2298,7 +2667,8 @@ void post_profiler (struct RPSPeer *rps_peer)
       stat_cls = GNUNET_malloc (sizeof (struct STATcls));
       stat_cls->rps_peer = rps_peer;
       stat_cls->stat_type = stat_type;
-      store_stats_file_name (rps_peer);
+      rps_peer->file_name_stats =
+        store_prefix_file_name (rps_peer->peer_id, "stats");
       GNUNET_STATISTICS_get (rps_peer->stats_h,
                              "rps",
                              stat_type_2_str (stat_type),
@@ -2339,6 +2709,9 @@ run (void *cls,
      unsigned int links_succeeded,
      unsigned int links_failed)
 {
+  (void) cls;
+  (void) h;
+  (void) links_failed;
   unsigned int i;
   struct OpListEntry *entry;
 
@@ -2411,6 +2784,9 @@ run (void *cls,
 
   if (NULL != churn_task)
     GNUNET_SCHEDULER_cancel (churn_task);
+  post_test_task = GNUNET_SCHEDULER_add_delayed (timeout, &post_test_op, NULL);
+  timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
+      (timeout_s * 1.2) + 0.1 * num_peers);
   shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
 }
 
@@ -2426,6 +2802,7 @@ int
 main (int argc, char *argv[])
 {
   int ret_value;
+  (void) argc;
 
   /* Defaults for tests */
   num_peers = 5;
@@ -2440,13 +2817,14 @@ main (int argc, char *argv[])
   cur_test_run.stat_collect_flags = 0;
   cur_test_run.have_collect_view = NO_COLLECT_VIEW;
   churn_task = NULL;
-  timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
+  timeout_s = 30;
 
   if (strstr (argv[0], "malicious") != NULL)
   {
     cur_test_run.pre_test = mal_pre;
     cur_test_run.main_test = mal_cb;
     cur_test_run.init_peer = mal_init_peer;
+    timeout_s = 40;
 
     if (strstr (argv[0], "_1") != NULL)
     {
@@ -2492,7 +2870,7 @@ main (int argc, char *argv[])
     cur_test_run.main_test = seed_big_cb;
     cur_test_run.eval_cb = no_eval;
     cur_test_run.have_churn = HAVE_NO_CHURN;
-    timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
+    timeout_s = 10;
   }
 
   else if (strstr (argv[0], "_single_peer_seed") != NULL)
@@ -2528,7 +2906,7 @@ main (int argc, char *argv[])
     cur_test_run.main_test = req_cancel_cb;
     cur_test_run.eval_cb = no_eval;
     cur_test_run.have_churn = HAVE_NO_CHURN;
-    timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
+    timeout_s = 10;
   }
 
   else if (strstr (argv[0], "_churn") != NULL)
@@ -2540,16 +2918,31 @@ main (int argc, char *argv[])
     cur_test_run.main_test = churn_test_cb;
     cur_test_run.reply_handle = default_reply_handle;
     cur_test_run.eval_cb = default_eval_cb;
-    cur_test_run.have_churn = HAVE_CHURN;
+    cur_test_run.have_churn = HAVE_NO_CHURN;
     cur_test_run.have_quick_quit = HAVE_NO_QUICK_QUIT;
-    timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
+    timeout_s = 40;
+  }
+
+  else if (strstr (argv[0], "_sub") != NULL)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test subs\n");
+    cur_test_run.name = "test-rps-sub";
+    num_peers = 5;
+    //cur_test_run.init_peer = &default_init_peer;
+    cur_test_run.pre_test = &sub_pre;
+    cur_test_run.main_test = &single_req_cb;
+    //cur_test_run.reply_handle = default_reply_handle;
+    cur_test_run.post_test = &sub_post;
+    //cur_test_run.eval_cb = default_eval_cb;
+    cur_test_run.have_churn = HAVE_NO_CHURN;
+    cur_test_run.have_quick_quit = HAVE_QUICK_QUIT;
   }
 
   else if (strstr (argv[0], "profiler") != NULL)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
     cur_test_run.name = "test-rps-profiler";
-    num_peers = 10;
+    num_peers = 16;
     mal_type = 3;
     cur_test_run.init_peer = profiler_init_peer;
     //cur_test_run.pre_test = mal_pre;
@@ -2560,7 +2953,8 @@ main (int argc, char *argv[])
     cur_test_run.post_test = post_profiler;
     cur_test_run.request_interval = 2;
     cur_test_run.num_requests = 5;
-    cur_test_run.have_churn = HAVE_CHURN;
+    //cur_test_run.have_churn = HAVE_CHURN;
+    cur_test_run.have_churn = HAVE_NO_CHURN;
     cur_test_run.have_quick_quit = HAVE_NO_QUICK_QUIT;
     cur_test_run.have_collect_statistics = COLLECT_STATISTICS;
     cur_test_run.stat_collect_flags = STAT_TYPE_ROUNDS |
@@ -2580,12 +2974,13 @@ main (int argc, char *argv[])
                                       STAT_TYPE_RECV_PULL_REQ |
                                       STAT_TYPE_RECV_PULL_REP;
     cur_test_run.have_collect_view = COLLECT_VIEW;
-    timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300);
+    timeout_s = 150;
 
     /* 'Clean' directory */
     (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
     GNUNET_DISK_directory_create ("/tmp/rps/");
   }
+  timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_s);
 
   rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
   peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
@@ -2613,6 +3008,7 @@ main (int argc, char *argv[])
   }
 
   ret_value = cur_test_run.eval_cb();
+
   if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
   {
     GNUNET_array_grow (rps_peers->cur_view,
@@ -2625,4 +3021,5 @@ main (int argc, char *argv[])
   return ret_value;
 }
 
+
 /* end of test_rps.c */