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"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util-bandwidth", __VA_ARGS__)
34 * Create a new bandwidth value.
36 * @param bytes_per_second value to create
37 * @return the new bandwidth value
39 struct GNUNET_BANDWIDTH_Value32NBO
40 GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second)
42 struct GNUNET_BANDWIDTH_Value32NBO ret;
44 LOG (GNUNET_ERROR_TYPE_DEBUG, "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 %llums\n",
87 (unsigned long long) ((b * deadline.rel_value + 500LL) / 1000LL),
89 return (b * deadline.rel_value + 500LL) / 1000LL;
94 * At the given bandwidth, calculate how long it would take for
95 * '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 = size * 1000LL / b;
116 LOG (GNUNET_ERROR_TYPE_DEBUG,
117 "Bandwidth suggests delay of %llu ms for %llu bytes of traffic\n",
118 (unsigned long long) ret.rel_value, (unsigned long long) size);
124 * Initialize bandwidth tracker. Note that in addition to the
125 * 'max_carry_s' limit, we also always allow at least
126 * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate. So if the
127 * bytes-per-second limit is so small that within 'max_carry_s' not
128 * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
129 * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
132 * @param av tracker to initialize
133 * @param bytes_per_second_limit initial limit to assume
134 * @param max_carry_s maximum number of seconds unused bandwidth
135 * may accumulate before it expires
138 GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
139 struct GNUNET_BANDWIDTH_Value32NBO
140 bytes_per_second_limit, uint32_t max_carry_s)
142 av->consumption_since_last_update__ = 0;
143 av->last_update__ = GNUNET_TIME_absolute_get ();
144 av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__);
145 av->max_carry_s__ = max_carry_s;
146 LOG (GNUNET_ERROR_TYPE_DEBUG,
147 "Tracker %p initialized with %u Bps and max carry %u\n", av,
148 (unsigned int) av->available_bytes_per_s__, (unsigned int) max_carry_s);
153 * Update the tracker, looking at the current time and
154 * bandwidth consumption data.
156 * @param av tracker to update
159 update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
161 struct GNUNET_TIME_Absolute now;
163 uint64_t delta_avail;
167 now = GNUNET_TIME_absolute_get ();
168 delta_time = now.abs_value - av->last_update__.abs_value;
170 (delta_time * ((unsigned long long) av->available_bytes_per_s__) +
172 av->consumption_since_last_update__ -= delta_avail;
173 av->last_update__ = now;
174 if (av->consumption_since_last_update__ < 0)
176 left_bytes = -av->consumption_since_last_update__;
177 max_carry = av->available_bytes_per_s__ * av->max_carry_s__;
178 if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE)
179 max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE;
180 if (max_carry > left_bytes)
181 av->consumption_since_last_update__ = -left_bytes;
183 av->consumption_since_last_update__ = -max_carry;
185 LOG (GNUNET_ERROR_TYPE_DEBUG,
186 "Tracker %p updated, have %u Bps, last update was %llu ms ago\n", av,
187 (unsigned int) av->available_bytes_per_s__,
188 (unsigned long long) delta_time);
193 * Notify the tracker that a certain number of bytes of bandwidth have
194 * been consumed. Note that it is legal to consume bytes even if not
195 * enough bandwidth is available (in that case,
196 * GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
197 * even for a size of zero for a while).
199 * @param av tracker to update
200 * @param size number of bytes consumed
201 * @return GNUNET_YES if this consumption is above the limit
204 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
209 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p consumes %d bytes\n", av,
213 nc = av->consumption_since_last_update__ + size;
214 if (nc < av->consumption_since_last_update__)
217 return GNUNET_SYSERR;
219 av->consumption_since_last_update__ = nc;
221 if (av->consumption_since_last_update__ > 0)
223 LOG (GNUNET_ERROR_TYPE_DEBUG,
224 "Tracker %p consumption %llu bytes above limit\n", av,
225 (unsigned long long) av->consumption_since_last_update__);
231 av->consumption_since_last_update__ += size;
238 * Compute how long we should wait until consuming 'size'
239 * bytes of bandwidth in order to stay within the given
242 * @param av tracker to query
243 * @param size number of bytes we would like to consume
244 * @return time in ms to wait for consumption to be OK
246 struct GNUNET_TIME_Relative
247 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
250 struct GNUNET_TIME_Relative ret;
251 int64_t bytes_needed;
253 if (av->available_bytes_per_s__ == 0)
255 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay is infinity\n", av);
256 return GNUNET_TIME_UNIT_FOREVER_REL;
259 bytes_needed = size + av->consumption_since_last_update__;
260 if (bytes_needed <= 0)
262 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay for %u bytes is zero\n", av,
263 (unsigned int) size);
264 return GNUNET_TIME_UNIT_ZERO;
267 (1000LL * bytes_needed) /
268 (unsigned long long) av->available_bytes_per_s__;
269 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay for %u bytes is %llu ms\n",
270 av, (unsigned int) size, (unsigned long long) ret.rel_value);
276 * Compute how many bytes are available for consumption right now.
279 * @param av tracker to query
280 * @return number of bytes available for consumption right now
283 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker * av)
285 struct GNUNET_BANDWIDTH_Value32NBO bps;
290 bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__);
292 GNUNET_BANDWIDTH_value_get_available_until (bps,
293 GNUNET_TIME_absolute_get_duration
294 (av->last_update__));
295 used = av->consumption_since_last_update__;
296 LOG (GNUNET_ERROR_TYPE_DEBUG,
297 "Tracker %p available bandwidth is %lld bytes\n", av,
298 (long long) (int64_t) (avail - used));
299 return (int64_t) (avail - used);
304 * Update quota of bandwidth tracker.
306 * @param av tracker to initialize
307 * @param bytes_per_second_limit new limit to assume
310 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
311 struct GNUNET_BANDWIDTH_Value32NBO
312 bytes_per_second_limit)
317 new_limit = ntohl (bytes_per_second_limit.value__);
318 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p bandwidth changed to %u Bps\n", av,
319 (unsigned int) new_limit);
321 old_limit = av->available_bytes_per_s__;
322 av->available_bytes_per_s__ = new_limit;
323 if (old_limit > new_limit)
324 update_tracker (av); /* maximum excess might be less now */
328 /* end of bandwidth.c */