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, "Initializing bandwidth of %u Bps\n",
44 (unsigned int) bytes_per_second);
45 ret.value__ = htonl (bytes_per_second);
51 * Compute the MIN of two bandwidth values.
53 * @param b1 first value
54 * @param b2 second value
55 * @return the min of b1 and b2
57 struct GNUNET_BANDWIDTH_Value32NBO
58 GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
59 struct GNUNET_BANDWIDTH_Value32NBO b2)
62 GNUNET_BANDWIDTH_value_init (GNUNET_MIN
63 (ntohl (b1.value__), ntohl (b2.value__)));
68 * At the given bandwidth, calculate how much traffic will be
69 * available until the given deadline.
71 * @param bps bandwidth
72 * @param deadline when is the deadline
73 * @return number of bytes available at bps until deadline
76 GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO
78 struct GNUNET_TIME_Relative
83 b = ntohl (bps.value__);
84 LOG (GNUNET_ERROR_TYPE_DEBUG,
85 "Bandwidth has %llu bytes available until deadline in %s\n",
86 (unsigned long long) ((b * deadline.rel_value_us + 500000LL) / 1000000LL),
87 GNUNET_STRINGS_relative_time_to_string (deadline, GNUNET_YES));
88 return (b * deadline.rel_value_us + 500000LL) / 1000000LL;
93 * At the given bandwidth, calculate how long it would take for
94 * 'size' bytes to be transmitted.
96 * @param bps bandwidth
97 * @param size number of bytes we want to have available
98 * @return how long it would take
100 struct GNUNET_TIME_Relative
101 GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
105 struct GNUNET_TIME_Relative ret;
107 b = ntohl (bps.value__);
110 LOG (GNUNET_ERROR_TYPE_DEBUG,
111 "Bandwidth suggests delay of infinity (zero bandwidth)\n");
112 return GNUNET_TIME_UNIT_FOREVER_REL;
114 ret.rel_value_us = size * 1000LL * 1000LL / b;
115 LOG (GNUNET_ERROR_TYPE_DEBUG,
116 "Bandwidth suggests delay of %s for %llu bytes of traffic\n",
117 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES),
118 (unsigned long long) size);
125 * Initialize bandwidth tracker. Note that in addition to the
126 * 'max_carry_s' limit, we also always allow at least
127 * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate. So if the
128 * bytes-per-second limit is so small that within 'max_carry_s' not
129 * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
130 * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
133 * @param av tracker to initialize
134 * @param update_cb callback to notify a client about the tracker being updated
135 * @param update_cb_cls cls for the callback
136 * @param bytes_per_second_limit initial limit to assume
137 * @param max_carry_s maximum number of seconds unused bandwidth
138 * may accumulate before it expires
141 GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
142 GNUNET_BANDWIDTH_tracker_update_cb update_cb,
144 struct GNUNET_BANDWIDTH_Value32NBO
145 bytes_per_second_limit, uint32_t max_carry_s)
147 av->update_cb = update_cb;
148 av->update_cb_cls = update_cb_cls;
149 av->consumption_since_last_update__ = 0;
150 av->last_update__ = GNUNET_TIME_absolute_get ();
151 av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__);
152 av->max_carry_s__ = max_carry_s;
153 LOG (GNUNET_ERROR_TYPE_DEBUG,
154 "Tracker %p initialized with %u Bps and max carry %u\n", av,
155 (unsigned int) av->available_bytes_per_s__, (unsigned int) max_carry_s);
160 * Update the tracker, looking at the current time and
161 * bandwidth consumption data.
163 * @param av tracker to update
166 update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
168 struct GNUNET_TIME_Absolute now;
169 struct GNUNET_TIME_Relative delta;
171 uint64_t delta_avail;
175 now = GNUNET_TIME_absolute_get ();
176 delta_time = now.abs_value_us - av->last_update__.abs_value_us;
178 (delta_time * ((unsigned long long) av->available_bytes_per_s__) +
179 500000LL) / 1000000LL;
180 av->consumption_since_last_update__ -= delta_avail;
181 av->last_update__ = now;
182 if (av->consumption_since_last_update__ < 0)
184 left_bytes = -av->consumption_since_last_update__;
185 max_carry = av->available_bytes_per_s__ * av->max_carry_s__;
186 if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE)
187 max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE;
188 if (max_carry > left_bytes)
189 av->consumption_since_last_update__ = -left_bytes;
191 av->consumption_since_last_update__ = -max_carry;
193 delta.rel_value_us = delta_time;
194 LOG (GNUNET_ERROR_TYPE_DEBUG,
195 "Tracker %p updated, have %u Bps, last update was %s ago\n", av,
196 (unsigned int) av->available_bytes_per_s__,
197 GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_YES));
202 * Notify the tracker that a certain number of bytes of bandwidth have
203 * been consumed. Note that it is legal to consume bytes even if not
204 * enough bandwidth is available (in that case,
205 * GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
206 * even for a size of zero for a while).
208 * @param av tracker to update
209 * @param size number of bytes consumed
210 * @return GNUNET_YES if this consumption is above the limit
213 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
218 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p consumes %d bytes\n", av,
222 nc = av->consumption_since_last_update__ + size;
223 if (nc < av->consumption_since_last_update__)
226 return GNUNET_SYSERR;
228 av->consumption_since_last_update__ = nc;
230 if (av->consumption_since_last_update__ > 0)
232 LOG (GNUNET_ERROR_TYPE_DEBUG,
233 "Tracker %p consumption %llu bytes above limit\n", av,
234 (unsigned long long) av->consumption_since_last_update__);
240 av->consumption_since_last_update__ += size;
247 * Compute how long we should wait until consuming 'size'
248 * bytes of bandwidth in order to stay within the given
251 * @param av tracker to query
252 * @param size number of bytes we would like to consume
253 * @return time in ms to wait for consumption to be OK
255 struct GNUNET_TIME_Relative
256 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
259 struct GNUNET_TIME_Relative ret;
260 int64_t bytes_needed;
262 if (av->available_bytes_per_s__ == 0)
264 LOG (GNUNET_ERROR_TYPE_DEBUG,
265 "Tracker %p delay is infinity\n", av);
266 return GNUNET_TIME_UNIT_FOREVER_REL;
269 bytes_needed = size + av->consumption_since_last_update__;
270 if (bytes_needed <= 0)
272 LOG (GNUNET_ERROR_TYPE_DEBUG,
273 "Tracker %p delay for %u bytes is zero\n", av,
274 (unsigned int) size);
275 return GNUNET_TIME_UNIT_ZERO;
278 (1000LL * 1000LL * bytes_needed) /
279 (unsigned long long) av->available_bytes_per_s__;
280 LOG (GNUNET_ERROR_TYPE_DEBUG,
281 "Tracker %p delay for %u bytes is %s\n",
282 av, (unsigned int) size,
283 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
289 * Compute how many bytes are available for consumption right now.
292 * @param av tracker to query
293 * @return number of bytes available for consumption right now
296 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker * av)
298 struct GNUNET_BANDWIDTH_Value32NBO bps;
303 bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__);
305 GNUNET_BANDWIDTH_value_get_available_until (bps,
306 GNUNET_TIME_absolute_get_duration
307 (av->last_update__));
308 used = av->consumption_since_last_update__;
309 LOG (GNUNET_ERROR_TYPE_DEBUG,
310 "Tracker %p available bandwidth is %lld bytes\n", av,
311 (long long) (int64_t) (avail - used));
312 return (int64_t) (avail - used);
317 * Update quota of bandwidth tracker.
319 * @param av tracker to initialize
320 * @param bytes_per_second_limit new limit to assume
323 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
324 struct GNUNET_BANDWIDTH_Value32NBO
325 bytes_per_second_limit)
330 new_limit = ntohl (bytes_per_second_limit.value__);
331 LOG (GNUNET_ERROR_TYPE_DEBUG,
332 "Tracker %p bandwidth changed to %u Bps\n", av,
333 (unsigned int) new_limit);
335 old_limit = av->available_bytes_per_s__;
336 av->available_bytes_per_s__ = new_limit;
337 if (NULL != av->update_cb)
338 av->update_cb (av->update_cb_cls);
339 if (old_limit > new_limit)
340 update_tracker (av); /* maximum excess might be less now */
344 /* end of bandwidth.c */