diff --git a/include/libtorrent/storage.hpp b/include/libtorrent/storage.hpp index 57a2ea458..7d16d1972 100644 --- a/include/libtorrent/storage.hpp +++ b/include/libtorrent/storage.hpp @@ -443,6 +443,11 @@ namespace libtorrent // file_storage, otherwise returns the original file_storage object. 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: int sparse_end(int start) const; diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 65d20a546..a788a771b 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -81,11 +81,6 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { -#ifdef TORRENT_DISK_STATS - // this is defined and used in storage.cpp - extern FILE* g_access_log; -#endif - #if TORRENT_USE_ASSERTS #define TORRENT_PIECE_ASSERT(cond, piece) \ @@ -168,10 +163,6 @@ namespace libtorrent m_disk_cache.set_settings(m_settings, 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 // ---- auto-cap open files ---- @@ -200,15 +191,6 @@ namespace libtorrent TORRENT_ASSERT(pieces.first == pieces.second); #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); #if TORRENT_USE_ASSERTS m_magic = 0xdead; diff --git a/src/storage.cpp b/src/storage.cpp index 6cb42ed01..85c3c3e3d 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -122,50 +122,6 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { - -#ifdef TORRENT_DISK_STATS - static boost::atomic 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 size = 0; @@ -231,6 +187,49 @@ namespace libtorrent } } #endif + +#ifdef TORRENT_DISK_STATS + static boost::atomic 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 default_storage::default_storage(storage_params const& params) @@ -1388,6 +1387,31 @@ namespace libtorrent 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) { return new default_storage(params);