source: trunk/libtransmission/completion.c @ 2479

Last change on this file since 2479 was 2479, checked in by charles, 15 years ago

faster tr_cpGetStatus() and tr_bitfieldHas(), which were showing up at the top of the CPU profile.

  • Property svn:keywords set to Date Rev Author Id
File size: 10.2 KB
Line 
1/******************************************************************************
2 * $Id: completion.c 2479 2007-07-24 14:51:46Z 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    cp->completeBlocks[piece] = 0;
130    tr_bitfieldRemRange ( cp->blockBitfield, startBlock, endBlock );
131    tr_bitfieldRem( cp->pieceBitfield, piece );
132}
133
134/* Blocks */
135void tr_cpDownloaderAdd( tr_completion_t * cp, int block )
136{
137    ++cp->blockDownloaders[block];
138}
139
140void tr_cpDownloaderRem( tr_completion_t * cp, int block )
141{
142    --cp->blockDownloaders[block];
143}
144
145int tr_cpBlockIsComplete( const tr_completion_t * cp, int block )
146{
147    return tr_bitfieldHas( cp->blockBitfield, block );
148}
149
150void tr_cpBlockAdd( tr_completion_t * cp, int block )
151{
152    const tr_torrent_t * tor;
153
154    assert( cp != NULL );
155    assert( cp->tor != NULL );
156    assert( 0 <= block );
157
158    tor = cp->tor;
159
160    if( !tr_cpBlockIsComplete( cp, block ) )
161    {
162        const int piece = block / cp->nBlocksInPiece;
163
164        ++cp->completeBlocks[piece];
165
166        if( cp->completeBlocks[piece] == tr_cpCountBlocks( cp, piece ) )
167            tr_bitfieldAdd( cp->pieceBitfield, piece );
168
169        tr_bitfieldAdd( cp->blockBitfield, block );
170    }
171}
172
173const tr_bitfield_t * tr_cpBlockBitfield( const tr_completion_t * cp )
174{
175    assert( cp != NULL );
176
177    return cp->blockBitfield;
178}
179
180void
181tr_cpBlockBitfieldSet( tr_completion_t * cp, tr_bitfield_t * bitfield )
182{
183    int i;
184
185    assert( cp != NULL );
186    assert( bitfield != NULL );
187
188    tr_cpReset( cp );
189
190    for( i=0; i < cp->tor->blockCount; ++i )
191        if( tr_bitfieldHas( bitfield, i ) )
192            tr_cpBlockAdd( cp, i );
193}
194
195float tr_cpPercentBlocksInPiece( const tr_completion_t * cp, int piece )
196{
197    assert( cp != NULL );
198
199    return cp->completeBlocks[piece] / (double)tr_cpCountBlocks( cp, piece );
200}
201
202int
203tr_cpMissingBlocksForPiece( const tr_completion_t * cp, int piece )
204{
205    int i;
206    int n;
207    const tr_torrent_t * tor = cp->tor;
208    const int start = tr_pieceStartBlock( piece );
209    const int end   = start + tr_cpCountBlocks( cp, piece );
210
211    n = 0;
212    for( i = start; i < end; ++i )
213        if( !tr_cpBlockIsComplete( cp, i ) && !cp->blockDownloaders[i] )
214            ++n;
215
216    return n;
217}
218
219int tr_cpMissingBlockInPiece( const tr_completion_t * cp, int piece )
220{
221    int i;
222    const tr_torrent_t * tor = cp->tor;
223    const int start = tr_pieceStartBlock( piece );
224    const int end   = start + tr_cpCountBlocks( cp, piece );
225
226    for( i = start; i < end; ++i )
227        if( !tr_cpBlockIsComplete( cp, i ) && !cp->blockDownloaders[i] )
228            return i;
229
230    return -1;
231}
232
233int tr_cpMostMissingBlockInPiece( const tr_completion_t * cp,
234                                  int                     piece,
235                                  int                   * downloaders )
236{
237    tr_torrent_t * tor = cp->tor;
238    int start, count, end, i;
239    int * pool, poolSize, min, ret;
240
241    start = tr_pieceStartBlock( piece );
242    count = tr_cpCountBlocks( cp, piece );
243    end   = start + count;
244
245    pool     = tr_new( int, count );
246    poolSize = 0;
247    min      = 255;
248
249    for( i = start; i < end; i++ )
250    {
251        if( tr_cpBlockIsComplete( cp, i ) || cp->blockDownloaders[i] > min )
252        {
253            continue;
254        }
255        if( cp->blockDownloaders[i] < min )
256        {
257            min      = cp->blockDownloaders[i];
258            poolSize = 0;
259        }
260        if( cp->blockDownloaders[i] <= min )
261        {
262            pool[poolSize++] = i;
263        }
264    }
265
266    if( poolSize > 0 )
267    {
268        ret = pool[0];
269        *downloaders = min;
270    }
271    else
272    {
273        ret = -1;
274    }
275
276    tr_free( pool );
277    return ret;
278}
279
280
281/***
282****
283***/
284
285cp_status_t
286tr_cpGetStatus ( const tr_completion_t * cp )
287{
288    int i;
289    int ret = TR_CP_COMPLETE;
290    const tr_info_t * info;
291
292    assert( cp != NULL );
293    assert( cp->tor != NULL );
294
295    info = &cp->tor->info;
296    for( i=0; i<info->pieceCount; ++i ) {
297        if( tr_cpPieceIsComplete( cp, i ) )
298            continue;
299        if( !info->pieces[i].dnd )
300            return TR_CP_INCOMPLETE;
301        ret = TR_CP_DONE;
302    }
303
304    return ret;
305}
306
307uint64_t
308tr_cpLeftUntilComplete ( const tr_completion_t * cp )
309{
310    int i;
311    uint64_t b=0;
312    const tr_torrent_t * tor;
313    const tr_info_t * info;
314
315    assert( cp != NULL );
316    assert( cp->tor != NULL );
317
318    tor = cp->tor;
319    info = &tor->info;
320    for( i=0; i<info->pieceCount; ++i )
321        if( !tr_cpPieceIsComplete( cp, i ) )
322            b += ( tr_cpCountBlocks( cp, i ) - cp->completeBlocks[ i ] );
323
324    b *= tor->blockSize;
325
326    if( tor->blockCount && !tr_cpBlockIsComplete( cp, tor->blockCount - 1 ) )
327          b -= (tor->blockSize - (tor->info.totalSize % tor->blockSize));
328
329    return b;
330}
331
332uint64_t
333tr_cpLeftUntilDone ( const tr_completion_t * cp )
334{
335    int i;
336    uint64_t b=0;
337    const tr_torrent_t * tor;
338    const tr_info_t * info;
339
340    assert( cp != NULL );
341    assert( cp->tor != NULL );
342
343    tor = cp->tor;
344    info = &tor->info;
345
346    for( i=0; i<info->pieceCount; ++i )
347        if( !tr_cpPieceIsComplete( cp, i ) && !info->pieces[i].dnd )
348            b += ( tr_cpCountBlocks( cp, i ) - cp->completeBlocks[ i ] );
349
350    b *= tor->blockSize;
351
352    if( tor->blockCount && !tr_cpBlockIsComplete( cp, tor->blockCount-1 )
353                        && !info->pieces[info->pieceCount-1].dnd )
354          b -= (tor->blockSize - (tor->info.totalSize % tor->blockSize));
355
356    return b;
357}
358
359float
360tr_cpPercentComplete ( const tr_completion_t * cp )
361{
362    const uint64_t tilComplete = tr_cpLeftUntilComplete( cp );
363    const uint64_t total = cp->tor->info.totalSize;
364    const float f = 1.0 - (double)tilComplete / total;
365    return MAX(0.0, f);
366}
367
368float
369tr_cpPercentDone( const tr_completion_t * cp )
370{
371    const uint64_t tilDone = tr_cpLeftUntilDone( cp );
372    const uint64_t total = cp->tor->info.totalSize;
373    const float f = 1.0 - (double)tilDone / total;
374    return MAX(0.0, f);
375}
376
377uint64_t
378tr_cpDownloadedValid( const tr_completion_t * cp )
379{
380    const tr_torrent_t * tor = cp->tor;
381
382    uint64_t b = tr_bitfieldCountTrueBits( cp->blockBitfield ) * tor->blockSize;
383
384    if( tor->blockCount && tr_bitfieldHas( cp->blockBitfield, tor->blockCount - 1 ) )
385        b -= (tor->blockSize - (tor->info.totalSize % tor->blockSize));
386
387   return b;
388}
Note: See TracBrowser for help on using the repository browser.