1 | /****************************************************************************** |
---|
2 | * $Id: completion.c 2513 2007-07-27 01:59:48Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005 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 "completion.h" |
---|
27 | |
---|
28 | struct tr_completion_s |
---|
29 | { |
---|
30 | tr_torrent_t * tor; |
---|
31 | |
---|
32 | /* number of peers from whom we've requested this block */ |
---|
33 | uint8_t * blockDownloaders; |
---|
34 | |
---|
35 | /* do we have this block? */ |
---|
36 | tr_bitfield_t * blockBitfield; |
---|
37 | |
---|
38 | /* do we have this piece? */ |
---|
39 | tr_bitfield_t * pieceBitfield; |
---|
40 | |
---|
41 | /* a block is complete if and only if we have it */ |
---|
42 | int * completeBlocks; |
---|
43 | }; |
---|
44 | |
---|
45 | tr_completion_t * tr_cpInit( tr_torrent_t * tor ) |
---|
46 | { |
---|
47 | tr_completion_t * cp; |
---|
48 | |
---|
49 | cp = tr_new( tr_completion_t, 1 ); |
---|
50 | cp->tor = tor; |
---|
51 | cp->blockBitfield = tr_bitfieldNew( tor->blockCount ); |
---|
52 | cp->blockDownloaders = tr_new( uint8_t, tor->blockCount ); |
---|
53 | cp->pieceBitfield = tr_bitfieldNew( tor->info.pieceCount ); |
---|
54 | cp->completeBlocks = tr_new( int, tor->info.pieceCount ); |
---|
55 | |
---|
56 | tr_cpReset( cp ); |
---|
57 | |
---|
58 | return cp; |
---|
59 | } |
---|
60 | |
---|
61 | void tr_cpClose( tr_completion_t * cp ) |
---|
62 | { |
---|
63 | tr_free( cp->completeBlocks ); |
---|
64 | tr_bitfieldFree( cp->pieceBitfield ); |
---|
65 | tr_free( cp->blockDownloaders ); |
---|
66 | tr_bitfieldFree( cp->blockBitfield ); |
---|
67 | tr_free( cp ); |
---|
68 | } |
---|
69 | |
---|
70 | void tr_cpReset( tr_completion_t * cp ) |
---|
71 | { |
---|
72 | tr_torrent_t * tor = cp->tor; |
---|
73 | int i; |
---|
74 | |
---|
75 | tr_bitfieldClear( cp->blockBitfield ); |
---|
76 | memset( cp->blockDownloaders, 0, tor->blockCount ); |
---|
77 | tr_bitfieldClear( cp->pieceBitfield ); |
---|
78 | for( i = 0; i < tor->info.pieceCount; ++i ) |
---|
79 | cp->completeBlocks[i] = 0; |
---|
80 | } |
---|
81 | |
---|
82 | int tr_cpPieceHasAllBlocks( const tr_completion_t * cp, int piece ) |
---|
83 | { |
---|
84 | return tr_cpPieceIsComplete( cp, piece ); |
---|
85 | } |
---|
86 | |
---|
87 | int tr_cpPieceIsComplete( const tr_completion_t * cp, int piece ) |
---|
88 | { |
---|
89 | return cp->completeBlocks[piece] >= TR_BLOCKS_IN_PIECE(cp->tor,piece); |
---|
90 | } |
---|
91 | |
---|
92 | const tr_bitfield_t * tr_cpPieceBitfield( const tr_completion_t * cp ) |
---|
93 | { |
---|
94 | return cp->pieceBitfield; |
---|
95 | } |
---|
96 | |
---|
97 | void tr_cpPieceAdd( tr_completion_t * cp, int piece ) |
---|
98 | { |
---|
99 | const tr_torrent_t * tor = cp->tor; |
---|
100 | const int n_blocks = TR_BLOCKS_IN_PIECE(tor,piece); |
---|
101 | const int startBlock = TOR_PIECE_FIRST_BLOCK(tor,piece); |
---|
102 | const int endBlock = startBlock + n_blocks; |
---|
103 | |
---|
104 | cp->completeBlocks[piece] = n_blocks; |
---|
105 | tr_bitfieldAddRange( cp->blockBitfield, startBlock, endBlock ); |
---|
106 | tr_bitfieldAdd( cp->pieceBitfield, piece ); |
---|
107 | } |
---|
108 | |
---|
109 | void tr_cpPieceRem( tr_completion_t * cp, int piece ) |
---|
110 | { |
---|
111 | const tr_torrent_t * tor = cp->tor; |
---|
112 | const int n_blocks = TR_BLOCKS_IN_PIECE(tor,piece); |
---|
113 | const int startBlock = TOR_PIECE_FIRST_BLOCK(tor,piece); |
---|
114 | const int endBlock = startBlock + n_blocks; |
---|
115 | |
---|
116 | assert( cp != NULL ); |
---|
117 | assert( 0 <= piece ); |
---|
118 | assert( piece < tor->info.pieceCount ); |
---|
119 | assert( 0 <= startBlock ); |
---|
120 | assert( startBlock < tor->blockCount ); |
---|
121 | assert( startBlock <= endBlock ); |
---|
122 | assert( endBlock <= tor->blockCount ); |
---|
123 | |
---|
124 | cp->completeBlocks[piece] = 0; |
---|
125 | tr_bitfieldRemRange ( cp->blockBitfield, startBlock, endBlock ); |
---|
126 | tr_bitfieldRem( cp->pieceBitfield, piece ); |
---|
127 | } |
---|
128 | |
---|
129 | /* Blocks */ |
---|
130 | void tr_cpDownloaderAdd( tr_completion_t * cp, int block ) |
---|
131 | { |
---|
132 | ++cp->blockDownloaders[block]; |
---|
133 | } |
---|
134 | |
---|
135 | void tr_cpDownloaderRem( tr_completion_t * cp, int block ) |
---|
136 | { |
---|
137 | --cp->blockDownloaders[block]; |
---|
138 | } |
---|
139 | |
---|
140 | int tr_cpBlockIsComplete( const tr_completion_t * cp, int block ) |
---|
141 | { |
---|
142 | return tr_bitfieldHas( cp->blockBitfield, block ); |
---|
143 | } |
---|
144 | |
---|
145 | void tr_cpBlockAdd( tr_completion_t * cp, int block ) |
---|
146 | { |
---|
147 | const tr_torrent_t * tor = cp->tor; |
---|
148 | |
---|
149 | if( !tr_cpBlockIsComplete( cp, block ) ) |
---|
150 | { |
---|
151 | const int piece = TOR_BLOCK_PIECE(tor, block); |
---|
152 | |
---|
153 | ++cp->completeBlocks[piece]; |
---|
154 | |
---|
155 | if( cp->completeBlocks[piece] == TR_BLOCKS_IN_PIECE(tor,piece) ) |
---|
156 | tr_bitfieldAdd( cp->pieceBitfield, piece ); |
---|
157 | |
---|
158 | tr_bitfieldAdd( cp->blockBitfield, block ); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | const tr_bitfield_t * tr_cpBlockBitfield( const tr_completion_t * cp ) |
---|
163 | { |
---|
164 | assert( cp != NULL ); |
---|
165 | |
---|
166 | return cp->blockBitfield; |
---|
167 | } |
---|
168 | |
---|
169 | void |
---|
170 | tr_cpBlockBitfieldSet( tr_completion_t * cp, tr_bitfield_t * bitfield ) |
---|
171 | { |
---|
172 | int i; |
---|
173 | |
---|
174 | assert( cp != NULL ); |
---|
175 | assert( bitfield != NULL ); |
---|
176 | |
---|
177 | tr_cpReset( cp ); |
---|
178 | |
---|
179 | for( i=0; i < cp->tor->blockCount; ++i ) |
---|
180 | if( tr_bitfieldHas( bitfield, i ) ) |
---|
181 | tr_cpBlockAdd( cp, i ); |
---|
182 | } |
---|
183 | |
---|
184 | float tr_cpPercentBlocksInPiece( const tr_completion_t * cp, int piece ) |
---|
185 | { |
---|
186 | assert( cp != NULL ); |
---|
187 | |
---|
188 | return cp->completeBlocks[piece] / (double)TR_BLOCKS_IN_PIECE(cp->tor,piece); |
---|
189 | } |
---|
190 | |
---|
191 | int |
---|
192 | tr_cpMissingBlocksForPiece( const tr_completion_t * cp, int piece ) |
---|
193 | { |
---|
194 | int i; |
---|
195 | int n; |
---|
196 | const tr_torrent_t * tor = cp->tor; |
---|
197 | const int start = TOR_PIECE_FIRST_BLOCK(tor,piece); |
---|
198 | const int end = start + TR_BLOCKS_IN_PIECE(tor,piece); |
---|
199 | |
---|
200 | n = 0; |
---|
201 | for( i = start; i < end; ++i ) |
---|
202 | if( !tr_cpBlockIsComplete( cp, i ) && !cp->blockDownloaders[i] ) |
---|
203 | ++n; |
---|
204 | |
---|
205 | return n; |
---|
206 | } |
---|
207 | |
---|
208 | int tr_cpMissingBlockInPiece( const tr_completion_t * cp, int piece ) |
---|
209 | { |
---|
210 | int i; |
---|
211 | const tr_torrent_t * tor = cp->tor; |
---|
212 | const int start = TOR_PIECE_FIRST_BLOCK(tor,piece); |
---|
213 | const int end = start + TR_BLOCKS_IN_PIECE(tor,piece); |
---|
214 | |
---|
215 | for( i = start; i < end; ++i ) |
---|
216 | if( !tr_cpBlockIsComplete( cp, i ) && !cp->blockDownloaders[i] ) |
---|
217 | return i; |
---|
218 | |
---|
219 | return -1; |
---|
220 | } |
---|
221 | |
---|
222 | /*** |
---|
223 | **** |
---|
224 | ***/ |
---|
225 | |
---|
226 | cp_status_t |
---|
227 | tr_cpGetStatus ( const tr_completion_t * cp ) |
---|
228 | { |
---|
229 | int i; |
---|
230 | int ret = TR_CP_COMPLETE; |
---|
231 | const tr_info_t * info; |
---|
232 | |
---|
233 | assert( cp != NULL ); |
---|
234 | assert( cp->tor != NULL ); |
---|
235 | |
---|
236 | info = &cp->tor->info; |
---|
237 | for( i=0; i<info->pieceCount; ++i ) { |
---|
238 | if( tr_cpPieceIsComplete( cp, i ) ) |
---|
239 | continue; |
---|
240 | if( !info->pieces[i].dnd ) |
---|
241 | return TR_CP_INCOMPLETE; |
---|
242 | ret = TR_CP_DONE; |
---|
243 | } |
---|
244 | |
---|
245 | return ret; |
---|
246 | } |
---|
247 | |
---|
248 | uint64_t |
---|
249 | tr_cpLeftUntilComplete ( const tr_completion_t * cp ) |
---|
250 | { |
---|
251 | int i; |
---|
252 | uint64_t b=0; |
---|
253 | const tr_torrent_t * tor; |
---|
254 | const tr_info_t * info; |
---|
255 | |
---|
256 | assert( cp != NULL ); |
---|
257 | assert( cp->tor != NULL ); |
---|
258 | |
---|
259 | tor = cp->tor; |
---|
260 | info = &tor->info; |
---|
261 | for( i=0; i<info->pieceCount; ++i ) |
---|
262 | if( !tr_cpPieceIsComplete( cp, i ) ) |
---|
263 | b += ( TR_BLOCKS_IN_PIECE(tor,i) - cp->completeBlocks[ i ] ); |
---|
264 | |
---|
265 | b *= tor->blockSize; |
---|
266 | |
---|
267 | if( tor->blockCount && !tr_cpBlockIsComplete( cp, tor->blockCount - 1 ) ) |
---|
268 | b -= (tor->blockSize - (tor->info.totalSize % tor->blockSize)); |
---|
269 | |
---|
270 | return b; |
---|
271 | } |
---|
272 | |
---|
273 | void |
---|
274 | tr_cpDoneStats( const tr_completion_t * cp , |
---|
275 | uint64_t * setmeHaveBytes, |
---|
276 | uint64_t * setmeTotalBytes ) |
---|
277 | { |
---|
278 | const tr_torrent_t * tor = cp->tor; |
---|
279 | const tr_info_t * info = &tor->info; |
---|
280 | uint64_t have=0, total=0; |
---|
281 | int i; |
---|
282 | |
---|
283 | for( i=0; i<info->pieceCount; ++i ) { |
---|
284 | if( !info->pieces[i].dnd ) { |
---|
285 | total += info->pieceSize; |
---|
286 | have += cp->completeBlocks[ i ]; |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|
290 | have *= tor->blockSize; |
---|
291 | |
---|
292 | /* the last piece/block is probably smaller than the others */ |
---|
293 | if( !info->pieces[info->pieceCount-1].dnd ) { |
---|
294 | total -= ( info->pieceSize - ( info->totalSize % info->pieceSize ) ); |
---|
295 | if( tr_cpBlockIsComplete( cp, tor->blockCount-1 ) ) |
---|
296 | have -= ( tor->blockSize - ( info->totalSize % tor->blockSize ) ); |
---|
297 | } |
---|
298 | |
---|
299 | assert( have <= total ); |
---|
300 | assert( total <= info->totalSize ); |
---|
301 | |
---|
302 | *setmeHaveBytes = have; |
---|
303 | *setmeTotalBytes = total; |
---|
304 | } |
---|
305 | |
---|
306 | float |
---|
307 | tr_cpPercentComplete ( const tr_completion_t * cp ) |
---|
308 | { |
---|
309 | const uint64_t tilComplete = tr_cpLeftUntilComplete( cp ); |
---|
310 | const uint64_t total = cp->tor->info.totalSize; |
---|
311 | const float f = 1.0 - (double)tilComplete / total; |
---|
312 | return MAX(0.0, f); |
---|
313 | } |
---|
314 | |
---|
315 | uint64_t |
---|
316 | tr_cpDownloadedValid( const tr_completion_t * cp ) |
---|
317 | { |
---|
318 | uint64_t b = 0; |
---|
319 | const tr_torrent_t * tor = cp->tor; |
---|
320 | const tr_info_t * info = &tor->info; |
---|
321 | int i; |
---|
322 | |
---|
323 | for( i=0; i<info->pieceCount; ++i ) |
---|
324 | if( tr_cpPieceIsComplete( cp, i ) ) |
---|
325 | ++b; |
---|
326 | |
---|
327 | b *= tor->blockSize; |
---|
328 | |
---|
329 | if( tor->blockCount && tr_bitfieldHas( cp->blockBitfield, tor->blockCount - 1 ) ) |
---|
330 | b -= (tor->blockSize - (tor->info.totalSize % tor->blockSize)); |
---|
331 | |
---|
332 | return b; |
---|
333 | } |
---|