fix comment about mq allocation
[oweals/gnunet.git] / src / util / bandwidth.c
index 4afaf4e4bcd075d8b62aec10cc943d644e8ac316..980af764a0638aa954ba0f2b06c6c513368c8f4a 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2010, 2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2010, 2013 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -40,9 +40,6 @@ GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second)
 {
   struct GNUNET_BANDWIDTH_Value32NBO ret;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Initializing bandwidth of %u Bps\n",
-       (unsigned int) bytes_per_second);
   ret.value__ = htonl (bytes_per_second);
   return ret;
 }
@@ -142,18 +139,19 @@ GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
  * Task run whenever we hit the bandwidth limit for a tracker.
  *
  * @param cls the `struct GNUNET_BANDWIDTH_Tracker`
- * @param tc scheduler context
  */
 static void
-excess_trigger (void *cls,
-                const struct GNUNET_SCHEDULER_TaskContext *tc)
+excess_trigger (void *cls)
 {
   struct GNUNET_BANDWIDTH_Tracker *av = cls;
 
   av->excess_task = NULL;
-
   if (NULL != av->excess_cb)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+               "Notifying application about excess bandwidth\n");
     av->excess_cb (av->excess_cb_cls);
+  }
 }
 
 
@@ -179,11 +177,23 @@ update_excess (struct GNUNET_BANDWIDTH_Tracker *av)
       (delta_time * ((unsigned long long) av->available_bytes_per_s__) +
        500000LL) / 1000000LL;
   current_consumption = av->consumption_since_last_update__ - delta_avail;
+  if (current_consumption > av->consumption_since_last_update__)
+  {
+    /* integer underflow, cap! */
+    current_consumption = INT64_MIN;
+  }
   /* negative current_consumption means that we have savings */
-  max_carry = av->available_bytes_per_s__ * av->max_carry_s__;
+  max_carry = ((uint64_t) av->available_bytes_per_s__) * av->max_carry_s__;
   if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE)
     max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE;
-  left_bytes = max_carry + current_consumption;
+  if (max_carry > INT64_MAX)
+    max_carry = INT64_MAX;
+  left_bytes = current_consumption + max_carry;
+  if (left_bytes < current_consumption)
+  {
+    /* integer overflow, cap! */
+    left_bytes = INT64_MAX;
+  }
   /* left_bytes now contains the number of bytes needed until
      we have more savings than allowed */
   if (left_bytes < 0)
@@ -193,11 +203,16 @@ update_excess (struct GNUNET_BANDWIDTH_Tracker *av)
   }
   else
   {
+    double factor = 1.0 * left_bytes / (double) av->available_bytes_per_s__; 
     delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
-                                           left_bytes);
-    delay = GNUNET_TIME_relative_divide (delay,
-                                         av->available_bytes_per_s__);
+                                           (unsigned long long) factor);
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "At %llu bps it will take us %s for %lld bytes to reach excess threshold\n",
+             (unsigned long long) av->available_bytes_per_s__,
+             GNUNET_STRINGS_relative_time_to_string (delay,
+                                                     GNUNET_NO),
+             (long long) left_bytes);
   if (NULL != av->excess_task)
     GNUNET_SCHEDULER_cancel (av->excess_task);
   av->excess_task = GNUNET_SCHEDULER_add_delayed (delay,
@@ -216,7 +231,7 @@ update_excess (struct GNUNET_BANDWIDTH_Tracker *av)
  * bytes).
  *
  * To stop notifications about updates and excess callbacks use
- * #GNUNET_BANDWIDTH_tracker_notification_stop
+ * #GNUNET_BANDWIDTH_tracker_notification_stop().
  *
  * @param av tracker to initialize
  * @param update_cb callback to notify a client about the tracker being updated
@@ -256,10 +271,10 @@ GNUNET_BANDWIDTH_tracker_init2 (struct GNUNET_BANDWIDTH_Tracker *av,
 /**
  * Initialize bandwidth tracker.  Note that in addition to the
  * 'max_carry_s' limit, we also always allow at least
- * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate.  So if the
+ * #GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate.  So if the
  * bytes-per-second limit is so small that within 'max_carry_s' not
- * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
- * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
+ * even #GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
+ * ignored and replaced by #GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
  * bytes).
  *
  * @param av tracker to initialize
@@ -302,7 +317,6 @@ GNUNET_BANDWIDTH_tracker_notification_stop (struct GNUNET_BANDWIDTH_Tracker *av)
 }
 
 
-
 /**
  * Update the tracker, looking at the current time and
  * bandwidth consumption data.
@@ -328,20 +342,26 @@ update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
   av->last_update__ = now;
   if (av->consumption_since_last_update__ < 0)
   {
-    left_bytes = -av->consumption_since_last_update__;
-    max_carry = av->available_bytes_per_s__ * av->max_carry_s__;
+    left_bytes = - av->consumption_since_last_update__;
+    max_carry = ((unsigned long long) av->available_bytes_per_s__) *
+                av->max_carry_s__;
     if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE)
       max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE;
+    if (max_carry > INT64_MAX)
+      max_carry = INT64_MAX;
     if (max_carry > left_bytes)
-      av->consumption_since_last_update__ = -left_bytes;
+      av->consumption_since_last_update__ = - left_bytes;
     else
-      av->consumption_since_last_update__ = -max_carry;
+      av->consumption_since_last_update__ = - max_carry;
   }
   delta.rel_value_us = delta_time;
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Tracker %p updated, have %u Bps, last update was %s ago\n", av,
+       "Tracker %p updated, consumption at %lld at %u Bps, last update was %s ago\n",
+       av,
+       (long long) av->consumption_since_last_update__,
        (unsigned int) av->available_bytes_per_s__,
-       GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_YES));
+       GNUNET_STRINGS_relative_time_to_string (delta,
+                                              GNUNET_YES));
 }
 
 
@@ -371,6 +391,7 @@ GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
     nc = av->consumption_since_last_update__ + size;
     if (nc < av->consumption_since_last_update__)
     {
+      /* integer overflow, very bad */
       GNUNET_break (0);
       return GNUNET_SYSERR;
     }
@@ -380,14 +401,22 @@ GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
     if (av->consumption_since_last_update__ > 0)
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG,
-           "Tracker %p consumption %llu bytes above limit\n", av,
+           "Tracker %p consumption %llu bytes above limit\n",
+          av,
            (unsigned long long) av->consumption_since_last_update__);
       return GNUNET_YES;
     }
   }
   else
   {
-    av->consumption_since_last_update__ += size;
+    nc = av->consumption_since_last_update__ + size;
+    if (nc > av->consumption_since_last_update__)
+    {
+      /* integer underflow, very bad */
+      GNUNET_break (0);
+      return GNUNET_SYSERR;
+    }
+    av->consumption_since_last_update__ = nc;
     update_excess (av);
   }
   return GNUNET_NO;
@@ -430,7 +459,8 @@ GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
       (unsigned long long) av->available_bytes_per_s__;
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Tracker %p delay for %u bytes is %s\n",
-       av, (unsigned int) size,
+       av,
+       (unsigned int) size,
        GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
   return ret;
 }