aboutsummaryrefslogtreecommitdiff
path: root/src/util/bandwidth.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/bandwidth.c')
-rw-r--r--src/util/bandwidth.c221
1 files changed, 221 insertions, 0 deletions
diff --git a/src/util/bandwidth.c b/src/util/bandwidth.c
new file mode 100644
index 000000000..aaf57619f
--- /dev/null
+++ b/src/util/bandwidth.c
@@ -0,0 +1,221 @@
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 */
36struct GNUNET_BANDWIDTH_Value32NBO
37GNUNET_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 */
53struct GNUNET_BANDWIDTH_Value32NBO
54GNUNET_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 * Initialize bandwidth tracker. Note that in addition to the
64 * 'max_carry_s' limit, we also always allow at least
65 * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate. So if the
66 * bytes-per-second limit is so small that within 'max_carry_s' not
67 * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
68 * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
69 * bytes).
70 *
71 * @param av tracker to initialize
72 * @param bytes_per_second_limit initial limit to assume
73 * @param max_carry_s maximum number of seconds unused bandwidth
74 * may accumulate before it expires
75 */
76void
77GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
78 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit,
79 uint32_t max_carry_s)
80{
81 av->consumption_since_last_update__ = 0;
82 av->last_update__ = GNUNET_TIME_absolute_get ();
83 av->available_bytes_per_s__ = ntohl (bytes_per_second_limit.value__);
84 av->max_carry_s__ = max_carry_s;
85}
86
87
88/**
89 * Update the tracker, looking at the current time and
90 * bandwidth consumption data.
91 *
92 * @param av tracker to update
93 */
94static void
95update_tracker (struct GNUNET_BANDWIDTH_Tracker *av)
96{
97 struct GNUNET_TIME_Absolute now;
98 uint64_t avail_per_ms;
99 uint64_t delta_time;
100 uint64_t delta_avail;
101 uint64_t left_bytes;
102 uint64_t left_time_ms;
103
104 now = GNUNET_TIME_absolute_get ();
105 delta_time = now.value - av->last_update__.value;
106 delta_avail = (delta_time * ((unsigned long long) av->available_bytes_per_s__)) / 1000LL;
107 if (av->consumption_since_last_update__ >= delta_avail)
108 {
109 av->consumption_since_last_update__ -= delta_avail;
110 av->last_update__ = now;
111 }
112 else
113 {
114 left_bytes = delta_avail - av->consumption_since_last_update__;
115 avail_per_ms = ((unsigned long long) av->available_bytes_per_s__) / 1000LL;
116 if (avail_per_ms > 0)
117 left_time_ms = left_bytes / avail_per_ms;
118 else
119 left_time_ms = 0;
120 if (left_time_ms > ((unsigned long long) av->max_carry_s__) * 1000LL)
121 {
122 /* need to limit accumulation of unused bandwidth */
123 left_time_ms = ((unsigned long long) av->max_carry_s__) * 1000LL;
124 if (left_time_ms * avail_per_ms < GNUNET_SERVER_MAX_MESSAGE_SIZE)
125 {
126 /* need to still allow GNUNET_SERVER_MAX_MESSAGE_SIZE accumulation */
127 if (left_bytes > GNUNET_SERVER_MAX_MESSAGE_SIZE)
128 left_bytes = GNUNET_SERVER_MAX_MESSAGE_SIZE;
129 left_time_ms = left_bytes / avail_per_ms;
130 }
131 }
132 av->consumption_since_last_update__ = 0;
133 av->last_update__.value = now.value - left_time_ms;
134 }
135}
136
137
138
139/**
140 * Notify the tracker that a certain number of bytes of bandwidth have
141 * been consumed. Note that it is legal to consume bytes even if not
142 * enough bandwidth is available (in that case,
143 * GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
144 * even for a size of zero for a while).
145 *
146 * @param av tracker to update
147 * @param size number of bytes consumed
148 */
149void
150GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
151 size_t size)
152{
153 uint64_t nc;
154
155 nc = av->consumption_since_last_update__ + size;
156 if (nc < av->consumption_since_last_update__)
157 {
158 GNUNET_break (0);
159 return;
160 }
161 av->consumption_since_last_update__ += size;
162 update_tracker (av);
163}
164
165
166/**
167 * Compute how long we should wait until consuming 'size'
168 * bytes of bandwidth in order to stay within the given
169 * quota.
170 *
171 * @param av tracker to query
172 * @param size number of bytes we would like to consume
173 * @return time to wait for consumption to be OK
174 */
175struct GNUNET_TIME_Relative
176GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
177 size_t size)
178{
179 struct GNUNET_TIME_Relative ret;
180 struct GNUNET_TIME_Absolute now;
181 uint64_t delta_avail;
182 uint64_t delta_time;
183 uint64_t bytes_needed;
184
185 if (av->available_bytes_per_s__ == 0)
186 return GNUNET_TIME_UNIT_FOREVER_REL;
187 update_tracker (av);
188 now = GNUNET_TIME_absolute_get ();
189 delta_time = now.value - av->last_update__.value;
190 delta_avail = (delta_time * ((unsigned long long) av->available_bytes_per_s__)) / 1000LL;
191 if (delta_avail >= size)
192 return GNUNET_TIME_UNIT_ZERO;
193 bytes_needed = size - delta_avail;
194 ret.value = 1000LL * bytes_needed / (unsigned long long) av->available_bytes_per_s__;
195 return ret;
196}
197
198
199/**
200 * Update quota of bandwidth tracker.
201 *
202 * @param av tracker to initialize
203 * @param bytes_per_second_limit new limit to assume
204 */
205void
206GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
207 struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit)
208{
209 uint32_t old_limit;
210 uint32_t new_limit;
211
212 new_limit = ntohl (bytes_per_second_limit.value__);
213 update_tracker (av);
214 old_limit = av->available_bytes_per_s__;
215 av->available_bytes_per_s__ = new_limit;
216 if (old_limit > new_limit)
217 update_tracker (av); /* maximum excess might be less now */
218}
219
220
221/* end of bandwidth.c */