1 | /* |
---|
2 | * This file Copyright (C) 2008 Charles Kerr <charles@transmissionbt.com> |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id:$ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <limits.h> |
---|
15 | |
---|
16 | #include "event.h" |
---|
17 | |
---|
18 | #include "transmission.h" |
---|
19 | #include "bandwidth.h" |
---|
20 | #include "crypto.h" |
---|
21 | #include "peer-io.h" |
---|
22 | #include "ptrarray.h" |
---|
23 | #include "utils.h" |
---|
24 | |
---|
25 | /*** |
---|
26 | **** |
---|
27 | ***/ |
---|
28 | |
---|
29 | enum |
---|
30 | { |
---|
31 | HISTORY_MSEC = 2000, |
---|
32 | INTERVAL_MSEC = HISTORY_MSEC, |
---|
33 | GRANULARITY_MSEC = 50, |
---|
34 | HISTORY_SIZE = ( INTERVAL_MSEC / GRANULARITY_MSEC ), |
---|
35 | MAGIC_NUMBER = 43143 |
---|
36 | }; |
---|
37 | |
---|
38 | struct bratecontrol |
---|
39 | { |
---|
40 | int newest; |
---|
41 | struct { uint64_t date, size; } transfers[HISTORY_SIZE]; |
---|
42 | }; |
---|
43 | |
---|
44 | static float |
---|
45 | getSpeed( const struct bratecontrol * r, int interval_msec ) |
---|
46 | { |
---|
47 | uint64_t bytes = 0; |
---|
48 | const uint64_t cutoff = tr_date ( ) - interval_msec; |
---|
49 | int i = r->newest; |
---|
50 | |
---|
51 | for( ;; ) |
---|
52 | { |
---|
53 | if( r->transfers[i].date <= cutoff ) |
---|
54 | break; |
---|
55 | |
---|
56 | bytes += r->transfers[i].size; |
---|
57 | |
---|
58 | if( --i == -1 ) i = HISTORY_SIZE - 1; /* circular history */ |
---|
59 | if( i == r->newest ) break; /* we've come all the way around */ |
---|
60 | } |
---|
61 | |
---|
62 | return ( bytes / 1024.0 ) * ( 1000.0 / interval_msec ); |
---|
63 | } |
---|
64 | |
---|
65 | static void |
---|
66 | bytesUsed( struct bratecontrol * r, size_t size ) |
---|
67 | { |
---|
68 | const uint64_t now = tr_date ( ); |
---|
69 | |
---|
70 | if( r->transfers[r->newest].date + GRANULARITY_MSEC >= now ) |
---|
71 | r->transfers[r->newest].size += size; |
---|
72 | else |
---|
73 | { |
---|
74 | if( ++r->newest == HISTORY_SIZE ) r->newest = 0; |
---|
75 | r->transfers[r->newest].date = now; |
---|
76 | r->transfers[r->newest].size = size; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | /****** |
---|
81 | ******* |
---|
82 | ******* |
---|
83 | ******/ |
---|
84 | |
---|
85 | struct tr_band |
---|
86 | { |
---|
87 | tr_bool isLimited; |
---|
88 | tr_bool honorParentLimits; |
---|
89 | size_t bytesLeft; |
---|
90 | double desiredSpeed; |
---|
91 | struct bratecontrol raw; |
---|
92 | struct bratecontrol piece; |
---|
93 | }; |
---|
94 | |
---|
95 | struct tr_bandwidth |
---|
96 | { |
---|
97 | struct tr_band band[2]; |
---|
98 | struct tr_bandwidth * parent; |
---|
99 | int magicNumber; |
---|
100 | tr_session * session; |
---|
101 | tr_ptrArray * children; /* struct tr_bandwidth */ |
---|
102 | tr_ptrArray * peers; /* tr_peerIo */ |
---|
103 | }; |
---|
104 | |
---|
105 | /*** |
---|
106 | **** |
---|
107 | ***/ |
---|
108 | |
---|
109 | static int |
---|
110 | comparePointers( const void * a, const void * b ) |
---|
111 | { |
---|
112 | return a - b; |
---|
113 | } |
---|
114 | |
---|
115 | static int |
---|
116 | isBandwidth( const tr_bandwidth * b ) |
---|
117 | { |
---|
118 | return ( b != NULL ) && ( b->magicNumber == MAGIC_NUMBER ); |
---|
119 | } |
---|
120 | |
---|
121 | static int |
---|
122 | isDirection( const tr_direction dir ) |
---|
123 | { |
---|
124 | return ( dir == TR_UP ) || ( dir == TR_DOWN ); |
---|
125 | } |
---|
126 | |
---|
127 | /*** |
---|
128 | **** |
---|
129 | ***/ |
---|
130 | |
---|
131 | tr_bandwidth* |
---|
132 | tr_bandwidthNew( tr_session * session, tr_bandwidth * parent ) |
---|
133 | { |
---|
134 | tr_bandwidth * b = tr_new0( tr_bandwidth, 1 ); |
---|
135 | b->session = session; |
---|
136 | b->children = tr_ptrArrayNew( ); |
---|
137 | b->peers = tr_ptrArrayNew( ); |
---|
138 | b->magicNumber = MAGIC_NUMBER; |
---|
139 | b->band[TR_UP].honorParentLimits = 1; |
---|
140 | b->band[TR_DOWN].honorParentLimits = 1; |
---|
141 | tr_bandwidthSetParent( b, parent ); |
---|
142 | return b; |
---|
143 | } |
---|
144 | |
---|
145 | void |
---|
146 | tr_bandwidthFree( tr_bandwidth * b ) |
---|
147 | { |
---|
148 | assert( isBandwidth( b ) ); |
---|
149 | |
---|
150 | tr_bandwidthSetParent( b, NULL ); |
---|
151 | tr_ptrArrayFree( b->peers, NULL ); |
---|
152 | tr_ptrArrayFree( b->children, NULL ); |
---|
153 | b->magicNumber = 0xDEAD; |
---|
154 | tr_free( b ); |
---|
155 | } |
---|
156 | |
---|
157 | /*** |
---|
158 | **** |
---|
159 | ***/ |
---|
160 | |
---|
161 | void |
---|
162 | tr_bandwidthSetParent( tr_bandwidth * b, |
---|
163 | tr_bandwidth * parent ) |
---|
164 | { |
---|
165 | assert( isBandwidth( b ) ); |
---|
166 | assert( b != parent ); |
---|
167 | |
---|
168 | if( b->parent ) |
---|
169 | { |
---|
170 | assert( isBandwidth( b->parent ) ); |
---|
171 | |
---|
172 | tr_ptrArrayRemoveSorted( b->parent->children, b, comparePointers ); |
---|
173 | b->parent= NULL; |
---|
174 | } |
---|
175 | |
---|
176 | if( parent ) |
---|
177 | { |
---|
178 | assert( isBandwidth( parent ) ); |
---|
179 | assert( parent->parent != b ); |
---|
180 | |
---|
181 | tr_ptrArrayInsertSorted( parent->children, b, comparePointers ); |
---|
182 | b->parent = parent; |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | void |
---|
187 | tr_bandwidthHonorParentLimits( tr_bandwidth * b, |
---|
188 | tr_direction dir, |
---|
189 | int honorParentLimits ) |
---|
190 | { |
---|
191 | assert( isBandwidth( b ) ); |
---|
192 | assert( isDirection( dir ) ); |
---|
193 | |
---|
194 | b->band[dir].honorParentLimits = honorParentLimits != 0; |
---|
195 | } |
---|
196 | |
---|
197 | /*** |
---|
198 | **** |
---|
199 | ***/ |
---|
200 | |
---|
201 | void |
---|
202 | tr_bandwidthSetDesiredSpeed( tr_bandwidth * b, |
---|
203 | tr_direction dir, |
---|
204 | double desiredSpeed ) |
---|
205 | { |
---|
206 | assert( isBandwidth( b ) ); |
---|
207 | assert( isDirection( dir ) ); |
---|
208 | |
---|
209 | b->band[dir].desiredSpeed = desiredSpeed; |
---|
210 | } |
---|
211 | |
---|
212 | double |
---|
213 | tr_bandwidthGetDesiredSpeed( const tr_bandwidth * b, |
---|
214 | tr_direction dir ) |
---|
215 | { |
---|
216 | assert( isBandwidth( b ) ); |
---|
217 | assert( isDirection( dir ) ); |
---|
218 | |
---|
219 | return b->band[dir].desiredSpeed; |
---|
220 | } |
---|
221 | |
---|
222 | void |
---|
223 | tr_bandwidthSetLimited( tr_bandwidth * b, |
---|
224 | tr_direction dir, |
---|
225 | int isLimited ) |
---|
226 | { |
---|
227 | assert( isBandwidth( b ) ); |
---|
228 | assert( isDirection( dir ) ); |
---|
229 | |
---|
230 | b->band[dir].isLimited = isLimited != 0; |
---|
231 | } |
---|
232 | |
---|
233 | int |
---|
234 | tr_bandwidthIsLimited( const tr_bandwidth * b, |
---|
235 | tr_direction dir ) |
---|
236 | { |
---|
237 | assert( isBandwidth( b ) ); |
---|
238 | assert( isDirection( dir ) ); |
---|
239 | |
---|
240 | return b->band[dir].isLimited != 0; |
---|
241 | } |
---|
242 | |
---|
243 | #if 0 |
---|
244 | #warning do not check the code in with this enabled |
---|
245 | #define DEBUG_DIRECTION TR_UP |
---|
246 | #endif |
---|
247 | |
---|
248 | static void |
---|
249 | allocateBandwidth( tr_bandwidth * b, |
---|
250 | tr_direction dir, |
---|
251 | int period_msec, |
---|
252 | tr_ptrArray * peer_pool ) |
---|
253 | { |
---|
254 | assert( isBandwidth( b ) ); |
---|
255 | assert( isDirection( dir ) ); |
---|
256 | |
---|
257 | if( b->band[dir].isLimited ) |
---|
258 | { |
---|
259 | const double desiredSpeed = b->band[dir].desiredSpeed; |
---|
260 | const double nextPulseSpeed = desiredSpeed; |
---|
261 | b->band[dir].bytesLeft = MAX( 0.0, nextPulseSpeed * 1024.0 * period_msec / 1000.0 ); |
---|
262 | |
---|
263 | #ifdef DEBUG_DIRECTION |
---|
264 | if( dir == DEBUG_DIRECTION ) |
---|
265 | fprintf( stderr, "bandwidth %p currentPieceSpeed(%5.2f of %5.2f) desiredSpeed(%5.2f), allocating %5.2f\n", |
---|
266 | b, currentSpeed, tr_bandwidthGetRawSpeed( b, dir ), desiredSpeed, |
---|
267 | b->band[dir].bytesLeft/1024.0 ); |
---|
268 | #endif |
---|
269 | } |
---|
270 | |
---|
271 | { |
---|
272 | int i; |
---|
273 | const int n = tr_ptrArraySize( b->peers ); |
---|
274 | for( i=0; i<n; ++i ) |
---|
275 | tr_ptrArrayAppend( peer_pool, tr_ptrArrayNth( b->peers, i ) ); |
---|
276 | } |
---|
277 | |
---|
278 | #ifdef DEBUG_DIRECTION |
---|
279 | if( ( dir == DEBUG_DIRECTION ) && ( n > 1 ) ) |
---|
280 | fprintf( stderr, "bandwidth %p has %d peers\n", b, n ); |
---|
281 | #endif |
---|
282 | |
---|
283 | /* all children should reallocate too */ |
---|
284 | if( 1 ) { |
---|
285 | int i, n=0; |
---|
286 | struct tr_bandwidth ** children = (struct tr_bandwidth**) tr_ptrArrayPeek( b->children, &n ); |
---|
287 | for( i=0; i<n; ++i ) |
---|
288 | allocateBandwidth( children[i], dir, period_msec, peer_pool ); |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | void |
---|
293 | tr_bandwidthAllocate( tr_bandwidth * b, |
---|
294 | tr_direction dir, |
---|
295 | int period_msec ) |
---|
296 | { |
---|
297 | int n; |
---|
298 | tr_ptrArray * tmp; |
---|
299 | struct tr_peerIo ** peers; |
---|
300 | const size_t chunkSize = 1024; /* arbitrary */ |
---|
301 | |
---|
302 | tmp = tr_ptrArrayNew( ); |
---|
303 | allocateBandwidth( b, dir, period_msec, tmp ); |
---|
304 | peers = (struct tr_peerIo**) tr_ptrArrayPeek( tmp, &n ); |
---|
305 | |
---|
306 | /* loop through all the peers, reading and writing in small chunks, |
---|
307 | * until we run out of bandwidth or peers. we do it this way to |
---|
308 | * prevent one peer from using up all the bandwidth */ |
---|
309 | fprintf( stderr, "%s - %d peers\n", (dir==TR_UP)?"up":"down", n ); |
---|
310 | while( n > 0 ) |
---|
311 | { |
---|
312 | int i; |
---|
313 | for( i=0; i<n; ) |
---|
314 | { |
---|
315 | const int byteCount = tr_peerIoFlush( peers[i], dir, chunkSize ); |
---|
316 | |
---|
317 | if( byteCount ) |
---|
318 | fprintf( stderr, "peer %p: %d bytes\n", peers[i], byteCount ); |
---|
319 | |
---|
320 | if( byteCount == (int)chunkSize ) |
---|
321 | ++i; |
---|
322 | else |
---|
323 | peers[i] = peers[--n]; |
---|
324 | } |
---|
325 | } |
---|
326 | |
---|
327 | /* cleanup */ |
---|
328 | tr_ptrArrayFree( tmp, NULL ); |
---|
329 | } |
---|
330 | |
---|
331 | /*** |
---|
332 | **** |
---|
333 | ***/ |
---|
334 | |
---|
335 | void |
---|
336 | tr_bandwidthAddPeer( tr_bandwidth * b, |
---|
337 | tr_peerIo * peerIo ) |
---|
338 | { |
---|
339 | assert( isBandwidth( b ) ); |
---|
340 | assert( peerIo ); |
---|
341 | |
---|
342 | tr_ptrArrayInsertSorted( b->peers, peerIo, comparePointers ); |
---|
343 | } |
---|
344 | |
---|
345 | void |
---|
346 | tr_bandwidthRemovePeer( tr_bandwidth * b, |
---|
347 | tr_peerIo * peerIo ) |
---|
348 | { |
---|
349 | assert( isBandwidth( b ) ); |
---|
350 | assert( peerIo ); |
---|
351 | |
---|
352 | tr_ptrArrayRemoveSorted( b->peers, peerIo, comparePointers ); |
---|
353 | } |
---|
354 | |
---|
355 | /*** |
---|
356 | **** |
---|
357 | ***/ |
---|
358 | |
---|
359 | size_t |
---|
360 | tr_bandwidthClamp( const tr_bandwidth * b, |
---|
361 | tr_direction dir, |
---|
362 | size_t byteCount ) |
---|
363 | { |
---|
364 | assert( isBandwidth( b ) ); |
---|
365 | assert( isDirection( dir ) ); |
---|
366 | |
---|
367 | if( b ) |
---|
368 | { |
---|
369 | if( b->band[dir].isLimited ) |
---|
370 | byteCount = MIN( byteCount, b->band[dir].bytesLeft ); |
---|
371 | |
---|
372 | if( b->parent && b->band[dir].honorParentLimits ) |
---|
373 | byteCount = tr_bandwidthClamp( b->parent, dir, byteCount ); |
---|
374 | } |
---|
375 | |
---|
376 | return byteCount; |
---|
377 | } |
---|
378 | |
---|
379 | double |
---|
380 | tr_bandwidthGetRawSpeed( const tr_bandwidth * b, tr_direction dir ) |
---|
381 | { |
---|
382 | assert( isBandwidth( b ) ); |
---|
383 | assert( isDirection( dir ) ); |
---|
384 | |
---|
385 | return getSpeed( &b->band[dir].raw, HISTORY_MSEC ); |
---|
386 | } |
---|
387 | |
---|
388 | double |
---|
389 | tr_bandwidthGetPieceSpeed( const tr_bandwidth * b, tr_direction dir ) |
---|
390 | { |
---|
391 | assert( isBandwidth( b ) ); |
---|
392 | assert( isDirection( dir ) ); |
---|
393 | |
---|
394 | return getSpeed( &b->band[dir].piece, HISTORY_MSEC ); |
---|
395 | } |
---|
396 | |
---|
397 | void |
---|
398 | tr_bandwidthUsed( tr_bandwidth * b, |
---|
399 | tr_direction dir, |
---|
400 | size_t byteCount, |
---|
401 | int isPieceData ) |
---|
402 | { |
---|
403 | struct tr_band * band; |
---|
404 | size_t oldBytesLeft; |
---|
405 | |
---|
406 | assert( isBandwidth( b ) ); |
---|
407 | assert( isDirection( dir ) ); |
---|
408 | |
---|
409 | band = &b->band[dir]; |
---|
410 | |
---|
411 | oldBytesLeft = band->bytesLeft; |
---|
412 | |
---|
413 | if( band->isLimited && isPieceData ) |
---|
414 | band->bytesLeft -= MIN( band->bytesLeft, byteCount ); |
---|
415 | |
---|
416 | #ifdef DEBUG_DIRECTION |
---|
417 | if( ( dir == DEBUG_DIRECTION ) && ( band->isLimited ) ) |
---|
418 | fprintf( stderr, "%p consumed %5zu bytes of %5s data... was %6zu, now %6zu left\n", |
---|
419 | b, byteCount, (isPieceData?"piece":"raw"), oldBytesLeft, band->bytesLeft ); |
---|
420 | #endif |
---|
421 | |
---|
422 | bytesUsed( &band->raw, byteCount ); |
---|
423 | |
---|
424 | if( isPieceData ) |
---|
425 | bytesUsed( &band->piece, byteCount ); |
---|
426 | |
---|
427 | if( b->parent != NULL ) |
---|
428 | tr_bandwidthUsed( b->parent, dir, byteCount, isPieceData ); |
---|
429 | } |
---|