improved struct layout debug logging and optimized some structs

This commit is contained in:
Arvid Norberg 2011-09-21 09:27:07 +00:00
parent 61ceacca9b
commit 7a50255865
10 changed files with 199 additions and 40 deletions

View File

@ -117,7 +117,12 @@ namespace libtorrent
void set_name(char const* n, int borrow_chars = 0);
std::string filename() const;
// make it available for logging
#if !defined TORRENT_VERBOSE_LOGGING \
&& !defined TORRENT_LOGGING \
&& !defined TORRENT_ERROR_LOGGING
private:
#endif
// This string is not necessarily null terminated!
// that's why it's private, to keep people away from it
char const* name;
@ -246,7 +251,11 @@ namespace libtorrent
void set_file_base(internal_file_entry const& fe, size_type off);
std::string file_path(internal_file_entry const& fe) const;
#if !defined TORRENT_VERBOSE_LOGGING \
&& !defined TORRENT_LOGGING \
&& !defined TORRENT_ERROR_LOGGING
private:
#endif
void update_path_index(internal_file_entry& e);
void reorder_file(int index, int dst);

View File

@ -74,7 +74,13 @@ namespace libtorrent
int refcount() const { return m_refs; }
intrusive_ptr_base(): m_refs(0) {}
// so that we can access this when logging
#if !defined TORRENT_LOGGING \
&& !defined TORRENT_VERBOSE_LOGGING \
&& !defined TORRENT_ERROR_LOGGING
private:
#endif
// reference counter for intrusive_ptr
mutable boost::detail::atomic_count m_refs;
};

View File

