-fix config, shutdown issue
[oweals/gnunet.git] / src / ats / gnunet-service-ats_normalization.c
index 28ab2fb87e6d65bc0b46cb5431dd0a3676eb37bf..936f8ca346c9bedde1238d297ecea971afcbba85 100644 (file)
@@ -1,22 +1,22 @@
 /*
    This file is part of GNUnet.
    (C) 2011 Christian Grothoff (and other contributing authors)
-
    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 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.
-
    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., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
-*/
+ This file is part of GNUnet.
Copyright (C) 2011-2015 Christian Grothoff (and other contributing authors)
+
+ 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 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.
+
+ 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., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+ */
 
 /**
  * @file ats/gnunet-service-ats_normalization.c
  * @author Christian Grothoff
  */
 #include "platform.h"
+#include <float.h>
 #include "gnunet_ats_service.h"
+#include "gnunet-service-ats_addresses.h"
 #include "gnunet-service-ats_normalization.h"
+#include "gnunet-service-ats_plugins.h"
 
+#define LOG(kind,...) GNUNET_log_from (kind, "ats-normalization",__VA_ARGS__)
 
 
 /**
- * Preference client
+ * Range information for normalization of quality properties.
  */
-struct PreferenceClient
+struct Property
 {
   /**
-   * Next in DLL
-   */
-  struct PreferenceClient *prev;
-
-  /**
-   * Next in DLL
-   */
-
-  struct PreferenceClient *next;
-
-  /**
-   * Client handle
+   * Index into the properties array.
    */
-  void *client;
+  uint32_t prop_type;
 
   /**
-   * Total preference for this peer
+   * Corresponding enum value of the respective quality property.
    */
-  double f_abs_sum[GNUNET_ATS_PreferenceCount];
+  enum GNUNET_ATS_Property atsi_type;
 
   /**
-   * List of peer preferences for this client
+   * Minimum value we see for this property across all addresses.
    */
+  uint32_t min;
 
   /**
-   * Head of peer list
+   * Maximum value we see for this property across all addresses.
    */
-  struct PreferencePeer *p_head;
-
-  /**
-   * Tail of peer list
-   */
-  struct PreferencePeer *p_tail;
+  uint32_t max;
 };
 
 
 /**
- * Preference peer
+ * Range information for all quality properties we see.
  */
