(trunk libT) #3531 "Pausing 500 torrents uses 650 MB of memory" -- on OS X, when saving a benc/json file, send a hint to the OS to not cache the file.

This commit is contained in:
Charles Kerr 2010-12-17 15:36:31 +00:00
parent 47a60e005e
commit f713451647
3 changed files with 27 additions and 16 deletions

View File

@ -1663,6 +1663,7 @@ tr_bencToFile( const tr_benc * top, tr_fmt_mode mode, const char * filename )
/* if the file already exists, try to move it out of the way & keep it as a backup */
tmp = tr_strdup_printf( "%s.tmp.XXXXXX", filename );
fd = tr_mkstemp( tmp );
tr_set_file_for_single_pass( fd );
if( fd >= 0 )
{
int len;

View File

@ -258,9 +258,29 @@ tr_prefetch( int fd UNUSED, off_t offset UNUSED, size_t count UNUSED )
#endif
}
void
tr_set_file_for_single_pass( int fd )
{
if( fd >= 0 )
{
/* Set hints about the lookahead buffer and caching. It's okay
for these to fail silently, so don't let them affect errno */
const int err = errno;
#ifdef HAVE_POSIX_FADVISE
posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL );
#endif
#ifdef SYS_DARWIN
fcntl( fd, F_RDAHEAD, 1 );
fcntl( fd, F_NOCACHE, 1 );
#endif
errno = err;
}
}
int
tr_open_file_for_writing( const char * filename )
{
int fd;
int flags = O_WRONLY | O_CREAT;
#ifdef O_BINARY
flags |= O_BINARY;
@ -268,7 +288,9 @@ tr_open_file_for_writing( const char * filename )
#ifdef O_LARGEFILE
flags |= O_LARGEFILE;
#endif
return open( filename, flags, 0666 );
fd = open( filename, flags, 0666 );
tr_set_file_for_single_pass( fd );
return fd;
}
int
@ -291,21 +313,7 @@ tr_open_file_for_scanning( const char * filename )
/* open the file */
fd = open( filename, flags, 0666 );
if( fd >= 0 )
{
/* Set hints about the lookahead buffer and caching. It's okay
for these to fail silently, so don't let them affect errno */
const int err = errno;
#ifdef HAVE_POSIX_FADVISE
posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL );
#endif
#ifdef SYS_DARWIN
fcntl( fd, F_NOCACHE, 1 );
fcntl( fd, F_RDAHEAD, 1 );
#endif
errno = err;
}
tr_set_file_for_single_pass( fd );
return fd;
}

View File

@ -32,6 +32,8 @@ void tr_fdSetGlobalPeerLimit( tr_session * session, int limit );
****
***/
void tr_set_file_for_single_pass( int fd );
int tr_open_file_for_scanning( const char * filename );
int tr_open_file_for_writing( const char * filename );