GNUnet  0.11.x
Macros | Functions
bandwidth.c File Reference

functions related to bandwidth (unit) More...

#include "platform.h"
#include "gnunet_util_lib.h"
Include dependency graph for bandwidth.c:

Go to the source code of this file.

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "util-bandwidth", __VA_ARGS__)
 

Functions

struct GNUNET_BANDWIDTH_Value32NBO GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second)
 Create a new bandwidth value. More...
 
struct GNUNET_BANDWIDTH_Value32NBO GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1, struct GNUNET_BANDWIDTH_Value32NBO b2)
 Compute the MIN of two bandwidth values. More...
 
struct GNUNET_BANDWIDTH_Value32NBO GNUNET_BANDWIDTH_value_max (struct GNUNET_BANDWIDTH_Value32NBO b1, struct GNUNET_BANDWIDTH_Value32NBO b2)
 Compute the MAX of two bandwidth values. More...
 
struct GNUNET_BANDWIDTH_Value32NBO GNUNET_BANDWIDTH_value_sum (struct GNUNET_BANDWIDTH_Value32NBO b1, struct GNUNET_BANDWIDTH_Value32NBO b2)
 Compute the SUM of two bandwidth values. More...
 
uint64_t GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO bps, struct GNUNET_TIME_Relative deadline)
 At the given bandwidth, calculate how much traffic will be available until the given deadline. More...
 
struct GNUNET_TIME_Relative GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps, uint64_t size)
 At the given bandwidth, calculate how long it would take for size bytes to be transmitted. More...
 
static void excess_trigger (void *cls)
 Task run whenever we hit the bandwidth limit for a tracker. More...
 
static void update_excess (struct GNUNET_BANDWIDTH_Tracker *av)
 Recalculate when we might need to call the excess callback. More...
 
void GNUNET_BANDWIDTH_tracker_init2 (struct GNUNET_BANDWIDTH_Tracker *av, GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb, void *update_cb_cls, struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit, uint32_t max_carry_s, GNUNET_BANDWIDTH_ExcessNotificationCallback excess_cb, void *excess_cb_cls)
 Initialize bandwidth tracker. More...
 
void GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av, GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb, void *update_cb_cls, struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit, uint32_t max_carry_s)
 Initialize bandwidth tracker. More...
 
void GNUNET_BANDWIDTH_tracker_notification_stop (struct GNUNET_BANDWIDTH_Tracker *av)
 Stop notifying about tracker updates and excess notifications. More...
 
static void update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
 Update the tracker, looking at the current time and bandwidth consumption data. More...
 
int GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av, ssize_t size)
 Notify the tracker that a certain number of bytes of bandwidth have been consumed. More...
 
struct GNUNET_TIME_Relative GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av, size_t size)
 Compute how long we should wait until consuming 'size' bytes of bandwidth in order to stay within the given quota. More...
 
int64_t GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker *av)
 Compute how many bytes are available for consumption right now. More...
 
void GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av, struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit)
 Update quota of bandwidth tracker. More...
 

Detailed Description

functions related to bandwidth (unit)

Author
Christian Grothoff

Definition in file bandwidth.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "util-bandwidth", __VA_ARGS__)

Function Documentation

◆ excess_trigger()

static void excess_trigger ( void *  cls)
static

Task run whenever we hit the bandwidth limit for a tracker.

Parameters
clsthe struct GNUNET_BANDWIDTH_Tracker

Definition at line 157 of file bandwidth.c.

References GNUNET_BANDWIDTH_Tracker::excess_cb, GNUNET_BANDWIDTH_Tracker::excess_cb_cls, GNUNET_BANDWIDTH_Tracker::excess_task, GNUNET_ERROR_TYPE_DEBUG, and GNUNET_log.

Referenced by update_excess().

158 {
159  struct GNUNET_BANDWIDTH_Tracker *av = cls;
160 
161  av->excess_task = NULL;
162  if (NULL != av->excess_cb)
163  {
165  "Notifying application about excess bandwidth\n");
166  av->excess_cb (av->excess_cb_cls);
167  }
168 }
struct GNUNET_SCHEDULER_Task * excess_task
Task scheduled to call the excess_cb once we have reached the maximum bandwidth the tracker can hold...
Struct to track available bandwidth.
GNUNET_BANDWIDTH_ExcessNotificationCallback excess_cb
Function we call if the tracker is about to throw away bandwidth due to excess (max carry exceeded)...
void * excess_cb_cls
Closure for excess_cb.
#define GNUNET_log(kind,...)
Here is the caller graph for this function:

