Returns now GNUNET_SYSERR
[oweals/gnunet.git] / src / util / load.c
index 7eff409196f4f73dde42e36bf338b6f7bfa8eab9..9bbea6c140401dd0a24411640fdded63a5e1ecd0 100644 (file)
 struct GNUNET_LOAD_Value 
 {
 
+  /**
+   * How fast should the load decline if no values are added?
+   */
+  struct GNUNET_TIME_Relative autodecline;
+
+  /**
+   * Last time this load value was updated by an event.
+   */ 
+  struct GNUNET_TIME_Absolute last_update;
+
   /**
    * Sum of all datastore delays ever observed (in ms).  Note that
    * delays above 64k ms are excluded (to avoid overflow within
@@ -70,15 +80,109 @@ struct GNUNET_LOAD_Value
 };
 
 
+static void
+internal_update (struct GNUNET_LOAD_Value *load)
+{
+  struct GNUNET_TIME_Relative delta;
+  unsigned int n;
+
+  if (load->autodecline.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
+    return;
+  delta = GNUNET_TIME_absolute_get_duration (load->last_update);
+  if (delta.rel_value < load->autodecline.rel_value)
+    return;
+  if (load->autodecline.rel_value == 0)
+    {
+      load->runavg_delay = 0.0;
+      load->load = 0;
+      return;
+    }
+  n = delta.rel_value / load->autodecline.rel_value;
+  if (n > 16)
+    {
+      load->runavg_delay = 0.0;
+      load->load = 0;
+      return;
+    }
+  while (n > 0)
+    {
+      n--;
+      load->runavg_delay = (load->runavg_delay * 7.0) / 8.0;
+    }  
+}
+
+
 /**
  * Create a new load value.
  *
+ * @param autodecline speed at which this value should automatically
+ *        decline in the absence of external events; at the given
+ *        frequency, 0-load values will be added to the load
  * @return the new load value
  */
 struct GNUNET_LOAD_Value *
-GNUNET_LOAD_value_init ()
+GNUNET_LOAD_value_init (struct GNUNET_TIME_Relative autodecline)
+{
+  struct GNUNET_LOAD_Value *ret;
+
+  ret = GNUNET_malloc (sizeof (struct GNUNET_LOAD_Value));
+  ret->autodecline = autodecline;
+  ret->last_update = GNUNET_TIME_absolute_get ();
+  return ret;
+}
+
+
+/**
+ * Change the value by which the load automatically declines.
+ *
+ * @param load load to update
+ * @param autodecline frequency of load decline
+ */
+void
+GNUNET_LOAD_value_set_decline (struct GNUNET_LOAD_Value *load,
+                              struct GNUNET_TIME_Relative autodecline)
 {
-  return GNUNET_malloc (sizeof (struct GNUNET_LOAD_Value));
+  internal_update (load);
+  load->autodecline = autodecline;  
+}
+
+
+/**
+ * Recalculate our load value.
+ *
+ * @param load load to update
+ */
+static void
+calculate_load (struct GNUNET_LOAD_Value *load)
+{
+  double stddev;
+  double avgdel;
+  double sum_val_i;
+  double n;
+  double nm1;
+
+  if (load->cummulative_request_count <= 1)
+    return;
+  /* calcuate std dev of latency; we have for n values of "i" that:
+     
+     avg = (sum val_i) / n
+     stddev = (sum (val_i - avg)^2) / (n-1)
+     = (sum (val_i^2 - 2 avg val_i + avg^2) / (n-1)
+     = (sum (val_i^2) - 2 avg sum (val_i) + n * avg^2) / (n-1)
+  */
+  sum_val_i = (double) load->cummulative_delay;
+  n = ((double) load->cummulative_request_count);
+  nm1 = n - 1.0;
+  avgdel = sum_val_i / n;
+  stddev = (((double) load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i + n * avgdel * avgdel) / nm1; 
+  if (stddev <= 0)
+    stddev = 0.01; /* must have been rounding error or zero; prevent division by zero */
+  /* now calculate load based on how far out we are from
+     std dev; or if we are below average, simply assume load zero */
+  if (load->runavg_delay < avgdel)
+    load->load = 0.0;
+  else
+    load->load = (load->runavg_delay - avgdel) / stddev;      
 }
 
 
@@ -92,8 +196,10 @@ GNUNET_LOAD_value_init ()
  *         that we could not do proper calculations
  */
 double
-GNUNET_LOAD_get_load (const struct GNUNET_LOAD_Value *load)
+GNUNET_LOAD_get_load (struct GNUNET_LOAD_Value *load)
 {
+  internal_update (load);
+  calculate_load (load);
   return load->load;
 }
 
@@ -105,11 +211,12 @@ GNUNET_LOAD_get_load (const struct GNUNET_LOAD_Value *load)
  * @return zero if update was never called
  */
 double
-GNUNET_LOAD_get_average (const struct GNUNET_LOAD_Value *load)
+GNUNET_LOAD_get_average (struct GNUNET_LOAD_Value *load)
 {
   double n;
   double sum_val_i;
 
+  internal_update (load);
   if (load->cummulative_request_count == 0)
     return 0.0;
   n = ((double) load->cummulative_request_count);
@@ -129,12 +236,9 @@ GNUNET_LOAD_update (struct GNUNET_LOAD_Value *load,
                    uint64_t data)
 {
   uint32_t dv;
-  double stddev;
-  double avgdel;
-  double sum_val_i;
-  double n;
-  double nm1;
 
+  internal_update (load);
+  load->last_update = GNUNET_TIME_absolute_get ();
   if (data > 64 * 1024)
     {
       /* very large */
@@ -146,30 +250,8 @@ GNUNET_LOAD_update (struct GNUNET_LOAD_Value *load,
   load->cummulative_squared_delay += dv * dv; 
   load->cummulative_request_count++;
   load->runavg_delay = ((load->runavg_delay * 7.0) + dv) / 8.0;
-  if (load->cummulative_request_count > 1)
-    {
-      /* calcuate std dev of latency; we have for n values of "i" that:
-
-        avg = (sum val_i) / n
-        stddev = (sum (val_i - avg)^2) / (n-1)
-               = (sum (val_i^2 - 2 avg val_i + avg^2) / (n-1)
-                = (sum (val_i^2) - 2 avg sum (val_i) + n * avg^2) / (n-1)
-      */
-      sum_val_i = (double) load->cummulative_delay;
-      n = ((double) load->cummulative_request_count);
-      nm1 = n - 1.0;
-      avgdel = sum_val_i / n;
-      stddev = (((double) load->cummulative_squared_delay) - 2.0 * avgdel * sum_val_i + n * avgdel * avgdel) / nm1; 
-      if (stddev <= 0)
-       stddev = 0.01; /* must have been rounding error or zero; prevent division by zero */
-      /* now calculate load based on how far out we are from
-        std dev; or if we are below average, simply assume load zero */
-      if (load->runavg_delay < avgdel)
-       load->load = 0.0;
-      else
-       load->load = (load->runavg_delay - avgdel) / stddev;
-    }  
 }
 
 
+
 /* end of load.c */