Skip to content

Commit

Permalink
Allow abort on OOM to be set both ways
Browse files Browse the repository at this point in the history
There is a global "abort on OOM" setting in the seastar, and
we allow enabling it via a public method in seastar::memory.

There was no corresponding method to disable it, but such
a method is useful in order to give applications full control
over this setting.

Introduce a new method which takes a boolean parameter in order to allow
both enabling and disabling the abort behavior. The old call is
deprecated and simply falls the new method with enabled=true.
  • Loading branch information
travisdowns committed Feb 23, 2023
1 parent ed16292 commit 19285ca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
20 changes: 20 additions & 0 deletions include/seastar/core/memory.hh
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,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 +225,24 @@ 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);


class statistics;

/// Capture a snapshot of memory allocation statistics for this lcore.
Expand Down
15 changes: 11 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,8 @@ 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);
}

void set_dump_memory_diagnostics_on_alloc_failure_kind(alloc_failure_kind kind) {
Expand Down Expand Up @@ -2268,8 +2272,11 @@ 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");
}
}
}

reclaimer::reclaimer(std::function<reclaiming_result ()> reclaim, reclaimer_scope) {
Expand Down

0 comments on commit 19285ca

Please sign in to comment.