source: trunk/libtransmission/completion.c @ 2485

Last change on this file since 2485 was 2485, checked in by charles, 14 years ago

add some assertion tests for #transmission

  • Property svn:keywords set to Date Rev Author Id
File size: 9.4 KB
Line 
1/******************************************************************************
2 * $Id: completion.c 2485 2007-07-24 20:49:47Z 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
28struct 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    /* rather than calculating these over and over again in loops,
45       just calculate them once */
46    int nBlocksInPiece;
47    int nBlocksInLastPiece;
48};
49
50#define tr_cpCountBlocks(cp,piece) (piece==cp->tor->info.pieceCount-1 \
51    ? cp->nBlocksInLastPiece \
52    : cp->nBlocksInPiece)
53
54tr_completion_t * tr_cpInit( tr_torrent_t * tor )
55{
56    tr_completion_t * cp;
57
58    cp                   = tr_new( tr_completion_t, 1 );
59    cp->tor              = tor;
60    cp->blockBitfield    = tr_bitfieldNew( tor->blockCount );
61    cp->blockDownloaders = tr_new( uint8_t, tor->blockCount );
62    cp->pieceBitfield    = tr_bitfieldNew( tor->info.pieceCount );
63    cp->completeBlocks   = tr_new( int, tor->info.pieceCount );
64
65    cp->nBlocksInLastPiece = tr_pieceCountBlocks( tor->info.pieceCount - 1 );
66    cp->nBlocksInPiece = tor->info.pieceCount==1 ? cp->nBlocksInLastPiece
67                                                 : tr_pieceCountBlocks( 0 );
68
69    tr_cpReset( cp );
70
71    return cp;
72}
73
74void tr_cpClose( tr_completion_t * cp )
75{
76    tr_free(         cp->completeBlocks );
77    tr_bitfieldFree( cp->pieceBitfield );
78    tr_free(         cp->blockDownloaders );
79    tr_bitfieldFree( cp->blockBitfield );
80    tr_free(         cp );
81}
82
83void tr_cpReset( tr_completion_t * cp )
84{
85    tr_torrent_t * tor = cp->tor;
86    int i;
87
88    tr_bitfieldClear( cp->blockBitfield );
89    memset( cp->blockDownloaders, 0, tor->blockCount );
90    tr_bitfieldClear( cp->pieceBitfield );
91    for( i = 0; i < tor->info.pieceCount; ++i )
92        cp->completeBlocks[i] = 0;
93}
94
95int tr_cpPieceHasAllBlocks( const tr_completion_t * cp, int piece )
96{
97    return tr_cpPieceIsComplete( cp, piece );
98}
99
100int tr_cpPieceIsComplete( const tr_completion_t * cp, int piece )
101{
102    return cp->completeBlocks[piece] >= tr_cpCountBlocks(cp,piece);
103}
104
105const tr_bitfield_t * tr_cpPieceBitfield( const tr_completion_t * cp )
106{
107    return cp->pieceBitfield;
108}
109
110void tr_cpPieceAdd( tr_completion_t * cp, int piece )
111{
112    const tr_torrent_t * tor = cp->tor;
113    const int n_blocks = tr_cpCountBlocks( cp, piece );
114    const int startBlock = tr_pieceStartBlock( piece );
115    const int endBlock   = startBlock + n_blocks;
116
117    cp->completeBlocks[piece] = n_blocks;
118    tr_bitfieldAddRange( cp->blockBitfield, startBlock, endBlock );
119    tr_bitfieldAdd( cp->pieceBitfield, piece );
120}
121
122void tr_cpPieceRem( tr_completion_t * cp, int piece )
123{
124    const tr_torrent_t * tor = cp->tor;
125    const int n_blocks = tr_cpCountBlocks( cp, piece );
126    const int startBlock = tr_pieceStartBlock( piece );
127    const int endBlock   = startBlock + n_blocks;
128
129    assert( cp != NULL );
130    assert( 0 <= piece );
131    assert( piece < tor->info.pieceCount );
132    assert( 0 <= startBlock );
133    assert( startBlock < tor->blockCount );
134    assert( startBlock <= endBlock );
135    assert( endBlock < tor->blockCount );
136
137    cp->completeBlocks[piece] = 0;
138    tr_bitfieldRemRange ( cp->blockBitfield, startBlock, endBlock );
139    tr_bitfieldRem( cp->pieceBitfield, piece );
140}
141
142/* Blocks */
143void tr_cpDownloaderAdd( tr_completion_t * cp, int block )
144{
145    ++cp->blockDownloaders[block];
146}
147
148void tr_cpDownloaderRem( tr_completion_t * cp, int block )
149{
150    --cp->blockDownloaders[block];
151}
152
153int tr_cpBlockIsComplete( const tr_completion_t * cp, int block )
154{
155    return tr_bitfieldHas( cp->blockBitfield, block );
156}
157
158void tr_cpBlockAdd( tr_completion_t * cp, int block )
159{
160    const tr_torrent_t * tor;
161
162    assert( cp != NULL );
163    assert( cp->tor != NULL );
164    assert( 0 <= block );
165
166    tor = cp->tor;
167
168    if( !tr_cpBlockIsComplete( cp, block ) )
169    {
170        const int piece = block / cp->nBlocksInPiece;
171
172        ++cp->completeBlocks[piece];
173
174        if( cp->completeBlocks[piece] == tr_cpCountBlocks( cp, piece ) )
175            tr_bitfieldAdd( cp->pieceBitfield, piece );
176
177        tr_bitfieldAdd( cp->blockBitfield, block );
178    }
179}
180
181const tr_bitfield_t * tr_cpBlockBitfield( const tr_completion_t * cp )
182{
183    assert( cp != NULL );
184
185    return cp->blockBitfield;
186}
187
188void
189tr_cpBlockBitfieldSet( tr_completion_t * cp, tr_bitfield_t * bitfield )
190{
191    int i;
192
193    assert( cp != NULL );
194    assert( bitfield != NULL );
195
196    tr_cpReset( cp );
197
198    for( i=0; i < cp->tor->blockCount; ++i )
199        if( tr_bitfieldHas( bitfield, i ) )
200            tr_cpBlockAdd( cp, i );
201}
202
203float tr_cpPercentBlocksInPiece( const tr_completion_t * cp, int piece )
204{
205    assert( cp != NULL );
206
207    return cp->completeBlocks[piece] / (double)tr_cpCountBlocks( cp, piece );
208}
209
210int
211tr_cpMissingBlocksForPiece( const tr_completion_t * cp, int piece )
212{
213    int i;
214    int n;
215    const tr_torrent_t * tor = cp->tor;
216    const int start = tr_pieceStartBlock( piece );
217    const int end   = start + tr_cpCountBlocks( cp, piece );
218
219    n = 0;
220    for( i = start; i < end; ++i )
221        if( !tr_cpBlockIsComplete( cp, i ) && !cp->blockDownloaders[i] )
222            ++n;
223
224    return n;
225}
226
227int tr_cpMissingBlockInPiece( const tr_completion_t * cp, int piece )
228{
229    int i;
230    const tr_torrent_t * tor = cp->tor;
231    const int start = tr_pieceStartBlock( piece );
232    const int end   = start + tr_cpCountBlocks( cp, piece );
233
234    for( i = start; i < end; ++i )
235        if( !tr_cpBlockIsComplete( cp, i ) && !cp->blockDownloaders[i] )
236            return i;
237
238    return -1;
239}
240
241/***
242****
243***/
244
245cp_status_t
246tr_cpGetStatus ( const tr_completion_t * cp )
247{
248    int i;
249    int ret = TR_CP_COMPLETE;
250    const tr_info_t * info;
251
252    assert( cp != NULL );
253    assert( cp->tor != NULL );
254
255    info = &cp->tor->info;
256    for( i=0; i<info->pieceCount; ++i ) {
257        if( tr_cpPieceIsComplete( cp, i ) )
258            continue;
259        if( !info->pieces[i].dnd )
260            return TR_CP_INCOMPLETE;
261        ret = TR_CP_DONE;
262    }
263
264    return ret;
265}
266
267uint64_t
268tr_cpLeftUntilComplete ( const tr_completion_t * cp )
269{
270    int i;
271    uint64_t b=0;
272    const tr_torrent_t * tor;
273    const tr_info_t * info;
274
275    assert( cp != NULL );
276    assert( cp->tor != NULL );
277
278    tor = cp->tor;
279    info = &tor->info;
280    for( i=0; i<info->pieceCount; ++i )
281        if( !tr_cpPieceIsComplete( cp, i ) )
282            b += ( tr_cpCountBlocks( cp, i ) - cp->completeBlocks[ i ] );
283
284    b *= tor->blockSize;
285
286    if( tor->blockCount && !tr_cpBlockIsComplete( cp, tor->blockCount - 1 ) )
287          b -= (tor->blockSize - (tor->info.totalSize % tor->blockSize));
288
289    return b;
290}
291
292uint64_t
293tr_cpLeftUntilDone ( const tr_completion_t * cp )
294{
295    int i;
296    uint64_t b=0;
297    const tr_torrent_t * tor;
298    const tr_info_t * info;
299
300    assert( cp != NULL );
301    assert( cp->tor != NULL );
302
303    tor = cp->tor;
304    info = &tor->info;
305
306    for( i=0; i<info->pieceCount; ++i )
307        if( !tr_cpPieceIsComplete( cp, i ) && !info->pieces[i].dnd )
308            b += ( tr_cpCountBlocks( cp, i ) - cp->completeBlocks[ i ] );
309
310    b *= tor->blockSize;
311
312    if( tor->blockCount && !tr_cpBlockIsComplete( cp, tor->blockCount-1 )
313                        && !info->pieces[info->pieceCount-1].dnd )
314          b -= (tor->blockSize - (tor->info.totalSize % tor->blockSize));
315
316    return b;
317}
318
319float
320tr_cpPercentComplete ( const tr_completion_t * cp )
321{
322    const uint64_t tilComplete = tr_cpLeftUntilComplete( cp );
323    const uint64_t total = cp->tor->info.totalSize;
324    const float f = 1.0 - (double)tilComplete / total;
325    return MAX(0.0, f);
326}
327
328float
329tr_cpPercentDone( const tr_completion_t * cp )
330{
331    const uint64_t tilDone = tr_cpLeftUntilDone( cp );
332    const uint64_t total = cp->tor->info.totalSize;
333    const float f = 1.0 - (double)tilDone / total;
334    return MAX(0.0, f);
335}
336
337uint64_t
338tr_cpDownloadedValid( const tr_completion_t * cp )
339{
340    const tr_torrent_t * tor = cp->tor;
341
342    uint64_t b = tr_bitfieldCountTrueBits( cp->blockBitfield ) * tor->blockSize;
343
344    if( tor->blockCount && tr_bitfieldHas( cp->blockBitfield, tor->blockCount - 1 ) )
345        b -= (tor->blockSize - (tor->info.totalSize % tor->blockSize));
346
347   return b;
348}
Note: See TracBrowser for help on using the repository browser.