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