From 613facafde67596a1b306071c5c3bb792575bdc7 Mon Sep 17 00:00:00 2001 From: "mei, yang" Date: Wed, 12 Jan 2022 18:07:51 +0800 Subject: [PATCH] =?UTF-8?q?ShutdownProtobufLibrary=20when=20unload=20paddl?= =?UTF-8?q?e=20frontend=20dynmaic=20library=20t=E2=80=A6=20(#9442)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ShutdownProtobufLibrary when unload paddle frontend dynmaic library to fix probuf memory leak * ShutdownProtobufLibrary if the frontend libraries use protobuf * make shutdown_protobuf a library --- .../frontends/frontends.cmake | 7 ++++- src/frontends/common/CMakeLists.txt | 4 +++ .../common/shutdown_protobuf/CMakeLists.txt | 4 +++ .../shutdown_protobuf/shutdown_protobuf.cpp | 27 +++++++++++++++++++ src/frontends/onnx/frontend/CMakeLists.txt | 1 + src/frontends/paddle/CMakeLists.txt | 1 + src/frontends/tensorflow/CMakeLists.txt | 1 + 7 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/frontends/common/shutdown_protobuf/CMakeLists.txt create mode 100644 src/frontends/common/shutdown_protobuf/shutdown_protobuf.cpp diff --git a/cmake/developer_package/frontends/frontends.cmake b/cmake/developer_package/frontends/frontends.cmake index 34b3963b088..f662332b4c7 100644 --- a/cmake/developer_package/frontends/frontends.cmake +++ b/cmake/developer_package/frontends/frontends.cmake @@ -89,7 +89,7 @@ unset(protobuf_installed CACHE) # [LINK_LIBRARIES ]) # macro(ov_add_frontend) - set(options LINKABLE_FRONTEND PROTOBUF_LITE SKIP_NCC_STYLE SKIP_INSTALL) + set(options LINKABLE_FRONTEND SHUTDOWN_PROTOBUF PROTOBUF_LITE SKIP_NCC_STYLE SKIP_INSTALL) set(oneValueArgs NAME FILEDESCRIPTION) set(multiValueArgs LINK_LIBRARIES PROTO_FILES) cmake_parse_arguments(OV_FRONTEND "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) @@ -149,6 +149,11 @@ macro(ov_add_frontend) add_library(openvino::frontend::${OV_FRONTEND_NAME} ALIAS ${TARGET_NAME}) endif() + # Shutdown protobuf when unloading the front dynamic library + if(OV_FRONTEND_SHUTDOWN_PROTOBUF AND BUILD_SHARED_LIBS) + target_link_libraries(${TARGET_NAME} PRIVATE ov_protobuf_shutdown) + endif() + if(NOT BUILD_SHARED_LIBS) # override default function names target_compile_definitions(${TARGET_NAME} PRIVATE diff --git a/src/frontends/common/CMakeLists.txt b/src/frontends/common/CMakeLists.txt index 96c4775cb3f..1987cdbe1df 100644 --- a/src/frontends/common/CMakeLists.txt +++ b/src/frontends/common/CMakeLists.txt @@ -72,3 +72,7 @@ openvino_developer_export_targets(COMPONENT core_legacy TARGETS ${TARGET_NAME}) install(DIRECTORY ${FRONTEND_INCLUDE_DIR}/openvino DESTINATION ${FRONTEND_INSTALL_INCLUDE} COMPONENT core_dev) + + +# Shutdown protobuf library +add_subdirectory(shutdown_protobuf) diff --git a/src/frontends/common/shutdown_protobuf/CMakeLists.txt b/src/frontends/common/shutdown_protobuf/CMakeLists.txt new file mode 100644 index 00000000000..6f8a06c62bf --- /dev/null +++ b/src/frontends/common/shutdown_protobuf/CMakeLists.txt @@ -0,0 +1,4 @@ +set(TARGET_NAME ov_protobuf_shutdown) +add_library(${TARGET_NAME} STATIC shutdown_protobuf.cpp) +target_sources(${TARGET_NAME} INTERFACE shutdown_protobuf.cpp) +target_include_directories(${TARGET_NAME} SYSTEM PRIVATE ${Protobuf_INCLUDE_DIRS}) diff --git a/src/frontends/common/shutdown_protobuf/shutdown_protobuf.cpp b/src/frontends/common/shutdown_protobuf/shutdown_protobuf.cpp new file mode 100644 index 00000000000..49defa1928a --- /dev/null +++ b/src/frontends/common/shutdown_protobuf/shutdown_protobuf.cpp @@ -0,0 +1,27 @@ +#include + +#if defined(_WIN32) +# include +BOOL WINAPI DllMain(HINSTANCE hinstDLL, // handle to DLL module + DWORD fdwReason, // reason for calling function + LPVOID lpReserved) // reserved +{ + // Perform actions based on the reason for calling. + switch (fdwReason) { + case DLL_PROCESS_ATTACH: + case DLL_THREAD_ATTACH: + case DLL_THREAD_DETACH: + break; + + case DLL_PROCESS_DETACH: + google::protobuf::ShutdownProtobufLibrary(); + break; + } + return TRUE; // Successful DLL_PROCESS_ATTACH. +} +#elif defined(__linux__) || defined(__APPLE__) +extern "C" __attribute__((destructor)) void library_unload(); +void library_unload() { + google::protobuf::ShutdownProtobufLibrary(); +} +#endif diff --git a/src/frontends/onnx/frontend/CMakeLists.txt b/src/frontends/onnx/frontend/CMakeLists.txt index f75a2917d81..f8a7faed867 100644 --- a/src/frontends/onnx/frontend/CMakeLists.txt +++ b/src/frontends/onnx/frontend/CMakeLists.txt @@ -4,6 +4,7 @@ ov_add_frontend(NAME onnx LINKABLE_FRONTEND + SHUTDOWN_PROTOBUF PROTOBUF_LITE SKIP_NCC_STYLE FILEDESCRIPTION "FrontEnd to load and convert ONNX file format" diff --git a/src/frontends/paddle/CMakeLists.txt b/src/frontends/paddle/CMakeLists.txt index 1d043d7e812..6ad6be9345d 100644 --- a/src/frontends/paddle/CMakeLists.txt +++ b/src/frontends/paddle/CMakeLists.txt @@ -4,6 +4,7 @@ ov_add_frontend(NAME paddle LINKABLE_FRONTEND + SHUTDOWN_PROTOBUF PROTOBUF_LITE FILEDESCRIPTION "FrontEnd to load and convert PaddlePaddle file format" LINK_LIBRARIES openvino::runtime::dev) diff --git a/src/frontends/tensorflow/CMakeLists.txt b/src/frontends/tensorflow/CMakeLists.txt index b285b4ed459..a0370422a70 100644 --- a/src/frontends/tensorflow/CMakeLists.txt +++ b/src/frontends/tensorflow/CMakeLists.txt @@ -4,6 +4,7 @@ # TODO: Add LINKABLE_FRONTEND option when tensorflow frontend directory is moved to openvino folder ov_add_frontend(NAME tensorflow + SHUTDOWN_PROTOBUF SKIP_INSTALL FILEDESCRIPTION "FrontEnd to load and convert TensorFlow file format" LINK_LIBRARIES openvino::util)