1 | /****************************************************************************** |
---|
2 | * $Id: ratecontrol.c 2388 2007-07-18 05:27:45Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2006 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #include "transmission.h" |
---|
26 | #include "shared.h" |
---|
27 | |
---|
28 | #define GRANULARITY_MSEC 250 |
---|
29 | #define SHORT_INTERVAL_MSEC 3000 |
---|
30 | #define LONG_INTERVAL_MSEC 20000 |
---|
31 | #define HISTORY_SIZE (LONG_INTERVAL_MSEC / GRANULARITY_MSEC) |
---|
32 | |
---|
33 | typedef struct |
---|
34 | { |
---|
35 | uint64_t date; |
---|
36 | int size; |
---|
37 | } |
---|
38 | tr_transfer_t; |
---|
39 | |
---|
40 | struct tr_ratecontrol_s |
---|
41 | { |
---|
42 | tr_rwlock_t lock; |
---|
43 | int limit; |
---|
44 | int newest; |
---|
45 | tr_transfer_t transfers[HISTORY_SIZE]; |
---|
46 | }; |
---|
47 | |
---|
48 | /* return the xfer rate over the last `interval' seconds in KiB/sec */ |
---|
49 | static float |
---|
50 | rateForInterval( const tr_ratecontrol_t * r, int interval_msec ) |
---|
51 | { |
---|
52 | uint64_t bytes = 0; |
---|
53 | const uint64_t now = tr_date (); |
---|
54 | int i = r->newest; |
---|
55 | for( ;; ) |
---|
56 | { |
---|
57 | if( r->transfers[i].date + interval_msec < now ) |
---|
58 | break; |
---|
59 | |
---|
60 | bytes += r->transfers[i].size; |
---|
61 | |
---|
62 | if( --i == -1 ) i = HISTORY_SIZE - 1; /* circular history */ |
---|
63 | if( i == r->newest ) break; /* we've come all the way around */ |
---|
64 | } |
---|
65 | |
---|
66 | return (bytes/1024.0) * (1000.0/interval_msec); |
---|
67 | } |
---|
68 | |
---|
69 | /*** |
---|
70 | **** |
---|
71 | ***/ |
---|
72 | |
---|
73 | tr_ratecontrol_t* |
---|
74 | tr_rcInit( void ) |
---|
75 | { |
---|
76 | tr_ratecontrol_t * r = tr_new0( tr_ratecontrol_t, 1 ); |
---|
77 | r->limit = -1; |
---|
78 | tr_rwInit( &r->lock ); |
---|
79 | return r; |
---|
80 | } |
---|
81 | |
---|
82 | void |
---|
83 | tr_rcClose( tr_ratecontrol_t * r ) |
---|
84 | { |
---|
85 | tr_rcReset( r ); |
---|
86 | tr_rwClose( &r->lock ); |
---|
87 | tr_free( r ); |
---|
88 | } |
---|
89 | |
---|
90 | /*** |
---|
91 | **** |
---|
92 | ***/ |
---|
93 | |
---|
94 | int |
---|
95 | tr_rcCanTransfer( const tr_ratecontrol_t * r ) |
---|
96 | { |
---|
97 | int ret; |
---|
98 | tr_rwReaderLock( (tr_rwlock_t*)&r->lock ); |
---|
99 | |
---|
100 | if( r->limit < 0 ) /* unbounded */ |
---|
101 | ret = TRUE; |
---|
102 | else if( !r->limit ) /* off */ |
---|
103 | ret = FALSE; |
---|
104 | else |
---|
105 | ret = rateForInterval( r, SHORT_INTERVAL_MSEC ) < r->limit; |
---|
106 | |
---|
107 | tr_rwReaderUnlock( (tr_rwlock_t*)&r->lock ); |
---|
108 | return ret; |
---|
109 | } |
---|
110 | |
---|
111 | float |
---|
112 | tr_rcRate( const tr_ratecontrol_t * r ) |
---|
113 | { |
---|
114 | float ret; |
---|
115 | tr_rwReaderLock( (tr_rwlock_t*)&r->lock ); |
---|
116 | |
---|
117 | ret = rateForInterval( r, LONG_INTERVAL_MSEC ); |
---|
118 | |
---|
119 | tr_rwReaderUnlock( (tr_rwlock_t*)&r->lock ); |
---|
120 | return ret; |
---|
121 | } |
---|
122 | |
---|
123 | /*** |
---|
124 | **** |
---|
125 | ***/ |
---|
126 | |
---|
127 | void |
---|
128 | tr_rcTransferred( tr_ratecontrol_t * r, int size ) |
---|
129 | { |
---|
130 | uint64_t now; |
---|
131 | |
---|
132 | if( size < 100 ) /* don't count small messages */ |
---|
133 | return; |
---|
134 | |
---|
135 | tr_rwWriterLock( &r->lock ); |
---|
136 | |
---|
137 | now = tr_date (); |
---|
138 | if( r->transfers[r->newest].date + GRANULARITY_MSEC >= now ) |
---|
139 | r->transfers[r->newest].size += size; |
---|
140 | else { |
---|
141 | if( ++r->newest == HISTORY_SIZE ) r->newest = 0; |
---|
142 | r->transfers[r->newest].date = now; |
---|
143 | r->transfers[r->newest].size = size; |
---|
144 | } |
---|
145 | |
---|
146 | tr_rwWriterUnlock( &r->lock ); |
---|
147 | } |
---|
148 | |
---|
149 | void |
---|
150 | tr_rcReset( tr_ratecontrol_t * r ) |
---|
151 | { |
---|
152 | tr_rwWriterLock( &r->lock ); |
---|
153 | r->newest = 0; |
---|
154 | memset( r->transfers, 0, sizeof(tr_transfer_t) * HISTORY_SIZE ); |
---|
155 | tr_rwWriterUnlock( &r->lock ); |
---|
156 | } |
---|
157 | |
---|
158 | void |
---|
159 | tr_rcSetLimit( tr_ratecontrol_t * r, int limit ) |
---|
160 | { |
---|
161 | tr_rwWriterLock( &r->lock ); |
---|
162 | r->limit = limit; |
---|
163 | tr_rwWriterUnlock( &r->lock ); |
---|
164 | } |
---|
165 | |
---|
166 | int |
---|
167 | tr_rcGetLimit( const tr_ratecontrol_t * r ) |
---|
168 | { |
---|
169 | return r->limit; |
---|
170 | } |
---|