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
79 struct GNUNET_TIME_Relative
84 b = ntohl (bps.value__);
85 LOG (GNUNET_ERROR_TYPE_DEBUG,
86 "Bandwidth has %llu bytes available until deadline in %s\n",
87 (unsigned long long) ((b * deadline.rel_value_us + 500000LL) / 1000000LL),
88 GNUNET_STRINGS_relative_time_to_string (deadline, GNUNET_YES));
89 return (b * deadline.rel_value_us + 500000LL) / 1000000LL;
94 * At the given bandwidth, calculate how long it would take for
95 * @a size bytes to be transmitted.
97 * @param bps bandwidth
98 * @param size number of bytes we want to have available
99 * @return how long it would take
101 struct GNUNET_TIME_Relative
102 GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
106 struct GNUNET_TIME_Relative ret;
108 b = ntohl (bps.value__);
111 LOG (GNUNET_ERROR_TYPE_DEBUG,
112 "Bandwidth suggests delay of infinity (zero bandwidth)\n");
113 return GNUNET_TIME_UNIT_FOREVER_REL;
115 ret.rel_value_us = size * 1000LL * 1000LL / b;
116 LOG (GNUNET_ERROR_TYPE_DEBUG,
117 "Bandwidth suggests delay of %s for %llu bytes of traffic\n",
118 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES),
119 (unsigned long long) size);
126 * Initialize bandwidth tracker. Note that in addition to the
127 * 'max_carry_s' limit, we also always allow at least
128 * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate. So if the
129 * bytes-per-second limit is so small that within 'max_carry_s' not
130 * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
131 * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
134 * @param av tracker to initialize
135 * @param update_cb callback to notify a client about the tracker being updated
136 * @param update_cb_cls cls for the callback
137 * @param bytes_per_second_limit initial limit to assume
138 * @param max_carry_s maximum number of seconds unused bandwidth
139 * may accumulate before it expires
142 GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
143 GNUNET_BANDWIDTH_tracker_update_cb update_cb,
145 struct GNUNET_BANDWIDTH_Value32NBO
146 bytes_per_second_limit, uint32_t max_carry_s)
148 av->update_cb = update_cb;
149 av->update_cb_cls = update_cb_cls;
150 av->consumption_since_last_update__ = 0;
151 av->last_update__ = GNUNET_TIME_absolute_get ();
152 av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__);
153 av->max_carry_s__ = max_carry_s;
154 LOG (GNUNET_ERROR_TYPE_DEBUG,
155 "Tracker %p initialized with %u Bps and max carry %u\n", av,
156 (unsigned int) av->available_bytes_per_s__, (unsigned int) max_carry_s);
161 * Update the tracker, looking at the current time and
162 * bandwidth consumption data.
164 * @param av tracker to update
167 update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
169 struct GNUNET_TIME_Absolute now;
170 struct GNUNET_TIME_Relative delta;
172 uint64_t delta_avail;
176 now = GNUNET_TIME_absolute_get ();
177 delta_time = now.abs_value_us - av->last_update__.abs_value_us;
179 (delta_time * ((unsigned long long) av->available_bytes_per_s__) +
180 500000LL) / 1000000LL;
181 av->consumption_since_last_update__ -= delta_avail;
182 av->last_update__ = now;
183 if (av->consumption_since_last_update__ < 0)
185 left_bytes = -av->consumption_since_last_update__;
186 max_carry = av->available_bytes_per_s__ * av->max_carry_s__;
187 if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE)
188 max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE;
189 if (max_carry > left_bytes)
190 av->consumption_since_last_update__ = -left_bytes;
192 av->consumption_since_last_update__ = -max_carry;
194 delta.rel_value_us = delta_time;
195 LOG (GNUNET_ERROR_TYPE_DEBUG,
196 "Tracker %p updated, have %u Bps, last update was %s ago\n", av,
197 (unsigned int) av->available_bytes_per_s__,
198 GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_YES));
203 * Notify the tracker that a certain number of bytes of bandwidth have
204 * been consumed. Note that it is legal to consume bytes even if not
205 * enough bandwidth is available (in that case,
206 * #GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
207 * even for a size of zero for a while).
209 * @param av tracker to update
210 * @param size number of bytes consumed
211 * @return #GNUNET_YES if this consumption is above the limit
214 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
219 LOG (GNUNET_ERROR_TYPE_DEBUG,
220 "Tracker %p consumes %d bytes\n",
225 nc = av->consumption_since_last_update__ + size;
226 if (nc < av->consumption_since_last_update__)
229 return GNUNET_SYSERR;
231 av->consumption_since_last_update__ = nc;
233 if (av->consumption_since_last_update__ > 0)
235 LOG (GNUNET_ERROR_TYPE_DEBUG,
236 "Tracker %p consumption %llu bytes above limit\n", av,
237 (unsigned long long) av->consumption_since_last_update__);
243 av->consumption_since_last_update__ += size;
250 * Compute how long we should wait until consuming 'size'
251 * bytes of bandwidth in order to stay within the given
254 * @param av tracker to query
255 * @param size number of bytes we would like to consume
256 * @return time in ms to wait for consumption to be OK
258 struct GNUNET_TIME_Relative
259 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
262 struct GNUNET_TIME_Relative ret;
263 int64_t bytes_needed;
265 if (av->available_bytes_per_s__ == 0)
267 LOG (GNUNET_ERROR_TYPE_DEBUG,
268 "Tracker %p delay is infinity\n", av);
269 return GNUNET_TIME_UNIT_FOREVER_REL;
272 bytes_needed = size + av->consumption_since_last_update__;
273 if (bytes_needed <= 0)
275 LOG (GNUNET_ERROR_TYPE_DEBUG,
276 "Tracker %p delay for %u bytes is zero\n", av,
277 (unsigned int) size);
278 return GNUNET_TIME_UNIT_ZERO;
281 (1000LL * 1000LL * bytes_needed) /
282 (unsigned long long) av->available_bytes_per_s__;
283 LOG (GNUNET_ERROR_TYPE_DEBUG,
284 "Tracker %p delay for %u bytes is %s\n",
285 av, (unsigned int) size,
286 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
292 * Compute how many bytes are available for consumption right now.
295 * @param av tracker to query
296 * @return number of bytes available for consumption right now
299 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker *av)
301 struct GNUNET_BANDWIDTH_Value32NBO bps;
306 bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__);
308 GNUNET_BANDWIDTH_value_get_available_until (bps,
309 GNUNET_TIME_absolute_get_duration
310 (av->last_update__));
311 used = av->consumption_since_last_update__;
312 LOG (GNUNET_ERROR_TYPE_DEBUG,
313 "Tracker %p available bandwidth is %lld bytes\n", av,
314 (long long) (int64_t) (avail - used));
315 return (int64_t) (avail - used);
320 * Update quota of bandwidth tracker.
322 * @param av tracker to initialize
323 * @param bytes_per_second_limit new limit to assume
326 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
327 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit)
332 new_limit = ntohl (bytes_per_second_limit.value__);
333 LOG (GNUNET_ERROR_TYPE_DEBUG,
334 "Tracker %p bandwidth changed to %u Bps\n", av,
335 (unsigned int) new_limit);
337 old_limit = av->available_bytes_per_s__;
338 av->available_bytes_per_s__ = new_limit;
339 if (NULL != av->update_cb)
340 av->update_cb (av->update_cb_cls);
341 if (old_limit > new_limit)
342 update_tracker (av); /* maximum excess might be less now */
346 /* end of bandwidth.c */