From d99438bd1cdb60e0e7476a8e77b747d59789350e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 26 Apr 2009 00:51:51 +0000 Subject: [PATCH] (trunk libT) need feedback from Mac users on this change. On Linux, it gets rid of the inactive-memory-grows-during-torrent-verification behavior that's often reported as a bug. --- libtransmission/fdlimit.c | 22 ++++++++++++++++++++-- libtransmission/fdlimit.h | 5 ++++- libtransmission/verify.c | 6 +++--- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/libtransmission/fdlimit.c b/libtransmission/fdlimit.c index af97c92a1..2a22479ec 100644 --- a/libtransmission/fdlimit.c +++ b/libtransmission/fdlimit.c @@ -202,7 +202,7 @@ preallocateFileFull( const char * filename, uint64_t length ) } FILE* -tr_open_file_for_reading( const char * filename, tr_bool sequential ) +tr_open_file_for_scanning( const char * filename ) { int fd; int flags; @@ -228,12 +228,29 @@ tr_open_file_for_reading( const char * filename, tr_bool sequential ) return NULL; #ifdef HAVE_POSIX_FADVISE - posix_fadvise( fd, 0, 0, sequential ? POSIX_FADV_SEQUENTIAL : POSIX_FADV_RANDOM ); + posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL ); #endif return fdopen( fd, "r" ); } +static void +flush_before_closing( int fd ) +{ +#if defined(HAVE_POSIX_FADVISE) + posix_fadvise( fd, 0, 0, POSIX_FADV_DONTNEED ); +#elif defined(SYS_DARWIN) + fcntl( fd, F_NOCACHE, 1 ); +#endif +} + +void +tr_close_file( FILE * fp ) +{ + flush_before_closing( fileno( fp ) ); + fclose( fp ); +} + /** * returns 0 on success, or an errno value on failure. * errno values include ENOENT if the parent folder doesn't exist, @@ -327,6 +344,7 @@ TrCloseFile( int i ) assert( i < gFd->openFileLimit ); assert( fileIsOpen( o ) ); + flush_before_closing( o->fd ); close( o->fd ); o->fd = -1; o->isCheckedOut = 0; diff --git a/libtransmission/fdlimit.h b/libtransmission/fdlimit.h index 09a1915d6..5679b18e6 100644 --- a/libtransmission/fdlimit.h +++ b/libtransmission/fdlimit.h @@ -32,7 +32,10 @@ void tr_fdInit( size_t openFileLimit, size_t globalPeerLimit ); -FILE* tr_open_file_for_reading( const char * filename, tr_bool sequential ); +FILE* tr_open_file_for_scanning( const char * filename ); + +void tr_close_file( FILE * fp ); + /** * Returns an fd to the specified filename. diff --git a/libtransmission/verify.c b/libtransmission/verify.c index f5fef6ba0..172bdf15d 100644 --- a/libtransmission/verify.c +++ b/libtransmission/verify.c @@ -75,7 +75,7 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag ) if( !filePos && !fp ) { char * filename = tr_buildPath( tor->downloadDir, file->name, NULL ); - fp = tr_open_file_for_reading( filename, TRUE ); + fp = tr_open_file_for_scanning( filename ); /* fprintf( stderr, "opening file #%d (%s) -- %p\n", fileIndex, filename, fp ); */ tr_free( filename ); } @@ -129,7 +129,7 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag ) if( leftInFile == 0 ) { /* fprintf( stderr, "closing file\n" ); */ - if( fp != NULL ) { fclose( fp ); fp = NULL; } + if( fp != NULL ) { tr_close_file( fp ); fp = NULL; } ++fileIndex; filePos = 0; } @@ -137,7 +137,7 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag ) /* cleanup */ if( fp != NULL ) - fclose( fp ); + tr_close_file( fp ); tr_free( buffer ); #ifdef STOPWATCH