Changeset 6556


Ignore:
Timestamp:
Aug 16, 2008, 9:06:57 PM (13 years ago)
Author:
charles
Message:

#220: lazy bitfields

Location:
trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/cli/cli.c

    r6496 r6556  
    272272            peerPort,
    273273            encryptionMode,
     274            TR_DEFAULT_LAZY_BITFIELD_ENABLED,
    274275            uploadLimit >= 0,
    275276            uploadLimit,
  • trunk/daemon/daemon.c

    r6490 r6556  
    3737#define KEY_DOWNLOAD_DIR     "download-dir"
    3838#define KEY_ENCRYPTION       "encryption"
     39#define KEY_LAZY_BITFIELD    "lazy-bitfield-enabled"
    3940#define KEY_PEER_LIMIT       "max-peers-global"
    4041#define KEY_PEER_PORT        "peer-port"
     
    152153    int upLimit=-1, upLimited=-1, downLimit=-1, downLimited=-1;
    153154    int encryption = -1;
     155    int useLazyBitfield = -1;
    154156    tr_ctor * ctor;
    155157    tr_torrent ** torrents;
     
    174176    getConfigInt( dict, KEY_USPEED,          &upLimit,           100 );
    175177    getConfigInt( dict, KEY_USPEED_ENABLED,  &upLimited,         FALSE );
     178    getConfigInt( dict, KEY_LAZY_BITFIELD,   &useLazyBitfield,   TR_DEFAULT_LAZY_BITFIELD_ENABLED );
    176179    getConfigInt( dict, KEY_PEER_LIMIT,      &peers,             TR_DEFAULT_GLOBAL_PEER_LIMIT );
    177180    getConfigInt( dict, KEY_BLOCKLIST,       &blocklistEnabled,  TR_DEFAULT_BLOCKLIST_ENABLED );
     
    191194                                    pexEnabled, fwdEnabled, peerPort,
    192195                                    encryption,
     196                                    useLazyBitfield,
    193197                                    upLimited, upLimit,
    194198                                    downLimited, downLimit,
  • trunk/gtk/main.c

    r6531 r6556  
    422422                            pref_int_get( PREF_KEY_PORT ),
    423423                            pref_int_get( PREF_KEY_ENCRYPTION ),
     424                            pref_flag_get( PREF_KEY_LAZY_BITFIELD ),
    424425                            pref_flag_get( PREF_KEY_UL_LIMIT_ENABLED ),
    425426                            pref_int_get( PREF_KEY_UL_LIMIT ),
  • trunk/gtk/tr-prefs.c

    r6517 r6556  
    100100    pref_flag_set_default   ( PREF_KEY_ASKQUIT, TRUE );
    101101    pref_flag_set_default   ( PREF_KEY_ENCRYPTION, TR_ENCRYPTION_PREFERRED );
     102    pref_flag_set_default   ( PREF_KEY_LAZY_BITFIELD, TR_DEFAULT_LAZY_BITFIELD_ENABLED );
    102103
    103104    pref_int_set_default    ( PREF_KEY_MSGLEVEL, TR_MSG_INF );
  • trunk/gtk/tr-prefs.h

    r6329 r6556  
    4141#define PREF_KEY_ENCRYPTION             "encryption"
    4242#define PREF_KEY_MSGLEVEL               "debug-message-level"
     43#define PREF_KEY_LAZY_BITFIELD          "lazy-bitfield-enabled"
    4344#define PREF_KEY_SORT_MODE              "sort-mode"
    4445#define PREF_KEY_SORT_REVERSED          "sort-reversed"
  • trunk/libtransmission/net.c

    r6552 r6556  
    4444#include "natpmp.h"
    4545#include "net.h"
     46#include "peer-io.h"
    4647#include "platform.h"
    4748#include "utils.h"
     
    123124        fd = makeSocketNonBlocking( fd );
    124125
     126#if 0
    125127    if( fd >= 0 ) {
    126128        const int buffsize = 1500*3; /* 3x MTU for most ethernet/wireless */
    127129        setsockopt( fd, SOL_SOCKET, SO_SNDBUF, &buffsize, sizeof( buffsize ) );
    128130    }
     131#endif
    129132
    130133    return fd;
     
    139142
    140143    if( ( s = createSocket( type, priority ) ) < 0 )
    141     {
    142144        return -1;
    143     }
    144145
    145146    memset( &sock, 0, sizeof( sock ) );
     
    162163    }
    163164
     165    tr_deepLog( __FILE__, __LINE__, NULL, "New OUTGOING connection %d (%s)",
     166                s, tr_peerIoAddrStr( addr, port ) );
     167
    164168    return s;
    165169}
  • trunk/libtransmission/peer-msgs.c

    r6548 r6556  
    2424#include "bencode.h"
    2525#include "completion.h"
     26#include "crypto.h"
    2627#include "inout.h"
    2728#include "peer-io.h"
     
    9091    IMMEDIATE_PRIORITY_INTERVAL_SECS = 0,
    9192    HIGH_PRIORITY_INTERVAL_SECS = 2,
    92     LOW_PRIORITY_INTERVAL_SECS = 20
     93    LOW_PRIORITY_INTERVAL_SECS = 20,
     94
     95    /* number of pieces to remove from the bitfield when
     96     * lazy bitfields are turned on */
     97    LAZY_PIECE_COUNT = 26
    9398};
    9499
     
    296301    tr_peer * info;
    297302
    298     tr_handle * handle;
     303    tr_session * session;
    299304    tr_torrent * torrent;
    300305    tr_peerIo * io;
     
    382387    struct evbuffer * out = msgs->outMessages;
    383388
    384     dbgmsg( msgs, "requesting %u:%u->%u", req->index, req->offset, req->length );
    385389    tr_peerIoWriteUint32( io, out, sizeof(uint8_t) + 3*sizeof(uint32_t) );
    386390    tr_peerIoWriteUint8 ( io, out, BT_REQUEST );
     
    388392    tr_peerIoWriteUint32( io, out, req->offset );
    389393    tr_peerIoWriteUint32( io, out, req->length );
     394    dbgmsg( msgs, "requesting %u:%u->%u... outMessage size is now %d",
     395            req->index, req->offset, req->length, (int)EVBUFFER_LENGTH(out) );
    390396    pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
    391     dbgmsg( msgs, "outMessage size is now %d", (int)EVBUFFER_LENGTH(out) );
    392397}
    393398
     
    398403    struct evbuffer * out = msgs->outMessages;
    399404
    400     dbgmsg( msgs, "cancelling %u:%u->%u", req->index, req->offset, req->length );
    401405    tr_peerIoWriteUint32( io, out, sizeof(uint8_t) + 3*sizeof(uint32_t) );
    402406    tr_peerIoWriteUint8 ( io, out, BT_CANCEL );
     
    404408    tr_peerIoWriteUint32( io, out, req->offset );
    405409    tr_peerIoWriteUint32( io, out, req->length );
     410    dbgmsg( msgs, "cancelling %u:%u->%u... outMessage size is now %d",
     411            req->index, req->offset, req->length, (int)EVBUFFER_LENGTH(out) );
    406412    pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
    407     dbgmsg( msgs, "outMessage size is now %d", (int)EVBUFFER_LENGTH(out) );
    408413}
    409414
     
    414419    struct evbuffer * out = msgs->outMessages;
    415420
    416     dbgmsg( msgs, "sending Have %u", index );
    417421    tr_peerIoWriteUint32( io, out, sizeof(uint8_t) + sizeof(uint32_t) );
    418422    tr_peerIoWriteUint8 ( io, out, BT_HAVE );
    419423    tr_peerIoWriteUint32( io, out, index );
     424    dbgmsg( msgs, "sending Have %u.. outMessage size is now %d",
     425            index, (int)EVBUFFER_LENGTH(out) );
    420426    pokeBatchPeriod( msgs, LOW_PRIORITY_INTERVAL_SECS );
    421     dbgmsg( msgs, "outMessage size is now %d", (int)EVBUFFER_LENGTH(out) );
    422427}
    423428
     
    428433    struct evbuffer * out = msgs->outMessages;
    429434
    430     dbgmsg( msgs, "sending %s", (choke ? "Choke" : "Unchoke") );
    431435    tr_peerIoWriteUint32( io, out, sizeof(uint8_t) );
    432436    tr_peerIoWriteUint8 ( io, out, choke ? BT_CHOKE : BT_UNCHOKE );
     437    dbgmsg( msgs, "sending %s... outMessage size is now %d",
     438                  (choke ? "Choke" : "Unchoke"),
     439                  (int)EVBUFFER_LENGTH(out) );
    433440    pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
    434     dbgmsg( msgs, "outMessage size is now %d", (int)EVBUFFER_LENGTH(out) );
    435441}
    436442
     
    952958
    953959    tr_bencInitDict( &val, 4 );
    954     tr_bencDictAddInt( &val, "e", msgs->handle->encryptionMode != TR_CLEAR_PREFERRED );
    955     tr_bencDictAddInt( &val, "p", tr_sessionGetPeerPort( msgs->handle ) );
     960    tr_bencDictAddInt( &val, "e", msgs->session->encryptionMode != TR_CLEAR_PREFERRED );
     961    tr_bencDictAddInt( &val, "p", tr_sessionGetPeerPort( msgs->session ) );
    956962    tr_bencDictAddStr( &val, "v", TR_NAME " " USERAGENT_PREFIX );
    957963    m  = tr_bencDictAddDict( &val, "m", 1 );
     
    10351041        pex = tr_peerMgrCompactToPex( added->val.s.s, added->val.s.i, added_f, added_f_len, &n );
    10361042        for( i=0; i<n; ++i )
    1037             tr_peerMgrAddPex( msgs->handle->peerMgr, tor->info.hash,
     1043            tr_peerMgrAddPex( msgs->session->peerMgr, tor->info.hash,
    10381044                              TR_PEER_FROM_PEX, pex+i );
    10391045        tr_free( pex );
     
    17511757sendBitfield( tr_peermsgs * msgs )
    17521758{
    1753     const tr_bitfield * bitfield = tr_cpPieceBitfield( msgs->torrent->completion );
    17541759    struct evbuffer * out = msgs->outMessages;
    1755 
    1756     dbgmsg( msgs, "sending peer a bitfield message" );
    1757     tr_peerIoWriteUint32( msgs->io, out, sizeof(uint8_t) + bitfield->byteCount );
     1760    const tr_piece_index_t pieceCount = msgs->torrent->info.pieceCount;
     1761    tr_bitfield * field;
     1762    tr_piece_index_t lazyPieces[LAZY_PIECE_COUNT];
     1763    int i;
     1764    int lazyCount = 0;
     1765
     1766    field = tr_bitfieldDup( tr_cpPieceBitfield( msgs->torrent->completion ) );
     1767
     1768    if( tr_sessionIsLazyBitfieldEnabled( msgs->session ) )
     1769    {
     1770        const int maxLazyCount = MIN( LAZY_PIECE_COUNT, pieceCount );
     1771
     1772        while( lazyCount < maxLazyCount )
     1773        {
     1774            const size_t pos = tr_cryptoRandInt ( pieceCount );
     1775            if( !tr_bitfieldHas( field, pos ) ) /* already removed it */
     1776                continue;
     1777            dbgmsg( msgs, "lazy bitfield #%d: piece %d of %d",
     1778                    (int)(lazyCount+1), (int)pos, (int)pieceCount );
     1779            tr_bitfieldRem( field, pos );
     1780            lazyPieces[lazyCount++] = pos;
     1781        }
     1782    }
     1783
     1784    tr_peerIoWriteUint32( msgs->io, out, sizeof(uint8_t) + field->byteCount );
    17581785    tr_peerIoWriteUint8 ( msgs->io, out, BT_BITFIELD );
    1759     tr_peerIoWriteBytes ( msgs->io, out, bitfield->bits, bitfield->byteCount );
     1786    tr_peerIoWriteBytes ( msgs->io, out, field->bits, field->byteCount );
     1787    dbgmsg( msgs, "sending bitfield... outMessage size is now %d",
     1788                  (int)EVBUFFER_LENGTH(out) );
    17601789    pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
    1761     dbgmsg( msgs, "outMessage size is now %d", (int)EVBUFFER_LENGTH(out) );
     1790
     1791    for( i=0; i<lazyCount; ++i )
     1792        protocolSendHave( msgs, lazyPieces[i] );
     1793
     1794    tr_bitfieldFree( field );
    17621795}
    17631796
     
    18211854        int i;
    18221855        tr_pex * newPex = NULL;
    1823         const int newCount = tr_peerMgrGetPeers( msgs->handle->peerMgr, msgs->torrent->info.hash, &newPex );
     1856        const int newCount = tr_peerMgrGetPeers( msgs->session->peerMgr, msgs->torrent->info.hash, &newPex );
    18241857        PexDiffs diffs;
    18251858        tr_benc val, *added, *dropped;
     
    19241957    m->publisher = tr_publisherNew( );
    19251958    m->info = info;
    1926     m->handle = torrent->handle;
     1959    m->session = torrent->handle;
    19271960    m->torrent = torrent;
    19281961    m->io = info->io;
     
    19331966    m->info->have = tr_bitfieldNew( torrent->info.pieceCount );
    19341967    m->state = AWAITING_BT_LENGTH;
    1935     m->pulseTimer = tr_timerNew( m->handle, peerPulse, m, PEER_PULSE_INTERVAL );
    1936     m->rateTimer = tr_timerNew( m->handle, ratePulse, m, RATE_PULSE_INTERVAL );
    1937     m->pexTimer = tr_timerNew( m->handle, pexPulse, m, PEX_INTERVAL );
     1968    m->pulseTimer = tr_timerNew( m->session, peerPulse, m, PEER_PULSE_INTERVAL );
     1969    m->rateTimer = tr_timerNew( m->session, ratePulse, m, RATE_PULSE_INTERVAL );
     1970    m->pexTimer = tr_timerNew( m->session, pexPulse, m, PEX_INTERVAL );
    19381971    m->outMessages = evbuffer_new( );
    19391972    m->outMessagesBatchedAt = 0;
  • trunk/libtransmission/port-forwarding.c

    r6389 r6556  
    2020#include "natpmp.h"
    2121#include "net.h"
     22#include "peer-io.h"
    2223#include "peer-mgr.h"
    2324#include "port-forwarding.h"
     
    131132            break;
    132133
     134        tr_deepLog( __FILE__, __LINE__, NULL, "New INCOMING connection %d (%s)",
     135                    socket, tr_peerIoAddrStr( &addr, port ) );
     136
    133137        tr_peerMgrAddIncoming( s->h->peerMgr, &addr, port, socket );
    134138    }
  • trunk/libtransmission/session.c

    r6517 r6556  
    180180                    int                 publicPort,
    181181                    tr_encryption_mode  encryptionMode,
    182                     int                 isUploadLimitEnabled,
     182                    int                 useLazyBitfield,
     183                    int                 useUploadLimit,
    183184                    int                 uploadLimit,
    184                     int                 isDownloadLimitEnabled,
     185                    int                 useDownloadLimit,
    185186                    int                 downloadLimit,
    186187                    int                 globalPeerLimit,
     
    225226    h->proxyPort = proxyPort;
    226227    h->proxyType = proxyType;
    227     h->isProxyAuthEnabled = proxyAuthIsEnabled ? 1 : 0;
     228    h->isProxyAuthEnabled = proxyAuthIsEnabled != 0;
    228229    h->proxyUsername = tr_strdup( proxyUsername );
    229230    h->proxyPassword = tr_strdup( proxyPassword );
     
    242243    h->peerMgr = tr_peerMgrNew( h );
    243244
     245    h->useLazyBitfield = useLazyBitfield != 0;
     246
    244247    /* Initialize rate and file descripts controls */
    245248
    246249    h->upload = tr_rcInit();
    247250    tr_rcSetLimit( h->upload, uploadLimit );
    248     h->useUploadLimit = isUploadLimitEnabled;
     251    h->useUploadLimit = useUploadLimit;
    249252
    250253    h->download = tr_rcInit();
    251254    tr_rcSetLimit( h->download, downloadLimit );
    252     h->useDownloadLimit = isDownloadLimitEnabled;
     255    h->useDownloadLimit = useDownloadLimit;
    253256
    254257    tr_fdInit( globalPeerLimit );
     
    289292                               -1, /* public port */
    290293                               TR_ENCRYPTION_PREFERRED, /* encryption mode */
     294                               TR_DEFAULT_LAZY_BITFIELD_ENABLED,
    291295                               FALSE, /* use upload speed limit? */
    292296                               -1, /* upload speed limit */
     
    644648
    645649void
    646 tr_sessionSetPexEnabled( tr_handle * handle, int isPexEnabled )
    647 {
    648     handle->isPexEnabled = isPexEnabled ? 1 : 0;
     650tr_sessionSetPexEnabled( tr_handle * handle, int enabled )
     651{
     652    handle->isPexEnabled = enabled ? 1 : 0;
    649653}
    650654
     
    653657{
    654658    return handle->isPexEnabled;
     659}
     660
     661/***
     662****
     663***/
     664
     665void
     666tr_sessionSetLazyBitfieldEnabled( tr_handle * handle, int enabled )
     667{
     668    handle->useLazyBitfield = enabled ? 1 : 0;
     669}
     670
     671int
     672tr_sessionIsLazyBitfieldEnabled( const tr_handle * handle )
     673{
     674    return handle->useLazyBitfield;
    655675}
    656676
  • trunk/libtransmission/session.h

    r6329 r6556  
    5858    unsigned int                 useUploadLimit     : 1;
    5959    unsigned int                 useDownloadLimit   : 1;
     60    unsigned int                 useLazyBitfield    : 1;
    6061
    6162    tr_encryption_mode           encryptionMode;
  • trunk/libtransmission/transmission.h

    r6510 r6556  
    114114#define TR_DEFAULT_PORT_STR                 "51413"
    115115/** @see tr_sessionInitFull */
     116#define TR_DEFAULT_LAZY_BITFIELD_ENABLED    0
     117/** @see tr_sessionInitFull */
    116118#define TR_DEFAULT_GLOBAL_PEER_LIMIT        200
    117119/** @see tr_sessionInitFull */
     
    254256                                int                  publicPort,
    255257                                tr_encryption_mode   encryptionMode,
    256                                 int                  isUploadLimitEnabled,
     258                                int                  useLazyBitfield,
     259                                int                  useUploadLimit,
    257260                                int                  uploadLimit,
    258                                 int                  isDownloadLimitEnabled,
     261                                int                  useDownloadLimit,
    259262                                int                  downloadLimit,
    260263                                int                  peerLimit,
     
    496499
    497500int tr_sessionIsPexEnabled( const tr_session * );
     501
     502void tr_sessionSetLazyBitfieldEnabled( tr_handle * handle, int enabled );
     503
     504int tr_sessionIsLazyBitfieldEnabled( const tr_handle * handle );
    498505
    499506tr_encryption_mode tr_sessionGetEncryption( tr_session * );
  • trunk/macosx/Controller.m

    r6529 r6556  
    210210                                [fDefaults integerForKey: @"BindPort"],
    211211                                TR_ENCRYPTION_PREFERRED, /* reset in prefs */
     212                                TR_DEFAULT_LAZY_BITFIELD_ENABLED,
    212213                                NO, /* reset in prefs */
    213214                                -1, /* reset in prefs */
Note: See TracChangeset for help on using the changeset viewer.