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 * Initialize bandwidth tracker. Note that in addition to the
64 * 'max_carry_s' limit, we also always allow at least
65 * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate. So if the
66 * bytes-per-second limit is so small that within 'max_carry_s' not
67 * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
68 * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
71 * @param av tracker to initialize
72 * @param bytes_per_second_limit initial limit to assume
73 * @param max_carry_s maximum number of seconds unused bandwidth
74 * may accumulate before it expires
77 GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
78 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit,
81 av->consumption_since_last_update__ = 0;
82 av->last_update__ = GNUNET_TIME_absolute_get ();
83 av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__);
84 av->max_carry_s__ = max_carry_s;
89 * Update the tracker, looking at the current time and
90 * bandwidth consumption data.
92 * @param av tracker to update
95 update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
97 struct GNUNET_TIME_Absolute now;
98 uint64_t avail_per_ms;
100 uint64_t delta_avail;
102 uint64_t left_time_ms;
104 now = GNUNET_TIME_absolute_get ();
105 delta_time = now.value - av->last_update__.value;
106 delta_avail = (delta_time * ((unsigned long long) av->available_bytes_per_s__)) / 1000LL;
107 if (av->consumption_since_last_update__ >= delta_avail)
109 av->consumption_since_last_update__ -= delta_avail;
110 av->last_update__ = now;
114 left_bytes = delta_avail - av->consumption_since_last_update__;
115 avail_per_ms = ((unsigned long long) av->available_bytes_per_s__) / 1000LL;
116 if (avail_per_ms > 0)
117 left_time_ms = left_bytes / avail_per_ms;
120 if (left_time_ms > ((unsigned long long) av->max_carry_s__) * 1000LL)
122 /* need to limit accumulation of unused bandwidth */
123 left_time_ms = ((unsigned long long) av->max_carry_s__) * 1000LL;
124 if (left_time_ms * avail_per_ms < GNUNET_SERVER_MAX_MESSAGE_SIZE)
126 /* need to still allow GNUNET_SERVER_MAX_MESSAGE_SIZE accumulation */
127 if (left_bytes > GNUNET_SERVER_MAX_MESSAGE_SIZE)
128 left_bytes = GNUNET_SERVER_MAX_MESSAGE_SIZE;
129 left_time_ms = left_bytes / avail_per_ms;
132 av->consumption_since_last_update__ = 0;
133 av->last_update__.value = now.value - left_time_ms;
140 * Notify the tracker that a certain number of bytes of bandwidth have
141 * been consumed. Note that it is legal to consume bytes even if not
142 * enough bandwidth is available (in that case,
143 * GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
144 * even for a size of zero for a while).
146 * @param av tracker to update
147 * @param size number of bytes consumed
150 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
155 nc = av->consumption_since_last_update__ + size;
156 if (nc < av->consumption_since_last_update__)
161 av->consumption_since_last_update__ += size;
167 * Compute how long we should wait until consuming 'size'
168 * bytes of bandwidth in order to stay within the given
171 * @param av tracker to query
172 * @param size number of bytes we would like to consume
173 * @return time to wait for consumption to be OK
175 struct GNUNET_TIME_Relative
176 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
179 struct GNUNET_TIME_Relative ret;
180 struct GNUNET_TIME_Absolute now;
181 uint64_t delta_avail;
183 uint64_t bytes_needed;
185 if (av->available_bytes_per_s__ == 0)
186 return GNUNET_TIME_UNIT_FOREVER_REL;
188 now = GNUNET_TIME_absolute_get ();
189 delta_time = now.value - av->last_update__.value;
190 delta_avail = (delta_time * ((unsigned long long) av->available_bytes_per_s__)) / 1000LL;
191 if (delta_avail >= size)
192 return GNUNET_TIME_UNIT_ZERO;
193 bytes_needed = size - delta_avail;
194 ret.value = 1000LL * bytes_needed / (unsigned long long) av->available_bytes_per_s__;
200 * Update quota of bandwidth tracker.
202 * @param av tracker to initialize
203 * @param bytes_per_second_limit new limit to assume
206 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
207 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit)
212 new_limit = ntohl (bytes_per_second_limit.value__);
214 old_limit = av->available_bytes_per_s__;
215 av->available_bytes_per_s__ = new_limit;
216 if (old_limit > new_limit)
217 update_tracker (av); /* maximum excess might be less now */
221 /* end of bandwidth.c */