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