2 This file is part of GNUnet.
3 Copyright (C) 2010, 2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 ret.value__ = htonl (bytes_per_second);
49 * Compute the MIN of two bandwidth values.
51 * @param b1 first value
52 * @param b2 second value
53 * @return the min of b1 and b2
55 struct GNUNET_BANDWIDTH_Value32NBO
56 GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
57 struct GNUNET_BANDWIDTH_Value32NBO b2)
60 GNUNET_BANDWIDTH_value_init (GNUNET_MIN
67 * Compute the MAX of two bandwidth values.
69 * @param b1 first value
70 * @param b2 second value
71 * @return the min of b1 and b2
73 struct GNUNET_BANDWIDTH_Value32NBO
74 GNUNET_BANDWIDTH_value_max (struct GNUNET_BANDWIDTH_Value32NBO b1,
75 struct GNUNET_BANDWIDTH_Value32NBO b2)
78 GNUNET_BANDWIDTH_value_init (GNUNET_MAX
85 * At the given bandwidth, calculate how much traffic will be
86 * available until the given deadline.
88 * @param bps bandwidth
89 * @param deadline when is the deadline
90 * @return number of bytes available at bps until deadline
93 GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO bps,
94 struct GNUNET_TIME_Relative deadline)
98 b = ntohl (bps.value__);
99 LOG (GNUNET_ERROR_TYPE_DEBUG,
100 "Bandwidth has %llu bytes available until deadline in %s\n",
101 (unsigned long long) ((b * deadline.rel_value_us + 500000LL) / 1000000LL),
102 GNUNET_STRINGS_relative_time_to_string (deadline, GNUNET_YES));
103 return (b * deadline.rel_value_us + 500000LL) / 1000000LL;
108 * At the given bandwidth, calculate how long it would take for
109 * @a size bytes to be transmitted.
111 * @param bps bandwidth
112 * @param size number of bytes we want to have available
113 * @return how long it would take
115 struct GNUNET_TIME_Relative
116 GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
120 struct GNUNET_TIME_Relative ret;
122 b = ntohl (bps.value__);
125 LOG (GNUNET_ERROR_TYPE_DEBUG,
126 "Bandwidth suggests delay of infinity (zero bandwidth)\n");
127 return GNUNET_TIME_UNIT_FOREVER_REL;
129 ret.rel_value_us = size * 1000LL * 1000LL / b;
130 LOG (GNUNET_ERROR_TYPE_DEBUG,
131 "Bandwidth suggests delay of %s for %llu bytes of traffic\n",
132 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES),
133 (unsigned long long) size);
139 * Task run whenever we hit the bandwidth limit for a tracker.
141 * @param cls the `struct GNUNET_BANDWIDTH_Tracker`
144 excess_trigger (void *cls)
146 struct GNUNET_BANDWIDTH_Tracker *av = cls;
148 av->excess_task = NULL;
149 if (NULL != av->excess_cb)
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152 "Notifying application about excess bandwidth\n");
153 av->excess_cb (av->excess_cb_cls);
159 * Recalculate when we might need to call the excess callback.
162 update_excess (struct GNUNET_BANDWIDTH_Tracker *av)
164 struct GNUNET_TIME_Relative delay;
165 struct GNUNET_TIME_Absolute now;
167 uint64_t delta_avail;
170 int64_t current_consumption;
172 if (NULL == av->excess_cb)
173 return; /* nothing to do */
174 now = GNUNET_TIME_absolute_get ();
175 delta_time = now.abs_value_us - av->last_update__.abs_value_us;
177 (delta_time * ((unsigned long long) av->available_bytes_per_s__) +
178 500000LL) / 1000000LL;
179 current_consumption = av->consumption_since_last_update__ - delta_avail;
180 if (current_consumption > av->consumption_since_last_update__)
182 /* integer underflow, cap! */
183 current_consumption = INT64_MIN;
185 /* negative current_consumption means that we have savings */
186 max_carry = ((uint64_t) av->available_bytes_per_s__) * av->max_carry_s__;
187 if (max_carry < GNUNET_MAX_MESSAGE_SIZE)
188 max_carry = GNUNET_MAX_MESSAGE_SIZE;
189 if (max_carry > INT64_MAX)
190 max_carry = INT64_MAX;
191 left_bytes = current_consumption + max_carry;
192 if (left_bytes < current_consumption)
194 /* integer overflow, cap! */
195 left_bytes = INT64_MAX;
197 /* left_bytes now contains the number of bytes needed until
198 we have more savings than allowed */
201 /* having excess already */
202 delay = GNUNET_TIME_UNIT_ZERO;
206 double factor = 1.0 * left_bytes / (double) av->available_bytes_per_s__;
207 delay = GNUNET_TIME_relative_saturating_multiply (GNUNET_TIME_UNIT_SECONDS,
208 (unsigned long long) factor);
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211 "At %llu bps it will take us %s for %lld bytes to reach excess threshold\n",
212 (unsigned long long) av->available_bytes_per_s__,
213 GNUNET_STRINGS_relative_time_to_string (delay,
215 (long long) left_bytes);
216 if (NULL != av->excess_task)
217 GNUNET_SCHEDULER_cancel (av->excess_task);
218 av->excess_task = GNUNET_SCHEDULER_add_delayed (delay,
225 * Initialize bandwidth tracker. Note that in addition to the
226 * 'max_carry_s' limit, we also always allow at least
227 * #GNUNET_MAX_MESSAGE_SIZE to accumulate. So if the
228 * bytes-per-second limit is so small that within 'max_carry_s' not
229 * even #GNUNET_MAX_MESSAGE_SIZE is allowed to accumulate, it is
230 * ignored and replaced by #GNUNET_MAX_MESSAGE_SIZE (which is in
233 * To stop notifications about updates and excess callbacks use
234 * #GNUNET_BANDWIDTH_tracker_notification_stop().
236 * @param av tracker to initialize
237 * @param update_cb callback to notify a client about the tracker being updated
238 * @param update_cb_cls cls for the callback
239 * @param bytes_per_second_limit initial limit to assume
240 * @param max_carry_s maximum number of seconds unused bandwidth
241 * may accumulate before it expires
242 * @param excess_cb callback to notify if we have excess bandwidth
243 * @param excess_cb_cls closure for @a excess_cb
246 GNUNET_BANDWIDTH_tracker_init2 (struct GNUNET_BANDWIDTH_Tracker *av,
247 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb,
249 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit,
250 uint32_t max_carry_s,
251 GNUNET_BANDWIDTH_ExcessNotificationCallback excess_cb,
254 av->update_cb = update_cb;
255 av->update_cb_cls = update_cb_cls;
256 av->consumption_since_last_update__ = 0;
257 av->last_update__ = GNUNET_TIME_absolute_get ();
258 av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__);
259 av->max_carry_s__ = max_carry_s;
260 av->excess_cb = excess_cb;
261 av->excess_cb_cls = excess_cb_cls;
262 LOG (GNUNET_ERROR_TYPE_DEBUG,
263 "Tracker %p initialized with %u Bps and max carry %u\n",
265 (unsigned int) av->available_bytes_per_s__,
266 (unsigned int) max_carry_s);
272 * Initialize bandwidth tracker. Note that in addition to the
273 * 'max_carry_s' limit, we also always allow at least
274 * #GNUNET_MAX_MESSAGE_SIZE to accumulate. So if the
275 * bytes-per-second limit is so small that within 'max_carry_s' not
276 * even #GNUNET_MAX_MESSAGE_SIZE is allowed to accumulate, it is
277 * ignored and replaced by #GNUNET_MAX_MESSAGE_SIZE (which is in
280 * @param av tracker to initialize
281 * @param update_cb callback to notify a client about the tracker being updated
282 * @param update_cb_cls cls for the callback
283 * @param bytes_per_second_limit initial limit to assume
284 * @param max_carry_s maximum number of seconds unused bandwidth
285 * may accumulate before it expires
288 GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
289 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb,
291 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit,
292 uint32_t max_carry_s)
294 GNUNET_BANDWIDTH_tracker_init2 (av, update_cb,
296 bytes_per_second_limit,
303 * Stop notifying about tracker updates and excess notifications
305 * @param av the respective trackers
308 GNUNET_BANDWIDTH_tracker_notification_stop (struct GNUNET_BANDWIDTH_Tracker *av)
310 if (NULL != av->excess_task)
311 GNUNET_SCHEDULER_cancel (av->excess_task);
312 av->excess_task = NULL;
313 av->excess_cb = NULL;
314 av->excess_cb_cls = NULL;
315 av->update_cb = NULL;
316 av->update_cb_cls = NULL;
321 * Update the tracker, looking at the current time and
322 * bandwidth consumption data.
324 * @param av tracker to update
327 update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
329 struct GNUNET_TIME_Absolute now;
331 uint64_t delta_avail;
335 now = GNUNET_TIME_absolute_get ();
336 delta_time = now.abs_value_us - av->last_update__.abs_value_us;
338 (delta_time * ((unsigned long long) av->available_bytes_per_s__) +
339 500000LL) / 1000000LL;
340 av->consumption_since_last_update__ -= delta_avail;
341 av->last_update__ = now;
342 if (av->consumption_since_last_update__ < 0)
344 left_bytes = - av->consumption_since_last_update__;
345 max_carry = ((unsigned long long) av->available_bytes_per_s__) *
347 if (max_carry < GNUNET_MAX_MESSAGE_SIZE)
348 max_carry = GNUNET_MAX_MESSAGE_SIZE;
349 if (max_carry > INT64_MAX)
350 max_carry = INT64_MAX;
351 if (max_carry > left_bytes)
352 av->consumption_since_last_update__ = - left_bytes;
354 av->consumption_since_last_update__ = - max_carry;
356 #if !defined(GNUNET_CULL_LOGGING)
358 struct GNUNET_TIME_Relative delta;
360 delta.rel_value_us = delta_time;
361 LOG (GNUNET_ERROR_TYPE_DEBUG,
362 "Tracker %p updated, consumption at %lld at %u Bps, last update was %s ago\n",
364 (long long) av->consumption_since_last_update__,
365 (unsigned int) av->available_bytes_per_s__,
366 GNUNET_STRINGS_relative_time_to_string (delta,
374 * Notify the tracker that a certain number of bytes of bandwidth have
375 * been consumed. Note that it is legal to consume bytes even if not
376 * enough bandwidth is available (in that case,
377 * #GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
378 * even for a size of zero for a while).
380 * @param av tracker to update
381 * @param size number of bytes consumed
382 * @return #GNUNET_YES if this consumption is above the limit
385 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
390 LOG (GNUNET_ERROR_TYPE_DEBUG,
391 "Tracker %p consumes %d bytes\n",
396 nc = av->consumption_since_last_update__ + size;
397 if (nc < av->consumption_since_last_update__)
399 /* integer overflow, very bad */
401 return GNUNET_SYSERR;
403 av->consumption_since_last_update__ = nc;
406 if (av->consumption_since_last_update__ > 0)
408 LOG (GNUNET_ERROR_TYPE_DEBUG,
409 "Tracker %p consumption %llu bytes above limit\n",
411 (unsigned long long) av->consumption_since_last_update__);
417 nc = av->consumption_since_last_update__ + size;
418 if (nc > av->consumption_since_last_update__)
420 /* integer underflow, very bad */
422 return GNUNET_SYSERR;
424 av->consumption_since_last_update__ = nc;
432 * Compute how long we should wait until consuming 'size'
433 * bytes of bandwidth in order to stay within the given
436 * @param av tracker to query
437 * @param size number of bytes we would like to consume
438 * @return time in ms to wait for consumption to be OK
440 struct GNUNET_TIME_Relative
441 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
444 struct GNUNET_TIME_Relative ret;
445 int64_t bytes_needed;
447 if (0 == av->available_bytes_per_s__)
449 LOG (GNUNET_ERROR_TYPE_DEBUG,
450 "Tracker %p delay is infinity\n", av);
451 return GNUNET_TIME_UNIT_FOREVER_REL;
454 bytes_needed = size + av->consumption_since_last_update__;
455 if (bytes_needed <= 0)
457 LOG (GNUNET_ERROR_TYPE_DEBUG,
458 "Tracker %p delay for %u bytes is zero\n", av,
459 (unsigned int) size);
460 return GNUNET_TIME_UNIT_ZERO;
463 (1000LL * 1000LL * bytes_needed) /
464 (unsigned long long) av->available_bytes_per_s__;
465 LOG (GNUNET_ERROR_TYPE_DEBUG,
466 "Tracker %p delay for %u bytes is %s\n",
469 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
475 * Compute how many bytes are available for consumption right now.
478 * @param av tracker to query
479 * @return number of bytes available for consumption right now
482 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker *av)
484 struct GNUNET_BANDWIDTH_Value32NBO bps;
489 bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__);
491 GNUNET_BANDWIDTH_value_get_available_until (bps,
492 GNUNET_TIME_absolute_get_duration
493 (av->last_update__));
494 used = av->consumption_since_last_update__;
495 LOG (GNUNET_ERROR_TYPE_DEBUG,
496 "Tracker %p available bandwidth is %lld bytes\n", av,
497 (long long) (int64_t) (avail - used));
498 return (int64_t) (avail - used);
503 * Update quota of bandwidth tracker.
505 * @param av tracker to initialize
506 * @param bytes_per_second_limit new limit to assume
509 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
510 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit)
515 new_limit = ntohl (bytes_per_second_limit.value__);
516 LOG (GNUNET_ERROR_TYPE_DEBUG,
517 "Tracker %p bandwidth changed to %u Bps\n", av,
518 (unsigned int) new_limit);
520 old_limit = av->available_bytes_per_s__;
521 av->available_bytes_per_s__ = new_limit;
522 if (NULL != av->update_cb)
523 av->update_cb (av->update_cb_cls);
524 if (old_limit > new_limit)
525 update_tracker (av); /* maximum excess might be less now */
530 /* end of bandwidth.c */