diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a49d2777dad7a6b645e3db01106d698191a0060..7dad8a96700fdbc06bdfabb6ac09bb60f0ded200 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -488,6 +488,47 @@ find_package(ICONV) set(ICU_FIND_REQUIRED ON) find_package(ICU) +# +# libunwind +# + +if(ENABLE_BACKTRACE) + if(NOT HAVE_FORCE_ALIGN_ARG_POINTER_ATTR) + message(SEND_ERROR "backtrace feature requires \ + `force_align_arg_pointer` function attribute \ + support from C/C++ compiler") + endif() + + if(NOT HAVE_CFI_ASM) + message(SEND_ERROR "backtrace feature requires CFI assembly support \ + from C/C++ compiler") + endif() + + if(ENABLE_BUNDLED_LIBUNWIND) + if(APPLE) + message(SEND_ERROR "libunwind does not support macOS") + endif() + if(NOT HAVE_STDATOMIC_H) + message(SEND_ERROR "building bundled libunwind requires stdatomic.h \ + support from C compiler") + endif() + + include(BuildLibUnwind) + libunwind_build() + add_dependencies(build_bundled_libs + bundled-libunwind + bundled-libunwind-platform) + else() + find_package(LibUnwind MODULE REQUIRED) + endif() + if(NOT APPLE AND NOT ENABLE_BUNDLED_LIBUNWIND AND + LIBUNWIND_VERSION VERSION_LESS "1.3" AND + ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64") + message(SEND_ERROR "versions of libunwind earlier than 1.3.0 are \ + broken on AARCH64 architecture") + endif() +endif() + # # LuaJIT # @@ -495,6 +536,11 @@ find_package(ICU) # set(ENABLE_BUNDLED_LUAJIT ON) set(LUAJIT_ENABLE_GC64_DEFAULT OFF) + +if(NOT ENABLE_BACKTRACE) + set(LUAJIT_DISABLE_SYSPROF ON) + message(STATUS "Sysprof is not available without backtrace.") +endif() if (TARGET_OS_DARWIN) # LuaJIT is unusable on OS X without enabled GC64 # See https://github.com/tarantool/tarantool/issues/2643 @@ -599,47 +645,6 @@ include(BuildMisc) libmisc_build() add_dependencies(build_bundled_libs misc) -# -# libunwind -# - -if(ENABLE_BACKTRACE) - if(NOT HAVE_FORCE_ALIGN_ARG_POINTER_ATTR) - message(SEND_ERROR "backtrace feature requires \ - `force_align_arg_pointer` function attribute \ - support from C/C++ compiler") - endif() - - if(NOT HAVE_CFI_ASM) - message(SEND_ERROR "backtrace feature requires CFI assembly support \ - from C/C++ compiler") - endif() - - if(ENABLE_BUNDLED_LIBUNWIND) - if(APPLE) - message(SEND_ERROR "libunwind does not support macOS") - endif() - if(NOT HAVE_STDATOMIC_H) - message(SEND_ERROR "building bundled libunwind requires stdatomic.h \ - support from C compiler") - endif() - - include(BuildLibUnwind) - libunwind_build() - add_dependencies(build_bundled_libs - bundled-libunwind - bundled-libunwind-platform) - else() - find_package(LibUnwind MODULE REQUIRED) - endif() - if(NOT APPLE AND NOT ENABLE_BUNDLED_LIBUNWIND AND - LIBUNWIND_VERSION VERSION_LESS "1.3" AND - ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64") - message(SEND_ERROR "versions of libunwind earlier than 1.3.0 are \ - broken on AARCH64 architecture") - endif() -endif() - # cpack config. called package.cmake to avoid # conflicts with the global CPack.cmake (On MacOS X # file names are case-insensitive) diff --git a/extra/exports b/extra/exports index 9d4a9f2e6e9b4eea895d76d0d5b4e56f670475f4..9dc404f01087a8fc1621bf1e861f57486dbc42fa 100644 --- a/extra/exports +++ b/extra/exports @@ -258,8 +258,10 @@ luaL_typerror luaL_unref luaL_where luaM_metrics -luaM_sysprof_configure luaM_sysprof_report +luaM_sysprof_set_backtracer +luaM_sysprof_set_on_stop +luaM_sysprof_set_writer luaM_sysprof_start luaM_sysprof_stop luaopen_base diff --git a/src/lua/init.c b/src/lua/init.c index 4af180e2c40111dbb0f13783caa776c55d872795..8160e024104dcdc0dd2ce7a0217db8ed4fdfc93d 100644 --- a/src/lua/init.c +++ b/src/lua/init.c @@ -39,6 +39,7 @@ #include <lauxlib.h> #include <lualib.h> #include <lj_cdata.h> +#include <lmisclib.h> #include <luajit.h> #include <fiber.h> @@ -67,6 +68,11 @@ #include "lua/uri.h" #include "digest.h" #include "errinj.h" + +#ifdef ENABLE_BACKTRACE +#include "core/backtrace.h" +#endif + #include <small/ibuf.h> #include <ctype.h> @@ -534,6 +540,23 @@ skip: base = (base == -1 ? 10 : base); /* }}} */ +#ifdef ENABLE_BACKTRACE +/** + * Backtracing function for sysprof. + **/ +static void +fiber_backtracer(void *(*frame_writer)(int frame_no, void *addr)) +{ + struct backtrace bt = {}; + int frame_no; + const struct fiber *cur = fiber_self(); + backtrace_collect(&bt, cur, 0); + for (frame_no = 0; frame_no < bt.frame_count; ++frame_no) { + frame_writer(frame_no, bt.frames[frame_no].ip); + } +} +#endif + /** * Original LuaJIT/Lua logic: <luajit/src/lib_package.c - function setpath> * @@ -712,6 +735,9 @@ tarantool_lua_init(const char *tarantool_bin, int argc, char **argv) tarantool_lua_serializer_init(L); tarantool_lua_swim_init(L); tarantool_lua_decimal_init(L); +#ifdef ENABLE_BACKTRACE + luaM_sysprof_set_backtracer(fiber_backtracer); +#endif luaopen_http_client_driver(L); lua_pop(L, 1); luaopen_msgpack(L); diff --git a/third_party/luajit b/third_party/luajit index 71e7020637bb15024ceb7e93ce66b61870567339..359bfe09d71869064e619c4affd749127ad5218f 160000 --- a/third_party/luajit +++ b/third_party/luajit @@ -1 +1 @@ -Subproject commit 71e7020637bb15024ceb7e93ce66b61870567339 +Subproject commit 359bfe09d71869064e619c4affd749127ad5218f