]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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) 2018 ScyllaDB Ltd. | |
20 | */ | |
21 | ||
1e59de90 TL |
22 | #include <boost/test/tools/old/interface.hpp> |
23 | #include <cstddef> | |
24 | #include <seastar/core/internal/stall_detector.hh> | |
11fdf7f2 | 25 | #include <seastar/core/reactor.hh> |
9f95a23c | 26 | #include <seastar/core/thread_cputime_clock.hh> |
f67539c2 TL |
27 | #include <seastar/core/loop.hh> |
28 | #include <seastar/util/later.hh> | |
11fdf7f2 TL |
29 | #include <seastar/testing/test_case.hh> |
30 | #include <seastar/testing/thread_test_case.hh> | |
31 | #include <atomic> | |
32 | #include <chrono> | |
1e59de90 | 33 | #include <sys/mman.h> |
11fdf7f2 TL |
34 | |
35 | using namespace seastar; | |
36 | using namespace std::chrono_literals; | |
37 | ||
1e59de90 TL |
38 | static seastar::logger testlog("testlog"); |
39 | ||
11fdf7f2 TL |
40 | class temporary_stall_detector_settings { |
41 | std::chrono::milliseconds _old_threshold; | |
42 | std::function<void ()> _old_report; | |
43 | public: | |
1e59de90 TL |
44 | /** |
45 | * Temporarily (until destructor) overload the stall detector threshold and reporting function. | |
46 | * | |
47 | * Also resets the reported stalls counter to zero, so the next backtraces will not be supressed. | |
48 | */ | |
49 | temporary_stall_detector_settings(std::chrono::duration<double> threshold, std::function<void ()> report = {}) | |
11fdf7f2 TL |
50 | : _old_threshold(engine().get_blocked_reactor_notify_ms()) |
51 | , _old_report(engine().get_stall_detector_report_function()) { | |
52 | engine().update_blocked_reactor_notify_ms(std::chrono::duration_cast<std::chrono::milliseconds>(threshold)); | |
53 | engine().set_stall_detector_report_function(std::move(report)); | |
54 | } | |
1e59de90 | 55 | |
11fdf7f2 TL |
56 | ~temporary_stall_detector_settings() { |
57 | engine().update_blocked_reactor_notify_ms(_old_threshold); | |
58 | engine().set_stall_detector_report_function(std::move(_old_report)); | |
59 | } | |
60 | }; | |
61 | ||
1e59de90 TL |
62 | using void_fn = std::function<void()>; |
63 | ||
64 | void spin(std::chrono::duration<double> how_much, void_fn body = []{}) { | |
f67539c2 TL |
65 | auto end = internal::cpu_stall_detector::clock_type::now() + how_much; |
66 | while (internal::cpu_stall_detector::clock_type::now() < end) { | |
1e59de90 | 67 | body(); // spin! |
11fdf7f2 TL |
68 | } |
69 | } | |
70 | ||
1e59de90 TL |
71 | static void spin_user_hires(std::chrono::duration<double> how_much) { |
72 | auto end = std::chrono::high_resolution_clock::now() + how_much; | |
73 | while (std::chrono::high_resolution_clock::now() < end) { | |
74 | ||
75 | } | |
76 | } | |
77 | ||
78 | void spin_some_cooperatively(std::chrono::duration<double> how_much, void_fn body = []{}) { | |
11fdf7f2 TL |
79 | auto end = std::chrono::steady_clock::now() + how_much; |
80 | while (std::chrono::steady_clock::now() < end) { | |
1e59de90 | 81 | spin(200us, body); |
11fdf7f2 TL |
82 | if (need_preempt()) { |
83 | thread::yield(); | |
84 | } | |
85 | } | |
86 | } | |
87 | ||
88 | SEASTAR_THREAD_TEST_CASE(normal_case) { | |
89 | std::atomic<unsigned> reports{}; | |
90 | temporary_stall_detector_settings tsds(10ms, [&] { ++reports; }); | |
91 | spin_some_cooperatively(1s); | |
92 | BOOST_REQUIRE_EQUAL(reports, 0); | |
93 | } | |
94 | ||
95 | SEASTAR_THREAD_TEST_CASE(simple_stalls) { | |
96 | std::atomic<unsigned> reports{}; | |
97 | temporary_stall_detector_settings tsds(10ms, [&] { ++reports; }); | |
98 | unsigned nr = 10; | |
99 | for (unsigned i = 0; i < nr; ++i) { | |
100 | spin_some_cooperatively(100ms); | |
101 | spin(20ms); | |
102 | } | |
103 | spin_some_cooperatively(100ms); | |
11fdf7f2 | 104 | |
9f95a23c TL |
105 | // blocked-reactor-reports-per-minute defaults to 5, so we don't |
106 | // get all 10 reports. | |
107 | BOOST_REQUIRE_EQUAL(reports, 5); | |
108 | } | |
11fdf7f2 | 109 | |
9f95a23c TL |
110 | SEASTAR_THREAD_TEST_CASE(no_poll_no_stall) { |
111 | std::atomic<unsigned> reports{}; | |
112 | temporary_stall_detector_settings tsds(10ms, [&] { ++reports; }); | |
113 | spin_some_cooperatively(1ms); // need to yield so that stall detector change from above take effect | |
114 | static constexpr unsigned tasks = 2000; | |
115 | promise<> p; | |
116 | auto f = p.get_future(); | |
117 | parallel_for_each(boost::irange(0u, tasks), [&p] (unsigned int i) { | |
1e59de90 | 118 | (void)yield().then([i, &p] { |
9f95a23c TL |
119 | spin(500us); |
120 | if (i == tasks - 1) { | |
121 | p.set_value(); | |
122 | } | |
123 | }); | |
124 | return make_ready_future<>(); | |
125 | }).get(); | |
126 | f.get(); | |
127 | BOOST_REQUIRE_EQUAL(reports, 0); | |
128 | } | |
1e59de90 TL |
129 | |
130 | // Triggers stalls by spinning with a specify "body" function | |
131 | // which takes most of the spin time. | |
132 | static void test_spin_with_body(const char* what, void_fn body) { | |
133 | // The !count_stacks mode outputs stall notification to stderr as usual | |
134 | // and do not assert anything, but are intended for diagnosing | |
135 | // stall problems by inspecting the output. We expect the userspace | |
136 | // spin test to show no kernel callstack, and the kernel test to | |
137 | // show kernel backtraces in the mmap or munmap path, but this is | |
138 | // not exact since neither test spends 100% of its time in the | |
139 | // selected mode (of course, kernel stacks only appear if the | |
140 | // perf-based stall detected could be enabled). | |
141 | // | |
142 | // Then the count_stacks mode tests that the right number of stacks | |
143 | // were output. | |
144 | for (auto count_stacks : {false, true}) { | |
145 | testlog.info("Starting spin test: {}", what); | |
146 | std::atomic<unsigned> reports{}; | |
147 | std::function<void()> reporter = count_stacks ? std::function<void()>{[&]{ ++reports; }} : nullptr; | |
148 | temporary_stall_detector_settings tsds(10ms, std::move(reporter)); | |
149 | constexpr unsigned nr = 5; | |
150 | for (unsigned i = 0; i < nr; ++i) { | |
151 | spin_some_cooperatively(100ms, body); | |
152 | spin(20ms, body); | |
153 | } | |
154 | testlog.info("Ending spin test: {}", what); | |
155 | BOOST_CHECK_EQUAL(reports, count_stacks ? 5 : 0); | |
156 | } | |
157 | } | |
158 | ||
159 | SEASTAR_THREAD_TEST_CASE(spin_in_userspace) { | |
160 | // a body which spends almost all of its time in userspace | |
161 | test_spin_with_body("userspace", [] { spin_user_hires(1ms); }); | |
162 | } | |
163 | ||
164 | static void mmap_populate(size_t len) { | |
165 | void *p = mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, 0, 0); | |
166 | BOOST_REQUIRE(p != MAP_FAILED); | |
167 | BOOST_REQUIRE(munmap(p, len) == 0); | |
168 | } | |
169 | ||
170 | SEASTAR_THREAD_TEST_CASE(spin_in_kernel) { | |
171 | // a body which spends almost all of its time in the kernel | |
172 | // doing 128K mmaps | |
173 | test_spin_with_body("kernel", [] { mmap_populate(128 * 1024); }); | |
174 | } |