close <div id="moderation_required">Attachments you submit will be routed for moderation. If you have an account, please <a href="/login">log in</a> first.</div>

Ticket #1521: block-cache-2.diff

File block-cache-2.diff, 7.3 KB (added by charles, 6 years ago)

revision of previous patch, which fixes the previous diff's "FIXME" for prefetching. The new version checks the block cache before prefetching from disk.

  • peer-msgs.c

     
    2222
    2323#include "transmission.h"
    2424#include "bencode.h"
     25#include "cache.h"
    2526#include "completion.h"
    2627#include "crypto.h"
    27 #include "inout.h"
    2828#ifdef WIN32
    2929#include "net.h" /* for ECONN */
    3030#endif
     
    12111211    for( i=msgs->prefetchCount; i<msgs->peer->pendingReqsToClient && i<12; ++i )
    12121212    {
    12131213        const struct peer_request * req = msgs->peerAskedFor + i;
    1214         tr_ioPrefetch( msgs->torrent, req->index, req->offset, req->length );
     1214        tr_cachePrefetchBlock( getSession(msgs)->cache, msgs->torrent, req->index, req->offset, req->length );
    12151215        ++msgs->prefetchCount;
    12161216    }
    12171217}
     
    16011601    ***  Save the block
    16021602    **/
    16031603
    1604     if(( err = tr_ioWrite( tor, req->index, req->offset, req->length, data )))
     1604    if(( err = tr_cacheWriteBlock( getSession(msgs)->cache, tor, req->index, req->offset, req->length, data )))
    16051605        return err;
    16061606
    16071607    addPeerToBlamefield( msgs, req->index );
     
    19131913            tr_peerIoWriteUint32( io, out, req.index );
    19141914            tr_peerIoWriteUint32( io, out, req.offset );
    19151915
    1916             err = tr_ioRead( msgs->torrent, req.index, req.offset, req.length, EVBUFFER_DATA(out)+EVBUFFER_LENGTH(out) );
     1916            err = tr_cacheReadBlock( getSession(msgs)->cache, msgs->torrent, req.index, req.offset, req.length, EVBUFFER_DATA(out)+EVBUFFER_LENGTH(out) );
    19171917            if( err )
    19181918            {
    19191919                if( fext )
  • peer-mgr.c

     
    2121#include "bandwidth.h"
    2222#include "bencode.h"
    2323#include "blocklist.h"
     24#include "cache.h"
    2425#include "clients.h"
    2526#include "completion.h"
    2627#include "crypto.h"
     
    14291430
    14301431                    tr_torrentSetHasPiece( tor, p, ok );
    14311432                    tr_torrentSetPieceChecked( tor, p, TRUE );
     1433                    tr_cacheFlushPiece( tor->session->cache, tor, p );
    14321434                    tr_peerMgrSetBlame( tor, p, ok );
    14331435
    14341436                    if( !ok )
  • ptrarray.c

     
    102102    return ret;
    103103}
    104104
    105 static void
     105void
    106106tr_ptrArrayErase( tr_ptrArray * t,
    107107                  int           begin,
    108108                  int           end )
     
    123123***
    124124**/
    125125
    126 static int
     126int
    127127tr_ptrArrayLowerBound( const tr_ptrArray *                t,
    128128                       const void *                       ptr,
    129129                       int                 compare( const void *,
  • ptrarray.h

     
    6565                              : NULL;
    6666}
    6767
     68void tr_ptrArrayErase( tr_ptrArray * t, int begin, int end );
    6869
     70
     71
    6972/** @brief Peek at the array pointer and its size, for easy iteration */
    7073void** tr_ptrArrayPeek( tr_ptrArray * array, int * size );
    7174
     
    100103    return tr_ptrArraySize(a) == 0;
    101104}
    102105
     106int tr_ptrArrayLowerBound( const tr_ptrArray * array,
     107                           const void * key,
     108                           int compare( const void * arrayItem, const void * key ),
     109                           tr_bool * exact_match );
     110
    103111/** @brief Insert a pointer into the array at the position determined by the sort function
    104112    @return the index of the stored pointer */
    105113int tr_ptrArrayInsertSorted( tr_ptrArray * array,
  • inout.c

     
    2626#include <openssl/sha.h>
    2727
    2828#include "transmission.h"
     29#include "cache.h"
    2930#include "crypto.h"
    3031#include "fdlimit.h"
    3132#include "inout.h"
     33#include "peer-common.h" /* MAX_BLOCK_SIZE */
    3234#include "platform.h"
    3335#include "stats.h"
    3436#include "torrent.h"
     
    295297    size_t   bytesLeft;
    296298    uint32_t offset = 0;
    297299    tr_bool  success = TRUE;
    298     uint8_t * buffer = tr_sessionGetBuffer( tor->session );
    299     const size_t buflen = SESSION_BUFFER_SIZE;
     300    const size_t buflen = MAX_BLOCK_SIZE;
     301    uint8_t * buffer = tr_new( uint8_t, buflen );
    300302    SHA_CTX  sha;
    301303
    302304    assert( tor != NULL );
     
    313315    while( bytesLeft )
    314316    {
    315317        const int len = MIN( bytesLeft, buflen );
    316         success = !tr_ioRead( tor, pieceIndex, offset, len, buffer );
     318        success = !tr_cacheReadBlock( tor->session->cache, tor, pieceIndex, offset, len, buffer );
    317319        if( !success )
    318320            break;
    319321        SHA1_Update( &sha, buffer, len );
     
    324326    if( success )
    325327        SHA1_Final( setme, &sha );
    326328
    327     tr_sessionReleaseBuffer( tor->session );
     329    tr_free( buffer );
    328330    return success;
    329331}
    330332
  • session.c

     
    2828#include "bandwidth.h"
    2929#include "bencode.h"
    3030#include "blocklist.h"
     31#include "cache.h"
    3132#include "crypto.h"
    3233#include "fdlimit.h"
    3334#include "list.h"
     
    506507    session = tr_new0( tr_session, 1 );
    507508    session->bandwidth = tr_bandwidthNew( session, NULL );
    508509    session->lock = tr_lockNew( );
     510    session->cache = tr_cacheNew( 2*1024*1024 ); // 2 MiB
    509511    session->tag = tr_strdup( tag );
    510512    session->magicNumber = SESSION_MAGIC_NUMBER;
    511513    session->buffer = tr_valloc( SESSION_BUFFER_SIZE );
     
    16051607        tr_torrentFree( torrents[i] );
    16061608    tr_free( torrents );
    16071609
     1610    tr_cacheFree( session->cache );
     1611    session->cache = NULL;
    16081612    tr_announcerClose( session );
    16091613    tr_statsClose( session );
    16101614    tr_peerMgrFree( session->peerMgr );
  • Makefile.am

     
    2121    bencode.c \
    2222    bitfield.c \
    2323    blocklist.c \
     24    cache.c \
    2425    clients.c \
    2526    completion.c \
    2627    ConvertUTF.c \
     
    7273    bitfield.h \
    7374    bitset.h \
    7475    blocklist.h \
     76    cache.h \
    7577    clients.h \
    7678    ConvertUTF.h \
    7779    crypto.h \
  • session.h

     
    4040struct tr_announcer;
    4141struct tr_bandwidth;
    4242struct tr_bindsockets;
     43struct tr_cache;
    4344struct tr_fdInfo;
    4445
    4546struct tr_turtle_info
     
    149150    struct tr_peerMgr *          peerMgr;
    150151    struct tr_shared *           shared;
    151152
     153    struct tr_cache *            cache;
     154
    152155    struct tr_lock *             lock;
    153156
    154157    struct tr_web *              web;
  • torrent.c

     
    2929#include "announcer.h"
    3030#include "bandwidth.h"
    3131#include "bencode.h"
     32#include "cache.h"
    3233#include "completion.h"
    3334#include "crypto.h" /* for tr_sha1 */
    3435#include "resume.h"
     
    15191520    tr_verifyRemove( tor );
    15201521    tr_peerMgrStopTorrent( tor );
    15211522    tr_announcerTorrentStopped( tor );
     1523    tr_cacheFlushTorrent( tor->session->cache, tor );
    15221524
    15231525    tr_fdTorrentClose( tor->session, tor->uniqueId );
    15241526