wrong if
[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         {
160           left_time_ms = left_bytes / avail_per_ms;       
161           if (left_time_ms > ((unsigned long long) av->max_carry_s__) * 1000LL)
162             {
163               /* need to limit accumulation of unused bandwidth */
164               left_time_ms = ((unsigned long long) av->max_carry_s__) * 1000LL;
165               if (left_time_ms * avail_per_ms < GNUNET_SERVER_MAX_MESSAGE_SIZE)
166                 {
167                   /* need to still allow GNUNET_SERVER_MAX_MESSAGE_SIZE accumulation */
168                   if (left_bytes > GNUNET_SERVER_MAX_MESSAGE_SIZE)
169                     left_bytes = GNUNET_SERVER_MAX_MESSAGE_SIZE;
170                   left_time_ms = left_bytes / avail_per_ms;
171                 }
172             }
173         }
174       else
175         {
176            left_time_ms = 0;
177         }
178       av->consumption_since_last_update__ = 0;
179       av->last_update__.value = now.value - left_time_ms;
180     }
181 }
182
183
184 /**
185  * Notify the tracker that a certain number of bytes of bandwidth have
186  * been consumed.  Note that it is legal to consume bytes even if not
187  * enough bandwidth is available (in that case,
188  * GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
189  * even for a size of zero for a while).
190  *
191  * @param av tracker to update
192  * @param size number of bytes consumed
193  * @return GNUNET_YES if this consumption is above the limit
194  */
195 int
196 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
197                                   ssize_t size)
198 {
199   uint64_t nc;
200
201   if (size > 0)
202     {
203       nc = av->consumption_since_last_update__ + size;
204       if (nc < av->consumption_since_last_update__) 
205         {
206           GNUNET_break (0);
207           return GNUNET_SYSERR;
208         }
209       av->consumption_since_last_update__ = nc;
210       update_tracker (av);
211       if (av->consumption_since_last_update__ > 0)
212         return GNUNET_YES;
213     }
214   else
215     {
216       av->last_update__.value -= (size * av->available_bytes_per_s__) / 1000LL;
217       update_tracker (av);
218     }
219   return GNUNET_NO;
220 }
221
222
223 /**
224  * Compute how long we should wait until consuming 'size'
225  * bytes of bandwidth in order to stay within the given
226  * quota.
227  *
228  * @param av tracker to query
229  * @param size number of bytes we would like to consume
230  * @return time to wait for consumption to be OK
231  */
232 struct GNUNET_TIME_Relative
233 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
234                                     size_t size)
235 {
236   struct GNUNET_TIME_Relative ret;
237   struct GNUNET_TIME_Absolute now;
238   uint64_t delta_avail;
239   uint64_t delta_time;
240   uint64_t bytes_needed;
241
242   if (av->available_bytes_per_s__ == 0)
243     return GNUNET_TIME_UNIT_FOREVER_REL;
244   update_tracker (av);
245   now = GNUNET_TIME_absolute_get ();
246   delta_time = now.value - av->last_update__.value;
247   delta_avail = (delta_time * ((unsigned long long) av->available_bytes_per_s__)) / 1000LL;
248   if (delta_avail >= size)
249     return GNUNET_TIME_UNIT_ZERO;
250   bytes_needed = size - delta_avail;
251   ret.value = 1000LL * bytes_needed / (unsigned long long) av->available_bytes_per_s__;
252   return ret;
253 }
254
255
256 /**
257  * Compute how many bytes are available for consumption right now.
258  * quota.
259  *
260  * @param av tracker to query
261  * @return number of bytes available for consumption right now
262  */
263 int64_t 
264 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker *av)
265 {
266   struct GNUNET_BANDWIDTH_Value32NBO bps;
267   uint64_t avail;
268   uint64_t used;
269
270   update_tracker (av);
271   bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__);
272   avail = GNUNET_BANDWIDTH_value_get_available_until (bps,
273                                                       GNUNET_TIME_absolute_get_duration (av->last_update__));
274   used = av->consumption_since_last_update__;
275   return (int64_t) (avail - used);
276 }
277
278
279 /**
280  * Update quota of bandwidth tracker.
281  *
282  * @param av tracker to initialize
283  * @param bytes_per_second_limit new limit to assume
284  */
285 void
286 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
287                                        struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit)
288 {
289   uint32_t old_limit;
290   uint32_t new_limit;
291
292   new_limit = ntohl (bytes_per_second_limit.value__);
293   update_tracker (av);
294   old_limit = av->available_bytes_per_s__;
295   av->available_bytes_per_s__ = new_limit;
296   if (old_limit > new_limit)
297     update_tracker (av); /* maximum excess might be less now */
298 }
299
300
301 /* end of bandwidth.c */