mirror of
https://github.com/matrix-construct/construct
synced 2024-05-20 03:43:47 +02:00
ircd: Add branchless optimistic assertion instrument --with-assert=opt
This commit is contained in:
parent
e4d517415b
commit
b2d451b74d
|
@ -235,6 +235,10 @@ AM_COND_IF(ASSERT,
|
||||||
RB_DEFINE([ASSERT_INTRINSIC], [1], [Intrinsic assertion behavior])
|
RB_DEFINE([ASSERT_INTRINSIC], [1], [Intrinsic assertion behavior])
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [[ "$assert_type" == "opt" ]]; then
|
||||||
|
RB_DEFINE([ASSERT_OPTIMISTIC], [1], [Optimistic assertion behavior])
|
||||||
|
fi
|
||||||
|
|
||||||
fi
|
fi
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
|
@ -19,13 +19,13 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#define HAVE_IRCD_ASSERT_H
|
#define HAVE_IRCD_ASSERT_H
|
||||||
|
|
||||||
/// This file has to be included prior to the standard library assert headers
|
// This file has to be included prior to the standard library assert headers
|
||||||
#if defined(RB_ASSERT) && defined(_ASSERT_H_DECLS) && defined(RB_DEBUG)
|
#if defined(RB_ASSERT) && defined(_ASSERT_H_DECLS) && defined(RB_DEBUG)
|
||||||
#error "Do not include <assert.h> or <cassert> first."
|
#error "Do not include <assert.h> or <cassert> first."
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// Define an indicator for whether we override standard assert() behavior in
|
// Define an indicator for whether we override standard assert() behavior in
|
||||||
/// this build if the conditions are appropriate.
|
// this build if the conditions are appropriate.
|
||||||
#if defined(RB_ASSERT) && !defined(NDEBUG)
|
#if defined(RB_ASSERT) && !defined(NDEBUG)
|
||||||
#define _ASSERT_H_DECLS
|
#define _ASSERT_H_DECLS
|
||||||
#endif
|
#endif
|
||||||
|
@ -34,57 +34,99 @@
|
||||||
namespace ircd
|
namespace ircd
|
||||||
{
|
{
|
||||||
void debugtrap() noexcept;
|
void debugtrap() noexcept;
|
||||||
template<class expr> void always_assert(expr&&) noexcept;
|
|
||||||
[[gnu::cold]] void print_assertion(const char *, const char *, const unsigned, const char *) noexcept;
|
template<class expr>
|
||||||
|
void always_assert(expr&&) noexcept;
|
||||||
|
|
||||||
|
[[gnu::cold]]
|
||||||
|
void print_assertion(const char *, const char *, const unsigned, const char *) noexcept;
|
||||||
|
|
||||||
|
#if defined(RB_ASSERT_OPTIMISTIC)
|
||||||
|
struct assertion extern assertion;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Override the standard assert behavior to take one of several different
|
// Alternative declaration of __assert_fail() which may return; our definitions
|
||||||
/// actions as defined in our internal assert.cc unit. When trapping assert
|
// take on different behaviors depending on the value of RB_ASSERT.
|
||||||
/// is disabled this path will be used instead.
|
#if defined(RB_ASSERT)
|
||||||
///
|
|
||||||
#if defined(RB_ASSERT) && !defined(RB_ASSERT_INTRINSIC)
|
|
||||||
extern "C" void
|
extern "C" void
|
||||||
__attribute__((visibility("default")))
|
__attribute__((visibility("default")))
|
||||||
__assert_fail(const char *__assertion,
|
__assert_fail(const char *__assertion,
|
||||||
const char *__file,
|
const char *__file,
|
||||||
unsigned int __line,
|
unsigned int __line,
|
||||||
const char *__function);
|
const char *__function) noexcept;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Custom assert
|
// Custom assert
|
||||||
#if defined(RB_ASSERT) && defined(RB_ASSERT_INTRINSIC) && !defined(NDEBUG)
|
#if defined(RB_ASSERT_INTRINSIC) && !defined(NDEBUG)
|
||||||
#undef assert
|
#undef assert
|
||||||
#define assert(expr) \
|
#define assert(expr) \
|
||||||
({ \
|
({ \
|
||||||
if(__builtin_expect(!static_cast<bool>(expr), 0)) \
|
if(__builtin_expect(!static_cast<bool>(expr), 0)) \
|
||||||
__assert_fail(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
__assert_fail(#expr, __FILE__, __LINE__, __FUNCTION__); \
|
||||||
|
})
|
||||||
|
#elif defined(RB_ASSERT_OPTIMISTIC) && !defined(NDEBUG)
|
||||||
|
#undef assert
|
||||||
|
#define assert(expr) \
|
||||||
|
({ \
|
||||||
|
ircd::assertion.ok &= static_cast<bool>(expr); \
|
||||||
})
|
})
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Custom addl cases to avoid any trouble w/ clang
|
// Custom addl cases to avoid any trouble w/ clang
|
||||||
#if defined(RB_ASSERT) && defined(__clang__) && !defined(assert)
|
#if defined(RB_ASSERT) && defined(__clang__) && !defined(assert) && defined(NDEBUG)
|
||||||
#ifdef NDEBUG
|
#define assert(expr) (static_cast<void>(0))
|
||||||
#define assert(expr) (static_cast<void>(0))
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// Override the standard assert behavior, if enabled, to trap into the
|
#if defined(RB_ASSERT_OPTIMISTIC)
|
||||||
/// debugger as close as possible to the offending site.
|
/// State structure for optimistic assertion mode.
|
||||||
///
|
struct ircd::assertion
|
||||||
#if defined(RB_ASSERT) && defined(RB_ASSERT_INTRINSIC)
|
{
|
||||||
|
bool ok alignas(8) {true};
|
||||||
|
const char *__assertion;
|
||||||
|
const char *__file;
|
||||||
|
unsigned int __line;
|
||||||
|
const char *__function;
|
||||||
|
|
||||||
|
bool point();
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(RB_ASSERT_OPTIMISTIC)
|
||||||
|
/// Test-and-reset for pending assertion.
|
||||||
|
[[gnu::hot]]
|
||||||
|
inline bool
|
||||||
|
ircd::assertion::point()
|
||||||
|
{
|
||||||
|
if(__builtin_expect(!ok, 0))
|
||||||
|
{
|
||||||
|
#if defined(__SSE2__) && defined(__clang__)
|
||||||
|
asm volatile ("lfence");
|
||||||
|
#endif
|
||||||
|
__assert_fail(__assertion, __file, __line, __function);
|
||||||
|
ok = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(RB_ASSERT_INTRINSIC)
|
||||||
extern "C" // for clang
|
extern "C" // for clang
|
||||||
{
|
{
|
||||||
|
/// Override the standard assert behavior, if enabled, to trap into the
|
||||||
|
/// debugger as close as possible to the offending site.
|
||||||
extern inline void
|
extern inline void
|
||||||
__attribute__((cold, flatten, always_inline, gnu_inline, artificial))
|
__attribute__((cold, flatten, always_inline, gnu_inline, artificial))
|
||||||
__assert_fail(const char *const __assertion,
|
__assert_fail(const char *const __assertion,
|
||||||
const char *const __file,
|
const char *const __file,
|
||||||
unsigned int __line,
|
unsigned int __line,
|
||||||
const char *const __function)
|
const char *const __function)
|
||||||
|
noexcept
|
||||||
{
|
{
|
||||||
#if defined(__SSE2__) && defined(__clang__)
|
#if defined(__SSE2__) && defined(__clang__)
|
||||||
asm volatile ("lfence");
|
asm volatile ("lfence");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ircd::print_assertion(__assertion, __file, __line, __function);
|
ircd::print_assertion(__assertion, __file, __line, __function);
|
||||||
ircd::debugtrap();
|
ircd::debugtrap();
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,12 @@
|
||||||
|
|
||||||
#include <RB_INC_SIGNAL_H
|
#include <RB_INC_SIGNAL_H
|
||||||
|
|
||||||
|
#if defined(RB_ASSERT_OPTIMISTIC)
|
||||||
|
decltype(ircd::assertion)
|
||||||
|
ircd::assertion
|
||||||
|
alignas(64);
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(RB_ASSERT) && !defined(RB_ASSERT_INTRINSIC)
|
#if defined(RB_ASSERT) && !defined(RB_ASSERT_INTRINSIC)
|
||||||
#ifdef __clang__
|
#ifdef __clang__
|
||||||
#pragma clang diagnostic push
|
#pragma clang diagnostic push
|
||||||
|
@ -21,6 +27,7 @@ __assert_fail(const char *__assertion,
|
||||||
const char *__file,
|
const char *__file,
|
||||||
unsigned int __line,
|
unsigned int __line,
|
||||||
const char *__function)
|
const char *__function)
|
||||||
|
noexcept
|
||||||
{
|
{
|
||||||
ircd::print_assertion(__assertion, __file, __line, __function);
|
ircd::print_assertion(__assertion, __file, __line, __function);
|
||||||
|
|
||||||
|
@ -30,6 +37,9 @@ __assert_fail(const char *__assertion,
|
||||||
if(strcmp(RB_ASSERT, "quit") == 0)
|
if(strcmp(RB_ASSERT, "quit") == 0)
|
||||||
ircd::quit();
|
ircd::quit();
|
||||||
|
|
||||||
|
else if(strcmp(RB_ASSERT, "opt") == 0)
|
||||||
|
ircd::debugtrap();
|
||||||
|
|
||||||
else if(strcmp(RB_ASSERT, "trap") == 0)
|
else if(strcmp(RB_ASSERT, "trap") == 0)
|
||||||
ircd::debugtrap();
|
ircd::debugtrap();
|
||||||
|
|
||||||
|
@ -79,7 +89,7 @@ ircd::print_assertion(const char *const __assertion,
|
||||||
const char *const __function)
|
const char *const __function)
|
||||||
noexcept
|
noexcept
|
||||||
{
|
{
|
||||||
if(strcmp(__assertion, "critical") == 0)
|
if(__assertion && strcmp(__assertion, "critical") == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
fprintf(stderr, "\nassertion failed [%s +%u] %s :%s\n",
|
fprintf(stderr, "\nassertion failed [%s +%u] %s :%s\n",
|
||||||
|
|
|
@ -609,6 +609,11 @@ noexcept
|
||||||
};
|
};
|
||||||
|
|
||||||
assert(handler::current == handler);
|
assert(handler::current == handler);
|
||||||
|
|
||||||
|
#if defined(RB_ASSERT_OPTIMISTIC)
|
||||||
|
ircd::assertion.point();
|
||||||
|
#endif
|
||||||
|
|
||||||
handler::current = nullptr;
|
handler::current = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue