Moved TORRENT_DISK_STATS to default_storage with the option of enable/disable at runtime.

This commit is contained in:
Alden Torres 2015-08-03 00:25:14 -04:00
parent 661a595cb0
commit cd8a4d2511
3 changed files with 73 additions and 62 deletions

View File

@ -443,6 +443,11 @@ namespace libtorrent
// file_storage, otherwise returns the original file_storage object. // file_storage, otherwise returns the original file_storage object.
file_storage const& files() const { return m_mapped_files?*m_mapped_files:m_files; } file_storage const& files() const { return m_mapped_files?*m_mapped_files:m_files; }
#ifdef TORRENT_DISK_STATS
static bool disk_write_access_log();
static void disk_write_access_log(bool enable);
#endif
private: private:
int sparse_end(int start) const; int sparse_end(int start) const;

View File

@ -81,11 +81,6 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent namespace libtorrent
{ {
#ifdef TORRENT_DISK_STATS
// this is defined and used in storage.cpp
extern FILE* g_access_log;
#endif
#if TORRENT_USE_ASSERTS #if TORRENT_USE_ASSERTS
#define TORRENT_PIECE_ASSERT(cond, piece) \ #define TORRENT_PIECE_ASSERT(cond, piece) \
@ -168,10 +163,6 @@ namespace libtorrent
m_disk_cache.set_settings(m_settings, ec); m_disk_cache.set_settings(m_settings, ec);
TORRENT_ASSERT(!ec); TORRENT_ASSERT(!ec);
#ifdef TORRENT_DISK_STATS
if (g_access_log == NULL) g_access_log = fopen("file_access.log", "a+");
#endif
#if TORRENT_USE_RLIMIT #if TORRENT_USE_RLIMIT
// ---- auto-cap open files ---- // ---- auto-cap open files ----
@ -200,15 +191,6 @@ namespace libtorrent
TORRENT_ASSERT(pieces.first == pieces.second); TORRENT_ASSERT(pieces.first == pieces.second);
#endif #endif
#ifdef TORRENT_DISK_STATS
if (g_access_log)
{
FILE* f = g_access_log;
g_access_log = NULL;
fclose(f);
}
#endif
TORRENT_ASSERT(m_magic == 0x1337); TORRENT_ASSERT(m_magic == 0x1337);
#if TORRENT_USE_ASSERTS #if TORRENT_USE_ASSERTS
m_magic = 0xdead; m_magic = 0xdead;

View File

@ -122,50 +122,6 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent namespace libtorrent
{ {
#ifdef TORRENT_DISK_STATS
static boost::atomic<int> event_id;
static mutex disk_access_mutex;
// this is opened and closed by the disk_io_thread class
FILE* g_access_log = NULL;
enum access_log_flags_t
{
op_read = 0,
op_write = 1,
op_start = 0,
op_end = 2
};
void write_access_log(boost::uint64_t offset, boost::uint32_t fileid, int flags, time_point timestamp)
{
if (g_access_log == NULL) return;
// the event format in the log is:
// uint64_t timestamp (microseconds)
// uint64_t file offset
// uint32_t file-id
// uint8_t event (0: start read, 1: start write, 2: complete read, 4: complete write)
char event[29];
char* ptr = event;
detail::write_uint64(timestamp.time_since_epoch().count(), ptr);
detail::write_uint64(offset, ptr);
detail::write_uint64((boost::uint64_t)event_id++, ptr);
detail::write_uint32(fileid, ptr);
detail::write_uint8(flags, ptr);
mutex::scoped_lock l(disk_access_mutex);
int ret = fwrite(event, 1, sizeof(event), g_access_log);
l.unlock();
if (ret != sizeof(event))
{
fprintf(stderr, "ERROR writing to disk access log: (%d) %s\n"
, errno, strerror(errno));
}
}
#endif
int copy_bufs(file::iovec_t const* bufs, int bytes, file::iovec_t* target) int copy_bufs(file::iovec_t const* bufs, int bytes, file::iovec_t* target)
{ {
int size = 0; int size = 0;
@ -231,6 +187,49 @@ namespace libtorrent
} }
} }
#endif #endif
#ifdef TORRENT_DISK_STATS
static boost::atomic<int> event_id;
static mutex disk_access_mutex;
// this is opened and closed by the disk_io_thread class
FILE* g_access_log = NULL;
enum access_log_flags_t
{
op_read = 0,
op_write = 1,
op_start = 0,
op_end = 2
};
void write_access_log(boost::uint64_t offset, boost::uint32_t fileid, int flags, time_point timestamp)
{
if (g_access_log == NULL) return;
// the event format in the log is:
// uint64_t timestamp (microseconds)
// uint64_t file offset
// uint32_t file-id
// uint8_t event (0: start read, 1: start write, 2: complete read, 4: complete write)
char event[29];
char* ptr = event;
detail::write_uint64(timestamp.time_since_epoch().count(), ptr);
detail::write_uint64(offset, ptr);
detail::write_uint64((boost::uint64_t)event_id++, ptr);
detail::write_uint32(fileid, ptr);
detail::write_uint8(flags, ptr);
mutex::scoped_lock l(disk_access_mutex);
int ret = fwrite(event, 1, sizeof(event), g_access_log);
l.unlock();
if (ret != sizeof(event))
{
fprintf(stderr, "ERROR writing to disk access log: (%d) %s\n"
, errno, strerror(errno));
}
}
#endif
} // anonymous namespace } // anonymous namespace
default_storage::default_storage(storage_params const& params) default_storage::default_storage(storage_params const& params)
@ -1388,6 +1387,31 @@ namespace libtorrent
return false; return false;
} }
#ifdef TORRENT_DISK_STATS
bool default_storage::disk_write_access_log() {
return g_access_log != NULL;
}
void default_storage::disk_write_access_log(bool enable) {
if (enable)
{
if (g_access_log == NULL)
{
g_access_log = fopen("file_access.log", "a+");
}
}
else
{
if (g_access_log != NULL)
{
FILE* f = g_access_log;
g_access_log = NULL;
fclose(f);
}
}
}
#endif
storage_interface* default_storage_constructor(storage_params const& params) storage_interface* default_storage_constructor(storage_params const& params)
{ {
return new default_storage(params); return new default_storage(params);