◆ update_excess()

static void update_excess ( struct GNUNET_BANDWIDTH_Tracker av)
static

Recalculate when we might need to call the excess callback.

Definition at line 175 of file bandwidth.c.

References GNUNET_TIME_Absolute::abs_value_us, GNUNET_BANDWIDTH_Tracker::available_bytes_per_s__, GNUNET_BANDWIDTH_Tracker::consumption_since_last_update__, GNUNET_BANDWIDTH_Tracker::excess_cb, GNUNET_BANDWIDTH_Tracker::excess_task, excess_trigger(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_MAX_MESSAGE_SIZE, GNUNET_NO, GNUNET_SCHEDULER_add_delayed(), GNUNET_SCHEDULER_cancel(), GNUNET_STRINGS_relative_time_to_string(), GNUNET_TIME_absolute_get(), GNUNET_TIME_relative_saturating_multiply(), GNUNET_TIME_UNIT_SECONDS, GNUNET_TIME_UNIT_ZERO, GNUNET_BANDWIDTH_Tracker::last_update__, and GNUNET_BANDWIDTH_Tracker::max_carry_s__.

Referenced by GNUNET_BANDWIDTH_tracker_consume(), GNUNET_BANDWIDTH_tracker_init2(), and GNUNET_BANDWIDTH_tracker_update_quota().

176 {
178  struct GNUNET_TIME_Absolute now;
179  uint64_t delta_time;
180  uint64_t delta_avail;
181  int64_t left_bytes;
182  uint64_t max_carry;
183  int64_t current_consumption;
184 
185  if (NULL == av->excess_cb)
186  return; /* nothing to do */
187  now = GNUNET_TIME_absolute_get ();
188  delta_time = now.abs_value_us - av->last_update__.abs_value_us;
189  delta_avail =
190  (delta_time * ((unsigned long long) av->available_bytes_per_s__)
191  + 500000LL)
192  / 1000000LL;
193  current_consumption = av->consumption_since_last_update__ - delta_avail;
194  if (current_consumption > av->consumption_since_last_update__)
195  {
196  /* integer underflow, cap! */
197  current_consumption = INT64_MIN;
198  }
199  /* negative current_consumption means that we have savings */
200  max_carry = ((uint64_t) av->available_bytes_per_s__) * av->max_carry_s__;
201  if (max_carry < GNUNET_MAX_MESSAGE_SIZE)
202  max_carry = GNUNET_MAX_MESSAGE_SIZE;
203  if (max_carry > INT64_MAX)
204  max_carry = INT64_MAX;
205  left_bytes = current_consumption + max_carry;
206  if (left_bytes < current_consumption)
207  {
208  /* integer overflow, cap! */
209  left_bytes = INT64_MAX;
210  }
211  /* left_bytes now contains the number of bytes needed until
212  we have more savings than allowed */
213  if (left_bytes < 0)
214  {
215  /* having excess already */
217  }
218  else
219  {
220  double factor = 1.0 * left_bytes / (double) av->available_bytes_per_s__;
221  delay =
223  (unsigned long long) factor);
224  }
225  GNUNET_log (
227  "At %llu bps it will take us %s for %lld bytes to reach excess threshold\n",
228  (unsigned long long) av->available_bytes_per_s__,
230  (long long) left_bytes);
231  if (NULL != av->excess_task)
234 }
#define GNUNET_TIME_UNIT_SECONDS
One second.
struct GNUNET_SCHEDULER_Task * excess_task
Task scheduled to call the excess_cb once we have reached the maximum bandwidth the tracker can hold...
uint32_t max_carry_s__
Maximum number of seconds over which bandwidth may "accumulate".
GNUNET_BANDWIDTH_ExcessNotificationCallback excess_cb
Function we call if the tracker is about to throw away bandwidth due to excess (max carry exceeded)...
uint64_t abs_value_us
The actual value.
struct GNUNET_TIME_Absolute last_update__
Time when we last updated the tracker.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1269
int64_t consumption_since_last_update__
Number of bytes consumed since we last updated the tracker.
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:701
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
static void excess_trigger(void *cls)
Task run whenever we hit the bandwidth limit for a tracker.
Definition: bandwidth.c:157
static struct GNUNET_TIME_Relative delay
When should dkg communication start?
#define GNUNET_TIME_UNIT_ZERO
Relative time zero.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message...
uint32_t available_bytes_per_s__
Bandwidth limit to enforce in bytes per second.
#define GNUNET_log(kind,...)
Time for absolute times used by GNUnet, in microseconds.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_saturating_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Saturating multiply relative time by a given factor.
Definition: time.c:501
Time for relative time used by GNUnet, in microseconds.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
Here is the call graph for this function:
Here is the caller graph for this function:

