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