fix crash on unexpected client disconnect on incoming message, remove bogus assertion
[oweals/gnunet.git] / src / rps / test_rps.c
index 03524eaebe44825e1f2e90d6dd636b8fdcbe1499..542919425da19ca90984fe8603233794f3157e12 100644 (file)
@@ -1809,56 +1809,6 @@ profiler_eval (void)
   return evaluate ();
 }
 
-/**
- * @brief Try to ensure that `/tmp/rps` exists.
- *
- * @return #GNUNET_YES on success
- *         #GNUNET_SYSERR on failure
- */
-static int ensure_folder_exist (void)
-{
-  if (GNUNET_NO == GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
-  {
-    GNUNET_DISK_directory_create ("/tmp/rps");
-  }
-  if (GNUNET_YES != GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
-  {
-    return GNUNET_SYSERR;
-  }
-  return GNUNET_YES;
-}
-
-static const char *
-store_prefix_file_name (struct RPSPeer *rps_peer, const char *prefix)
-{
-  unsigned int len_file_name;
-  unsigned int out_size;
-  char *file_name;
-  const char *pid_long;
-
-  if (GNUNET_SYSERR == ensure_folder_exist()) return NULL;
-  pid_long = GNUNET_i2s_full (rps_peer->peer_id);
-  len_file_name = (strlen (prefix) +
-                   strlen (pid_long) +
-                   11)
-                     * sizeof (char);
-  file_name = GNUNET_malloc (len_file_name);
-  out_size = GNUNET_snprintf (file_name,
-                              len_file_name,
-                              "/tmp/rps/%s-%s",
-                              prefix,
-                              pid_long);
-  if (len_file_name < out_size ||
-      0 > out_size)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-               "Failed to write string to buffer (size: %i, out_size: %i)\n",
-               len_file_name,
-               out_size);
-  }
-  return file_name;
-}
-
 static uint32_t fac (uint32_t x)
 {
   if (1 >= x)
@@ -1886,7 +1836,7 @@ static uint32_t binom (uint32_t n, uint32_t k)
  * @param a
  * @param b
  *
- * @return 
+ * @return
  */
 static int is_in_view (uint32_t a, uint32_t b)
 {
@@ -1917,6 +1867,9 @@ static uint32_t get_idx_of_pid (const struct GNUNET_PeerIdentity *pid)
     }
   }
   //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);
 }
 
@@ -1955,7 +1908,7 @@ 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 * 6 + 1) * sizeof (char);
+  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;
@@ -1967,10 +1920,16 @@ static void compute_probabilities (uint32_t peer_idx)
   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:
@@ -1989,6 +1948,10 @@ static void compute_probabilities (uint32_t peer_idx)
                  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,
@@ -2004,12 +1967,35 @@ static void compute_probabilities (uint32_t peer_idx)
        binom (view_size - cont_views, 0.45 * view_size));
     if (0 != number_of_being_in_pull_events)
     {
-      prob_pull = 1.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++;
   }
@@ -2032,7 +2018,7 @@ static void compute_probabilities (uint32_t peer_idx)
     probs_as_str_cpy = GNUNET_strndup (probs_as_str, probs_as_str_size);
     tmp = GNUNET_snprintf (probs_as_str,
                            probs_as_str_size,
-                           "%s %3.2f", probs_as_str_cpy, probs[i]);
+                           "%s %7.6f", probs_as_str_cpy, probs[i]);
     GNUNET_free (probs_as_str_cpy);
     GNUNET_assert (0 <= tmp);
   }
@@ -2186,30 +2172,33 @@ void view_update_cb (void *cls,
                      rps_peer->cur_view_count,
                      view_size);
   //*rps_peer->cur_view = *peers;
-  memcpy (rps_peer->cur_view,
-          peers,
-          view_size * 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();
-  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 */
-         );
+  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();
 }
@@ -2217,7 +2206,8 @@ void view_update_cb (void *cls,
 static void
 pre_profiler (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
 {
-  rps_peer->file_name_probs = store_prefix_file_name (rps_peer, "probs");
+  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);
 }
 
@@ -2540,7 +2530,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;
-      rps_peer->file_name_stats = store_prefix_file_name (rps_peer, "stats");
+      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),
@@ -2782,7 +2773,7 @@ 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);
   }
@@ -2802,7 +2793,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 |