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 * Create a new bandwidth value.
33 * @param bytes_per_second value to create
34 * @return the new bandwidth value
36 struct GNUNET_BANDWIDTH_Value32NBO
37 GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second)
39 struct GNUNET_BANDWIDTH_Value32NBO ret;
41 ret.value__ = htonl (bytes_per_second);
47 * Compute the MIN of two bandwidth values.
49 * @param b1 first value
50 * @param b2 second value
51 * @return the min of b1 and b2
53 struct GNUNET_BANDWIDTH_Value32NBO
54 GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
55 struct GNUNET_BANDWIDTH_Value32NBO b2)
57 return GNUNET_BANDWIDTH_value_init (GNUNET_MIN (ntohl (b1.value__),
63 * At the given bandwidth, calculate how much traffic will be
64 * available until the given deadline.
66 * @param bps bandwidth
67 * @param deadline when is the deadline
68 * @return number of bytes available at bps until deadline
71 GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO bps,
72 struct GNUNET_TIME_Relative deadline)
76 b = ntohl (bps.value__);
77 return b * deadline.value / 1000LL;
82 * At the given bandwidth, calculate how long it would take for
83 * 'size' bytes to be transmitted.
85 * @param bps bandwidth
86 * @param size number of bytes we want to have available
87 * @return how long it would take
89 struct GNUNET_TIME_Relative
90 GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
94 struct GNUNET_TIME_Relative ret;
96 b = ntohl (bps.value__);
98 return GNUNET_TIME_UNIT_FOREVER_REL;
99 ret.value = size * 1000LL / b;
105 * Initialize bandwidth tracker. Note that in addition to the
106 * 'max_carry_s' limit, we also always allow at least
107 * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate. So if the
108 * bytes-per-second limit is so small that within 'max_carry_s' not
109 * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
110 * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
113 * @param av tracker to initialize
114 * @param bytes_per_second_limit initial limit to assume
115 * @param max_carry_s maximum number of seconds unused bandwidth
116 * may accumulate before it expires
119 GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
120 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit,
121 uint32_t max_carry_s)
123 av->consumption_since_last_update__ = 0;
124 av->last_update__ = GNUNET_TIME_absolute_get ();
125 av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__);
126 av->max_carry_s__ = max_carry_s;
131 * Update the tracker, looking at the current time and
132 * bandwidth consumption data.
134 * @param av tracker to update
137 update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
139 struct GNUNET_TIME_Absolute now;
140 uint64_t avail_per_ms;
142 uint64_t delta_avail;
144 uint64_t left_time_ms;
146 now = GNUNET_TIME_absolute_get ();
147 delta_time = now.value - av->last_update__.value;
148 delta_avail = (delta_time * ((unsigned long long) av->available_bytes_per_s__)) / 1000LL;
149 if (av->consumption_since_last_update__ >= delta_avail)
151 av->consumption_since_last_update__ -= delta_avail;
152 av->last_update__ = now;
156 left_bytes = delta_avail - av->consumption_since_last_update__;
157 avail_per_ms = ((unsigned long long) av->available_bytes_per_s__) / 1000LL;
158 if (avail_per_ms > 0)
160 left_time_ms = left_bytes / avail_per_ms;
161 if (left_time_ms > ((unsigned long long) av->max_carry_s__) * 1000LL)
163 /* need to limit accumulation of unused bandwidth */
164 left_time_ms = ((unsigned long long) av->max_carry_s__) * 1000LL;
165 if (left_time_ms * avail_per_ms < GNUNET_SERVER_MAX_MESSAGE_SIZE)
167 /* need to still allow GNUNET_SERVER_MAX_MESSAGE_SIZE accumulation */
168 if (left_bytes > GNUNET_SERVER_MAX_MESSAGE_SIZE)
169 left_bytes = GNUNET_SERVER_MAX_MESSAGE_SIZE;
170 left_time_ms = left_bytes / avail_per_ms;
178 av->consumption_since_last_update__ = 0;
179 av->last_update__.value = now.value - left_time_ms;
185 * Notify the tracker that a certain number of bytes of bandwidth have
186 * been consumed. Note that it is legal to consume bytes even if not
187 * enough bandwidth is available (in that case,
188 * GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
189 * even for a size of zero for a while).
191 * @param av tracker to update
192 * @param size number of bytes consumed
193 * @return GNUNET_YES if this consumption is above the limit
196 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
203 nc = av->consumption_since_last_update__ + size;
204 if (nc < av->consumption_since_last_update__)
207 return GNUNET_SYSERR;
209 av->consumption_since_last_update__ = nc;
211 if (av->consumption_since_last_update__ > 0)
216 av->last_update__.value -= (size * av->available_bytes_per_s__) / 1000LL;
224 * Compute how long we should wait until consuming 'size'
225 * bytes of bandwidth in order to stay within the given
228 * @param av tracker to query
229 * @param size number of bytes we would like to consume
230 * @return time to wait for consumption to be OK
232 struct GNUNET_TIME_Relative
233 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
236 struct GNUNET_TIME_Relative ret;
237 struct GNUNET_TIME_Absolute now;
238 uint64_t delta_avail;
240 uint64_t bytes_needed;
242 if (av->available_bytes_per_s__ == 0)
243 return GNUNET_TIME_UNIT_FOREVER_REL;
245 now = GNUNET_TIME_absolute_get ();
246 delta_time = now.value - av->last_update__.value;
247 delta_avail = (delta_time * ((unsigned long long) av->available_bytes_per_s__)) / 1000LL;
248 if (delta_avail >= size)
249 return GNUNET_TIME_UNIT_ZERO;
250 bytes_needed = size - delta_avail;
251 ret.value = 1000LL * bytes_needed / (unsigned long long) av->available_bytes_per_s__;
257 * Compute how many bytes are available for consumption right now.
260 * @param av tracker to query
261 * @return number of bytes available for consumption right now
264 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker *av)
266 struct GNUNET_BANDWIDTH_Value32NBO bps;
271 bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__);
272 avail = GNUNET_BANDWIDTH_value_get_available_until (bps,
273 GNUNET_TIME_absolute_get_duration (av->last_update__));
274 used = av->consumption_since_last_update__;
275 return (int64_t) (avail - used);
280 * Update quota of bandwidth tracker.
282 * @param av tracker to initialize
283 * @param bytes_per_second_limit new limit to assume
286 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
287 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit)
292 new_limit = ntohl (bytes_per_second_limit.value__);
294 old_limit = av->available_bytes_per_s__;
295 av->available_bytes_per_s__ = new_limit;
296 if (old_limit > new_limit)
297 update_tracker (av); /* maximum excess might be less now */
301 /* end of bandwidth.c */