-struct PreferencePeer
-{
-  /**
-   * Next in DLL
-   */
-  struct PreferencePeer *next;
+static struct Property properties[GNUNET_ATS_QualityPropertiesCount];
 
-  /**
-   * Previous in DLL
-   */
-  struct PreferencePeer *prev;
 
-  /**
-   * Client
-   */
-  struct PreferenceClient *client;
 
-  /**
-   * Peer id
-   */
-  struct GNUNET_PeerIdentity id;
+/**
+ * Add the value from @a atsi to the running average of the
+ * given @a ni quality property.
+ *
+ * @param ni normalization information to update
+ * @param atsi the ats information
+ */
+static void
+property_average (struct GAS_NormalizationInfo *ni,
+                  const struct GNUNET_ATS_Information *atsi)
+{
+  uint32_t current_val;
+  uint32_t res;
+  uint64_t sum;
+  uint32_t count;
+  unsigned int c1;
+
+  current_val = ntohl (atsi->value);
+  GNUNET_assert (GNUNET_ATS_VALUE_UNDEFINED != current_val);
+  ni->atsi_abs[ni->avg_queue_index++] = current_val;
+  if (GAS_normalization_queue_length == ni->avg_queue_index)
+    ni->avg_queue_index = 0;
+  count = 0;
+  sum = 0;
+  for (c1 = 0; c1 < GAS_normalization_queue_length; c1++)
+  {
+    if (GNUNET_ATS_VALUE_UNDEFINED != ni->atsi_abs[c1])
+    {
+      count++;
+      sum += ni->atsi_abs[c1];
+    }
+  }
+  GNUNET_assert (0 != count);
+  res = sum / count;
+  ni->avg = res;
+}
 
-  /**
-   * Absolute preference values
-   */
-  double f_abs[GNUNET_ATS_PreferenceCount];
 
+/**
+ * Closure for #find_min_max_it().
+ */
+struct FindMinMaxCtx
+{
   /**
-   * Relative preference values
+   * Property we are looking for.
    */
-  double f_rel[GNUNET_ATS_PreferenceCount];
+  struct Property *p;
 
   /**
-   * Relative total preference value
+   * Set to mimimum value observed.
    */
-  double f_rel_total;
-
+  uint32_t min;
 
   /**
-   * Aging Task
+   * Set to maximum value observed.
    */
-  GNUNET_SCHEDULER_TaskIdentifier aging_task;
+  uint32_t max;
 };
 
 
-
-struct PreferenceClient *pc_head;
-struct PreferenceClient *pc_tail;
-
-
-
-static double
-update_peers (struct GNUNET_PeerIdentity *id,
-                                                       enum GNUNET_ATS_PreferenceKind kind)
-{
-       struct PreferenceClient *c_cur;
-       struct PreferencePeer *p_cur;
-       double f_rel_total;
-       unsigned int count;
-
-       f_rel_total = 0.0;
-       count = 0;
-       for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next)
-       {
-               for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
-               {
-                       if (0 == memcmp (id, &p_cur->id, sizeof (struct GNUNET_PeerIdentity)))
-                               break;
-               }
-               if (NULL != p_cur)
-               {
-                       f_rel_total +=  p_cur->f_rel[kind];
-                       count ++;
-               }
-       }
-
-       if (0 < count)
-       {
-               f_rel_total /= count;
-       }
-       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Total relative preference for all %u clients for %s is %.3f\n",
-                       count,
-                       GNUNET_ATS_print_preference_type (kind),
-                       f_rel_total);
-       return f_rel_total;
-}
-
 /**
- * Recalculate preference for a specific ATS property
+ * Function called for all addresses and peers to find the minimum and
+ * maximum (averaged) values for a given quality property.  Given
+ * those, we can then calculate the normalized score.
  *
- * @param p the peer
- * @param kind the preference kind
+ * @param cls the `struct FindMinMaxCtx`
+ * @param h which peer are we looking at (ignored)
+ * @param k the address for that peer
+ * @return #GNUNET_OK (continue to iterate)
  */
