diff options
author | babenko <babenko@yandex-team.com> | 2024-11-06 13:35:13 +0300 |
---|---|---|
committer | babenko <babenko@yandex-team.com> | 2024-11-06 14:01:26 +0300 |
commit | 14a70ab65849ee4dcf734051372b1ff5be87a793 (patch) | |
tree | 073b90a3452607dc47222c2065e6b2b6c214b040 /library/cpp | |
parent | b60a78031c047a8c8543bf6a3dc4f828d104dd3c (diff) | |
download | ydb-14a70ab65849ee4dcf734051372b1ff5be87a793.tar.gz |
Better diagnostics in AbortProcess(Silently|Dramatically)
commit_hash:7bbc13afbd569ca9bb064aed656c12644fd2de96
Diffstat (limited to 'library/cpp')
-rw-r--r-- | library/cpp/yt/memory/chunked_memory_pool-inl.h | 7 | ||||
-rw-r--r-- | library/cpp/yt/memory/new-inl.h | 12 | ||||
-rw-r--r-- | library/cpp/yt/memory/new.cpp | 18 | ||||
-rw-r--r-- | library/cpp/yt/memory/ya.make | 1 | ||||
-rw-r--r-- | library/cpp/yt/system/exit-inl.h | 32 | ||||
-rw-r--r-- | library/cpp/yt/system/exit.cpp | 16 | ||||
-rw-r--r-- | library/cpp/yt/system/exit.h | 30 | ||||
-rw-r--r-- | library/cpp/yt/system/ya.make | 4 |
8 files changed, 108 insertions, 12 deletions
diff --git a/library/cpp/yt/memory/chunked_memory_pool-inl.h b/library/cpp/yt/memory/chunked_memory_pool-inl.h index 2502204160..00733235f4 100644 --- a/library/cpp/yt/memory/chunked_memory_pool-inl.h +++ b/library/cpp/yt/memory/chunked_memory_pool-inl.h @@ -31,9 +31,10 @@ TDerived* TAllocationHolder::Allocate(size_t size, TRefCountedTypeCookie cookie) { auto requestedSize = sizeof(TDerived) + size; auto* ptr = ::malloc(requestedSize); - - if (!ptr) { - AbortProcess(ToUnderlying(EProcessExitCode::OutOfMemory)); + if (Y_UNLIKELY(!ptr)) { + AbortProcessDramatically( + EProcessExitCode::OutOfMemory, + "Out-of-memory during chunked memory pool allocation"); } #ifndef _win_ diff --git a/library/cpp/yt/memory/new-inl.h b/library/cpp/yt/memory/new-inl.h index f0646582ec..53308fb254 100644 --- a/library/cpp/yt/memory/new-inl.h +++ b/library/cpp/yt/memory/new-inl.h @@ -10,8 +10,6 @@ #include <library/cpp/yt/malloc//malloc.h> -#include <library/cpp/yt/system/exit.h> - namespace NYT { //////////////////////////////////////////////////////////////////////////////// @@ -178,6 +176,8 @@ Y_FORCE_INLINE TIntrusivePtr<T> SafeConstruct(void* ptr, As&&... args) } } +void AbortOnOom(); + template <size_t Size, size_t Alignment> Y_FORCE_INLINE void* AllocateConstSizeAlignedOrCrash() { @@ -192,7 +192,7 @@ Y_FORCE_INLINE void* AllocateConstSizeAlignedOrCrash() } #endif if (Y_UNLIKELY(!ptr)) { - AbortProcess(ToUnderlying(EProcessExitCode::OutOfMemory)); + AbortOnOom(); } return ptr; } @@ -211,7 +211,7 @@ Y_FORCE_INLINE void* AllocateOrCrash(size_t size) } #endif if (Y_UNLIKELY(!ptr)) { - AbortProcess(ToUnderlying(EProcessExitCode::OutOfMemory)); + AbortOnOom(); } return ptr; } @@ -249,7 +249,7 @@ Y_FORCE_INLINE TIntrusivePtr<T> New( { auto obj = TryNew<T>(allocator, std::forward<As>(args)...); if (Y_UNLIKELY(!obj)) { - AbortProcess(ToUnderlying(EProcessExitCode::OutOfMemory)); + NYT::NDetail::AbortOnOom(); } return obj; } @@ -288,7 +288,7 @@ Y_FORCE_INLINE TIntrusivePtr<T> NewWithExtraSpace( { auto obj = TryNewWithExtraSpace<T>(allocator, extraSpaceSize, std::forward<As>(args)...); if (Y_UNLIKELY(!obj)) { - AbortProcess(ToUnderlying(EProcessExitCode::OutOfMemory)); + NYT::NDetail::AbortOnOom(); } return obj; } diff --git a/library/cpp/yt/memory/new.cpp b/library/cpp/yt/memory/new.cpp new file mode 100644 index 0000000000..0b4a4b3b8b --- /dev/null +++ b/library/cpp/yt/memory/new.cpp @@ -0,0 +1,18 @@ +#include "new.h" + +#include <library/cpp/yt/system/exit.h> + +namespace NYT::NDetail { + +//////////////////////////////////////////////////////////////////////////////// + +void AbortOnOom() +{ + AbortProcessDramatically( + EProcessExitCode::OutOfMemory, + "Out-of-memory during object allocation"); +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT::NDetail diff --git a/library/cpp/yt/memory/ya.make b/library/cpp/yt/memory/ya.make index bd817454a9..5397dccf32 100644 --- a/library/cpp/yt/memory/ya.make +++ b/library/cpp/yt/memory/ya.make @@ -15,6 +15,7 @@ SRCS( chunked_memory_pool_output.cpp chunked_output_stream.cpp memory_tag.cpp + new.cpp ref.cpp ref_tracked.cpp safe_memory_reader.cpp diff --git a/library/cpp/yt/system/exit-inl.h b/library/cpp/yt/system/exit-inl.h new file mode 100644 index 0000000000..e4fe8ea594 --- /dev/null +++ b/library/cpp/yt/system/exit-inl.h @@ -0,0 +1,32 @@ +#ifndef EXIT_INL_H_ +#error "Direct inclusion of this file is not allowed, include exit.h" +// For the sake of sane code completion. +#include "exit.h" +#endif + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +template <class E> + requires std::is_enum_v<E> +[[noreturn]] void AbortProcessSilently(E exitCode) +{ + AbortProcessSilently(ToUnderlying(exitCode)); +} + +template <class E> + requires std::is_enum_v<E> +[[noreturn]] void AbortProcessDramatically( + E exitCode, + TStringBuf message) +{ + AbortProcessDramatically( + ToUnderlying(exitCode), + TEnumTraits<E>::FindLiteralByValue(exitCode).value_or("<unknown>"), + message); +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/library/cpp/yt/system/exit.cpp b/library/cpp/yt/system/exit.cpp index b17f6c5ad7..e4088fe4da 100644 --- a/library/cpp/yt/system/exit.cpp +++ b/library/cpp/yt/system/exit.cpp @@ -4,11 +4,25 @@ namespace NYT { //////////////////////////////////////////////////////////////////////////////// -void AbortProcess(int exitCode) +void AbortProcessSilently(int exitCode) { _exit(exitCode); } +void AbortProcessDramatically(int exitCode, TStringBuf exitCodeStr, TStringBuf message) +{ + fprintf(stderr, "\n"); + if (message) { + fprintf(stderr, "*** %s\n", message.data()); + } + fprintf(stderr, "*** Aborting process with exit code %d", exitCode); + if (exitCodeStr) { + fprintf(stderr, " (%s)", exitCodeStr.data()); + } + fprintf(stderr, "\n"); + _exit(exitCode); +} + //////////////////////////////////////////////////////////////////////////////// } // namespace NYT diff --git a/library/cpp/yt/system/exit.h b/library/cpp/yt/system/exit.h index 6c009fff90..e06c0bd3dd 100644 --- a/library/cpp/yt/system/exit.h +++ b/library/cpp/yt/system/exit.h @@ -2,6 +2,8 @@ #include <library/cpp/yt/misc/enum.h> +#include <type_traits> + namespace NYT { //////////////////////////////////////////////////////////////////////////////// @@ -14,9 +16,33 @@ DEFINE_ENUM(EProcessExitCode, ((OutOfMemory) (9)) ); -//! Invokes _exit to abort the process immediately without calling any cleanup code. -[[noreturn]] void AbortProcess(int exitCode); +//! Invokes _exit to abort the process immediately without calling any cleanup code +//! and without printing any details to stderr. +[[noreturn]] void AbortProcessSilently(int exitCode); + +//! A typed version of #AbortProcessSilently. +template <class E> + requires std::is_enum_v<E> +[[noreturn]] void AbortProcessSilently(E exitCode); + +//! Invokes _exit to abort the process immediately +//! without calling any cleanup code but printing error message to stderr. +[[noreturn]] void AbortProcessDramatically( + int exitCode, + TStringBuf exitCodeStr, + TStringBuf message); + +//! A typed version of #AbortProcessDramatically. +template <class E> + requires std::is_enum_v<E> +[[noreturn]] void AbortProcessDramatically( + E exitCode, + TStringBuf message); //////////////////////////////////////////////////////////////////////////////// } // namespace NYT + +#define EXIT_INL_H_ +#include "exit-inl.h" +#undef EXIT_INL_H_ diff --git a/library/cpp/yt/system/ya.make b/library/cpp/yt/system/ya.make index de4516d928..c60a940dbf 100644 --- a/library/cpp/yt/system/ya.make +++ b/library/cpp/yt/system/ya.make @@ -7,4 +7,8 @@ SRCS( thread_id.cpp ) +PEERDIR( + library/cpp/yt/misc +) + END() |