31#define LOG(kind, ...) GNUNET_log_from (kind, "util-bandwidth", __VA_ARGS__)
44 ret.value__ = htonl (bytes_per_second);
61 GNUNET_MIN (ntohl (b1.value__), ntohl (b2.value__)));
77 GNUNET_MAX (ntohl (b1.value__), ntohl (b2.value__)));
105 "Bandwidth has %llu bytes available until deadline in %s\n",
106 (
unsigned long long) ((b * deadline.
rel_value_us + 500000LL)
109 return (b * deadline.
rel_value_us + 500000LL) / 1000000LL;
128 b = ntohl (bps.value__);
132 "Bandwidth suggests delay of infinity (zero bandwidth)\n");
135 ret.rel_value_us =
size * 1000LL * 1000LL / b;
137 "Bandwidth suggests delay of %s for %llu bytes of traffic\n",
139 (
unsigned long long)
size);
158 "Notifying application about excess bandwidth\n");
173 uint64_t delta_avail;
176 int64_t current_consumption;
190 current_consumption = INT64_MIN;
196 if (max_carry > INT64_MAX)
197 max_carry = INT64_MAX;
198 left_bytes = current_consumption + max_carry;
199 if (left_bytes < current_consumption)
202 left_bytes = INT64_MAX;
216 (
unsigned long long) factor);
220 "At %llu bps it will take us %s for %lld bytes to reach excess threshold\n",
223 (
long long) left_bytes);
257 uint32_t max_carry_s,
270 "Tracker %p initialized with %u Bps and max carry %u\n",
273 (
unsigned int) max_carry_s);
284 uint32_t max_carry_s)
289 bytes_per_second_limit,
325 uint64_t delta_avail;
344 if (max_carry > INT64_MAX)
345 max_carry = INT64_MAX;
346 if (max_carry > left_bytes)
351#if ! defined(GNUNET_CULL_LOGGING)
357 "Tracker %p updated, consumption at %lld at %u Bps, last update was %s ago\n",
374 "Tracker %p consumes %d bytes\n",
380 if (nc < av->consumption_since_last_update__)
392 "Tracker %p consumption %llu bytes above limit\n",
428 int64_t bytes_needed;
430 if (0 == av->available_bytes_per_s__)
436 bytes_needed =
size + av->consumption_since_last_update__;
437 if (bytes_needed <= 0)
440 "Tracker %p delay for %u bytes is zero\n",
442 (
unsigned int)
size);
445 ret.rel_value_us = (1000LL * 1000LL * bytes_needed)
446 / (
unsigned long long) av->available_bytes_per_s__;
448 "Tracker %p delay for %u bytes is %s\n",
478 "Tracker %p available bandwidth is %lld bytes\n",
480 (
long long) (int64_t) (avail - used));
481 return (int64_t) (avail - used);
499 new_limit = ntohl (bytes_per_second_limit.
value__);
501 "Tracker %p bandwidth changed to %u Bps\n",
503 (
unsigned int) new_limit);
509 if (old_limit > new_limit)
static void update_excess(struct GNUNET_BANDWIDTH_Tracker *av)
Recalculate when we might need to call the excess callback.
static void excess_trigger(void *cls)
Task run whenever we hit the bandwidth limit for a tracker.
static void update_tracker(struct GNUNET_BANDWIDTH_Tracker *av)
Update the tracker, looking at the current time and bandwidth consumption data.
static int ret
Final status code.
static struct GNUNET_NotificationContext * nc
Notification context for broadcasting to monitors.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message,...
void GNUNET_BANDWIDTH_tracker_notification_stop(struct GNUNET_BANDWIDTH_Tracker *av)
Stop notifying about tracker updates and excess notifications.
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.
void GNUNET_BANDWIDTH_tracker_update_quota(struct GNUNET_BANDWIDTH_Tracker *av, struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit)
Update quota of bandwidth tracker.
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.
void(* GNUNET_BANDWIDTH_ExcessNotificationCallback)(void *cls)
Callback to be called by the bandwidth tracker if the tracker was updated and the client should updat...
struct GNUNET_BANDWIDTH_Value32NBO GNUNET_BANDWIDTH_value_init(uint32_t bytes_per_second)
Create a new bandwidth value.
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.
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.
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.
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.
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.
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.
void(* GNUNET_BANDWIDTH_TrackerUpdateCallback)(void *cls)
Callback to be called by the bandwidth tracker if the tracker was updated and the client should updat...
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...
int64_t GNUNET_BANDWIDTH_tracker_get_available(struct GNUNET_BANDWIDTH_Tracker *av)
Compute how many bytes are available for consumption right now.
#define GNUNET_log(kind,...)
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_DEBUG
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
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.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence)
Get the duration of an operation as the difference of the current time and the given start time "henc...
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.
#define GNUNET_TIME_UNIT_SECONDS
One second.
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
#define GNUNET_TIME_UNIT_ZERO
Relative time zero.
static unsigned int size
Size of the "table".
static struct GNUNET_TIME_Relative delta
Struct to track available bandwidth.
struct GNUNET_SCHEDULER_Task * excess_task
Task scheduled to call the excess_cb once we have reached the maximum bandwidth the tracker can hold.
int64_t consumption_since_last_update__
Number of bytes consumed since we last updated the tracker.
GNUNET_BANDWIDTH_ExcessNotificationCallback excess_cb
Function we call if the tracker is about to throw away bandwidth due to excess (max carry exceeded).
void * update_cb_cls
Closure for update_cb.
struct GNUNET_TIME_Absolute last_update__
Time when we last updated the tracker.
void * excess_cb_cls
Closure for excess_cb.
GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb
Function we call if the tracker's bandwidth is increased and a previously returned timeout might now ...
uint32_t available_bytes_per_s__
Bandwidth limit to enforce in bytes per second.
uint32_t max_carry_s__
Maximum number of seconds over which bandwidth may "accumulate".
32-bit bandwidth used for network exchange by GNUnet, in bytes per second.
uint32_t value__
The actual value (bytes per second).
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.