- verboser log, faster start
[oweals/gnunet.git] / src / testbed / testbed_api_hosts.c
index e611644757f55e3b55cae1460da8ae53d3e39394..9eef0f0ccd73c813aa42d67082a2d8faa9022dad 100644 (file)
@@ -33,6 +33,8 @@
 
 #include "testbed_api.h"
 #include "testbed_api_hosts.h"
+#include "testbed_api_operations.h"
+#include "testbed_api_sd.h"
 
 /**
  * Generic logging shorthand
@@ -68,6 +70,28 @@ struct RegisteredController
 };
 
 
+/**
+ * A slot to record time taken by an overlay connect operation
+ */
+struct TimeSlot
+{
+  /**
+   * A key to identify this timeslot
+   */
+  void *key;
+
+  /**
+   * Time
+   */
+  struct GNUNET_TIME_Relative time;
+
+  /**
+   * Number of timing values accumulated
+   */
+  unsigned int nvals;
+};
+
+
 /**
  * Opaque handle to a host running experiments managed by the testing framework.
  * The master process must be able to SSH to this host without password (via
@@ -106,6 +130,34 @@ struct GNUNET_TESTBED_Host
    */
   struct RegisteredController *rc_tail;
 
+  /**
+   * Operation queue for simultaneous overlay connect operations target at this
+   * host
+   */
+  struct OperationQueue *opq_parallel_overlay_connect_operations;
+
+  /**
+   * An array of timing slots; size should be equal to the current number of parallel
+   * overlay connects
+   */
+  struct TimeSlot *tslots;
+
+  /**
+   * Handle for SD calculations amount parallel overlay connect operation finish
+   * times
+   */
+  struct SDHandle *poc_sd;
+
+  /**
+   * The number of parallel overlay connects we do currently
+   */
+  unsigned int num_parallel_connects;
+
+  /**
+   * Counter to indicate when all the available time slots are filled
+   */
+  unsigned int tslots_filled;
+
   /**
    * Global ID we use to refer to a host on the network
    */
@@ -241,7 +293,11 @@ GNUNET_TESTBED_host_create_with_id (uint32_t id, const char *hostname,
   host->hostname = (NULL != hostname) ? GNUNET_strdup (hostname) : NULL;
   host->username = (NULL != username) ? GNUNET_strdup (username) : NULL;
   host->id = id;
-  host->port = (0 == port) ? 22 : port;
+  host->port = (0 == port) ? 22 : port;  
+  host->opq_parallel_overlay_connect_operations =
+      GNUNET_TESTBED_operation_queue_create_ (0);
+  GNUNET_TESTBED_set_num_parallel_overlay_connects_ (host, 1);
+  host->poc_sd = GNUNET_TESTBED_SD_init_ (10);
   new_size = host_list_size;
   while (id >= new_size)
     new_size += HOST_LIST_GROW_STEP;
@@ -332,18 +388,18 @@ GNUNET_TESTBED_hosts_load_from_file (const char *filename,
     if (((data[offset] == '\n')) && (buf != &data[offset]))
     {
       data[offset] = '\0';
-      ret = SSCANF (buf, "%255[a-zA-Z0-9_]@%255[a-zA-Z0-9.]:%5hd",
-                    username, hostname, &port);
-      if  (3 == ret)
+      ret =
+          SSCANF (buf, "%255[a-zA-Z0-9_]@%255[a-zA-Z0-9.]:%5hd", username,
+                  hostname, &port);
+      if (3 == ret)
       {
         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                     "Successfully read host %s, port %d and user %s from file\n",
                     hostname, port, username);
         /* We store hosts in a static list; hence we only require the starting
-           host pointer in that list to access the newly created list of hosts */
+         * host pointer in that list to access the newly created list of hosts */
         if (NULL == starting_host)
-          starting_host = GNUNET_TESTBED_host_create (hostname, username,
-                                                      port);
+          starting_host = GNUNET_TESTBED_host_create (hostname, username, port);
         else
           (void) GNUNET_TESTBED_host_create (hostname, username, port);
         count++;
@@ -360,8 +416,7 @@ GNUNET_TESTBED_hosts_load_from_file (const char *filename,
   if (NULL == starting_host)
     return 0;
   *hosts = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * count);
-  memcpy (*hosts,
-          &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)],
+  memcpy (*hosts, &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)],
           sizeof (struct GNUNET_TESTBED_Host *) * count);
   return count;
 }
@@ -390,6 +445,10 @@ GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
   }
   GNUNET_free_non_null ((char *) host->username);
   GNUNET_free_non_null ((char *) host->hostname);
