From 1ed583011c87c4049a9ec9bc448ecd2146cb6fe3 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 12 Jul 2008 17:51:59 +0000 Subject: [PATCH] made the invariant checks cheaper and more useful. The full invariant checks are still available as an option --- Jamfile | 3 +- docs/building.rst | 212 +++++++++++++++++++++------------------- src/peer_connection.cpp | 13 ++- src/torrent.cpp | 4 + 4 files changed, 123 insertions(+), 109 deletions(-) diff --git a/Jamfile b/Jamfile index 45a407dbe..3377e7d67 100755 --- a/Jamfile +++ b/Jamfile @@ -203,8 +203,9 @@ feature.compose off : TORRENT_DISABLE_GEO_IP ; feature bandwidth-limit-logging : off on : composite propagated link-incompatible ; feature.compose on : TORRENT_VERBOSE_BANDWIDTH_LIMIT ; -feature invariant-checks : on off : composite propagated link-incompatible ; +feature invariant-checks : on off full : composite propagated link-incompatible ; feature.compose off : TORRENT_DISABLE_INVARIANT_CHECKS ; +feature.compose full : TORRENT_EXPENSIVE_INVARIANT_CHECKS ; feature disk-stats : off on : composite propagated link-incompatible ; feature.compose on : TORRENT_DISK_STATS ; diff --git a/docs/building.rst b/docs/building.rst index 01e21d3c4..2a6149fb4 100644 --- a/docs/building.rst +++ b/docs/building.rst @@ -315,6 +315,8 @@ Build features: | | * ``off`` - internal invariant checks are | | | disabled. The resulting executable will run | | | faster than a regular debug build. | +| | * ``full`` - turns on extra expensive invariant | +| | checks. | +------------------------+----------------------------------------------------+ | ``debug-symbols`` | * ``on`` - default for debug builds. This setting | | | is useful for building release builds with | @@ -493,109 +495,113 @@ invariant checks and asserts built into it. If you want to disable such checks (you want to do that in a release build) you can see the table below for which defines you can use to control the build. -+---------------------------------------+-------------------------------------------------+ -| macro | description | -+=======================================+=================================================+ -| ``NDEBUG`` | If you define this macro, all asserts, | -| | invariant checks and general debug code will be | -| | removed. Since there is quite a lot of code in | -| | in header files in libtorrent, it may be | -| | important to define the symbol consistently | -| | across compilation units, including the clients | -| | files. Potential problems is different | -| | compilation units having different views of | -| | structs and class layouts and sizes. | -+---------------------------------------+-------------------------------------------------+ -| ``TORRENT_LOGGING`` | This macro will enable logging of the session | -| | events, such as tracker announces and incoming | -| | connections (as well as blocked connections). | -+---------------------------------------+-------------------------------------------------+ -| ``TORRENT_DISABLE_GEO_IP`` | This is defined by default by the Jamfile. It | -| | disables the GeoIP features, and avoids linking | -| | against LGPL:ed code. | -+---------------------------------------+-------------------------------------------------+ -| ``TORRENT_VERBOSE_LOGGING`` | If you define this macro, every peer connection | -| | will log its traffic to a log file as well as | -| | the session log. | -+---------------------------------------+-------------------------------------------------+ -| ``TORRENT_STORAGE_DEBUG`` | This will enable extra expensive invariant | -| | checks in the storage, including logging of | -| | piece sorting. | -+---------------------------------------+-------------------------------------------------+ -| ``TORRENT_UPNP_LOGGING`` | Generates a "upnp.log" file with the UPnP | -| | traffic. This is very useful when debugging | -| | support for various UPnP routers. | -| | support for various UPnP routers. | -+---------------------------------------+-------------------------------------------------+ -| ``TORRENT_DISK_STATS`` | This will create a log of all disk activity | -| | which later can parsed and graphed using | -| | ``parse_disk_log.py``. | -+---------------------------------------+-------------------------------------------------+ -| ``TORRENT_STATS`` | This will generate a log with transfer rates, | -| | downloading torrents, seeding torrents, peers, | -| | connecting peers and disk buffers in use. The | -| | log can be parsed and graphed with | -| | ``parse_session_stats.py``. | -+---------------------------------------+-------------------------------------------------+ -| ``UNICODE`` | If building on windows this will make sure the | -| | UTF-8 strings in pathnames are converted into | -| | UTF-16 before they are passed to the file | -| | operations. | -+---------------------------------------+-------------------------------------------------+ -| ``LITTLE_ENDIAN`` | This will use the little endian version of the | -| | sha-1 code. If defined on a big-endian system | -| | the sha-1 hashes will be incorrect and fail. | -| | If it is not defined and ``__BIG_ENDIAN__`` | -| | isn't defined either (it is defined by Apple's | -| | GCC) both little-endian and big-endian versions | -| | will be built and the correct code will be | -| | chosen at run-time. | -+---------------------------------------+-------------------------------------------------+ -| ``TORRENT_DISABLE_POOL_ALLOCATOR`` | Disables use of ``boost::pool<>``. | -+---------------------------------------+-------------------------------------------------+ -| ``TORRENT_LINKING_SHARED`` | If this is defined when including the | -| | libtorrent headers, the classes and functions | -| | will be tagged with ``__declspec(dllimport)`` | -| | on msvc and default visibility on GCC 4 and | -| | later. Set this in your project if you're | -| | linking against libtorrent as a shared library. | -| | (This is set by the Jamfile when | -| | ``link=shared`` is set). | -+---------------------------------------+-------------------------------------------------+ -| ``TORRENT_BUILDING_SHARED`` | If this is defined, the functions and classes | -| | in libtorrent are marked with | -| | ``__declspec(dllexport)`` on msvc, or with | -| | default visibility on GCC 4 and later. This | -| | should be defined when building libtorrent as | -| | a shared library. (This is set by the Jamfile | -| | when ``link=shared`` is set). | -+---------------------------------------+-------------------------------------------------+ -| ``TORRENT_DISABLE_DHT`` | If this is defined, the support for trackerless | -| | torrents will be disabled. | -+---------------------------------------+-------------------------------------------------+ -| ``TORRENT_DHT_VERBOSE_LOGGING`` | This will enable verbose logging of the DHT | -| | protocol traffic. | -+---------------------------------------+-------------------------------------------------+ -| ``TORRENT_DISABLE_ENCRYPTION`` | This will disable any encryption support and | -| | the openssl dependency that comes with it. | -| | Encryption support is the peer connection | -| | encrypted supported by clients such as | -| | uTorrent, Azureus and KTorrent. | -+---------------------------------------+-------------------------------------------------+ -| ``_UNICODE`` | On windows, this will cause the file IO | -| | use wide character API, to properly support | -| | non-ansi characters. | -+---------------------------------------+-------------------------------------------------+ -| ``TORRENT_DISABLE_RESOLVE_COUNTRIES`` | Defining this will disable the ability to | -| | resolve countries of origin for peer IPs. | -+---------------------------------------+-------------------------------------------------+ -| ``TORRENT_DISABLE_INVARIANT_CHECKS`` | This will disable internal invariant checks in | -| | libtorrent. The invariant checks can sometime | -| | be quite expensive, they typically don't scale | -| | very well. This option can be used to still | -| | build in debug mode, with asserts enabled, but | -| | make the resulting executable faster. | -+---------------------------------------+-------------------------------------------------+ ++----------------------------------------+-------------------------------------------------+ +| macro | description | ++========================================+=================================================+ +| ``NDEBUG`` | If you define this macro, all asserts, | +| | invariant checks and general debug code will be | +| | removed. Since there is quite a lot of code in | +| | in header files in libtorrent, it may be | +| | important to define the symbol consistently | +| | across compilation units, including the clients | +| | files. Potential problems is different | +| | compilation units having different views of | +| | structs and class layouts and sizes. | ++----------------------------------------+-------------------------------------------------+ +| ``TORRENT_LOGGING`` | This macro will enable logging of the session | +| | events, such as tracker announces and incoming | +| | connections (as well as blocked connections). | ++----------------------------------------+-------------------------------------------------+ +| ``TORRENT_DISABLE_GEO_IP`` | This is defined by default by the Jamfile. It | +| | disables the GeoIP features, and avoids linking | +| | against LGPL:ed code. | ++----------------------------------------+-------------------------------------------------+ +| ``TORRENT_VERBOSE_LOGGING`` | If you define this macro, every peer connection | +| | will log its traffic to a log file as well as | +| | the session log. | ++----------------------------------------+-------------------------------------------------+ +| ``TORRENT_STORAGE_DEBUG`` | This will enable extra expensive invariant | +| | checks in the storage, including logging of | +| | piece sorting. | ++----------------------------------------+-------------------------------------------------+ +| ``TORRENT_UPNP_LOGGING`` | Generates a "upnp.log" file with the UPnP | +| | traffic. This is very useful when debugging | +| | support for various UPnP routers. | +| | support for various UPnP routers. | ++----------------------------------------+-------------------------------------------------+ +| ``TORRENT_DISK_STATS`` | This will create a log of all disk activity | +| | which later can parsed and graphed using | +| | ``parse_disk_log.py``. | ++----------------------------------------+-------------------------------------------------+ +| ``TORRENT_STATS`` | This will generate a log with transfer rates, | +| | downloading torrents, seeding torrents, peers, | +| | connecting peers and disk buffers in use. The | +| | log can be parsed and graphed with | +| | ``parse_session_stats.py``. | ++----------------------------------------+-------------------------------------------------+ +| ``UNICODE`` | If building on windows this will make sure the | +| | UTF-8 strings in pathnames are converted into | +| | UTF-16 before they are passed to the file | +| | operations. | ++----------------------------------------+-------------------------------------------------+ +| ``LITTLE_ENDIAN`` | This will use the little endian version of the | +| | sha-1 code. If defined on a big-endian system | +| | the sha-1 hashes will be incorrect and fail. | +| | If it is not defined and ``__BIG_ENDIAN__`` | +| | isn't defined either (it is defined by Apple's | +| | GCC) both little-endian and big-endian versions | +| | will be built and the correct code will be | +| | chosen at run-time. | ++----------------------------------------+-------------------------------------------------+ +| ``TORRENT_DISABLE_POOL_ALLOCATOR`` | Disables use of ``boost::pool<>``. | ++----------------------------------------+-------------------------------------------------+ +| ``TORRENT_LINKING_SHARED`` | If this is defined when including the | +| | libtorrent headers, the classes and functions | +| | will be tagged with ``__declspec(dllimport)`` | +| | on msvc and default visibility on GCC 4 and | +| | later. Set this in your project if you're | +| | linking against libtorrent as a shared library. | +| | (This is set by the Jamfile when | +| | ``link=shared`` is set). | ++----------------------------------------+-------------------------------------------------+ +| ``TORRENT_BUILDING_SHARED`` | If this is defined, the functions and classes | +| | in libtorrent are marked with | +| | ``__declspec(dllexport)`` on msvc, or with | +| | default visibility on GCC 4 and later. This | +| | should be defined when building libtorrent as | +| | a shared library. (This is set by the Jamfile | +| | when ``link=shared`` is set). | ++----------------------------------------+-------------------------------------------------+ +| ``TORRENT_DISABLE_DHT`` | If this is defined, the support for trackerless | +| | torrents will be disabled. | ++----------------------------------------+-------------------------------------------------+ +| ``TORRENT_DHT_VERBOSE_LOGGING`` | This will enable verbose logging of the DHT | +| | protocol traffic. | ++----------------------------------------+-------------------------------------------------+ +| ``TORRENT_DISABLE_ENCRYPTION`` | This will disable any encryption support and | +| | the openssl dependency that comes with it. | +| | Encryption support is the peer connection | +| | encrypted supported by clients such as | +| | uTorrent, Azureus and KTorrent. | ++----------------------------------------+-------------------------------------------------+ +| ``_UNICODE`` | On windows, this will cause the file IO | +| | use wide character API, to properly support | +| | non-ansi characters. | ++----------------------------------------+-------------------------------------------------+ +| ``TORRENT_DISABLE_RESOLVE_COUNTRIES`` | Defining this will disable the ability to | +| | resolve countries of origin for peer IPs. | ++----------------------------------------+-------------------------------------------------+ +| ``TORRENT_DISABLE_INVARIANT_CHECKS`` | This will disable internal invariant checks in | +| | libtorrent. The invariant checks can sometime | +| | be quite expensive, they typically don't scale | +| | very well. This option can be used to still | +| | build in debug mode, with asserts enabled, but | +| | make the resulting executable faster. | ++----------------------------------------+-------------------------------------------------+ +| ``TORRENT_EXPENSIVE_INVARIANT_CHECKS`` | This will enable extra expensive invariant | +| | checks. Useful for finding particular bugs | +| | or for running before releases. | ++----------------------------------------+-------------------------------------------------+ If you experience that libtorrent uses unreasonable amounts of cpu, it will diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 5568ca61c..845d014b4 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -3586,9 +3586,6 @@ namespace libtorrent boost::shared_ptr t = m_torrent.lock(); if (m_disconnecting) { - for (aux::session_impl::torrent_map::const_iterator i = m_ses.m_torrents.begin() - , end(m_ses.m_torrents.end()); i != end; ++i) - TORRENT_ASSERT(!i->second->has_peer((peer_connection*)this)); TORRENT_ASSERT(!t); } else if (!m_in_constructor) @@ -3629,11 +3626,13 @@ namespace libtorrent if (!t) { +#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS // since this connection doesn't have a torrent reference // no torrent should have a reference to this connection either for (aux::session_impl::torrent_map::const_iterator i = m_ses.m_torrents.begin() , end(m_ses.m_torrents.end()); i != end; ++i) TORRENT_ASSERT(!i->second->has_peer((peer_connection*)this)); +#endif return; } @@ -3643,7 +3642,9 @@ namespace libtorrent for (torrent::const_peer_iterator i = t->begin(); i != t->end(); ++i) { // make sure this peer is not a dangling pointer +#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS TORRENT_ASSERT(m_ses.has_peer(*i)); +#endif peer_connection const& p = *(*i); for (std::deque::const_iterator i = p.request_queue().begin() , end(p.request_queue().end()); i != end; ++i) @@ -3659,16 +3660,18 @@ namespace libtorrent TORRENT_ASSERT(t->picker().num_peers(i->first) == i->second); } } +#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS if (m_peer_info) { policy::const_iterator i; - for (i = t->get_policy().begin_peer(); - i != t->get_policy().end_peer(); ++i) + for (i = t->get_policy().begin_peer() + , end(t->get_policy().end_peer()); i != end; ++i) { if (&i->second == m_peer_info) break; } TORRENT_ASSERT(i != t->get_policy().end_peer()); } +#endif if (t->has_picker() && !t->is_aborted()) { // make sure that pieces that have completed the download diff --git a/src/torrent.cpp b/src/torrent.cpp index e00ba1236..dd388de2b 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -3351,8 +3351,10 @@ namespace libtorrent std::map num_requests; for (const_peer_iterator i = begin(); i != end(); ++i) { +#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS // make sure this peer is not a dangling pointer TORRENT_ASSERT(m_ses.has_peer(*i)); +#endif peer_connection const& p = *(*i); for (std::deque::const_iterator i = p.request_queue().begin() , end(p.request_queue().end()); i != end; ++i) @@ -3387,11 +3389,13 @@ namespace libtorrent TORRENT_ASSERT(m_abort || m_picker->num_pieces() == 0); } +#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS for (policy::const_iterator i = m_policy.begin_peer() , end(m_policy.end_peer()); i != end; ++i) { TORRENT_ASSERT(i->second.ip.address() == i->first); } +#endif size_type total_done = quantized_bytes_done(); if (m_torrent_file->is_valid())