Changeset 8725


Ignore:
Timestamp:
Jun 21, 2009, 7:36:51 AM (13 years ago)
Author:
charles
Message:

(trunk libT) more efficient closing of a torrent's open files

Location:
trunk/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/fdlimit.c

    r8707 r8725  
    8787    tr_bool    isWritable;
    8888    tr_bool    closeWhenDone;
     89    int        torrentId;
    8990    char       filename[MAX_PATH_LENGTH];
    9091    int        fd;
     
    381382/* returns an fd on success, or a -1 on failure and sets errno */
    382383int
    383 tr_fdFileCheckout( const char             * folder,
     384tr_fdFileCheckout( int                      torrentId,
     385                   const char             * folder,
    384386                   const char             * torrentFile,
    385387                   tr_bool                  doWrite,
     
    391393    char filename[MAX_PATH_LENGTH];
    392394
     395    assert( torrentId > 0 );
    393396    assert( folder && *folder );
    394397    assert( torrentFile && *torrentFile );
     
    400403    tr_lockLock( gFd->lock );
    401404
    402     /* Is it already open? */
    403     for( i = 0; i < gFd->openFileLimit; ++i )
     405    /* is it already open? */
     406    for( i=0; i<gFd->openFileLimit; ++i )
    404407    {
    405408        o = &gFd->openFiles[i];
     
    407410        if( !fileIsOpen( o ) )
    408411            continue;
    409 
     412        if( torrentId != o->torrentId )
     413            continue;
    410414        if( strcmp( filename, o->filename ) )
    411415            continue;
     
    423427        if( doWrite && !o->isWritable )
    424428        {
    425             dbgmsg(
    426                 "found it!  it's open and available, but isn't writable. closing..." );
     429            dbgmsg( "found it!  it's open and available, but isn't writable. closing..." );
    427430            TrCloseFile( i );
    428431            break;
     
    434437    }
    435438
    436     dbgmsg(
    437         "it's not already open.  looking for an open slot or an old file." );
     439    dbgmsg( "it's not already open.  looking for an open slot or an old file." );
    438440    while( winner < 0 )
    439441    {
     
    441443
    442444        /* look for the file that's been open longest */
    443         for( i = 0; i < gFd->openFileLimit; ++i )
     445        for( i=0; i<gFd->openFileLimit; ++i )
    444446        {
    445447            o = &gFd->openFiles[i];
     
    496498
    497499    dbgmsg( "checking out '%s' in slot %d", filename, winner );
     500    o->torrentId = torrentId;
    498501    o->isCheckedOut = 1;
    499502    o->closeWhenDone = 0;
     
    531534{
    532535    int i;
    533 
    534536    tr_lockLock( gFd->lock );
    535537
    536     for( i = 0; i < gFd->openFileLimit; ++i )
     538    for( i=0; i<gFd->openFileLimit; ++i )
    537539    {
    538540        struct tr_openfile * o = &gFd->openFiles[i];
     
    542544        dbgmsg( "tr_fdFileClose closing '%s'", filename );
    543545
    544         if( !o->isCheckedOut )
    545         {
    546             dbgmsg( "not checked out, so closing it now... '%s'", filename );
     546        assert( !o->isCheckedOut && "this is a test assertion... I *think* this is always true now" );
     547
     548        TrCloseFile( i );
     549    }
     550
     551    tr_lockUnlock( gFd->lock );
     552}
     553
     554void
     555tr_fdTorrentClose( int torrentId )
     556{
     557    int i;
     558    tr_lockLock( gFd->lock );
     559
     560    for( i=0; i<gFd->openFileLimit; ++i )
     561    {
     562        struct tr_openfile * o = &gFd->openFiles[i];
     563
     564        assert( !o->isCheckedOut && "this is a test assertion... I *think* this is always true now" );
     565
     566        if( fileIsOpen( o ) && o->torrentId == torrentId )
    547567            TrCloseFile( i );
    548         }
    549         else
    550         {
    551             dbgmsg(
    552                 "flagging file '%s', slot #%d to be closed when checked in",
    553                 gFd->openFiles[i].filename, i );
    554             o->closeWhenDone = 1;
    555         }
    556568    }
    557569
    558570    tr_lockUnlock( gFd->lock );
    559571}
     572
    560573
    561574/***
     
    737750    return gFd ? gFd->socketLimit : -1;
    738751}
    739 
  • trunk/libtransmission/fdlimit.h

    r8561 r8725  
    6868 * @see tr_fdFileClose
    6969 */
    70 int  tr_fdFileCheckout( const char             * folder,
     70int  tr_fdFileCheckout( int                      torrentId,
     71                        const char             * folder,
    7172                        const char             * torrentFile,
    7273                        tr_bool                  doWrite,
     
    9293 */
    9394void     tr_fdFileClose( const char * filename );
     95
     96
     97/**
     98 * Closes all the files associated with a given torrent id
     99 */
     100void tr_fdTorrentClose( int torrentId );
    94101
    95102
  • trunk/libtransmission/inout.c

    r8296 r8725  
    102102    if( ( ioMode == TR_IO_READ ) && !fileExists ) /* does file exist? */
    103103        err = errno;
    104     else if( ( fd = tr_fdFileCheckout ( tor->downloadDir, file->name, ioMode == TR_IO_WRITE, preallocationMode, file->length ) ) < 0 )
     104    else if( ( fd = tr_fdFileCheckout ( tor->uniqueId, tor->downloadDir, file->name, ioMode == TR_IO_WRITE, preallocationMode, file->length ) ) < 0 )
    105105        err = errno;
    106106    else if( tr_lseek( fd, (int64_t)fileOffset, SEEK_SET ) == -1 )
  • trunk/libtransmission/torrent.c

    r8709 r8725  
    13311331
    13321332static void
    1333 tr_torrentCloseLocalFiles( const tr_torrent * tor )
    1334 {
    1335     tr_file_index_t i;
    1336     struct evbuffer * buf = evbuffer_new( );
    1337 
    1338     assert( tr_isTorrent( tor ) );
    1339 
    1340     /* FIXME(libevent2) we're just using the evbuffer to build a key here anyway.
    1341        so we do (tor->info.fileCount * fd.openFileLimit) strcmps for these keys. :/
    1342        it would be more efficient to remove this code altogether and
    1343        add "int torrentId;" to "struct tr_openfile", and a new function
    1344        tr_fdCloseTorrentFiles( tr_session*, int torrentId ) */
    1345     for( i=0; i<tor->info.fileCount; ++i )
    1346     {
    1347         const tr_file * file = &tor->info.files[i];
    1348         evbuffer_drain( buf, EVBUFFER_LENGTH( buf ) );
    1349         evbuffer_add_printf( buf, "%s%s%s", tor->downloadDir, TR_PATH_DELIMITER_STR, file->name );
    1350         tr_fdFileClose( (const char*) EVBUFFER_DATA( buf ) );
    1351     }
    1352 
    1353     evbuffer_free( buf );
    1354 }
    1355 
    1356 static void
    13571333stopTorrent( void * vtor )
    13581334{
     
    13651341    tr_trackerStop( tor->tracker );
    13661342
    1367     tr_torrentCloseLocalFiles( tor );
     1343    tr_fdTorrentClose( tor->uniqueId );
    13681344}
    13691345
     
    15301506        tor->completeness = completeness;
    15311507        tor->needsSeedRatioCheck = TRUE;
    1532         tr_torrentCloseLocalFiles( tor );
     1508        tr_fdTorrentClose( tor->uniqueId );
    15331509        fireCompletenessChange( tor, completeness );
    15341510
     
    22192195
    22202196    /* close all the files because we're about to delete them */
    2221     tr_torrentCloseLocalFiles( tor );
     2197    tr_fdTorrentClose( tor->uniqueId );
    22222198
    22232199    if( tor->info.fileCount > 1 )
Note: See TracChangeset for help on using the changeset viewer.