Changeset 3871


Ignore:
Timestamp:
Nov 18, 2007, 3:18:26 AM (15 years ago)
Author:
charles
Message:

this should improve download speeds. test it!

Location:
trunk/libtransmission
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/peer-io.c

    r3859 r3871  
    3131
    3232/* arbitrary */
    33 #define TR_RDBUF (1024*3)
     33#define TR_RDBUF (1024*8)
    3434
    3535/**
     
    408408                   struct evbuffer * buf )
    409409{
    410     tr_peerIoWrite( io, EVBUFFER_DATA(buf), EVBUFFER_LENGTH(buf) );
    411     evbuffer_drain( buf, ~0 );
     410    const size_t n = EVBUFFER_LENGTH( buf );
     411    tr_peerIoWrite( io, EVBUFFER_DATA(buf), n );
     412    evbuffer_drain( buf, n );
    412413}
    413414
  • trunk/libtransmission/peer-mgr.c

    r3864 r3871  
    15941594        tr_peer ** deadbeats = tr_new( tr_peer*, peerCount );
    15951595
     1596        const double ul_KiBsec = tr_rcRate( t->tor->upload );
     1597        const double ul_KiB = ul_KiBsec * (SWIFT_PERIOD_MSEC/1000.0);
     1598        const double ul_bytes = ul_KiB * 1024;
     1599        const double freeCreditTotal = ul_bytes * SWIFT_LARGESSE;
     1600        int freeCreditPerPeer;
     1601
    15961602        for( i=0; i<peerCount; ++i ) {
    15971603            tr_peer * peer = peers[i];
    1598             if( peer->credit < 0 )
     1604            if( peer->credit <= 0 )
    15991605                deadbeats[deadbeatCount++] =  peer;
    16001606        }
    16011607
    1602         if( deadbeatCount )
    1603         {
    1604             const double ul_KiBsec = tr_rcRate( t->tor->upload );
    1605             const double ul_KiB = ul_KiBsec * (SWIFT_PERIOD_MSEC/1000.0);
    1606             const double ul_bytes = ul_KiB * 1024;
    1607             const double freeCreditTotal = ul_bytes * SWIFT_LARGESSE;
    1608             const int freeCreditPerPeer = (int)( freeCreditTotal / deadbeatCount );
    1609             for( i=0; i<deadbeatCount; ++i )
    1610                 deadbeats[i]->credit = freeCreditPerPeer;
    1611             tordbg( t, "%d deadbeats, "
    1612                        "who are each being granted %d bytes' credit "
    1613                        "for a total of %.1f KiB, "
    1614                        "%d%% of the torrent's ul speed %.1f\n",
    1615                 deadbeatCount, freeCreditPerPeer,
    1616                 ul_KiBsec*SWIFT_LARGESSE, (int)(SWIFT_LARGESSE*100), ul_KiBsec );
    1617         }
     1608        freeCreditPerPeer = (int)( freeCreditTotal / deadbeatCount );
     1609        for( i=0; i<deadbeatCount; ++i )
     1610            deadbeats[i]->credit = freeCreditPerPeer;
     1611
     1612        tordbg( t, "%d deadbeats, "
     1613            "who are each being granted %d bytes' credit "
     1614            "for a total of %.1f KiB, "
     1615            "%d%% of the torrent's ul speed %.1f\n",
     1616            deadbeatCount, freeCreditPerPeer,
     1617            ul_KiBsec*SWIFT_LARGESSE, (int)(SWIFT_LARGESSE*100), ul_KiBsec );
    16181618
    16191619        tr_free( deadbeats );
     
    17321732         * now to weed it out in case someone sends it to us via pex */
    17331733        if( atom->myflags & MYFLAG_BANNED ) {
     1734#if 0
    17341735            tordbg( t, "RECONNECT peer %d (%s) is banned...",
    17351736                    i, tr_peerIoAddrStr(&atom->addr,atom->port) );
     1737#endif
    17361738            continue;
    17371739        }
     
    17391741        /* we don't need two connections to the same peer... */
    17401742        if( peerIsInUse( t, &atom->addr ) ) {
     1743#if 0
    17411744            tordbg( t, "RECONNECT peer %d (%s) is in use..",
    17421745                    i, tr_peerIoAddrStr(&atom->addr,atom->port) );
     1746#endif
    17431747            continue;
    17441748        }
     
    17461750        /* no need to connect if we're both seeds... */
    17471751        if( seed && (atom->flags & ADDED_F_SEED_FLAG) ) {
     1752#if 0
    17481753            tordbg( t, "RECONNECT peer %d (%s) is a seed and so are we..",
    17491754                    i, tr_peerIoAddrStr(&atom->addr,atom->port) );
     1755#endif
    17501756            continue;
    17511757        }
     
    17531759        /* we're wasting our time trying to connect to this bozo. */
    17541760        if( atom->numFails > 10 ) {
     1761#if 0
    17551762            tordbg( t, "RECONNECT peer %d (%s) gives us nothing but failure.",
    17561763                    i, tr_peerIoAddrStr(&atom->addr,atom->port) );
     1764#endif
    17571765            continue;
    17581766        }
  • trunk/libtransmission/peer-msgs.c

    r3864 r3871  
    145145    unsigned int clientSentLtepHandshake  : 1;
    146146    unsigned int peerSentLtepHandshake    : 1;
     147    unsigned int sendingBlock             : 1;
    147148   
    148149    tr_bitfield * clientAllowedPieces;
     
    566567    {
    567568        struct peer_request * r = tr_list_pop_front( &msgs->clientWillAskFor );
     569        assert( requestIsValid( msgs, r ) );
     570        assert( tr_bitfieldHas( msgs->info->have, r->index ) );
    568571        protocolSendRequest( msgs, r );
    569572        r->time_requested = msgs->lastReqAddedAt = time( NULL );
     
    640643    *req = tmp;
    641644    tr_list_append( &msgs->clientWillAskFor, req );
    642     pulse( msgs );
    643645    return TR_ADDREQ_OK;
    644646}
     
    14431445{
    14441446    /* don't let our outbuffer get too large */
    1445     if( tr_peerIoWriteBytesWaiting( msgs->io ) > 4096 )
    1446         return FALSE;
    1447 
    1448     /* SWIFT */
    1449     if( SWIFT_ENABLED && !tr_torrentIsSeed( msgs->torrent )
    1450                       && ( msgs->info->credit < 0 ) )
    1451         return FALSE;
    1452 
    1453     return TRUE;
     1447    return tr_peerIoWriteBytesWaiting( msgs->io ) < 4096;
    14541448}
    14551449
     
    14591453    static const size_t maxval = ~0;
    14601454    const tr_torrent * tor = msgs->torrent;
     1455    const int useSwift = SWIFT_ENABLED && !tr_torrentIsSeed( msgs->torrent );
     1456    const size_t swiftBytes = msgs->info->credit;
     1457    size_t speedBytes;
    14611458
    14621459    if( !canWrite( msgs ) )
     
    14641461
    14651462    if( tor->uploadLimitMode == TR_SPEEDLIMIT_GLOBAL )
    1466         return tor->handle->useUploadLimit
    1467             ? tr_rcBytesLeft( tor->handle->upload ) : maxval;
    1468 
    1469     if( tor->uploadLimitMode == TR_SPEEDLIMIT_SINGLE )
    1470         return tr_rcBytesLeft( tor->upload );
    1471 
    1472     return maxval;
     1463        speedBytes = tor->handle->useUploadLimit ? tr_rcBytesLeft( tor->handle->upload ) : maxval;
     1464    else if( tor->uploadLimitMode == TR_SPEEDLIMIT_SINGLE )
     1465        speedBytes = tr_rcBytesLeft( tor->upload );
     1466    else
     1467        speedBytes = ~0;
     1468
     1469    return useSwift ? MIN( speedBytes, swiftBytes )
     1470                    : speedBytes;
    14731471}
    14741472
     
    15341532    updatePeerStatus( msgs );
    15351533
    1536     if( !canWrite( msgs ) )
    1537     {
    1538     }
    1539     else if(( len = EVBUFFER_LENGTH( msgs->outBlock ) ))
     1534    if( msgs->sendingBlock )
    15401535    {
    15411536        const size_t uploadMax = getUploadMax( msgs );
     1537        size_t len = EVBUFFER_LENGTH( msgs->outBlock );
    15421538        const size_t outlen = MIN( len, uploadMax );
    1543         tr_peerIoWrite( msgs->io, EVBUFFER_DATA( msgs->outBlock ), outlen );
    1544         evbuffer_drain( msgs->outBlock, outlen );
    1545         msgs->clientSentAnythingAt = now;
    1546         peerGotBytes( msgs, outlen );
    1547         len -= outlen;
    1548         dbgmsg( msgs, "wrote %d bytes; %d left in block", (int)outlen, (int)len );
    1549         fflush( stdout );
    1550     }
    1551     else if(( len = EVBUFFER_LENGTH( msgs->outMessages ) ))
    1552     {
    1553         tr_peerIoWriteBuf( msgs->io, msgs->outMessages );
    1554         msgs->clientSentAnythingAt = now;
    1555     }
    1556     else if( ( now - msgs->clientSentAnythingAt ) > KEEPALIVE_INTERVAL_SECS )
    1557     {
    1558         sendKeepalive( msgs );
    1559     }
    1560 
    1561     if( !EVBUFFER_LENGTH( msgs->outBlock )
    1562         && (( r = popNextRequest( msgs )))
    1563         && requestIsValid( msgs, r )
    1564         && tr_cpPieceIsComplete( msgs->torrent->completion, r->index ) )
    1565     {
    1566         uint8_t * buf = tr_new( uint8_t, r->length );
    1567 
    1568         if( !tr_ioRead( msgs->torrent, r->index, r->offset, r->length, buf ) )
     1539
     1540        assert( len );
     1541
     1542        if( outlen && canWrite( msgs ) )
    15691543        {
    1570             tr_peerIo * io = msgs->io;
    1571             struct evbuffer * out = msgs->outBlock;
    1572 
    1573             dbgmsg( msgs, "sending block %u:%u->%u", r->index, r->offset, r->length );
    1574             tr_peerIoWriteUint32( io, out, sizeof(uint8_t) + 2*sizeof(uint32_t) + r->length );
    1575             tr_peerIoWriteUint8 ( io, out, BT_PIECE );
    1576             tr_peerIoWriteUint32( io, out, r->index );
    1577             tr_peerIoWriteUint32( io, out, r->offset );
    1578             tr_peerIoWriteBytes ( io, out, buf, r->length );
     1544            tr_peerIoWrite( msgs->io, EVBUFFER_DATA( msgs->outBlock ), outlen );
     1545            evbuffer_drain( msgs->outBlock, outlen );
     1546            peerGotBytes( msgs, outlen );
     1547
     1548            len -= outlen;
     1549            msgs->clientSentAnythingAt = now;
     1550            msgs->sendingBlock = len!=0;
     1551
     1552            dbgmsg( msgs, "wrote %d bytes; %d left in block", (int)outlen, (int)len );
    15791553        }
    1580 
    1581         tr_free( buf );
    1582         tr_free( r );
    1583 
    1584         pulse( msgs ); /* start sending it right away */
     1554    }
     1555
     1556    if( !msgs->sendingBlock )
     1557    {
     1558        if(( len = EVBUFFER_LENGTH( msgs->outMessages ) ))
     1559        {
     1560            tr_peerIoWriteBuf( msgs->io, msgs->outMessages );
     1561            msgs->clientSentAnythingAt = now;
     1562        }
     1563        else if( !EVBUFFER_LENGTH( msgs->outBlock )
     1564            && (( r = popNextRequest( msgs )))
     1565            && requestIsValid( msgs, r )
     1566            && tr_cpPieceIsComplete( msgs->torrent->completion, r->index ) )
     1567        {
     1568            uint8_t * buf = tr_new( uint8_t, r->length );
     1569
     1570            if( !tr_ioRead( msgs->torrent, r->index, r->offset, r->length, buf ) )
     1571            {
     1572                tr_peerIo * io = msgs->io;
     1573                struct evbuffer * out = msgs->outBlock;
     1574
     1575                dbgmsg( msgs, "sending block %u:%u->%u", r->index, r->offset, r->length );
     1576                tr_peerIoWriteUint32( io, out, sizeof(uint8_t) + 2*sizeof(uint32_t) + r->length );
     1577                tr_peerIoWriteUint8 ( io, out, BT_PIECE );
     1578                tr_peerIoWriteUint32( io, out, r->index );
     1579                tr_peerIoWriteUint32( io, out, r->offset );
     1580                tr_peerIoWriteBytes ( io, out, buf, r->length );
     1581                msgs->sendingBlock = 1;
     1582            }
     1583
     1584            tr_free( buf );
     1585            tr_free( r );
     1586        }
     1587        else if( ( now - msgs->clientSentAnythingAt ) > KEEPALIVE_INTERVAL_SECS )
     1588        {
     1589            sendKeepalive( msgs );
     1590        }
    15851591    }
    15861592
Note: See TracChangeset for help on using the changeset viewer.