-static double
-recalculate_rel_preferences (struct PreferenceClient *c,
-                                                                                                struct PreferencePeer *p,
-                                                                                                enum GNUNET_ATS_PreferenceKind kind)
+static int
+find_min_max_it (void *cls,
+                 const struct GNUNET_PeerIdentity *h,
+                 void *k)
 {
-       struct PreferencePeer *p_cur;
-       double backup;
-       double res;
-       double ret;
-
-       /* For this client: sum preferences to total preference */
-       c->f_abs_sum[kind] = 0;
-       for (p_cur = c->p_head; NULL != p_cur; p_cur = p_cur->next)
-               c->f_abs_sum[kind] += p_cur->f_abs[kind];
-
-       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p has total preference for %s of %.3f\n",
-                       c->client,
-                       GNUNET_ATS_print_preference_type (kind),
-                       c->f_abs_sum[kind]);
-
-       ret = 0.0;
-       /* For all peers: calculate relative preference */
-       for (p_cur = c->p_head; NULL != p_cur; p_cur = p_cur->next)
-       {
-               /* Calculate relative preference for specific kind */
-               backup = p_cur->f_rel[kind];
-               if (DEFAULT_ABS_PREFERENCE == c->f_abs_sum[kind])
-               {
-                               /* No peer has a preference for this property, so set default preference */
-                               p_cur->f_rel[kind] = DEFAULT_REL_PREFERENCE;
-               }
-               else
-               {
-                               p_cur->f_rel[kind] = (c->f_abs_sum[kind] + p_cur->f_abs[kind]) / c->f_abs_sum[kind];
-               }
-
-               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p: peer `%s' has relative preference for %s of %.3f\n",
-                               c->client,
-                               GNUNET_i2s (&p_cur->id),
-                               GNUNET_ATS_print_preference_type (kind),
-                               p_cur->f_rel[kind]);
-
-               res = 0.0;
-               if (p_cur->f_rel[kind] != backup)
-               {
-                       res = update_peers (&p_cur->id,kind);
-                       if (0 == memcmp (&p->id, &p_cur->id, sizeof (struct GNUNET_PeerIdentity)))
-                       {
-                               ret = res;
-                       }
-               }
-
-       }
-       return ret;
+  struct FindMinMaxCtx *find_res = cls;
+  const struct ATS_Address *a = k;
+
+  find_res->max = GNUNET_MAX (find_res->max,
+                              a->atsin[find_res->p->prop_type].avg);
+  find_res->min = GNUNET_MIN (find_res->min,
+                              a->atsin[find_res->p->prop_type].avg);
+  return GNUNET_OK;
 }
 
-static double
-update_preference (struct PreferenceClient *c,
-                                                                        struct PreferencePeer *p,
-                                                                        enum GNUNET_ATS_PreferenceKind kind,
-                                                        float score_abs)
-{
-       double score = score_abs;
-
-  /* Update preference value according to type */
-  switch (kind) {
-    case GNUNET_ATS_PREFERENCE_BANDWIDTH:
-    case GNUNET_ATS_PREFERENCE_LATENCY:
-      p->f_abs[kind] = (p->f_abs[kind] + score) / 2;
-      break;
-    case GNUNET_ATS_PREFERENCE_END:
-      break;
-    default:
-      break;
-  }
-  return recalculate_rel_preferences (c, p, kind);
-}
 
-static void
-preference_aging (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+/**
+ * Normalize the property value for a given address based
+ * on the range we know that property value has globally.
+ *
+ * @param cls the `struct Property` with details on the
+ *            property and its global range
+ * @param key which peer are we looking at (ignored)
+ * @param value the address for that peer, from where we get
+ *            the original value and where we write the
+ *            normalized value
+ * @return #GNUNET_OK (continue to iterate)
+ */
+static int
+normalize_address (void *cls,
+                  const struct GNUNET_PeerIdentity *key,
+                  void *value)
 {
-       int i;
-       //double *t = NULL;
-       double backup;
-       struct PreferencePeer *p = cls;
-       GNUNET_assert (NULL != p);
-
-       p->aging_task = GNUNET_SCHEDULER_NO_TASK;
-
-       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Aging preferences for peer `%s'\n",
-               GNUNET_i2s (&p->id));
-
-  /* Issue for aging :
-   *
-   * Not for every peer preference values are set by default, so reducing the
-   * absolute preference value does not help for aging because it does not have
-   * influence on the relative values.
-   *
-   * So we have to reduce the relative value to have an immediate impact on
-   * quota calculation. In addition we cannot call recalculate_preferences here
-   * but instead reduce the absolute value to have an aging impact on future
-   * calls to change_preference where recalculate_preferences is called
-   *
-   */
-  /* Aging absolute values: */
-  for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
-  {
-                       backup = p->f_abs[i];
-               if (p->f_abs[i] > DEFAULT_ABS_PREFERENCE)
-                       p->f_abs[i] *= PREF_AGING_FACTOR;
-               if (backup != p->f_abs[i])
-               {
-                       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Aged preference for peer `%s' from %.3f to %.3f\n",
-                       GNUNET_i2s (&p->id), backup, p->f_abs[i]);
-                       recalculate_rel_preferences (p->client, p, i);
-               }
-  }
-  p->aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL,
-               &preference_aging, p);
+  struct Property *p = cls;
+  struct ATS_Address *address = value;
+  double delta;
+  double update;
+  uint32_t avg_value;
+
+  avg_value = address->atsin[p->prop_type].avg;
+  delta = p->max - p->min;
+  /* max - 2 * min + avg_value / (max - min) */
+  if (delta > DBL_EPSILON)
+    update = DEFAULT_REL_QUALITY + (avg_value - p->min) / delta;
+  else
+    update = DEFAULT_REL_QUALITY;
+
+  if (update == address->atsin[p->prop_type].norm)
+    return GNUNET_OK;
+  address->atsin[p->prop_type].norm = update;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Normalize `%s' address %p's '%s' with value %u to range [%u..%u] = %.3f\n",
+       GNUNET_i2s (&address->peer),
+       address,
+       GNUNET_ATS_print_property_type (p->atsi_type),
+       address->atsin[p->prop_type].avg,
+       p->min,
+       p->max,
+       address->atsin[p->prop_type].norm);
+  return GNUNET_OK;
 }
 
