diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2022-04-11 21:15:40 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2022-04-12 22:38:31 +0100 |
commit | 2ce0f5185ba95131b3c538507323d8ecb561a0c2 (patch) | |
tree | e026d7533b9a9c86d03f8ebcfcceb78c4e2114fc /libstdc++-v3/include/std/stacktrace | |
parent | 6e5ad1cc24a315d07f24c95d76c269cafe2a8182 (diff) | |
download | gcc-2ce0f5185ba95131b3c538507323d8ecb561a0c2.zip gcc-2ce0f5185ba95131b3c538507323d8ecb561a0c2.tar.gz gcc-2ce0f5185ba95131b3c538507323d8ecb561a0c2.tar.bz2 |
libstdc++: Reduce memory usage in std::stacktrace::current
This adds an alternative callback for use in the overload of
basic_stacktrace::current that takes a max_depth parameter. The new
callback will not allow the container to grow past the initial capacity,
which is set to the specified maximum depth. This avoids allocating
memory for hundreds of frames only to discard them again because of a
small maximum depth limit.
For larger maximum depths the normal callback is used, with a smaller
initial capacity that can grow as needed. The container will be resized
to the given max depth after the entire backtrace has been produced
(relying on the fact that std::stacktrace_entry objects are trivially
destructible to elide their destruction).
Currently the value for "larger" limits is 128, so a max depth <= 128
will allocate capacity for exactly that many frames. A larger max depth
(or an unspecified max depth) will use an initial capacity of 64 frames
and grow as needed. Since each frame is only a uintptr_t value it might
be reasonable to increase the first value so that memory usage can be
capped for larger maximum depths.
This change also delays the creation of the libbacktrace state until we
actually need it, so that the state is not created if allocation fails.
libstdc++-v3/ChangeLog:
* include/std/stacktrace (basic_stacktrace::current): Replace
calls to _M_reserve and _S_curr_cb with call to _M_prepare.
Check return value of backtrace_simple when max depth given.
(basic_stacktrace::_M_reserve): Remove.
(basic_stacktrace::_S_curr_cb): Remove.
(basic_stacktrace::_M_prepare(size_type)): New function to
reserve initial capacity and return callback.
(basic_stacktrace::_Impl::_M_allocate): Remove check for 0 < n
and remove redundant zeroing of _M_frames and _M_capacity.
(basic_stacktrace::_Impl::_M_push_back): Add [[unlikely]]
attribute. Assign _Impl instead of swapping.
* testsuite/19_diagnostics/stacktrace/current.cc: New test.
Diffstat (limited to 'libstdc++-v3/include/std/stacktrace')
-rw-r--r-- | libstdc++-v3/include/std/stacktrace | 110 |
1 files changed, 62 insertions, 48 deletions
diff --git a/libstdc++-v3/include/std/stacktrace b/libstdc++-v3/include/std/stacktrace index 79038e8..5f928f1 100644 --- a/libstdc++-v3/include/std/stacktrace +++ b/libstdc++-v3/include/std/stacktrace @@ -237,15 +237,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static basic_stacktrace current(const allocator_type& __alloc = allocator_type()) noexcept { - auto __state = stacktrace_entry::_S_init(); basic_stacktrace __ret(__alloc); - if (!__ret._M_reserve(64)) [[unlikely]] - return __ret; - - if (__glibcxx_backtrace_simple(__state, 1, _S_curr_cb(), - nullptr, std::__addressof(__ret))) - __ret._M_clear(); - + if (auto __cb = __ret._M_prepare()) [[likely]] + { + auto __state = stacktrace_entry::_S_init(); + if (__glibcxx_backtrace_simple(__state, 1, __cb, nullptr, + std::__addressof(__ret))) + __ret._M_clear(); + } return __ret; } @@ -254,16 +253,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION current(size_type __skip, const allocator_type& __alloc = allocator_type()) noexcept { - auto __state = stacktrace_entry::_S_init(); basic_stacktrace __ret(__alloc); if (__skip >= __INT_MAX__) [[unlikely]] return __ret; - if (!__ret._M_reserve(64)) [[unlikely]] - return __ret; - - if (__glibcxx_backtrace_simple(__state, __skip + 1, _S_curr_cb(), - nullptr, std::__addressof(__ret))) - __ret._M_clear(); + if (auto __cb = __ret._M_prepare()) [[likely]] + { + auto __state = stacktrace_entry::_S_init(); + if (__glibcxx_backtrace_simple(__state, __skip + 1, __cb, nullptr, + std::__addressof(__ret))) + __ret._M_clear(); + } return __ret; } @@ -275,19 +274,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __glibcxx_assert(__skip <= (size_type(-1) - __max_depth)); - auto __state = stacktrace_entry::_S_init(); basic_stacktrace __ret(__alloc); - if (__max_depth == 0 || __skip >= __INT_MAX__) [[unlikely]] + if (__max_depth == 0) [[unlikely]] return __ret; - if (!__ret._M_reserve(std::min<int>(__max_depth, 64))) [[unlikely]] + if (__skip >= __INT_MAX__) [[unlikely]] return __ret; - - if (__glibcxx_backtrace_simple(__state, __skip + 1, _S_curr_cb(), - nullptr, std::__addressof(__ret))) - __ret._M_clear(); - else if (__ret.size() > __max_depth) - __ret.resize(__max_depth); - + if (auto __cb = __ret._M_prepare(__max_depth)) [[likely]] + { + auto __state = stacktrace_entry::_S_init(); + int __err = __glibcxx_backtrace_simple(__state, __skip + 1, __cb, + nullptr, + std::__addressof(__ret)); + if (__err < 0) + __ret._M_clear(); + else if (__ret.size() > __max_depth) + __ret._M_impl._M_size = __max_depth; + } return __ret; } @@ -524,12 +526,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } private: - bool - _M_reserve(size_type __n) noexcept - { - return _M_impl._M_allocate(_M_alloc, __n) != nullptr; - } - + // Precondition: _M_capacity != 0 bool _M_push_back(const value_type& __x) noexcept { @@ -543,18 +540,37 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_impl._M_deallocate(_M_alloc); } - static auto - _S_curr_cb() noexcept + // Precondition: __max_depth != 0 + auto + _M_prepare(size_type __max_depth = -1) noexcept -> int (*) (void*, uintptr_t) { - return [](void* __data, uintptr_t __pc) { + auto __cb = +[](void* __data, uintptr_t __pc) { auto& __s = *static_cast<basic_stacktrace*>(__data); stacktrace_entry __f; __f._M_pc = __pc; - if (__s._M_push_back(__f)) - return 0; - return 1; + if (__s._M_push_back(__f)) [[likely]] + return 0; // continue tracing + return -1; // stop tracing due to error }; + + if (__max_depth > 128) + __max_depth = 64; // soft limit, _M_push_back will reallocate + else + __cb = [](void* __data, uintptr_t __pc) { + auto& __s = *static_cast<basic_stacktrace*>(__data); + stacktrace_entry __f; + __f._M_pc = __pc; + if (__s.size() == __s._M_impl._M_capacity) [[unlikely]] + return 1; // stop tracing due to reaching max depth + if (__s._M_push_back(__f)) [[likely]] + return 0; // continue tracing + return -1; // stop tracing due to error + }; + + if (_M_impl._M_allocate(_M_alloc, __max_depth)) [[likely]] + return __cb; + return nullptr; } struct _Impl @@ -573,24 +589,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return std::min(__size_max, __alloc_max); } - // Precondition: _M_frames == nullptr + // Precondition: _M_frames == nullptr && __n != 0 pointer _M_allocate(allocator_type& __alloc, size_type __n) noexcept { - __try + if (__n <= _S_max_size(__alloc)) [[likely]] { - if (0 < __n && __n <= _S_max_size(__alloc)) [[likely]] + __try { _M_frames = __alloc.allocate(__n); _M_capacity = __n; return _M_frames; } + __catch (...) + { + } } - __catch (...) - { - } - _M_frames = nullptr; - _M_capacity = 0; return nullptr;; } @@ -612,11 +626,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_size = 0; } + // Precondition: _M_capacity != 0 bool _M_push_back(allocator_type& __alloc, const stacktrace_entry& __f) noexcept { - if (_M_size == _M_capacity) + if (_M_size == _M_capacity) [[unlikely]] { _Impl __tmp; if (auto __f = __tmp._M_allocate(__alloc, _M_capacity * 2)) @@ -624,13 +639,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION else return false; _M_deallocate(__alloc); - std::swap(*this, __tmp); + *this = __tmp; } stacktrace_entry* __addr = std::to_address(_M_frames + _M_size++); std::construct_at(__addr, __f); return true; } - }; [[no_unique_address]] allocator_type _M_alloc{}; |