+  GNUNET_TESTBED_operation_queue_destroy_
+      (host->opq_parallel_overlay_connect_operations);
+  GNUNET_TESTBED_SD_destroy_ (host->poc_sd);
+  GNUNET_free_non_null (host->tslots);
   GNUNET_free (host);
   while (host_list_size >= HOST_LIST_GROW_STEP)
   {
@@ -475,7 +534,7 @@ struct GNUNET_TESTBED_HostHabitableCheckHandle
   /*  * the configuration handle to lookup the path of the testbed helper */
   /*  *\/ */
   /* const struct GNUNET_CONFIGURATION_Handle *cfg; */
-  
+
   /**
    * The callback to call once we have the status
    */
@@ -549,15 +608,14 @@ habitability_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   {
     h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
     h->habitability_check_task =
-        GNUNET_SCHEDULER_add_delayed (h->wait_time,
-                                      &habitability_check, h);
+        GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
     return;
   }
   GNUNET_OS_process_destroy (h->auxp);
   h->auxp = NULL;
   ret = (0 != code) ? GNUNET_NO : GNUNET_YES;
-  
- call_cb:
+
+call_cb:
   GNUNET_free (h->ssh_addr);
   GNUNET_free (h->portstr);
   GNUNET_free (h->helper_binary_path);
@@ -604,9 +662,10 @@ GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host,
     h->ssh_addr = GNUNET_strdup (hostname);
   else
     GNUNET_asprintf (&h->ssh_addr, "%s@%s", host->username, hostname);
-  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (config, "testbed",
-                                                          "HELPER_BINARY_PATH",
-                                                          &h->helper_binary_path))
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_string (config, "testbed",
+                                             "HELPER_BINARY_PATH",
+                                             &h->helper_binary_path))
     h->helper_binary_path =
         GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
   argp = 0;
@@ -628,7 +687,7 @@ GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host,
                                    NULL, "ssh", remote_args);
   if (NULL == h->auxp)
   {
-    GNUNET_break (0);         /* Cannot exec SSH? */
+    GNUNET_break (0);           /* Cannot exec SSH? */
     GNUNET_free (h->ssh_addr);
     GNUNET_free (h->portstr);
     GNUNET_free (h->helper_binary_path);
@@ -637,8 +696,7 @@ GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host,
   }
   h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
   h->habitability_check_task =
-      GNUNET_SCHEDULER_add_delayed (h->wait_time,
-                                    &habitability_check, h);
+      GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
   return h;
 }
 
