From f64e8dc439624d018690630ba3b8e8158fa9992b Mon Sep 17 00:00:00 2001 From: lizhuoyu5 <49020515+lzydmxy@users.noreply.github.com> Date: Fri, 27 Sep 2024 16:48:50 +0800 Subject: [PATCH] Use llvm project (#361) * Add submodule llvm-project * Build libcxx and libcxxabi from llvm-project * Remove submodule libcxx and libcxxabi * Use llvm-17 from github workflows * Check if the desired LLVM version is already installed for github workflows * Fix llvm download script * Fix llvm download script * Fix cmake error when find libcxx * Fix build failed on MacOS * Fix misaligned address when cast 4lw code str to Int32 * Replace __builtin_bswap* with std::byteswap * Add toString for all enum types * Update build docs * Check Clang version be at least 17 --- .github/workflows/build.yml | 2 +- .github/workflows/prepare-env.sh | 17 +-- .gitmodules | 9 +- README.md | 2 +- base/common/logger_useful.h | 7 +- base/readpassphrase/CMakeLists.txt | 2 +- cmake/darwin/default_libs.cmake | 1 + cmake/find/cxx.cmake | 23 ++-- cmake/tools.cmake | 2 +- contrib/boost-cmake/CMakeLists.txt | 1 + contrib/fmtlib | 2 +- contrib/fmtlib-cmake/CMakeLists.txt | 3 +- contrib/libcxx | 1 - contrib/libcxx-cmake/CMakeLists.txt | 122 ++++++++++------------ contrib/libcxxabi | 1 - contrib/libcxxabi-cmake/CMakeLists.txt | 17 +-- contrib/libunwind | 2 +- contrib/libunwind-cmake/CMakeLists.txt | 78 +++++--------- contrib/libunwind-cmake/unwind-override.c | 4 + contrib/llvm-project | 1 + docs/how-to-build.md | 4 +- src/Common/Epoll.h | 3 +- src/Common/Exception.cpp | 37 ++----- src/Common/Exception.h | 37 +++---- src/Common/IO/ReadHelpers.cpp | 4 +- src/Common/IO/ReadHelpers.h | 12 +-- src/Common/IO/WriteHelpers.h | 8 +- src/Common/StackTrace.cpp | 14 ++- src/Common/StringUtils.h | 2 +- src/Common/ThreadPool.h | 2 +- src/Common/ThreadProfileEvents.h | 1 + src/Common/formatReadable.h | 24 +---- src/Service/FourLetterCommand.cpp | 7 +- src/Service/KeeperCommon.cpp | 2 +- src/Service/KeeperCommon.h | 59 ++++++++++- src/Service/KeeperStore.cpp | 9 +- src/Service/NuRaftLogSegment.cpp | 13 +-- src/Service/NuRaftLogSnapshot.cpp | 4 +- src/Service/NuRaftStateMachine.cpp | 10 +- src/ZooKeeper/ZooKeeper.cpp | 1 + src/ZooKeeper/ZooKeeperCommon.cpp | 4 +- src/ZooKeeper/ZooKeeperIO.cpp | 20 ++-- 42 files changed, 283 insertions(+), 291 deletions(-) delete mode 160000 contrib/libcxx delete mode 160000 contrib/libcxxabi create mode 160000 contrib/llvm-project diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9c0e47d9e4f..025170e0969 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,7 +19,7 @@ jobs: submodules: recursive - name: Prepare environment - run: bash .github/workflows/prepare-env.sh 14 + run: bash .github/workflows/prepare-env.sh 17 - name: Generate Makefile run: | diff --git a/.github/workflows/prepare-env.sh b/.github/workflows/prepare-env.sh index aa5248d7b6e..9fa413e04cf 100644 --- a/.github/workflows/prepare-env.sh +++ b/.github/workflows/prepare-env.sh @@ -5,15 +5,18 @@ llvm_priority=300 sudo apt install -y ninja-build ccache -# install llvm -#wget https://apt.llvm.org/llvm.sh -#chmod 755 llvm.sh && sudo ./llvm.sh ${llvm_version} all +# Check if the desired LLVM version is already installed +if ! llvm-config-"${llvm_version}" --version &>/dev/null; then + sudo curl -sL https://apt.llvm.org/llvm.sh | sudo bash -s -- "${llvm_version}" all +else + echo "LLVM ${llvm_version} is already installed." +fi # make clang as default compiler -sudo update-alternatives --install /usr/bin/llvm-config llvm-config /usr/bin/llvm-config-${llvm_version} ${llvm_priority} -sudo update-alternatives --install /usr/bin/llvm-symbolizer llvm-symbolizer /usr/bin/llvm-symbolizer-${llvm_version} ${llvm_priority} -sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-${llvm_version} ${llvm_priority} -sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-${llvm_version} ${llvm_priority} +sudo update-alternatives --install /usr/bin/llvm-config llvm-config /usr/bin/llvm-config-"${llvm_version}" ${llvm_priority} +sudo update-alternatives --install /usr/bin/llvm-symbolizer llvm-symbolizer /usr/bin/llvm-symbolizer-"${llvm_version}" ${llvm_priority} +sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-"${llvm_version}" ${llvm_priority} +sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-"${llvm_version}" ${llvm_priority} clang-${llvm_version} -v diff --git a/.gitmodules b/.gitmodules index 2367a29d58c..8f4be4e1319 100644 --- a/.gitmodules +++ b/.gitmodules @@ -22,12 +22,6 @@ [submodule "contrib/libxml2"] path = contrib/libxml2 url = https://github.com/GNOME/libxml2.git -[submodule "contrib/libcxx"] - path = contrib/libcxx - url = https://github.com/ClickHouse-Extras/libcxx.git -[submodule "contrib/libcxxabi"] - path = contrib/libcxxabi - url = https://github.com/ClickHouse-Extras/libcxxabi.git [submodule "contrib/libunwind"] path = contrib/libunwind url = https://github.com/ClickHouse-Extras/libunwind.git @@ -43,3 +37,6 @@ [submodule "contrib/libc-headers"] path = contrib/libc-headers url = https://github.com/ClickHouse-Extras/libc-headers.git +[submodule "contrib/llvm-project"] + path = contrib/llvm-project + url = https://github.com/llvm/llvm-project.git diff --git a/README.md b/README.md index 4b91267bcd6..d9d28ed148c 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ We really appreciate the excellent work of the ClickHouse and NuRaft teams. ### Build on Ubuntu -Requirement: Ubuntu 20.04+, Clang 13+(17 is recommended), Cmake 3.20+ +Requirement: Ubuntu 20.04+, Clang 17+, Cmake 3.20+ ``` # install tools sudo apt-get install cmake llvm-17 diff --git a/base/common/logger_useful.h b/base/common/logger_useful.h index 7a1349d5590..5e1e83e0f25 100644 --- a/base/common/logger_useful.h +++ b/base/common/logger_useful.h @@ -13,6 +13,11 @@ namespace template constexpr auto firstArg(T && x, Ts &&...) { return std::forward(x); } } +template +std::string logFormat(const std::string& fmt, Args&&... args) { + std::string formatted_string = fmt::format(fmt::runtime(std::string_view(fmt.data(), fmt.size())), std::forward(args)...); + return formatted_string; +} /// Logs a message to a specified logger with that level. /// If more than one argument is provided, @@ -24,7 +29,7 @@ namespace { \ if ((logger)->is((PRIORITY))) \ { \ - std::string formatted_message = numArgs(__VA_ARGS__) > 1 ? fmt::format(__VA_ARGS__) : firstArg(__VA_ARGS__); \ + std::string formatted_message = numArgs(__VA_ARGS__) > 1 ? logFormat(__VA_ARGS__) : firstArg(__VA_ARGS__); \ if (auto channel = (logger)->getChannel()) \ { \ std::string file_function; \ diff --git a/base/readpassphrase/CMakeLists.txt b/base/readpassphrase/CMakeLists.txt index 986bb85455f..786ed03842f 100644 --- a/base/readpassphrase/CMakeLists.txt +++ b/base/readpassphrase/CMakeLists.txt @@ -4,5 +4,5 @@ add_library(readpassphrase readpassphrase.c) set_target_properties(readpassphrase PROPERTIES LINKER_LANGUAGE C) -target_compile_options(readpassphrase PRIVATE -Wno-unused-result -Wno-reserved-id-macro -Wno-reserved-identifier -Wno-disabled-macro-expansion -Wno-sign-conversion) +target_compile_options(readpassphrase PRIVATE -Wno-unused-result -Wno-reserved-id-macro -Wno-reserved-identifier -Wno-disabled-macro-expansion -Wno-sign-conversion -Wno-switch-default) target_include_directories(readpassphrase PUBLIC .) diff --git a/cmake/darwin/default_libs.cmake b/cmake/darwin/default_libs.cmake index 1d337466f32..2a499e8e186 100644 --- a/cmake/darwin/default_libs.cmake +++ b/cmake/darwin/default_libs.cmake @@ -15,6 +15,7 @@ set(CMAKE_OSX_DEPLOYMENT_TARGET 10.15) set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) +include (cmake/find/unwind.cmake) include (cmake/find/cxx.cmake) add_library(global-group INTERFACE) diff --git a/cmake/find/cxx.cmake b/cmake/find/cxx.cmake index 826b81848da..281267bc43d 100644 --- a/cmake/find/cxx.cmake +++ b/cmake/find/cxx.cmake @@ -15,9 +15,9 @@ set(USE_INTERNAL_LIBCXX_LIBRARY_DEFAULT ON) option (USE_INTERNAL_LIBCXX_LIBRARY "Disable to use system libcxx and libcxxabi libraries instead of bundled" ${USE_INTERNAL_LIBCXX_LIBRARY_DEFAULT}) -if(NOT EXISTS "${RaftKeeper_SOURCE_DIR}/contrib/libcxx/CMakeLists.txt") +if(NOT EXISTS "${RaftKeeper_SOURCE_DIR}/contrib/llvm-project/libcxx") if (USE_INTERNAL_LIBCXX_LIBRARY) - message(WARNING "submodule contrib/libcxx is missing. to fix try run: \n git submodule update --init --recursive") + message(WARNING "submodule contrib/llvm-project is missing. to fix try run: \n git submodule update --init --recursive") message (${RECONFIGURE_MESSAGE_LEVEL} "Can't find internal libcxx") set(USE_INTERNAL_LIBCXX_LIBRARY 0) endif() @@ -29,7 +29,6 @@ set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_LIBCPP_DEBUG=0") # More if (NOT USE_INTERNAL_LIBCXX_LIBRARY) find_library (LIBCXX_LIBRARY c++) - find_library (LIBCXXFS_LIBRARY c++fs) find_library (LIBCXXABI_LIBRARY c++abi) if(LIBCXX_LIBRARY AND LIBCXXABI_LIBRARY) # c++fs is now a part of the libc++ @@ -38,16 +37,23 @@ if (NOT USE_INTERNAL_LIBCXX_LIBRARY) message (${RECONFIGURE_MESSAGE_LEVEL} "Can't find system libcxx") endif() - if(NOT LIBCXXFS_LIBRARY) - set(LIBCXXFS_LIBRARY ${LIBCXX_LIBRARY}) - endif() - set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") target_link_libraries(global-libs INTERFACE ${EXCEPTION_HANDLING_LIBRARY}) endif () if (NOT HAVE_LIBCXX AND NOT MISSING_INTERNAL_LIBCXX_LIBRARY) + set(LIBCXX_SOURCE_DIR "${RaftKeeper_SOURCE_DIR}/contrib/llvm-project/libcxx") + + set(_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS ON) + set(_LIBCPP_PSTL_CPU_BACKEND_SERIAL ON) + set(_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION ON) + + configure_file("${LIBCXX_SOURCE_DIR}/include/__config_site.in" "${CMAKE_CURRENT_BINARY_DIR}/include/__config_site" @ONLY) + set(CONFIG_SITE_DIR "${CMAKE_CURRENT_BINARY_DIR}/include") + message("CONFIG_SITE_DIR ${CONFIG_SITE_DIR}") + include_directories(${CONFIG_SITE_DIR}) + set (LIBCXX_LIBRARY cxx) set (LIBCXXABI_LIBRARY cxxabi) add_subdirectory(contrib/libcxxabi-cmake) @@ -60,10 +66,9 @@ if (NOT HAVE_LIBCXX AND NOT MISSING_INTERNAL_LIBCXX_LIBRARY) endif () if (HAVE_LIBCXX) - target_link_libraries(global-libs INTERFACE ${LIBCXX_LIBRARY} ${LIBCXXABI_LIBRARY} ${LIBCXXFS_LIBRARY}) + target_link_libraries(global-libs INTERFACE ${LIBCXX_LIBRARY} ${LIBCXXABI_LIBRARY}) message (STATUS "Using libcxx: ${LIBCXX_LIBRARY}") - message (STATUS "Using libcxxfs: ${LIBCXXFS_LIBRARY}") message (STATUS "Using libcxxabi: ${LIBCXXABI_LIBRARY}") else() target_link_libraries(global-libs INTERFACE -l:libstdc++.a -l:libstdc++fs.a) # Always link these libraries as static diff --git a/cmake/tools.cmake b/cmake/tools.cmake index d6ced1147e9..4535e822190 100644 --- a/cmake/tools.cmake +++ b/cmake/tools.cmake @@ -7,7 +7,7 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") endif () if (COMPILER_CLANG) - set (CLANG_MINIMUM_VERSION 13) + set (CLANG_MINIMUM_VERSION 17) if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${CLANG_MINIMUM_VERSION}) message (FATAL_ERROR "Clang version must be at least ${CLANG_MINIMUM_VERSION}.") endif () diff --git a/contrib/boost-cmake/CMakeLists.txt b/contrib/boost-cmake/CMakeLists.txt index 3445e0a3f86..ba77cebd812 100644 --- a/contrib/boost-cmake/CMakeLists.txt +++ b/contrib/boost-cmake/CMakeLists.txt @@ -116,6 +116,7 @@ if (NOT EXTERNAL_BOOST_FOUND) ) add_library (_boost_program_options ${SRCS_PROGRAM_OPTIONS}) + target_compile_definitions (_boost_program_options PUBLIC BOOST_NO_CXX98_FUNCTION_BASE=1) add_library (boost::program_options ALIAS _boost_program_options) target_include_directories (_boost_program_options SYSTEM BEFORE PUBLIC ${LIBRARY_DIR}) diff --git a/contrib/fmtlib b/contrib/fmtlib index c108ee1d590..ed8f8be70d8 160000 --- a/contrib/fmtlib +++ b/contrib/fmtlib @@ -1 +1 @@ -Subproject commit c108ee1d590089ccf642fc85652b845924067af2 +Subproject commit ed8f8be70d82b0a081286e262867956e565c1bf6 diff --git a/contrib/fmtlib-cmake/CMakeLists.txt b/contrib/fmtlib-cmake/CMakeLists.txt index f3bf73d7dbc..5b856cbc1c1 100644 --- a/contrib/fmtlib-cmake/CMakeLists.txt +++ b/contrib/fmtlib-cmake/CMakeLists.txt @@ -1,4 +1,5 @@ set (SRCS + ../fmtlib/src/fmt.cc ../fmtlib/src/format.cc ../fmtlib/src/os.cc @@ -8,10 +9,8 @@ set (SRCS ../fmtlib/include/fmt/core.h ../fmtlib/include/fmt/format.h ../fmtlib/include/fmt/format-inl.h - ../fmtlib/include/fmt/locale.h ../fmtlib/include/fmt/os.h ../fmtlib/include/fmt/ostream.h - ../fmtlib/include/fmt/posix.h ../fmtlib/include/fmt/printf.h ../fmtlib/include/fmt/ranges.h ) diff --git a/contrib/libcxx b/contrib/libcxx deleted file mode 160000 index 9a457ab3c64..00000000000 --- a/contrib/libcxx +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 9a457ab3c64a533a06922b386b284215c17ce627 diff --git a/contrib/libcxx-cmake/CMakeLists.txt b/contrib/libcxx-cmake/CMakeLists.txt index 3741e5f0192..1eb981e4162 100644 --- a/contrib/libcxx-cmake/CMakeLists.txt +++ b/contrib/libcxx-cmake/CMakeLists.txt @@ -1,79 +1,74 @@ -include(CheckCXXCompilerFlag) - -set(LIBCXX_SOURCE_DIR ${RaftKeeper_SOURCE_DIR}/contrib/libcxx) +set(LIBCXX_SOURCE_DIR "${RaftKeeper_SOURCE_DIR}/contrib/llvm-project/libcxx") +message("${RaftKeeper_SOURCE_DIR}/contrib/llvm-project/libcxx") set(SRCS -"${LIBCXX_SOURCE_DIR}/src/algorithm.cpp" -"${LIBCXX_SOURCE_DIR}/src/any.cpp" -"${LIBCXX_SOURCE_DIR}/src/atomic.cpp" -"${LIBCXX_SOURCE_DIR}/src/barrier.cpp" -"${LIBCXX_SOURCE_DIR}/src/bind.cpp" -"${LIBCXX_SOURCE_DIR}/src/charconv.cpp" -"${LIBCXX_SOURCE_DIR}/src/chrono.cpp" -"${LIBCXX_SOURCE_DIR}/src/condition_variable.cpp" -"${LIBCXX_SOURCE_DIR}/src/condition_variable_destructor.cpp" -"${LIBCXX_SOURCE_DIR}/src/debug.cpp" -"${LIBCXX_SOURCE_DIR}/src/exception.cpp" -"${LIBCXX_SOURCE_DIR}/src/experimental/memory_resource.cpp" -"${LIBCXX_SOURCE_DIR}/src/filesystem/directory_iterator.cpp" -"${LIBCXX_SOURCE_DIR}/src/filesystem/int128_builtins.cpp" -"${LIBCXX_SOURCE_DIR}/src/filesystem/operations.cpp" -"${LIBCXX_SOURCE_DIR}/src/format.cpp" -"${LIBCXX_SOURCE_DIR}/src/functional.cpp" -"${LIBCXX_SOURCE_DIR}/src/future.cpp" -"${LIBCXX_SOURCE_DIR}/src/hash.cpp" -"${LIBCXX_SOURCE_DIR}/src/ios.cpp" -"${LIBCXX_SOURCE_DIR}/src/ios.instantiations.cpp" -"${LIBCXX_SOURCE_DIR}/src/iostream.cpp" -"${LIBCXX_SOURCE_DIR}/src/legacy_debug_handler.cpp" -"${LIBCXX_SOURCE_DIR}/src/legacy_pointer_safety.cpp" -"${LIBCXX_SOURCE_DIR}/src/locale.cpp" -"${LIBCXX_SOURCE_DIR}/src/memory.cpp" -"${LIBCXX_SOURCE_DIR}/src/mutex.cpp" -"${LIBCXX_SOURCE_DIR}/src/mutex_destructor.cpp" -"${LIBCXX_SOURCE_DIR}/src/new.cpp" -"${LIBCXX_SOURCE_DIR}/src/optional.cpp" -"${LIBCXX_SOURCE_DIR}/src/random.cpp" -"${LIBCXX_SOURCE_DIR}/src/random_shuffle.cpp" -"${LIBCXX_SOURCE_DIR}/src/regex.cpp" -"${LIBCXX_SOURCE_DIR}/src/ryu/d2fixed.cpp" -"${LIBCXX_SOURCE_DIR}/src/ryu/d2s.cpp" -"${LIBCXX_SOURCE_DIR}/src/ryu/f2s.cpp" -"${LIBCXX_SOURCE_DIR}/src/shared_mutex.cpp" -"${LIBCXX_SOURCE_DIR}/src/stdexcept.cpp" -"${LIBCXX_SOURCE_DIR}/src/string.cpp" -"${LIBCXX_SOURCE_DIR}/src/strstream.cpp" -"${LIBCXX_SOURCE_DIR}/src/system_error.cpp" -"${LIBCXX_SOURCE_DIR}/src/thread.cpp" -"${LIBCXX_SOURCE_DIR}/src/typeinfo.cpp" -"${LIBCXX_SOURCE_DIR}/src/utility.cpp" -"${LIBCXX_SOURCE_DIR}/src/valarray.cpp" -"${LIBCXX_SOURCE_DIR}/src/variant.cpp" -"${LIBCXX_SOURCE_DIR}/src/vector.cpp" -"${LIBCXX_SOURCE_DIR}/src/verbose_abort.cpp" + "${LIBCXX_SOURCE_DIR}/src/algorithm.cpp" + "${LIBCXX_SOURCE_DIR}/src/any.cpp" + "${LIBCXX_SOURCE_DIR}/src/atomic.cpp" + "${LIBCXX_SOURCE_DIR}/src/barrier.cpp" + "${LIBCXX_SOURCE_DIR}/src/bind.cpp" + "${LIBCXX_SOURCE_DIR}/src/charconv.cpp" + "${LIBCXX_SOURCE_DIR}/src/chrono.cpp" + "${LIBCXX_SOURCE_DIR}/src/condition_variable.cpp" + "${LIBCXX_SOURCE_DIR}/src/condition_variable_destructor.cpp" + # "${LIBCXX_SOURCE_DIR}/src/debug.cpp" + "${LIBCXX_SOURCE_DIR}/src/exception.cpp" + "${LIBCXX_SOURCE_DIR}/src/experimental/memory_resource.cpp" + "${LIBCXX_SOURCE_DIR}/src/filesystem/directory_iterator.cpp" + "${LIBCXX_SOURCE_DIR}/src/filesystem/int128_builtins.cpp" + "${LIBCXX_SOURCE_DIR}/src/filesystem/operations.cpp" + "${LIBCXX_SOURCE_DIR}/src/filesystem/path.cpp" + "${LIBCXX_SOURCE_DIR}/src/filesystem/filesystem_error.cpp" + # "${LIBCXX_SOURCE_DIR}/src/format.cpp" + "${LIBCXX_SOURCE_DIR}/src/functional.cpp" + "${LIBCXX_SOURCE_DIR}/src/future.cpp" + "${LIBCXX_SOURCE_DIR}/src/hash.cpp" + "${LIBCXX_SOURCE_DIR}/src/ios.cpp" + "${LIBCXX_SOURCE_DIR}/src/ios.instantiations.cpp" + "${LIBCXX_SOURCE_DIR}/src/iostream.cpp" + "${LIBCXX_SOURCE_DIR}/src/legacy_debug_handler.cpp" + "${LIBCXX_SOURCE_DIR}/src/legacy_pointer_safety.cpp" + "${LIBCXX_SOURCE_DIR}/src/locale.cpp" + "${LIBCXX_SOURCE_DIR}/src/memory.cpp" + "${LIBCXX_SOURCE_DIR}/src/mutex.cpp" + "${LIBCXX_SOURCE_DIR}/src/mutex_destructor.cpp" + "${LIBCXX_SOURCE_DIR}/src/new.cpp" + "${LIBCXX_SOURCE_DIR}/src/optional.cpp" + "${LIBCXX_SOURCE_DIR}/src/random.cpp" + "${LIBCXX_SOURCE_DIR}/src/random_shuffle.cpp" + "${LIBCXX_SOURCE_DIR}/src/ryu/d2fixed.cpp" + "${LIBCXX_SOURCE_DIR}/src/ryu/d2s.cpp" + "${LIBCXX_SOURCE_DIR}/src/ryu/f2s.cpp" + "${LIBCXX_SOURCE_DIR}/src/shared_mutex.cpp" + "${LIBCXX_SOURCE_DIR}/src/stdexcept.cpp" + "${LIBCXX_SOURCE_DIR}/src/string.cpp" + "${LIBCXX_SOURCE_DIR}/src/strstream.cpp" + "${LIBCXX_SOURCE_DIR}/src/system_error.cpp" + "${LIBCXX_SOURCE_DIR}/src/thread.cpp" + "${LIBCXX_SOURCE_DIR}/src/typeinfo.cpp" + # "${LIBCXX_SOURCE_DIR}/src/utility.cpp" + "${LIBCXX_SOURCE_DIR}/src/valarray.cpp" + "${LIBCXX_SOURCE_DIR}/src/variant.cpp" + "${LIBCXX_SOURCE_DIR}/src/vector.cpp" + "${LIBCXX_SOURCE_DIR}/src/verbose_abort.cpp" + "${LIBCXX_SOURCE_DIR}/src/stdexcept.cpp" + "${LIBCXX_SOURCE_DIR}/src/new_helpers.cpp" ) add_library(cxx ${SRCS}) set_target_properties(cxx PROPERTIES FOLDER "contrib/libcxx-cmake") target_include_directories(cxx SYSTEM BEFORE PRIVATE $) -target_include_directories(cxx SYSTEM BEFORE PUBLIC $) +target_include_directories(cxx SYSTEM BEFORE PUBLIC $<$:$>) target_compile_definitions(cxx PRIVATE -D_LIBCPP_BUILDING_LIBRARY -DLIBCXX_BUILDING_LIBCXXABI) # Enable capturing stack traces for all exceptions. -if (USE_UNWIND) - target_compile_definitions(cxx PUBLIC -DSTD_EXCEPTION_HAS_STACK_TRACE=1) -endif () +#target_compile_definitions(cxx PUBLIC -DSTD_EXCEPTION_HAS_STACK_TRACE=0) if (USE_MUSL) target_compile_definitions(cxx PUBLIC -D_LIBCPP_HAS_MUSL_LIBC=1) endif () -# Override the deduced attribute support that causes error. -if (OS_DARWIN AND COMPILER_GCC) - add_compile_definitions(_LIBCPP_INIT_PRIORITY_MAX) -endif () - target_compile_options(cxx PUBLIC $<$:-nostdinc++>) # Third party library may have substandard code. @@ -84,15 +79,10 @@ target_compile_definitions(cxx PUBLIC -D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS target_link_libraries(cxx PUBLIC cxxabi) -# For __udivmodti4, __divmodti4. -if (OS_DARWIN AND COMPILER_GCC) - target_link_libraries(cxx PRIVATE gcc) -endif () - install( TARGETS cxx EXPORT global ARCHIVE DESTINATION lib RUNTIME DESTINATION lib LIBRARY DESTINATION lib -) \ No newline at end of file +) diff --git a/contrib/libcxxabi b/contrib/libcxxabi deleted file mode 160000 index 65ba93a0ace..00000000000 --- a/contrib/libcxxabi +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 65ba93a0ace1abd7deced33b8c907f27714ce350 diff --git a/contrib/libcxxabi-cmake/CMakeLists.txt b/contrib/libcxxabi-cmake/CMakeLists.txt index 21baf2cbb34..bf869c35457 100644 --- a/contrib/libcxxabi-cmake/CMakeLists.txt +++ b/contrib/libcxxabi-cmake/CMakeLists.txt @@ -1,4 +1,5 @@ -set(LIBCXXABI_SOURCE_DIR ${RaftKeeper_SOURCE_DIR}/contrib/libcxxabi) +set(LIBCXXABI_SOURCE_DIR "${RaftKeeper_SOURCE_DIR}/contrib/llvm-project/libcxxabi") +message("${RaftKeeper_SOURCE_DIR}/contrib/llvm-project/libcxxabi") set(SRCS "${LIBCXXABI_SOURCE_DIR}/src/abort_message.cpp" @@ -9,7 +10,7 @@ set(SRCS "${LIBCXXABI_SOURCE_DIR}/src/cxa_exception_storage.cpp" "${LIBCXXABI_SOURCE_DIR}/src/cxa_guard.cpp" "${LIBCXXABI_SOURCE_DIR}/src/cxa_handlers.cpp" - # "${LIBCXXABI_SOURCE_DIR}/src/cxa_noexception.cpp" + "${LIBCXXABI_SOURCE_DIR}/src/cxa_noexception.cpp" "${LIBCXXABI_SOURCE_DIR}/src/cxa_personality.cpp" "${LIBCXXABI_SOURCE_DIR}/src/cxa_thread_atexit.cpp" "${LIBCXXABI_SOURCE_DIR}/src/cxa_vector.cpp" @@ -20,6 +21,8 @@ set(SRCS "${LIBCXXABI_SOURCE_DIR}/src/stdlib_new_delete.cpp" "${LIBCXXABI_SOURCE_DIR}/src/stdlib_stdexcept.cpp" "${LIBCXXABI_SOURCE_DIR}/src/stdlib_typeinfo.cpp" + "${LIBCXXABI_SOURCE_DIR}/src/stdlib_new_delete.cpp" + "${LIBCXXABI_SOURCE_DIR}/src/fallback_malloc.cpp" ) add_library(cxxabi ${SRCS}) @@ -33,14 +36,12 @@ target_include_directories(cxxabi SYSTEM BEFORE PRIVATE $ PRIVATE $ ) -target_compile_definitions(cxxabi PRIVATE -D_LIBCPP_BUILDING_LIBRARY) +target_compile_definitions(cxxabi PRIVATE -D_LIBCPP_BUILDING_LIBRARY -DHAS_THREAD_LOCAL) target_compile_options(cxxabi PRIVATE -nostdinc++ -fno-sanitize=undefined -Wno-macro-redefined) # If we don't disable UBSan, infinite recursion happens in dynamic_cast. -target_link_libraries(cxxabi PUBLIC ${EXCEPTION_HANDLING_LIBRARY}) +target_link_libraries(cxxabi PUBLIC unwind) # Enable capturing stack traces for all exceptions. -if (USE_UNWIND) - target_compile_definitions(cxxabi PUBLIC -DSTD_EXCEPTION_HAS_STACK_TRACE=1) -endif () +#target_compile_definitions(cxxabi PUBLIC -DSTD_EXCEPTION_HAS_STACK_TRACE=0) install( TARGETS cxxabi @@ -48,4 +49,4 @@ install( ARCHIVE DESTINATION lib RUNTIME DESTINATION lib LIBRARY DESTINATION lib -) \ No newline at end of file +) diff --git a/contrib/libunwind b/contrib/libunwind index 8fe25d7dc70..601db0b0e03 160000 --- a/contrib/libunwind +++ b/contrib/libunwind @@ -1 +1 @@ -Subproject commit 8fe25d7dc70f2a4ea38c3e5a33fa9d4199b67a5a +Subproject commit 601db0b0e03018c01710470a37703b618f9cf08b diff --git a/contrib/libunwind-cmake/CMakeLists.txt b/contrib/libunwind-cmake/CMakeLists.txt index ecd90c8b762..734f855cddc 100644 --- a/contrib/libunwind-cmake/CMakeLists.txt +++ b/contrib/libunwind-cmake/CMakeLists.txt @@ -4,71 +4,51 @@ include(CheckCXXCompilerFlag) set(LIBUNWIND_SOURCE_DIR ${RaftKeeper_SOURCE_DIR}/contrib/libunwind) set(LIBUNWIND_CXX_SOURCES - ${LIBUNWIND_SOURCE_DIR}/src/libunwind.cpp - ${LIBUNWIND_SOURCE_DIR}/src/Unwind-EHABI.cpp - ${LIBUNWIND_SOURCE_DIR}/src/Unwind-seh.cpp) -if (APPLE) - set(LIBUNWIND_CXX_SOURCES ${LIBUNWIND_CXX_SOURCES} ${LIBUNWIND_SOURCE_DIR}/src/Unwind_AppleExtras.cpp) -endif () + "${LIBUNWIND_SOURCE_DIR}/src/libunwind.cpp" + "${LIBUNWIND_SOURCE_DIR}/src/Unwind-EHABI.cpp" + "${LIBUNWIND_SOURCE_DIR}/src/Unwind-seh.cpp") set(LIBUNWIND_C_SOURCES - ${LIBUNWIND_SOURCE_DIR}/src/UnwindLevel1.c - ${LIBUNWIND_SOURCE_DIR}/src/UnwindLevel1-gcc-ext.c - ${LIBUNWIND_SOURCE_DIR}/src/Unwind-sjlj.c - # Use unw_backtrace to override libgcc's backtrace symbol for better ABI compatibility - unwind-override.c) + "${LIBUNWIND_SOURCE_DIR}/src/UnwindLevel1.c" + "${LIBUNWIND_SOURCE_DIR}/src/UnwindLevel1-gcc-ext.c" + "${LIBUNWIND_SOURCE_DIR}/src/Unwind-sjlj.c" + # Use unw_backtrace to override libgcc's backtrace symbol for better ABI compatibility + unwind-override.c) set_source_files_properties(${LIBUNWIND_C_SOURCES} PROPERTIES COMPILE_FLAGS "-std=c99") set(LIBUNWIND_ASM_SOURCES - ${LIBUNWIND_SOURCE_DIR}/src/UnwindRegistersRestore.S - ${LIBUNWIND_SOURCE_DIR}/src/UnwindRegistersSave.S) + "${LIBUNWIND_SOURCE_DIR}/src/UnwindRegistersRestore.S" + "${LIBUNWIND_SOURCE_DIR}/src/UnwindRegistersSave.S") -# CMake doesn't pass the correct architecture for Apple prior to CMake 3.19 [1] -# Workaround these two issues by compiling as C. -# -# [1]: https://gitlab.kitware.com/cmake/cmake/-/issues/20771 -if (APPLE AND CMAKE_VERSION VERSION_LESS 3.19) - set_source_files_properties(${LIBUNWIND_ASM_SOURCES} PROPERTIES LANGUAGE C) -else() - enable_language(ASM) -endif() +enable_language(ASM) set(LIBUNWIND_SOURCES - ${LIBUNWIND_CXX_SOURCES} - ${LIBUNWIND_C_SOURCES} - ${LIBUNWIND_ASM_SOURCES}) + ${LIBUNWIND_CXX_SOURCES} + ${LIBUNWIND_C_SOURCES} + ${LIBUNWIND_ASM_SOURCES}) add_library(unwind ${LIBUNWIND_SOURCES}) +set_target_properties(unwind PROPERTIES FOLDER "contrib/libunwind-cmake") target_include_directories(unwind SYSTEM BEFORE PUBLIC $) -target_compile_definitions(unwind PRIVATE -D_LIBUNWIND_NO_HEAP=1 -D_DEBUG -D_LIBUNWIND_IS_NATIVE_ONLY) -target_compile_options(unwind PRIVATE -fno-exceptions -funwind-tables -fno-sanitize=all $<$:-nostdinc++ -fno-rtti> -Wno-unused-but-set-variable) +target_compile_definitions(unwind PRIVATE -D_LIBUNWIND_NO_HEAP=1) +target_compile_definitions(unwind PRIVATE -D_LIBUNWIND_REMEMBER_STACK_ALLOC=1) +# NOTE: from this macros sizeof(unw_context_t)/sizeof(unw_cursor_t) is depends, so it should be set always +target_compile_definitions(unwind PUBLIC -D_LIBUNWIND_IS_NATIVE_ONLY) -check_c_compiler_flag(-Wunused-but-set-variable HAVE_WARNING_UNUSED_BUT_SET_VARIABLE) -if (HAVE_WARNING_UNUSED_BUT_SET_VARIABLE) - target_compile_options(unwind PRIVATE -Wno-unused-but-set-variable) -endif () +# We should enable optimizations (otherwise it will be too slow in debug) +# and disable sanitizers (otherwise infinite loop may happen) +target_compile_options(unwind PRIVATE -O3 -fno-exceptions -funwind-tables -fno-sanitize=all $<$:-nostdinc++ -fno-rtti>) -check_cxx_compiler_flag(-Wmissing-attributes HAVE_WARNING_MISSING_ATTRIBUTES) -if (HAVE_WARNING_MISSING_ATTRIBUTES) - target_compile_options(unwind PRIVATE -Wno-missing-attributes) -endif () - -check_cxx_compiler_flag(-Wmaybe-uninitialized HAVE_WARNING_MAYBE_UNINITIALIZED) -if (HAVE_WARNING_MAYBE_UNINITIALIZED) - target_compile_options(unwind PRIVATE -Wno-maybe-uninitialized) -endif () +target_compile_options(unwind PRIVATE -Wno-unused-but-set-variable) # The library is using register variables that are bound to specific registers # Example: DwarfInstructions.hpp: register unsigned long long x16 __asm("x16") = cfa; -check_cxx_compiler_flag(-Wregister HAVE_WARNING_REGISTER) -if (HAVE_WARNING_REGISTER) - target_compile_options(unwind PRIVATE "$<$:-Wno-register>") -endif () +target_compile_options(unwind PRIVATE "$<$:-Wno-register>") install( - TARGETS unwind - EXPORT global - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib -) + TARGETS unwind + EXPORT global + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib +) \ No newline at end of file diff --git a/contrib/libunwind-cmake/unwind-override.c b/contrib/libunwind-cmake/unwind-override.c index 616bab6ae4b..57928d817eb 100644 --- a/contrib/libunwind-cmake/unwind-override.c +++ b/contrib/libunwind-cmake/unwind-override.c @@ -1,6 +1,10 @@ #include +/// On MacOS this function will be replaced with a dynamic symbol +/// from the system library. +#if !defined(OS_DARWIN) int backtrace(void ** buffer, int size) { return unw_backtrace(buffer, size); } +#endif diff --git a/contrib/llvm-project b/contrib/llvm-project new file mode 160000 index 00000000000..6009708b436 --- /dev/null +++ b/contrib/llvm-project @@ -0,0 +1 @@ +Subproject commit 6009708b4367171ccdbf4b5905cb6a803753fe18 diff --git a/docs/how-to-build.md b/docs/how-to-build.md index 9b8bd77d74a..dce9e84c3d3 100644 --- a/docs/how-to-build.md +++ b/docs/how-to-build.md @@ -4,7 +4,7 @@ RaftKeeper supports build on Linux and Mac OX. ### Build on Ubuntu -Requirement: Ubuntu 20.04+, Clang 13+, Cmake 3.12+ +Requirement: Ubuntu 20.04+, Clang 17+, Cmake 3.12+ ``` # install tools sudo apt-get install cmake llvm-17 @@ -20,7 +20,7 @@ cd RaftKeeper && /bin/bash build.sh ### Build on macOS -Requirement: macOS 10.15+, Clang 13+, Cmake 3.20+ +Requirement: macOS 10.15+, Clang 17+, Cmake 3.20+ ``` # install tools diff --git a/src/Common/Epoll.h b/src/Common/Epoll.h index 45f88b048ce..53c27cc3dd9 100644 --- a/src/Common/Epoll.h +++ b/src/Common/Epoll.h @@ -2,8 +2,7 @@ #if defined(OS_LINUX) #include -#include -#include +#include #include namespace RK diff --git a/src/Common/Exception.cpp b/src/Common/Exception.cpp index 516b056e03a..42ab54ed1bf 100644 --- a/src/Common/Exception.cpp +++ b/src/Common/Exception.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -50,55 +49,31 @@ void handle_error_code([[maybe_unused]] const std::string & msg, int code) ErrorCodes::increment(code); } -Exception::Exception(const std::string & msg, int code, bool remote_) +Exception::Exception(const std::string & msg, int code) : Poco::Exception(msg, code) - , remote(remote_) { + trace = StackTrace(); handle_error_code(msg, code); } Exception::Exception(const std::string & msg, const Exception & nested, int code) : Poco::Exception(msg, nested, code) { + trace = StackTrace(); handle_error_code(msg, code); } -Exception::Exception(CreateFromPocoTag, const Poco::Exception & exc) - : Poco::Exception(exc.displayText(), ErrorCodes::POCO_EXCEPTION) -{ -#ifdef STD_EXCEPTION_HAS_STACK_TRACE - set_stack_trace(exc.get_stack_trace_frames(), exc.get_stack_trace_size()); -#endif -} - -Exception::Exception(CreateFromSTDTag, const std::exception & exc) - : Poco::Exception(demangle(typeid(exc).name()) + ": " + String(exc.what()), ErrorCodes::STD_EXCEPTION) -{ -#ifdef STD_EXCEPTION_HAS_STACK_TRACE - set_stack_trace(exc.get_stack_trace_frames(), exc.get_stack_trace_size()); -#endif -} - - std::string getExceptionStackTraceString(const std::exception & e) { -#ifdef STD_EXCEPTION_HAS_STACK_TRACE - return StackTrace::toString(e.get_stack_trace_frames(), 0, e.get_stack_trace_size()); -#else if (const auto * db_exception = dynamic_cast(&e)) return db_exception->getStackTraceString(); return {}; -#endif } std::string Exception::getStackTraceString() const { -#ifdef STD_EXCEPTION_HAS_STACK_TRACE - return StackTrace::toString(get_stack_trace_frames(), 0, get_stack_trace_size()); -#else - return trace.toString(); -#endif + return trace ? trace->toString() : ""; } void throwFromErrno(const std::string & s, int code, int the_errno) @@ -139,10 +114,10 @@ void tryLogCurrentException(Poco::Logger * logger, const std::string & start_of_ static void getNoSpaceLeftInfoMessage(std::filesystem::path path, std::string & msg) { - path = std::filesystem::absolute(path); + path = absolute(path); /// It's possible to get ENOSPC for non existent file (e.g. if there are no free inodes and creat() fails) /// So try to get info for existent parent directory. - while (!std::filesystem::exists(path) && path.has_relative_path()) + while (!exists(path) && path.has_relative_path()) path = path.parent_path(); auto fs = getStatVFS(path); diff --git a/src/Common/Exception.h b/src/Common/Exception.h index 29663d7dd4c..deda5b59393 100644 --- a/src/Common/Exception.h +++ b/src/Common/Exception.h @@ -2,16 +2,19 @@ #include #include -#include -#include #include +#include +#include +#include +#include #include #include #include + #if !defined(NDEBUG) || defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || defined(UNDEFINED_BEHAVIOR_SANITIZER) #define ABORT_ON_LOGICAL_ERROR #endif @@ -26,7 +29,7 @@ class Exception : public Poco::Exception { public: Exception() = default; - Exception(const std::string & msg, int code, bool remote_ = false); + Exception(const std::string & msg, int code); Exception(const std::string & msg, const Exception & nested, int code); Exception(int code, const std::string & message) @@ -35,16 +38,13 @@ class Exception : public Poco::Exception // Format message with fmt::format, like the logging functions. template - Exception(int code, const std::string & fmt, Args&&... args) - : Exception(fmt::format(fmt, std::forward(args)...), code) + Exception(int code, std::string_view fmt, Args&&... args) + : Exception(fmt::format(fmt::runtime(fmt), std::forward(args)...), code) {} struct CreateFromPocoTag {}; struct CreateFromSTDTag {}; - Exception(CreateFromPocoTag, const Poco::Exception & exc); - Exception(CreateFromSTDTag, const std::exception & exc); - Exception * clone() const override { return new Exception(*this); } void rethrow() const override { throw *this; } const char * name() const throw() override { return "RK::Exception"; } @@ -52,9 +52,9 @@ class Exception : public Poco::Exception /// Add something to the existing message. template - void addMessage(const std::string& format, Args&&... args) + void addMessage(std::string_view fmt, Args&&... args) { - extendedMessage(fmt::format(format, std::forward(args)...)); + extendedMessage(fmt::format(fmt::runtime(fmt), std::forward(args)...)); } void addMessage(const std::string& message) @@ -62,17 +62,10 @@ class Exception : public Poco::Exception extendedMessage(message); } - /// Used to distinguish local exceptions from the one that was received from remote node. - void setRemoteException(bool remote_ = true) { remote = remote_; } - bool isRemoteException() const { return remote; } - std::string getStackTraceString() const; private: -#ifndef STD_EXCEPTION_HAS_STACK_TRACE - StackTrace trace; -#endif - bool remote = false; + std::optional trace; const char * className() const throw() override { return "RK::Exception"; } }; @@ -114,8 +107,8 @@ class ParsingException : public Exception // Format message with fmt::format, like the logging functions. template - ParsingException(int code, const std::string & fmt, Args&&... args) - : Exception(fmt::format(fmt, std::forward(args)...), code) + ParsingException(int code, std::string_view fmt, Args&&... args) + : Exception(fmt::format(fmt::runtime(fmt), std::forward(args)...), code) {} @@ -141,9 +134,9 @@ using Exceptions = std::vector; template -void throwFromErrno(int code, const std::string & fmt, Args&&... args) +void throwFromErrno(int code, std::string_view fmt, Args&&... args) { - throw ErrnoException(fmt::format(fmt, std::forward(args)...) + ", " + errnoToString(code, errno), code, errno); + throw ErrnoException(fmt::format(fmt::runtime(fmt), std::forward(args)...) + ", " + errnoToString(code, errno), code, errno); } [[noreturn]] void throwFromErrno(const std::string & s, int code, int the_errno = errno); diff --git a/src/Common/IO/ReadHelpers.cpp b/src/Common/IO/ReadHelpers.cpp index e5fda1fbc9f..7c536d14eea 100644 --- a/src/Common/IO/ReadHelpers.cpp +++ b/src/Common/IO/ReadHelpers.cpp @@ -587,7 +587,7 @@ template void readDateTimeTextFallback(time_t &, ReadBuffer &, const DateL template bool readDateTimeTextFallback(time_t &, ReadBuffer &, const DateLUTImpl &); -Exception readException(ReadBuffer & buf, const String & additional_message, bool remote_exception) +Exception readException(ReadBuffer & buf, const String & additional_message) { int code = 0; String name; @@ -614,7 +614,7 @@ Exception readException(ReadBuffer & buf, const String & additional_message, boo if (!stack_trace.empty()) out << " Stack trace:\n\n" << stack_trace; - return Exception(out.str(), code, remote_exception); + return Exception(out.str(), code); } void readAndThrowException(ReadBuffer & buf, const String & additional_message) diff --git a/src/Common/IO/ReadHelpers.h b/src/Common/IO/ReadHelpers.h index 7c4bc351e62..5a9066fc40a 100644 --- a/src/Common/IO/ReadHelpers.h +++ b/src/Common/IO/ReadHelpers.h @@ -667,15 +667,7 @@ inline std::enable_if_t && (sizeof(T) <= 8), void> readBinaryBigEndian(T & x, ReadBuffer & buf) /// Assuming little endian architecture. { readPODBinary(x, buf); - - if constexpr (sizeof(x) == 1) - return; - else if constexpr (sizeof(x) == 2) - x = __builtin_bswap16(x); - else if constexpr (sizeof(x) == 4) - x = __builtin_bswap32(x); - else if constexpr (sizeof(x) == 8) - x = __builtin_bswap64(x); + x = std::byteswap(x); } @@ -828,7 +820,7 @@ inline void skipWhitespaceIfAny(ReadBuffer & buf, bool one_line = false) * (type is cut to base class, 'message' replaced by 'displayText', and stack trace is appended to 'message') * Some additional message could be appended to exception (example: you could add information about from where it was received). */ -Exception readException(ReadBuffer & buf, const String & additional_message = "", bool remote_exception = false); +Exception readException(ReadBuffer & buf, const String & additional_message = ""); void readAndThrowException(ReadBuffer & buf, const String & additional_message = ""); diff --git a/src/Common/IO/WriteHelpers.h b/src/Common/IO/WriteHelpers.h index 0469803291c..64faec4c25c 100644 --- a/src/Common/IO/WriteHelpers.h +++ b/src/Common/IO/WriteHelpers.h @@ -917,13 +917,7 @@ template inline std::enable_if_t && (sizeof(T) <= 8), void> writeBinaryBigEndian(T x, WriteBuffer & buf) /// Assuming little endian architecture. { - if constexpr (sizeof(x) == 2) - x = __builtin_bswap16(x); - else if constexpr (sizeof(x) == 4) - x = __builtin_bswap32(x); - else if constexpr (sizeof(x) == 8) - x = __builtin_bswap64(x); - + x = std::byteswap(x); writePODBinary(x, buf); } diff --git a/src/Common/StackTrace.cpp b/src/Common/StackTrace.cpp index d6872650799..29f589fd56c 100644 --- a/src/Common/StackTrace.cpp +++ b/src/Common/StackTrace.cpp @@ -12,13 +12,17 @@ #include #include #include +#include #if !defined(ARCADIA_BUILD) # include #endif -#if USE_UNWIND -# include +#if defined(OS_DARWIN) +/// This header contains functions like `backtrace` and `backtrace_symbols` +/// Which will be used for stack unwinding on Mac. +/// Read: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/backtrace.3.html +#include "execinfo.h" #endif std::string signalToErrorMessage(int sig, const siginfo_t & info, const ucontext_t & context) @@ -294,10 +298,12 @@ StackTrace::StackTrace(NoCapture) void StackTrace::tryCapture() { size = 0; -#if USE_UNWIND +#if defined(OS_DARWIN) + size = backtrace(frame_pointers.data(), capacity); +#else size = unw_backtrace(frame_pointers.data(), capacity); - __msan_unpoison(frame_pointers.data(), size * sizeof(frame_pointers[0])); #endif + __msan_unpoison(frame_pointers.data(), size * sizeof(frame_pointers[0])); } size_t StackTrace::getSize() const diff --git a/src/Common/StringUtils.h b/src/Common/StringUtils.h index 20c0a5ca380..7af694bba4f 100644 --- a/src/Common/StringUtils.h +++ b/src/Common/StringUtils.h @@ -4,7 +4,7 @@ #include #include #include -#include +#include namespace detail diff --git a/src/Common/ThreadPool.h b/src/Common/ThreadPool.h index 6fcfe31471e..d61f93247c9 100644 --- a/src/Common/ThreadPool.h +++ b/src/Common/ThreadPool.h @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/Common/ThreadProfileEvents.h b/src/Common/ThreadProfileEvents.h index ee1656d8ffc..2c6627d8f91 100644 --- a/src/Common/ThreadProfileEvents.h +++ b/src/Common/ThreadProfileEvents.h @@ -7,6 +7,7 @@ #include #include #include +#include #if defined(__linux__) diff --git a/src/Common/formatReadable.h b/src/Common/formatReadable.h index ad7a04f3373..78c061170fd 100644 --- a/src/Common/formatReadable.h +++ b/src/Common/formatReadable.h @@ -33,24 +33,10 @@ struct ReadableSize }; /// See https://fmt.dev/latest/api.html#formatting-user-defined-types -template <> -struct fmt::formatter -{ - constexpr auto parse(format_parse_context & ctx) - { - auto it = ctx.begin(); - auto end = ctx.end(); - - /// Only support {}. - if (it != end && *it != '}') - throw format_error("invalid format"); - - return it; - } - - template - auto format(const ReadableSize & size, FormatContext & ctx) - { - return format_to(ctx.out(), "{}", formatReadableSizeWithBinarySuffix(size.value)); +template +struct fmt::formatter, char>> : + fmt::formatter { + auto format(const ReadableSize& size, format_context& ctx) const { + return formatter::format(formatReadableSizeWithBinarySuffix(size.value), ctx); } }; diff --git a/src/Service/FourLetterCommand.cpp b/src/Service/FourLetterCommand.cpp index cb929bd8b43..971b89e0d51 100644 --- a/src/Service/FourLetterCommand.cpp +++ b/src/Service/FourLetterCommand.cpp @@ -39,15 +39,16 @@ int32_t IFourLetterCommand::code() String IFourLetterCommand::toName(int32_t code) { - int reverted_code = __builtin_bswap32(code); + int reverted_code = std::byteswap(code); return String(reinterpret_cast(&reverted_code), 4); } int32_t IFourLetterCommand::toCode(const String & name) { - int32_t res = *reinterpret_cast(name.data()); + int32_t res; + std::memcpy(&res, name.data(), sizeof(res)); /// keep consistent with Coordination::read method by changing big endian to little endian. - return __builtin_bswap32(res); + return std::byteswap(res); } IFourLetterCommand::~IFourLetterCommand() = default; diff --git a/src/Service/KeeperCommon.cpp b/src/Service/KeeperCommon.cpp index c97d83eae38..db02c83ae1d 100644 --- a/src/Service/KeeperCommon.cpp +++ b/src/Service/KeeperCommon.cpp @@ -12,7 +12,7 @@ String ErrorRequest::toString() const xid, Coordination::toString(opnum), accepted, - error_code); + RK::toString(error_code)); } RequestId ErrorRequest::getRequestId() const diff --git a/src/Service/KeeperCommon.h b/src/Service/KeeperCommon.h index 9a0fbf4514a..034f1a27ae0 100644 --- a/src/Service/KeeperCommon.h +++ b/src/Service/KeeperCommon.h @@ -87,11 +87,47 @@ struct RequestId }; }; +using nuraft::cmd_result_code; +inline String toString(const cmd_result_code & code) +{ + switch (code) { + case cmd_result_code::OK: + return "OK"; + case cmd_result_code::CANCELLED: + return "CANCELLED"; + case cmd_result_code::TIMEOUT: + return "TIMEOUT"; + case cmd_result_code::NOT_LEADER: + return "NOT_LEADER"; + case cmd_result_code::BAD_REQUEST: + return "BAD_REQUEST"; + case cmd_result_code::SERVER_ALREADY_EXISTS: + return "SERVER_ALREADY_EXISTS"; + case cmd_result_code::CONFIG_CHANGING: + return "CONFIG_CHANGING"; + case cmd_result_code::SERVER_IS_JOINING: + return "SERVER_IS_JOINING"; + case cmd_result_code::SERVER_NOT_FOUND: + return "SERVER_NOT_FOUND"; + case cmd_result_code::CANNOT_REMOVE_LEADER: + return "CANNOT_REMOVE_LEADER"; + case cmd_result_code::SERVER_IS_LEAVING: + return "SERVER_IS_LEAVING"; + case cmd_result_code::TERM_MISMATCH: + return "TERM_MISMATCH"; + case cmd_result_code::RESULT_NOT_EXIST_YET: + return "RESULT_NOT_EXIST_YET"; + case cmd_result_code::FAILED: + return "FAILED"; + } + return "Unknown value: {}" + std::to_string(code); +} + /// Simple error request info. struct ErrorRequest { bool accepted; - nuraft::cmd_result_code error_code; /// TODO new error code instead of NuRaft error code + cmd_result_code error_code; /// TODO new error code instead of NuRaft error code int64_t session_id; /// For new session request, this is internal_id, for there is no session_id right now. Coordination::XID xid; Coordination::OpNum opnum; @@ -108,4 +144,25 @@ bool isSessionRequest(const Coordination::ZooKeeperRequestPtr & request); bool isNewSessionRequest(Coordination::OpNum opnum); +using nuraft::log_val_type; +inline std::string toString(const log_val_type & log_type) +{ + switch (log_type) + { + case log_val_type::app_log: + return "app_log"; + case log_val_type::conf: + return "conf"; + case log_val_type::cluster_server: + return "cluster_server"; + case log_val_type::log_pack: + return "log_pack"; + case log_val_type::snp_sync_req: + return "snp_sync_req"; + case log_val_type::custom: + return "custom"; + } + return "Unknown value: {}" + std::to_string(log_type); +} + } diff --git a/src/Service/KeeperStore.cpp b/src/Service/KeeperStore.cpp index 00c56469454..ae52865ea52 100644 --- a/src/Service/KeeperStore.cpp +++ b/src/Service/KeeperStore.cpp @@ -270,7 +270,8 @@ struct StoreRequestCreate final : public StoreRequest response.error = Coordination::Error::ZNONODE; return {response_ptr, undo}; } - else if (parent->is_ephemeral) + + if (parent->is_ephemeral) { response.error = Coordination::Error::ZNOCHILDRENFOREPHEMERALS; return {response_ptr, undo}; @@ -1018,7 +1019,7 @@ struct StoreRequestMultiTxn final : public StoreRequest } else throw RK::Exception( - ErrorCodes::BAD_ARGUMENTS, "Illegal command as part of multi ZooKeeper request {}", sub_zk_request->getOpNum()); + ErrorCodes::BAD_ARGUMENTS, "Illegal command as part of multi ZooKeeper request {}", toString(sub_zk_request->getOpNum())); } } @@ -1153,7 +1154,7 @@ class StoreRequestFactory final : private boost::noncopyable void registerRequest(Coordination::OpNum op_num, Creator creator) { if (!op_num_to_request.try_emplace(op_num, creator).second) - throw RK::Exception(ErrorCodes::LOGICAL_ERROR, "Request with op num {} already registered", op_num); + throw RK::Exception(ErrorCodes::LOGICAL_ERROR, "Request with op num {} already registered", toString(op_num)); } private: @@ -1205,7 +1206,7 @@ void KeeperStore::processRequest( { if (zxid >= *new_last_zxid) throw Exception( - ErrorCodes::LOGICAL_ERROR, "Got new ZXID {} smaller or equal than current {}. It's a bug", *new_last_zxid, zxid); + ErrorCodes::LOGICAL_ERROR, "Got new ZXID {} smaller or equal than current {}. It's a bug", *new_last_zxid, zxid.load()); zxid = *new_last_zxid; } diff --git a/src/Service/NuRaftLogSegment.cpp b/src/Service/NuRaftLogSegment.cpp index f5a7011c898..ff0d3bae10f 100644 --- a/src/Service/NuRaftLogSegment.cpp +++ b/src/Service/NuRaftLogSegment.cpp @@ -14,6 +14,7 @@ #include #include #include +#include namespace RK @@ -387,8 +388,8 @@ UInt64 NuRaftLogSegment::appendEntry(const ptr & entry, std::atomicget_val_type()); + file_size.load(), + toString(entry->get_val_type())); return header.index; } @@ -472,7 +473,7 @@ bool NuRaftLogSegment::truncate(const UInt64 last_index_kept) log, "Truncate a closed segment, should re-open it. Current first index {}, last index {}, rename file from {} to {}.", first_index, - last_index, + last_index.load(), getClosedFileName(), getOpenFileName()); @@ -494,7 +495,7 @@ bool NuRaftLogSegment::truncate(const UInt64 last_index_kept) std::lock_guard write_lock(log_mutex); if (last_index <= last_index_kept) { - LOG_INFO(log, "Log segment {} truncates nothing, last_index {}, last_index_kept {}", file_name, last_index, last_index_kept); + LOG_INFO(log, "Log segment {} truncates nothing, last_index {}, last_index_kept {}", file_name, last_index.load(), last_index_kept); reopen_closed_segment(); return false; } @@ -508,7 +509,7 @@ bool NuRaftLogSegment::truncate(const UInt64 last_index_kept) file_name, first_log_offset_to_truncate, first_index, - last_index, + last_index.load(), last_index_kept, file_size_to_keep); } @@ -518,7 +519,7 @@ bool NuRaftLogSegment::truncate(const UInt64 last_index_kept) if (ftruncate(seg_fd, file_size_to_keep) != 0) throwFromErrno(ErrorCodes::CANNOT_WRITE_TO_FILE_DESCRIPTOR, "Fail to truncate log segment {}", file_name); - LOG_INFO(log, "Truncate file {} with fd {}, from {} to size {}", file_name, seg_fd, file_size_to_keep, file_size); + LOG_INFO(log, "Truncate file {} with fd {}, from {} to size {}", file_name, seg_fd, file_size_to_keep, file_size.load()); /// seek fd off_t ret_off = lseek(seg_fd, file_size_to_keep, SEEK_SET); diff --git a/src/Service/NuRaftLogSnapshot.cpp b/src/Service/NuRaftLogSnapshot.cpp index 1c9fee0bd26..d83362c3bf0 100644 --- a/src/Service/NuRaftLogSnapshot.cpp +++ b/src/Service/NuRaftLogSnapshot.cpp @@ -466,7 +466,7 @@ void KeeperSnapshotStore::parseObject(KeeperStore & store, String obj_path, Buck read_size += 1; LOG_DEBUG(log, "Got snapshot file header with version {}", toString(version_from_obj)); if (version_from_obj > CURRENT_SNAPSHOT_VERSION) - throw Exception(ErrorCodes::UNKNOWN_FORMAT_VERSION, "Unsupported snapshot version {}", version_from_obj); + throw Exception(ErrorCodes::UNKNOWN_FORMAT_VERSION, "Unsupported snapshot version {}", toString(version_from_obj)); } else if (isSnapshotFileTail(magic)) { @@ -897,7 +897,7 @@ size_t KeeperSnapshotManager::removeSnapshots() { Int64 remove_count = static_cast(snapshots.size()) - static_cast(keep_max_snapshot_count); - LOG_INFO(log, "There are {} snapshots, we will try to move {remove_count} of them", snapshots.size(), remove_count); + LOG_INFO(log, "There are {} snapshots, we will try to move {} of them", snapshots.size(), remove_count); while (remove_count > 0) { diff --git a/src/Service/NuRaftStateMachine.cpp b/src/Service/NuRaftStateMachine.cpp index 8f152c6bec3..5c448d558a9 100644 --- a/src/Service/NuRaftStateMachine.cpp +++ b/src/Service/NuRaftStateMachine.cpp @@ -78,7 +78,7 @@ NuRaftStateMachine::NuRaftStateMachine( log, "Previous last commit idx {} is less than the last committed idx {} from snapshot, skip replaying logs.", previous_last_commit_id, - last_committed_idx); + last_committed_idx.load()); } else { @@ -331,7 +331,7 @@ bool NuRaftStateMachine::applySnapshotImpl(snapshot & s) if (succeed) { last_committed_idx = s.get_last_log_idx(); - LOG_INFO(log, "Applied snapshot, now the last log index is {}", last_committed_idx); + LOG_INFO(log, "Applied snapshot, now the last log index is {}", last_committed_idx.load()); } return succeed; } @@ -394,7 +394,7 @@ void NuRaftStateMachine::replayLogs(ptr log_store_, uint64_t from, ui if (batch_end_index > last_index_in_store + 1) batch_end_index = last_index_in_store + 1; - LOG_INFO(thread_log, "Begin to load batch [{} , {})", batch_start_index, batch_end_index); + LOG_INFO(thread_log, "Begin to load batch [{} , {})", batch_start_index.load(), batch_end_index.load()); ReplayLogBatch batch; batch.log_entries @@ -408,7 +408,7 @@ void NuRaftStateMachine::replayLogs(ptr log_store_, uint64_t from, ui { if (entry_with_version.entry->get_val_type() != nuraft::log_val_type::app_log) { - LOG_DEBUG(thread_log, "Found non app nuraft log(type {}), ignore it", entry_with_version.entry->get_val_type()); + LOG_DEBUG(thread_log, "Found non app nuraft log(type {}), ignore it", toString(entry_with_version.entry->get_val_type())); batch.requests->push_back(nullptr); } else @@ -419,7 +419,7 @@ void NuRaftStateMachine::replayLogs(ptr log_store_, uint64_t from, ui } } - LOG_INFO(thread_log, "Finish to load batch [{}, {})", batch_start_index, batch_end_index); + LOG_INFO(thread_log, "Finish to load batch [{}, {})", batch_start_index.load(), batch_end_index.load()); log_queue.push(batch); batch_start_index.store(batch_end_index); } diff --git a/src/ZooKeeper/ZooKeeper.cpp b/src/ZooKeeper/ZooKeeper.cpp index 08c0650e613..3d36ca3b787 100644 --- a/src/ZooKeeper/ZooKeeper.cpp +++ b/src/ZooKeeper/ZooKeeper.cpp @@ -11,6 +11,7 @@ #include "common/logger_useful.h" #include +#include #define ZOOKEEPER_CONNECTION_TIMEOUT_MS 1000 diff --git a/src/ZooKeeper/ZooKeeperCommon.cpp b/src/ZooKeeper/ZooKeeperCommon.cpp index be347ce37dc..85d89a54ef4 100644 --- a/src/ZooKeeper/ZooKeeperCommon.cpp +++ b/src/ZooKeeper/ZooKeeperCommon.cpp @@ -35,7 +35,7 @@ void ZooKeeperResponse::writeNoCopy(WriteBufferFromOwnString & out) const String & result = out.str(); // write data length at begin of string - int32_t len = __builtin_bswap32(static_cast(result.size() - pre_size - sizeof(int32_t))); + int32_t len = std::byteswap(static_cast(result.size() - pre_size - sizeof(int32_t))); memcpy(result.data() + pre_size, reinterpret_cast(&len), sizeof(int32_t)); } @@ -343,7 +343,7 @@ void ZooKeeperErrorResponse::readImpl(ReadBuffer & in) Coordination::read(read_error, in); if (read_error != error) - throw Exception(fmt::format("Error code in ErrorResponse ({}) doesn't match error code in header ({})", read_error, error), + throw Exception(fmt::format("Error code in ErrorResponse ({}) doesn't match error code in header ({})", errorMessage(read_error), errorMessage(error)), Error::ZMARSHALLINGERROR); } diff --git a/src/ZooKeeper/ZooKeeperIO.cpp b/src/ZooKeeper/ZooKeeperIO.cpp index 56106f5710c..4335dc2ee1f 100644 --- a/src/ZooKeeper/ZooKeeperIO.cpp +++ b/src/ZooKeeper/ZooKeeperIO.cpp @@ -5,30 +5,30 @@ namespace Coordination void write(size_t x, WriteBuffer & out) { - x = __builtin_bswap64(x); + x = std::byteswap(x); writeBinary(x, out); } void write(int64_t x, WriteBuffer & out) { - x = __builtin_bswap64(x); + x = std::byteswap(x); writeBinary(x, out); } #ifdef __APPLE__ void write(uint64_t x, WriteBuffer & out) { - x = __builtin_bswap64(x); + x = std::byteswap(x); writeBinary(x, out); } #endif void write(int32_t x, WriteBuffer & out) { - x = __builtin_bswap32(x); + x = std::byteswap(x); writeBinary(x, out); } void write(uint32_t x, WriteBuffer & out) { - x = __builtin_bswap32(x); + x = std::byteswap(x); writeBinary(x, out); } void write(int8_t x, WriteBuffer & out) @@ -99,31 +99,31 @@ void write(const CompactStrings & strings, WriteBuffer & out) void read(size_t & x, ReadBuffer & in) { readBinary(x, in); - x = __builtin_bswap64(x); + x = std::byteswap(x); } void read(int64_t & x, ReadBuffer & in) { readBinary(x, in); - x = __builtin_bswap64(x); + x = std::byteswap(x); } #ifdef __APPLE__ void read(uint64_t & x, ReadBuffer & in) { readBinary(x, in); - x = __builtin_bswap64(x); + x = std::byteswap(x); } #endif void read(int32_t & x, ReadBuffer & in) { readBinary(x, in); - x = __builtin_bswap32(x); + x = std::byteswap(x); } void read(uint32_t & x, ReadBuffer & in) { readBinary(x, in); - x = __builtin_bswap32(x); + x = std::byteswap(x); } void read(uint8_t & x, ReadBuffer & in)