count send and receive buffers against the cache size limit

This commit is contained in:
Arvid Norberg 2009-05-01 08:00:58 +00:00
parent 84dda7b617
commit 033fd5bd22
4 changed files with 21 additions and 8 deletions

View File

@ -3554,8 +3554,11 @@ where they are only allowed to download whole pieces. If the whole piece a peer
in parole mode fails the hash check, it is banned. If a peer participates in a
piece that passes the hash check, it is taken out of parole mode.
``cache_size`` is the disk write cache. It is specified in units of 16 KiB blocks.
It defaults to 512 (= 8 MB).
``cache_size`` is the disk write and read cache. It is specified in units of
16 KiB blocks. It defaults to 1024 (= 16 MB). Buffers that are part of a peer's
send or receive buffer also count against this limit. Send and receive buffers
will never be denied to be allocated, but they will cause the actual cached blocks
to be flushed or evicted.
``cache_expiry`` is the number of seconds from the last cached write to a piece
in the write cache, to when it's forcefully flushed to disk. Default is 60 second.

View File

@ -181,12 +181,17 @@ namespace libtorrent
void release_memory();
int in_use() const { return m_in_use; }
protected:
// number of bytes per block. The BitTorrent
// protocol defines the block size to 16 KiB.
const int m_block_size;
// number of disk buffers currently allocated
int m_in_use;
session_settings m_settings;
private:

View File

@ -123,7 +123,7 @@ namespace libtorrent
, auto_upload_slots(true)
, auto_upload_slots_rate_based(false)
, use_parole_mode(true)
, cache_size(512)
, cache_size(1024)
, cache_expiry(60)
, use_read_cache(true)
, disk_io_write_mode(0)

View File

@ -51,6 +51,7 @@ namespace libtorrent
{
disk_buffer_pool::disk_buffer_pool(int block_size)
: m_block_size(block_size)
, m_in_use(0)
#ifndef TORRENT_DISABLE_POOL_ALLOCATOR
, m_pool(block_size)
#endif
@ -83,6 +84,7 @@ namespace libtorrent
#else
char* ret = (char*)m_pool.ordered_malloc();
#endif
++m_in_use;
#if TORRENT_USE_MLOCK
if (m_settings.lock_disk_cache)
{
@ -131,6 +133,7 @@ namespace libtorrent
#else
m_pool.ordered_free(buf);
#endif
--m_in_use;
}
char* disk_buffer_pool::allocate_buffers(int num_blocks, char const* category)
@ -141,6 +144,7 @@ namespace libtorrent
#else
char* ret = (char*)m_pool.ordered_malloc(num_blocks);
#endif
m_in_use += num_blocks;
#if TORRENT_USE_MLOCK
if (m_settings.lock_disk_cache)
{
@ -189,6 +193,7 @@ namespace libtorrent
#else
m_pool.ordered_free(buf, num_blocks);
#endif
m_in_use -= num_blocks;
}
void disk_buffer_pool::release_memory()
@ -567,7 +572,7 @@ namespace libtorrent
int end_block = start_block;
for (int i = start_block; i < blocks_in_piece
&& (m_cache_stats.cache_size < m_settings.cache_size
&& (in_use() < m_settings.cache_size
|| (options && ignore_cache_size)); ++i)
{
// this is a block that is already allocated
@ -652,14 +657,14 @@ namespace libtorrent
, cache_t::iterator ignore
, mutex_t::scoped_lock& l)
{
if (m_settings.cache_size - m_cache_stats.cache_size < num_blocks)
if (m_settings.cache_size - in_use() < num_blocks)
{
// there's not enough room in the cache, clear a piece
// from the read cache
if (!clear_oldest_read_piece(ignore, l)) return false;
}
return m_settings.cache_size - m_cache_stats.cache_size >= num_blocks;
return m_settings.cache_size - in_use() >= num_blocks;
}
// returns -1 on read error, -2 on out of memory error or the number of bytes read
@ -829,7 +834,7 @@ namespace libtorrent
// if read cache is disabled or we exceeded the
// limit, remove this piece from the cache
if (m_cache_stats.cache_size >= m_settings.cache_size
if (in_use() >= m_settings.cache_size
|| !m_settings.use_read_cache)
{
TORRENT_ASSERT(!m_read_pieces.empty());
@ -1279,7 +1284,7 @@ namespace libtorrent
// in the cache, we should not
// free it at the end
holder.release();
if (m_cache_stats.cache_size >= m_settings.cache_size)
if (in_use() >= m_settings.cache_size)
flush_oldest_piece(l);
break;
}