2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
19 * Copyright 2020 ScyllaDB
27 #ifdef SEASTAR_ENABLE_ALLOC_FAILURE_INJECTION
32 // This variable is used in hot paths so we want to avoid the compiler
33 // generating TLS init guards for it. In C++20 we have constinit to tell the
34 // compiler that it can be initialized compile time (although gcc still doesn't
35 // completely drops the init guards - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97848).
36 // In < c++20 we use `__thread` which results in no TLS init guards generated.
37 #ifdef __cpp_constinit
38 extern thread_local constinit volatile int critical_alloc_section;
40 extern __thread volatile int critical_alloc_section;
43 } // namespace internal
46 /// \brief Marks scopes that contain critical allocations.
48 /// Critical allocations are those, whose failure the application cannot
49 /// tolerate. In a perfect world, there should be no such allocation, but we
50 /// don't live in a perfect world.
51 /// This information is used by other parts of the memory subsystem:
52 /// * \ref alloc_failure_injector will not inject errors into these scopes.
53 /// * A memory diagnostics report will be dumped if an allocation fails in these
54 /// scopes when the memory diagnostics subsystem is configured to dump reports
55 /// for \ref alloc_failure_kind \ref alloc_failure_kind::critical or above.
56 /// See \ref set_dump_memory_diagnostics_on_alloc_failure_kind().
57 class scoped_critical_alloc_section {
59 scoped_critical_alloc_section() {
60 // we assume the critical_alloc_section is thread local
61 // and there's seastar threads are non-preemptive.
62 // Otherwise, this would require an atomic variable
63 internal::critical_alloc_section = internal::critical_alloc_section + 1;
65 ~scoped_critical_alloc_section() {
66 internal::critical_alloc_section = internal::critical_alloc_section - 1;
70 /// \brief Is the current context inside a critical alloc section?
72 /// Will return true if there is at least one \ref scoped_critical_alloc_section
73 /// alive in the current scope or the scope of any of the caller functions.
74 inline bool is_critical_alloc_section() {
75 return bool(internal::critical_alloc_section);
78 #else // SEASTAR_ENABLE_ALLOC_FAILURE_INJECTION
80 struct [[maybe_unused]] scoped_critical_alloc_section {};
82 inline bool is_critical_alloc_section() {
86 #endif // SEASTAR_ENABLE_ALLOC_FAILURE_INJECTION
88 } // namespace seastar