Changeset 3054


Ignore:
Timestamp:
Sep 12, 2007, 11:09:02 PM (14 years ago)
Author:
charles
Message:

use much less memory in the peer manager

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/encryption/libtransmission/peer-mgr.c

    r3049 r3054  
    320320}
    321321
     322static void
     323populateBlockArray( Torrent * t )
     324{
     325    uint32_t i;
     326    struct tr_block * b;
     327    const tr_torrent * tor = t->tor;
     328
     329    t->blockCount = tor->blockCount;
     330    t->blocks = b = tr_new( struct tr_block, t->blockCount );
     331
     332    for( i=0; i<t->blockCount; ++i, ++b )
     333    {
     334        const int index = tr_torBlockPiece( tor, i );
     335        b->have = tr_cpBlockIsComplete( tor->completion, i ) ? 1 : 0;
     336        b->dnd = tor->info.pieces[index].dnd ? 1 : 0;
     337        b->low_priority = tor->info.pieces[index].priority == TR_PRI_LOW;
     338        b->high_priority = tor->info.pieces[index].priority == TR_PRI_HIGH;
     339        b->requestCount = 0;
     340        b->scarcity = 0;
     341        b->block = i;
     342    }
     343}
     344
    322345static int
    323346refillPulse( void * vtorrent )
     
    326349    int size;
    327350    Torrent * t = (Torrent *) vtorrent;
    328     tr_peer ** peers = getConnectedPeers( t, &size );
     351    tr_peer ** peers;
    329352    const int isSeeding = tr_cpGetStatus( t->tor->completion ) != TR_CP_INCOMPLETE;
    330 
    331 fprintf( stderr, "in refill pulse for [%s]... sorting blocks by interest...", t->tor->info.name );
    332 
    333     if( t->isRunning && !isSeeding && size>0 )
     353    const int wantToRefill = t->isRunning && !isSeeding;
     354
     355    if( !wantToRefill && t->blocks!=NULL ) /* torrent has stopped or switched to seeding */
     356    {
     357        tr_free( t->blocks );
     358        t->blocks = NULL;
     359        t->blockCount = 0;
     360    }
     361    else if( wantToRefill && t->blocks==NULL ) /* torrent has started or switched to leeching */
     362    {
     363        populateBlockArray( t );
     364    }
     365
     366    peers = getConnectedPeers( t, &size );
     367    if( size>0 )
    334368    {
    335369        /* sort the blocks by interest */
     370        fprintf( stderr, "sorting [%s] blocks by interest...", t->tor->info.name );
    336371        qsort( t->blocks, t->blockCount, sizeof(struct tr_block), compareBlockByInterest );
    337     fprintf( stderr, " .done.\n" );
     372        fprintf( stderr, "done\n" );
    338373
    339374        /* walk through all the most interesting blocks */
     
    349384                continue;
    350385
    351             if( !size ) { /* all peers full */
    352                 fprintf( stderr, "all peers full...\n" );
     386            if( !size ) /* all peers full */
    353387                break;
    354             }
    355388
    356389            /* find a peer who can ask for this block */
     
    358391            {
    359392                const int val = tr_peerMsgsAddRequest( peers[j]->msgs, index, begin, length );
    360     //fprintf( stderr, " block %"PRIu64", peer %"PRIu64, (uint64_t)i,  (uint64_t)j );
    361393                if( val == TR_ADDREQ_FULL ) {
    362     fprintf( stderr, "peer %d of %d is full\n", (int)j, size );
     394                    fprintf( stderr, "peer %d of %d is full\n", (int)j, size );
    363395                    peers[j] = peers[--size];
    364396                }
    365397                else if( val == TR_ADDREQ_MISSING ) {
    366     //fprintf( stderr, "peer doesn't have it\n" );
     398                    fprintf( stderr, "peer doesn't have it\n" );
    367399                    ++j;
    368400                }
    369401                else if( val == TR_ADDREQ_OK ) {
    370     fprintf( stderr, "peer %d took the request for block %d\n", j, i );
     402                    fprintf( stderr, "peer %d took the request for block %d\n", j, i );
    371403                    incrementReqCount( &t->blocks[i] );
    372404                    j = size;
     
    375407        }
    376408
    377         /* put the blocks back by index */
     409        /* put the blocks back the way we found them */
    378410        qsort( t->blocks, t->blockCount, sizeof(struct tr_block), compareBlockByIndex );
    379411    }
     
    408440            const uint32_t end = begin + t->tor->info.pieceCount;
    409441            uint32_t i;
    410             for( i=begin; i<end; ++i ) {
     442            for( i=begin; t->blocks!=NULL && i<end; ++i ) {
    411443                if( !tr_bitfieldHas( e->bitfield, i ) )
    412444                    continue;
     
    421453            const uint32_t end = begin + tr_torPieceCountBlocks( t->tor, (int)e->pieceIndex );
    422454            uint32_t i;
    423             for( i=begin; i<end; ++i ) {
     455            for( i=begin; t->blocks!=NULL && i<end; ++i ) {
    424456                assert( t->blocks[i].block == i );
    425457                incrementScarcity( &t->blocks[i] );
     
    430462        case TR_PEERMSG_GOT_BLOCK: {
    431463            uint32_t i = e->blockIndex;
    432             assert( t->blocks[i].block == i );
    433             t->blocks[i].have = 1;
     464            if( t->blocks != NULL ) {
     465                assert( t->blocks[i].block == i );
     466                t->blocks[i].have = 1;
     467            }
    434468            break;
    435469        }
     
    690724    Torrent * t = getExistingTorrent( manager, torrentHash );
    691725
    692     for( i=0; i<t->blockCount; ++i ) {
     726    for( i=0; t->blocks!=NULL && i<t->blockCount; ++i ) {
    693727        assert( t->blocks[i].block == i );
    694728        t->blocks[i].have = tr_cpBlockIsComplete( t->tor->completion, i ) ? 1 : 0;
     
    701735{
    702736    Torrent * t;
    703     uint32_t i;
    704737
    705738    assert( tor != NULL );
     
    712745    t->chokeTimer = tr_timerNew( manager->handle, chokePulse, t, RECHOKE_PERIOD_SECONDS );
    713746fprintf( stderr, "timer CHOKE %p is new\n", t->chokeTimer );
    714 
    715     t->blockCount = tor->blockCount;
    716     t->blocks = tr_new( struct tr_block, t->blockCount );
    717     for( i=0; i<t->blockCount; ++i ) {
    718         const int index = tr_torBlockPiece( tor, i );
    719         t->blocks[i].have = tr_cpBlockIsComplete( t->tor->completion, i ) ? 1 : 0;
    720 if( tr_cpBlockIsComplete( t->tor->completion, i ) ) fprintf( stderr, "have block %d\n", (int)i );
    721         t->blocks[i].dnd = tor->info.pieces[index].dnd;
    722         t->blocks[i].low_priority = tor->info.pieces[index].priority == TR_PRI_LOW;
    723         t->blocks[i].high_priority = tor->info.pieces[index].priority == TR_PRI_HIGH;
    724         t->blocks[i].requestCount = 0;
    725         t->blocks[i].scarcity = 0;
    726         t->blocks[i].block = i;
    727     }
    728747
    729748    memcpy( t->hash, tor->info.hash, SHA_DIGEST_LENGTH );
Note: See TracChangeset for help on using the changeset viewer.