From 5c12db28d2daa512c1b00feca85aa390a4d0705c Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Fri, 6 Feb 2009 09:46:13 +0000 Subject: [PATCH] added option to lock disk cache in physical memory --- docs/manual.rst | 6 ++++ include/libtorrent/disk_io_thread.hpp | 6 ++-- include/libtorrent/session_settings.hpp | 10 ++++++ src/disk_io_thread.cpp | 45 +++++++++++++++++++++++++ src/session_impl.cpp | 3 ++ 5 files changed, 68 insertions(+), 2 deletions(-) diff --git a/docs/manual.rst b/docs/manual.rst index 127d9cfa7..a764b643b 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -3253,6 +3253,8 @@ that will be sent to the tracker. The user-agent is a good way to identify your int seeding_piece_quota; int max_sparse_regions; + + bool lock_disk_cache; }; ``user_agent`` this is the client identification to the tracker. @@ -3584,6 +3586,10 @@ that will maintain or decrease the number of sparse regions are prioritized. To disable this functionality, set this to 0. It defaults to 0 on all platforms except windows. +``lock_disk_cache`` if lock disk cache is set to true the disk cache +that's in use, will be locked in physical memory, preventing it from +being swapped out. + pe_settings =========== diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index dd63ba4b8..2b02931e3 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -181,10 +181,14 @@ namespace libtorrent void release_memory(); + protected: + // number of bytes per block. The BitTorrent // protocol defines the block size to 16 KiB. const int m_block_size; + session_settings m_settings; + private: // this only protects the pool allocator @@ -307,8 +311,6 @@ namespace libtorrent cache_status m_cache_stats; int m_num_cached_blocks; - session_settings m_settings; - #ifdef TORRENT_DISK_STATS std::ofstream m_log; #endif diff --git a/include/libtorrent/session_settings.hpp b/include/libtorrent/session_settings.hpp index 2cd093bfd..f0be88018 100644 --- a/include/libtorrent/session_settings.hpp +++ b/include/libtorrent/session_settings.hpp @@ -155,6 +155,9 @@ namespace libtorrent , max_sparse_regions(30000) #else , max_sparse_regions(0) +#endif +#ifndef TORRENT_DISABLE_MLOCK + , lock_disk_cache(true) #endif {} @@ -513,6 +516,13 @@ namespace libtorrent // don't use unless you have to, it screws with rarest-first // piece selection, and reduces swarm performance int max_sparse_regions; + +#ifndef TORRENT_DISABLE_MLOCK + // if this is set to true, the memory allocated for the + // disk cache will be locked in physical RAM, never to + // be swapped out + bool lock_disk_cache; +#endif }; #ifndef TORRENT_DISABLE_DHT diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 4b312aa40..8e6ee06d7 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -43,6 +43,10 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/time.hpp" #endif +#ifndef TORRENT_DISABLE_MLOCK +#include +#endif + namespace libtorrent { disk_buffer_pool::disk_buffer_pool(int block_size) @@ -79,6 +83,17 @@ namespace libtorrent #else char* ret = (char*)m_pool.ordered_malloc(); #endif +#ifndef TORRENT_DISABLE_MLOCK + if (m_settings.lock_disk_cache) + { +#ifdef TORRENT_WINDOWS + VirtualLock(ret, m_block_size); +#else + mlock(ret, m_block_size); +#endif + } +#endif + #ifdef TORRENT_STATS ++m_allocations; ++m_categories[category]; @@ -101,6 +116,16 @@ namespace libtorrent m_log << log_time() << " " << category << ": " << m_categories[category] << "\n"; m_buf_to_category.erase(buf); #endif +#ifndef TORRENT_DISABLE_MLOCK + if (m_settings.lock_disk_cache) + { +#ifdef TORRENT_WINDOWS + VirtualUnlock(buf, m_block_size); +#else + munlock(buf, m_block_size); +#endif + } +#endif #ifdef TORRENT_DISABLE_POOL_ALLOCATOR page_aligned_allocator::free(buf); #else @@ -116,6 +141,16 @@ namespace libtorrent #else char* ret = (char*)m_pool.ordered_malloc(num_blocks); #endif +#ifndef TORRENT_DISABLE_MLOCK + if (m_settings.lock_disk_cache) + { +#ifdef TORRENT_WINDOWS + VirtualLock(ret, m_block_size * num_blocks); +#else + mlock(ret, m_block_size * num_blocks); +#endif + } +#endif #ifdef TORRENT_STATS m_allocations += num_blocks; m_categories[category] += num_blocks; @@ -139,6 +174,16 @@ namespace libtorrent m_log << log_time() << " " << category << ": " << m_categories[category] << "\n"; m_buf_to_category.erase(buf); #endif +#ifndef TORRENT_DISABLE_MLOCK + if (m_settings.lock_disk_cache) + { +#ifdef TORRENT_WINDOWS + VirtualUnlock(buf, m_block_size * num_blocks); +#else + munlock(buf, m_block_size * num_blocks); +#endif + } +#endif #ifdef TORRENT_DISABLE_POOL_ALLOCATOR page_aligned_allocator::free(buf); #else diff --git a/src/session_impl.cpp b/src/session_impl.cpp index b5dd39d8e..9afd52185 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -589,6 +589,9 @@ namespace aux { bool update_disk_io_thread = false; if (m_settings.cache_size != s.cache_size || m_settings.cache_expiry != s.cache_expiry +#ifndef TORRENT_DISABLE_MLOCK + || m_settings.lock_disk_cache != s.lock_disk_cache +#endif || m_settings.use_read_cache != s.use_read_cache) update_disk_io_thread = true;