]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/util/critical_alloc_section.hh
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / seastar / include / seastar / util / critical_alloc_section.hh
1 /*
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.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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
16 * under the License.
17 */
18 /*
19 * Copyright 2020 ScyllaDB
20 */
21
22 #pragma once
23
24 namespace seastar {
25 namespace memory {
26
27 #ifdef SEASTAR_ENABLE_ALLOC_FAILURE_INJECTION
28
29 /// \cond internal
30 namespace internal {
31
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;
39 #else
40 extern __thread volatile int critical_alloc_section;
41 #endif
42
43 } // namespace internal
44 /// \endcond
45
46 /// \brief Marks scopes that contain critical allocations.
47 ///
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 {
58 public:
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;
64 }
65 ~scoped_critical_alloc_section() {
66 internal::critical_alloc_section = internal::critical_alloc_section - 1;
67 }
68 };
69
70 /// \brief Is the current context inside a critical alloc section?
71 ///
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);
76 }
77
78 #else // SEASTAR_ENABLE_ALLOC_FAILURE_INJECTION
79
80 struct [[maybe_unused]] scoped_critical_alloc_section {};
81
82 inline bool is_critical_alloc_section() {
83 return false;
84 }
85
86 #endif // SEASTAR_ENABLE_ALLOC_FAILURE_INJECTION
87
88 } // namespace seastar
89 } // namespace memory