rps service/profiler: count observed (unique) peers
authorJulius Bünger <buenger@mytum.de>
Thu, 12 Apr 2018 10:06:46 +0000 (12:06 +0200)
committerJulius Bünger <buenger@mytum.de>
Thu, 12 Apr 2018 20:25:42 +0000 (22:25 +0200)
src/rps/gnunet-service-rps.c
src/rps/rps-test_util.c
src/rps/rps-test_util.h
src/rps/test_rps.c

index c23a64285228353d8a2c406414d1e1eda82f447c..eb47903e000dd2b41adb5e38ef7f94c98a1dbb3a 100644 (file)
@@ -1920,7 +1920,24 @@ static struct RPS_Sampler *client_sampler;
 /**
  * Name to log view to
  */
-static char *file_name_view_log;
+static const char *file_name_view_log;
+
+#ifdef TO_FILE
+/**
+ * Name to log number of observed peers to
+ */
+static const char *file_name_observed_log;
+
+/**
+ * @brief Count the observed peers
+ */
+static uint32_t num_observed_peers;
+
+/**
+ * @brief Multipeermap (ab-) used to count unique peer_ids
+ */
+static struct GNUNET_CONTAINER_MultiPeerMap *observed_unique_peers;
+#endif /* TO_FILE */
 
 /**
  * The size of sampler we need to be able to satisfy the client's need
@@ -2511,6 +2528,21 @@ insert_in_sampler (void *cls,
      * messages to it */
     //Peers_indicate_sending_intention (peer);
   }
+  #ifdef TO_FILE
+  num_observed_peers++;
+  GNUNET_CONTAINER_multipeermap_put
+    (observed_unique_peers,
+     peer,
+     NULL,
+     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+  uint32_t num_observed_unique_peers = GNUNET_CONTAINER_multipeermap_size (
+      observed_unique_peers);
+  to_file (file_name_observed_log,
+          "%" PRIu32 " %" PRIu32 " %f\n",
+          num_observed_peers,
+          num_observed_unique_peers,
+          1.0*num_observed_unique_peers/num_observed_peers)
+  #endif /* TO_FILE */
 }
 
 /**
@@ -4126,7 +4158,12 @@ shutdown_task (void *cls)
   }
   #ifdef ENABLE_MALICIOUS
   struct AttackedPeer *tmp_att_peer;
-  GNUNET_free (file_name_view_log);
+  /* it is ok to free this const during shutdown: */
+  GNUNET_free ((char *) file_name_view_log);
+  #ifdef TO_FILE
+  GNUNET_free ((char *) file_name_observed_log);
+  GNUNET_CONTAINER_multipeermap_destroy (observed_unique_peers);
+  #endif /* TO_FILE */
   GNUNET_array_grow (mal_peers, num_mal_peers, 0);
   if (NULL != mal_peer_set)
     GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
@@ -4211,8 +4248,6 @@ run (void *cls,
      const struct GNUNET_CONFIGURATION_Handle *c,
      struct GNUNET_SERVICE_Handle *service)
 {
-  int size;
-  int out_size;
   char* fn_valid_peers;
   struct GNUNET_HashCode port;
 
@@ -4271,27 +4306,11 @@ run (void *cls,
   View_create (view_size_est_min);
 
   /* file_name_view_log */
-  if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
-  {
-    LOG (GNUNET_ERROR_TYPE_WARNING,
-         "Failed to create directory /tmp/rps/\n");
-  }
-
-  size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
-  file_name_view_log = GNUNET_malloc (size);
-  out_size = GNUNET_snprintf (file_name_view_log,
-                              size,
-                              "/tmp/rps/view-%s",
-                              GNUNET_i2s_full (&own_identity));
-  if (size < out_size ||
-      0 > out_size)
-  {
-    LOG (GNUNET_ERROR_TYPE_WARNING,
-         "Failed to write string to buffer (size: %i, out_size: %i)\n",
-         size,
-         out_size);
-  }
-
+  file_name_view_log = store_prefix_file_name (&own_identity, "view");
+  #ifdef TO_FILE
+  file_name_observed_log = store_prefix_file_name (&own_identity, "observed");
+  observed_unique_peers = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
+  #endif /* TO_FILE */
 
   /* connect to NSE */
   nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
index 817967e0c1f6ec43d6971a2b1b18b8da82cb650f..869170a8ae469207896b6c792e5e0beed58cb795 100644 (file)
@@ -208,4 +208,55 @@ create_file (const char *name)
 
 #endif /* TO_FILE */
 
+/**
+ * @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;
+}
+
+const char *
+store_prefix_file_name (const struct GNUNET_PeerIdentity *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 (peer);
+  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;
+}
+
 /* end of gnunet-service-rps.c */
index 725be815c85b2c45d65e8ad4745b9d90346f9c21..59f062331946619567acbc45aa02b0d07649cd47 100644 (file)
@@ -72,5 +72,9 @@ create_file (const char *name);
 #  define to_file_w_len(file_name, len, ...)
 #endif /* TO_FILE */
 
+const char *
+store_prefix_file_name (const struct GNUNET_PeerIdentity *peer,
+    const char *prefix);
+
 #endif /* RPS_TEST_UTIL_H */
 /* end of gnunet-service-rps.c */
index 9ccf3e61ce3538c196826d5633474eb7c23a0a6c..8d31bf50d6437a8a8af3989bd1266f3cdfeb96ba 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)
@@ -2253,7 +2203,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);
 }
 
@@ -2576,7 +2527,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),