move disk_buffer_pool out to its own file

This commit is contained in:
Arvid Norberg 2011-07-31 02:24:36 +00:00
parent a46fe97ab3
commit bc2f0c8c5e
5 changed files with 4 additions and 250 deletions

View File

@ -60,6 +60,7 @@ set(sources
logger
file_pool
lsd
disk_buffer_pool
disk_io_thread
enum_net
broadcast_socket

View File

@ -444,6 +444,7 @@ SOURCES =
logger
file_pool
lsd
disk_buffer_pool
disk_io_thread
enum_net
broadcast_socket

View File

@ -33,10 +33,6 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef TORRENT_DISK_IO_THREAD
#define TORRENT_DISK_IO_THREAD
#if defined TORRENT_DISK_STATS || defined TORRENT_STATS
#include <fstream>
#endif
#include "libtorrent/storage.hpp"
#include "libtorrent/allocator.hpp"
#include "libtorrent/io_service.hpp"
@ -48,11 +44,9 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/shared_array.hpp>
#include <deque>
#include "libtorrent/config.hpp"
#ifndef TORRENT_DISABLE_POOL_ALLOCATOR
#include <boost/pool/pool.hpp>
#endif
#include "libtorrent/session_settings.hpp"
#include "libtorrent/thread.hpp"
#include "libtorrent/disk_buffer_pool.hpp"
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
@ -229,78 +223,6 @@ namespace libtorrent
int read_queue_size;
};
struct TORRENT_EXPORT disk_buffer_pool : boost::noncopyable
{
disk_buffer_pool(int block_size);
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
~disk_buffer_pool();
#endif
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS || defined TORRENT_DISK_STATS
bool is_disk_buffer(char* buffer
, mutex::scoped_lock& l) const;
bool is_disk_buffer(char* buffer) const;
#endif
char* allocate_buffer(char const* category);
void free_buffer(char* buf);
void free_multiple_buffers(char** bufvec, int numbufs);
int block_size() const { return m_block_size; }
#ifdef TORRENT_STATS
int disk_allocations() const
{ return m_allocations; }
#endif
#ifdef TORRENT_DISK_STATS
std::ofstream m_disk_access_log;
#endif
void release_memory();
int in_use() const { return m_in_use; }
protected:
void free_buffer_impl(char* buf, mutex::scoped_lock& l);
// 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:
mutable mutex m_pool_mutex;
#ifndef TORRENT_DISABLE_POOL_ALLOCATOR
// memory pool for read and write operations
// and disk cache
boost::pool<page_aligned_allocator> m_pool;
#endif
#if defined TORRENT_DISK_STATS || defined TORRENT_STATS
int m_allocations;
#endif
#ifdef TORRENT_DISK_STATS
public:
void rename_buffer(char* buf, char const* category);
protected:
std::map<std::string, int> m_categories;
std::map<char*, std::string> m_buf_to_category;
std::ofstream m_log;
private:
#endif
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
int m_magic;
#endif
};
// this is a singleton consisting of the thread and a queue
// of disk io jobs
struct TORRENT_EXPORT disk_io_thread : disk_buffer_pool

View File

@ -31,6 +31,7 @@ libtorrent_rasterbar_la_SOURCES = \
ConvertUTF.cpp \
create_torrent.cpp \
disk_buffer_holder.cpp \
disk_buffer_pool.cpp \
disk_io_thread.cpp \
entry.cpp \
enum_net.cpp \

View File

