From 058b3919c7580d9ab9ba3c9234e6db368d7c323e Mon Sep 17 00:00:00 2001 From: wangguibao Date: Fri, 15 Feb 2019 12:23:55 +0800 Subject: [PATCH] libpdserving & libpdclient --- cmake/flags.cmake | 1 - cmake/generic.cmake | 57 ++++++++++++++++++++++++++++++ predictor/CMakeLists.txt | 38 +++++++++++++++++--- predictor/common/CMakeLists.txt | 3 +- predictor/framework/CMakeLists.txt | 3 +- predictor/framework/logger.h | 35 ------------------ predictor/op/CMakeLists.txt | 3 +- predictor/plugin/CMakeLists.txt | 2 ++ predictor/proto/CMakeLists.txt | 23 ++++++++++-- predictor/src/CMakeLists.txt | 3 ++ predictor/src/pdclient.cpp | 27 +++----------- predictor/src/pdserving.cpp | 21 ++++------- 12 files changed, 132 insertions(+), 84 deletions(-) delete mode 100644 predictor/framework/logger.h create mode 100644 predictor/plugin/CMakeLists.txt create mode 100644 predictor/src/CMakeLists.txt diff --git a/cmake/flags.cmake b/cmake/flags.cmake index 81e7868a..dfbe8b6f 100644 --- a/cmake/flags.cmake +++ b/cmake/flags.cmake @@ -116,7 +116,6 @@ if (NOT WIN32) set(COMMON_FLAGS -fPIC -fno-omit-frame-pointer - -Werror -Wall -Wextra -Wnon-virtual-dtor diff --git a/cmake/generic.cmake b/cmake/generic.cmake index 6679a09d..f72cd613 100644 --- a/cmake/generic.cmake +++ b/cmake/generic.cmake @@ -792,3 +792,60 @@ function(brpc_library TARGET_NAME) cc_library("${TARGET_NAME}_proto" SRCS "${brpc_proto_srcs}") cc_library("${TARGET_NAME}" SRCS "${brpc_library_SRCS}" DEPS "${TARGET_NAME}_proto" "${brpc_library_DEPS}") endfunction() + +function(PROTOBUF_GENERATE_SERVING_CPP SRCS HDRS) + if(NOT ARGN) + message(SEND_ERROR "Error: PROTOBUF_GENERATE_SERVING_CPP() called without any proto files") + return() + endif() + + if(PROTOBUF_GENERATE_CPP_APPEND_PATH) # This variable is common for all types of output. + # Create an include path for each file specified + foreach(FIL ${ARGN}) + get_filename_component(ABS_FIL ${FIL} ABSOLUTE) + get_filename_component(ABS_PATH ${ABS_FIL} PATH) + list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) + if(${_contains_already} EQUAL -1) + list(APPEND _protobuf_include_path -I ${ABS_PATH}) + endif() + endforeach() + else() + set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR}) + endif() + + if(DEFINED PROTOBUF_IMPORT_DIRS) + foreach(DIR ${Protobuf_IMPORT_DIRS}) + get_filename_component(ABS_PATH ${DIR} ABSOLUTE) + list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) + if(${_contains_already} EQUAL -1) + list(APPEND _protobuf_include_path -I ${ABS_PATH}) + endif() + endforeach() + endif() + + set(${SRCS}) + set(${HDRS}) + foreach(FIL ${ARGN}) + get_filename_component(ABS_FIL ${FIL} ABSOLUTE) + get_filename_component(FIL_WE ${FIL} NAME_WE) + + list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc") + list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h") + + add_custom_command( + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc" + "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h" + COMMAND ${Protobuf_PROTOC_EXECUTABLE} + ARGS --pdcodegen_out=${CMAKE_CURRENT_BINARY_DIR} + --plugin=protoc-gen-pdcodegen=${CMAKE_CURRENT_BINARY_DIR}/pdcodegen + --proto_path=. + ${_protobuf_include_path} ${ABS_FIL} + DEPENDS ${ABS_FIL} ${Protobuf_PROTOC_EXECUTABLE} + COMMENT "Running Paddle-serving C++ protocol buffer compiler on ${FIL}" + VERBATIM) + endforeach() + + set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE) + set(${SRCS} ${${SRCS}} PARENT_SCOPE) + set(${HDRS} ${${HDRS}} PARENT_SCOPE) +endfunction() diff --git a/predictor/CMakeLists.txt b/predictor/CMakeLists.txt index 4896441e..e017c015 100644 --- a/predictor/CMakeLists.txt +++ b/predictor/CMakeLists.txt @@ -2,13 +2,41 @@ include(proto/CMakeLists.txt) include(common/CMakeLists.txt) include(op/CMakeLists.txt) include(framework/CMakeLists.txt) -add_library(predictor ${predictor_srcs}) +include(plugin/CMakeLists.txt) +include(src/CMakeLists.txt) + +add_executable(pdcodegen ${pdcodegen_srcs}) +target_link_libraries(pdcodegen protobuf ${PROTOBUF_PROTOC_LIBRARY}) +target_include_directories(pdcodegen PUBLIC + ${CMAKE_CURRENT_LIST_DIR}/ + ${CMAKE_CURRENT_BINARY_DIR}/) + +add_library(pdserving ${pdserving_srcs}) +set_source_files_properties( + ${pdserving_srcs} + PROPERTIES + COMPILE_FLAGS "-Wno-strict-aliasing -Wno-unused-variable -Wno-non-virtual-dtor -Wno-error=non-virtual-dtor -Wno-error=delete-non-virtual-dtor") +add_dependencies(pdserving protobuf boost brpc pdcodegen) + +target_include_directories(pdserving PUBLIC + ${CMAKE_CURRENT_LIST_DIR}/ + ${CMAKE_CURRENT_BINARY_DIR}/ + ${CMAKE_CURRENT_LIST_DIR}/../configure + ${CMAKE_CURRENT_LIST_DIR}/../mempool + ${CMAKE_CURRENT_LIST_DIR}/../spreg + ${CMAKE_CURRENT_LIST_DIR}/../ullib/include + ${CMAKE_CURRENT_BINARY_DIR}/../bsl/include) +target_link_libraries(pdserving -lpthread -lcrypto -lm -lrt -lssl -ldl -lz) + + +add_library(pdclient ${pdclient_srcs}) set_source_files_properties( - ${predictor_srcs} + ${pdclient_srcs} PROPERTIES COMPILE_FLAGS "-Wno-strict-aliasing -Wno-unused-variable -Wno-non-virtual-dtor -Wno-error=non-virtual-dtor -Wno-error=delete-non-virtual-dtor") -add_dependencies(predictor protobuf boost brpc) -target_include_directories(predictor PUBLIC +add_dependencies(pdclient protobuf boost brpc pdcodegen) + +target_include_directories(pdclient PUBLIC ${CMAKE_CURRENT_LIST_DIR}/ ${CMAKE_CURRENT_BINARY_DIR}/ ${CMAKE_CURRENT_LIST_DIR}/../configure @@ -16,3 +44,5 @@ target_include_directories(predictor PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../spreg ${CMAKE_CURRENT_LIST_DIR}/../ullib/include ${CMAKE_CURRENT_BINARY_DIR}/../bsl/include) +target_link_libraries(pdclient -lpthread -lcrypto -lm -lrt -lssl -ldl -lz) + diff --git a/predictor/common/CMakeLists.txt b/predictor/common/CMakeLists.txt index 7530df97..22728514 100644 --- a/predictor/common/CMakeLists.txt +++ b/predictor/common/CMakeLists.txt @@ -1,2 +1,3 @@ FILE(GLOB common_srcs ${CMAKE_CURRENT_LIST_DIR}/*.cpp) -LIST(APPEND predictor_srcs ${common_srcs}) +LIST(APPEND pdserving_srcs ${common_srcs}) +LIST(APPEND pdclient_srcs ${common_srcs}) diff --git a/predictor/framework/CMakeLists.txt b/predictor/framework/CMakeLists.txt index 29b29be4..641ba7ef 100644 --- a/predictor/framework/CMakeLists.txt +++ b/predictor/framework/CMakeLists.txt @@ -1,2 +1,3 @@ FILE(GLOB framework_srcs ${CMAKE_CURRENT_LIST_DIR}/*.cpp) -LIST(APPEND predictor_srcs ${framework_srcs}) +LIST(APPEND pdserving_srcs ${framework_srcs}) +LIST(APPEND pclient_srcs ${framework_srcs}) diff --git a/predictor/framework/logger.h b/predictor/framework/logger.h deleted file mode 100644 index d2c3a3bf..00000000 --- a/predictor/framework/logger.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef BAIDU_PADDLE_SERVING_PREDICTOR_LOGGER_H -#define BAIDU_PADDLE_SERVING_PREDICTOR_LOGGER_H - -#include - -namespace baidu { -namespace paddle_serving { -namespace predictor { - -class LoggerWrapper { -public: - static LoggerWrapper& instance() { - static LoggerWrapper lw; - return lw; - } - - int initialize(const std::string& path, const std::string& name) { - if (com_loadlog(path.c_str(), name.c_str()) != 0) { - LOG(ERROR) << "Fail to com_loadlog from: " - << path << "/" << name; - return -1; - } - return 0; - } - - int finalize() { - return 0; - } -}; - -} -} -} - -#endif diff --git a/predictor/op/CMakeLists.txt b/predictor/op/CMakeLists.txt index 191fe75d..28a5b060 100644 --- a/predictor/op/CMakeLists.txt +++ b/predictor/op/CMakeLists.txt @@ -1,2 +1,3 @@ FILE(GLOB op_srcs ${CMAKE_CURRENT_LIST_DIR}/*.cpp) -LIST(APPEND predictor_srcs ${op_srcs}) +LIST(APPEND pdserving_srcs ${op_srcs}) +LIST(APPEND pdclient_srcs ${op_srcs}) diff --git a/predictor/plugin/CMakeLists.txt b/predictor/plugin/CMakeLists.txt new file mode 100644 index 00000000..89ee09fb --- /dev/null +++ b/predictor/plugin/CMakeLists.txt @@ -0,0 +1,2 @@ +FILE(GLOB plugin_srcs ${CMAKE_CURRENT_LIST_DIR}/*.cc) +LIST(APPEND pdcodegen_srcs ${plugin_srcs}) diff --git a/predictor/proto/CMakeLists.txt b/predictor/proto/CMakeLists.txt index 0ad5320a..b24ff17e 100644 --- a/predictor/proto/CMakeLists.txt +++ b/predictor/proto/CMakeLists.txt @@ -1,3 +1,20 @@ -FILE(GLOB protofiles "${CMAKE_CURRENT_LIST_DIR}/*.proto") -protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${protofiles}) -LIST(APPEND predictor_srcs ${PROTO_SRCS}) +PROTOBUF_GENERATE_CPP(pdcodegen_proto_srcs pdcodegen_proto_hdrs + ${CMAKE_CURRENT_LIST_DIR}/pds_option.proto) + +LIST(APPEND pdcodegen_srcs ${pdcodegen_proto_srcs}) + +LIST(APPEND protofiles + ${CMAKE_CURRENT_LIST_DIR}/./builtin_format.proto + ${CMAKE_CURRENT_LIST_DIR}/./dense_service.proto + ${CMAKE_CURRENT_LIST_DIR}/./echo_service.proto + ${CMAKE_CURRENT_LIST_DIR}/./image_classification.proto + ${CMAKE_CURRENT_LIST_DIR}/./int64tensor_service.proto + ${CMAKE_CURRENT_LIST_DIR}/./msg_data.proto + ${CMAKE_CURRENT_LIST_DIR}/./sparse_service.proto + ${CMAKE_CURRENT_LIST_DIR}/./xrecord_format.proto +) + +# PROTOBUF_GENERATE_SERVING_CPP(PROTO_SRCS PROTO_HDRS ${protofiles}) +PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS ${protofiles}) +LIST(APPEND pdserving_srcs ${PROTO_SRCS} ${pdcodegen_proto_srcs}) +LIST(APPEND pdclient_srcs ${PROTO_SRCS} ${pdcodegen_proto_srcs}) diff --git a/predictor/src/CMakeLists.txt b/predictor/src/CMakeLists.txt new file mode 100644 index 00000000..428b6979 --- /dev/null +++ b/predictor/src/CMakeLists.txt @@ -0,0 +1,3 @@ +LIST(APPEND pdcodegen_srcs ${CMAKE_CURRENT_LIST_DIR}/pdcodegen.cpp) +LIST(APPEND pdserving_srcs ${CMAKE_CURRENT_LIST_DIR}/pdserving.cpp) +LIST(APPEND pdclient_srcs ${CMAKE_CURRENT_LIST_DIR}/pdclient.cpp) diff --git a/predictor/src/pdclient.cpp b/predictor/src/pdclient.cpp index 1731c201..128252dd 100644 --- a/predictor/src/pdclient.cpp +++ b/predictor/src/pdclient.cpp @@ -24,11 +24,9 @@ #include "common/utils.h" #include "common/inner_common.h" #include "common/constant.h" -#include "framework/logger.h" +#include "butil/logging.h" DEFINE_string(attachment, "foo", "Carry this along with requests"); -DEFINE_bool(auth, false, "Enable Giano authentication"); -DEFINE_string(auth_group, "g_guest", "Giano Group"); DEFINE_string(protocol, "baidu_std", "Protocol type. Defined in protocol/baidu/rpc/options.proto"); DEFINE_bool(compress, true, "Enable compression"); //DEFINE_string(protocol, "http", "Protocol type. Defined in protocol/baidu/rpc/options.proto"); @@ -42,7 +40,6 @@ DEFINE_string(http_content_type, "application/json", "Content type of http reque using baidu::paddle_serving::predictor::FLAGS_logger_path; using baidu::paddle_serving::predictor::FLAGS_logger_file; -using baidu::paddle_serving::predictor::LoggerWrapper; namespace dense_format { @@ -372,25 +369,8 @@ int main(int argc, char* argv[]) { google::ParseCommandLineFlags(&argc, &argv, true); // initialize logger instance - if (LoggerWrapper::instance().initialize( - FLAGS_logger_path, FLAGS_logger_file) != 0) { - LOG(ERROR) << "Failed initialize logger, conf:" - << FLAGS_logger_path << "/" << FLAGS_logger_file; - return -1; - } + google::InitGoogleLogging(strdup(argv[0])); - // Login to get `CredentialGenerator' (see baas-lib-c/baas.h for more - // information) and then pass it to `GianoAuthenticator'. - std::unique_ptr auth; - if (FLAGS_auth) { - if (baas::BAAS_Init() != 0) { - LOG(ERROR) << "Fail to init BAAS"; - return -1; - } - baas::CredentialGenerator gen = baas::ClientUtility::Login(FLAGS_auth_group); - auth.reset(new brpc::policy::GianoAuthenticator(&gen, NULL)); - } - // A Channel represents a communication line to a Server. Notice that // Channel is thread-safe and can be shared by all threads in your program. brpc::Channel channel; @@ -399,7 +379,6 @@ int main(int argc, char* argv[]) { brpc::ChannelOptions options; options.protocol = FLAGS_protocol; options.connection_type = FLAGS_connection_type; - options.auth = auth.get(); options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/; options.max_retry = FLAGS_max_retry; if (channel.Init(FLAGS_server.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) { @@ -441,6 +420,8 @@ int main(int argc, char* argv[]) { } LOG(INFO) << "Pdserving Client is going to quit"; + + google::ShutdownGoogleLogging(); return 0; } diff --git a/predictor/src/pdserving.cpp b/predictor/src/pdserving.cpp index 5f2088d9..cc257048 100644 --- a/predictor/src/pdserving.cpp +++ b/predictor/src/pdserving.cpp @@ -1,18 +1,17 @@ #include #include -#include // bthread_set_worker_startfn +#include // bthread_set_worker_startfn #include "common/inner_common.h" #include "framework/workflow.h" #include "framework/service.h" #include "framework/manager.h" #include "framework/server.h" -#include "framework/logger.h" +#include "butil/logging.h" #include "framework/resource.h" #include "common/constant.h" using baidu::paddle_serving::predictor::ServerManager; using baidu::paddle_serving::predictor::WorkflowManager; -using baidu::paddle_serving::predictor::LoggerWrapper; using baidu::paddle_serving::predictor::InferServiceManager; using baidu::paddle_serving::predictor::Resource; using baidu::paddle_serving::predictor::FLAGS_workflow_path; @@ -40,7 +39,7 @@ void print_revision(std::ostream& os, void*) { static bvar::PassiveStatus s_predictor_revision( "predictor_revision", print_revision, NULL); -DEFINE_bool(v, false, "print version, bool"); +DEFINE_bool(V, false, "print version, bool"); DEFINE_bool(g, false, "user defined gflag path"); DECLARE_string(flagfile); @@ -72,7 +71,7 @@ int main(int argc, char** argv) { #endif google::ParseCommandLineFlags(&argc, &argv, true); - if (FLAGS_v) { + if (FLAGS_V) { print_revision(std::cout, NULL); std::cout << std::flush; return 0; @@ -87,12 +86,7 @@ int main(int argc, char** argv) { g_change_server_port(); // initialize logger instance - if (LoggerWrapper::instance().initialize( - FLAGS_logger_path, FLAGS_logger_file) != 0) { - LOG(ERROR) << "Failed initialize logger, conf:" - << FLAGS_logger_path << "/" << FLAGS_logger_file; - return -1; - } + google::InitGoogleLogging(strdup(argv[0])); LOG(INFO) << "Succ initialize logger"; @@ -149,10 +143,7 @@ int main(int argc, char** argv) { LOG(ERROR) << "Failed finalize resource manager"; } - if (LoggerWrapper::instance().finalize() != 0) { - LOG(ERROR) << "Failed finalize logger wrapper"; - } - + google::ShutdownGoogleLogging(); LOG(INFO) << "Paddle Inference Server exit successfully!"; return 0; } -- GitLab