Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
alloc_failure_injector.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 2017 ScyllaDB
20 */
21
22#pragma once
23
24#include "modules.hh"
25#ifndef SEASTAR_MODULE
26#include <limits>
27#include <cstdint>
28#include <functional>
29#endif
30#include <seastar/util/noncopyable_function.hh>
31#include <seastar/util/critical_alloc_section.hh>
32#include <seastar/util/modules.hh>
33
34namespace seastar {
35namespace memory {
36
53SEASTAR_MODULE_EXPORT
55 uint64_t _alloc_count = 0;
56 uint64_t _fail_at = std::numeric_limits<uint64_t>::max();
57 noncopyable_function<void()> _on_alloc_failure = [] { throw std::bad_alloc(); };
58 bool _failed = false;
59private:
60 void fail();
61public:
64 if (is_critical_alloc_section()) {
65 return;
66 }
67 if (_alloc_count >= _fail_at) {
68 fail();
69 }
70 ++_alloc_count;
71 }
72
74 uint64_t alloc_count() const {
75 return _alloc_count;
76 }
77
79 void fail_after(uint64_t count) {
80 _fail_at = _alloc_count + count;
81 _failed = false;
82 }
83
85 void cancel() {
86 _fail_at = std::numeric_limits<uint64_t>::max();
87 }
88
90 bool failed() const {
91 return _failed;
92 }
93
95 void run_with_callback(noncopyable_function<void()> callback, noncopyable_function<void()> to_run);
96};
97
99extern thread_local alloc_failure_injector the_alloc_failure_injector;
101
103SEASTAR_MODULE_EXPORT
104inline
106 return the_alloc_failure_injector;
107}
108
109#ifdef SEASTAR_ENABLE_ALLOC_FAILURE_INJECTION
110
111#ifdef SEASTAR_DEFAULT_ALLOCATOR
112#error SEASTAR_ENABLE_ALLOC_FAILURE_INJECTION is not supported when using SEASTAR_DEFAULT_ALLOCATOR
113#endif
114
115#endif
116
117
118struct [[deprecated("Use scoped_critical_section instead")]] disable_failure_guard {
120};
121
123SEASTAR_MODULE_EXPORT
124inline
126#ifdef SEASTAR_ENABLE_ALLOC_FAILURE_INJECTION
128#endif
129}
130
136SEASTAR_MODULE_EXPORT
138
139}
140}
Definition: alloc_failure_injector.hh:54
void fail_after(uint64_t count)
Will cause count-th allocation point from now to fail, counting from 0.
Definition: alloc_failure_injector.hh:79
bool failed() const
Returns true iff allocation was failed since last fail_after().
Definition: alloc_failure_injector.hh:90
void on_alloc_point()
Marks a point in code which should be considered for failure injection.
Definition: alloc_failure_injector.hh:63
void cancel()
Cancels the failure scheduled by fail_after().
Definition: alloc_failure_injector.hh:85
uint64_t alloc_count() const
Counts encountered allocation points which didn't fail and didn't have failure suppressed.
Definition: alloc_failure_injector.hh:74
void run_with_callback(noncopyable_function< void()> callback, noncopyable_function< void()> to_run)
Runs given function with a custom failure action instead of the default std::bad_alloc throw.
void with_allocation_failures(noncopyable_function< void()> func)
void on_alloc_point()
Marks a point in code which should be considered for failure injection.
Definition: alloc_failure_injector.hh:125
alloc_failure_injector & local_failure_injector()
Return the shard-local alloc_failure_injector instance.
Definition: alloc_failure_injector.hh:105
Definition: alloc_failure_injector.hh:118
Definition: critical_alloc_section.hh:80
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
Definition: noncopyable_function.hh:37