Returns now GNUNET_SYSERR
[oweals/gnunet.git] / src / util / load.c
index fd8a5afabbd759045b073d646582cd81ae84d675..9bbea6c140401dd0a24411640fdded63a5e1ecd0 100644 (file)
@@ -86,12 +86,18 @@ internal_update (struct GNUNET_LOAD_Value *load)
   struct GNUNET_TIME_Relative delta;
   unsigned int n;
 
-  if (load->autodecline.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
+  if (load->autodecline.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
     return;
   delta = GNUNET_TIME_absolute_get_duration (load->last_update);
-  if (delta.value < load->autodecline.value)
+  if (delta.rel_value < load->autodecline.rel_value)
     return;
-  n = delta.value / load->autodecline.value;
+  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;
@@ -119,7 +125,6 @@ GNUNET_LOAD_value_init (struct GNUNET_TIME_Relative autodecline)
 {
   struct GNUNET_LOAD_Value *ret;
 
-  GNUNET_assert (autodecline.value != 0);
   ret = GNUNET_malloc (sizeof (struct GNUNET_LOAD_Value));
   ret->autodecline = autodecline;
   ret->last_update = GNUNET_TIME_absolute_get ();
@@ -127,6 +132,21 @@ GNUNET_LOAD_value_init (struct GNUNET_TIME_Relative autodecline)
 }
 
 
+/**
+ * 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)
+{
+  internal_update (load);
+  load->autodecline = autodecline;  
+}
+
+
 /**
  * Recalculate our load value.
  *
@@ -141,7 +161,7 @@ calculate_load (struct GNUNET_LOAD_Value *load)
   double n;
   double nm1;
 
-  if (load->cummulative_request_count == 0)
+  if (load->cummulative_request_count <= 1)
     return;
   /* calcuate std dev of latency; we have for n values of "i" that: