Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

future: hint need_preempt() accordingly to SEASTAR_DEBUG #8

Open
wants to merge 2 commits into
base: ceph-octopus
Choose a base branch
from

Conversation

rzarzynski
Copy link

@rzarzynski rzarzynski commented Oct 24, 2019

Although seastar::need_preempt() is really small in terms of code footprint, it's also frequently used with no code differences between occurrences. Not surprisingly compilers might consider it as a candidate to actually not inline. Moreover, GCC 8.1.0 on Ubuntu has failed to propagate the branch prediction hinting present in the definition:

inline bool need_preempt() noexcept {
    // ...
    return __builtin_expect(head != tail, false);
}

over function's callers.

The topic whether wrapping __builtin_expect() inside inlineable function stops the propagation was discussed on Stack Overflow (see https://stackoverflow.com/a/34427803) but without conclusion for GCC (though Clang forgets it for sure). However, objdump shows the hint has been lost:

  # fragment of crimson-osd's hot path: call the future-returning
  # `PGBackend::read()` and schedule a continuation with `then()`.
  # The future is always ready by the way.
  853b47:       4c 8d bd c0 fe ff ff    lea    -0x140(%rbp),%r15
  ...
  853b57:       4c 89 ff                mov    %r15,%rdi
  ...
  853b6c:       e8 af fb fc ff          callq  823720 <...>
  853b71:       48 83 bd c8 fe ff ff    cmpq   $0x1,-0x138(%rbp)
  853b78:       01
  ...
  853b7b:       0f 86 7f 03 00 00       jbe    853f00 <...>
  853b81:       e8 3a 89 f1 ff          callq  76c4c0 <_ZN7seastar12need_preemptEv>
  853b86:       84 c0                   test   %al,%al
  853b88:       0f 84 32 04 00 00       je     853fc0 <...>

After the patch the same fragment looks in following way:

  854397:       4c 8d bd c0 fe ff ff    lea    -0x140(%rbp),%r15
  ...
  8543a7:       4c 89 ff                mov    %r15,%rdi
  ...
  8543bc:       e8 6f f5 fc ff          callq  823930 <...>
  8543c1:       48 83 bd c8 fe ff ff    cmpq   $0x1,-0x138(%rbp)
  8543c8:       01
  ...
  8543cb:       0f 86 cf 02 00 00       jbe    8546a0 <...>
  8543d1:       e8 ca 89 f1 ff          callq  76cda0 <_ZN7seastar12need_preemptEv>
  8543d6:       84 c0                   test   %al,%al
  8543d8:       0f 85 c9 03 00 00       jne    8547a7 <...>

and the handling of ready, exception-less future is put on the optimistic, fallthrough path. This might have some impact on CPU's frontend but for sure makes reading assembler a little bit easier as humans are much slower with resteers than any CPU. That's also the reason why the hint value depends on SEASTAR_DEBUG.

Signed-off-by: Radoslaw Zarzynski [email protected]

Although `seastar::need_preempt()` is really small in terms
of code footprint, it's also frequently used with no code
differences between occurrences. Not surprisingly compilers
might consider it as a candidate to actually not inline.
Moreover, GCC 8.1.0 on Ubuntu has failed to propagate
the branch prediction hinting present in the definition:

```cpp
inline bool need_preempt() noexcept {
    // ...
    return __builtin_expect(head != tail, false);
}
```
over function's callers.

The topic whether wrapping `__builtin_expect()` inside inlineable
function stops the propagation was discussed on Stack Overflow
(see https://stackoverflow.com/a/34427803) but without conclusion
for GCC (but Clang forgets it for sure). However, `objdump` shows
the hint has been lost:

```
  # fragment of crimson-osd's hot path: call the future-returning
  # `PGBackend::read()` and schedule a continuation with `then()`.
  # The future is always ready by the way.
  853b47:       4c 8d bd c0 fe ff ff    lea    -0x140(%rbp),%r15
  ...
  853b57:       4c 89 ff                mov    %r15,%rdi
  ...
  853b6c:       e8 af fb fc ff          callq  823720 <...>
  853b71:       48 83 bd c8 fe ff ff    cmpq   $0x1,-0x138(%rbp)
  853b78:       01
  ...
  853b7b:       0f 86 7f 03 00 00       jbe    853f00 <...>
  853b81:       e8 3a 89 f1 ff          callq  76c4c0 <_ZN7seastar12need_preemptEv>
  853b86:       84 c0                   test   %al,%al
  853b88:       0f 84 32 04 00 00       je     853fc0 <...>
```

After the patch the same fragment looks in following way:
```
  854397:       4c 8d bd c0 fe ff ff    lea    -0x140(%rbp),%r15
  ...
  8543a7:       4c 89 ff                mov    %r15,%rdi
  ...
  8543bc:       e8 6f f5 fc ff          callq  823930 <...>
  8543c1:       48 83 bd c8 fe ff ff    cmpq   $0x1,-0x138(%rbp)
  8543c8:       01
  ...
  8543cb:       0f 86 cf 02 00 00       jbe    8546a0 <...>
  8543d1:       e8 ca 89 f1 ff          callq  76cda0 <_ZN7seastar12need_preemptEv>
  8543d6:       84 c0                   test   %al,%al
  8543d8:       0f 85 c9 03 00 00       jne    8547a7 <...>
```
and the handling of ready, exception-less future is put on the hot,
fallthrough path. This might have some impact on CPU's frontend but
for sure makes reading assembler a little bit easier as humans are
much slower with resteers than any CPU. That's also the reason why
the hint value depends on `SEASTAR_DEBUG`.

Signed-off-by: Radoslaw Zarzynski <[email protected]>
Copy link

@tchaikov tchaikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also i'd suggest fold these two commits into a single one.

include/seastar/core/preempt.hh Outdated Show resolved Hide resolved
include/seastar/core/preempt.hh Show resolved Hide resolved
@tchaikov
Copy link

tchaikov commented Oct 25, 2019

also tested with GCC 9.2

before the change:

(gdb) disass need_preempt
Dump of assembler code for function seastar::need_preempt():
=> 0x0000555555630f90 <+0>:     mov    $0xffffffffffffed80,%rax
   0x0000555555630f97 <+7>:     mov    %fs:(%rax),%rax
   0x0000555555630f9b <+11>:    mov    (%rax),%edx
   0x0000555555630f9d <+13>:    mov    0x4(%rax),%eax
   0x0000555555630fa0 <+16>:    cmp    %edx,%eax
   0x0000555555630fa2 <+18>:    setne  %al
   0x0000555555630fa5 <+21>:    retq
End of assembler dump.

so there is no jump at all! 🤣

on the caller site:

src/seastar/include/seastar/core/future.hh

1064                if (available() && !need_preempt()) {

   0x000055555571e452 <+290>:   jbe    0x55555571ee00 <seastar::reactor::configure(boost::program_options::variables_map)+2768>
   0x000055555571e458 <+296>:   callq  0x555555630f90 <seastar::need_preempt()>
=> 0x000055555571e45d <+301>:   test   %al,%al
   0x000055555571e45f <+303>:   jne    0x55555571e4e0 <seastar::reactor::configure(boost::program_options::variables_map)+432>
   0x000055555571e461 <+305>:   mov    -0x238(%rbp),%rax
   0x000055555571e468 <+312>:   mov    -0x240(%rbp),%rdx
   0x000055555571e46f <+319>:   cmp    $0x2,%rax /// inlined available(), compare _u.st with state::result (which is "2")

so the happy/predicted path assumes head == tail. this behavior is expected. but after applying

diff --git a/include/seastar/core/preempt.hh b/include/seastar/core/preempt.hh
index 722c7831..665a4050 100644
--- a/include/seastar/core/preempt.hh
+++ b/include/seastar/core/preempt.hh
@@ -49,7 +49,7 @@ inline bool need_preempt() noexcept {
     auto tail = np->tail.load(std::memory_order_relaxed);
     // Possible optimization: read head and tail in a single 64-bit load,
     // and find a funky way to compare the two 32-bit halves.
-    return __builtin_expect(head != tail, false);
+    return __builtin_expect(head != tail, true);
 #else
     return true;
 #endif

the compiler emits exactly the same assembly.

@rzarzynski
Copy link
Author

@tchaikov: need_preempt() looks exactly the same on my machine too (x86-64, Ubuntu, GCC 8.1.0).

After consideration I think we should keep the __builtin_expect() there intact. The reason I see is combination of two factors:

  • depending on compiler, targeted microarchitecture (-march=) and so on conditional jump might be emitted instead of setne. There is long but interesting post on StackOverflow about cmov and differences between GCCs.
  • The macro targets situations when a compiler decides to actually not inline the function. However, such decisions are made on per-occurance basis – there could be places where inlining happens. I haven't verified propagation of the hint in "the reversed direction".

@rzarzynski rzarzynski force-pushed the wip-hinted-need_preempt-ceph branch 2 times, most recently from f45b960 to 63b725c Compare October 29, 2019 14:58
@rzarzynski rzarzynski force-pushed the wip-hinted-need_preempt-ceph branch from 63b725c to afb186c Compare October 29, 2019 16:42
@rzarzynski rzarzynski force-pushed the wip-hinted-need_preempt-ceph branch from afb186c to 817938b Compare November 3, 2019 12:20
cyx1231st pushed a commit to cyx1231st/seastar that referenced this pull request Jan 2, 2020
This reverts commit 33406cf. It
introduces memory leaks:

Direct leak of 24 byte(s) in 1 object(s) allocated from:
    #0 0x7fb773b389d7 in operator new(unsigned long) (/lib64/libasan.so.5+0x10f9d7)
    ceph#1 0x108f0d4 in seastar::reactor::poller::~poller() ../src/core/reactor.cc:2879
    ceph#2 0x11c1e59 in std::experimental::fundamentals_v1::_Optional_base<seastar::reactor::poller, true>::~_Optional_base() /usr/include/c++/9/experimental/optional:288
    ceph#3 0x118f2d7 in std::experimental::fundamentals_v1::optional<seastar::reactor::poller>::~optional() /usr/include/c++/9/experimental/optional:491
    ceph#4 0x108c5a5 in seastar::reactor::run() ../src/core/reactor.cc:2587
    ceph#5 0xf1a822 in seastar::app_template::run_deprecated(int, char**, std::function<void ()>&&) ../src/core/app-template.cc:199
    ceph#6 0xf1885d in seastar::app_template::run(int, char**, std::function<seastar::future<int> ()>&&) ../src/core/app-template.cc:115
    ceph#7 0xeb2735 in operator() ../src/testing/test_runner.cc:72
    ceph#8 0xebb342 in _M_invoke /usr/include/c++/9/bits/std_function.h:300
    ceph#9 0xf3d8b0 in std::function<void ()>::operator()() const /usr/include/c++/9/bits/std_function.h:690
    ceph#10 0x1034c72 in seastar::posix_thread::start_routine(void*) ../src/core/posix.cc:52
    ceph#11 0x7fb7738804e1 in start_thread /usr/src/debug/glibc-2.30-13-g919af705ee/nptl/pthread_create.c:479

Reported-by: Rafael Avila de Espindola <[email protected]>
tchaikov pushed a commit that referenced this pull request May 13, 2021
…o_with

Fixes failures in debug mode:
```
$ build/debug/tests/unit/closeable_test -l all -t deferred_close_test
WARNING: debug mode. Not for benchmarking or production
random-seed=3064133628
Running 1 test case...
Entering test module "../../tests/unit/closeable_test.cc"
../../tests/unit/closeable_test.cc(0): Entering test case "deferred_close_test"
../../src/testing/seastar_test.cc(43): info: check true has passed
==9449==WARNING: ASan doesn't fully support makecontext/swapcontext functions and may produce false positives in some cases!
terminate called after throwing an instance of 'seastar::broken_promise'
  what():  broken promise
==9449==WARNING: ASan is ignoring requested __asan_handle_no_return: stack top: 0x7fbf1f49f000; bottom 0x7fbf40971000; size: 0xffffffffdeb2e000 (-558702592)
False positive error reports may follow
For details see google/sanitizers#189
=================================================================
==9449==AddressSanitizer CHECK failed: ../../../../libsanitizer/asan/asan_thread.cpp:356 "((ptr[0] == kCurrentStackFrameMagic)) != (0)" (0x0, 0x0)
    #0 0x7fbf45f39d0b  (/lib64/libasan.so.6+0xb3d0b)
    #1 0x7fbf45f57d4e  (/lib64/libasan.so.6+0xd1d4e)
    #2 0x7fbf45f3e724  (/lib64/libasan.so.6+0xb8724)
    #3 0x7fbf45eb3e5b  (/lib64/libasan.so.6+0x2de5b)
    #4 0x7fbf45eb51e8  (/lib64/libasan.so.6+0x2f1e8)
    #5 0x7fbf45eb7694  (/lib64/libasan.so.6+0x31694)
    #6 0x7fbf45f39398  (/lib64/libasan.so.6+0xb3398)
    #7 0x7fbf45f3a00b in __asan_report_load8 (/lib64/libasan.so.6+0xb400b)
    #8 0xfe6d52 in bool __gnu_cxx::operator!=<dl_phdr_info*, std::vector<dl_phdr_info, std::allocator<dl_phdr_info> > >(__gnu_cxx::__normal_iterator<dl_phdr_info*, std::vector<dl_phdr_info, std::allocator<dl_phdr_info> > > const&, __gnu_cxx::__normal_iterator<dl_phdr_info*, std::vector<dl_phdr_info, std::allocator<dl_phdr_info> > > const&) /usr/include/c++/10/bits/stl_iterator.h:1116
    #9 0xfe615c in dl_iterate_phdr ../../src/core/exception_hacks.cc:121
    #10 0x7fbf44bd1810 in _Unwind_Find_FDE (/lib64/libgcc_s.so.1+0x13810)
    #11 0x7fbf44bcd897  (/lib64/libgcc_s.so.1+0xf897)
    #12 0x7fbf44bcea5f  (/lib64/libgcc_s.so.1+0x10a5f)
    #13 0x7fbf44bcefd8 in _Unwind_RaiseException (/lib64/libgcc_s.so.1+0x10fd8)
    #14 0xfe6281 in _Unwind_RaiseException ../../src/core/exception_hacks.cc:148
    scylladb#15 0x7fbf457364bb in __cxa_throw (/lib64/libstdc++.so.6+0xaa4bb)
    scylladb#16 0x7fbf45e10a21  (/lib64/libboost_unit_test_framework.so.1.73.0+0x1aa21)
    scylladb#17 0x7fbf45e20fe0 in boost::execution_monitor::execute(boost::function<int ()> const&) (/lib64/libboost_unit_test_framework.so.1.73.0+0x2afe0)
    scylladb#18 0x7fbf45e21094 in boost::execution_monitor::vexecute(boost::function<void ()> const&) (/lib64/libboost_unit_test_framework.so.1.73.0+0x2b094)
    scylladb#19 0x7fbf45e43921 in boost::unit_test::unit_test_monitor_t::execute_and_translate(boost::function<void ()> const&, unsigned long) (/lib64/libboost_unit_test_framework.so.1.73.0+0x4d921)
    scylladb#20 0x7fbf45e5eae1  (/lib64/libboost_unit_test_framework.so.1.73.0+0x68ae1)
    scylladb#21 0x7fbf45e5ed31  (/lib64/libboost_unit_test_framework.so.1.73.0+0x68d31)
    scylladb#22 0x7fbf45e2e547 in boost::unit_test::framework::run(unsigned long, bool) (/lib64/libboost_unit_test_framework.so.1.73.0+0x38547)
    scylladb#23 0x7fbf45e43618 in boost::unit_test::unit_test_main(bool (*)(), int, char**) (/lib64/libboost_unit_test_framework.so.1.73.0+0x4d618)
    scylladb#24 0x44798d in seastar::testing::entry_point(int, char**) ../../src/testing/entry_point.cc:77
    scylladb#25 0x4134b5 in main ../../include/seastar/testing/seastar_test.hh:65
    scylladb#26 0x7fbf44a1b1e1 in __libc_start_main (/lib64/libc.so.6+0x281e1)
    scylladb#27 0x4133dd in _start (/home/bhalevy/dev/seastar/build/debug/tests/unit/closeable_test+0x4133dd)
```

Signed-off-by: Benny Halevy <[email protected]>
Message-Id: <[email protected]>
Matan-B pushed a commit that referenced this pull request Jul 7, 2024
in main(), we creates an instance of `http_server_control` using
new, but we never destroy it. this is identified by ASan

```
==2190125==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 8 byte(s) in 1 object(s) allocated from:
    #0 0x55e21cf487bd in operator new(unsigned long) /home/kefu/dev/llvm-project/compiler-rt/lib/asan/asan_new_delete.cpp:86:3
    #1 0x55e21cf6cf31 in main::$_0::operator()() const::'lambda'()::operator()() const /home/kefu/dev/seastar/apps/httpd/main.cc:121:27
    #2 0x55e21cf6b4cc in int std::__invoke_impl<int, main::$_0::operator()() const::'lambda'()>(std::__invoke_other, main::$_0::operator()() const::'lambda'()&&) /usr/lib/gcc/x86_64-redhat-linux/14/../../../../incl
ude/c++/14/bits/invoke.h:61:14
    #3 0x55e21cf6b46c in std::__invoke_result<main::$_0::operator()() const::'lambda'()>::type std::__invoke<main::$_0::operator()() const::'lambda'()>(main::$_0::operator()() const::'lambda'()&&) /usr/lib/gcc/x86_
64-redhat-linux/14/../../../../include/c++/14/bits/invoke.h:96:14
    #4 0x55e21cf6b410 in decltype(auto) std::__apply_impl<main::$_0::operator()() const::'lambda'(), std::tuple<>>(main::$_0::operator()() const::'lambda'()&&, std::tuple<>&&, std::integer_sequence<unsigned long, .
..>) /usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/tuple:2921:14
    #5 0x55e21cf6b3b2 in decltype(auto) std::apply<main::$_0::operator()() const::'lambda'(), std::tuple<>>(main::$_0::operator()() const::'lambda'()&&, std::tuple<>&&) /usr/lib/gcc/x86_64-redhat-linux/14/../../../
../include/c++/14/tuple:2936:14
    #6 0x55e21cf6b283 in seastar::future<int> seastar::futurize<int>::apply<main::$_0::operator()() const::'lambda'()>(main::$_0::operator()() const::'lambda'()&&, std::tuple<>&&) /home/kefu/dev/seastar/include/sea
star/core/future.hh:2005:28
    #7 0x55e21cf6b043 in seastar::futurize<std::invoke_result<main::$_0::operator()() const::'lambda'()>::type>::type seastar::async<main::$_0::operator()() const::'lambda'()>(seastar::thread_attributes, main::$_0:
:operator()() const::'lambda'()&&)::'lambda'()::operator()() const /home/kefu/dev/seastar/include/seastar/core/thread.hh:260:13
    #8 0x55e21cf6ae74 in seastar::noncopyable_function<void ()>::direct_vtable_for<seastar::futurize<std::invoke_result<main::$_0::operator()() const::'lambda'()>::type>::type seastar::async<main::$_0::operator()()
 const::'lambda'()>(seastar::thread_attributes, main::$_0::operator()() const::'lambda'()&&)::'lambda'()>::call(seastar::noncopyable_function<void ()> const*) /home/kefu/dev/seastar/include/seastar/util/noncopyable
_function.hh:129:20
    #9 0x7f5d757a0fb3 in seastar::noncopyable_function<void ()>::operator()() const /home/kefu/dev/seastar/include/seastar/util/noncopyable_function.hh:215:16
    #10 0x7f5d75ef5611 in seastar::thread_context::main() /home/kefu/dev/seastar/src/core/thread.cc:311:9
    #11 0x7f5d75ef50eb in seastar::thread_context::s_main(int, int) /home/kefu/dev/seastar/src/core/thread.cc:287:43
    #12 0x7f5d72f8a18f  (/lib64/libc.so.6+0x5a18f) (BuildId: b098f1c75a76548bb230d8f551eae07a2aeccf06)
```

so, in this change, let's hold it using a smart pointer, so we
can destroy it when it leaves the lexical scope.

Signed-off-by: Kefu Chai <[email protected]>

Closes scylladb#2224
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants