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