@ -0,0 +1,59 @@
/*
Copyright (c) 2011, Arvid Norberg, Magnus Jonsson
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TORRENT_STRUCT_DEBUG
#define TORRENT_STRUCT_DEBUG
#define PRINT_SIZEOF(x) snprintf(tmp, sizeof(tmp), "\nsizeof(" #x ") = %d\n", int(sizeof(x))); \
l << tmp; \
temp = 0; \
prev_size = 0;
#define PRINT_OFFSETOF(x, y) if (offsetof(x, y) > 0) { \
snprintf(tmp, sizeof(tmp), "\tsize: %-3d\tpadding: %-3d\n" \
, prev_size \
, int((offsetof(x, y) - temp)) - prev_size); \
l << tmp; \
} \
snprintf(tmp, sizeof(tmp), "%-50s: %-3d" \
, #x "::" #y \
, int(offsetof(x, y))); \
temp = offsetof(x, y); \
prev_size = sizeof(reinterpret_cast<x*>(0)->y); \
l << tmp;
#define PRINT_OFFSETOF_END(x) snprintf(tmp, sizeof(tmp), "\tsize: %-3d\tpadding: %-3d\n" \
, prev_size, int((sizeof(x) - temp) - prev_size)); \
l << tmp;
#endif // TORRENT_STRUCT_DEBUG

View File

@ -428,6 +428,10 @@ namespace libtorrent
void copy_on_write();
bool parse_torrent_file(lazy_entry const& libtorrent, error_code& ec, int flags);
// the index to the first leaf. This is where the hash for the
// first piece is stored
boost::uint32_t m_merkle_first_leaf;
file_storage m_files;
// if m_files is modified, it is first copied into
@ -486,10 +490,6 @@ namespace libtorrent
// the hash that identifies this torrent
sha1_hash m_info_hash;
// the index to the first leaf. This is where the hash for the
// first piece is stored
boost::uint32_t m_merkle_first_leaf:24;
// the number of bytes in m_info_section
boost::uint32_t m_info_section_size:24;

View File

@ -168,8 +168,9 @@ namespace libtorrent
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
virtual void debug_log(const std::string& line) = 0;
#endif
#else
private:
#endif
tracker_manager* m_manager;
};
@ -187,7 +188,12 @@ namespace libtorrent
virtual void on_timeout(error_code const& ec) = 0;
virtual ~timeout_handler() {}
#if !defined TORRENT_VERBOSE_LOGGING \
&& !defined TORRENT_LOGGING \
&& !defined TORRENT_ERROR_LOGGING
// necessary for logging member offsets
private:
#endif
void timeout_callback(error_code const&);
@ -236,10 +242,24 @@ namespace libtorrent
virtual bool on_receive_hostname(error_code const& ec, char const* hostname
, char const* buf, int size) { return false; }
#if !defined TORRENT_VERBOSE_LOGGING \
&& !defined TORRENT_LOGGING \
&& !defined TORRENT_ERROR_LOGGING
// necessary for logging member offsets
protected:
#endif
boost::weak_ptr<request_callback> m_requester;
private:
tracker_manager& m_man;
#if !defined TORRENT_VERBOSE_LOGGING \
&& !defined TORRENT_LOGGING \
&& !defined TORRENT_ERROR_LOGGING
// necessary for logging member offsets
private:
#endif
const tracker_request m_req;
};

View File

@ -78,7 +78,9 @@ namespace libtorrent
void start();
void close();
#if !defined TORRENT_VERBOSE_LOGGING && !defined TORRENT_LOGGING && !defined TORRENT_ERROR_LOGGING
#if !defined TORRENT_VERBOSE_LOGGING \
&& !defined TORRENT_LOGGING \
&& !defined TORRENT_ERROR_LOGGING
// necessary for logging member offsets
private:
#endif
@ -112,7 +114,7 @@ namespace libtorrent
virtual void on_timeout(error_code const& ec);
tracker_manager& m_man;
// tracker_manager& m_man;
bool m_abort;
std::string m_hostname;

View File

@ -82,9 +82,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/extensions.hpp"
#include "libtorrent/random.hpp"
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
#endif
#ifndef TORRENT_WINDOWS
#include <sys/resource.h>
#endif
@ -93,6 +90,7 @@ POSSIBILITY OF SUCH DAMAGE.
// for logging stat layout
#include "libtorrent/stat.hpp"
#include "libtorrent/struct_debug.hpp"
// for logging the size of DHT structures
#ifndef TORRENT_DISABLE_DHT
@ -670,35 +668,43 @@ namespace aux {
, LIBTORRENT_REVISION);
(*m_logger) << tmp;
logger& l = *m_logger;
#define PRINT_SIZEOF(x) snprintf(tmp, sizeof(tmp), "sizeof(" #x ") = %d\n", int(sizeof(x))); (*m_logger) << tmp;
#define PRINT_OFFSETOF(x, y) snprintf(tmp, sizeof(tmp), " offsetof(" #x "," #y "): %d\n", int(offsetof(x, y))); \
(*m_logger) << tmp;
PRINT_SIZEOF(internal_file_entry)
int temp = 0;
int prev_size = 0;
PRINT_SIZEOF(announce_entry)
PRINT_OFFSETOF(announce_entry, url)
PRINT_OFFSETOF(announce_entry, trackerid)
PRINT_OFFSETOF(announce_entry, message)
PRINT_OFFSETOF(announce_entry, last_error)
PRINT_OFFSETOF(announce_entry, next_announce)
PRINT_OFFSETOF(announce_entry, min_announce)
PRINT_OFFSETOF(announce_entry, tier)
PRINT_OFFSETOF(announce_entry, fail_limit)
PRINT_OFFSETOF_END(announce_entry)
PRINT_SIZEOF(torrent_info)
PRINT_OFFSETOF(torrent_info, m_refs)
PRINT_OFFSETOF(torrent_info, m_merkle_first_leaf)
PRINT_OFFSETOF(torrent_info, m_files)
PRINT_OFFSETOF(torrent_info, m_orig_files)
PRINT_OFFSETOF(torrent_info, m_urls)
PRINT_OFFSETOF(torrent_info, m_web_seeds)
PRINT_OFFSETOF(torrent_info, m_nodes)
PRINT_OFFSETOF(torrent_info, m_merkle_tree)
PRINT_OFFSETOF(torrent_info, m_info_section)
PRINT_OFFSETOF(torrent_info, m_piece_hashes)
PRINT_OFFSETOF(torrent_info, m_info_dict)
PRINT_OFFSETOF(torrent_info, m_creation_date)
PRINT_OFFSETOF(torrent_info, m_comment)
PRINT_OFFSETOF(torrent_info, m_created_by)
#ifdef TORRENT_USE_OPENSSL
PRINT_OFFSETOF(torrent_info, m_ssl_root_cert)
PRINT_OFFSETOF(torrent_info, m_aes_key)
#endif
PRINT_OFFSETOF(torrent_info, m_info_dict)
PRINT_OFFSETOF(torrent_info, m_creation_date)
PRINT_OFFSETOF(torrent_info, m_info_hash)
PRINT_OFFSETOF_END(torrent_info)
PRINT_SIZEOF(union_endpoint)
PRINT_SIZEOF(request_callback)
@ -708,6 +714,23 @@ namespace aux {
(*m_logger) << "sizeof(utp_socket_impl): " << socket_impl_size() << "\n";
PRINT_SIZEOF(file_entry)
PRINT_SIZEOF(internal_file_entry)
PRINT_OFFSETOF(internal_file_entry, name)
PRINT_OFFSETOF(internal_file_entry, path_index)
PRINT_OFFSETOF_END(internal_file_entry)
PRINT_SIZEOF(file_storage)
PRINT_OFFSETOF(file_storage, m_files)
PRINT_OFFSETOF(file_storage, m_file_hashes)
PRINT_OFFSETOF(file_storage, m_symlinks)
PRINT_OFFSETOF(file_storage, m_mtime)
PRINT_OFFSETOF(file_storage, m_file_base)
PRINT_OFFSETOF(file_storage, m_paths)
PRINT_OFFSETOF(file_storage, m_name)
PRINT_OFFSETOF(file_storage, m_total_size)
PRINT_OFFSETOF(file_storage, m_num_pieces)
PRINT_OFFSETOF(file_storage, m_piece_length)
PRINT_OFFSETOF_END(file_storage)
// PRINT_SIZEOF(stat_channel)
// PRINT_OFFSETOF(stat_channel, m_counter)
@ -731,11 +754,22 @@ namespace aux {
#endif
PRINT_SIZEOF(policy::peer)
PRINT_OFFSETOF(policy::peer, prev_amount_upload)
PRINT_OFFSETOF(policy::peer, prev_amount_download)
PRINT_OFFSETOF(policy::peer, connection)
#ifndef TORRENT_DISABLE_GEO_IP
#ifdef TORRENT_DEBUG
PRINT_OFFSETOF(policy::peer, inet_as_num)
#endif
PRINT_OFFSETOF(policy::peer, inet_as)
#endif
PRINT_OFFSETOF(policy::peer, last_optimistically_unchoked)
PRINT_OFFSETOF(policy::peer, last_connected)
PRINT_OFFSETOF(policy::peer, port)
PRINT_OFFSETOF(policy::peer, upload_rate_limit)
PRINT_OFFSETOF(policy::peer, download_rate_limit)
PRINT_OFFSETOF(policy::peer, hashfails)
PRINT_OFFSETOF_END(policy::peer)
PRINT_SIZEOF(policy::ipv4_peer)
#if TORRENT_USE_IPV6
@ -744,13 +778,16 @@ namespace aux {
PRINT_SIZEOF(udp_socket)
PRINT_OFFSETOF(udp_socket, m_callback)
PRINT_OFFSETOF(udp_socket, m_callback2)
PRINT_OFFSETOF(udp_socket, m_ipv4_sock)
PRINT_OFFSETOF(udp_socket, m_v4_ep)
PRINT_OFFSETOF(udp_socket, m_v4_buf)
PRINT_OFFSETOF(udp_socket, m_reallocate_buffer4)
#if TORRENT_USE_IPV6
PRINT_OFFSETOF(udp_socket, m_ipv6_sock)
PRINT_OFFSETOF(udp_socket, m_v6_ep)
PRINT_OFFSETOF(udp_socket, m_v6_buf)
PRINT_OFFSETOF(udp_socket, m_reallocate_buffer6)
#endif
PRINT_OFFSETOF(udp_socket, m_bind_port)
PRINT_OFFSETOF(udp_socket, m_v4_outstanding)
@ -770,19 +807,34 @@ namespace aux {
PRINT_OFFSETOF(udp_socket, m_abort)
PRINT_OFFSETOF(udp_socket, m_proxy_addr)
PRINT_OFFSETOF(udp_socket, m_queue)
PRINT_OFFSETOF(udp_socket, m_outstanding_ops)
#ifdef TORRENT_DEBUG
PRINT_OFFSETOF(udp_socket, m_started)
PRINT_OFFSETOF(udp_socket, m_magic)
PRINT_OFFSETOF(udp_socket, m_outstanding_when_aborted)
#endif
PRINT_OFFSETOF_END(udp_socket)
PRINT_SIZEOF(tracker_connection)
PRINT_SIZEOF(http_tracker_connection)
PRINT_SIZEOF(udp_tracker_connection)
PRINT_OFFSETOF(udp_tracker_connection, m_refs)
PRINT_OFFSETOF(udp_tracker_connection, m_start_time)
PRINT_OFFSETOF(udp_tracker_connection, m_read_time)
PRINT_OFFSETOF(udp_tracker_connection, m_timeout)
PRINT_OFFSETOF(udp_tracker_connection, m_completion_timeout)
PRINT_OFFSETOF(udp_tracker_connection, m_read_timeout)
PRINT_OFFSETOF(udp_tracker_connection, m_mutex)
PRINT_OFFSETOF(udp_tracker_connection, timeout_handler::m_abort)
PRINT_OFFSETOF(udp_tracker_connection, m_requester)
#ifndef _MSC_VER
PRINT_OFFSETOF(udp_tracker_connection, m_man)
#endif
PRINT_OFFSETOF(udp_tracker_connection, m_req)
PRINT_OFFSETOF(udp_tracker_connection, m_abort)
PRINT_OFFSETOF(udp_tracker_connection, m_hostname)
PRINT_OFFSETOF(udp_tracker_connection, m_target)
@ -794,12 +846,14 @@ namespace aux {
PRINT_OFFSETOF(udp_tracker_connection, m_attempts)
PRINT_OFFSETOF(udp_tracker_connection, m_state)
PRINT_OFFSETOF(udp_tracker_connection, m_proxy)
PRINT_OFFSETOF_END(udp_tracker_connection)
#ifndef TORRENT_DISABLE_DHT
PRINT_SIZEOF(dht::find_data_observer)
PRINT_SIZEOF(dht::announce_observer)
PRINT_SIZEOF(dht::null_observer)
#endif
#undef PRINT_OFFSETOF_END
#undef PRINT_OFFSETOF
#undef PRINT_SIZEOF

View File

@ -88,6 +88,10 @@ POSSIBILITY OF SUCH DAMAGE.
//#include <boost/asio/ssl/verify_context.hpp>
#endif
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
#include "libtorrent/struct_debug.hpp"
#endif
#if TORRENT_USE_IOSTREAM
#include <iostream>
#endif
@ -208,14 +212,15 @@ namespace libtorrent
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
#define PRINT_SIZEOF(x) l << "sizeof(" #x "): " << sizeof(x) << "\n";
#define PRINT_OFFSETOF(x, y) l << " " << (offsetof(x, y) - temp) << " offsetof(" #x "," #y "): " << offsetof(x, y) << "\n"; temp = offsetof(x, y);
void torrent::print_size(logger& l)
{
char tmp[300];
int temp = 0;
int prev_size = 0;
PRINT_SIZEOF(torrent)
PRINT_OFFSETOF(torrent, m_tracker_address)
PRINT_OFFSETOF(torrent, m_manager)
PRINT_OFFSETOF(torrent, m_policy)
PRINT_OFFSETOF(torrent, m_total_uploaded)
PRINT_OFFSETOF(torrent, m_total_downloaded)
@ -239,6 +244,10 @@ namespace libtorrent
PRINT_OFFSETOF(torrent, m_password)
PRINT_OFFSETOF(torrent, m_net_interfaces)
PRINT_OFFSETOF(torrent, m_save_path)
PRINT_OFFSETOF(torrent, m_url)
PRINT_OFFSETOF(torrent, m_uuid)
PRINT_OFFSETOF(torrent, m_source_feed_url)
PRINT_OFFSETOF(torrent, m_torrent_file_buf)
PRINT_OFFSETOF(torrent, m_verified)
PRINT_OFFSETOF(torrent, m_error)
PRINT_OFFSETOF(torrent, m_error_file)

View File

@ -458,7 +458,8 @@ namespace libtorrent
}
torrent_info::torrent_info(torrent_info const& t, int flags)
: m_files(t.m_files)
: m_merkle_first_leaf(t.m_merkle_first_leaf)
, m_files(t.m_files)
, m_orig_files(t.m_orig_files)
, m_urls(t.m_urls)
, m_web_seeds(t.m_web_seeds)
@ -473,7 +474,6 @@ namespace libtorrent
#endif
, m_creation_date(t.m_creation_date)
, m_info_hash(t.m_info_hash)
, m_merkle_first_leaf(t.m_merkle_first_leaf)
, m_info_section_size(t.m_info_section_size)
, m_multifile(t.m_multifile)
, m_private(t.m_private)
@ -518,9 +518,9 @@ namespace libtorrent
#ifndef TORRENT_NO_DEPRECATE
// standard constructor that parses a torrent file
torrent_info::torrent_info(entry const& torrent_file)
: m_piece_hashes(0)
: m_merkle_first_leaf(0)
, m_piece_hashes(0)
, m_creation_date(0)
, m_merkle_first_leaf(0)
, m_info_section_size(0)
, m_multifile(false)
, m_private(false)
@ -551,9 +551,9 @@ namespace libtorrent
#ifndef BOOST_NO_EXCEPTIONS
torrent_info::torrent_info(lazy_entry const& torrent_file, int flags)
: m_piece_hashes(0)
: m_merkle_first_leaf(0)
, m_piece_hashes(0)
, m_creation_date(0)
, m_merkle_first_leaf(0)
, m_info_section_size(0)
, m_multifile(false)
, m_private(false)
@ -567,9 +567,9 @@ namespace libtorrent
}
torrent_info::torrent_info(char const* buffer, int size, int flags)
: m_piece_hashes(0)
: m_merkle_first_leaf(0)
, m_piece_hashes(0)
, m_creation_date(0)
, m_merkle_first_leaf(0)
, m_info_section_size(0)
, m_multifile(false)
, m_private(false)
@ -611,9 +611,9 @@ namespace libtorrent
#if TORRENT_USE_WSTRING
torrent_info::torrent_info(std::wstring const& filename, int flags)
: m_piece_hashes(0)
: m_merkle_first_leaf(0)
, m_piece_hashes(0)
, m_creation_date(0)
, m_merkle_first_leaf(0)
, m_info_section_size(0)
, m_multifile(false)
, m_private(false)
@ -652,9 +652,9 @@ namespace libtorrent
}
torrent_info::torrent_info(char const* buffer, int size, error_code& ec, int flags)
: m_piece_hashes(0)
: m_merkle_first_leaf(0)
, m_piece_hashes(0)
, m_creation_date(0)
, m_merkle_first_leaf(0)
, m_info_section_size(0)
, m_multifile(false)
, m_private(false)
@ -669,9 +669,9 @@ namespace libtorrent
}
torrent_info::torrent_info(std::string const& filename, error_code& ec, int flags)
: m_piece_hashes(0)
: m_merkle_first_leaf(0)
, m_piece_hashes(0)
, m_creation_date(0)
, m_merkle_first_leaf(0)
, m_info_section_size(0)
, m_multifile(false)
, m_private(false)
@ -691,9 +691,9 @@ namespace libtorrent
#if TORRENT_USE_WSTRING
torrent_info::torrent_info(std::wstring const& filename, error_code& ec, int flags)
: m_piece_hashes(0)
: m_merkle_first_leaf(0)
, m_piece_hashes(0)
, m_creation_date(0)
, m_merkle_first_leaf(0)
, m_info_section_size(0)
, m_multifile(false)
, m_private(false)
@ -719,10 +719,10 @@ namespace libtorrent
// just the necessary to use it with piece manager
// used for torrents with no metadata
torrent_info::torrent_info(sha1_hash const& info_hash, int flags)
: m_piece_hashes(0)
: m_merkle_first_leaf(0)
, m_piece_hashes(0)
, m_creation_date(time(0))
, m_info_hash(info_hash)
, m_merkle_first_leaf(0)
, m_info_section_size(0)
, m_multifile(false)
, m_private(false)

View File

@ -71,7 +71,7 @@ namespace libtorrent
, aux::session_impl& ses
, proxy_settings const& proxy)
: tracker_connection(man, req, ios, c)
, m_man(man)
// , m_man(man)
, m_abort(false)
, m_transaction_id(0)
, m_ses(ses)