@ -69,179 +69,8 @@ namespace libtorrent
bool is_read_operation(disk_io_job const& j);
bool operation_has_buffer(disk_io_job const& j);
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, m_settings.cache_buffer_chunk_size)
#endif
{
#if defined TORRENT_DISK_STATS || defined TORRENT_STATS
m_allocations = 0;
#endif
#ifdef TORRENT_DISK_STATS
m_log.open("disk_buffers.log", std::ios::trunc);
m_categories["read cache"] = 0;
m_categories["write cache"] = 0;
m_disk_access_log.open("disk_access.log", std::ios::trunc);
#endif
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
m_magic = 0x1337;
#endif
}
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
disk_buffer_pool::~disk_buffer_pool()
{
TORRENT_ASSERT(m_magic == 0x1337);
m_magic = 0;
}
#endif
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS || defined TORRENT_DISK_STATS
bool disk_buffer_pool::is_disk_buffer(char* buffer
, mutex::scoped_lock& l) const
{
TORRENT_ASSERT(m_magic == 0x1337);
#ifdef TORRENT_DISK_STATS
if (m_buf_to_category.find(buffer)
== m_buf_to_category.end()) return false;
#endif
#ifdef TORRENT_DISABLE_POOL_ALLOCATOR
return true;
#else
return m_pool.is_from(buffer);
#endif
}
bool disk_buffer_pool::is_disk_buffer(char* buffer) const
{
mutex::scoped_lock l(m_pool_mutex);
return is_disk_buffer(buffer, l);
}
#endif
char* disk_buffer_pool::allocate_buffer(char const* category)
{
mutex::scoped_lock l(m_pool_mutex);
TORRENT_ASSERT(m_magic == 0x1337);
#ifdef TORRENT_DISABLE_POOL_ALLOCATOR
char* ret = page_aligned_allocator::malloc(m_block_size);
#else
char* ret = (char*)m_pool.ordered_malloc();
m_pool.set_next_size(m_settings.cache_buffer_chunk_size);
#endif
++m_in_use;
#if TORRENT_USE_MLOCK
if (m_settings.lock_disk_cache)
{
#ifdef TORRENT_WINDOWS
VirtualLock(ret, m_block_size);
#else
mlock(ret, m_block_size);
#endif
}
#endif
#if defined TORRENT_DISK_STATS || defined TORRENT_STATS
++m_allocations;
#endif
#ifdef TORRENT_DISK_STATS
++m_categories[category];
m_buf_to_category[ret] = category;
m_log << log_time() << " " << category << ": " << m_categories[category] << "\n";
#endif
TORRENT_ASSERT(ret == 0 || is_disk_buffer(ret, l));
return ret;
}
#ifdef TORRENT_DISK_STATS
void disk_buffer_pool::rename_buffer(char* buf, char const* category)
{
mutex::scoped_lock l(m_pool_mutex);
TORRENT_ASSERT(is_disk_buffer(buf, l));
TORRENT_ASSERT(m_categories.find(m_buf_to_category[buf])
!= m_categories.end());
std::string const& prev_category = m_buf_to_category[buf];
--m_categories[prev_category];
m_log << log_time() << " " << prev_category << ": " << m_categories[prev_category] << "\n";
++m_categories[category];
m_buf_to_category[buf] = category;
m_log << log_time() << " " << category << ": " << m_categories[category] << "\n";
TORRENT_ASSERT(m_categories.find(m_buf_to_category[buf])
!= m_categories.end());
}
#endif
void disk_buffer_pool::free_multiple_buffers(char** bufvec, int numbufs)
{
char** end = bufvec + numbufs;
// sort the pointers in order to maximize cache hits
std::sort(bufvec, end);
mutex::scoped_lock l(m_pool_mutex);
for (; bufvec != end; ++bufvec)
{
char* buf = *bufvec;
TORRENT_ASSERT(buf);
free_buffer_impl(buf, l);;
}
}
void disk_buffer_pool::free_buffer(char* buf)
{
mutex::scoped_lock l(m_pool_mutex);
free_buffer_impl(buf, l);
}
void disk_buffer_pool::free_buffer_impl(char* buf, mutex::scoped_lock& l)
{
TORRENT_ASSERT(buf);
TORRENT_ASSERT(m_magic == 0x1337);
TORRENT_ASSERT(is_disk_buffer(buf, l));
#if defined TORRENT_DISK_STATS || defined TORRENT_STATS
--m_allocations;
#endif
#ifdef TORRENT_DISK_STATS
TORRENT_ASSERT(m_categories.find(m_buf_to_category[buf])
!= m_categories.end());
std::string const& category = m_buf_to_category[buf];
--m_categories[category];
m_log << log_time() << " " << category << ": " << m_categories[category] << "\n";
m_buf_to_category.erase(buf);
#endif
#if TORRENT_USE_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
m_pool.free(buf);
#endif
--m_in_use;
}
void disk_buffer_pool::release_memory()
{
TORRENT_ASSERT(m_magic == 0x1337);
#ifndef TORRENT_DISABLE_POOL_ALLOCATOR
mutex::scoped_lock l(m_pool_mutex);
m_pool.release_memory();
#endif
}
// ------- disk_io_thread ------
disk_io_thread::disk_io_thread(io_service& ios
, boost::function<void()> const& queue_callback
, file_pool& fp