extending bandwidth tracker api to support notifications
[oweals/gnunet.git] / src / util / bandwidth.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010, 2013 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 3, 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_us + 500000LL) / 1000000LL),
87        GNUNET_STRINGS_relative_time_to_string (deadline, GNUNET_YES));
88   return (b * deadline.rel_value_us + 500000LL) / 1000000LL;
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 (0 == b)
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_us = size * 1000LL * 1000LL / b;
115   LOG (GNUNET_ERROR_TYPE_DEBUG,
116        "Bandwidth suggests delay of %s for %llu bytes of traffic\n",
117        GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES),
118        (unsigned long long) size);
119   return ret;
120 }
121
122
123
124 /**
125  * Initialize bandwidth tracker.  Note that in addition to the
126  * 'max_carry_s' limit, we also always allow at least
127  * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate.  So if the
128  * bytes-per-second limit is so small that within 'max_carry_s' not
129  * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
130  * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
131  * bytes).
132  *
133  * @param av tracker to initialize
134  * @param update_cb callback to notify a client about the tracker being updated
135  * @param update_cb_cls cls for the callback
136  * @param bytes_per_second_limit initial limit to assume
137  * @param max_carry_s maximum number of seconds unused bandwidth
138  *        may accumulate before it expires
139  */
140 void
141 GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
142                                GNUNET_BANDWIDTH_tracker_update_cb update_cb,
143                                void *update_cb_cls,
144                                struct GNUNET_BANDWIDTH_Value32NBO
145                                bytes_per_second_limit, uint32_t max_carry_s)
146 {
147   av->update_cb = update_cb;
148   av->update_cb_cls = update_cb_cls;
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   LOG (GNUNET_ERROR_TYPE_DEBUG,
154        "Tracker %p initialized with %u Bps and max carry %u\n", av,
155        (unsigned int) av->available_bytes_per_s__, (unsigned int) max_carry_s);
156 }
157
158
159 /**
160  * Update the tracker, looking at the current time and
161  * bandwidth consumption data.
162  *
163  * @param av tracker to update
164  */
165 static void
166 update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
167 {
168   struct GNUNET_TIME_Absolute now;
169   struct GNUNET_TIME_Relative delta;
170   uint64_t delta_time;
171   uint64_t delta_avail;
172   uint64_t left_bytes;
173   uint64_t max_carry;
174
175   now = GNUNET_TIME_absolute_get ();
176   delta_time = now.abs_value_us - av->last_update__.abs_value_us;
177   delta_avail =
178       (delta_time * ((unsigned long long) av->available_bytes_per_s__) +
179        500000LL) / 1000000LL;
180   av->consumption_since_last_update__ -= delta_avail;
181   av->last_update__ = now;
182   if (av->consumption_since_last_update__ < 0)
183   {
184     left_bytes = -av->consumption_since_last_update__;
185     max_carry = av->available_bytes_per_s__ * av->max_carry_s__;
186     if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE)
187       max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE;
188     if (max_carry > left_bytes)
189       av->consumption_since_last_update__ = -left_bytes;
190     else
191       av->consumption_since_last_update__ = -max_carry;
192   }
193   delta.rel_value_us = delta_time;
194   LOG (GNUNET_ERROR_TYPE_DEBUG,
195        "Tracker %p updated, have %u Bps, last update was %s ago\n", av,
196        (unsigned int) av->available_bytes_per_s__,
197        GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_YES));
198 }
199
200
201 /**
202  * Notify the tracker that a certain number of bytes of bandwidth have
203  * been consumed.  Note that it is legal to consume bytes even if not
204  * enough bandwidth is available (in that case,
205  * GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
206  * even for a size of zero for a while).
207  *
208  * @param av tracker to update
209  * @param size number of bytes consumed
210  * @return GNUNET_YES if this consumption is above the limit
211  */
212 int
213 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
214                                   ssize_t size)
215 {
216   int64_t nc;
217
218   LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p consumes %d bytes\n", av,
219        (int) size);
220   if (size > 0)
221   {
222     nc = av->consumption_since_last_update__ + size;
223     if (nc < av->consumption_since_last_update__)
224     {
225       GNUNET_break (0);
226       return GNUNET_SYSERR;
227     }
228     av->consumption_since_last_update__ = nc;
229     update_tracker (av);
230     if (av->consumption_since_last_update__ > 0)
231     {
232       LOG (GNUNET_ERROR_TYPE_DEBUG,
233            "Tracker %p consumption %llu bytes above limit\n", av,
234            (unsigned long long) av->consumption_since_last_update__);
235       return GNUNET_YES;
236     }
237   }
238   else
239   {
240     av->consumption_since_last_update__ += size;
241   }
242   return GNUNET_NO;
243 }
244
245
246 /**
247  * Compute how long we should wait until consuming 'size'
248  * bytes of bandwidth in order to stay within the given
249  * quota.
250  *
251  * @param av tracker to query
252  * @param size number of bytes we would like to consume
253  * @return time in ms to wait for consumption to be OK
254  */
255 struct GNUNET_TIME_Relative
256 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
257                                     size_t size)
258 {
259   struct GNUNET_TIME_Relative ret;
260   int64_t bytes_needed;
261
262   if (av->available_bytes_per_s__ == 0)
263   {
264     LOG (GNUNET_ERROR_TYPE_DEBUG,
265          "Tracker %p delay is infinity\n", av);
266     return GNUNET_TIME_UNIT_FOREVER_REL;
267   }
268   update_tracker (av);
269   bytes_needed = size + av->consumption_since_last_update__;
270   if (bytes_needed <= 0)
271   {
272     LOG (GNUNET_ERROR_TYPE_DEBUG,
273          "Tracker %p delay for %u bytes is zero\n", av,
274          (unsigned int) size);
275     return GNUNET_TIME_UNIT_ZERO;
276   }
277   ret.rel_value_us =
278       (1000LL * 1000LL * bytes_needed) /
279       (unsigned long long) av->available_bytes_per_s__;
280   LOG (GNUNET_ERROR_TYPE_DEBUG,
281        "Tracker %p delay for %u bytes is %s\n",
282        av, (unsigned int) size,
283        GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
284   return ret;
285 }
286
287
288 /**
289  * Compute how many bytes are available for consumption right now.
290  * quota.
291  *
292  * @param av tracker to query
293  * @return number of bytes available for consumption right now
294  */
295 int64_t
296 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker * av)
297 {
298   struct GNUNET_BANDWIDTH_Value32NBO bps;
299   uint64_t avail;
300   int64_t used;
301
302   update_tracker (av);
303   bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__);
304   avail =
305       GNUNET_BANDWIDTH_value_get_available_until (bps,
306                                                   GNUNET_TIME_absolute_get_duration
307                                                   (av->last_update__));
308   used = av->consumption_since_last_update__;
309   LOG (GNUNET_ERROR_TYPE_DEBUG,
310        "Tracker %p available bandwidth is %lld bytes\n", av,
311        (long long) (int64_t) (avail - used));
312   return (int64_t) (avail - used);
313 }
314
315
316 /**
317  * Update quota of bandwidth tracker.
318  *
319  * @param av tracker to initialize
320  * @param bytes_per_second_limit new limit to assume
321  */
322 void
323 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
324                                        struct GNUNET_BANDWIDTH_Value32NBO
325                                        bytes_per_second_limit)
326 {
327   uint32_t old_limit;
328   uint32_t new_limit;
329
330   new_limit = ntohl (bytes_per_second_limit.value__);
331   LOG (GNUNET_ERROR_TYPE_DEBUG,
332        "Tracker %p bandwidth changed to %u Bps\n", av,
333        (unsigned int) new_limit);
334   update_tracker (av);
335   old_limit = av->available_bytes_per_s__;
336   av->available_bytes_per_s__ = new_limit;
337   if (NULL != av->update_cb)
338     av->update_cb (av->update_cb_cls);
339   if (old_limit > new_limit)
340     update_tracker (av);        /* maximum excess might be less now */
341 }
342
343
344 /* end of bandwidth.c */