From 41ba52bb28d9dcff140a97f5d76d44bf3d2f47b4 Mon Sep 17 00:00:00 2001 From: Cai Yudong Date: Tue, 7 Dec 2021 20:17:03 +0800 Subject: [PATCH] Support C unittest profiling (#12899) Signed-off-by: yudong.cai --- .../core/src/index/thirdparty/CMakeLists.txt | 16 +++ .../index/thirdparty/profilers/CMakeLists.txt | 104 ++++++++++++++++++ internal/core/unittest/CMakeLists.txt | 2 + 3 files changed, 122 insertions(+) create mode 100644 internal/core/src/index/thirdparty/profilers/CMakeLists.txt diff --git a/internal/core/src/index/thirdparty/CMakeLists.txt b/internal/core/src/index/thirdparty/CMakeLists.txt index 5edd64fd1f..89787da579 100644 --- a/internal/core/src/index/thirdparty/CMakeLists.txt +++ b/internal/core/src/index/thirdparty/CMakeLists.txt @@ -1,3 +1,14 @@ +# Copyright (C) 2019-2020 Zilliz. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software distributed under the License +# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express +# or implied. See the License for the specific language governing permissions and limitations under the License + add_compile_options( -O3 -fPIC -Wno-error -fopenmp ) if ( NOT KNOWHERE_VERBOSE_THIRDPARTY_BUILD ) @@ -178,3 +189,8 @@ if (KNOWHERE_WITH_FAISS AND NOT TARGET faiss_ep) include_directories(SYSTEM "${FAISS_INCLUDE_DIR}") link_directories(SYSTEM ${FAISS_PREFIX}/lib/) endif () + +if ( BUILD_UNIT_TEST STREQUAL "ON" ) + add_subdirectory(profilers) +endif () + diff --git a/internal/core/src/index/thirdparty/profilers/CMakeLists.txt b/internal/core/src/index/thirdparty/profilers/CMakeLists.txt new file mode 100644 index 0000000000..33821d305e --- /dev/null +++ b/internal/core/src/index/thirdparty/profilers/CMakeLists.txt @@ -0,0 +1,104 @@ +#------------------------------------------------------------------------------- +# Copyright (C) 2019-2020 Zilliz. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software distributed under the License +# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express +# or implied. See the License for the specific language governing permissions and limitations under the License. +#------------------------------------------------------------------------------- +set( LIBUNWIND_VERSION 1.6.2 ) +set( GPERFTOOLS_VERSION 2.9.1 ) + +if ( DEFINED ENV{KNOWHERE_LIBUNWIND_URL} ) + set( LIBUNWIND_SOURCE_URL "$ENV{KNOWHERE_LIBUNWIND_URL}" ) +else () + set( LIBUNWIND_SOURCE_URL + "https://github.com/libunwind/libunwind/releases/download/v${LIBUNWIND_VERSION}/libunwind-${LIBUNWIND_VERSION}.tar.gz" ) +endif () + +if ( DEFINED ENV{KNOWHERE_GPERFTOOLS_URL} ) + set( GPERFTOOLS_SOURCE_URL "$ENV{KNOWHERE_GPERFTOOLS_URL}" ) +else () + set( GPERFTOOLS_SOURCE_URL + "https://github.com/gperftools/gperftools/releases/download/gperftools-${GPERFTOOLS_VERSION}/gperftools-${GPERFTOOLS_VERSION}.tar.gz" ) +endif () + +# ---------------------------------------------------------------------- +# libunwind + +macro( build_libunwind ) + message( STATUS "Building libunwind-${LIBUNWIND_VERSION} from source" ) + + set( LIBUNWIND_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/libunwind) + ExternalProject_Add( + libunwind_ep + DOWNLOAD_DIR ${THIRDPARTY_DOWNLOAD_PATH} + INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/libunwind + URL ${LIBUNWIND_SOURCE_URL} + URL_MD5 f625b6a98ac1976116c71708a73dc44a + CONFIGURE_COMMAND /configure + "--prefix=" + "--quiet" + "--disable-tests" + "cc=${EP_C_COMPILER}" + "cxx=${EP_CXX_COMPILER}" + BUILD_COMMAND ${MAKE} ${MAKE_BUILD_ARGS} + INSTALL_COMMAND ${MAKE} install + ${EP_LOG_OPTIONS} ) + + ExternalProject_Get_Property( libunwind_ep INSTALL_DIR ) + file(MAKE_DIRECTORY ${INSTALL_DIR}/include) + add_library( libunwind SHARED IMPORTED ) + set_target_properties( + libunwind PROPERTIES + IMPORTED_GLOBAL TRUE + IMPORTED_LOCATION "${INSTALL_DIR}/lib/libunwind.so" + INTERFACE_INCLUDE_DIRECTORIES "${INSTALL_DIR}/include" ) + + add_dependencies( libunwind libunwind_ep ) +endmacro() + + +# ---------------------------------------------------------------------- +# gperftools + +macro( build_gperftools ) + message( STATUS "Building gperftools-${GPERFTOOLS_VERSION} from source" ) + + ExternalProject_Add( + gperftools_ep + DEPENDS libunwind_ep + DOWNLOAD_DIR ${CMAKE_BINARY_DIR}/3rdparty_download/download + INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/gperftools + URL ${GPERFTOOLS_SOURCE_URL} + URL_MD5 cb21f2ebe71bbc8d5ad101b310be980a + CONFIGURE_COMMAND /configure + "--prefix=" + "--quiet" + "cc=${EP_C_COMPILER}" + "cxx=${EP_CXX_COMPILER}" + BUILD_COMMAND ${MAKE} ${MAKE_BUILD_ARGS} + INSTALL_COMMAND ${MAKE} install + ${EP_LOG_OPTIONS} ) + + ExternalProject_Get_Property( gperftools_ep INSTALL_DIR ) + file(MAKE_DIRECTORY ${INSTALL_DIR}/include) + # libprofiler.a + add_library( gperftools STATIC IMPORTED ) + set_target_properties( gperftools + PROPERTIES + IMPORTED_GLOBAL TRUE + IMPORTED_LOCATION "${INSTALL_DIR}/lib/libtcmalloc_and_profiler.a" + INTERFACE_INCLUDE_DIRECTORIES "${INSTALL_DIR}/include" + INTERFACE_LINK_LIBRARIES libunwind + ) + add_dependencies( gperftools gperftools_ep ) +endmacro() + + +build_libunwind() +build_gperftools() diff --git a/internal/core/unittest/CMakeLists.txt b/internal/core/unittest/CMakeLists.txt index dd3e8ee525..cac78af130 100644 --- a/internal/core/unittest/CMakeLists.txt +++ b/internal/core/unittest/CMakeLists.txt @@ -68,7 +68,9 @@ target_link_libraries(all_tests milvus_indexbuilder log pthread + gperftools ) install(TARGETS all_tests DESTINATION unittest) install(TARGETS index_builder_test DESTINATION unittest) +