summaryrefslogtreecommitdiff
path: root/src/util/bandwidth.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/bandwidth.c')
-rw-r--r--src/util/bandwidth.c376
1 files changed, 188 insertions, 188 deletions
diff --git a/src/util/bandwidth.c b/src/util/bandwidth.c
index 562eed01b..1f889d678 100644
--- a/src/util/bandwidth.c
+++ b/src/util/bandwidth.c
@@ -27,7 +27,7 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28 28
29 29
30#define LOG(kind, ...) GNUNET_log_from(kind, "util-bandwidth", __VA_ARGS__) 30#define LOG(kind, ...) GNUNET_log_from (kind, "util-bandwidth", __VA_ARGS__)
31 31
32/** 32/**
33 * Create a new bandwidth value. 33 * Create a new bandwidth value.
@@ -36,11 +36,11 @@
36 * @return the new bandwidth value 36 * @return the new bandwidth value
37 */ 37 */
38struct GNUNET_BANDWIDTH_Value32NBO 38struct GNUNET_BANDWIDTH_Value32NBO
39GNUNET_BANDWIDTH_value_init(uint32_t bytes_per_second) 39GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second)
40{ 40{
41 struct GNUNET_BANDWIDTH_Value32NBO ret; 41 struct GNUNET_BANDWIDTH_Value32NBO ret;
42 42
43 ret.value__ = htonl(bytes_per_second); 43 ret.value__ = htonl (bytes_per_second);
44 return ret; 44 return ret;
45} 45}
46 46
@@ -53,11 +53,11 @@ GNUNET_BANDWIDTH_value_init(uint32_t bytes_per_second)
53 * @return the min of b1 and b2 53 * @return the min of b1 and b2
54 */ 54 */
55struct GNUNET_BANDWIDTH_Value32NBO 55struct GNUNET_BANDWIDTH_Value32NBO
56GNUNET_BANDWIDTH_value_min(struct GNUNET_BANDWIDTH_Value32NBO b1, 56GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
57 struct GNUNET_BANDWIDTH_Value32NBO b2) 57 struct GNUNET_BANDWIDTH_Value32NBO b2)
58{ 58{
59 return GNUNET_BANDWIDTH_value_init( 59 return GNUNET_BANDWIDTH_value_init (
60 GNUNET_MIN(ntohl(b1.value__), ntohl(b2.value__))); 60 GNUNET_MIN (ntohl (b1.value__), ntohl (b2.value__)));
61} 61}
62 62
63 63
@@ -69,11 +69,11 @@ GNUNET_BANDWIDTH_value_min(struct GNUNET_BANDWIDTH_Value32NBO b1,
69 * @return the min of b1 and b2 69 * @return the min of b1 and b2
70 */ 70 */
71struct GNUNET_BANDWIDTH_Value32NBO 71struct GNUNET_BANDWIDTH_Value32NBO
72GNUNET_BANDWIDTH_value_max(struct GNUNET_BANDWIDTH_Value32NBO b1, 72GNUNET_BANDWIDTH_value_max (struct GNUNET_BANDWIDTH_Value32NBO b1,
73 struct GNUNET_BANDWIDTH_Value32NBO b2) 73 struct GNUNET_BANDWIDTH_Value32NBO b2)
74{ 74{
75 return GNUNET_BANDWIDTH_value_init( 75 return GNUNET_BANDWIDTH_value_init (
76 GNUNET_MAX(ntohl(b1.value__), ntohl(b2.value__))); 76 GNUNET_MAX (ntohl (b1.value__), ntohl (b2.value__)));
77} 77}
78 78
79 79
@@ -85,10 +85,10 @@ GNUNET_BANDWIDTH_value_max(struct GNUNET_BANDWIDTH_Value32NBO b1,
85 * @return the sum of b1 and b2 85 * @return the sum of b1 and b2
86 */ 86 */
87struct GNUNET_BANDWIDTH_Value32NBO 87struct GNUNET_BANDWIDTH_Value32NBO
88GNUNET_BANDWIDTH_value_sum(struct GNUNET_BANDWIDTH_Value32NBO b1, 88GNUNET_BANDWIDTH_value_sum (struct GNUNET_BANDWIDTH_Value32NBO b1,
89 struct GNUNET_BANDWIDTH_Value32NBO b2) 89 struct GNUNET_BANDWIDTH_Value32NBO b2)
90{ 90{
91 return GNUNET_BANDWIDTH_value_init(ntohl(b1.value__) + ntohl(b2.value__)); 91 return GNUNET_BANDWIDTH_value_init (ntohl (b1.value__) + ntohl (b2.value__));
92} 92}
93 93
94 94
@@ -101,18 +101,18 @@ GNUNET_BANDWIDTH_value_sum(struct GNUNET_BANDWIDTH_Value32NBO b1,
101 * @return number of bytes available at bps until deadline 101 * @return number of bytes available at bps until deadline
102 */ 102 */
103uint64_t 103uint64_t
104GNUNET_BANDWIDTH_value_get_available_until( 104GNUNET_BANDWIDTH_value_get_available_until (
105 struct GNUNET_BANDWIDTH_Value32NBO bps, 105 struct GNUNET_BANDWIDTH_Value32NBO bps,
106 struct GNUNET_TIME_Relative deadline) 106 struct GNUNET_TIME_Relative deadline)
107{ 107{
108 uint64_t b; 108 uint64_t b;
109 109
110 b = ntohl(bps.value__); 110 b = ntohl (bps.value__);
111 LOG(GNUNET_ERROR_TYPE_DEBUG, 111 LOG (GNUNET_ERROR_TYPE_DEBUG,
112 "Bandwidth has %llu bytes available until deadline in %s\n", 112 "Bandwidth has %llu bytes available until deadline in %s\n",
113 (unsigned long long)((b * deadline.rel_value_us + 500000LL) / 113 (unsigned long long) ((b * deadline.rel_value_us + 500000LL)
114 1000000LL), 114 / 1000000LL),
115 GNUNET_STRINGS_relative_time_to_string(deadline, GNUNET_YES)); 115 GNUNET_STRINGS_relative_time_to_string (deadline, GNUNET_YES));
116 return (b * deadline.rel_value_us + 500000LL) / 1000000LL; 116 return (b * deadline.rel_value_us + 500000LL) / 1000000LL;
117} 117}
118 118
@@ -126,24 +126,24 @@ GNUNET_BANDWIDTH_value_get_available_until(
126 * @return how long it would take 126 * @return how long it would take
127 */ 127 */
128struct GNUNET_TIME_Relative 128struct GNUNET_TIME_Relative
129GNUNET_BANDWIDTH_value_get_delay_for(struct GNUNET_BANDWIDTH_Value32NBO bps, 129GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
130 uint64_t size) 130 uint64_t size)
131{ 131{
132 uint64_t b; 132 uint64_t b;
133 struct GNUNET_TIME_Relative ret; 133 struct GNUNET_TIME_Relative ret;
134 134
135 b = ntohl(bps.value__); 135 b = ntohl (bps.value__);
136 if (0 == b) 136 if (0 == b)
137 { 137 {
138 LOG(GNUNET_ERROR_TYPE_DEBUG, 138 LOG (GNUNET_ERROR_TYPE_DEBUG,
139 "Bandwidth suggests delay of infinity (zero bandwidth)\n"); 139 "Bandwidth suggests delay of infinity (zero bandwidth)\n");
140 return GNUNET_TIME_UNIT_FOREVER_REL; 140 return GNUNET_TIME_UNIT_FOREVER_REL;
141 } 141 }
142 ret.rel_value_us = size * 1000LL * 1000LL / b; 142 ret.rel_value_us = size * 1000LL * 1000LL / b;
143 LOG(GNUNET_ERROR_TYPE_DEBUG, 143 LOG (GNUNET_ERROR_TYPE_DEBUG,
144 "Bandwidth suggests delay of %s for %llu bytes of traffic\n", 144 "Bandwidth suggests delay of %s for %llu bytes of traffic\n",
145 GNUNET_STRINGS_relative_time_to_string(ret, GNUNET_YES), 145 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES),
146 (unsigned long long)size); 146 (unsigned long long) size);
147 return ret; 147 return ret;
148} 148}
149 149
@@ -154,17 +154,17 @@ GNUNET_BANDWIDTH_value_get_delay_for(struct GNUNET_BANDWIDTH_Value32NBO bps,
154 * @param cls the `struct GNUNET_BANDWIDTH_Tracker` 154 * @param cls the `struct GNUNET_BANDWIDTH_Tracker`
155 */ 155 */
156static void 156static void
157excess_trigger(void *cls) 157excess_trigger (void *cls)
158{ 158{
159 struct GNUNET_BANDWIDTH_Tracker *av = cls; 159 struct GNUNET_BANDWIDTH_Tracker *av = cls;
160 160
161 av->excess_task = NULL; 161 av->excess_task = NULL;
162 if (NULL != av->excess_cb) 162 if (NULL != av->excess_cb)
163 { 163 {
164 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165 "Notifying application about excess bandwidth\n"); 165 "Notifying application about excess bandwidth\n");
166 av->excess_cb(av->excess_cb_cls); 166 av->excess_cb (av->excess_cb_cls);
167 } 167 }
168} 168}
169 169
170 170
@@ -172,7 +172,7 @@ excess_trigger(void *cls)
172 * Recalculate when we might need to call the excess callback. 172 * Recalculate when we might need to call the excess callback.
173 */ 173 */
174static void 174static void
175update_excess(struct GNUNET_BANDWIDTH_Tracker *av) 175update_excess (struct GNUNET_BANDWIDTH_Tracker *av)
176{ 176{
177 struct GNUNET_TIME_Relative delay; 177 struct GNUNET_TIME_Relative delay;
178 struct GNUNET_TIME_Absolute now; 178 struct GNUNET_TIME_Absolute now;
@@ -184,53 +184,53 @@ update_excess(struct GNUNET_BANDWIDTH_Tracker *av)
184 184
185 if (NULL == av->excess_cb) 185 if (NULL == av->excess_cb)
186 return; /* nothing to do */ 186 return; /* nothing to do */
187 now = GNUNET_TIME_absolute_get(); 187 now = GNUNET_TIME_absolute_get ();
188 delta_time = now.abs_value_us - av->last_update__.abs_value_us; 188 delta_time = now.abs_value_us - av->last_update__.abs_value_us;
189 delta_avail = 189 delta_avail =
190 (delta_time * ((unsigned long long)av->available_bytes_per_s__) + 190 (delta_time * ((unsigned long long) av->available_bytes_per_s__)
191 500000LL) / 191 + 500000LL)
192 1000000LL; 192 / 1000000LL;
193 current_consumption = av->consumption_since_last_update__ - delta_avail; 193 current_consumption = av->consumption_since_last_update__ - delta_avail;
194 if (current_consumption > av->consumption_since_last_update__) 194 if (current_consumption > av->consumption_since_last_update__)
195 { 195 {
196 /* integer underflow, cap! */ 196 /* integer underflow, cap! */
197 current_consumption = INT64_MIN; 197 current_consumption = INT64_MIN;
198 } 198 }
199 /* negative current_consumption means that we have savings */ 199 /* negative current_consumption means that we have savings */
200 max_carry = ((uint64_t)av->available_bytes_per_s__) * av->max_carry_s__; 200 max_carry = ((uint64_t) av->available_bytes_per_s__) * av->max_carry_s__;
201 if (max_carry < GNUNET_MAX_MESSAGE_SIZE) 201 if (max_carry < GNUNET_MAX_MESSAGE_SIZE)
202 max_carry = GNUNET_MAX_MESSAGE_SIZE; 202 max_carry = GNUNET_MAX_MESSAGE_SIZE;
203 if (max_carry > INT64_MAX) 203 if (max_carry > INT64_MAX)
204 max_carry = INT64_MAX; 204 max_carry = INT64_MAX;
205 left_bytes = current_consumption + max_carry; 205 left_bytes = current_consumption + max_carry;
206 if (left_bytes < current_consumption) 206 if (left_bytes < current_consumption)
207 { 207 {
208 /* integer overflow, cap! */ 208 /* integer overflow, cap! */
209 left_bytes = INT64_MAX; 209 left_bytes = INT64_MAX;
210 } 210 }
211 /* left_bytes now contains the number of bytes needed until 211 /* left_bytes now contains the number of bytes needed until
212 we have more savings than allowed */ 212 we have more savings than allowed */
213 if (left_bytes < 0) 213 if (left_bytes < 0)
214 { 214 {
215 /* having excess already */ 215 /* having excess already */
216 delay = GNUNET_TIME_UNIT_ZERO; 216 delay = GNUNET_TIME_UNIT_ZERO;
217 } 217 }
218 else 218 else
219 { 219 {
220 double factor = 1.0 * left_bytes / (double)av->available_bytes_per_s__; 220 double factor = 1.0 * left_bytes / (double) av->available_bytes_per_s__;
221 delay = 221 delay =
222 GNUNET_TIME_relative_saturating_multiply(GNUNET_TIME_UNIT_SECONDS, 222 GNUNET_TIME_relative_saturating_multiply (GNUNET_TIME_UNIT_SECONDS,
223 (unsigned long long)factor); 223 (unsigned long long) factor);
224 } 224 }
225 GNUNET_log( 225 GNUNET_log (
226 GNUNET_ERROR_TYPE_DEBUG, 226 GNUNET_ERROR_TYPE_DEBUG,
227 "At %llu bps it will take us %s for %lld bytes to reach excess threshold\n", 227 "At %llu bps it will take us %s for %lld bytes to reach excess threshold\n",
228 (unsigned long long)av->available_bytes_per_s__, 228 (unsigned long long) av->available_bytes_per_s__,
229 GNUNET_STRINGS_relative_time_to_string(delay, GNUNET_NO), 229 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO),
230 (long long)left_bytes); 230 (long long) left_bytes);
231 if (NULL != av->excess_task) 231 if (NULL != av->excess_task)
232 GNUNET_SCHEDULER_cancel(av->excess_task); 232 GNUNET_SCHEDULER_cancel (av->excess_task);
233 av->excess_task = GNUNET_SCHEDULER_add_delayed(delay, &excess_trigger, av); 233 av->excess_task = GNUNET_SCHEDULER_add_delayed (delay, &excess_trigger, av);
234} 234}
235 235
236 236
@@ -256,7 +256,7 @@ update_excess(struct GNUNET_BANDWIDTH_Tracker *av)
256 * @param excess_cb_cls closure for @a excess_cb 256 * @param excess_cb_cls closure for @a excess_cb
257 */ 257 */
258void 258void
259GNUNET_BANDWIDTH_tracker_init2( 259GNUNET_BANDWIDTH_tracker_init2 (
260 struct GNUNET_BANDWIDTH_Tracker *av, 260 struct GNUNET_BANDWIDTH_Tracker *av,
261 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb, 261 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb,
262 void *update_cb_cls, 262 void *update_cb_cls,
@@ -268,17 +268,17 @@ GNUNET_BANDWIDTH_tracker_init2(
268 av->update_cb = update_cb; 268 av->update_cb = update_cb;
269 av->update_cb_cls = update_cb_cls; 269 av->update_cb_cls = update_cb_cls;
270 av->consumption_since_last_update__ = 0; 270 av->consumption_since_last_update__ = 0;
271 av->last_update__ = GNUNET_TIME_absolute_get(); 271 av->last_update__ = GNUNET_TIME_absolute_get ();
272 av->available_bytes_per_s__ = ntohl(bytes_per_second_limit.value__); 272 av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__);
273 av->max_carry_s__ = max_carry_s; 273 av->max_carry_s__ = max_carry_s;
274 av->excess_cb = excess_cb; 274 av->excess_cb = excess_cb;
275 av->excess_cb_cls = excess_cb_cls; 275 av->excess_cb_cls = excess_cb_cls;
276 LOG(GNUNET_ERROR_TYPE_DEBUG, 276 LOG (GNUNET_ERROR_TYPE_DEBUG,
277 "Tracker %p initialized with %u Bps and max carry %u\n", 277 "Tracker %p initialized with %u Bps and max carry %u\n",
278 av, 278 av,
279 (unsigned int)av->available_bytes_per_s__, 279 (unsigned int) av->available_bytes_per_s__,
280 (unsigned int)max_carry_s); 280 (unsigned int) max_carry_s);
281 update_excess(av); 281 update_excess (av);
282} 282}
283 283
284 284
@@ -299,20 +299,20 @@ GNUNET_BANDWIDTH_tracker_init2(
299 * may accumulate before it expires 299 * may accumulate before it expires
300 */ 300 */
301void 301void
302GNUNET_BANDWIDTH_tracker_init( 302GNUNET_BANDWIDTH_tracker_init (
303 struct GNUNET_BANDWIDTH_Tracker *av, 303 struct GNUNET_BANDWIDTH_Tracker *av,
304 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb, 304 GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb,
305 void *update_cb_cls, 305 void *update_cb_cls,
306 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit, 306 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit,
307 uint32_t max_carry_s) 307 uint32_t max_carry_s)
308{ 308{
309 GNUNET_BANDWIDTH_tracker_init2(av, 309 GNUNET_BANDWIDTH_tracker_init2 (av,
310 update_cb, 310 update_cb,
311 update_cb_cls, 311 update_cb_cls,
312 bytes_per_second_limit, 312 bytes_per_second_limit,
313 max_carry_s, 313 max_carry_s,
314 NULL, 314 NULL,
315 NULL); 315 NULL);
316} 316}
317 317
318 318
@@ -322,10 +322,10 @@ GNUNET_BANDWIDTH_tracker_init(
322 * @param av the respective trackers 322 * @param av the respective trackers
323 */ 323 */
324void 324void
325GNUNET_BANDWIDTH_tracker_notification_stop(struct GNUNET_BANDWIDTH_Tracker *av) 325GNUNET_BANDWIDTH_tracker_notification_stop (struct GNUNET_BANDWIDTH_Tracker *av)
326{ 326{
327 if (NULL != av->excess_task) 327 if (NULL != av->excess_task)
328 GNUNET_SCHEDULER_cancel(av->excess_task); 328 GNUNET_SCHEDULER_cancel (av->excess_task);
329 av->excess_task = NULL; 329 av->excess_task = NULL;
330 av->excess_cb = NULL; 330 av->excess_cb = NULL;
331 av->excess_cb_cls = NULL; 331 av->excess_cb_cls = NULL;
@@ -341,7 +341,7 @@ GNUNET_BANDWIDTH_tracker_notification_stop(struct GNUNET_BANDWIDTH_Tracker *av)
341 * @param av tracker to update 341 * @param av tracker to update
342 */ 342 */
343static void 343static void
344update_tracker(struct GNUNET_BANDWIDTH_Tracker *av) 344update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
345{ 345{
346 struct GNUNET_TIME_Absolute now; 346 struct GNUNET_TIME_Absolute now;
347 uint64_t delta_time; 347 uint64_t delta_time;
@@ -349,39 +349,39 @@ update_tracker(struct GNUNET_BANDWIDTH_Tracker *av)
349 uint64_t left_bytes; 349 uint64_t left_bytes;
350 uint64_t max_carry; 350 uint64_t max_carry;
351 351
352 now = GNUNET_TIME_absolute_get(); 352 now = GNUNET_TIME_absolute_get ();
353 delta_time = now.abs_value_us - av->last_update__.abs_value_us; 353 delta_time = now.abs_value_us - av->last_update__.abs_value_us;
354 delta_avail = 354 delta_avail =
355 (delta_time * ((unsigned long long)av->available_bytes_per_s__) + 355 (delta_time * ((unsigned long long) av->available_bytes_per_s__)
356 500000LL) / 356 + 500000LL)
357 1000000LL; 357 / 1000000LL;
358 av->consumption_since_last_update__ -= delta_avail; 358 av->consumption_since_last_update__ -= delta_avail;
359 av->last_update__ = now; 359 av->last_update__ = now;
360 if (av->consumption_since_last_update__ < 0) 360 if (av->consumption_since_last_update__ < 0)
361 { 361 {
362 left_bytes = -av->consumption_since_last_update__; 362 left_bytes = -av->consumption_since_last_update__;
363 max_carry = 363 max_carry =
364 ((unsigned long long)av->available_bytes_per_s__) * av->max_carry_s__; 364 ((unsigned long long) av->available_bytes_per_s__) * av->max_carry_s__;
365 if (max_carry < GNUNET_MAX_MESSAGE_SIZE) 365 if (max_carry < GNUNET_MAX_MESSAGE_SIZE)
366 max_carry = GNUNET_MAX_MESSAGE_SIZE; 366 max_carry = GNUNET_MAX_MESSAGE_SIZE;
367 if (max_carry > INT64_MAX) 367 if (max_carry > INT64_MAX)
368 max_carry = INT64_MAX; 368 max_carry = INT64_MAX;
369 if (max_carry > left_bytes) 369 if (max_carry > left_bytes)
370 av->consumption_since_last_update__ = -left_bytes; 370 av->consumption_since_last_update__ = -left_bytes;
371 else 371 else
372 av->consumption_since_last_update__ = -max_carry; 372 av->consumption_since_last_update__ = -max_carry;
373 } 373 }
374#if !defined(GNUNET_CULL_LOGGING) 374#if ! defined(GNUNET_CULL_LOGGING)
375 { 375 {
376 struct GNUNET_TIME_Relative delta; 376 struct GNUNET_TIME_Relative delta;
377 377
378 delta.rel_value_us = delta_time; 378 delta.rel_value_us = delta_time;
379 LOG(GNUNET_ERROR_TYPE_DEBUG, 379 LOG (GNUNET_ERROR_TYPE_DEBUG,
380 "Tracker %p updated, consumption at %lld at %u Bps, last update was %s ago\n", 380 "Tracker %p updated, consumption at %lld at %u Bps, last update was %s ago\n",
381 av, 381 av,
382 (long long)av->consumption_since_last_update__, 382 (long long) av->consumption_since_last_update__,
383 (unsigned int)av->available_bytes_per_s__, 383 (unsigned int) av->available_bytes_per_s__,
384 GNUNET_STRINGS_relative_time_to_string(delta, GNUNET_YES)); 384 GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_YES));
385 } 385 }
386#endif 386#endif
387} 387}
@@ -399,48 +399,48 @@ update_tracker(struct GNUNET_BANDWIDTH_Tracker *av)
399 * @return #GNUNET_YES if this consumption is above the limit 399 * @return #GNUNET_YES if this consumption is above the limit
400 */ 400 */
401int 401int
402GNUNET_BANDWIDTH_tracker_consume(struct GNUNET_BANDWIDTH_Tracker *av, 402GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
403 ssize_t size) 403 ssize_t size)
404{ 404{
405 int64_t nc; 405 int64_t nc;
406 406
407 LOG(GNUNET_ERROR_TYPE_DEBUG, 407 LOG (GNUNET_ERROR_TYPE_DEBUG,
408 "Tracker %p consumes %d bytes\n", 408 "Tracker %p consumes %d bytes\n",
409 av, 409 av,
410 (int)size); 410 (int) size);
411 if (size > 0) 411 if (size > 0)
412 {
413 nc = av->consumption_since_last_update__ + size;
414 if (nc < av->consumption_since_last_update__)
412 { 415 {
413 nc = av->consumption_since_last_update__ + size; 416 /* integer overflow, very bad */
414 if (nc < av->consumption_since_last_update__) 417 GNUNET_break (0);
415 { 418 return GNUNET_SYSERR;
416 /* integer overflow, very bad */
417 GNUNET_break(0);
418 return GNUNET_SYSERR;
419 }
420 av->consumption_since_last_update__ = nc;
421 update_tracker(av);
422 update_excess(av);
423 if (av->consumption_since_last_update__ > 0)
424 {
425 LOG(GNUNET_ERROR_TYPE_DEBUG,
426 "Tracker %p consumption %llu bytes above limit\n",
427 av,
428 (unsigned long long)av->consumption_since_last_update__);
429 return GNUNET_YES;
430 }
431 } 419 }
420 av->consumption_since_last_update__ = nc;
421 update_tracker (av);
422 update_excess (av);
423 if (av->consumption_since_last_update__ > 0)
424 {
425 LOG (GNUNET_ERROR_TYPE_DEBUG,
426 "Tracker %p consumption %llu bytes above limit\n",
427 av,
428 (unsigned long long) av->consumption_since_last_update__);
429 return GNUNET_YES;
430 }
431 }
432 else 432 else
433 {
434 nc = av->consumption_since_last_update__ + size;
435 if (nc > av->consumption_since_last_update__)
433 { 436 {
434 nc = av->consumption_since_last_update__ + size; 437 /* integer underflow, very bad */
435 if (nc > av->consumption_since_last_update__) 438 GNUNET_break (0);
436 { 439 return GNUNET_SYSERR;
437 /* integer underflow, very bad */
438 GNUNET_break(0);
439 return GNUNET_SYSERR;
440 }
441 av->consumption_since_last_update__ = nc;
442 update_excess(av);
443 } 440 }
441 av->consumption_since_last_update__ = nc;
442 update_excess (av);
443 }
444 return GNUNET_NO; 444 return GNUNET_NO;
445} 445}
446 446
@@ -455,34 +455,34 @@ GNUNET_BANDWIDTH_tracker_consume(struct GNUNET_BANDWIDTH_Tracker *av,
455 * @return time in ms to wait for consumption to be OK 455 * @return time in ms to wait for consumption to be OK
456 */ 456 */
457struct GNUNET_TIME_Relative 457struct GNUNET_TIME_Relative
458GNUNET_BANDWIDTH_tracker_get_delay(struct GNUNET_BANDWIDTH_Tracker *av, 458GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
459 size_t size) 459 size_t size)
460{ 460{
461 struct GNUNET_TIME_Relative ret; 461 struct GNUNET_TIME_Relative ret;
462 int64_t bytes_needed; 462 int64_t bytes_needed;
463 463
464 if (0 == av->available_bytes_per_s__) 464 if (0 == av->available_bytes_per_s__)
465 { 465 {
466 LOG(GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay is infinity\n", av); 466 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay is infinity\n", av);
467 return GNUNET_TIME_UNIT_FOREVER_REL; 467 return GNUNET_TIME_UNIT_FOREVER_REL;
468 } 468 }
469 update_tracker(av); 469 update_tracker (av);
470 bytes_needed = size + av->consumption_since_last_update__; 470 bytes_needed = size + av->consumption_since_last_update__;
471 if (bytes_needed <= 0) 471 if (bytes_needed <= 0)
472 { 472 {
473 LOG(GNUNET_ERROR_TYPE_DEBUG, 473 LOG (GNUNET_ERROR_TYPE_DEBUG,
474 "Tracker %p delay for %u bytes is zero\n", 474 "Tracker %p delay for %u bytes is zero\n",
475 av, 475 av,
476 (unsigned int)size); 476 (unsigned int) size);
477 return GNUNET_TIME_UNIT_ZERO; 477 return GNUNET_TIME_UNIT_ZERO;
478 } 478 }
479 ret.rel_value_us = (1000LL * 1000LL * bytes_needed) / 479 ret.rel_value_us = (1000LL * 1000LL * bytes_needed)
480 (unsigned long long)av->available_bytes_per_s__; 480 / (unsigned long long) av->available_bytes_per_s__;
481 LOG(GNUNET_ERROR_TYPE_DEBUG, 481 LOG (GNUNET_ERROR_TYPE_DEBUG,
482 "Tracker %p delay for %u bytes is %s\n", 482 "Tracker %p delay for %u bytes is %s\n",
483 av, 483 av,
484 (unsigned int)size, 484 (unsigned int) size,
485 GNUNET_STRINGS_relative_time_to_string(ret, GNUNET_YES)); 485 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
486 return ret; 486 return ret;
487} 487}
488 488
@@ -495,24 +495,24 @@ GNUNET_BANDWIDTH_tracker_get_delay(struct GNUNET_BANDWIDTH_Tracker *av,
495 * @return number of bytes available for consumption right now 495 * @return number of bytes available for consumption right now
496 */ 496 */
497int64_t 497int64_t
498GNUNET_BANDWIDTH_tracker_get_available(struct GNUNET_BANDWIDTH_Tracker *av) 498GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker *av)
499{ 499{
500 struct GNUNET_BANDWIDTH_Value32NBO bps; 500 struct GNUNET_BANDWIDTH_Value32NBO bps;
501 uint64_t avail; 501 uint64_t avail;
502 int64_t used; 502 int64_t used;
503 503
504 update_tracker(av); 504 update_tracker (av);
505 bps = GNUNET_BANDWIDTH_value_init(av->available_bytes_per_s__); 505 bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__);
506 avail = 506 avail =
507 GNUNET_BANDWIDTH_value_get_available_until(bps, 507 GNUNET_BANDWIDTH_value_get_available_until (bps,
508 GNUNET_TIME_absolute_get_duration( 508 GNUNET_TIME_absolute_get_duration (
509 av->last_update__)); 509 av->last_update__));
510 used = av->consumption_since_last_update__; 510 used = av->consumption_since_last_update__;
511 LOG(GNUNET_ERROR_TYPE_DEBUG, 511 LOG (GNUNET_ERROR_TYPE_DEBUG,
512 "Tracker %p available bandwidth is %lld bytes\n", 512 "Tracker %p available bandwidth is %lld bytes\n",
513 av, 513 av,
514 (long long)(int64_t)(avail - used)); 514 (long long) (int64_t) (avail - used));
515 return (int64_t)(avail - used); 515 return (int64_t) (avail - used);
516} 516}
517 517
518 518
@@ -523,26 +523,26 @@ GNUNET_BANDWIDTH_tracker_get_available(struct GNUNET_BANDWIDTH_Tracker *av)
523 * @param bytes_per_second_limit new limit to assume 523 * @param bytes_per_second_limit new limit to assume
524 */ 524 */
525void 525void
526GNUNET_BANDWIDTH_tracker_update_quota( 526GNUNET_BANDWIDTH_tracker_update_quota (
527 struct GNUNET_BANDWIDTH_Tracker *av, 527 struct GNUNET_BANDWIDTH_Tracker *av,
528 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit) 528 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit)
529{ 529{
530 uint32_t old_limit; 530 uint32_t old_limit;
531 uint32_t new_limit; 531 uint32_t new_limit;
532 532
533 new_limit = ntohl(bytes_per_second_limit.value__); 533 new_limit = ntohl (bytes_per_second_limit.value__);
534 LOG(GNUNET_ERROR_TYPE_DEBUG, 534 LOG (GNUNET_ERROR_TYPE_DEBUG,
535 "Tracker %p bandwidth changed to %u Bps\n", 535 "Tracker %p bandwidth changed to %u Bps\n",
536 av, 536 av,
537 (unsigned int)new_limit); 537 (unsigned int) new_limit);
538 update_tracker(av); 538 update_tracker (av);
539 old_limit = av->available_bytes_per_s__; 539 old_limit = av->available_bytes_per_s__;
540 av->available_bytes_per_s__ = new_limit; 540 av->available_bytes_per_s__ = new_limit;
541 if (NULL != av->update_cb) 541 if (NULL != av->update_cb)
542 av->update_cb(av->update_cb_cls); 542 av->update_cb (av->update_cb_cls);
543 if (old_limit > new_limit) 543 if (old_limit > new_limit)
544 update_tracker(av); /* maximum excess might be less now */ 544 update_tracker (av); /* maximum excess might be less now */
545 update_excess(av); 545 update_excess (av);
546} 546}
547 547
548 548