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