From 033fd5bd223d1ecd4b2d8cb5c0f9ec9a88204d7b Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Fri, 1 May 2009 08:00:58 +0000 Subject: [PATCH] count send and receive buffers against the cache size limit --- docs/manual.rst | 7 +++++-- include/libtorrent/disk_io_thread.hpp | 5 +++++ include/libtorrent/session_settings.hpp | 2 +- src/disk_io_thread.cpp | 15 ++++++++++----- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/docs/manual.rst b/docs/manual.rst index 60d286216..243450d81 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -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. diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index d325d455e..76369dd98 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -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: diff --git a/include/libtorrent/session_settings.hpp b/include/libtorrent/session_settings.hpp index 072835358..8a4e32405 100644 --- a/include/libtorrent/session_settings.hpp +++ b/include/libtorrent/session_settings.hpp @@ -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) diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index af4eb0c5a..90e3b4f72 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -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; }