-typo
[oweals/gnunet.git] / src / util / bandwidth.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file util/bandwidth.c
23  * @brief functions related to bandwidth (unit)
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_bandwidth_lib.h"
28 #include "gnunet_server_lib.h"
29
30
31 #define LOG(kind,...) GNUNET_log_from (kind, "util-bandwidth", __VA_ARGS__)
32
33 /**
34  * Create a new bandwidth value.
35  *
36  * @param bytes_per_second value to create
37  * @return the new bandwidth value
38  */
39 struct GNUNET_BANDWIDTH_Value32NBO
40 GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second)
41 {
42   struct GNUNET_BANDWIDTH_Value32NBO ret;
43
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);
47   return ret;
48 }
49
50
51 /**
52  * Compute the MIN of two bandwidth values.
53  *
54  * @param b1 first value
55  * @param b2 second value
56  * @return the min of b1 and b2
57  */
58 struct GNUNET_BANDWIDTH_Value32NBO
59 GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
60                             struct GNUNET_BANDWIDTH_Value32NBO b2)
61 {
62   return
63       GNUNET_BANDWIDTH_value_init (GNUNET_MIN
64                                    (ntohl (b1.value__), ntohl (b2.value__)));
65 }
66
67
68 /**
69  * At the given bandwidth, calculate how much traffic will be
70  * available until the given deadline.
71  *
72  * @param bps bandwidth
73  * @param deadline when is the deadline
74  * @return number of bytes available at bps until deadline
75  */
76 uint64_t
77 GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO
78                                             bps,
79                                             struct GNUNET_TIME_Relative
80                                             deadline)
81 {
82   uint64_t b;
83
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),
88        deadline.rel_value);
89   return (b * deadline.rel_value + 500LL) / 1000LL;
90 }
91
92
93 /**
94  * At the given bandwidth, calculate how long it would take for
95  * 'size' bytes to be transmitted.
96  *
97  * @param bps bandwidth
98  * @param size number of bytes we want to have available
99  * @return how long it would take
100  */
101 struct GNUNET_TIME_Relative
102 GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
103                                       uint64_t size)
104 {
105   uint64_t b;
106   struct GNUNET_TIME_Relative ret;
107
108   b = ntohl (bps.value__);
109   if (b == 0)
110   {
111     LOG (GNUNET_ERROR_TYPE_DEBUG,
112          "Bandwidth suggests delay of infinity (zero bandwidth)\n");
113     return GNUNET_TIME_UNIT_FOREVER_REL;
114   }
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);
119   return ret;
120 }
121
122
123 /**
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
130  * bytes).
131  *
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
136  */
137 void
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)
141 {
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);
149 }
150
151
152 /**
153  * Update the tracker, looking at the current time and
154  * bandwidth consumption data.
155  *
156  * @param av tracker to update
157  */
158 static void
159 update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
160 {
161   struct GNUNET_TIME_Absolute now;
162   uint64_t delta_time;
163   uint64_t delta_avail;
164   uint64_t left_bytes;
165   uint64_t max_carry;
166
167   now = GNUNET_TIME_absolute_get ();
168   delta_time = now.abs_value - av->last_update__.abs_value;
169   delta_avail =
170       (delta_time * ((unsigned long long) av->available_bytes_per_s__) +
171        500LL) / 1000LL;
172   av->consumption_since_last_update__ -= delta_avail;
173   av->last_update__ = now;
174   if (av->consumption_since_last_update__ < 0)
175   {
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;
182     else
183       av->consumption_since_last_update__ = -max_carry;
184   }
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);
189 }
190
191
192 /**
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).
198  *
199  * @param av tracker to update
200  * @param size number of bytes consumed
201  * @return GNUNET_YES if this consumption is above the limit
202  */
203 int
204 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
205                                   ssize_t size)
206 {
207   int64_t nc;
208
209   LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p consumes %d bytes\n", av,
210        (int) size);
211   if (size > 0)
212   {
213     nc = av->consumption_since_last_update__ + size;
214     if (nc < av->consumption_since_last_update__)
215     {
216       GNUNET_break (0);
217       return GNUNET_SYSERR;
218     }
219     av->consumption_since_last_update__ = nc;
220     update_tracker (av);
221     if (av->consumption_since_last_update__ > 0)
222     {
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__);
226       return GNUNET_YES;
227     }
228   }
229   else
230   {
231     av->consumption_since_last_update__ += size;
232   }
233   return GNUNET_NO;
234 }
235
236
237 /**
238  * Compute how long we should wait until consuming 'size'
239  * bytes of bandwidth in order to stay within the given
240  * quota.
241  *
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
245  */
246 struct GNUNET_TIME_Relative
247 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
248                                     size_t size)
249 {
250   struct GNUNET_TIME_Relative ret;
251   int64_t bytes_needed;
252
253   if (av->available_bytes_per_s__ == 0)
254   {
255     LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay is infinity\n", av);
256     return GNUNET_TIME_UNIT_FOREVER_REL;
257   }
258   update_tracker (av);
259   bytes_needed = size + av->consumption_since_last_update__;
260   if (bytes_needed <= 0)
261   {
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;
265   }
266   ret.rel_value =
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);
271   return ret;
272 }
273
274
275 /**
276  * Compute how many bytes are available for consumption right now.
277  * quota.
278  *
279  * @param av tracker to query
280  * @return number of bytes available for consumption right now
281  */
282 int64_t
283 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker * av)
284 {
285   struct GNUNET_BANDWIDTH_Value32NBO bps;
286   uint64_t avail;
287   int64_t used;
288
289   update_tracker (av);
290   bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__);
291   avail =
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);
300 }
301
302
303 /**
304  * Update quota of bandwidth tracker.
305  *
306  * @param av tracker to initialize
307  * @param bytes_per_second_limit new limit to assume
308  */
309 void
310 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
311                                        struct GNUNET_BANDWIDTH_Value32NBO
312                                        bytes_per_second_limit)
313 {
314   uint32_t old_limit;
315   uint32_t new_limit;
316
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);
320   update_tracker (av);
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 */
325 }
326
327
328 /* end of bandwidth.c */