Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor abort on OOM changes #1506

Merged
merged 5 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions include/seastar/core/memory.hh
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ namespace seastar {
///
/// Often, the best way to debug an allocation failure is a coredump. This
/// feature allows dumping core on allocation failures, containing the stack of
/// the failed allocation, by means of aborting. To enable set the
/// `abort_on_seastar_bad_alloc` configuration option or the respective command
/// line flag.
/// the failed allocation, by means of aborting. To enable this behavior, set
/// `abort_on_seastar_bad_alloc` in `reactor_options` or pass the
/// `--abort-on-seastar-bad-alloc` command line flag. Additionally, applications
/// may enable or disable this functionality globally at runtime by calling
/// `set_abort_on_allocation_failure()`.
///
/// ### Dump diagnostics report
///
Expand Down Expand Up @@ -139,6 +141,8 @@ static constexpr size_t huge_page_size =
void configure(std::vector<resource::memory> m, bool mbind,
std::optional<std::string> hugetlbfs_path = {});

// A deprecated alias for set_abort_on_allocation_failure(true).
[[deprecated("use set_abort_on_allocation_failure(true) instead")]]
void enable_abort_on_allocation_failure();

class disable_abort_on_alloc_failure_temporarily {
Expand Down Expand Up @@ -223,6 +227,30 @@ void set_reclaim_hook(

/// \endcond

/// \brief Set the global state of the abort on allocation failure behavior.
///
/// If enabled, an allocation failure (i.e., the requested memory
/// could not be allocated even after reclaim was attempted), will
/// generally result in `abort()` being called. If disabled, the
/// failure is reported to the caller, e.g., by throwing a
/// `std::bad_alloc` for C++ allocations such as new, or returning
/// a null pointer from malloc.
///
/// Note that even if the global state is set to enabled, the
/// `disable_abort_on_alloc_failure_temporarily` class may override
/// the behavior tepmorarily on a given shard. That is, abort only
/// occurs if abort is globablly enabled on this shard _and_ there
/// are no `disable_abort_on_alloc_failure_temporarily` objects
/// currently alive on this shard.
void set_abort_on_allocation_failure(bool enabled);

/// \brief Determine the abort on allocation failure mode.
///
/// Return true if the global abort on allocation failure behavior
/// is enabled, or false otherwise. Always returns false if the
/// default (system) allocator is being used.
bool is_abort_on_allocation_failure();

class statistics;

/// Capture a snapshot of memory allocation statistics for this lcore.
Expand Down
22 changes: 18 additions & 4 deletions src/core/memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ disable_abort_on_alloc_failure_temporarily::~disable_abort_on_alloc_failure_temp
--abort_on_alloc_failure_suppressed;
}

void enable_abort_on_allocation_failure() {
set_abort_on_allocation_failure(true);
}

static std::pmr::polymorphic_allocator<char> static_malloc_allocator{std::pmr::get_default_resource()};;
std::pmr::polymorphic_allocator<char>* malloc_allocator{&static_malloc_allocator};

Expand Down Expand Up @@ -1600,8 +1604,12 @@ class disable_report_on_alloc_failure_temporarily {
static std::atomic<bool> abort_on_allocation_failure{false};
static std::atomic<alloc_failure_kind> dump_diagnostics_on_alloc_failure_kind{alloc_failure_kind::critical};

void enable_abort_on_allocation_failure() {
abort_on_allocation_failure.store(true, std::memory_order_seq_cst);
void set_abort_on_allocation_failure(bool enabled) {
abort_on_allocation_failure.store(enabled, std::memory_order_seq_cst);
}

bool is_abort_on_allocation_failure() {
return abort_on_allocation_failure;
}

void set_dump_memory_diagnostics_on_alloc_failure_kind(alloc_failure_kind kind) {
Expand Down Expand Up @@ -2268,8 +2276,14 @@ scoped_heap_profiling::scoped_heap_profiling() noexcept {
scoped_heap_profiling::~scoped_heap_profiling() {
}

void enable_abort_on_allocation_failure() {
seastar_logger.warn("Seastar compiled with default allocator, will not abort on bad_alloc");
void set_abort_on_allocation_failure(bool enabled) {
if (enabled) {
seastar_logger.warn("Seastar compiled with default allocator, will not abort on bad_alloc");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only warn if we are trying to turn enable abort on OOM since the other way is "OK", I suppose.

}
}

bool is_abort_on_allocation_failure() {
return false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inside ifdef DEFAULT_ALLOCATOR block.

}

reclaimer::reclaimer(std::function<reclaiming_result ()> reclaim, reclaimer_scope) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/reactor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4205,7 +4205,7 @@ void smp::configure(const smp_options& smp_opts, const reactor_options& reactor_
}

if (reactor_opts.abort_on_seastar_bad_alloc) {
memory::enable_abort_on_allocation_failure();
memory::set_abort_on_allocation_failure(true);
}

if (reactor_opts.dump_memory_diagnostics_on_alloc_failure_kind) {
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/alloc_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ SEASTAR_TEST_CASE(test_realloc_nullptr) {
return make_ready_future<>();
}

SEASTAR_TEST_CASE(test_enable_abort_on_oom) {
bool original = seastar::memory::is_abort_on_allocation_failure();

seastar::memory::set_abort_on_allocation_failure(false);
BOOST_CHECK(!seastar::memory::is_abort_on_allocation_failure());

seastar::memory::set_abort_on_allocation_failure(true);
BOOST_CHECK(seastar::memory::is_abort_on_allocation_failure());

seastar::memory::set_abort_on_allocation_failure(original);

return make_ready_future<>();
}

void * volatile sink;

SEASTAR_TEST_CASE(test_bad_alloc_throws) {
Expand Down