8d81e55a089d191a21a2752461bab2b407c1d730
[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 #define DEBUG_BANDWIDTH GNUNET_NO
31
32 /**
33  * Create a new bandwidth value.
34  *
35  * @param bytes_per_second value to create
36  * @return the new bandwidth value
37  */
38 struct GNUNET_BANDWIDTH_Value32NBO
39 GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second)
40 {
41   struct GNUNET_BANDWIDTH_Value32NBO ret;
42
43 #if DEBUG_BANDWIDTH
44   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing bandwidth of %u Bps\n",
45               (unsigned int) bytes_per_second);
46 #endif
47   ret.value__ = htonl (bytes_per_second);
48   return ret;
49 }
50
51
52 /**
53  * Compute the MIN of two bandwidth values.
54  *
55  * @param b1 first value
56  * @param b2 second value
57  * @return the min of b1 and b2
58  */
59 struct GNUNET_BANDWIDTH_Value32NBO
60 GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
61                             struct GNUNET_BANDWIDTH_Value32NBO b2)
62 {
63   return
64       GNUNET_BANDWIDTH_value_init (GNUNET_MIN
65                                    (ntohl (b1.value__), ntohl (b2.value__)));
66 }
67
68
69 /**
70  * At the given bandwidth, calculate how much traffic will be
71  * available until the given deadline.
72  *
73  * @param bps bandwidth
74  * @param deadline when is the deadline
75  * @return number of bytes available at bps until deadline
76  */
77 uint64_t
78 GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO
79                                             bps,
80                                             struct GNUNET_TIME_Relative
81                                             deadline)
82 {
83   uint64_t b;
84
85   b = ntohl (bps.value__);
86 #if DEBUG_BANDWIDTH
87   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
88               "Bandwidth has %llu bytes available until deadline in %llums\n",
89               (unsigned long long) ((b * deadline.rel_value + 500LL) / 1000LL),
90               deadline.rel_value);
91 #endif
92   return (b * deadline.rel_value + 500LL) / 1000LL;
93 }
94
95
96 /**
97  * At the given bandwidth, calculate how long it would take for
98  * 'size' bytes to be transmitted.
99  *
100  * @param bps bandwidth
101  * @param size number of bytes we want to have available
102  * @return how long it would take
103  */
104 struct GNUNET_TIME_Relative
105 GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
106                                       uint64_t size)
107 {
108   uint64_t b;
109   struct GNUNET_TIME_Relative ret;
110
111   b = ntohl (bps.value__);
112   if (b == 0)
113   {
114 #if DEBUG_BANDWIDTH
115     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116                 "Bandwidth suggests delay of infinity (zero bandwidth)\n");
117 #endif
118     return GNUNET_TIME_UNIT_FOREVER_REL;
119   }
120   ret.rel_value = size * 1000LL / b;
121 #if DEBUG_BANDWIDTH
122   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123               "Bandwidth suggests delay of %llu ms for %llu bytes of traffic\n",
124               (unsigned long long) ret.rel_value, (unsigned long long) size);
125 #endif
126   return ret;
127 }
128
129
130 /**
131  * Initialize bandwidth tracker.  Note that in addition to the
132  * 'max_carry_s' limit, we also always allow at least
133  * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate.  So if the
134  * bytes-per-second limit is so small that within 'max_carry_s' not
135  * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
136  * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
137  * bytes).
138  *
139  * @param av tracker to initialize
140  * @param bytes_per_second_limit initial limit to assume
141  * @param max_carry_s maximum number of seconds unused bandwidth
142  *        may accumulate before it expires
143  */
144 void
145 GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
146                                struct GNUNET_BANDWIDTH_Value32NBO
147                                bytes_per_second_limit, uint32_t max_carry_s)
148 {
149   av->consumption_since_last_update__ = 0;
150   av->last_update__ = GNUNET_TIME_absolute_get ();
151   av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__);
152   av->max_carry_s__ = max_carry_s;
153 #if DEBUG_BANDWIDTH
154   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155               "Tracker %p initialized with %u Bps and max carry %u\n", av,
156               (unsigned int) av->available_bytes_per_s__,
157               (unsigned int) max_carry_s);
158 #endif
159 }
160
161
162 /**
163  * Update the tracker, looking at the current time and
164  * bandwidth consumption data.
165  *
166  * @param av tracker to update
167  */
168 static void
169 update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
170 {
171   struct GNUNET_TIME_Absolute now;
172   uint64_t delta_time;
173   uint64_t delta_avail;
174   uint64_t left_bytes;
175   uint64_t max_carry;
176
177   now = GNUNET_TIME_absolute_get ();
178   delta_time = now.abs_value - av->last_update__.abs_value;
179   delta_avail =
180       (delta_time * ((unsigned long long) av->available_bytes_per_s__) +
181        500LL) / 1000LL;
182   av->consumption_since_last_update__ -= delta_avail;
183   av->last_update__ = now;
184   if (av->consumption_since_last_update__ < 0)
185   {
186     left_bytes = -av->consumption_since_last_update__;
187     max_carry = av->available_bytes_per_s__ * av->max_carry_s__;
188     if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE)
189       max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE;
190     if (max_carry > left_bytes)
191       av->consumption_since_last_update__ = -left_bytes;
192     else
193       av->consumption_since_last_update__ = -max_carry;
194   }
195 #if DEBUG_BANDWIDTH
196   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197               "Tracker %p  updated, have %u Bps, last update was %llu ms ago\n",
198               av, (unsigned int) av->available_bytes_per_s__,
199               (unsigned long long) delta_time);
200 #endif
201
202 }
203
204
205 /**
206  * Notify the tracker that a certain number of bytes of bandwidth have
207  * been consumed.  Note that it is legal to consume bytes even if not
208  * enough bandwidth is available (in that case,
209  * GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
210  * even for a size of zero for a while).
211  *
212  * @param av tracker to update
213  * @param size number of bytes consumed
214  * @return GNUNET_YES if this consumption is above the limit
215  */
216 int
217 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
218                                   ssize_t size)
219 {
220   int64_t nc;
221
222 #if DEBUG_BANDWIDTH
223   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p consumes %d bytes\n", av,
224               (int) size);
225 #endif
226   if (size > 0)
227   {
228     nc = av->consumption_since_last_update__ + size;
229     if (nc < av->consumption_since_last_update__)
230     {
231       GNUNET_break (0);
232       return GNUNET_SYSERR;
233     }
234     av->consumption_since_last_update__ = nc;
235     update_tracker (av);
236     if (av->consumption_since_last_update__ > 0)
237     {
238 #if DEBUG_BANDWIDTH
239       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240                   "Tracker %p consumption %llu bytes above limit\n", av,
241                   (unsigned long long) av->consumption_since_last_update__);
242 #endif
243       return GNUNET_YES;
244     }
245   }
246   else
247   {
248     av->consumption_since_last_update__ += size;
249   }
250   return GNUNET_NO;
251 }
252
253
254 /**
255  * Compute how long we should wait until consuming 'size'
256  * bytes of bandwidth in order to stay within the given
257  * quota.
258  *
259  * @param av tracker to query
260  * @param size number of bytes we would like to consume
261  * @return time in ms to wait for consumption to be OK
262  */
263 struct GNUNET_TIME_Relative
264 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
265                                     size_t size)
266 {
267   struct GNUNET_TIME_Relative ret;
268   int64_t bytes_needed;
269
270   if (av->available_bytes_per_s__ == 0)
271   {
272 #if DEBUG_BANDWIDTH
273     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay is infinity\n", av);
274 #endif
275     return GNUNET_TIME_UNIT_FOREVER_REL;
276   }
277   update_tracker (av);
278   bytes_needed = size + av->consumption_since_last_update__;
279   if (bytes_needed <= 0)
280   {
281 #if DEBUG_BANDWIDTH
282     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
283                 "Tracker %p delay for %u bytes is zero\n", av,
284                 (unsigned int) size);
285 #endif
286     return GNUNET_TIME_UNIT_ZERO;
287   }
288   ret.rel_value =
289       1000LL * bytes_needed / (unsigned long long) av->available_bytes_per_s__;
290 #if DEBUG_BANDWIDTH
291   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292               "Tracker %p delay for %u bytes is %llu ms\n", av,
293               (unsigned int) size, (unsigned long long) ret.rel_value);
294 #endif
295   return ret;
296 }
297
298
299 /**
300  * Compute how many bytes are available for consumption right now.
301  * quota.
302  *
303  * @param av tracker to query
304  * @return number of bytes available for consumption right now
305  */
306 int64_t
307 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker * av)
308 {
309   struct GNUNET_BANDWIDTH_Value32NBO bps;
310   uint64_t avail;
311   int64_t used;
312
313   update_tracker (av);
314   bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__);
315   avail =
316       GNUNET_BANDWIDTH_value_get_available_until (bps,
317                                                   GNUNET_TIME_absolute_get_duration
318                                                   (av->last_update__));
319   used = av->consumption_since_last_update__;
320 #if DEBUG_BANDWIDTH
321   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322               "Tracker %p  available bandwidth is %lld bytes\n", av,
323               (long long) (int64_t) (avail - used));
324 #endif
325   return (int64_t) (avail - used);
326 }
327
328
329 /**
330  * Update quota of bandwidth tracker.
331  *
332  * @param av tracker to initialize
333  * @param bytes_per_second_limit new limit to assume
334  */
335 void
336 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
337                                        struct GNUNET_BANDWIDTH_Value32NBO
338                                        bytes_per_second_limit)
339 {
340   uint32_t old_limit;
341   uint32_t new_limit;
342
343   new_limit = ntohl (bytes_per_second_limit.value__);
344 #if DEBUG_BANDWIDTH
345   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346               "Tracker %p bandwidth changed to %u Bps\n", av,
347               (unsigned int) new_limit);
348 #endif
349   update_tracker (av);
350   old_limit = av->available_bytes_per_s__;
351   av->available_bytes_per_s__ = new_limit;
352   if (old_limit > new_limit)
353     update_tracker (av);        /* maximum excess might be less now */
354 }
355
356
357 /* end of bandwidth.c */