2 This file is part of GNUnet.
3 (C) 2010, 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/bandwidth.c
23 * @brief functions related to bandwidth (unit)
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "util-bandwidth", __VA_ARGS__)
33 * Create a new bandwidth value.
35 * @param bytes_per_second value to create
36 * @return the new bandwidth value
38 struct GNUNET_BANDWIDTH_Value32NBO
39 GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second)
41 struct GNUNET_BANDWIDTH_Value32NBO ret;
43 LOG (GNUNET_ERROR_TYPE_DEBUG,
44 "Initializing bandwidth of %u Bps\n",
45 (unsigned int) bytes_per_second);
46 ret.value__ = htonl (bytes_per_second);
52 * Compute the MIN of two bandwidth values.
54 * @param b1 first value
55 * @param b2 second value
56 * @return the min of b1 and b2
58 struct GNUNET_BANDWIDTH_Value32NBO
59 GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
60 struct GNUNET_BANDWIDTH_Value32NBO b2)
63 GNUNET_BANDWIDTH_value_init (GNUNET_MIN
64 (ntohl (b1.value__), ntohl (b2.value__)));
69 * At the given bandwidth, calculate how much traffic will be
70 * available until the given deadline.
72 * @param bps bandwidth
73 * @param deadline when is the deadline
74 * @return number of bytes available at bps until deadline
77 GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO bps,
78 struct GNUNET_TIME_Relative deadline)
82 b = ntohl (bps.value__);
83 LOG (GNUNET_ERROR_TYPE_DEBUG,
84 "Bandwidth has %llu bytes available until deadline in %s\n",
85 (unsigned long long) ((b * deadline.rel_value_us + 500000LL) / 1000000LL),
86 GNUNET_STRINGS_relative_time_to_string (deadline, GNUNET_YES));
87 return (b * deadline.rel_value_us + 500000LL) / 1000000LL;
92 * At the given bandwidth, calculate how long it would take for
93 * @a size bytes to be transmitted.
95 * @param bps bandwidth
96 * @param size number of bytes we want to have available
97 * @return how long it would take
99 struct GNUNET_TIME_Relative
100 GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
104 struct GNUNET_TIME_Relative ret;
106 b = ntohl (bps.value__);
109 LOG (GNUNET_ERROR_TYPE_DEBUG,
110 "Bandwidth suggests delay of infinity (zero bandwidth)\n");
111 return GNUNET_TIME_UNIT_FOREVER_REL;
113 ret.rel_value_us = size * 1000LL * 1000LL / b;
114 LOG (GNUNET_ERROR_TYPE_DEBUG,
115 "Bandwidth suggests delay of %s for %llu bytes of traffic\n",
116 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES),
117 (unsigned long long) size);
123 * Task run whenever we hit the bandwidth limit for a tracker.
125 * @param cls the `struct GNUNET_BANDWIDTH_Tracker`
126 * @param tc scheduler context
129 excess_trigger (void *cls,
130 const struct GNUNET_SCHEDULER_TaskContext *tc)
132 struct GNUNET_BANDWIDTH_Tracker *av = cls;
134 av->excess_task = GNUNET_SCHEDULER_NO_TASK;
136 if (NULL != av->excess_cb)
137 av->excess_cb (av->excess_cb_cls);
142 * Recalculate when we might need to call the excess callback.
145 update_excess (struct GNUNET_BANDWIDTH_Tracker *av)
147 struct GNUNET_TIME_Relative delay;
148 struct GNUNET_TIME_Absolute now;
150 uint64_t delta_avail;
153 int64_t current_consumption;
155 if (NULL == av->excess_cb)
156 return; /* nothing to do */
157 now = GNUNET_TIME_absolute_get ();
158 delta_time = now.abs_value_us - av->last_update__.abs_value_us;
160 (delta_time * ((unsigned long long) av->available_bytes_per_s__) +
161 500000LL) / 1000000LL;
162 current_consumption = av->consumption_since_last_update__ - delta_avail;
163 /* negative current_consumption means that we have savings */
164 max_carry = av->available_bytes_per_s__ * av->max_carry_s__;
165 if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE)
166 max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE;
167 left_bytes = max_carry + current_consumption;
168 /* left_bytes now contains the number of bytes needed until
169 we have more savings than allowed */
172 /* having excess already */
173 delay = GNUNET_TIME_UNIT_ZERO;
177 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
179 delay = GNUNET_TIME_relative_divide (delay,
180 av->available_bytes_per_s__);
182 if (GNUNET_SCHEDULER_NO_TASK != av->excess_task)
183 GNUNET_SCHEDULER_cancel (av->excess_task);
184 av->excess_task = GNUNET_SCHEDULER_add_delayed (delay,
191 * Initialize bandwidth tracker. Note that in addition to the
192 * 'max_carry_s' limit, we also always allow at least
193 * #GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate. So if the
194 * bytes-per-second limit is so small that within 'max_carry_s' not
195 * even #GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
196 * ignored and replaced by #GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
199 * To stop notifications about updates and excess callbacks use
200 * #GNUNET_BANDWIDTH_tracker_notification_stop
202 * @param av tracker to initialize
203 * @param update_cb callback to notify a client about the tracker being updated
204 * @param update_cb_cls cls for the callback
205 * @param bytes_per_second_limit initial limit to assume
206 * @param max_carry_s maximum number of seconds unused bandwidth
207 * may accumulate before it expires
208 * @param excess_cb callback to notify if we have excess bandwidth
209 * @param excess_cb_cls closure for @a excess_cb
212 GNUNET_BANDWIDTH_tracker_init2 (struct GNUNET_BANDWIDTH_Tracker *av,
213 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb,
215 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit,
216 uint32_t max_carry_s,
217 GNUNET_BANDWIDTH_ExcessNotificationCallback excess_cb,
220 av->update_cb = update_cb;
221 av->update_cb_cls = update_cb_cls;
222 av->consumption_since_last_update__ = 0;
223 av->last_update__ = GNUNET_TIME_absolute_get ();
224 av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__);
225 av->max_carry_s__ = max_carry_s;
226 av->excess_cb = excess_cb;
227 av->excess_cb_cls = excess_cb_cls;
228 LOG (GNUNET_ERROR_TYPE_DEBUG,
229 "Tracker %p initialized with %u Bps and max carry %u\n",
231 (unsigned int) av->available_bytes_per_s__,
232 (unsigned int) max_carry_s);
238 * Initialize bandwidth tracker. Note that in addition to the
239 * 'max_carry_s' limit, we also always allow at least
240 * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate. So if the
241 * bytes-per-second limit is so small that within 'max_carry_s' not
242 * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
243 * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
246 * @param av tracker to initialize
247 * @param update_cb callback to notify a client about the tracker being updated
248 * @param update_cb_cls cls for the callback
249 * @param bytes_per_second_limit initial limit to assume
250 * @param max_carry_s maximum number of seconds unused bandwidth
251 * may accumulate before it expires
254 GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
255 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb,
257 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit,
258 uint32_t max_carry_s)
260 GNUNET_BANDWIDTH_tracker_init2 (av, update_cb,
262 bytes_per_second_limit,
269 * Stop notifying about tracker updates and excess notifications
271 * @param av the respective trackers
274 GNUNET_BANDWIDTH_tracker_notification_stop (struct GNUNET_BANDWIDTH_Tracker *av)
276 if (GNUNET_SCHEDULER_NO_TASK != av->excess_task)
277 GNUNET_SCHEDULER_cancel (av->excess_task);
278 av->excess_task = GNUNET_SCHEDULER_NO_TASK;
279 av->excess_cb = NULL;
280 av->excess_cb_cls = NULL;
281 av->update_cb = NULL;
282 av->update_cb_cls = NULL;
288 * Update the tracker, looking at the current time and
289 * bandwidth consumption data.
291 * @param av tracker to update
294 update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
296 struct GNUNET_TIME_Absolute now;
297 struct GNUNET_TIME_Relative delta;
299 uint64_t delta_avail;
303 now = GNUNET_TIME_absolute_get ();
304 delta_time = now.abs_value_us - av->last_update__.abs_value_us;
306 (delta_time * ((unsigned long long) av->available_bytes_per_s__) +
307 500000LL) / 1000000LL;
308 av->consumption_since_last_update__ -= delta_avail;
309 av->last_update__ = now;
310 if (av->consumption_since_last_update__ < 0)
312 left_bytes = -av->consumption_since_last_update__;
313 max_carry = av->available_bytes_per_s__ * av->max_carry_s__;
314 if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE)
315 max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE;
316 if (max_carry > left_bytes)
317 av->consumption_since_last_update__ = -left_bytes;
319 av->consumption_since_last_update__ = -max_carry;
321 delta.rel_value_us = delta_time;
322 LOG (GNUNET_ERROR_TYPE_DEBUG,
323 "Tracker %p updated, have %u Bps, last update was %s ago\n", av,
324 (unsigned int) av->available_bytes_per_s__,
325 GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_YES));
330 * Notify the tracker that a certain number of bytes of bandwidth have
331 * been consumed. Note that it is legal to consume bytes even if not
332 * enough bandwidth is available (in that case,
333 * #GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
334 * even for a size of zero for a while).
336 * @param av tracker to update
337 * @param size number of bytes consumed
338 * @return #GNUNET_YES if this consumption is above the limit
341 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
346 LOG (GNUNET_ERROR_TYPE_DEBUG,
347 "Tracker %p consumes %d bytes\n",
352 nc = av->consumption_since_last_update__ + size;
353 if (nc < av->consumption_since_last_update__)
356 return GNUNET_SYSERR;
358 av->consumption_since_last_update__ = nc;
361 if (av->consumption_since_last_update__ > 0)
363 LOG (GNUNET_ERROR_TYPE_DEBUG,
364 "Tracker %p consumption %llu bytes above limit\n", av,
365 (unsigned long long) av->consumption_since_last_update__);
371 av->consumption_since_last_update__ += size;
379 * Compute how long we should wait until consuming 'size'
380 * bytes of bandwidth in order to stay within the given
383 * @param av tracker to query
384 * @param size number of bytes we would like to consume
385 * @return time in ms to wait for consumption to be OK
387 struct GNUNET_TIME_Relative
388 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
391 struct GNUNET_TIME_Relative ret;
392 int64_t bytes_needed;
394 if (0 == av->available_bytes_per_s__)
396 LOG (GNUNET_ERROR_TYPE_DEBUG,
397 "Tracker %p delay is infinity\n", av);
398 return GNUNET_TIME_UNIT_FOREVER_REL;
401 bytes_needed = size + av->consumption_since_last_update__;
402 if (bytes_needed <= 0)
404 LOG (GNUNET_ERROR_TYPE_DEBUG,
405 "Tracker %p delay for %u bytes is zero\n", av,
406 (unsigned int) size);
407 return GNUNET_TIME_UNIT_ZERO;
410 (1000LL * 1000LL * bytes_needed) /
411 (unsigned long long) av->available_bytes_per_s__;
412 LOG (GNUNET_ERROR_TYPE_DEBUG,
413 "Tracker %p delay for %u bytes is %s\n",
414 av, (unsigned int) size,
415 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
421 * Compute how many bytes are available for consumption right now.
424 * @param av tracker to query
425 * @return number of bytes available for consumption right now
428 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker *av)
430 struct GNUNET_BANDWIDTH_Value32NBO bps;
435 bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__);
437 GNUNET_BANDWIDTH_value_get_available_until (bps,
438 GNUNET_TIME_absolute_get_duration
439 (av->last_update__));
440 used = av->consumption_since_last_update__;
441 LOG (GNUNET_ERROR_TYPE_DEBUG,
442 "Tracker %p available bandwidth is %lld bytes\n", av,
443 (long long) (int64_t) (avail - used));
444 return (int64_t) (avail - used);
449 * Update quota of bandwidth tracker.
451 * @param av tracker to initialize
452 * @param bytes_per_second_limit new limit to assume
455 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
456 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit)
461 new_limit = ntohl (bytes_per_second_limit.value__);
462 LOG (GNUNET_ERROR_TYPE_DEBUG,
463 "Tracker %p bandwidth changed to %u Bps\n", av,
464 (unsigned int) new_limit);
466 old_limit = av->available_bytes_per_s__;
467 av->available_bytes_per_s__ = new_limit;
468 if (NULL != av->update_cb)
469 av->update_cb (av->update_cb_cls);
470 if (old_limit > new_limit)
471 update_tracker (av); /* maximum excess might be less now */
476 /* end of bandwidth.c */