diff options
Diffstat (limited to 'src/util/bandwidth.c')
-rw-r--r-- | src/util/bandwidth.c | 515 |
1 files changed, 0 insertions, 515 deletions
diff --git a/src/util/bandwidth.c b/src/util/bandwidth.c deleted file mode 100644 index 8411c12ee..000000000 --- a/src/util/bandwidth.c +++ /dev/null | |||
@@ -1,515 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2010, 2013 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file util/bandwidth.c | ||
23 | * @brief functions related to bandwidth (unit) | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | |||
30 | |||
31 | #define LOG(kind, ...) GNUNET_log_from (kind, "util-bandwidth", __VA_ARGS__) | ||
32 | |||
33 | /** | ||
34 | * Create a new bandwidth value. | ||
35 | * | ||
36 | * @param bytes_per_second value to create | ||
37 | * @return the new bandwidth value | ||
38 | */ | ||
39 | struct GNUNET_BANDWIDTH_Value32NBO | ||
40 | GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second) | ||
41 | { | ||
42 | struct GNUNET_BANDWIDTH_Value32NBO ret; | ||
43 | |||
44 | ret.value__ = htonl (bytes_per_second); | ||
45 | return ret; | ||
46 | } | ||
47 | |||
48 | |||
49 | /** | ||
50 | * Compute the MIN of two bandwidth values. | ||
51 | * | ||
52 | * @param b1 first value | ||
53 | * @param b2 second value | ||
54 | * @return the min of b1 and b2 | ||
55 | */ | ||
56 | struct GNUNET_BANDWIDTH_Value32NBO | ||
57 | GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1, | ||
58 | struct GNUNET_BANDWIDTH_Value32NBO b2) | ||
59 | { | ||
60 | return GNUNET_BANDWIDTH_value_init ( | ||
61 | GNUNET_MIN (ntohl (b1.value__), ntohl (b2.value__))); | ||
62 | } | ||
63 | |||
64 | |||
65 | /** | ||
66 | * Compute the MAX of two bandwidth values. | ||
67 | * | ||
68 | * @param b1 first value | ||
69 | * @param b2 second value | ||
70 | * @return the min of b1 and b2 | ||
71 | */ | ||
72 | struct GNUNET_BANDWIDTH_Value32NBO | ||
73 | GNUNET_BANDWIDTH_value_max (struct GNUNET_BANDWIDTH_Value32NBO b1, | ||
74 | struct GNUNET_BANDWIDTH_Value32NBO b2) | ||
75 | { | ||
76 | return GNUNET_BANDWIDTH_value_init ( | ||
77 | GNUNET_MAX (ntohl (b1.value__), ntohl (b2.value__))); | ||
78 | } | ||
79 | |||
80 | |||
81 | /** | ||
82 | * Compute the SUM of two bandwidth values. | ||
83 | * | ||
84 | * @param b1 first value | ||
85 | * @param b2 second value | ||
86 | * @return the sum of b1 and b2 | ||
87 | */ | ||
88 | struct GNUNET_BANDWIDTH_Value32NBO | ||
89 | GNUNET_BANDWIDTH_value_sum (struct GNUNET_BANDWIDTH_Value32NBO b1, | ||
90 | struct GNUNET_BANDWIDTH_Value32NBO b2) | ||
91 | { | ||
92 | return GNUNET_BANDWIDTH_value_init (ntohl (b1.value__) + ntohl (b2.value__)); | ||
93 | } | ||
94 | |||
95 | |||
96 | uint64_t | ||
97 | GNUNET_BANDWIDTH_value_get_available_until ( | ||
98 | struct GNUNET_BANDWIDTH_Value32NBO bps, | ||
99 | struct GNUNET_TIME_Relative deadline) | ||
100 | { | ||
101 | uint64_t b; | ||
102 | |||
103 | b = ntohl (bps.value__); | ||
104 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
105 | "Bandwidth has %llu bytes available until deadline in %s\n", | ||
106 | (unsigned long long) ((b * deadline.rel_value_us + 500000LL) | ||
107 | / 1000000LL), | ||
108 | GNUNET_STRINGS_relative_time_to_string (deadline, GNUNET_YES)); | ||
109 | return (b * deadline.rel_value_us + 500000LL) / 1000000LL; | ||
110 | } | ||
111 | |||
112 | |||
113 | /** | ||
114 | * At the given bandwidth, calculate how long it would take for | ||
115 | * @a size bytes to be transmitted. | ||
116 | * | ||
117 | * @param bps bandwidth | ||
118 | * @param size number of bytes we want to have available | ||
119 | * @return how long it would take | ||
120 | */ | ||
121 | struct GNUNET_TIME_Relative | ||
122 | GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps, | ||
123 | uint64_t size) | ||
124 | { | ||
125 | uint64_t b; | ||
126 | struct GNUNET_TIME_Relative ret; | ||
127 | |||
128 | b = ntohl (bps.value__); | ||
129 | if (0 == b) | ||
130 | { | ||
131 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
132 | "Bandwidth suggests delay of infinity (zero bandwidth)\n"); | ||
133 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
134 | } | ||
135 | ret.rel_value_us = size * 1000LL * 1000LL / b; | ||
136 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
137 | "Bandwidth suggests delay of %s for %llu bytes of traffic\n", | ||
138 | GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES), | ||
139 | (unsigned long long) size); | ||
140 | return ret; | ||
141 | } | ||
142 | |||
143 | |||
144 | /** | ||
145 | * Task run whenever we hit the bandwidth limit for a tracker. | ||
146 | * | ||
147 | * @param cls the `struct GNUNET_BANDWIDTH_Tracker` | ||
148 | */ | ||
149 | static void | ||
150 | excess_trigger (void *cls) | ||
151 | { | ||
152 | struct GNUNET_BANDWIDTH_Tracker *av = cls; | ||
153 | |||
154 | av->excess_task = NULL; | ||
155 | if (NULL != av->excess_cb) | ||
156 | { | ||
157 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
158 | "Notifying application about excess bandwidth\n"); | ||
159 | av->excess_cb (av->excess_cb_cls); | ||
160 | } | ||
161 | } | ||
162 | |||
163 | |||
164 | /** | ||
165 | * Recalculate when we might need to call the excess callback. | ||
166 | */ | ||
167 | static void | ||
168 | update_excess (struct GNUNET_BANDWIDTH_Tracker *av) | ||
169 | { | ||
170 | struct GNUNET_TIME_Relative delay; | ||
171 | struct GNUNET_TIME_Absolute now; | ||
172 | uint64_t delta_time; | ||
173 | uint64_t delta_avail; | ||
174 | int64_t left_bytes; | ||
175 | uint64_t max_carry; | ||
176 | int64_t current_consumption; | ||
177 | |||
178 | if (NULL == av->excess_cb) | ||
179 | return; /* nothing to do */ | ||
180 | now = GNUNET_TIME_absolute_get (); | ||
181 | delta_time = now.abs_value_us - av->last_update__.abs_value_us; | ||
182 | delta_avail = | ||
183 | (delta_time * ((unsigned long long) av->available_bytes_per_s__) | ||
184 | + 500000LL) | ||
185 | / 1000000LL; | ||
186 | current_consumption = av->consumption_since_last_update__ - delta_avail; | ||
187 | if (current_consumption > av->consumption_since_last_update__) | ||
188 | { | ||
189 | /* integer underflow, cap! */ | ||
190 | current_consumption = INT64_MIN; | ||
191 | } | ||
192 | /* negative current_consumption means that we have savings */ | ||
193 | max_carry = ((uint64_t) av->available_bytes_per_s__) * av->max_carry_s__; | ||
194 | if (max_carry < GNUNET_MAX_MESSAGE_SIZE) | ||
195 | max_carry = GNUNET_MAX_MESSAGE_SIZE; | ||
196 | if (max_carry > INT64_MAX) | ||
197 | max_carry = INT64_MAX; | ||
198 | left_bytes = current_consumption + max_carry; | ||
199 | if (left_bytes < current_consumption) | ||
200 | { | ||
201 | /* integer overflow, cap! */ | ||
202 | left_bytes = INT64_MAX; | ||
203 | } | ||
204 | /* left_bytes now contains the number of bytes needed until | ||
205 | we have more savings than allowed */ | ||
206 | if (left_bytes < 0) | ||
207 | { | ||
208 | /* having excess already */ | ||
209 | delay = GNUNET_TIME_UNIT_ZERO; | ||
210 | } | ||
211 | else | ||
212 | { | ||
213 | double factor = 1.0 * left_bytes / (double) av->available_bytes_per_s__; | ||
214 | delay = | ||
215 | GNUNET_TIME_relative_saturating_multiply (GNUNET_TIME_UNIT_SECONDS, | ||
216 | (unsigned long long) factor); | ||
217 | } | ||
218 | GNUNET_log ( | ||
219 | GNUNET_ERROR_TYPE_DEBUG, | ||
220 | "At %llu bps it will take us %s for %lld bytes to reach excess threshold\n", | ||
221 | (unsigned long long) av->available_bytes_per_s__, | ||
222 | GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO), | ||
223 | (long long) left_bytes); | ||
224 | if (NULL != av->excess_task) | ||
225 | GNUNET_SCHEDULER_cancel (av->excess_task); | ||
226 | av->excess_task = GNUNET_SCHEDULER_add_delayed (delay, &excess_trigger, av); | ||
227 | } | ||
228 | |||
229 | |||
230 | /** | ||
231 | * Initialize bandwidth tracker. Note that in addition to the | ||
232 | * 'max_carry_s' limit, we also always allow at least | ||
233 | * #GNUNET_MAX_MESSAGE_SIZE to accumulate. So if the | ||
234 | * bytes-per-second limit is so small that within 'max_carry_s' not | ||
235 | * even #GNUNET_MAX_MESSAGE_SIZE is allowed to accumulate, it is | ||
236 | * ignored and replaced by #GNUNET_MAX_MESSAGE_SIZE (which is in | ||
237 | * bytes). | ||
238 | * | ||
239 | * To stop notifications about updates and excess callbacks use | ||
240 | * #GNUNET_BANDWIDTH_tracker_notification_stop(). | ||
241 | * | ||
242 | * @param av tracker to initialize | ||
243 | * @param update_cb callback to notify a client about the tracker being updated | ||
244 | * @param update_cb_cls cls for the callback | ||
245 | * @param bytes_per_second_limit initial limit to assume | ||
246 | * @param max_carry_s maximum number of seconds unused bandwidth | ||
247 | * may accumulate before it expires | ||
248 | * @param excess_cb callback to notify if we have excess bandwidth | ||
249 | * @param excess_cb_cls closure for @a excess_cb | ||
250 | */ | ||
251 | void | ||
252 | GNUNET_BANDWIDTH_tracker_init2 ( | ||
253 | struct GNUNET_BANDWIDTH_Tracker *av, | ||
254 | GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb, | ||
255 | void *update_cb_cls, | ||
256 | struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit, | ||
257 | uint32_t max_carry_s, | ||
258 | GNUNET_BANDWIDTH_ExcessNotificationCallback excess_cb, | ||
259 | void *excess_cb_cls) | ||
260 | { | ||
261 | av->update_cb = update_cb; | ||
262 | av->update_cb_cls = update_cb_cls; | ||
263 | av->consumption_since_last_update__ = 0; | ||
264 | av->last_update__ = GNUNET_TIME_absolute_get (); | ||
265 | av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__); | ||
266 | av->max_carry_s__ = max_carry_s; | ||
267 | av->excess_cb = excess_cb; | ||
268 | av->excess_cb_cls = excess_cb_cls; | ||
269 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
270 | "Tracker %p initialized with %u Bps and max carry %u\n", | ||
271 | av, | ||
272 | (unsigned int) av->available_bytes_per_s__, | ||
273 | (unsigned int) max_carry_s); | ||
274 | update_excess (av); | ||
275 | } | ||
276 | |||
277 | |||
278 | void | ||
279 | GNUNET_BANDWIDTH_tracker_init ( | ||
280 | struct GNUNET_BANDWIDTH_Tracker *av, | ||
281 | GNUNET_BANDWIDTH_TrackerUpdateCallback update_cb, | ||
282 | void *update_cb_cls, | ||
283 | struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit, | ||
284 | uint32_t max_carry_s) | ||
285 | { | ||
286 | GNUNET_BANDWIDTH_tracker_init2 (av, | ||
287 | update_cb, | ||
288 | update_cb_cls, | ||
289 | bytes_per_second_limit, | ||
290 | max_carry_s, | ||
291 | NULL, | ||
292 | NULL); | ||
293 | } | ||
294 | |||
295 | |||
296 | /** | ||
297 | * Stop notifying about tracker updates and excess notifications | ||
298 | * | ||
299 | * @param av the respective trackers | ||
300 | */ | ||
301 | void | ||
302 | GNUNET_BANDWIDTH_tracker_notification_stop (struct GNUNET_BANDWIDTH_Tracker *av) | ||
303 | { | ||
304 | if (NULL != av->excess_task) | ||
305 | GNUNET_SCHEDULER_cancel (av->excess_task); | ||
306 | av->excess_task = NULL; | ||
307 | av->excess_cb = NULL; | ||
308 | av->excess_cb_cls = NULL; | ||
309 | av->update_cb = NULL; | ||
310 | av->update_cb_cls = NULL; | ||
311 | } | ||
312 | |||
313 | |||
314 | /** | ||
315 | * Update the tracker, looking at the current time and | ||
316 | * bandwidth consumption data. | ||
317 | * | ||
318 | * @param av tracker to update | ||
319 | */ | ||
320 | static void | ||
321 | update_tracker (struct GNUNET_BANDWIDTH_Tracker *av) | ||
322 | { | ||
323 | struct GNUNET_TIME_Absolute now; | ||
324 | uint64_t delta_time; | ||
325 | uint64_t delta_avail; | ||
326 | uint64_t left_bytes; | ||
327 | uint64_t max_carry; | ||
328 | |||
329 | now = GNUNET_TIME_absolute_get (); | ||
330 | delta_time = now.abs_value_us - av->last_update__.abs_value_us; | ||
331 | delta_avail = | ||
332 | (delta_time * ((unsigned long long) av->available_bytes_per_s__) | ||
333 | + 500000LL) | ||
334 | / 1000000LL; | ||
335 | av->consumption_since_last_update__ -= delta_avail; | ||
336 | av->last_update__ = now; | ||
337 | if (av->consumption_since_last_update__ < 0) | ||
338 | { | ||
339 | left_bytes = -av->consumption_since_last_update__; | ||
340 | max_carry = | ||
341 | ((unsigned long long) av->available_bytes_per_s__) * av->max_carry_s__; | ||
342 | if (max_carry < GNUNET_MAX_MESSAGE_SIZE) | ||
343 | max_carry = GNUNET_MAX_MESSAGE_SIZE; | ||
344 | if (max_carry > INT64_MAX) | ||
345 | max_carry = INT64_MAX; | ||
346 | if (max_carry > left_bytes) | ||
347 | av->consumption_since_last_update__ = -left_bytes; | ||
348 | else | ||
349 | av->consumption_since_last_update__ = -max_carry; | ||
350 | } | ||
351 | #if ! defined(GNUNET_CULL_LOGGING) | ||
352 | { | ||
353 | struct GNUNET_TIME_Relative delta; | ||
354 | |||
355 | delta.rel_value_us = delta_time; | ||
356 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
357 | "Tracker %p updated, consumption at %lld at %u Bps, last update was %s ago\n", | ||
358 | av, | ||
359 | (long long) av->consumption_since_last_update__, | ||
360 | (unsigned int) av->available_bytes_per_s__, | ||
361 | GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_YES)); | ||
362 | } | ||
363 | #endif | ||
364 | } | ||
365 | |||
366 | |||
367 | int | ||
368 | GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av, | ||
369 | ssize_t size) | ||
370 | { | ||
371 | int64_t nc; | ||
372 | |||
373 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
374 | "Tracker %p consumes %d bytes\n", | ||
375 | av, | ||
376 | (int) size); | ||
377 | if (size > 0) | ||
378 | { | ||
379 | nc = av->consumption_since_last_update__ + size; | ||
380 | if (nc < av->consumption_since_last_update__) | ||
381 | { | ||
382 | /* integer overflow, very bad */ | ||
383 | GNUNET_break (0); | ||
384 | return GNUNET_SYSERR; | ||
385 | } | ||
386 | av->consumption_since_last_update__ = nc; | ||
387 | update_tracker (av); | ||
388 | update_excess (av); | ||
389 | if (av->consumption_since_last_update__ > 0) | ||
390 | { | ||
391 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
392 | "Tracker %p consumption %llu bytes above limit\n", | ||
393 | av, | ||
394 | (unsigned long long) av->consumption_since_last_update__); | ||
395 | return GNUNET_YES; | ||
396 | } | ||
397 | } | ||
398 | else | ||
399 | { | ||
400 | nc = av->consumption_since_last_update__ + size; | ||
401 | if (nc > av->consumption_since_last_update__) | ||
402 | { | ||
403 | /* integer underflow, very bad */ | ||
404 | GNUNET_break (0); | ||
405 | return GNUNET_SYSERR; | ||
406 | } | ||
407 | av->consumption_since_last_update__ = nc; | ||
408 | update_excess (av); | ||
409 | } | ||
410 | return GNUNET_NO; | ||
411 | } | ||
412 | |||
413 | |||
414 | /** | ||
415 | * Compute how long we should wait until consuming 'size' | ||
416 | * bytes of bandwidth in order to stay within the given | ||
417 | * quota. | ||
418 | * | ||
419 | * @param av tracker to query | ||
420 | * @param size number of bytes we would like to consume | ||
421 | * @return time in ms to wait for consumption to be OK | ||
422 | */ | ||
423 | struct GNUNET_TIME_Relative | ||
424 | GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av, | ||
425 | size_t size) | ||
426 | { | ||
427 | struct GNUNET_TIME_Relative ret; | ||
428 | int64_t bytes_needed; | ||
429 | |||
430 | if (0 == av->available_bytes_per_s__) | ||
431 | { | ||
432 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Tracker %p delay is infinity\n", av); | ||
433 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
434 | } | ||
435 | update_tracker (av); | ||
436 | bytes_needed = size + av->consumption_since_last_update__; | ||
437 | if (bytes_needed <= 0) | ||
438 | { | ||
439 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
440 | "Tracker %p delay for %u bytes is zero\n", | ||
441 | av, | ||
442 | (unsigned int) size); | ||
443 | return GNUNET_TIME_UNIT_ZERO; | ||
444 | } | ||
445 | ret.rel_value_us = (1000LL * 1000LL * bytes_needed) | ||
446 | / (unsigned long long) av->available_bytes_per_s__; | ||
447 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
448 | "Tracker %p delay for %u bytes is %s\n", | ||
449 | av, | ||
450 | (unsigned int) size, | ||
451 | GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES)); | ||
452 | return ret; | ||
453 | } | ||
454 | |||
455 | |||
456 | /** | ||
457 | * Compute how many bytes are available for consumption right now. | ||
458 | * quota. | ||
459 | * | ||
460 | * @param av tracker to query | ||
461 | * @return number of bytes available for consumption right now | ||
462 | */ | ||
463 | int64_t | ||
464 | GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker *av) | ||
465 | { | ||
466 | struct GNUNET_BANDWIDTH_Value32NBO bps; | ||
467 | uint64_t avail; | ||
468 | int64_t used; | ||
469 | |||
470 | update_tracker (av); | ||
471 | bps = GNUNET_BANDWIDTH_value_init (av->available_bytes_per_s__); | ||
472 | avail = | ||
473 | GNUNET_BANDWIDTH_value_get_available_until (bps, | ||
474 | GNUNET_TIME_absolute_get_duration ( | ||
475 | av->last_update__)); | ||
476 | used = av->consumption_since_last_update__; | ||
477 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
478 | "Tracker %p available bandwidth is %lld bytes\n", | ||
479 | av, | ||
480 | (long long) (int64_t) (avail - used)); | ||
481 | return (int64_t) (avail - used); | ||
482 | } | ||
483 | |||
484 | |||
485 | /** | ||
486 | * Update quota of bandwidth tracker. | ||
487 | * | ||
488 | * @param av tracker to initialize | ||
489 | * @param bytes_per_second_limit new limit to assume | ||
490 | */ | ||
491 | void | ||
492 | GNUNET_BANDWIDTH_tracker_update_quota ( | ||
493 | struct GNUNET_BANDWIDTH_Tracker *av, | ||
494 | struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit) | ||
495 | { | ||
496 | uint32_t old_limit; | ||
497 | uint32_t new_limit; | ||
498 | |||
499 | new_limit = ntohl (bytes_per_second_limit.value__); | ||
500 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
501 | "Tracker %p bandwidth changed to %u Bps\n", | ||
502 | av, | ||
503 | (unsigned int) new_limit); | ||
504 | update_tracker (av); | ||
505 | old_limit = av->available_bytes_per_s__; | ||
506 | av->available_bytes_per_s__ = new_limit; | ||
507 | if (NULL != av->update_cb) | ||
508 | av->update_cb (av->update_cb_cls); | ||
509 | if (old_limit > new_limit) | ||
510 | update_tracker (av); /* maximum excess might be less now */ | ||
511 | update_excess (av); | ||
512 | } | ||
513 | |||
514 | |||
515 | /* end of bandwidth.c */ | ||