source: trunk/libtransmission/completion.c @ 2242

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