2 This file is part of GNUnet.
3 (C) 2010 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 2, 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_bandwidth_lib.h"
28 #include "gnunet_server_lib.h"
30 #define DEBUG_BANDWIDTH GNUNET_NO
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;
44 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
45 "Initializing bandwidth of %u Bps\n",
46 (unsigned int) bytes_per_second);
48 ret.value__ = htonl (bytes_per_second);
54 * Compute the MIN of two bandwidth values.
56 * @param b1 first value
57 * @param b2 second value
58 * @return the min of b1 and b2
60 struct GNUNET_BANDWIDTH_Value32NBO
61 GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
62 struct GNUNET_BANDWIDTH_Value32NBO b2)
64 return GNUNET_BANDWIDTH_value_init (GNUNET_MIN (ntohl (b1.value__),
70 * At the given bandwidth, calculate how much traffic will be
71 * available until the given deadline.
73 * @param bps bandwidth
74 * @param deadline when is the deadline
75 * @return number of bytes available at bps until deadline
78 GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO
80 struct GNUNET_TIME_Relative
85 b = ntohl (bps.value__);
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
88 "Bandwidth has %llu bytes available until deadline in %llums\n",
89 (unsigned long long) ((b * deadline.rel_value + 500LL) / 1000LL),
92 return (b * deadline.rel_value + 500LL) / 1000LL;
97 * At the given bandwidth, calculate how long it would take for
98 * 'size' bytes to be transmitted.
100 * @param bps bandwidth
101 * @param size number of bytes we want to have available
102 * @return how long it would take
104 struct GNUNET_TIME_Relative
105 GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
109 struct GNUNET_TIME_Relative ret;
111 b = ntohl (bps.value__);
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116 "Bandwidth suggests delay of infinity (zero bandwidth)\n");
118 return GNUNET_TIME_UNIT_FOREVER_REL;
120 ret.rel_value = size * 1000LL / b;
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123 "Bandwidth suggests delay of %llu ms for %llu bytes of traffic\n",
124 (unsigned long long) ret.rel_value, (unsigned long long) size);
131 * Initialize bandwidth tracker. Note that in addition to the
132 * 'max_carry_s' limit, we also always allow at least
133 * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate. So if the
134 * bytes-per-second limit is so small that within 'max_carry_s' not
135 * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
136 * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
139 * @param av tracker to initialize
140 * @param bytes_per_second_limit initial limit to assume
141 * @param max_carry_s maximum number of seconds unused bandwidth
142 * may accumulate before it expires
145 GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
146 struct GNUNET_BANDWIDTH_Value32NBO
147 bytes_per_second_limit, uint32_t max_carry_s)
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;
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 "Tracker %p initialized with %u Bps and max carry %u\n",
157 (unsigned int) av->available_bytes_per_s__,
158 (unsigned int) max_carry_s);
164 * Update the tracker, looking at the current time and
165 * bandwidth consumption data.
167 * @param av tracker to update
170 update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
172 struct GNUNET_TIME_Absolute now;
174 uint64_t delta_avail;
178 now = GNUNET_TIME_absolute_get ();
179 delta_time = now.abs_value - av->last_update__.abs_value;
181 (delta_time * ((unsigned long long) av->available_bytes_per_s__) +
183 av->consumption_since_last_update__ -= delta_avail;
184 av->last_update__ = now;
185 if (av->consumption_since_last_update__ < 0)
187 left_bytes = -av->consumption_since_last_update__;
188 max_carry = av->available_bytes_per_s__ * av->max_carry_s__;
189 if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE)
190 max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE;
191 if (max_carry > left_bytes)
192 av->consumption_since_last_update__ = -left_bytes;
194 av->consumption_since_last_update__ = -max_carry;
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "Tracker %p updated, have %u Bps, last update was %llu ms ago\n",
200 (unsigned int) av->available_bytes_per_s__,
201 (unsigned long long) delta_time);
208 * Notify the tracker that a certain number of bytes of bandwidth have
209 * been consumed. Note that it is legal to consume bytes even if not
210 * enough bandwidth is available (in that case,
211 * GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
212 * even for a size of zero for a while).
214 * @param av tracker to update
215 * @param size number of bytes consumed
216 * @return GNUNET_YES if this consumption is above the limit
219 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Tracker %p consumes %d bytes\n", av, (int) size);
230 nc = av->consumption_since_last_update__ + size;
231 if (nc < av->consumption_since_last_update__)
234 return GNUNET_SYSERR;
236 av->consumption_since_last_update__ = nc;
238 if (av->consumption_since_last_update__ > 0)
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242 "Tracker %p consumption %llu bytes above limit\n",
243 av, (unsigned long long) av->consumption_since_last_update__);
250 av->consumption_since_last_update__ += size;
257 * Compute how long we should wait until consuming 'size'
258 * bytes of bandwidth in order to stay within the given
261 * @param av tracker to query
262 * @param size number of bytes we would like to consume
263 * @return time in ms to wait for consumption to be OK
265 struct GNUNET_TIME_Relative
266 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
269 struct GNUNET_TIME_Relative ret;
270 int64_t bytes_needed;
272 if (av->available_bytes_per_s__ == 0)
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay is infinity\n", av);
277 return GNUNET_TIME_UNIT_FOREVER_REL;
280 bytes_needed = size + av->consumption_since_last_update__;
281 if (bytes_needed <= 0)
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "Tracker %p delay for %u bytes is zero\n",
286 av, (unsigned int) size);
288 return GNUNET_TIME_UNIT_ZERO;
291 1000LL * bytes_needed / (unsigned long long) av->available_bytes_per_s__;
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294 "Tracker %p delay for %u bytes is %llu ms\n",
295 av, (unsigned int) size, (unsigned long long) ret.rel_value);
302 * Compute how many bytes are available for consumption right now.
305 * @param av tracker to query
306 * @return number of bytes available for consumption right now
309 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker * av)
311 struct GNUNET_BANDWIDTH_Value32NBO bps;
316 bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__);
317 avail = GNUNET_BANDWIDTH_value_get_available_until (bps,
318 GNUNET_TIME_absolute_get_duration
319 (av->last_update__));
320 used = av->consumption_since_last_update__;
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "Tracker %p available bandwidth is %lld bytes\n",
324 av, (long long) (int64_t) (avail - used));
326 return (int64_t) (avail - used);
331 * Update quota of bandwidth tracker.
333 * @param av tracker to initialize
334 * @param bytes_per_second_limit new limit to assume
337 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
338 struct GNUNET_BANDWIDTH_Value32NBO
339 bytes_per_second_limit)
344 new_limit = ntohl (bytes_per_second_limit.value__);
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347 "Tracker %p bandwidth changed to %u Bps\n",
348 av, (unsigned int) new_limit);
351 old_limit = av->available_bytes_per_s__;
352 av->available_bytes_per_s__ = new_limit;
353 if (old_limit > new_limit)
354 update_tracker (av); /* maximum excess might be less now */
358 /* end of bandwidth.c */