1 | /****************************************************************************** |
---|
2 | * $Id: ratecontrol.c 3900 2007-11-20 03:11:50Z 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 <string.h> /* memset */ |
---|
26 | |
---|
27 | #include "transmission.h" |
---|
28 | #include "platform.h" |
---|
29 | #include "ratecontrol.h" |
---|
30 | #include "utils.h" |
---|
31 | |
---|
32 | #define GRANULARITY_MSEC 250 |
---|
33 | #define SHORT_INTERVAL_MSEC 4000 |
---|
34 | #define LONG_INTERVAL_MSEC 8000 |
---|
35 | #define HISTORY_SIZE (LONG_INTERVAL_MSEC / GRANULARITY_MSEC) |
---|
36 | |
---|
37 | struct tr_transfer |
---|
38 | { |
---|
39 | uint64_t date; |
---|
40 | uint64_t size; |
---|
41 | }; |
---|
42 | |
---|
43 | struct tr_ratecontrol |
---|
44 | { |
---|
45 | tr_lock * lock; |
---|
46 | int limit; |
---|
47 | int newest; |
---|
48 | struct tr_transfer transfers[HISTORY_SIZE]; |
---|
49 | }; |
---|
50 | |
---|
51 | /* return the xfer rate over the last `interval' seconds in KiB/sec */ |
---|
52 | static float |
---|
53 | rateForInterval( const tr_ratecontrol * r, int interval_msec ) |
---|
54 | { |
---|
55 | uint64_t bytes = 0; |
---|
56 | const uint64_t cutoff = tr_date () - interval_msec; |
---|
57 | int i = r->newest; |
---|
58 | for( ;; ) |
---|
59 | { |
---|
60 | if( r->transfers[i].date < cutoff ) |
---|
61 | break; |
---|
62 | |
---|
63 | bytes += r->transfers[i].size; |
---|
64 | |
---|
65 | if( --i == -1 ) i = HISTORY_SIZE - 1; /* circular history */ |
---|
66 | if( i == r->newest ) break; /* we've come all the way around */ |
---|
67 | } |
---|
68 | |
---|
69 | return (bytes/1024.0) * (1000.0/interval_msec); |
---|
70 | } |
---|
71 | |
---|
72 | /*** |
---|
73 | **** |
---|
74 | ***/ |
---|
75 | |
---|
76 | tr_ratecontrol* |
---|
77 | tr_rcInit( void ) |
---|
78 | { |
---|
79 | tr_ratecontrol * r = tr_new0( tr_ratecontrol, 1 ); |
---|
80 | r->limit = 0; |
---|
81 | r->lock = tr_lockNew( ); |
---|
82 | return r; |
---|
83 | } |
---|
84 | |
---|
85 | void |
---|
86 | tr_rcClose( tr_ratecontrol * r ) |
---|
87 | { |
---|
88 | tr_rcReset( r ); |
---|
89 | tr_lockFree( r->lock ); |
---|
90 | tr_free( r ); |
---|
91 | } |
---|
92 | |
---|
93 | /*** |
---|
94 | **** |
---|
95 | ***/ |
---|
96 | |
---|
97 | size_t |
---|
98 | tr_rcBytesLeft( const tr_ratecontrol * r ) |
---|
99 | { |
---|
100 | size_t bytes = 0; |
---|
101 | |
---|
102 | if( r != NULL ) |
---|
103 | { |
---|
104 | float cur, max, kb; |
---|
105 | |
---|
106 | tr_lockLock( (tr_lock*)r->lock ); |
---|
107 | |
---|
108 | cur = rateForInterval( r, SHORT_INTERVAL_MSEC ); |
---|
109 | max = r->limit; |
---|
110 | kb = max>cur ? max-cur : 0; |
---|
111 | bytes = (size_t)(kb * 1024); |
---|
112 | |
---|
113 | tr_lockUnlock( (tr_lock*)r->lock ); |
---|
114 | } |
---|
115 | |
---|
116 | return bytes; |
---|
117 | } |
---|
118 | |
---|
119 | float |
---|
120 | tr_rcRate( const tr_ratecontrol * r ) |
---|
121 | { |
---|
122 | float ret; |
---|
123 | |
---|
124 | if( r == NULL ) |
---|
125 | ret = 0.0f; |
---|
126 | else { |
---|
127 | tr_lockLock( (tr_lock*)r->lock ); |
---|
128 | ret = rateForInterval( r, LONG_INTERVAL_MSEC ); |
---|
129 | tr_lockUnlock( (tr_lock*)r->lock ); |
---|
130 | } |
---|
131 | |
---|
132 | return ret; |
---|
133 | } |
---|
134 | |
---|
135 | /*** |
---|
136 | **** |
---|
137 | ***/ |
---|
138 | |
---|
139 | void |
---|
140 | tr_rcTransferred( tr_ratecontrol * r, size_t size ) |
---|
141 | { |
---|
142 | uint64_t now; |
---|
143 | |
---|
144 | if( size < 100 ) /* don't count small messages */ |
---|
145 | return; |
---|
146 | |
---|
147 | tr_lockLock( (tr_lock*)r->lock ); |
---|
148 | |
---|
149 | now = tr_date (); |
---|
150 | if( r->transfers[r->newest].date + GRANULARITY_MSEC >= now ) |
---|
151 | r->transfers[r->newest].size += size; |
---|
152 | else { |
---|
153 | if( ++r->newest == HISTORY_SIZE ) r->newest = 0; |
---|
154 | r->transfers[r->newest].date = now; |
---|
155 | r->transfers[r->newest].size = size; |
---|
156 | } |
---|
157 | |
---|
158 | tr_lockUnlock( (tr_lock*)r->lock ); |
---|
159 | } |
---|
160 | |
---|
161 | void |
---|
162 | tr_rcReset( tr_ratecontrol * r ) |
---|
163 | { |
---|
164 | tr_lockLock( (tr_lock*)r->lock ); |
---|
165 | r->newest = 0; |
---|
166 | memset( r->transfers, 0, sizeof(struct tr_transfer) * HISTORY_SIZE ); |
---|
167 | tr_lockUnlock( (tr_lock*)r->lock ); |
---|
168 | } |
---|
169 | |
---|
170 | void |
---|
171 | tr_rcSetLimit( tr_ratecontrol * r, int limit ) |
---|
172 | { |
---|
173 | tr_lockLock( (tr_lock*)r->lock ); |
---|
174 | r->limit = limit; |
---|
175 | tr_lockUnlock( (tr_lock*)r->lock ); |
---|
176 | } |
---|
177 | |
---|
178 | int |
---|
179 | tr_rcGetLimit( const tr_ratecontrol * r ) |
---|
180 | { |
---|
181 | return r->limit; |
---|
182 | } |
---|