+
 /**
- * Changes the preferences for a peer in the problem
+ * Notify about change in normalized property.
  *
- * @param solver the solver handle
- * @param client the client with this preference
- * @param peer the peer to change the preference for
- * @param kind the kind to change the preference
- * @param score the normalized score
+ * @param cls the `struct Property` with details on the
+ *            property and its global range
+ * @param key which peer are we looking at (ignored)
+ * @param value the address for that peer
+ * @return #GNUNET_OK (continue to iterate)
  */
-float
-GAS_normalization_change_preference (void *src,
-                                        const struct GNUNET_PeerIdentity *peer,
-                                        enum GNUNET_ATS_PreferenceKind kind,
-                                        float score_abs)
+static int
+notify_change (void *cls,
+               const struct GNUNET_PeerIdentity *key,
+               void *value)
 {
-       float score_rel = 1.0;
-  struct PreferenceClient *c_cur;
-  struct PreferencePeer *p_cur;
-  int i;
-
+  struct Property *p = cls;
+  struct ATS_Address *address = value;
 
-  GNUNET_assert (NULL != src);
-  GNUNET_assert (NULL != peer);
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p changes preference for peer `%s' for `%s' to %.2f\n",
-                                src,
-                                GNUNET_i2s (peer),
-                                GNUNET_ATS_print_preference_type (kind),
-                                score_abs);
+  GAS_plugin_notify_property_changed (address,
+                                  p->atsi_type,
+                                  address->atsin[p->prop_type].norm);
+  return GNUNET_OK;
+}
 
-  if (kind >= GNUNET_ATS_PreferenceCount)
-  {
-      GNUNET_break (0);
-      return 0.0;
-  }
 
