source: trunk/libtransmission/completion.c @ 5682

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

#894: wont start - cp->doneHave <= cp->completeHave

  • Property svn:keywords set to Date Rev Author Id
File size: 8.0 KB
Line 
1/******************************************************************************
2 * $Id: completion.c 5682 2008-04-24 15:25:01Z charles $
3 *
4 * Copyright (c) 2005-2008 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 <assert.h>
26#include <string.h>
27
28#include "transmission.h"
29#include "completion.h"
30#include "torrent.h"
31#include "utils.h"
32
33struct tr_completion
34{
35    unsigned int sizeWhenDoneIsDirty : 1;
36
37    tr_torrent * tor;
38
39    /* do we have this block? */
40    tr_bitfield * blockBitfield;
41
42    /* do we have this piece? */
43    tr_bitfield * pieceBitfield;
44
45    /* a block is complete if and only if we have it */
46    uint16_t * completeBlocks;
47
48    /* number of bytes we'll have when done downloading. [0..info.totalSize]
49       DON'T access this directly; it's a lazy field.
50       use tr_cpSizeWhenDone() instead! */
51    uint64_t sizeWhenDoneLazy;
52
53    /* number of bytes we want or have now. [0..sizeWhenDone] */
54    uint64_t sizeNow;
55};
56
57static void
58tr_cpReset( tr_completion * cp )
59{
60    tr_bitfieldClear( cp->pieceBitfield );
61    tr_bitfieldClear( cp->blockBitfield );
62    memset( cp->completeBlocks, 0, sizeof(uint16_t) * cp->tor->info.pieceCount );
63    cp->sizeNow = 0;
64    cp->sizeWhenDoneIsDirty = 1;
65}
66
67tr_completion *
68tr_cpInit( tr_torrent * tor )
69{
70    tr_completion * cp  = tr_new( tr_completion, 1 );
71    cp->tor             = tor;
72    cp->blockBitfield   = tr_bitfieldNew( tor->blockCount );
73    cp->pieceBitfield   = tr_bitfieldNew( tor->info.pieceCount );
74    cp->completeBlocks  = tr_new( uint16_t, tor->info.pieceCount );
75    tr_cpReset( cp );
76    return cp;
77}
78
79void
80tr_cpClose( tr_completion * cp )
81{
82    tr_free        ( cp->completeBlocks );
83    tr_bitfieldFree( cp->pieceBitfield );
84    tr_bitfieldFree( cp->blockBitfield );
85    tr_free        ( cp );
86}
87
88void
89tr_cpInvalidateDND ( tr_completion * cp )
90{
91    cp->sizeWhenDoneIsDirty = 1;
92}
93
94uint64_t
95tr_cpSizeWhenDone( const tr_completion * ccp )
96{
97    if( ccp->sizeWhenDoneIsDirty )
98    {
99        tr_completion * cp = (tr_completion *) ccp; /* mutable */
100        const tr_info * info = &cp->tor->info;
101        tr_piece_index_t i;
102        uint64_t size = 0;
103
104        for( i=0; i<info->pieceCount; ++i )
105            if( !info->pieces[i].dnd || tr_cpPieceIsComplete( cp, i ) )
106                size += tr_torPieceCountBytes( cp->tor, i );
107
108        cp->sizeWhenDoneLazy = size;
109        cp->sizeWhenDoneIsDirty = 0;
110    }
111
112    assert( ccp->sizeWhenDoneLazy <= ccp->tor->info.totalSize );
113    assert( ccp->sizeWhenDoneLazy >= ccp->sizeNow );
114    return ccp->sizeWhenDoneLazy;
115}
116
117int
118tr_cpPieceIsComplete( const tr_completion  * cp,
119                      tr_piece_index_t       piece )
120{
121    return cp->completeBlocks[piece] == tr_torPieceCountBlocks(cp->tor,piece);
122}
123
124const tr_bitfield *
125tr_cpPieceBitfield( const tr_completion * cp )
126{
127    return cp->pieceBitfield;
128}
129
130void
131tr_cpPieceAdd( tr_completion * cp, tr_piece_index_t piece )
132{
133    const tr_torrent * tor = cp->tor;
134    const tr_block_index_t start = tr_torPieceFirstBlock(tor,piece);
135    const tr_block_index_t end = start + tr_torPieceCountBlocks(tor, piece);
136    tr_block_index_t i;
137
138    for( i=start; i<end; ++i )
139        tr_cpBlockAdd( cp, i );
140}
141
142void
143tr_cpPieceRem( tr_completion * cp, tr_piece_index_t piece )
144{
145    const tr_torrent * tor = cp->tor;
146    const tr_block_index_t start = tr_torPieceFirstBlock( tor, piece );
147    const tr_block_index_t end = start + tr_torPieceCountBlocks( tor, piece );
148    tr_block_index_t block;
149
150    assert( cp != NULL );
151    assert( piece < tor->info.pieceCount );
152    assert( start < tor->blockCount );
153    assert( start <= end );
154    assert( end <= tor->blockCount );
155
156    for( block=start; block<end; ++block )
157        if( tr_cpBlockIsComplete( cp, block ) )
158            cp->sizeNow -= tr_torBlockCountBytes( tor, block );
159
160    cp->sizeWhenDoneIsDirty = 1;
161    cp->completeBlocks[piece] = 0;
162    tr_bitfieldRemRange ( cp->blockBitfield, start, end );
163    tr_bitfieldRem( cp->pieceBitfield, piece );
164}
165
166int
167tr_cpBlockIsComplete( const tr_completion * cp, tr_block_index_t block )
168{
169    return tr_bitfieldHas( cp->blockBitfield, block );
170}
171
172void
173tr_cpBlockAdd( tr_completion * cp, tr_block_index_t block )
174{
175    const tr_torrent * tor = cp->tor;
176
177    if( !tr_cpBlockIsComplete( cp, block ) )
178    {
179        const tr_piece_index_t piece = tr_torBlockPiece( tor, block );
180        const int blockSize = tr_torBlockCountBytes( tor, block );
181
182        ++cp->completeBlocks[piece];
183
184        if( cp->completeBlocks[piece] == tr_torPieceCountBlocks( tor, piece ) )
185            tr_bitfieldAdd( cp->pieceBitfield, piece );
186
187        tr_bitfieldAdd( cp->blockBitfield, block );
188
189        cp->sizeNow += blockSize;
190
191        cp->sizeWhenDoneIsDirty = 1;
192    }
193}
194
195const tr_bitfield *
196tr_cpBlockBitfield( const tr_completion * cp )
197{
198    assert( cp );
199    assert( cp->blockBitfield );
200    assert( cp->blockBitfield->bits );
201    assert( cp->blockBitfield->len );
202
203    return cp->blockBitfield;
204}
205
206tr_errno
207tr_cpBlockBitfieldSet( tr_completion * cp, tr_bitfield * bitfield )
208{
209    tr_block_index_t i;
210
211    assert( cp );
212    assert( bitfield );
213    assert( cp->blockBitfield );
214
215    if( !cp || !bitfield || ( bitfield->len != cp->blockBitfield->len ) )
216        return TR_ERROR_ASSERT;
217
218    tr_cpReset( cp );
219    for( i=0; i < cp->tor->blockCount; ++i )
220        if( tr_bitfieldHas( bitfield, i ) )
221            tr_cpBlockAdd( cp, i );
222
223    return 0;
224}
225
226int
227tr_cpMissingBlocksInPiece( const tr_completion * cp, tr_piece_index_t piece )
228{
229    return tr_torPieceCountBlocks( cp->tor, piece ) - cp->completeBlocks[piece];
230}
231
232/***
233****
234***/
235
236float
237tr_cpPercentDone( const tr_completion * cp )
238{
239    return tr_getRatio( cp->sizeNow, tr_cpSizeWhenDone(cp) );
240}
241
242float
243tr_cpPercentComplete ( const tr_completion * cp )
244{
245    return tr_getRatio( cp->sizeNow, cp->tor->info.totalSize );
246}
247
248uint64_t
249tr_cpLeftUntilDone ( const tr_completion * cp )
250{
251    return tr_cpSizeWhenDone(cp) - cp->sizeNow;
252}
253
254uint64_t
255tr_cpLeftUntilComplete ( const tr_completion * cp )
256{
257    return cp->tor->info.totalSize - cp->sizeNow;
258}
259
260cp_status_t
261tr_cpGetStatus ( const tr_completion * cp )
262{
263    if( cp->sizeNow == cp->tor->info.totalSize ) return TR_CP_COMPLETE;
264    if( cp->sizeNow == tr_cpSizeWhenDone(cp) ) return TR_CP_DONE;
265    return TR_CP_INCOMPLETE;
266}
267
268uint64_t
269tr_cpHaveValid( const tr_completion * cp )
270{
271    uint64_t b = 0;
272    tr_piece_index_t i;
273    const tr_torrent * tor = cp->tor;
274
275    for( i=0; i<tor->info.pieceCount; ++i )
276        if( tr_cpPieceIsComplete( cp, i ) )
277            b += tr_torPieceCountBytes( tor, i );
278
279    return b;
280}
281
282uint64_t
283tr_cpHaveTotal( const tr_completion * cp )
284{
285    return cp->sizeNow;
286}
287
288void
289tr_cpGetAmountDone( const tr_completion * cp, float * tab, int tabCount )
290{
291    const int tabSpan = tabCount / cp->tor->blockCount;
292    tr_block_index_t block_i = 0;
293    int tab_i;
294    for( tab_i=0; tab_i<tabCount; ++tab_i ) {
295        int loop, have;
296        for( loop=have=0; loop<tabSpan; ++loop )
297            if( tr_cpBlockIsComplete( cp, block_i++ ) )
298                ++have;
299        tab[tab_i] = (float)have / tabSpan;
300    }
301}
Note: See TracBrowser for help on using the repository browser.