aboutsummaryrefslogtreecommitdiff
path: root/src/util/bandwidth.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-04-06 11:09:44 +0000
committerChristian Grothoff <christian@grothoff.org>2010-04-06 11:09:44 +0000
commit85a2e0d5b24495e0b2595a76a3205c4970a71fce (patch)
treedb95c013f061d68ae4b6a65e0d196703ea33777a /src/util/bandwidth.c
parent46c8217e87a0be0550b88ae9bafc4760305eabfa (diff)
downloadgnunet-85a2e0d5b24495e0b2595a76a3205c4970a71fce.tar.gz
gnunet-85a2e0d5b24495e0b2595a76a3205c4970a71fce.zip
bwtracking
Diffstat (limited to 'src/util/bandwidth.c')
-rw-r--r--src/util/bandwidth.c94
1 files changed, 90 insertions, 4 deletions
diff --git a/src/util/bandwidth.c b/src/util/bandwidth.c
index 52b0dd0e5..6cca58995 100644
--- a/src/util/bandwidth.c
+++ b/src/util/bandwidth.c
@@ -27,6 +27,8 @@
27#include "gnunet_bandwidth_lib.h" 27#include "gnunet_bandwidth_lib.h"
28#include "gnunet_server_lib.h" 28#include "gnunet_server_lib.h"
29 29
30#define DEBUG_BANDWIDTH GNUNET_YES
31
30/** 32/**
31 * Create a new bandwidth value. 33 * Create a new bandwidth value.
32 * 34 *
@@ -38,6 +40,11 @@ GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second)
38{ 40{
39 struct GNUNET_BANDWIDTH_Value32NBO ret; 41 struct GNUNET_BANDWIDTH_Value32NBO ret;
40 42
43#if DEBUG_BANDWIDTH
44 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
45 "Initializing bandwidth of %u bps\n",
46 (unsigned int) bytes_per_second);
47#endif
41 ret.value__ = htonl (bytes_per_second); 48 ret.value__ = htonl (bytes_per_second);
42 return ret; 49 return ret;
43} 50}
@@ -74,6 +81,12 @@ GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO b
74 uint64_t b; 81 uint64_t b;
75 82
76 b = ntohl (bps.value__); 83 b = ntohl (bps.value__);
84#if DEBUG_BANDWIDTH
85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86 "Bandwidth has %llu bytes available until deadline in %llums\n",
87 (unsigned long long) (b * deadline.value / 1000LL),
88 deadline.value);
89#endif
77 return b * deadline.value / 1000LL; 90 return b * deadline.value / 1000LL;
78} 91}
79 92
@@ -95,8 +108,20 @@ GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
95 108
96 b = ntohl (bps.value__); 109 b = ntohl (bps.value__);
97 if (b == 0) 110 if (b == 0)
98 return GNUNET_TIME_UNIT_FOREVER_REL; 111 {
112#if DEBUG_BANDWIDTH
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
114 "Bandwidth suggests delay of infinity (zero bandwidth)\n");
115#endif
116 return GNUNET_TIME_UNIT_FOREVER_REL;
117 }
99 ret.value = size * 1000LL / b; 118 ret.value = size * 1000LL / b;
119#if DEBUG_BANDWIDTH
120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
121 "Bandwidth suggests delay of %llu ms for %llu bytes of traffic\n",
122 (unsigned long long) ret.value,
123 (unsigned long long) size);
124#endif
100 return ret; 125 return ret;
101} 126}
102 127
@@ -124,6 +149,13 @@ GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
124 av->last_update__ = GNUNET_TIME_absolute_get (); 149 av->last_update__ = GNUNET_TIME_absolute_get ();
125 av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__); 150 av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__);
126 av->max_carry_s__ = max_carry_s; 151 av->max_carry_s__ = max_carry_s;
152#if DEBUG_BANDWIDTH
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
154 "Tracker %p initialized with %u bps and max carry %u\n",
155 av,
156 (unsigned int) av->available_bytes_per_s__,
157 (unsigned int) max_carry_s);
158#endif
127} 159}
128 160
129 161
@@ -178,6 +210,13 @@ update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
178 av->consumption_since_last_update__ = 0; 210 av->consumption_since_last_update__ = 0;
179 av->last_update__.value = now.value - left_time_ms; 211 av->last_update__.value = now.value - left_time_ms;
180 } 212 }
213#if DEBUG_BANDWIDTH
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "Tracker %p updated, have %u bps last update was %llu ms ago\n",
216 av,
217 (unsigned int) av->available_bytes_per_s__,
218 (unsigned long long) (now.value - av->last_update__.value));
219#endif
181} 220}
182 221
183 222
@@ -198,6 +237,12 @@ GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
198{ 237{
199 uint64_t nc; 238 uint64_t nc;
200 239
240#if DEBUG_BANDWIDTH
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242 "Tracker %p consumes %d bytes\n",
243 av,
244 (int) size);
245#endif
201 if (size > 0) 246 if (size > 0)
202 { 247 {
203 nc = av->consumption_since_last_update__ + size; 248 nc = av->consumption_since_last_update__ + size;
@@ -209,7 +254,14 @@ GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
209 av->consumption_since_last_update__ = nc; 254 av->consumption_since_last_update__ = nc;
210 update_tracker (av); 255 update_tracker (av);
211 if (av->consumption_since_last_update__ > 0) 256 if (av->consumption_since_last_update__ > 0)
212 return GNUNET_YES; 257 {
258#if DEBUG_BANDWIDTH
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 "Tracker %p consumption is above limit\n",
261 av);
262#endif
263 return GNUNET_YES;
264 }
213 } 265 }
214 else 266 else
215 { 267 {
@@ -240,15 +292,37 @@ GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
240 uint64_t bytes_needed; 292 uint64_t bytes_needed;
241 293
242 if (av->available_bytes_per_s__ == 0) 294 if (av->available_bytes_per_s__ == 0)
243 return GNUNET_TIME_UNIT_FOREVER_REL; 295 {
296#if DEBUG_BANDWIDTH
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 "Tracker %p delay is infinity\n",
299 av);
300#endif
301 return GNUNET_TIME_UNIT_FOREVER_REL;
302 }
244 update_tracker (av); 303 update_tracker (av);
245 now = GNUNET_TIME_absolute_get (); 304 now = GNUNET_TIME_absolute_get ();
246 delta_time = now.value - av->last_update__.value; 305 delta_time = now.value - av->last_update__.value;
247 delta_avail = (delta_time * ((unsigned long long) av->available_bytes_per_s__)) / 1000LL; 306 delta_avail = (delta_time * ((unsigned long long) av->available_bytes_per_s__)) / 1000LL;
248 if (delta_avail >= size) 307 if (delta_avail >= size)
249 return GNUNET_TIME_UNIT_ZERO; 308 {
309#if DEBUG_BANDWIDTH
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311 "Tracker %p delay for %u bytes is zero\n",
312 av,
313 (unsigned int) size);
314#endif
315 return GNUNET_TIME_UNIT_ZERO;
316 }
250 bytes_needed = size - delta_avail; 317 bytes_needed = size - delta_avail;
251 ret.value = 1000LL * bytes_needed / (unsigned long long) av->available_bytes_per_s__; 318 ret.value = 1000LL * bytes_needed / (unsigned long long) av->available_bytes_per_s__;
319#if DEBUG_BANDWIDTH
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "Tracker %p delay for %u bytes is %llu ms\n",
322 av,
323 (unsigned int) size,
324 (unsigned long long) ret.value);
325#endif
252 return ret; 326 return ret;
253} 327}
254 328
@@ -272,6 +346,12 @@ GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker *av)
272 avail = GNUNET_BANDWIDTH_value_get_available_until (bps, 346 avail = GNUNET_BANDWIDTH_value_get_available_until (bps,
273 GNUNET_TIME_absolute_get_duration (av->last_update__)); 347 GNUNET_TIME_absolute_get_duration (av->last_update__));
274 used = av->consumption_since_last_update__; 348 used = av->consumption_since_last_update__;
349#if DEBUG_BANDWIDTH
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
351 "Tracker %p available bandwith is %lld ms\n",
352 av,
353 (long long) (int64_t) (avail - used));
354#endif
275 return (int64_t) (avail - used); 355 return (int64_t) (avail - used);
276} 356}
277 357
@@ -290,6 +370,12 @@ GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
290 uint32_t new_limit; 370 uint32_t new_limit;
291 371
292 new_limit = ntohl (bytes_per_second_limit.value__); 372 new_limit = ntohl (bytes_per_second_limit.value__);
373#if DEBUG_BANDWIDTH
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
375 "Tracker %p bandwith changed to %u bps\n",
376 av,
377 (unsigned int) new_limit);
378#endif
293 update_tracker (av); 379 update_tracker (av);
294 old_limit = av->available_bytes_per_s__; 380 old_limit = av->available_bytes_per_s__;
295 av->available_bytes_per_s__ = new_limit; 381 av->available_bytes_per_s__ = new_limit;