@@ -646,7 +704,7 @@ GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host,
 /**
  * Function to cancel a request started using GNUNET_TESTBED_is_host_habitable()
  *
- * @param struct handle the habitability check handle
+ * @param handle the habitability check handle
  */
 void
 GNUNET_TESTBED_is_host_habitable_cancel (struct
@@ -662,4 +720,208 @@ GNUNET_TESTBED_is_host_habitable_cancel (struct
   GNUNET_free (handle->helper_binary_path);
   GNUNET_free (handle);
 }
+
+
+/**
+ * Initializes the operation queue for parallel overlay connects
+ *
+ * @param h the host handle
+ * @param npoc the number of parallel overlay connects - the queue size
+ */
+void
+GNUNET_TESTBED_set_num_parallel_overlay_connects_ (struct
+                                                   GNUNET_TESTBED_Host *h,
+                                                   unsigned int npoc)
+{
+  //fprintf (stderr, "%d", npoc);
+  GNUNET_free_non_null (h->tslots);
+  h->tslots_filled = 0;
+  h->num_parallel_connects = npoc;
+  h->tslots = GNUNET_malloc (npoc * sizeof (struct TimeSlot));
+  GNUNET_TESTBED_operation_queue_reset_max_active_
+      (h->opq_parallel_overlay_connect_operations, npoc);
+}
+
+
+/**
+ * Returns a timing slot which will be exclusively locked
+ *
+ * @param h the host handle
+ * @param key a pointer which is associated to the returned slot; should not be
+ *          NULL. It serves as a key to determine the correct owner of the slot
+ * @return the time slot index in the array of time slots in the controller
+ *           handle
+ */
+unsigned int
+GNUNET_TESTBED_get_tslot_ (struct GNUNET_TESTBED_Host *h, void *key)
+{
+  unsigned int slot;
+
+  GNUNET_assert (NULL != h->tslots);
+  GNUNET_assert (NULL != key);
+  for (slot = 0; slot < h->num_parallel_connects; slot++)
+    if (NULL == h->tslots[slot].key)
+    {
+      h->tslots[slot].key = key;
+      return slot;
+    }
+  GNUNET_assert (0);            /* We should always find a free tslot */
+}
+
+
+/**
+ * Decides whether any change in the number of parallel overlay connects is
+ * necessary to adapt to the load on the system
+ *
+ * @param h the host handle
+ */
+static void
+decide_npoc (struct GNUNET_TESTBED_Host *h)
+{
+  struct GNUNET_TIME_Relative avg;
+  int sd;
+  unsigned int slot;
+  unsigned int nvals;
+
+  if (h->tslots_filled != h->num_parallel_connects)
+    return;
+  avg = GNUNET_TIME_UNIT_ZERO;
+  nvals = 0;
+  for (slot = 0; slot < h->num_parallel_connects; slot++)
+  {
+    avg = GNUNET_TIME_relative_add (avg, h->tslots[slot].time);
+    nvals += h->tslots[slot].nvals;
+  }
+  GNUNET_assert (nvals >= h->num_parallel_connects);
+  avg = GNUNET_TIME_relative_divide (avg, nvals);
+  GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != avg.rel_value);
+  sd = GNUNET_TESTBED_SD_deviation_factor_ (h->poc_sd, (unsigned int) avg.rel_value);
+  if ( (sd <= 5) ||
+       (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                      h->num_parallel_connects)) )
+    GNUNET_TESTBED_SD_add_data_ (h->poc_sd, (unsigned int) avg.rel_value);
+  if (GNUNET_SYSERR == sd)
+  {
+    GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
+                                                       h->num_parallel_connects);
+    return;
+  }
+  GNUNET_assert (0 <= sd);
+  if (0 == sd)
+  {
+    GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
+                                                       h->num_parallel_connects
+                                                       * 2);
+    return;
+  }
+  if (1 == sd)
+  {
+    GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
+                                                       h->num_parallel_connects
+                                                       + 1);
+    return;
+  }
+  if (1 == h->num_parallel_connects)
+  {
+    GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h, 1);
+    return;
+  }
+  if (2 == sd)
+  {
+    GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
+                                                       h->num_parallel_connects
+                                                       - 1);
+    return;
+  }
+  GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
+                                                     h->num_parallel_connects /
+                                                     2);
+}
+
+
+/**
+ * Releases a time slot thus making it available for be used again
+ *
+ * @param h the host handle
+ * @param index the index of the the time slot
+ * @param key the key to prove ownership of the timeslot
+ * @return GNUNET_YES if the time slot is successfully removed; GNUNET_NO if the
+ *           time slot cannot be removed - this could be because of the index
+ *           greater than existing number of time slots or `key' being different
+ */
+int
+GNUNET_TESTBED_release_time_slot_ (struct GNUNET_TESTBED_Host *h,
+                                   unsigned int index, void *key)
+{
+  struct TimeSlot *slot;
+
+  GNUNET_assert (NULL != key);
+  if (index >= h->num_parallel_connects)
+    return GNUNET_NO;
+  slot = &h->tslots[index];
+  if (key != slot->key)
+    return GNUNET_NO;
+  slot->key = NULL;
+  return GNUNET_YES;
+}
+
+
+/**
+ * Function to update a time slot
+ *
+ * @param h the host handle
+ * @param index the index of the time slot to update
+ * @param key the key to identify ownership of the slot
+ * @param time the new time
+ * @param failed should this reading be treated as coming from a fail event
+ */
+void
+GNUNET_TESTBED_update_time_slot_ (struct GNUNET_TESTBED_Host *h,
+                                  unsigned int index, void *key,
+                                  struct GNUNET_TIME_Relative time, int failed)
+{
+  struct TimeSlot *slot;
+
+  if (GNUNET_YES == failed)
+  {
+    if (1 == h->num_parallel_connects)
+    {
+      GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h, 1);
+      return;
+    }
+    GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
+                                                       h->num_parallel_connects
+                                                       - 1);
+  }
+  if (GNUNET_NO == GNUNET_TESTBED_release_time_slot_ (h, index, key))
+    return;
+  slot = &h->tslots[index];
+  slot->nvals++;
+  if (GNUNET_TIME_UNIT_ZERO.rel_value == slot->time.rel_value)
+  {
+    slot->time = time;
+    h->tslots_filled++;
+    decide_npoc (h);
+    return;
+  }
+  slot->time = GNUNET_TIME_relative_add (slot->time, time);
+}
+
+
+/**
+ * Queues the given operation in the queue for parallel overlay connects of the
+ * given host
+ *
+ * @param h the host handle
+ * @param op the operation to queue in the given host's parally overlay connect
+ *          queue 
+ */
+void
+GNUNET_TESTBED_host_queue_oc (struct GNUNET_TESTBED_Host *h, 
+                              struct GNUNET_TESTBED_Operation *op)
+{  
+  GNUNET_TESTBED_operation_queue_insert_
+      (h->opq_parallel_overlay_connect_operations, op);
+}
+
 /* end of testbed_api_hosts.c */