◆ update_tracker()

static void update_tracker ( struct GNUNET_BANDWIDTH_Tracker av)
static

Update the tracker, looking at the current time and bandwidth consumption data.

Parameters
avtracker to update

Definition at line 344 of file bandwidth.c.

References GNUNET_TIME_Absolute::abs_value_us, GNUNET_BANDWIDTH_Tracker::available_bytes_per_s__, GNUNET_BANDWIDTH_Tracker::consumption_since_last_update__, GNUNET_ERROR_TYPE_DEBUG, GNUNET_MAX_MESSAGE_SIZE, GNUNET_STRINGS_relative_time_to_string(), GNUNET_TIME_absolute_get(), GNUNET_YES, GNUNET_BANDWIDTH_Tracker::last_update__, LOG, GNUNET_BANDWIDTH_Tracker::max_carry_s__, and GNUNET_TIME_Relative::rel_value_us.

Referenced by GNUNET_BANDWIDTH_tracker_consume(), GNUNET_BANDWIDTH_tracker_get_available(), GNUNET_BANDWIDTH_tracker_get_delay(), and GNUNET_BANDWIDTH_tracker_update_quota().

345 {
346  struct GNUNET_TIME_Absolute now;
347  uint64_t delta_time;
348  uint64_t delta_avail;
349  uint64_t left_bytes;
350  uint64_t max_carry;
351 
352  now = GNUNET_TIME_absolute_get ();
353  delta_time = now.abs_value_us - av->last_update__.abs_value_us;
354  delta_avail =
355  (delta_time * ((unsigned long long) av->available_bytes_per_s__)
356  + 500000LL)
357  / 1000000LL;
358  av->consumption_since_last_update__ -= delta_avail;
359  av->last_update__ = now;
361  {
362  left_bytes = -av->consumption_since_last_update__;
363  max_carry =
364  ((unsigned long long) av->available_bytes_per_s__) * av->max_carry_s__;
365  if (max_carry < GNUNET_MAX_MESSAGE_SIZE)
366  max_carry = GNUNET_MAX_MESSAGE_SIZE;
367  if (max_carry > INT64_MAX)
368  max_carry = INT64_MAX;
369  if (max_carry > left_bytes)
370  av->consumption_since_last_update__ = -left_bytes;
371  else
372  av->consumption_since_last_update__ = -max_carry;
373  }
374 #if ! defined(GNUNET_CULL_LOGGING)
375  {
377 
378  delta.rel_value_us = delta_time;
380  "Tracker %p updated, consumption at %lld at %u Bps, last update was %s ago\n",
381  av,
382  (long long) av->consumption_since_last_update__,
383  (unsigned int) av->available_bytes_per_s__,
385  }
386 #endif
387 }
uint64_t rel_value_us
The actual value.
static struct GNUNET_TIME_Relative delta
Definition: speedup.c:35
uint32_t max_carry_s__
Maximum number of seconds over which bandwidth may "accumulate".
uint64_t abs_value_us
The actual value.
struct GNUNET_TIME_Absolute last_update__
Time when we last updated the tracker.
int64_t consumption_since_last_update__
Number of bytes consumed since we last updated the tracker.
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:701
#define LOG(kind,...)
Definition: bandwidth.c:30
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message...
uint32_t available_bytes_per_s__
Bandwidth limit to enforce in bytes per second.
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.
Here is the call graph for this function:
Here is the caller graph for this function: