!40504 [lite]java和jni可以将日志保存到文件中_master

Merge pull request !40504 from wangtongyu6/java_jni_log_master
This commit is contained in:
i-robot 2022-09-05 11:17:49 +00:00 committed by Gitee
commit 8036533fca
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
16 changed files with 157 additions and 28 deletions

View File

@ -869,6 +869,8 @@ else()
if(MSLITE_ENABLE_RUNTIME_GLOG)
install(FILES ${glog_LIBPATH}/libmindspore_glog.so.0.4.0 DESTINATION ${GLOG_DIR} RENAME libmindspore_glog.so.0
COMPONENT ${RUNTIME_COMPONENT_NAME})
install(DIRECTORY ${glog_LIBPATH}/../include/glog/ DESTINATION ${RUNTIME_INC_DIR}/third_party/glog
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h")
endif()
if(MSLITE_ENABLE_CONVERTER)
install(FILES ${TOP_DIR}/mindspore/lite/include/kernel_interface.h DESTINATION ${CONVERTER_ROOT_DIR}/include

View File

@ -42,8 +42,9 @@
#undef SM_DEBUG
// NOTICE: when relative path of 'log_adapter.h' changed, macro 'LOG_HDR_FILE_REL_PATH' must be changed
#ifndef LOG_HDR_FILE_REL_PATH
#define LOG_HDR_FILE_REL_PATH "mindspore/core/utils/log_adapter.h"
#endif
// Get start index of file relative path in __FILE__
static constexpr size_t GetRelPathPos() noexcept {
return sizeof(__FILE__) > sizeof(LOG_HDR_FILE_REL_PATH) ? sizeof(__FILE__) - sizeof(LOG_HDR_FILE_REL_PATH) : 0;

View File

@ -85,10 +85,29 @@ build_lite_jni_and_jar() {
cp ./${PKG_NAME}/runtime/third_party/libjpeg-turbo/lib/*.so* ${LITE_JAVA_PATH}/java/${NATIVE_PATH_ARCH}/libs/
cp ./${PKG_NAME}/runtime/third_party/libjpeg-turbo/lib/*.so* ${LITE_JAVA_PATH}/native/libs/${NATIVE_PATH_ARCH}/
fi
# build jni so
# prepare
cd ${BASEPATH}/mindspore/lite/build
rm -rf java/jni && mkdir -pv java/jni
cd java/jni
# copy glog lib and headers
LIB_GLOG="libmindspore_glog.so*"
if [[ ${MSLITE_ENABLE_RUNTIME_GLOG} == "on" || ${MSLITE_ENABLE_RUNTIME_GLOG} == "ON" || ${MSLITE_ENABLE_SERVER_INFERENCE} == "on" || ${MSLITE_ENABLE_SERVER_INFERENCE} == "ON" ]]; then
if [ -f "`echo ${INSTALL_PREFIX}/${PKG_NAME}/runtime/third_party/glog/${LIB_GLOG}`" ]; then
cp ${INSTALL_PREFIX}/${PKG_NAME}/runtime/third_party/glog/*.so* ${LITE_JAVA_PATH}/java/${NATIVE_PATH_ARCH}/libs/
cp ${INSTALL_PREFIX}/${PKG_NAME}/runtime/third_party/glog/*.so* ${LITE_JAVA_PATH}/native/libs/${NATIVE_PATH_ARCH}/
else
echo "no glog lib found, exit."
exit 1
fi
if [ -d "${BASEPATH}/output/tmp/${PKG_NAME}/runtime/include/third_party/glog" ]; then
rm -rf jni_include && mkdir jni_include
cp ${BASEPATH}/output/tmp/${PKG_NAME}/runtime/include/third_party/glog ./jni_include -r
else
echo "no glog hesders found, exit."
exit 1
fi
fi
# build jni so
echo "cmake ${JNI_CMAKE_ARGS} -DSUPPORT_TRAIN=${is_train} ${LITE_JAVA_PATH}/native/"
cmake ${JNI_CMAKE_ARGS} -DSUPPORT_TRAIN=${is_train} "${LITE_JAVA_PATH}/native/"
make -j$THREAD_NUM
@ -101,7 +120,6 @@ build_lite_jni_and_jar() {
cp ./libmindspore-lite-jni.so ${LITE_JAVA_PATH}/native/libs/${NATIVE_PATH_ARCH}/
cp ./libmindspore-lite-jni.so ${INSTALL_PREFIX}/${PKG_NAME}/runtime/lib/
cp ${BASEPATH}/output/tmp/${PKG_NAME}/runtime/lib/*.so ${LITE_JAVA_PATH}/src/main/resources/com/mindspore/lite/${RESOURCE_PATH_ARCH}/
LIB_GLOG="libmindspore_glog.so*"
if [ -f "`echo ${BASEPATH}/output/tmp/${PKG_NAME}/runtime/third_party/glog/${LIB_GLOG}`" ]; then
cp ${BASEPATH}/output/tmp/${PKG_NAME}/runtime/third_party/glog/libmindspore_glog.so* ${LITE_JAVA_PATH}/src/main/resources/com/mindspore/lite/${RESOURCE_PATH_ARCH}/libmindspore_glog.so
fi

View File

@ -14,6 +14,14 @@ if(PLATFORM_ARM64 OR PLATFORM_ARM32)
add_compile_definitions(PLATFORM_ARM)
endif()
if(CMAKE_BUILD_TYPE)
if("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
message("build release")
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_C_VISIBILITY_PRESET hidden)
endif()
endif()
if(PLATFORM_ARM32 OR PLATFORM_ARM64)
#for performance
if("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
@ -32,12 +40,18 @@ set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
if(DEFINED ENV{MSLITE_ENABLE_RUNTIME_GLOG})
set(USE_GLOG on)
endif()
if(DEFINED ENV{MSLITE_ENABLE_SERVER_INFERENCE})
set(MSLITE_ENABLE_PARALLEL_INFERENCE on)
set(USE_GLOG on)
endif()
if(DEFINED ENV{MSLITE_ENABLE_PARALLEL_INFERENCE})
set(MSLITE_ENABLE_PARALLEL_INFERENCE $ENV{MSLITE_ENABLE_PARALLEL_INFERENCE})
set(USE_GLOG on)
endif()
if(ENABLE_VERBOSE)
@ -56,20 +70,21 @@ set(LITE_DIR ${TOP_DIR}/mindspore/lite)
set(NEW_NATIVE_DIR ${LITE_DIR}/java/src/main/native)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../src)##for common/log.h
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../src)##for common/log_adapter.h
include_directories(${LITE_DIR}) ## lite include
include_directories(${TOP_DIR}) ## api include
include_directories(${TOP_DIR}/mindspore/core/) ## core include
include_directories(${LITE_DIR}/build) ## flatbuffers
if(PLATFORM_ARM64 OR PLATFORM_ARM32)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/)
set(PLATFORM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/)
elseif(MACHINE_LINUX_ARM64)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/libs/linux_aarch64/)
set(PLATFORM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/linux_aarch64/)
else()
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/libs/linux_x86/)
set(PLATFORM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/linux_x86/)
endif()
link_directories(${PLATFORM_DIR})
set(JAVA_HOME "$ENV{JAVA_HOME}")
include_directories(${JAVA_HOME}/include/) ## jni
@ -82,8 +97,7 @@ set(JNI_SRC
${NEW_NATIVE_DIR}/ms_context.cpp
${NEW_NATIVE_DIR}/ms_tensor.cpp
${NEW_NATIVE_DIR}/version.cpp
${TOP_DIR}/mindspore/lite/src/common/log.cc
)
)
if(MSLITE_ENABLE_PARALLEL_INFERENCE)
set(JNI_SRC
@ -93,10 +107,28 @@ if(MSLITE_ENABLE_PARALLEL_INFERENCE)
)
endif()
if(USE_GLOG)
set_property(SOURCE ${JNI_SRC} PROPERTY COMPILE_DEFINITIONS
LOG_HDR_FILE_REL_PATH="mindspore/lite/java/native/../../../../mindspore/core/utils/log_adapter.h")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/securec/include)
include_directories(${CMAKE_BINARY_DIR}/jni_include)
add_definitions(-DUSE_GLOG)
set(JNI_SRC
${JNI_SRC}
${TOP_DIR}/mindspore/core/utils/log_adapter.cc
)
else()
set(JNI_SRC
${JNI_SRC}
${TOP_DIR}/mindspore/lite/src/common/log.cc
)
set_property(SOURCE ${JNI_SRC} PROPERTY COMPILE_DEFINITIONS
LITE_LOG_HEAR_FILE_REL_PATH="mindspore/lite/java/native/../../src/common/log.h")
endif()
set(LITE_SO_NAME mindspore-lite)
set_property(SOURCE ${JNI_SRC} PROPERTY COMPILE_DEFINITIONS
LITE_LOG_HEAR_FILE_REL_PATH="mindspore/lite/java/native/../../src/common/log.h")
add_library(mindspore-lite-jni SHARED ${JNI_SRC})
if(PLATFORM_ARM64 OR PLATFORM_ARM32)
@ -104,22 +136,53 @@ if(PLATFORM_ARM64 OR PLATFORM_ARM32)
target_link_libraries(mindspore-lite-jni ${LITE_SO_NAME} ${log-lib})
else()
target_link_libraries(mindspore-lite-jni ${LITE_SO_NAME})
if(USE_GLOG)
find_library(GLOG_LIB
NAMES mindspore_glog libmindspore_glog.so.0
PATHS ${PLATFORM_DIR}
)
if(GLOG_LIB)
message("Found glog lib :${GLOG_LIB}")
else()
message(FATAL_ERROR "No glog lib found, exit.")
endif()
target_link_libraries(mindspore-lite-jni -Wl,--no-as-needed ${GLOG_LIB} -Wl,--as-needed)
endif()
endif()
if(SUPPORT_TRAIN)
set(LITE_TRAIN_SO_NAME mindspore-lite-train)
set(JNI_TRAIN_SRC
${NEW_NATIVE_DIR}/train_config.cpp
${TOP_DIR}/mindspore/lite/src/common/log.cc
)
set_property(SOURCE ${JNI_TRAIN_SRC} PROPERTY COMPILE_DEFINITIONS
LITE_LOG_HEAR_FILE_REL_PATH="mindspore/lite/java/native/../../src/common/log.h")
${NEW_NATIVE_DIR}/train_config.cpp
)
if(USE_GLOG)
set_property(SOURCE ${JNI_SRC} PROPERTY COMPILE_DEFINITIONS
LOG_HDR_FILE_REL_PATH="mindspore/lite/java/native/../../../../mindspore/core/utils/log_adapter.h")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/securec/include)
include_directories(${CMAKE_BINARY_DIR}/jni_include)
add_definitions(-DUSE_GLOG)
set(JNI_TRAIN_SRC
${JNI_TRAIN_SRC}
${TOP_DIR}/mindspore/core/utils/log_adapter.cc
)
else()
set(JNI_TRAIN_SRC
${JNI_TRAIN_SRC}
${TOP_DIR}/mindspore/lite/src/common/log.cc
)
set_property(SOURCE ${JNI_TRAIN_SRC} PROPERTY COMPILE_DEFINITIONS
LITE_LOG_HEAR_FILE_REL_PATH="mindspore/lite/java/native/../../src/common/log.h")
endif()
add_library(mindspore-lite-train-jni SHARED ${JNI_TRAIN_SRC})
if(PLATFORM_ARM64 OR PLATFORM_ARM32)
find_library(log-lib log)
target_link_libraries(mindspore-lite-train-jni -Wl,--no-as-needed ${LITE_TRAIN_SO_NAME} -Wl,--as-needed
${log-lib})
else()
if(USE_GLOG)
target_link_libraries(mindspore-lite-train-jni -Wl,--no-as-needed ${GLOG_LIB} -Wl,--as-needed)
endif()
target_link_libraries(mindspore-lite-train-jni -Wl,--no-as-needed ${LITE_TRAIN_SO_NAME} -Wl,--as-needed)
endif()
endif()

View File

@ -19,7 +19,7 @@
#include <cstdlib>
#include <climits>
#include <memory>
#include "common/log.h"
#include "common/log_adapter.h"
std::string RealPath(const char *path) {
if (path == nullptr) {
@ -47,3 +47,13 @@ std::string RealPath(const char *path) {
std::string res = resolved_path.get();
return res;
}
#if defined(USE_GLOG) || defined(BUILD_CORE_RUNTIME)
extern "C" {
extern void mindspore_log_init();
}
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
mindspore_log_init();
return JNI_VERSION_1_4;
}
#endif

View File

@ -22,7 +22,7 @@ import java.util.logging.Logger;
public class MSContext {
private static final Logger LOGGER = Logger.getLogger(MSContext.class.toString());
private static Logger LOGGER = MindsporeLite.GetLogger();
static {
MindsporeLite.init();
}

View File

@ -1,9 +1,44 @@
package com.mindspore.config;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public final class MindsporeLite {
private static final Logger LOGGER = Logger.getLogger(MindsporeLite.class.toString());
private static final Object lock = new Object();
private static Logger LOGGER = GetLogger();
public static Logger GetLogger() {
if (LOGGER == null) {
synchronized (lock) {
if (LOGGER != null) {
return LOGGER;
}
LOGGER = Logger.getLogger(MindsporeLite.class.toString());
String logtostderr = System.getenv("GLOG_logtostderr");
if ("0".equals(logtostderr)) {
String GLOG_log_dir = System.getenv("GLOG_log_dir");
if (GLOG_log_dir != null && !"".equals(GLOG_log_dir)) {
FileHandler fileHandler = null;
try {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuuMMdd-HHmmss.SSSS");
LocalDateTime now = LocalDateTime.now();
fileHandler = new FileHandler(GLOG_log_dir + "/MinsSporeLite.java_log." + dtf.format(now) + ".log");
} catch (IOException e) {
e.printStackTrace();
}
SimpleFormatter formatter = new SimpleFormatter();
fileHandler.setFormatter(formatter);
LOGGER.addHandler(fileHandler);
}
}
}
}
return LOGGER;
}
/**
* Init function.

View File

@ -7,7 +7,7 @@ import java.io.InputStream;
import java.util.logging.Logger;
public class NativeLibrary {
private static final Logger LOGGER = Logger.getLogger(NativeLibrary.class.toString());
private static final Logger LOGGER = MindsporeLite.GetLogger();
private static final String GLOG_LIBNAME = "mindspore_glog";
private static final String JPEG_LIBNAME = "jpeg";

View File

@ -24,7 +24,7 @@ import java.util.logging.Logger;
* @since v1.0
*/
public class Version {
private static final Logger LOGGER = Logger.getLogger(Version.class.toString());
private static final Logger LOGGER = MindsporeLite.GetLogger();
static {
LOGGER.info("Version init ...");
init();

View File

@ -15,7 +15,7 @@
*/
#include <jni.h>
#include "common/log.h"
#include "common/log_adapter.h"
#include "include/api/graph.h"
#include "include/api/serialization.h"
#include "include/api/types.h"

View File

@ -16,7 +16,7 @@
#include "include/api/model.h"
#include <jni.h>
#include "common/log.h"
#include "common/log_adapter.h"
#include "include/api/serialization.h"
extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_Model_buildByGraph(JNIEnv *env, jobject thiz, jlong graph_ptr,

View File

@ -15,7 +15,7 @@
*/
#include <jni.h>
#include "common/log.h"
#include "common/log_adapter.h"
#include "include/api/model_parallel_runner.h"
#include "include/api/context.h"

View File

@ -15,7 +15,7 @@
*/
#include <jni.h>
#include "common/log.h"
#include "common/log_adapter.h"
#include "include/api/context.h"
#include "common/jni_utils.h"

View File

@ -16,7 +16,7 @@
#include <jni.h>
#include <cstring>
#include "common/log.h"
#include "common/log_adapter.h"
#include "include/api/types.h"
extern "C" JNIEXPORT jintArray JNICALL Java_com_mindspore_MSTensor_getShape(JNIEnv *env, jobject thiz,

View File

@ -15,7 +15,7 @@
*/
#include <jni.h>
#include "common/log.h"
#include "common/log_adapter.h"
#include "include/api/model_parallel_runner.h"
extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_config_RunnerConfig_createRunnerConfig(JNIEnv *env,

View File

@ -15,7 +15,7 @@
*/
#include <jni.h>
#include "common/log.h"
#include "common/log_adapter.h"
#include "include/api/types.h"
#include "include/api/cfg.h"