-  /* Find preference client */
-  for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next)
-  {
-      if (src == c_cur->client)
-        break;
-  }
-  /* Not found: create new preference client */
-  if (NULL == c_cur)
+/**
+ * Update and normalize atsi performance information
+ *
+ * @param address the address to update
+ * @param atsi the array of performance information
+ * @param atsi_count the number of atsi information in the array
+ */
+void
+GAS_normalization_update_property (struct ATS_Address *address,
+                                      const struct GNUNET_ATS_Information *atsi,
+                                      uint32_t atsi_count)
+{
+  unsigned int c1;
+  unsigned int c2;
+  uint32_t current_type;
+  uint32_t old;
+  struct FindMinMaxCtx find_ctx;
+  int range_changed;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Updating %u elements for peer `%s'\n",
+       atsi_count,
+       GNUNET_i2s (&address->peer));
+  GAS_plugin_solver_lock ();
+  for (c1 = 0; c1 < atsi_count; c1++)
   {
-    c_cur = GNUNET_malloc (sizeof (struct PreferenceClient));
-    c_cur->client = src;
-    GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, c_cur);
-  }
+    current_type = ntohl (atsi[c1].type);
 
-  /* Find entry for peer */
-  for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
-    if (0 == memcmp (&p_cur->id, peer, sizeof (p_cur->id)))
+    for (c2 = 0; c2 < GNUNET_ATS_QualityPropertiesCount; c2++)
+      if (current_type == properties[c2].atsi_type)
         break;
-
-  /* Not found: create new peer entry */
-  if (NULL == p_cur)
-  {
-      p_cur = GNUNET_malloc (sizeof (struct PreferencePeer));
-      p_cur->client = c_cur;
-      p_cur->id = (*peer);
-      for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
+    if (GNUNET_ATS_QualityPropertiesCount == c2)
+    {
+      /* Not a quality property, continue with next element */
+      continue;
+    }
+    /* Calculate running average */
+    old = address->atsin[c2].avg;
+    property_average (&address->atsin[c2],
+                      &atsi[c1]);
+    if (old == address->atsin[c2].avg)
+      continue; /* no change */
+    range_changed = GNUNET_NO;
+    if ( (old == properties[c2].min) ||
+         (old == properties[c2].max) ||
+         (address->atsin[c2].avg < properties[c2].min) ||
+         (address->atsin[c2].avg > properties[c2].max) )
+    {
+      /* need to re-calculate min/max range, as it may have changed */
+      find_ctx.p = &properties[c2];
+      find_ctx.max = 0;
+      find_ctx.min = UINT32_MAX;
+      if (0 ==
+          GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
+                                                 &find_min_max_it,
+                                                 &find_ctx))
+      {
+        GNUNET_break(0);
+        continue;
+      }
+      if ( (find_ctx.min != properties[c2].min) ||
+           (find_ctx.max != properties[c2].max) )
       {
-        /* Default value per peer absolut preference for a quality:
-         * No value set, so absolute preference 0 */
-        p_cur->f_abs[i] = DEFAULT_ABS_PREFERENCE;
-        /* Default value per peer relative preference for a quality: 1.0 */
-        p_cur->f_rel[i] = DEFAULT_REL_PREFERENCE;
+        properties[c2].min = find_ctx.min;
+        properties[c2].max = find_ctx.max;
+        /* limits changed, (re)normalize all addresses */
+        range_changed = GNUNET_YES;
       }
-      p_cur->aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL, &preference_aging, p_cur);
-      GNUNET_CONTAINER_DLL_insert (c_cur->p_head, c_cur->p_tail, p_cur);
+    }
+    if (GNUNET_YES == range_changed)
+      GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
+                                             &normalize_address,
+                                             &properties[c2]);
+    else
+      normalize_address (&properties[c2],
+                         &address->peer,
+                         address);
+    /* after all peers have been updated, notify about changes */
+    if (GNUNET_YES == range_changed)
+      GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
+                                             &notify_change,
+                                             &properties[c2]);
+    else
+      notify_change (&properties[c2],
+                     &address->peer,
+                     address);
+
   }
-  score_rel = update_preference (c_cur, p_cur, kind, score_abs);
-  return score_rel;
+  GAS_plugin_solver_unlock ();
 }
 
+
+/**
+ * Start the normalization component
+ */
 void
 GAS_normalization_start ()
 {
-       return;
+  unsigned int c1;
+  const unsigned int existing_properties[] = GNUNET_ATS_QualityProperties;
+
+  for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
+  {
+    properties[c1].prop_type = c1;
+    properties[c1].atsi_type = existing_properties[c1];
+    properties[c1].min = UINT32_MAX;
+    properties[c1].max = 0;
+  }
 }
 
+
+/**
+ * Stop the normalization component and free all items
+ */
 void
 GAS_normalization_stop ()
 {
-  struct PreferenceClient *pc;
-  struct PreferenceClient *next_pc;
-  struct PreferencePeer *p;
-  struct PreferencePeer *next_p;
-
-  next_pc = pc_head;
-  while (NULL != (pc = next_pc))
-  {
-      next_pc = pc->next;
-      GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
-      next_p = pc->p_head;
-      while (NULL != (p = next_p))
-      {
-          next_p = p->next;
-          if (GNUNET_SCHEDULER_NO_TASK != p->aging_task)
-          {
-               GNUNET_SCHEDULER_cancel(p->aging_task);
-               p->aging_task = GNUNET_SCHEDULER_NO_TASK;
-          }
-          GNUNET_CONTAINER_DLL_remove (pc->p_head, pc->p_tail, p);
-          GNUNET_free (p);
-      }
-      GNUNET_free (pc);
-  }
-       return;
+  /* nothing to do */
 }
 
+
 /* end of gnunet-service-ats_normalization.c */