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);
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;
162 struct GNUNET_TIME_Relative delta;
164 uint64_t delta_avail;
168 now = GNUNET_TIME_absolute_get ();
169 delta_time = now.abs_value_us - av->last_update__.abs_value_us;
171 (delta_time * ((unsigned long long) av->available_bytes_per_s__) +
172 500000LL) / 1000000LL;
173 av->consumption_since_last_update__ -= delta_avail;
174 av->last_update__ = now;
175 if (av->consumption_since_last_update__ < 0)
177 left_bytes = -av->consumption_since_last_update__;
178 max_carry = av->available_bytes_per_s__ * av->max_carry_s__;
179 if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE)
180 max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE;
181 if (max_carry > left_bytes)
182 av->consumption_since_last_update__ = -left_bytes;
184 av->consumption_since_last_update__ = -max_carry;
186 delta.rel_value_us = delta_time;
187 LOG (GNUNET_ERROR_TYPE_DEBUG,
188 "Tracker %p updated, have %u Bps, last update was %s ago\n", av,
189 (unsigned int) av->available_bytes_per_s__,
190 GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_YES));
195 * Notify the tracker that a certain number of bytes of bandwidth have
196 * been consumed. Note that it is legal to consume bytes even if not
197 * enough bandwidth is available (in that case,
198 * GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
199 * even for a size of zero for a while).
201 * @param av tracker to update
202 * @param size number of bytes consumed
203 * @return GNUNET_YES if this consumption is above the limit
206 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
211 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p consumes %d bytes\n", av,
215 nc = av->consumption_since_last_update__ + size;
216 if (nc < av->consumption_since_last_update__)
219 return GNUNET_SYSERR;
221 av->consumption_since_last_update__ = nc;
223 if (av->consumption_since_last_update__ > 0)
225 LOG (GNUNET_ERROR_TYPE_DEBUG,
226 "Tracker %p consumption %llu bytes above limit\n", av,
227 (unsigned long long) av->consumption_since_last_update__);
233 av->consumption_since_last_update__ += size;
240 * Compute how long we should wait until consuming 'size'
241 * bytes of bandwidth in order to stay within the given
244 * @param av tracker to query
245 * @param size number of bytes we would like to consume
246 * @return time in ms to wait for consumption to be OK
248 struct GNUNET_TIME_Relative
249 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
252 struct GNUNET_TIME_Relative ret;
253 int64_t bytes_needed;
255 if (av->available_bytes_per_s__ == 0)
257 LOG (GNUNET_ERROR_TYPE_DEBUG,
258 "Tracker %p delay is infinity\n", av);
259 return GNUNET_TIME_UNIT_FOREVER_REL;
262 bytes_needed = size + av->consumption_since_last_update__;
263 if (bytes_needed <= 0)
265 LOG (GNUNET_ERROR_TYPE_DEBUG,
266 "Tracker %p delay for %u bytes is zero\n", av,
267 (unsigned int) size);
268 return GNUNET_TIME_UNIT_ZERO;
271 (1000LL * 1000LL * bytes_needed) /
272 (unsigned long long) av->available_bytes_per_s__;
273 LOG (GNUNET_ERROR_TYPE_DEBUG,
274 "Tracker %p delay for %u bytes is %s\n",
275 av, (unsigned int) size,
276 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
282 * Compute how many bytes are available for consumption right now.
285 * @param av tracker to query
286 * @return number of bytes available for consumption right now
289 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker * av)
291 struct GNUNET_BANDWIDTH_Value32NBO bps;
296 bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__);
298 GNUNET_BANDWIDTH_value_get_available_until (bps,
299 GNUNET_TIME_absolute_get_duration
300 (av->last_update__));
301 used = av->consumption_since_last_update__;
302 LOG (GNUNET_ERROR_TYPE_DEBUG,
303 "Tracker %p available bandwidth is %lld bytes\n", av,
304 (long long) (int64_t) (avail - used));
305 return (int64_t) (avail - used);
310 * Update quota of bandwidth tracker.
312 * @param av tracker to initialize
313 * @param bytes_per_second_limit new limit to assume
316 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
317 struct GNUNET_BANDWIDTH_Value32NBO
318 bytes_per_second_limit)
323 new_limit = ntohl (bytes_per_second_limit.value__);
324 LOG (GNUNET_ERROR_TYPE_DEBUG,
325 "Tracker %p bandwidth changed to %u Bps\n", av,
326 (unsigned int) new_limit);
328 old_limit = av->available_bytes_per_s__;
329 av->available_bytes_per_s__ = new_limit;
330 if (old_limit > new_limit)
331 update_tracker (av); /* maximum excess might be less now */
335 /* end of bandwidth.c */