diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-03-05 15:43:20 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-03-05 15:43:20 +0000 |
commit | 3caf199795bd1c7b475f9cb7941186ee4b66eaec (patch) | |
tree | 51bac749422d8d376efdc6dbfb8c636d4d09f581 /src | |
parent | 441d62754d26edab61ef7fd9bc5c3f6c3c59af5a (diff) | |
download | gnunet-3caf199795bd1c7b475f9cb7941186ee4b66eaec.tar.gz gnunet-3caf199795bd1c7b475f9cb7941186ee4b66eaec.zip |
bwlib
Diffstat (limited to 'src')
-rw-r--r-- | src/include/gnunet_bandwidth_lib.h | 178 | ||||
-rw-r--r-- | src/util/Makefile.am | 1 | ||||
-rw-r--r-- | src/util/bandwidth.c | 221 |
3 files changed, 400 insertions, 0 deletions
diff --git a/src/include/gnunet_bandwidth_lib.h b/src/include/gnunet_bandwidth_lib.h new file mode 100644 index 000000000..2dbe6e562 --- /dev/null +++ b/src/include/gnunet_bandwidth_lib.h | |||
@@ -0,0 +1,178 @@ | |||
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 include/gnunet_bandwidth_lib.h | ||
23 | * @brief functions related to bandwidth (unit) | ||
24 | * | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | |||
28 | #ifndef GNUNET_BANDWIDTH_LIB_H | ||
29 | #define GNUNET_BANDWIDTH_LIB_H | ||
30 | |||
31 | #ifdef __cplusplus | ||
32 | extern "C" | ||
33 | { | ||
34 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
35 | } | ||
36 | #endif | ||
37 | #endif | ||
38 | |||
39 | #include "gnunet_common.h" | ||
40 | #include "gnunet_time_lib.h" | ||
41 | |||
42 | /** | ||
43 | * 32-bit bandwidth used for network exchange by GNUnet, in bytes per second. | ||
44 | */ | ||
45 | struct GNUNET_BANDWIDTH_Value32NBO | ||
46 | { | ||
47 | /** | ||
48 | * The actual value (bytes per second). | ||
49 | */ | ||
50 | uint32_t value__ GNUNET_PACKED; | ||
51 | }; | ||
52 | |||
53 | |||
54 | /** | ||
55 | * Struct to track available bandwidth. Combines a time stamp with a | ||
56 | * number of bytes transmitted, a quota and a maximum amount that | ||
57 | * carries over. Not opaque so that it can be inlined into data | ||
58 | * structures (reducing malloc-ing); however, values should not be | ||
59 | * accessed directly by clients (hence the '__'). | ||
60 | */ | ||
61 | struct GNUNET_BANDWIDTH_Tracker | ||
62 | { | ||
63 | /** | ||
64 | * Number of bytes consumed since we last updated the tracker. | ||
65 | */ | ||
66 | uint64_t consumption_since_last_update__; | ||
67 | |||
68 | /** | ||
69 | * Time when we last updated the tracker. | ||
70 | */ | ||
71 | struct GNUNET_TIME_Absolute last_update__; | ||
72 | |||
73 | /** | ||
74 | * Bandwidth limit to enforce in bytes per s. | ||
75 | */ | ||
76 | uint32_t available_bytes_per_s__; | ||
77 | |||
78 | /** | ||
79 | * Maximum number of seconds over which bandwidth may "accumulate". | ||
80 | * Note that additionally, we also always allow at least | ||
81 | * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate. | ||
82 | */ | ||
83 | uint32_t max_carry_s__; | ||
84 | }; | ||
85 | |||
86 | |||
87 | /** | ||
88 | * Create a new bandwidth value. | ||
89 | * | ||
90 | * @param bytes_per_second value to create | ||
91 | * @return the new bandwidth value | ||
92 | */ | ||
93 | struct GNUNET_BANDWIDTH_Value32NBO | ||
94 | GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second); | ||
95 | |||
96 | |||
97 | /** | ||
98 | * Compute the MIN of two bandwidth values. | ||
99 | * | ||
100 | * @param b1 first value | ||
101 | * @param b2 second value | ||
102 | * @return the min of b1 and b2 | ||
103 | */ | ||
104 | struct GNUNET_BANDWIDTH_Value32NBO | ||
105 | GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1, | ||
106 | struct GNUNET_BANDWIDTH_Value32NBO b2); | ||
107 | |||
108 | |||
109 | /** | ||
110 | * Initialize bandwidth tracker. Note that in addition to the | ||
111 | * 'max_carry_s' limit, we also always allow at least | ||
112 | * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate. So if the | ||
113 | * bytes-per-second limit is so small that within 'max_carry_s' not | ||
114 | * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is | ||
115 | * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in | ||
116 | * bytes). | ||
117 | * | ||
118 | * @param av tracker to initialize | ||
119 | * @param bytes_per_second_limit initial limit to assume | ||
120 | * @param max_carry_s maximum number of seconds unused bandwidth | ||
121 | * may accumulate before it expires | ||
122 | */ | ||
123 | void | ||
124 | GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av, | ||
125 | struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit, | ||
126 | uint32_t max_carry_s); | ||
127 | |||
128 | |||
129 | /** | ||
130 | * Notify the tracker that a certain number of bytes of bandwidth have | ||
131 | * been consumed. Note that it is legal to consume bytes even if not | ||
132 | * enough bandwidth is available (in that case, | ||
133 | * GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values | ||
134 | * even for a size of zero for a while). | ||
135 | * | ||
136 | * @param av tracker to update | ||
137 | * @param size number of bytes consumed | ||
138 | */ | ||
139 | void | ||
140 | GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av, | ||
141 | size_t size); | ||
142 | |||
143 | |||
144 | /** | ||
145 | * Compute how long we should wait until consuming 'size' | ||
146 | * bytes of bandwidth in order to stay within the given | ||
147 | * quota. | ||
148 | * | ||
149 | * @param av tracker to query | ||
150 | * @param size number of bytes we would like to consume | ||
151 | * @return time to wait for consumption to be OK | ||
152 | */ | ||
153 | struct GNUNET_TIME_Relative | ||
154 | GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av, | ||
155 | size_t size); | ||
156 | |||
157 | |||
158 | /** | ||
159 | * Update quota of bandwidth tracker. | ||
160 | * | ||
161 | * @param av tracker to initialize | ||
162 | * @param bytes_per_second_limit new limit to assume | ||
163 | */ | ||
164 | void | ||
165 | GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av, | ||
166 | struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit); | ||
167 | |||
168 | |||
169 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
170 | { | ||
171 | #endif | ||
172 | #ifdef __cplusplus | ||
173 | } | ||
174 | #endif | ||
175 | |||
176 | /* ifndef GNUNET_BANDWIDTH_LIB_H */ | ||
177 | #endif | ||
178 | /* end of gnunet_bandwidth_lib.h */ | ||
diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 99cb0af97..81e6f79d8 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am | |||
@@ -24,6 +24,7 @@ endif | |||
24 | lib_LTLIBRARIES = libgnunetutil.la | 24 | lib_LTLIBRARIES = libgnunetutil.la |
25 | 25 | ||
26 | libgnunetutil_la_SOURCES = \ | 26 | libgnunetutil_la_SOURCES = \ |
27 | bandwidth.c \ | ||
27 | bio.c \ | 28 | bio.c \ |
28 | client.c \ | 29 | client.c \ |
29 | common_allocation.c \ | 30 | common_allocation.c \ |
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 | */ | ||
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 | * 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 | */ | ||
76 | void | ||
77 | GNUNET_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 | */ | ||
94 | static void | ||
95 | update_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 | */ | ||
149 | void | ||
150 | GNUNET_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 | */ | ||
175 | struct GNUNET_TIME_Relative | ||
176 | GNUNET_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 | */ | ||
205 | void | ||
206 | GNUNET_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 */ | ||