Seastar
High performance C++ framework for concurrent servers
preempt.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 (C) 2016 ScyllaDB.
20 */
21
22#pragma once
23#ifndef SEASTAR_MODULE
24#include <atomic>
25#include <seastar/util/modules.hh>
26#endif
27
28namespace seastar {
29
30namespace internal {
31
32struct preemption_monitor {
33 // We preempt when head != tail
34 // This happens to match the Linux aio completion ring, so we can have the
35 // kernel preempt a task by queuing a completion event to an io_context.
36 std::atomic<uint32_t> head;
37 std::atomic<uint32_t> tail;
38};
39
40#ifdef SEASTAR_BUILD_SHARED_LIBS
41const preemption_monitor*& get_need_preempt_var();
42#else
43inline const preemption_monitor*& get_need_preempt_var() {
44 static preemption_monitor bootstrap_preemption_monitor;
45 static thread_local const preemption_monitor* g_need_preempt = &bootstrap_preemption_monitor;
46 return g_need_preempt;
47}
48#endif
49
50void set_need_preempt_var(const preemption_monitor* pm);
51
52inline
53bool
54monitor_need_preempt() noexcept {
55 // prevent compiler from eliminating loads in a loop
56 std::atomic_signal_fence(std::memory_order_seq_cst);
57 auto np = internal::get_need_preempt_var();
58 // We aren't reading anything from the ring, so we don't need
59 // any barriers.
60 auto head = np->head.load(std::memory_order_relaxed);
61 auto tail = np->tail.load(std::memory_order_relaxed);
62 // Possible optimization: read head and tail in a single 64-bit load,
63 // and find a funky way to compare the two 32-bit halves.
64 return __builtin_expect(head != tail, false);
65}
66
67}
68
69SEASTAR_MODULE_EXPORT
70inline bool need_preempt() noexcept {
71#ifndef SEASTAR_DEBUG
72 return internal::monitor_need_preempt();
73#else
74 return true;
75#endif
76}
77
78namespace internal {
79
80
81
82// Same as need_preempt(), but for the scheduler's use. Outside debug
83// mode they have the same meaning - the task quota expired and we need
84// to check for I/O.
85inline
86bool
87scheduler_need_preempt() {
88#ifndef SEASTAR_DEBUG
89 return need_preempt();
90#else
91 // Within the scheduler, preempting all the time (as need_preempt()
92 // does in debug mode) reduces performance drastically since we check
93 // for I/O after every task. Since we don't care about latency in debug
94 // mode, run some random-but-bounded number of tasks instead. Latency
95 // will be high if those tasks are slow, but this is debug mode anyway.
96 // We still check if preemption was requested to allow lowres_clock
97 // updates.
98 static thread_local unsigned counter = 0;
99 return ++counter % 64 == 0 || monitor_need_preempt();;
100#endif
101}
102
103}
104
105}
Seastar API namespace.
Definition: abort_on_ebadf.hh:26