Changeset 9495


Ignore:
Timestamp:
Nov 8, 2009, 11:43:38 PM (11 years ago)
Author:
charles
Message:

(trunk libT) #2551 "better buffer cache management": apply jch's prefetch-base.patch

Location:
trunk/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/fdlimit.c

    r9417 r9495  
    209209}
    210210
     211/* Like pread and pwrite, except that the position is undefined afterwards.
     212   And of course they are not thread-safe. */
     213
     214#ifdef SYS_DARWIN
     215 #define HAVE_PREAD
     216 #define HAVE_PWRITE
     217#endif
     218
     219ssize_t
     220tr_pread( int fd, void *buf, size_t count, off_t offset )
     221{
     222#ifdef HAVE_PREAD
     223    return pread( fd, buf, count, offset );
     224#else
     225    const off_t lrc = lseek( fd, offset, SEEK_SET );
     226    if( lrc < 0 )
     227        return -1;
     228    return read( fd, buf, count );
     229#endif
     230}
     231
     232ssize_t
     233tr_pwrite( int fd, void *buf, size_t count, off_t offset )
     234{
     235#ifdef HAVE_PWRITE
     236    return pwrite( fd, buf, count, offset );
     237#else
     238    const off_t lrc = lseek( fd, offset, SEEK_SET );
     239    if( lrc < 0 )
     240        return -1;
     241    return write( fd, buf, count );
     242#endif
     243}
     244
     245int
     246tr_prefetch( int fd, off_t offset, size_t count )
     247{
     248#ifdef HAVE_POSIX_FADVISE
     249    return posix_fadvise( fd, offset, count, POSIX_FADV_WILLNEED );
     250#elif defined(SYS_DARWIN)
     251    int val;
     252    struct radvisory radv;
     253    radv.ra_offset = offset;
     254    radv.ra_count = count;
     255    return fcntl( fd, F_RDADVISE, &radv );
     256#else
     257    return 0;
     258#endif
     259}
     260
    211261int
    212262tr_open_file_for_writing( const char * filename )
  • trunk/libtransmission/fdlimit.h

    r9387 r9495  
    5454
    5555int64_t tr_lseek( int fd, int64_t offset, int whence );
     56ssize_t tr_pread(int fd, void *buf, size_t count, off_t offset);
     57ssize_t tr_pwrite(int fd, void *buf, size_t count, off_t offset);
     58int tr_prefetch(int fd, off_t offset, size_t count);
    5659
    5760
  • trunk/libtransmission/inout.c

    r9387 r9495  
    5151#endif
    5252
    53 enum { TR_IO_READ, TR_IO_WRITE };
     53enum { TR_IO_READ, TR_IO_PREFETCH,
     54       /* Any operations that require write access must follow TR_IO_WRITE. */
     55       TR_IO_WRITE
     56};
    5457
    5558int64_t
     
    7881    const tr_file * file = &info->files[fileIndex];
    7982
    80     typedef size_t ( *iofunc )( int, void *, size_t );
    81     iofunc          func = ioMode == TR_IO_READ ? (iofunc)read : (iofunc)write;
    8283    int             fd = -1;
    8384    int             err = 0;
    84     const tr_bool doWrite = ioMode == TR_IO_WRITE;
     85    const tr_bool doWrite = ioMode >= TR_IO_WRITE;
    8586
    8687    assert( fileIndex < info->fileCount );
     
    114115        }
    115116
    116         if( ( file->dnd ) || ( ioMode != TR_IO_WRITE ) )
     117        if( ( file->dnd ) || ( ioMode < TR_IO_WRITE ) )
    117118            preallocationMode = TR_PREALLOCATE_NONE;
    118119        else
    119120            preallocationMode = tor->session->preallocationMode;
    120121
    121         if( ( ioMode == TR_IO_READ ) && !fileExists ) /* does file exist? */
     122        if( ( ioMode < TR_IO_WRITE ) && !fileExists ) /* does file exist? */
    122123        {
    123124            err = ENOENT;
     
    145146    if( !err )
    146147    {
    147         if( tr_lseek( fd, (int64_t)fileOffset, SEEK_SET ) == -1 )
    148         {
    149             err = errno;
    150             tr_torerr( tor, "tr_lseek failed for \"%s\": %s", file->name, tr_strerror( err ) );
    151         }
    152         else if( func( fd, buf, buflen ) != buflen )
    153         {
    154             err = errno;
    155             tr_torerr( tor, "read/write failed for \"%s\": %s", file->name, tr_strerror( err ) );
    156         }
     148        if( ioMode == TR_IO_READ ) {
     149            int rc = tr_pread(fd, buf, buflen, fileOffset);
     150            if(rc < 0) {
     151                err = errno;
     152                tr_torerr( tor, "read failed for \"%s\": %s",
     153                           file->name, tr_strerror( err ) );
     154            }
     155        } else if( ioMode == TR_IO_PREFETCH ) {
     156            int rc = tr_prefetch(fd, fileOffset, buflen);
     157            if(rc < 0) {
     158                err = errno;
     159                tr_torerr( tor, "prefetch failed for \"%s\": %s",
     160                           file->name, tr_strerror( err ) );
     161            }
     162        } else if( ioMode == TR_IO_WRITE ) {
     163            int rc = tr_pwrite(fd, buf, buflen, fileOffset);
     164            if(rc < 0) {
     165                err = errno;
     166                tr_torerr( tor, "write failed for \"%s\": %s",
     167                           file->name, tr_strerror( err ) );
     168            }
     169        } else
     170            abort();
    157171    }
    158172
     
    248262
    249263int
     264tr_ioPrefetch( tr_torrent       * tor,
     265               tr_piece_index_t   pieceIndex,
     266               uint32_t           begin,
     267               uint32_t           len)
     268{
     269    return readOrWritePiece( tor, TR_IO_PREFETCH, pieceIndex, begin,
     270                             NULL, len );
     271}
     272
     273int
    250274tr_ioWrite( tr_torrent       * tor,
    251275            tr_piece_index_t   pieceIndex,
  • trunk/libtransmission/inout.h

    r8912 r9495  
    3535               uint8_t             * setme );
    3636
     37int
     38tr_ioPrefetch( tr_torrent       * tor,
     39               tr_piece_index_t   pieceIndex,
     40               uint32_t           begin,
     41               uint32_t           len );
     42
    3743/**
    3844 * Writes the block specified by the piece index, offset, and length.
Note: See TracChangeset for help on using the changeset viewer.